time: read the clock from the TSC, not from counting interrupts
A timer interrupt that arrives while interrupts are disabled is not delayed,
it is lost -- the LAPIC in periodic mode holds one pending interrupt and drops
the rest. So anything the kernel does with interrupts off for longer than
10 ms makes `ticks` run slow, and it never catches up.
That has now presented twice as "uptime is wrong": once as an interrupt storm
taking 83% of the CPU, once as heavy console output on a display that is
expensive to draw to. Both times the tick was the messenger, and both times it
looked like a timekeeping bug.
monotonic() reads the TSC instead. It is a counter in the silicon that
advances whether or not this kernel is paying attention, it is already
measured against the PIT at boot, and reading it is one instruction and no
lock. wallclock(), /proc/uptime, sysinfo, getrusage and times() all go through
it now.
`ticks` keeps the job it is good at -- the scheduler preempts on it, sleep()
counts it -- where losing one means a slightly longer sleep rather than a
wrong answer about what time it is.
Lost ticks are no longer harmful, which makes them invisible, which is worse.
So /proc/interrupts now says
timer: 3383 ticks taken, 3387 expected
and names the gap when it grows, because a machine dropping timer interrupts
is spending too long with interrupts disabled, and that is worth knowing
before it turns into a clock that lies.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
This commit is contained in:
parent
65906639da
commit
bbc512a51c
7 changed files with 192 additions and 37 deletions
|
|
@ -269,5 +269,34 @@ irq_info(char *buf, int cap)
|
|||
off = emit(buf, cap, off, " [spurious]");
|
||||
off = emit(buf, cap, off, line[irq].level_low ? " level\n" : " edge\n");
|
||||
}
|
||||
|
||||
// How many timer interrupts were lost.
|
||||
//
|
||||
// The clock does not depend on this any more -- kernel/core/time.c reads the
|
||||
// TSC -- but that makes lost ticks *invisible* rather than harmless, and
|
||||
// they are not harmless: the scheduler preempts on them, and a machine
|
||||
// dropping them is a machine spending too long with interrupts disabled.
|
||||
//
|
||||
// Twice now that has been diagnosed from the outside, by noticing a clock
|
||||
// running slow, which is a terrible way to find out. This is the number to
|
||||
// look at instead: expected is elapsed time times HZ, and the gap is how
|
||||
// many the machine did not take.
|
||||
{
|
||||
uint64 sec, usec;
|
||||
monotonic(&sec, &usec);
|
||||
uint64 expect = sec * HZ + (usec * HZ) / 1000000;
|
||||
uint64 got = ticks;
|
||||
off = emit(buf, cap, off, "\ntimer: ");
|
||||
off = emit_dec(buf, cap, off, got, 0);
|
||||
off = emit(buf, cap, off, " ticks taken, ");
|
||||
off = emit_dec(buf, cap, off, expect, 0);
|
||||
off = emit(buf, cap, off, " expected");
|
||||
if (expect > got + HZ) {
|
||||
off = emit(buf, cap, off, " -- LOSING ");
|
||||
off = emit_dec(buf, cap, off, expect - got, 0);
|
||||
off = emit(buf, cap, off, ", interrupts are off too long somewhere");
|
||||
}
|
||||
off = emit(buf, cap, off, "\n");
|
||||
}
|
||||
return off;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue