Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
cpu_hog: don't use non-reentrant rand() in threads, do smth with result
Previously, stress -c did a terrible job at actually loading the CPU; it
was idle most of the times:
This means that in more than half of the cycles, the CPU frontend
couldn't do something. Why? A
perf record -g
trace of the sameinvocation tells us that the CPU is spending > 99% of its time in
__random, waiting for an integer comparison that involves a data load.
No surprise there: rand() relies on global state that needs to get
synchronized.
With this percentage in mind, it's not so bad that the result of sqrt
never got used.
This commit changes both:
pseudo-random number generation, we just inline xoroshiro128+ [1],
which is under a MIT-0 style "dedication to public domain" license.
We still don't "spin on sqrt()", because floating point sqrt is very
very fast on modern desktop/server CPUs; but at least we actually make
the CPU do its rounds.
With this change, the statistic now looks like this:
So, we're nearly doubling the number of actually executed instructions, proving that we're now really stressing our superscalar CPU
[1] https://prng.di.unimi.it/