From c6d8e516b2385603641c766b428668a40ab413c5 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Fri, 10 Jun 2022 20:51:36 -0700 Subject: [PATCH] Print warning when microbenchmarking w/ powersave RDTSC on Linux has so much jitter when the CPU is in powersave mode causing things like microbenchmarks to have a 1000% margin of error --- libc/runtime/runtime.h | 1 + libc/runtime/warnifpowersave.c | 42 ++++++++++++++++++++++++++++ libc/testlib/ezbenchreport.c | 5 +++- test/libc/calls/clock_gettime_test.c | 19 +++++++++++-- tool/net/lfuncs.c | 1 + 5 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 libc/runtime/warnifpowersave.c diff --git a/libc/runtime/runtime.h b/libc/runtime/runtime.h index 1ad50777c..1ca227780 100644 --- a/libc/runtime/runtime.h +++ b/libc/runtime/runtime.h @@ -115,6 +115,7 @@ void __morph_end(void); unsigned char *GetFirstInstruction(void); unsigned char *GetInstructionLengths(void); void __print_maps(void); +void __warn_if_powersave(void); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/runtime/warnifpowersave.c b/libc/runtime/warnifpowersave.c new file mode 100644 index 000000000..bc3ceaff1 --- /dev/null +++ b/libc/runtime/warnifpowersave.c @@ -0,0 +1,42 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/calls.h" +#include "libc/runtime/runtime.h" +#include "libc/str/str.h" +#include "libc/sysv/consts/o.h" + +// RDTSC on Linux has so much jitter when the CPU is in powersave mode. +// Causing things like microbenchmarks to have a 1000% margin of error. + +#define FILE "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor" +#define WARN \ + "warning: this operation isn't reliable in powersave mode. please run:\n\t" \ + "echo performance | sudo tee " \ + "/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor\n" + +void __warn_if_powersave(void) { + int fd; + char buf[16] = {0}; + if (!fileexists(FILE)) return; + if ((fd = open(FILE, O_RDONLY)) == -1) return; + read(fd, buf, 15); + close(fd); + if (!startswith(buf, "powersave")) return; + write(2, WARN, sizeof(WARN) - 1); +} diff --git a/libc/testlib/ezbenchreport.c b/libc/testlib/ezbenchreport.c index bb2bc878e..5e196020b 100644 --- a/libc/testlib/ezbenchreport.c +++ b/libc/testlib/ezbenchreport.c @@ -19,6 +19,7 @@ #include "libc/fmt/fmt.h" #include "libc/math.h" #include "libc/runtime/gc.internal.h" +#include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/testlib/testlib.h" #include "libc/time/time.h" @@ -28,10 +29,11 @@ STATIC_YOINK("strnwidth"); void __testlib_ezbenchreport(const char *form, uint64_t c1, uint64_t c2) { uint64_t ns1, ns2; + __warn_if_powersave(); ns1 = rintl(ConvertTicksToNanos(c1)); ns2 = rintl(ConvertTicksToNanos(c2)); (fprintf)(stderr, - VEIL("r", " * %-19s l: %,9lu𝑐 %,9lu𝑛𝑠 m: %,9lu𝑐 %,9lu𝑛𝑠\n"), + VEIL("r", " * %-19s l: %,9luc %,9luns m: %,9luc %,9luns\n"), form, c1, ns1, c2, ns2); } @@ -39,6 +41,7 @@ void __testlib_ezbenchreport_n(const char *form, char z, size_t n, uint64_t c) { char msg[128]; uint64_t bps; long double cn, lat; + __warn_if_powersave(); (snprintf)(msg, sizeof(msg), "%s %c=%d", form, z, n); cn = ConvertTicksToNanos(c); if (!n) { diff --git a/test/libc/calls/clock_gettime_test.c b/test/libc/calls/clock_gettime_test.c index 94d00d4bb..aae5b9719 100644 --- a/test/libc/calls/clock_gettime_test.c +++ b/test/libc/calls/clock_gettime_test.c @@ -21,6 +21,7 @@ #include "libc/calls/struct/timespec.h" #include "libc/calls/struct/timeval.h" #include "libc/calls/syscall_support-sysv.internal.h" +#include "libc/dce.h" #include "libc/nexgen32e/rdtsc.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/auxv.h" @@ -49,7 +50,19 @@ BENCH(clock_gettime, bench) { EZBENCH2("nowl", donothing, nowl()); EZBENCH2("rdtsc", donothing, rdtsc()); EZBENCH2("gettimeofday", donothing, gettimeofday(&tv, 0)); - EZBENCH2("clock_gettime", donothing, clock_gettime(0, &ts)); - EZBENCH2("__clock_gettime", donothing, __clock_gettime(0, &ts)); - EZBENCH2("sys_clock_gettime", donothing, sys_clock_gettime(0, &ts)); + EZBENCH2("clock_gettime 0", donothing, clock_gettime(0, &ts)); + EZBENCH2("clock_gettime 1", donothing, clock_gettime(1, &ts)); + EZBENCH2("clock_gettime 4", donothing, clock_gettime(4, &ts)); + EZBENCH2("__clock_gettime 0", donothing, __clock_gettime(0, &ts)); + EZBENCH2("__clock_gettime 1", donothing, __clock_gettime(1, &ts)); + EZBENCH2("__clock_gettime 4", donothing, __clock_gettime(4, &ts)); + if (IsWindows()) { + EZBENCH2("sys_clock_gettime 0", donothing, sys_clock_gettime_nt(0, &ts)); + EZBENCH2("sys_clock_gettime 1", donothing, sys_clock_gettime_nt(1, &ts)); + EZBENCH2("sys_clock_gettime 4", donothing, sys_clock_gettime_nt(4, &ts)); + } else { + EZBENCH2("sys_clock_gettime 0", donothing, sys_clock_gettime(0, &ts)); + EZBENCH2("sys_clock_gettime 1", donothing, sys_clock_gettime(1, &ts)); + EZBENCH2("sys_clock_gettime 4", donothing, sys_clock_gettime(4, &ts)); + } } diff --git a/tool/net/lfuncs.c b/tool/net/lfuncs.c index 134d3a9c4..73d9f2f26 100644 --- a/tool/net/lfuncs.c +++ b/tool/net/lfuncs.c @@ -642,6 +642,7 @@ int LuaBenchmark(lua_State *L) { luaL_checktype(L, 1, LUA_TFUNCTION); count = luaL_optinteger(L, 2, 100); maxattempts = luaL_optinteger(L, 3, 10); + __warn_if_powersave(); lua_gc(L, LUA_GCSTOP); for (attempts = 0;;) {