diff mbox

[FFmpeg-devel] libavutil/random_seed: Ensure that get_generic_seed() spends at least 1/32 sec gathering entropy

Message ID 20161228233457.12906-1-michael@niedermayer.cc
State Accepted
Commit 7525517593dff427021c893a1b21333510b7803b
Headers show

Commit Message

Michael Niedermayer Dec. 28, 2016, 11:34 p.m. UTC
This may fix the failures on windows

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavutil/random_seed.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

James Almer Dec. 29, 2016, 2:37 a.m. UTC | #1
On 12/28/2016 8:34 PM, Michael Niedermayer wrote:
> This may fix the failures on windows

Not sure what failures this is about, but compiling with mingw-w64 x86_64 (GCC
6.2.0) and running on a Haswell i5 on Win10 x86_64 i got this before this patch

$ time libavutil/tests/random_seed.exe
seeds OK
seeds OK

real    0m2.082s
user    0m0.000s
sys     0m0.000s

And this after

$ time libavutil/tests/random_seed.exe
seeds OK
seeds OK

real    0m8.100s
user    0m0.000s
sys     0m0.015s

> 
> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> ---
>  libavutil/random_seed.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c
> index 0c58c8ffa1..d1c9a3f704 100644
> --- a/libavutil/random_seed.c
> +++ b/libavutil/random_seed.c
> @@ -68,6 +68,7 @@ static uint32_t get_generic_seed(void)
>      struct AVSHA *sha = (void*)tmp;
>      clock_t last_t  = 0;
>      clock_t last_td = 0;
> +    clock_t init_t = 0;
>      static uint64_t i = 0;
>      static uint32_t buffer[512] = { 0 };
>      unsigned char digest[20];
> @@ -93,10 +94,13 @@ static uint32_t get_generic_seed(void)
>          } else {
>              last_td = t - last_t;
>              buffer[++i & 511] += last_td % 3294638521U;
> -            if (last_i && i - last_i > 4 || i - last_i > 64 || TEST && i - last_i > 8)
> -                break;
> +            if ((t - init_t) >= CLOCKS_PER_SEC>>5)
> +                if (last_i && i - last_i > 4 || i - last_i > 64 || TEST && i - last_i > 8)
> +                    break;
>          }
>          last_t = t;
> +        if (!init_t)
> +            init_t = t;
>      }
>  
>      if(TEST) {
>
Hendrik Leppkes Dec. 29, 2016, 10:13 a.m. UTC | #2
On Thu, Dec 29, 2016 at 3:37 AM, James Almer <jamrial@gmail.com> wrote:
> On 12/28/2016 8:34 PM, Michael Niedermayer wrote:
>> This may fix the failures on windows
>
> Not sure what failures this is about

This failure, which keeps popping up sporadically since
c4152fc42e480c41efb7f761b1bbe5f0bc43d5bc, because Windows uses
CLOCKS_PER_SEC=1000

http://fate.ffmpeg.org/report.cgi?time=20161228142737&slot=x86_64-msvc14-windows-native

- Hendrik
Hendrik Leppkes Dec. 29, 2016, 10:16 a.m. UTC | #3
On Thu, Dec 29, 2016 at 11:13 AM, Hendrik Leppkes <h.leppkes@gmail.com> wrote:
> On Thu, Dec 29, 2016 at 3:37 AM, James Almer <jamrial@gmail.com> wrote:
>> On 12/28/2016 8:34 PM, Michael Niedermayer wrote:
>>> This may fix the failures on windows
>>
>> Not sure what failures this is about
>
> This failure, which keeps popping up sporadically since
> c4152fc42e480c41efb7f761b1bbe5f0bc43d5bc, because Windows uses
> CLOCKS_PER_SEC=1000
>
> http://fate.ffmpeg.org/report.cgi?time=20161228142737&slot=x86_64-msvc14-windows-native
>

Its not isolated to windows either, OSX/Darwin has the same issue:
http://fate.ffmpeg.org/report.cgi?time=20161229055656&slot=i386-darwin-clang-3.8-O3
http://fate.ffmpeg.org/report.cgi?time=20161229052603&slot=i386-darwin-gcc-6

- Hendrik
Michael Niedermayer Dec. 31, 2016, 4:14 p.m. UTC | #4
On Thu, Dec 29, 2016 at 12:34:57AM +0100, Michael Niedermayer wrote:
> This may fix the failures on windows
> 
> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> ---
>  libavutil/random_seed.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)

as noone seems to say if it fixes the issue or not ill apply it to
see if the fate failures disappear afterwards or not

[...]
diff mbox

Patch

diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c
index 0c58c8ffa1..d1c9a3f704 100644
--- a/libavutil/random_seed.c
+++ b/libavutil/random_seed.c
@@ -68,6 +68,7 @@  static uint32_t get_generic_seed(void)
     struct AVSHA *sha = (void*)tmp;
     clock_t last_t  = 0;
     clock_t last_td = 0;
+    clock_t init_t = 0;
     static uint64_t i = 0;
     static uint32_t buffer[512] = { 0 };
     unsigned char digest[20];
@@ -93,10 +94,13 @@  static uint32_t get_generic_seed(void)
         } else {
             last_td = t - last_t;
             buffer[++i & 511] += last_td % 3294638521U;
-            if (last_i && i - last_i > 4 || i - last_i > 64 || TEST && i - last_i > 8)
-                break;
+            if ((t - init_t) >= CLOCKS_PER_SEC>>5)
+                if (last_i && i - last_i > 4 || i - last_i > 64 || TEST && i - last_i > 8)
+                    break;
         }
         last_t = t;
+        if (!init_t)
+            init_t = t;
     }
 
     if(TEST) {