Files
nt-lite/code/user/tests/pcache_smoke/pcache_smoke.c
T

161 lines
5.2 KiB
C

// Read-only page-cache smoke test.
// Verifies that ReadFile and file-backed mappings share the same cached pages.
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <ntlite/syscall.h>
#define TEST_SIZE 8192u
static uint8_t gA[TEST_SIZE];
static uint8_t gB[TEST_SIZE];
static void print(const char* s) {
SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromCstr(s) };
SysWriteConsole(&req);
}
static void print_u64(uint64_t v) {
char buf[32];
int i = 0;
if (v == 0) { print("0"); return; }
while (v && i < (int)sizeof(buf)) {
buf[i++] = (char)('0' + (v % 10));
v /= 10;
}
while (i--) {
char c = buf[i];
SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromParts(&c, 1) };
SysWriteConsole(&req);
}
}
static void exit_now(int code) {
for (;;) SysExitProcess(code);
}
static void fail(const char* msg) {
print("pcache_smoke: ");
print(msg);
print("\n");
exit_now(1);
}
static void fail_status(const char* what, uint32_t s) {
print("pcache_smoke: "); print(what); print(" failed status="); print_u64(s); print("\n");
exit_now(1);
}
static uint32_t query_cache(SYS_FILE_CACHE_STATS* out) {
memset(out, 0, sizeof(*out));
return SysQueryKernelStats(SYS_KERNEL_STATS_FILE_CACHE, out, sizeof(*out), 0);
}
static uint32_t open_self(uint32_t* out) {
static const char* paths[] = {
"pcache_smoke.exe",
"/apps/pcache_smoke.exe",
"Y:/apps/pcache_smoke.exe",
"0:/apps/pcache_smoke.exe",
"1:/apps/pcache_smoke.exe",
"/apps/shell_native.exe",
"0:/apps/shell_native.exe",
"1:/apps/shell_native.exe",
};
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); ++i) {
uint32_t h = 0;
SYS_OPEN_FILE_REQUEST req = SysMakeOpenFileRequest(SysSvFromCstr(paths[i]),
SYS_FILE_ACCESS_READ | SYS_FILE_ACCESS_QUERY,
SYS_FILE_OPEN_EXISTING,
SYS_FILE_FLAG_NONE,
&h);
uint32_t s = SysOpenFile(&req);
if (s == NTLSTATUS_SUCCESS) {
*out = h;
print("pcache_smoke: using "); print(paths[i]); print("\n");
return NTLSTATUS_SUCCESS;
}
}
return NTLSTATUS_NOT_FOUND;
}
int main(void) {
print("pcache_smoke: start\n");
SYS_FILE_CACHE_STATS before, afterFirst, afterSecond, afterMap;
uint32_t s = query_cache(&before);
if (s != NTLSTATUS_SUCCESS) fail_status("query initial stats", s);
uint32_t file = 0;
s = open_self(&file);
if (s != NTLSTATUS_SUCCESS) fail_status("open test file", s);
size_t gotA = 0;
memset(gA, 0xA5, sizeof(gA));
SYS_FILE_IO_REQUEST readReqA = SysMakeFileIoRequest(file, gA, sizeof(gA), &gotA);
s = SysReadFile(&readReqA);
if (s != NTLSTATUS_SUCCESS) fail_status("first read", s);
if (gotA < 4096) fail("first read too short");
print("pcache_smoke: first read OK\n");
s = query_cache(&afterFirst);
if (s != NTLSTATUS_SUCCESS) fail_status("query after first read", s);
if (afterFirst.Loads <= before.Loads)
fail("first read did not load file-cache pages");
s = SysSeekFile(file, 0);
if (s != NTLSTATUS_SUCCESS) fail_status("seek", s);
size_t gotB = 0;
memset(gB, 0x5A, sizeof(gB));
SYS_FILE_IO_REQUEST readReqB = SysMakeFileIoRequest(file, gB, sizeof(gB), &gotB);
s = SysReadFile(&readReqB);
if (s != NTLSTATUS_SUCCESS) fail_status("second read", s);
if (gotB != gotA) fail("second read length mismatch");
if (memcmp(gA, gB, gotA) != 0) fail("second read data mismatch");
print("pcache_smoke: second read OK\n");
s = query_cache(&afterSecond);
if (s != NTLSTATUS_SUCCESS) fail_status("query after second read", s);
if (afterSecond.Hits <= afterFirst.Hits)
fail("second read did not hit file cache");
uint32_t mapping = 0;
s = SysCreateFileMapping(file, SYS_VM_READ, SYS_MAP_FLAG_NONE, sizeof(gA), &mapping);
if (s != NTLSTATUS_SUCCESS) fail_status("create mapping", s);
SysCloseHandle(file);
file = 0;
void* base = 0;
s = SysMapFile(mapping, 0, sizeof(gA), SYS_MAP_ACCESS_READ, SYS_MAP_FLAG_NONE, &base);
SysCloseHandle(mapping);
if (s != NTLSTATUS_SUCCESS) fail_status("map file", s);
if (!base) fail("map returned null");
volatile uint8_t* view = (volatile uint8_t*)base;
(void)view[0];
(void)view[4096];
print("pcache_smoke: mmap faults OK\n");
if (memcmp((const void*)base, gA, gotA) != 0) {
SysFreeVm(base);
fail("mapped data mismatch");
}
s = query_cache(&afterMap);
if (s != NTLSTATUS_SUCCESS) fail_status("query after map", s);
if (afterMap.Hits <= afterSecond.Hits)
fail("mmap faults did not hit already cached pages");
SysFreeVm(base);
print("pcache_smoke: stats loads="); print_u64(afterMap.Loads - before.Loads);
print(" hits="); print_u64(afterMap.Hits - before.Hits);
print(" resident="); print_u64(afterMap.ResidentPages); print("\n");
print("pcache_smoke: PASS\n");
exit_now(0);
return 0;
}