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
|
|
@ -87,6 +87,7 @@ static const struct global_file globals[] = {
|
|||
{ "filesystems", 8 },
|
||||
{ "schfs", 9 },
|
||||
{ "sysrq", 10 },
|
||||
{ "schinit", 16 },
|
||||
{ "pci", 11 },
|
||||
{ "interrupts", 12 },
|
||||
{ "devices", 13 },
|
||||
|
|
@ -253,11 +254,13 @@ procfs_iload(struct inode *ip)
|
|||
ip->valid = 1;
|
||||
ip->nlink = 1;
|
||||
ip->major = ip->minor = 0;
|
||||
// Read-only, with one exception: /proc/sysrq is the deliberate write target
|
||||
// (`echo c > /proc/sysrq` to crash on purpose). Marking everything 2 made
|
||||
// filewrite() refuse it with "permission denied", which the USB suite
|
||||
// caught immediately -- three of its checks drive the disk through sysrq.
|
||||
ip->rwmode = (INO_IS_GLOBAL(ino) && (ino & 0xFF) == 10) ? 3 : 2;
|
||||
// Read-only, with two exceptions: /proc/sysrq is the deliberate write target
|
||||
// (`echo c > /proc/sysrq` to crash on purpose), and /proc/schinit is where
|
||||
// PID 1 publishes its job table. Marking everything 2 made filewrite()
|
||||
// refuse with "permission denied", which the USB suite caught immediately --
|
||||
// three of its checks drive the disk through sysrq.
|
||||
ip->rwmode = (INO_IS_GLOBAL(ino) &&
|
||||
((ino & 0xFF) == 10 || (ino & 0xFF) == 16)) ? 3 : 2;
|
||||
ip->showmode = 1;
|
||||
ip->atime = ip->mtime = ip->ctime = 0;
|
||||
|
||||
|
|
@ -331,6 +334,12 @@ procfs_write(struct inode *ip, int user_src, uint64 src, uint64 off, uint n)
|
|||
// generators. Everything else is read-only, and says so.
|
||||
if (INO_IS_GLOBAL(ip->inum) && (ip->inum & 0xFF) == 10)
|
||||
return proc_write_sysrq(user_src, src, n);
|
||||
// /proc/schinit is the job table, and PID 1 is the one that knows it. The
|
||||
// kernel holds the text rather than generating it: walking another
|
||||
// process's memory to answer a read is not something a procfs generator
|
||||
// should be doing, and PID 1 already knows when the table changed.
|
||||
if (INO_IS_GLOBAL(ip->inum) && (ip->inum & 0xFF) == 16)
|
||||
return proc_write_schinit(user_src, src, n);
|
||||
(void)off;
|
||||
return -EROFS;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue