diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2015-12-11 22:52:32 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2015-12-11 22:52:32 +0000 |
commit | 515f8df3f16fe74a5a1032f81a23556bede27537 (patch) | |
tree | 957e594553fb7d1a5007c8c3adb7ab1f3536b30a /llvm/lib/Support/Unix/Process.inc | |
parent | 9a58133698c831321785863c83f69ec1145cfb69 (diff) | |
download | bcm5719-llvm-515f8df3f16fe74a5a1032f81a23556bede27537.tar.gz bcm5719-llvm-515f8df3f16fe74a5a1032f81a23556bede27537.zip |
Avoid buffered reads of /dev/urandom
I am seeing disappointing clang performance on a large PowerPC64
Linux box. GetRandomNumberSeed() does a buffered read from
/dev/urandom to seed its PRNG. As a result we read an entire page
even though we only need 4 bytes.
With every clang task reading a page worth of /dev/urandom we
end up spending a large amount of time stuck on kernel spinlock.
Patch by Anton Blanchard!
llvm-svn: 255386
Diffstat (limited to 'llvm/lib/Support/Unix/Process.inc')
-rw-r--r-- | llvm/lib/Support/Unix/Process.inc | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/llvm/lib/Support/Unix/Process.inc b/llvm/lib/Support/Unix/Process.inc index df13bd22173..27083eeb072 100644 --- a/llvm/lib/Support/Unix/Process.inc +++ b/llvm/lib/Support/Unix/Process.inc @@ -430,13 +430,18 @@ const char *Process::ResetColor() { #if !defined(HAVE_DECL_ARC4RANDOM) || !HAVE_DECL_ARC4RANDOM static unsigned GetRandomNumberSeed() { // Attempt to get the initial seed from /dev/urandom, if possible. - if (FILE *RandomSource = ::fopen("/dev/urandom", "r")) { + int urandomFD = open("/dev/urandom", O_RDONLY); + + if (urandomFD != -1) { unsigned seed; - int count = ::fread((void *)&seed, sizeof(seed), 1, RandomSource); - ::fclose(RandomSource); + // Don't use a buffered read to avoid reading more data + // from /dev/urandom than we need. + int count = read(urandomFD, (void *)&seed, sizeof(seed)); + + close(urandomFD); // Return the seed if the read was successful. - if (count == 1) + if (count == sizeof(seed)) return seed; } |