فهرست منبع

sched: lock-free clock syscall and BKL contention statistics

Every syscall took the big kernel lock, including SYS_GET_TICKS_MS.
QuickJS's interrupt check reads the clock every ~10000 operations, so
on a board whose kernel holds the BKL often (the ION) the browser's
JavaScript slept on the lock at each check and ran many times slower.

- syscall.c: SYS_GET_TICKS_MS is answered before the BKL is taken (one
  aligned counter read).
- js_mu.c: the interrupt hook reads the clock every 16th check only.
- sched.c: count contended acquisitions, total wait and the longest
  hold with its holder; `sched` prints and resets them.
Johannes Zellner 2 روز پیش
والد
کامیت
13e673edb3
5فایلهای تغییر یافته به همراه69 افزوده شده و 2 حذف شده
  1. 5 1
      AGENTS.md
  2. 2 0
      docs/cpu.html
  3. 49 0
      kernel/sched.c
  4. 7 0
      kernel/syscall.c
  5. 6 1
      user/apps/browser/js_mu.c

+ 5 - 1
AGENTS.md

@@ -329,7 +329,11 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   kernel stack this CPU is still unwinding.  Excess wake-up races are handled
   by skipping a queued process that is still marked running on another CPU.
   The **big kernel lock** (`bkl_*`) is a sleeping recursive mutex: every
-  ring-3 syscall and every kernel thread takes it, voluntary block/sleep
+  ring-3 syscall (except `SYS_GET_TICKS_MS`, one lock-free counter read: as
+  QuickJS's interrupt check it slept on a contended BKL every ~10000
+  operations) and every kernel thread takes it; `sched` prints the
+  contended acquisitions, total wait and longest hold (with its holder)
+  since the last `sched`.  Voluntary block/sleep
   points drop it (`sched_yield`), and long serial/raw-network waits release it
   via `bkl_pause()`/`bkl_resume()`.  A BKL holder is not timer-preempted, so it can
   safely be in the middle of kernel data structures.  Kernel threads are pinned

+ 2 - 0
docs/cpu.html

@@ -331,6 +331,8 @@
 			<li><strong>Not timer-preempted.</strong> A CPU running a BKL holder is skipped by the tick decision, so the owner can safely be in the middle of kernel data structures.</li>
 			<li><strong>Recursive.</strong> The owner may re-enter; the depth is restored across a block in the PCB's <code>bkl_saved</code> field.</li>
 			<li><strong>Sleep, not spin.</strong> A waiter blocks on the BKL wait queue and is woken when the owner releases.</li>
+			<li><strong>Lock-free clock.</strong> <code>SYS_GET_TICKS_MS</code> is answered before the lock is taken (one aligned counter read). It is the hottest syscall: QuickJS's interrupt check called it every ~10000 operations and slept on a contended lock each time, and the browser's interrupt hook now reads the clock only every 16th check.</li>
+			<li><strong>Contention statistics.</strong> <code>sched</code> prints how often a taker had to sleep, the total wait and the longest single hold with its holder, all since the previous <code>sched</code>.</li>
 			<li><strong>Idle uses trylock.</strong> The idle fallback never blocks on the BKL; kworker and kernel commands take it normally.</li>
 		</ul>
 		<div class="note">

+ 49 - 0
kernel/sched.c

@@ -50,8 +50,31 @@ static struct {
 	struct process *owner;
 	unsigned int depth;
 	struct wait_queue wait;
+	unsigned int since;     /* get_ticks_ms() when the owner took it */
 } bkl;
 
+/* BKL contention, for `sched`: how often a taker had to sleep, the total
+ * wait, and the longest single hold with its holder (reset when printed).
+ * Every syscall takes the lock, so a long holder stalls ring-3 work on the
+ * other CPUs. */
+static struct {
+	unsigned int contended, wait_ms, hold_max_ms;
+	char hold_max_name[PROC_NAME_LEN];
+} bkl_stat;
+
+static void bkl_note_release(struct process *p)
+{
+	unsigned int held = get_ticks_ms() - bkl.since;
+	unsigned int i;
+
+	if (held <= bkl_stat.hold_max_ms)
+		return;
+	bkl_stat.hold_max_ms = held;
+	for (i = 0; i + 1 < PROC_NAME_LEN && p->name[i]; i++)
+		bkl_stat.hold_max_name[i] = p->name[i];
+	bkl_stat.hold_max_name[i] = '\0';
+}
+
 unsigned int sched_lock_acquire(void)
 {
 	unsigned int flags = spin_irq_save_disable();
@@ -389,6 +412,8 @@ void bkl_lock(void)
 
 	if (!p)
 		return;
+	unsigned int t0 = 0;
+
 	if (bkl.owner == p) {
 		bkl.depth++;
 		return;
@@ -398,9 +423,16 @@ void bkl_lock(void)
 		if (!bkl.owner) {
 			bkl.owner = p;
 			bkl.depth = 1;
+			bkl.since = get_ticks_ms();
+			if (t0)
+				bkl_stat.wait_ms += bkl.since - t0;
 			sched_lock_release(flags);
 			return;
 		}
+		if (!t0) {
+			t0 = get_ticks_ms();
+			bkl_stat.contended++;
+		}
 		sched_block_on_locked(&bkl.wait);
 		sched_lock_release(flags);
 		sched_yield();
@@ -419,6 +451,7 @@ void bkl_unlock(void)
 		if (bkl.depth > 1) {
 			bkl.depth--;
 		} else {
+			bkl_note_release(p);
 			bkl.owner = 0;
 			bkl.depth = 0;
 			wake_one_locked(&bkl.wait);
@@ -442,6 +475,7 @@ int bkl_trylock(void)
 	if (!bkl.owner) {
 		bkl.owner = p;
 		bkl.depth = 1;
+		bkl.since = get_ticks_ms();
 		sched_lock_release(flags);
 		return 1;
 	}
@@ -462,6 +496,7 @@ static void bkl_yield_release(void)
 	flags = sched_lock_acquire();
 	if (bkl.owner == p) {
 		p->bkl_saved = bkl.depth;
+		bkl_note_release(p);
 		bkl.owner = 0;
 		bkl.depth = 0;
 		wake_one_locked(&bkl.wait);
@@ -907,4 +942,18 @@ void sched_print(void)
 	io_putchar('.');
 	io_putchar((char)('0' + idle_pm % 10));
 	io_putstr("%\n");
+	/* Since the last `sched`. */
+	io_putstr("bkl: contended ");
+	sched_put_dec(bkl_stat.contended);
+	io_putstr("  waited ");
+	sched_put_dec(bkl_stat.wait_ms);
+	io_putstr(" ms  longest hold ");
+	sched_put_dec(bkl_stat.hold_max_ms);
+	io_putstr(" ms (");
+	io_putstr(bkl_stat.hold_max_name[0] ? bkl_stat.hold_max_name : "-");
+	io_putstr(")\n");
+	bkl_stat.contended = 0;
+	bkl_stat.wait_ms = 0;
+	bkl_stat.hold_max_ms = 0;
+	bkl_stat.hold_max_name[0] = '\0';
 }

+ 7 - 0
kernel/syscall.c

@@ -553,6 +553,13 @@ int syscall_dispatch(int num, int a1, int a2, int a3)
 	if (p && p->state == PROC_DEAD)
 		process_exit();
 
+	/* The clock is one aligned 32-bit read and needs no lock.  It is the
+	 * hottest syscall (QuickJS's interrupt check, every poll loop), and
+	 * under the BKL a busy kernel holder made each call sleep for a
+	 * scheduler tick: the ION's browser ran its scripts 50x slower. */
+	if (num == SYS_GET_TICKS_MS)
+		return (int)get_ticks_ms();
+
 	bkl_lock();
 	r = syscall_dispatch_locked(num, a1, a2, a3);
 	bkl_unlock();

+ 6 - 1
user/apps/browser/js_mu.c

@@ -676,9 +676,14 @@ void *js_touserdata(js_State *J, int idx, const char *tag)
 static int mu_interrupt(JSRuntime *rt, void *opaque)
 {
 	js_State *J = (js_State *)opaque;
+	static unsigned int calls;
 
 	(void)rt;
-	return J->limited && (int)(u_ticks_ms() - J->deadline) >= 0;
+	/* QuickJS asks every ~10000 operations; a 20 s budget needs nothing
+	 * like that precision, and each clock read is a syscall. */
+	if (!J->limited || (++calls & 15))
+		return 0;
+	return (int)(u_ticks_ms() - J->deadline) >= 0;
 }
 
 /* An async function or a promise chain that fails with nobody listening