mirror of
https://github.com/gperftools/gperftools
synced 2024-12-22 15:22:07 +00:00
[benchmark] detect iterations overflow
If for some reason benchmark function is too fast (like when it got optimized out to nothing), we'd previously hang in infinite loop. Now we'll catch this condition due to iterations count overflowing.
This commit is contained in:
parent
e003e91b74
commit
ff68bcab60
@ -27,6 +27,7 @@
|
||||
|
||||
#include "run_benchmark.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -80,10 +81,17 @@ static double run_benchmark(struct internal_bench *b)
|
||||
if (nsec > TRIAL_NSEC) {
|
||||
break;
|
||||
}
|
||||
iterations <<= 1;
|
||||
iterations = ((unsigned long)iterations) << 1;
|
||||
if (iterations <= 0) { // overflow
|
||||
abort();
|
||||
}
|
||||
}
|
||||
while (nsec < TARGET_NSEC) {
|
||||
iterations = (long)(iterations * TARGET_NSEC * 1.1 / nsec);
|
||||
double target_iterations = iterations * TARGET_NSEC * 1.1 / nsec;
|
||||
if (target_iterations > (double)LONG_MAX) {
|
||||
abort();
|
||||
}
|
||||
iterations = target_iterations;
|
||||
nsec = measure_once(b, iterations);
|
||||
}
|
||||
return nsec / iterations;
|
||||
|
Loading…
Reference in New Issue
Block a user