Skip to content

Instantly share code, notes, and snippets.

@TellowKrinkle
Created May 9, 2021 04:47
Show Gist options
  • Select an option

  • Save TellowKrinkle/863f51a59f21497e062a2c55e141fceb to your computer and use it in GitHub Desktop.

Select an option

Save TellowKrinkle/863f51a59f21497e062a2c55e141fceb to your computer and use it in GitHub Desktop.

Revisions

  1. TellowKrinkle created this gist May 9, 2021.
    79 changes: 79 additions & 0 deletions CPUWakeupSpeedTester.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    #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;
    }
    20 changes: 20 additions & 0 deletions CPUWakeupSpeedTester.s
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    .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