From 4a1bfefa4ac3741bd4992fa2db1d88e2e4172ffa Mon Sep 17 00:00:00 2001 From: Jim Wilcoxson Date: Sat, 18 Apr 2020 03:11:56 +0000 Subject: [PATCH] Minimize clock inaccuracies during system boot The initial estimate for instructions per millisec (instpermses) was much too low for modern CPUs, and was also only updated every 5 seconds. Combined, these were causing the clock to be erratic during the first 5-15 seconds of system boot. This was easily noticed by running the MIPS benchmark program right after a system boot, where instead of delaying 5 seconds, it might only delay 2 seconds, and then would report a bogus MIPS rating. Waiting a minute and running it again would give an accurate rating. Adjusting the instpermsec initial value from 2000 to 40000 and IPMSTIME time from 5000 ms to 1000 ms (every second vs 5 seconds) fixes the problem. --- em.c | 8 ++++++-- emdev.h | 8 ++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/em.c b/em.c index ffbb698..0511c8f 100644 --- a/em.c +++ b/em.c @@ -4408,12 +4408,16 @@ int main (int argc, char **argv) { } setvbuf(stderr, NULL, _IONBF, 0); - /* initialize global variables */ + /* initialize global variables + + NOTE: if instpermsec is off by more than a factor of 2, it causes + some minor clock skew problems during the first few seconds of + system boot. There is debug code in emdev.h/devcp to see this. */ gv.intvec = -1; gv.instcount = 0; gv.inhcount = 0; - gv.instpermsec = 2000; + gv.instpermsec = 40000; gv.livereglim = 040; gv.mapvacalls = 0; gv.mapvamisses = 0; diff --git a/emdev.h b/emdev.h index 2491bc9..dfb333a 100644 --- a/emdev.h +++ b/emdev.h @@ -1530,15 +1530,15 @@ int devcp (int class, int func, int device) { else if (cpuid >= 15) /* newer machines: 250 ticks/second */ clkpic = -1250; TRACE(T_INST, "Clock PIC %d requested, set to %d\n", getcrs16s(A), clkpic); - SETCLKPOLL; ticks = -1; + SETCLKPOLL; } else if (func == 07) { - TRACE(T_INST, "Clock control register set to '%o\n", getcrs16(A)); if (getcrs16(A) & 020) clkrate = 102.4; else clkrate = 3.2; + TRACE(T_INST, "Clock control register set to '%o\n", getcrs16(A)); ticks = -1; SETCLKPOLL; @@ -1586,7 +1586,7 @@ int devcp (int class, int func, int device) { targetticks = elapsedms/(-clkpic*clkrate/1000); #if 0 if (abs(ticks-targetticks) > 5) - printf("\nClock: target=%d, ticks=%d, offset=%d\n", targetticks, ticks, ticks-targetticks); + printf("\nClock: target=%d, ticks=%d, offset=%d, ipms=%d, poll=%d\n", targetticks, ticks, ticks-targetticks, gv.instpermsec, devpoll[device]); #endif /* if the clock gets way out of whack (eg, because of a host @@ -1629,7 +1629,7 @@ int devcp (int class, int func, int device) { XXX: this code should probably be done whether or not the clock is running */ -#define IPMTIME 5000 +#define IPMTIME 1000 if ((gv.instcount < previnstcount) || (gv.instcount-previnstcount > gv.instpermsec*IPMTIME)) { if (gv.instcount-previnstcount > gv.instpermsec*IPMTIME) {