diff options
author | Ingo Molnar <mingo@elte.hu> | 2009-03-12 10:15:31 -0400 |
---|---|---|
committer | Sam Ravnborg <sam@ravnborg.org> | 2009-03-15 18:02:07 -0400 |
commit | b0fe551000179c868d46266278a890eab878baca (patch) | |
tree | 2881090d0a3da1508dc51abffe358406e0527fe1 /scripts | |
parent | 184832c981fd38b1052f2dfa0262e793fa6c67d3 (diff) |
kconfig: improve seed in randconfig
'make randconfig' uses glibc's rand function, and the seed of
that PRNG is set via:
srand(time(NULL));
But 'time()' only increases once every second - freezing the
randconfig result within a single second.
My Nehalem testbox does randconfig much faster than 1 second
and i have a few scripts that do 'randconfig until condition X'
loops.
Those scripts currently waste a lot of CPU time due to randconfig
changing its seed only once per second currently.
Change the seed to be micrseconds based. (I checked the statistical
spread of the seed - the now.tv_sec*now.tv_usec multiplication
there further improves it.)
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Cc: Roman Zippel <zippel@linux-m68k.org>
[sam: fix for systems where usec is zero - noticed by Geert Uytterhoeven]
Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/kconfig/conf.c | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/scripts/kconfig/conf.c b/scripts/kconfig/conf.c index 3e1057f885c6..d190092c3b6e 100644 --- a/scripts/kconfig/conf.c +++ b/scripts/kconfig/conf.c | |||
@@ -11,6 +11,7 @@ | |||
11 | #include <time.h> | 11 | #include <time.h> |
12 | #include <unistd.h> | 12 | #include <unistd.h> |
13 | #include <sys/stat.h> | 13 | #include <sys/stat.h> |
14 | #include <sys/time.h> | ||
14 | 15 | ||
15 | #define LKC_DIRECT_LINK | 16 | #define LKC_DIRECT_LINK |
16 | #include "lkc.h" | 17 | #include "lkc.h" |
@@ -464,9 +465,22 @@ int main(int ac, char **av) | |||
464 | input_mode = set_yes; | 465 | input_mode = set_yes; |
465 | break; | 466 | break; |
466 | case 'r': | 467 | case 'r': |
468 | { | ||
469 | struct timeval now; | ||
470 | unsigned int seed; | ||
471 | |||
472 | /* | ||
473 | * Use microseconds derived seed, | ||
474 | * compensate for systems where it may be zero | ||
475 | */ | ||
476 | gettimeofday(&now, NULL); | ||
477 | |||
478 | seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1)); | ||
479 | srand(seed); | ||
480 | |||
467 | input_mode = set_random; | 481 | input_mode = set_random; |
468 | srand(time(NULL)); | ||
469 | break; | 482 | break; |
483 | } | ||
470 | case 'h': | 484 | case 'h': |
471 | printf(_("See README for usage info\n")); | 485 | printf(_("See README for usage info\n")); |
472 | exit(0); | 486 | exit(0); |