Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/kern Replace random with cprng_fast32. Reorganise comput...



details:   https://anonhg.NetBSD.org/src/rev/a4663927ecc9
branches:  trunk
changeset: 802286:a4663927ecc9
user:      joerg <joerg%NetBSD.org@localhost>
date:      Mon Sep 08 22:01:24 2014 +0000

description:
Replace random with cprng_fast32. Reorganise computation to replace
(32bit) division with (long) multiplication.

diffstat:

 sys/kern/vfs_bio.c |  21 ++++++++++-----------
 1 files changed, 10 insertions(+), 11 deletions(-)

diffs (58 lines):

diff -r 97829c8a3a32 -r a4663927ecc9 sys/kern/vfs_bio.c
--- a/sys/kern/vfs_bio.c        Mon Sep 08 20:52:37 2014 +0000
+++ b/sys/kern/vfs_bio.c        Mon Sep 08 22:01:24 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vfs_bio.c,v 1.251 2014/09/05 05:57:21 matt Exp $       */
+/*     $NetBSD: vfs_bio.c,v 1.252 2014/09/08 22:01:24 joerg Exp $      */
 
 /*-
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -123,7 +123,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.251 2014/09/05 05:57:21 matt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.252 2014/09/08 22:01:24 joerg Exp $");
 
 #include "opt_bufcache.h"
 
@@ -143,6 +143,7 @@
 #include <sys/cpu.h>
 #include <sys/wapbl.h>
 #include <sys/bitops.h>
+#include <sys/cprng.h>
 
 #include <uvm/uvm.h>   /* extern struct uvm uvm */
 
@@ -532,7 +533,7 @@
 static int
 buf_lotsfree(void)
 {
-       int guess, thresh;
+       u_long guess;
 
        /* Always allocate if less than the low water mark. */
        if (bufmem < bufmem_lowater)
@@ -548,16 +549,14 @@
 
        /*
         * The probabily of getting a new allocation is inversely
-        * proportional to the current size of the cache, using
-        * a granularity of 16 steps.
+        * proportional  to the current size of the cache above
+        * the low water mark.  Divide the total first to avoid overflows
+        * in the product.
         */
-       guess = random() & 0x0000000fL;
+       guess = cprng_fast32() % 16;
 
-       /* Don't use "16 * bufmem" here to avoid a 32-bit overflow. */
-       thresh = (bufmem - bufmem_lowater) /
-           ((bufmem_hiwater - bufmem_lowater) / 16);
-
-       if (guess >= thresh)
+       if ((bufmem_hiwater - bufmem_lowater) / 16 * guess >=
+           (bufmem - bufmem_lowater))
                return 1;
 
        /* Otherwise don't allocate. */



Home | Main Index | Thread Index | Old Index