mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-24 14:22:28 +00:00
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
This commit is contained in:
parent
41c86fe86b
commit
c6d8e516b2
5 changed files with 64 additions and 4 deletions
|
@ -115,6 +115,7 @@ void __morph_end(void);
|
||||||
unsigned char *GetFirstInstruction(void);
|
unsigned char *GetFirstInstruction(void);
|
||||||
unsigned char *GetInstructionLengths(void);
|
unsigned char *GetInstructionLengths(void);
|
||||||
void __print_maps(void);
|
void __print_maps(void);
|
||||||
|
void __warn_if_powersave(void);
|
||||||
|
|
||||||
COSMOPOLITAN_C_END_
|
COSMOPOLITAN_C_END_
|
||||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||||
|
|
42
libc/runtime/warnifpowersave.c
Normal file
42
libc/runtime/warnifpowersave.c
Normal file
|
@ -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);
|
||||||
|
}
|
|
@ -19,6 +19,7 @@
|
||||||
#include "libc/fmt/fmt.h"
|
#include "libc/fmt/fmt.h"
|
||||||
#include "libc/math.h"
|
#include "libc/math.h"
|
||||||
#include "libc/runtime/gc.internal.h"
|
#include "libc/runtime/gc.internal.h"
|
||||||
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/stdio/stdio.h"
|
#include "libc/stdio/stdio.h"
|
||||||
#include "libc/testlib/testlib.h"
|
#include "libc/testlib/testlib.h"
|
||||||
#include "libc/time/time.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) {
|
void __testlib_ezbenchreport(const char *form, uint64_t c1, uint64_t c2) {
|
||||||
uint64_t ns1, ns2;
|
uint64_t ns1, ns2;
|
||||||
|
__warn_if_powersave();
|
||||||
ns1 = rintl(ConvertTicksToNanos(c1));
|
ns1 = rintl(ConvertTicksToNanos(c1));
|
||||||
ns2 = rintl(ConvertTicksToNanos(c2));
|
ns2 = rintl(ConvertTicksToNanos(c2));
|
||||||
(fprintf)(stderr,
|
(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);
|
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];
|
char msg[128];
|
||||||
uint64_t bps;
|
uint64_t bps;
|
||||||
long double cn, lat;
|
long double cn, lat;
|
||||||
|
__warn_if_powersave();
|
||||||
(snprintf)(msg, sizeof(msg), "%s %c=%d", form, z, n);
|
(snprintf)(msg, sizeof(msg), "%s %c=%d", form, z, n);
|
||||||
cn = ConvertTicksToNanos(c);
|
cn = ConvertTicksToNanos(c);
|
||||||
if (!n) {
|
if (!n) {
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#include "libc/calls/struct/timespec.h"
|
#include "libc/calls/struct/timespec.h"
|
||||||
#include "libc/calls/struct/timeval.h"
|
#include "libc/calls/struct/timeval.h"
|
||||||
#include "libc/calls/syscall_support-sysv.internal.h"
|
#include "libc/calls/syscall_support-sysv.internal.h"
|
||||||
|
#include "libc/dce.h"
|
||||||
#include "libc/nexgen32e/rdtsc.h"
|
#include "libc/nexgen32e/rdtsc.h"
|
||||||
#include "libc/runtime/runtime.h"
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/sysv/consts/auxv.h"
|
#include "libc/sysv/consts/auxv.h"
|
||||||
|
@ -49,7 +50,19 @@ BENCH(clock_gettime, bench) {
|
||||||
EZBENCH2("nowl", donothing, nowl());
|
EZBENCH2("nowl", donothing, nowl());
|
||||||
EZBENCH2("rdtsc", donothing, rdtsc());
|
EZBENCH2("rdtsc", donothing, rdtsc());
|
||||||
EZBENCH2("gettimeofday", donothing, gettimeofday(&tv, 0));
|
EZBENCH2("gettimeofday", donothing, gettimeofday(&tv, 0));
|
||||||
EZBENCH2("clock_gettime", donothing, clock_gettime(0, &ts));
|
EZBENCH2("clock_gettime 0", donothing, clock_gettime(0, &ts));
|
||||||
EZBENCH2("__clock_gettime", donothing, __clock_gettime(0, &ts));
|
EZBENCH2("clock_gettime 1", donothing, clock_gettime(1, &ts));
|
||||||
EZBENCH2("sys_clock_gettime", donothing, sys_clock_gettime(0, &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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -642,6 +642,7 @@ int LuaBenchmark(lua_State *L) {
|
||||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||||
count = luaL_optinteger(L, 2, 100);
|
count = luaL_optinteger(L, 2, 100);
|
||||||
maxattempts = luaL_optinteger(L, 3, 10);
|
maxattempts = luaL_optinteger(L, 3, 10);
|
||||||
|
__warn_if_powersave();
|
||||||
lua_gc(L, LUA_GCSTOP);
|
lua_gc(L, LUA_GCSTOP);
|
||||||
|
|
||||||
for (attempts = 0;;) {
|
for (attempts = 0;;) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue