From owner-svn-src-head@FreeBSD.ORG Fri Feb 27 21:15:14 2015 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 0778D2C6; Fri, 27 Feb 2015 21:15:14 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id CD7ACACB; Fri, 27 Feb 2015 21:15:13 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id t1RLFDEB007283; Fri, 27 Feb 2015 21:15:13 GMT (envelope-from imp@FreeBSD.org) Received: (from imp@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id t1RLFDfw007282; Fri, 27 Feb 2015 21:15:13 GMT (envelope-from imp@FreeBSD.org) Message-Id: <201502272115.t1RLFDfw007282@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: imp set sender to imp@FreeBSD.org using -f From: Warner Losh Date: Fri, 27 Feb 2015 21:15:13 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r279373 - head/sys/kern X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 27 Feb 2015 21:15:14 -0000 Author: imp Date: Fri Feb 27 21:15:12 2015 New Revision: 279373 URL: https://svnweb.freebsd.org/changeset/base/279373 Log: Make sched_random() return an unsigned number, and use uint32_t consistently. This also matches the per-cpu pointer declaration anyway. This changes the tweak we give to the load from -32..31 to be 0..31 which seems more inline with the rest of the code (- rnd and the -= 64). It should also provide the randomness we need, and may fix a signedness bug in the old code (it isn't clear that the effect was intentional as opposed to sloppy, and the right shift of a signed value is undefined to boot). This stores sched_balance() behavior when it used random(). Differential Revision: https://reviews.freebsd.org/D1981 Modified: head/sys/kern/sched_ule.c Modified: head/sys/kern/sched_ule.c ============================================================================== --- head/sys/kern/sched_ule.c Fri Feb 27 20:32:50 2015 (r279372) +++ head/sys/kern/sched_ule.c Fri Feb 27 21:15:12 2015 (r279373) @@ -360,17 +360,19 @@ SDT_PROBE_DEFINE2(sched, , , surrender, /* * We need some randomness. Implement the classic Linear Congruential * generator X_{n+1}=(aX_n+c) mod m. These values are optimized for - * m = 2^32, a = 69069 and c = 5. This is signed so that we can get - * both positive and negative values from it by shifting the value - * right. - */ -static int sched_random(void) -{ - int rnd, *rndptr; - rndptr = DPCPU_PTR(randomval); - rnd = *rndptr * 69069 + 5; - *rndptr = rnd; - return(rnd); + * m = 2^32, a = 69069 and c = 5. We only return the upper 16 bits + * of the random state (in the low bits of our answer) to return + * the maximum randomness. + */ +static uint32_t +sched_random() +{ + uint32_t *rndptr; + + rndptr = DPCPU_PTR(randomval); + *rndptr = *rndptr * 69069 + 5; + + return (*rndptr >> 16); } #endif @@ -718,7 +720,7 @@ cpu_search(const struct cpu_group *cg, s CPU_CLR(cpu, &cpumask); tdq = TDQ_CPU(cpu); load = tdq->tdq_load * 256; - rnd = sched_random() >> 26; /* -32 to +31 */ + rnd = sched_random() % 32; if (match & CPU_SEARCH_LOWEST) { if (cpu == low->cs_prefer) load -= 64; @@ -882,7 +884,7 @@ sched_balance(void) return; balance_ticks = max(balance_interval / 2, 1) + - ((sched_random() >> 16) % balance_interval); + (sched_random() % balance_interval); tdq = TDQ_SELF(); TDQ_UNLOCK(tdq); sched_balance_group(cpu_top);