import { writeFileSync } from 'node:fs'; import { simulate } from './cache.mjs'; const workloads = { sequential: { label: 'Sequential 32-bit words', addresses: Array.from({ length: 128 }, (_, i) => i * 4), description: '128 word accesses, 4-byte stride (spatial locality)' }, conflict: { label: 'Two competing addresses', addresses: Array.from({ length: 96 }, (_, i) => i % 2 ? 128 : 0), description: '96 accesses alternating byte addresses 0 and 128 (mapping conflict)' }, workingSet: { label: 'Repeated four-line working set', addresses: Array.from({ length: 96 }, (_, i) => (i % 4) * 16), description: '96 accesses to four 16-byte blocks (temporal locality)' }, replacement: { label: 'Revisit before replacement', addresses: [0, 64, 0, 128, 0], description: 'Five accesses that distinguish LRU from FIFO in one two-way set' }, }; const configurations = { directLRU: { sizeBytes: 128, lineBytes: 16, ways: 1, policy: 'LRU' }, twoWayLRU: { sizeBytes: 128, lineBytes: 16, ways: 2, policy: 'LRU' }, twoWayFIFO: { sizeBytes: 128, lineBytes: 16, ways: 2, policy: 'FIFO' }, }; const results = { model: 'Read-only lookup cache; 32-bit byte addresses; no timing or write model', configurations, workloads: {} }; for (const [key, workload] of Object.entries(workloads)) { results.workloads[key] = { label: workload.label, description: workload.description, accesses: workload.addresses.length, results: Object.fromEntries(Object.entries(configurations).map(([name, config]) => { const { hits, misses, evictions, hitRate } = simulate(workload.addresses, config); return [name, { hits, misses, evictions, hitRatePercent: Number((hitRate * 100).toFixed(2)) }]; })), }; } if (process.argv.includes('--write')) { writeFileSync(new URL('./results.json', import.meta.url), JSON.stringify(results, null, 2) + '\n'); } for (const entry of Object.values(results.workloads)) { console.log(`\n${entry.label} — ${entry.accesses} accesses`); for (const [name, result] of Object.entries(entry.results)) { console.log(` ${name}: ${result.hits} hits / ${result.misses} misses / ${result.evictions} evictions (${result.hitRatePercent}% hit)`); } }