add page cache with read-ahead and dirty writeback, file-backed and anonymous mmap with CoW, VAD-based virtual memory replacing the old user region model, PFN database with eviction and reclaim, file identity for cross-open section sharing, FAT32 path cache and per-file cluster chain cache, serial.sys for COM1 keyboard input, xHCI MSI with INTx and polled fallback, PciEnableMsiAuto, waitable input queue, KeWaitForAnyEvent, per-process thread info syscall, expanded kernel stats (PFN/PTE/VAD/file-cache/disk), pool allocation tags, async I/O user buffer locking, file flush on close, add docs.md.
This commit is contained in:
1 parent
61ec2442eb
commit
2c4a68a75f
114 files changed
+18069
-2620
No files matched your search
@@ -0,0 +1,223 @@
|
||||
// Page-cache write/truncate invalidation regression test.
|
||||
// Verifies cached clean pages are not reused after file write or truncate.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <ntlite/syscall.h>
|
||||
|
||||
#define TEST_SIZE 8192u
|
||||
#define TRUNC_SIZE 1024u
|
||||
|
||||
static uint8_t gOriginal[TEST_SIZE];
|
||||
static uint8_t gReplacement[4096];
|
||||
static uint8_t gReadback[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[2] = { buf[i], 0 };
|
||||
print(c);
|
||||
}
|
||||
}
|
||||
|
||||
static void exit_now(int code) {
|
||||
for (;;) SysExitProcess(code);
|
||||
}
|
||||
|
||||
static void fail(const char* msg) {
|
||||
print("pcache_invalidate: ");
|
||||
print(msg);
|
||||
print("\n");
|
||||
exit_now(1);
|
||||
}
|
||||
|
||||
static void fail_status(const char* what, uint32_t status) {
|
||||
print("pcache_invalidate: ");
|
||||
print(what);
|
||||
print(" failed status=");
|
||||
print_u64(status);
|
||||
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 void delete_path(const char* path) {
|
||||
SYS_PATH_REQUEST req = { SysSvFromCstr(path) };
|
||||
(void)SysDeleteFile(&req);
|
||||
}
|
||||
|
||||
static uint32_t create_temp(uint32_t* fileOut, const char** pathOut) {
|
||||
static const char* paths[] = {
|
||||
"pcache_invalidate.tmp",
|
||||
"/pcache_invalidate.tmp",
|
||||
"1:/pcache_invalidate.tmp",
|
||||
"0:/pcache_invalidate.tmp",
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); ++i) {
|
||||
uint32_t h = 0;
|
||||
delete_path(paths[i]);
|
||||
SYS_PATH_HANDLE_REQUEST req = { SysSvFromCstr(paths[i]), &h };
|
||||
uint32_t s = SysCreateFile(&req);
|
||||
if (s == SYS_STATUS_SUCCESS) {
|
||||
*fileOut = h;
|
||||
if (pathOut) *pathOut = paths[i];
|
||||
return SYS_STATUS_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
return SYS_STATUS_NOT_FOUND;
|
||||
}
|
||||
|
||||
static void fill_patterns(void) {
|
||||
for (size_t i = 0; i < TEST_SIZE; ++i)
|
||||
gOriginal[i] = (uint8_t)(0x30u + (uint8_t)(i * 17u + (i >> 7)));
|
||||
for (size_t i = 0; i < sizeof(gReplacement); ++i)
|
||||
gReplacement[i] = (uint8_t)(0xA0u + (uint8_t)(i * 29u + (i >> 5)));
|
||||
}
|
||||
|
||||
static void checked_write(uint32_t file, const void* buffer, size_t size, const char* what) {
|
||||
size_t done = 0;
|
||||
SYS_FILE_IO_REQUEST req = SysMakeFileIoRequest(file, buffer, size, &done);
|
||||
uint32_t s = SysWriteFile(&req);
|
||||
if (s != SYS_STATUS_SUCCESS) fail_status(what, s);
|
||||
if (done != size) fail("short write");
|
||||
}
|
||||
|
||||
static void checked_read(uint32_t file, void* buffer, size_t size, size_t* gotOut, const char* what) {
|
||||
size_t got = 0;
|
||||
memset(buffer, 0xCC, size);
|
||||
SYS_FILE_IO_REQUEST req = SysMakeFileIoRequest(file, buffer, size, &got);
|
||||
uint32_t s = SysReadFile(&req);
|
||||
if (s != SYS_STATUS_SUCCESS) fail_status(what, s);
|
||||
if (gotOut) *gotOut = got;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
print("pcache_invalidate: start\n");
|
||||
fill_patterns();
|
||||
|
||||
uint32_t file = 0;
|
||||
const char* path = 0;
|
||||
uint32_t s = create_temp(&file, &path);
|
||||
if (s != SYS_STATUS_SUCCESS) fail_status("create temp", s);
|
||||
|
||||
print("pcache_invalidate: using ");
|
||||
print(path ? path : "<unknown>");
|
||||
print("\n");
|
||||
|
||||
checked_write(file, gOriginal, sizeof(gOriginal), "write original");
|
||||
|
||||
s = SysSeekFile(file, 0);
|
||||
if (s != SYS_STATUS_SUCCESS) fail_status("seek fill", s);
|
||||
|
||||
SYS_FILE_CACHE_STATS before;
|
||||
SYS_FILE_CACHE_STATS afterFill;
|
||||
SYS_FILE_CACHE_STATS afterSecond;
|
||||
SYS_FILE_CACHE_STATS afterWrite;
|
||||
SYS_FILE_CACHE_STATS afterRewriteRead;
|
||||
SYS_FILE_CACHE_STATS afterTruncate;
|
||||
if (query_cache(&before) != SYS_STATUS_SUCCESS) fail("initial stats query failed");
|
||||
|
||||
size_t got = 0;
|
||||
checked_read(file, gReadback, sizeof(gReadback), &got, "first read");
|
||||
if (got != sizeof(gOriginal)) fail("first read size mismatch");
|
||||
if (memcmp(gReadback, gOriginal, sizeof(gOriginal)) != 0) fail("first read data mismatch");
|
||||
|
||||
if (query_cache(&afterFill) != SYS_STATUS_SUCCESS) fail("fill stats query failed");
|
||||
if (afterFill.Loads <= before.Loads) fail("initial read did not populate cache");
|
||||
|
||||
s = SysSeekFile(file, 0);
|
||||
if (s != SYS_STATUS_SUCCESS) fail_status("seek second", s);
|
||||
checked_read(file, gReadback, sizeof(gReadback), &got, "second read");
|
||||
if (got != sizeof(gOriginal)) fail("second read size mismatch");
|
||||
if (memcmp(gReadback, gOriginal, sizeof(gOriginal)) != 0) fail("second read data mismatch");
|
||||
|
||||
if (query_cache(&afterSecond) != SYS_STATUS_SUCCESS) fail("second stats query failed");
|
||||
// Do not require this second read to hit: the cache is intentionally
|
||||
// bounded and pcache_pressure.exe may have left it at the soft limit, so
|
||||
// the newly-populated temp pages are eligible for immediate pressure
|
||||
// eviction. This test is about write/truncate freshness; mmap/read reuse
|
||||
// is covered by pcache_mmap_read_reuse.exe.
|
||||
|
||||
s = SysSeekFile(file, 0);
|
||||
if (s != SYS_STATUS_SUCCESS) fail_status("seek rewrite", s);
|
||||
checked_write(file, gReplacement, sizeof(gReplacement), "rewrite first page");
|
||||
|
||||
if (query_cache(&afterWrite) != SYS_STATUS_SUCCESS) fail("post-write stats query failed");
|
||||
if (afterWrite.Invalidations <= afterSecond.Invalidations) fail("write did not invalidate cached page");
|
||||
|
||||
uint32_t mapping = 0;
|
||||
s = SysCreateFileMapping(file, SYS_VM_READ, SYS_MAP_FLAG_NONE, sizeof(gReplacement), &mapping);
|
||||
if (s != SYS_STATUS_SUCCESS) fail_status("create mapping after rewrite", s);
|
||||
|
||||
void* mappedBase = 0;
|
||||
s = SysMapFile(mapping, 0, sizeof(gReplacement), SYS_MAP_ACCESS_READ, SYS_MAP_FLAG_NONE, &mappedBase);
|
||||
SysCloseHandle(mapping);
|
||||
if (s != SYS_STATUS_SUCCESS) fail_status("map after rewrite", s);
|
||||
if (!mappedBase) fail("map after rewrite returned null");
|
||||
|
||||
volatile uint8_t* mappedView = (volatile uint8_t*)mappedBase;
|
||||
(void)mappedView[0];
|
||||
(void)mappedView[sizeof(gReplacement) - 1];
|
||||
if (memcmp((const void*)mappedBase, gReplacement, sizeof(gReplacement)) != 0) {
|
||||
SysFreeVm(mappedBase);
|
||||
fail("stale mmap data after write invalidation");
|
||||
}
|
||||
s = SysFreeVm(mappedBase);
|
||||
if (s != SYS_STATUS_SUCCESS) fail_status("unmap after rewrite", s);
|
||||
|
||||
s = SysSeekFile(file, 0);
|
||||
if (s != SYS_STATUS_SUCCESS) fail_status("seek verify rewrite", s);
|
||||
checked_read(file, gReadback, sizeof(gReadback), &got, "read after rewrite");
|
||||
if (got != sizeof(gOriginal)) fail("rewrite read size mismatch");
|
||||
if (memcmp(gReadback, gReplacement, sizeof(gReplacement)) != 0) fail("stale data after write invalidation");
|
||||
if (memcmp(gReadback + sizeof(gReplacement), gOriginal + sizeof(gReplacement),
|
||||
sizeof(gOriginal) - sizeof(gReplacement)) != 0) fail("unwritten page changed after rewrite");
|
||||
|
||||
if (query_cache(&afterRewriteRead) != SYS_STATUS_SUCCESS) fail("rewrite-read stats query failed");
|
||||
|
||||
s = SysTruncateFile(file, TRUNC_SIZE);
|
||||
if (s != SYS_STATUS_SUCCESS) fail_status("truncate", s);
|
||||
if (SysGetFileSize(file) != TRUNC_SIZE) fail("truncate size mismatch");
|
||||
|
||||
if (query_cache(&afterTruncate) != SYS_STATUS_SUCCESS) fail("truncate stats query failed");
|
||||
if (afterTruncate.Invalidations <= afterRewriteRead.Invalidations) fail("truncate did not invalidate cached tail");
|
||||
|
||||
s = SysSeekFile(file, 0);
|
||||
if (s != SYS_STATUS_SUCCESS) fail_status("seek truncated", s);
|
||||
checked_read(file, gReadback, sizeof(gReadback), &got, "read truncated");
|
||||
if (got != TRUNC_SIZE) fail("truncated read length mismatch");
|
||||
if (memcmp(gReadback, gReplacement, TRUNC_SIZE) != 0) fail("truncated data mismatch");
|
||||
|
||||
SysCloseHandle(file);
|
||||
delete_path(path);
|
||||
|
||||
print("pcache_invalidate: invalidations+=");
|
||||
print_u64(afterTruncate.Invalidations - before.Invalidations);
|
||||
print(" loads+=");
|
||||
print_u64(afterTruncate.Loads - before.Loads);
|
||||
print(" hits+=");
|
||||
print_u64(afterTruncate.Hits - before.Hits);
|
||||
print("\n");
|
||||
|
||||
print("pcache_invalidate: PASS\n");
|
||||
exit_now(0);
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user