Files
DucielandClaude Opus 5.5 986599e659 Buffered SavedVariables writes: logout/reload no longer stall on WriteFile (0.6.0)
The client's SavedVariables writer issues one WriteFile per value; tonight's
raid log showed a logout spending ~6 s of an 8.5 s frame in NtWriteFile
(aux-addon and pfExtend files of ~8 MB each). Writes made inside the
per-variable writer (0x7043F0) to that file are now collected in a 256 KB
buffer and written in large chunks, flushed before the variable ends and
before the file is closed (OsCloseFile 0x42A4D0): identical bytes and order.
Main thread and SavedVariables only; svwrite=0 in ducielperf.cfg disables it.

tests/test_svwrite: 40 random saves (1.44M writes) byte-identical with
4,516 OS writes instead of 1,441,387; close mid-variable flushes; other
handles never buffered; failures counted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 22:06:24 +01:00

135 lines
4.7 KiB
C++

// Test for buffered SavedVariables writing (src/modules/svwrite_core.h).
// Simulates the client's saver: per variable, begin(); many small writes
// (one per value), sometimes large or empty ones; end(). The bytes reaching
// the "OS" must equal the unbuffered writes exactly, in order, with far fewer
// OS writes. Also: a close in the middle of a variable flushes, writes to
// another handle are never buffered, and a failing OS write is counted.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../src/modules/svwrite_core.h"
static char g_disk[2][64 << 20]; // [0] unbuffered reference, [1] through the writer
static size_t g_disk_len[2];
static int g_target;
static uint64_t g_os_writes;
static bool g_fail_next;
struct TestApi {
static bool write(uint32_t h, const char *data, uint32_t size) {
g_os_writes++;
if (g_fail_next) {
g_fail_next = false;
return false;
}
memcpy(g_disk[g_target] + g_disk_len[g_target], data, size);
g_disk_len[g_target] += size;
return true;
}
};
static uint32_t g_seed = 12345;
static uint32_t rnd(uint32_t n) {
g_seed = g_seed * 1103515245u + 12345u;
return (g_seed >> 8) % n;
}
static int g_failures;
static void check(const char *what, bool ok) {
printf(" %-60s %s\n", what, ok ? "ok" : "FAIL");
if (!ok) g_failures++;
}
static char g_src[1 << 20];
// One "save": nvars variables, each with a random number of writes.
static uint64_t save(svwrite::Writer<TestApi> *w, int target, uint32_t seed, int nvars, uint64_t *writes) {
g_seed = seed;
g_target = target;
g_disk_len[target] = 0;
g_os_writes = 0;
*writes = 0;
const uint32_t H = 0x1234;
for (int v = 0; v < nvars; v++) {
if (w) w->begin(H);
uint32_t n = rnd(10) == 0 ? rnd(20000) : rnd(200);
for (uint32_t i = 0; i < n; i++) {
uint32_t k = rnd(1000), size;
if (k < 900) size = rnd(40); // typical value / key / punctuation
else if (k < 990) size = rnd(4000); // long string
else if (k < 999) size = 0; // empty write
else size = 64 * 1024 + rnd(100000); // larger than DIRECT_SIZE
const char *data = g_src + rnd(sizeof g_src - size);
(*writes)++;
uint32_t written = 0;
if (!(w && w->write(H, data, size, &written))) TestApi::write(H, data, size);
else if (written != size) g_failures++;
// a write to another file in the middle is never buffered
if (k == 5) {
uint32_t other_written = 0;
if (w && w->write(H + 4, data, 1, &other_written)) {
printf(" write to another handle was buffered: FAIL\n");
g_failures++;
}
}
}
if (w) w->end();
}
return g_os_writes;
}
int main() {
for (size_t i = 0; i < sizeof g_src; i++) g_src[i] = (char)(rand() & 0xFF);
static char buffer[svwrite::BUFFER_SIZE];
printf("Identical file content, fewer OS writes:\n");
int same = 0, runs = 40;
uint64_t total_writes = 0, os_ref = 0, os_buf = 0;
for (int r = 0; r < runs; r++) {
svwrite::Writer<TestApi> w;
w.buf = buffer;
uint64_t writes;
os_ref += save(nullptr, 0, 1000 + r, 30, &writes);
total_writes += writes;
os_buf += save(&w, 1, 1000 + r, 30, &writes);
same += g_disk_len[0] == g_disk_len[1] && memcmp(g_disk[0], g_disk[1], g_disk_len[0]) == 0;
}
char line[128];
snprintf(line, sizeof line, "%d/%d saves byte-identical (%llu writes)", same, runs,
(unsigned long long)total_writes);
check(line, same == runs);
snprintf(line, sizeof line, "OS writes: %llu unbuffered -> %llu buffered", (unsigned long long)os_ref,
(unsigned long long)os_buf);
check(line, os_buf * 50 < os_ref);
printf("Close in the middle of a variable:\n");
{
svwrite::Writer<TestApi> w;
w.buf = buffer;
g_target = 1;
g_disk_len[1] = 0;
uint32_t wr;
w.begin(7);
w.write(7, "abc", 3, &wr);
w.touch(7); // close: flushes
check("bytes written before the close", g_disk_len[1] == 3 && memcmp(g_disk[1], "abc", 3) == 0);
check("no longer buffering after the close", !w.active && !w.write(7, "x", 1, &wr));
}
printf("A failing OS write is counted:\n");
{
svwrite::Writer<TestApi> w;
w.buf = buffer;
uint32_t wr;
w.begin(9);
w.write(9, "abc", 3, &wr);
g_fail_next = true;
w.end();
check("failure counted", w.failures == 1);
}
printf("%s\n", g_failures ? "FAIL" : "PASS");
return g_failures ? 1 : 0;
}