About: how pages are checked

tests/sims/gosched.test.ts

The automated tests for the /simulators/goroutine-scheduler 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. On every workload, with 1 to 8 Ps, a live goroutine is always in exactly one place and every one finishes its CPU work.
  2. A tight loop is preempted after 10 ms with asynchronous preemption (Go 1.14+) and never with cooperative scheduling.
// /simulators/goroutine-scheduler: Go's G-M-P scheduler with work stealing and preemption.
import { test } from 'node:test';
import { setup, WORKLOADS, TICK, type Sched } from '../../src/lib/sims/goSched.ts';
import { checker } from './check.ts';

test('on every workload, with 1 to 8 Ps, a live goroutine is always in exactly one place and every one finishes its CPU work', () => {
	const { ok, done } = checker();
	const invariants = (s: Sched, tag: string) => {
		const where = new Map<number, string[]>();
		const put = (g: { id: number }, w: string) => where.set(g.id, [...(where.get(g.id) ?? []), w]);
		for (const p of s.ps) { if (p.g) put(p.g, `P${p.id} running`); if (p.runnext) put(p.runnext, `P${p.id} runnext`); for (const g of p.runq) put(g, `P${p.id} queue`); }
		for (const g of s.global) put(g, 'global queue');
		for (const x of s.inSyscall) put(x.g, 'syscall');
		for (const [id, w] of where) ok(w.length === 1, `${tag} t=${s.now}: G${id} is in ${w.join(' and ')}`);
		for (const g of s.gs) {
			const w = where.get(g.id);
			if (g.state === 'running' || g.state === 'runnable' || g.state === 'syscall') ok(!!w, `${tag}: G${g.id} is ${g.state} but in no queue`);
			if (g.state === 'waiting' || g.state === 'dead') ok(!w, `${tag}: G${g.id} is ${g.state} but queued`);
		}
	};
	for (const w of WORKLOADS) for (const v of ['async', 'cooperative'] as const) for (const procs of [1, 2, 4, 8]) for (const steal of [true, false]) {
		const tag = `${w.id}/${v}/${procs}P/${steal ? 'steal' : 'no steal'}`;
		const s = setup(w, procs, v, steal);
		let steps = 0;
		while (!s.done() && s.now < 3_000_000) { s.tick(); if (++steps % 7 === 0) invariants(s, tag); }
		ok(s.done(), `${tag} did not finish`);
		const want = s.gs.reduce((a, g) => a + g.ops.filter((o) => o.k === 'cpu').reduce((b, o: any) => b + o.us, 0), 0);
		const ran = s.gs.reduce((a, g) => a + g.ran, 0);
		ok(Math.abs(ran - want) <= s.gs.length * TICK * 2, `${tag}: ran ${ran} us of ${want}`);
	}
	done();
});

test('a tight loop is preempted after 10 ms with asynchronous preemption (Go 1.14+) and never with cooperative scheduling', () => {
	const { ok, done } = checker();
	const tight = WORKLOADS.find((w) => w.id === 'tight')!;
	const a = setup(tight, 1, 'async').run(), c = setup(tight, 1, 'cooperative').run();
	const wa = a.gs.filter((g) => g.label === 'worker'), wc = c.gs.filter((g) => g.label === 'worker');
	ok(a.stats.preempts >= 1 && c.stats.preempts === 0, `preemptions: async ${a.stats.preempts}, cooperative ${c.stats.preempts}`);
	ok(Math.max(...wa.map((g) => g.finished)) < 12_000, 'with async preemption the waiting workers should finish in about 11 ms');
	ok(Math.max(...wc.map((g) => g.finished)) > 40_000, 'with cooperative scheduling a worker should wait for the loop to end');
	done();
});

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

Published