Files
mandy/scripts/write_watch_bench.c

103 lines
4.9 KiB
C

// How much noticing writes to memory costs on this host, per 4 KiB page: Windows write watch (MEM_WRITE_WATCH, which
// the runtimes use for the title's graphics memory) and guards (read-only pages and a vectored handler that opens the
// page at its first write). It touches only memory it allocates itself. docs/linux.md, "Which Wine", has the results.
//
// Build it on Linux with the cross tools of docs/linux.md, then run it under the Wine to compare:
// clang-cl --target=x86_64-pc-windows-msvc /winsysroot <xwin-sdk> /O2 -MD /W4 -c write_watch_bench.c
// lld-link /winsysroot:<xwin-sdk> /subsystem:console write_watch_bench.obj kernel32.lib
// wine write_watch_bench.exe [MiB=256] [rounds=3]
// or on Windows: cl /O2 write_watch_bench.c
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
static LARGE_INTEGER freq;
static double now_us(void) {
LARGE_INTEGER t;
QueryPerformanceCounter(&t);
return (double)t.QuadPart * 1e6 / (double)freq.QuadPart;
}
static char *guard_base;
static SIZE_T guard_len;
static volatile LONG faults;
static LONG CALLBACK opener(EXCEPTION_POINTERS *e) {
if (e->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION) return EXCEPTION_CONTINUE_SEARCH;
char *at = (char *)e->ExceptionRecord->ExceptionInformation[1];
if (at < guard_base || at >= guard_base + guard_len) return EXCEPTION_CONTINUE_SEARCH;
DWORD old;
VirtualProtect((void *)((ULONG_PTR)at & ~(ULONG_PTR)4095), 4096, PAGE_READWRITE, &old);
InterlockedIncrement(&faults);
return EXCEPTION_CONTINUE_EXECUTION;
}
int main(int argc, char **argv) {
QueryPerformanceFrequency(&freq);
int mib_arg = argc > 1 ? atoi(argv[1]) : 256;
int rounds = argc > 2 ? atoi(argv[2]) : 3;
if (mib_arg < 1 || mib_arg > 65536 || rounds < 1 || rounds > 1000) {
printf("usage: write_watch_bench [MiB 1-65536] [rounds 1-1000]\n");
return 2;
}
SIZE_T mib = (SIZE_T)mib_arg;
SIZE_T len = mib << 20, pages = len >> 12;
HMODULE ntdll = GetModuleHandleA("ntdll.dll");
FARPROC wine = GetProcAddress(ntdll, "wine_get_version");
printf("host: %s%s, region %zu MiB = %zu pages\n", wine ? "Wine " : "Windows",
wine ? ((const char *(*)(void))wine)() : "", mib, pages);
// 1. Write watch.
char *ww = VirtualAlloc(NULL, len, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE);
if (!ww) { printf("write-watch VirtualAlloc failed: %lu\n", GetLastError()); return 1; }
for (SIZE_T i = 0; i < pages; ++i) ww[i << 12] = 1;
void **found = malloc(pages * sizeof(void *));
if (!found) { printf("malloc failed\n"); return 1; }
for (int r = 0; r < rounds; ++r) {
double t0 = now_us();
ResetWriteWatch(ww, len);
double t1 = now_us();
for (SIZE_T i = 0; i < pages; ++i) ww[i << 12] = (char)r; // first write after the reset
double t2 = now_us();
for (SIZE_T i = 0; i < pages; ++i) ww[(i << 12) + 64] = (char)r; // second write to each page, no reset between
double t3 = now_us();
ULONG_PTR count = pages; DWORD gran;
UINT rc = GetWriteWatch(0, ww, len, found, &count, &gran);
double t4 = now_us();
ResetWriteWatch(ww, len);
ww[0] = 2; ww[len / 2] = 2; // two pages dirty
double t5 = now_us();
ULONG_PTR few = pages;
GetWriteWatch(0, ww, len, found, &few, &gran);
double t6 = now_us();
printf("watch round %d: reset %.0f us, first writes %.3f us/page, second writes %.3f us/page, "
"GetWriteWatch(all dirty) %.0f us (rc %u, %llu pages), GetWriteWatch(2 dirty) %.0f us (%llu)\n",
r, t1 - t0, (t2 - t1) / pages, (t3 - t2) / pages, t4 - t3, rc, (unsigned long long)count,
t6 - t5, (unsigned long long)few);
}
// 2. Guards: read-only pages and a handler that opens one page per fault.
guard_len = len;
guard_base = VirtualAlloc(NULL, len, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (!guard_base) { printf("guard VirtualAlloc failed: %lu\n", GetLastError()); return 1; }
for (SIZE_T i = 0; i < pages; ++i) guard_base[i << 12] = 1;
AddVectoredExceptionHandler(1, opener);
for (int r = 0; r < rounds; ++r) {
DWORD old;
faults = 0;
double t0 = now_us();
VirtualProtect(guard_base, len, PAGE_READONLY, &old);
double t1 = now_us();
for (SIZE_T i = 0; i < pages; ++i) guard_base[i << 12] = (char)r;
double t2 = now_us();
for (SIZE_T i = 0; i < 4096 && i < pages; ++i) VirtualProtect(guard_base + (i << 12), 4096, PAGE_READONLY, &old);
double t3 = now_us();
SIZE_T n = pages < 4096 ? pages : 4096;
printf("guard round %d: protect all %.0f us, write faults %.3f us/page (%ld faults), "
"VirtualProtect of one page %.3f us\n",
r, t1 - t0, (t2 - t1) / pages, faults, (t3 - t2) / n);
VirtualProtect(guard_base, len, PAGE_READWRITE, &old);
}
return 0;
}