About: how pages are checked

tests/sims/vm.test.ts

The automated tests for the /simulators/virtual-memory simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.

  1. A 48-bit address splits into the same four 9-bit table indexes and 12-bit offset as shifting and masking the bits.
  2. Page fault counts match a separate least-recently-used model on 200 random access traces.
  3. After fork, a page either process writes gets its own frame (copy-on-write), so the two stop sharing it.
// /simulators/virtual-memory: x86-64 four-level page tables, page faults and copy-on-write after fork.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { VM, split, WORKLOADS, REGIONS, PAGE } from '../../src/lib/sims/vm.ts';
import { checker, lcg } from './check.ts';

test('a 48-bit address splits into the same four 9-bit table indexes and 12-bit offset as shifting and masking the bits', () => {
	for (const v of [0x400040, 0x1000010, 0x7ffffffe1f80, 0x7fffffffefff, 0x123456789abc, 0x10]) {
		const b = BigInt(v);
		const want = [39n, 30n, 21n, 12n].map((sh) => Number((b >> sh) & 511n)).concat(Number(b & 4095n));
		const s = split(v);
		assert.deepEqual([...s.idx, s.offset], want, `0x${v.toString(16)}`);
	}
});

test('page fault counts match a separate least-recently-used model on 200 random access traces', () => {
	const { ok, done } = checker();
	const r = lcg(3);
	for (let trial = 0; trial < 200; trial++) {
		const nF = [3, 4, 8][trial % 3];
		const vm = new VM(nF, 4);
		const lru: number[] = [];
		let misses = 0;
		for (let i = 0; i < 300; i++) {
			const reg = r() < 0.2 ? REGIONS[0] : r() < 0.5 ? REGIONS[2] : REGIONS[1];
			const va = reg.start + Math.floor(r() * reg.pages) * PAGE + 8;
			const res = vm.access(1, va, reg.perm === 'rw-' && r() < 0.5);
			ok(res.kind !== 'segv', `segfault on a mapped address 0x${va.toString(16)}`);
			const vpn = Math.floor(va / PAGE), j = lru.indexOf(vpn);
			if (j >= 0) lru.splice(j, 1);
			else { misses++; if (lru.length >= nF) lru.shift(); }
			lru.push(vpn);
		}
		ok(vm.stats.minor + vm.stats.major === misses, `${nF} frames: ${vm.stats.minor + vm.stats.major} faults, LRU model says ${misses}`);
		ok(vm.frames.filter((f) => f).length <= nF, `more than ${nF} frames in use`);
	}
	done();
});

test('after fork, a page either process writes gets its own frame (copy-on-write), so the two stop sharing it', () => {
	const vm = new VM(8, 4);
	for (const op of WORKLOADS.find((w) => w.id === 'fork')!.ops) {
		if ('fork' in op) vm.fork(op.fork);
		else vm.access(op.pid, op.va, op.write);
	}
	const h0 = Math.floor(REGIONS[1].start / PAGE), h1 = h0 + 1;
	const f = (pid: number, v: number) => vm.pte(pid, v)!.frame;
	assert.notEqual(f(1, h0), f(2, h0), 'heap page 0 still shared after both processes wrote it');
	assert.notEqual(f(1, h1), f(2, h1), 'heap page 1 still shared after the parent wrote it');
	assert.ok(vm.stats.cow > 0, 'no copy-on-write faults recorded');
});

Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.

Published