Created
May 9, 2021 04:47
-
-
Save TellowKrinkle/863f51a59f21497e062a2c55e141fceb to your computer and use it in GitHub Desktop.
CPU Wakeup Speed Tester
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
extern "C" size_t test(size_t amt); | |
template <size_t Len, size_t Iters> | |
struct Tester { | |
size_t data[Iters][Len]; | |
void run(useconds_t sleep_us, int sleep_count, size_t iterlen) { | |
for (auto& iter : data) { | |
for (int i = 0; i < sleep_count; i++) | |
usleep(sleep_us); | |
for (size_t i = 0; i < Len; i++) { | |
auto& pt = iter[i]; | |
pt = test(iterlen); | |
} | |
} | |
} | |
void results() { | |
size_t min = SIZE_MAX; | |
static constexpr int HIST_MAX = 8; | |
static constexpr int HIST_RES = 4; // Bucket size: Min/HIST_RES | |
int histograms[Len][HIST_MAX] = {}; | |
for (const auto& iter : data) { | |
for (auto pt : iter) { | |
if (min > pt) { min = pt; } | |
} | |
} | |
for (const auto& iter : data) { | |
for (size_t i = 0; i < Len; i++) { | |
auto pt = iter[i]; | |
auto& hist = histograms[i]; | |
auto quartile = (pt - min) / (min / HIST_RES); | |
if (quartile < HIST_MAX) { | |
hist[quartile]++; | |
} | |
} | |
} | |
printf("%8zu Baseline\n", min); | |
int digits[HIST_MAX] = {}; | |
for (const auto& hist : histograms) { | |
for (int i = 0; i < HIST_MAX; i++) { | |
int v = hist[i]; | |
int digitcount = 1; | |
while (v >= 10) { | |
digitcount++; | |
v /= 10; | |
} | |
if (digits[i] < digitcount) { digits[i] = digitcount; } | |
} | |
} | |
for (const auto& hist : histograms) { | |
printf("%2zd: ", (&hist - histograms)); | |
for (int i = 0; i < HIST_MAX; i++) { | |
char fmt[10]; | |
sprintf(fmt, "%%%dd ", digits[i]); | |
printf(fmt, hist[i]); | |
} | |
printf("\n"); | |
} | |
} | |
}; | |
int main(int argc, const char * argv[]) { | |
Tester<32, 1000> tester; | |
tester.run(4000, 1, 100000); | |
tester.results(); | |
tester.run(1000, 1, 100000); | |
tester.results(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.intel_syntax | |
.globl _test | |
_test: | |
rdtsc | |
shl rdx, 32 | |
or rax, rdx | |
mov rcx, rax | |
loop: | |
add rdx, rdx | |
add rdx, rdx | |
add rdx, rdx | |
add rdx, rdx | |
dec rdi | |
jne loop | |
rdtsc | |
shl rdx, 32 | |
or rax, rdx | |
sub rax, rcx | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment