Преглед изворни кода

membench: ring-3 speed check for CPU, memory and syscalls

Times the TSC rate, an ALU loop, memcpy/memset over the libc heap and
over BSS, and the syscall rate of the lock-free clock against a BKL
syscall, three rounds each.  Written to find the ION's slow scripts.
Johannes Zellner пре 2 дана
родитељ
комит
b2fe5704ca
4 измењених фајлова са 132 додато и 3 уклоњено
  1. 1 1
      AGENTS.md
  2. 5 1
      Makefile
  3. 1 1
      docs/userland.html
  4. 125 0
      user/apps/membench/ucmd_membench.c

+ 1 - 1
AGENTS.md

@@ -2171,7 +2171,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
 `cd [path]`, `ls [path]`, `cat <path>`, `save <path> <text>`, `rm <path>`,
 `mkdir <path>`, `rmdir <path>`, `hello`, `reboot`, `poweroff`,
 `display`, `cpuinfo`, `mouseinfo`, `gfxbench`, `lsusb`, `touchpad [rightclick off|tap|corner|both] [tapclick on|off] [scroll on|off|natural|traditional]`, `lspci [id]`, `peek <addr> [len]`, `poke <addr> <val>`, `storage`, `disks` (the ngui storage manager), `meminfo`, `top`, `process` (the ngui task manager), `bootscan` (the live boot storage-scan window), `sched`, `format [-y] <device> [size]` (binary units, e.g. `format ide0 100mb` or `1gb`; no size formats the whole disk; `-y` skips the prompt), `install [-y] <target> [source|self]` (clone the live image, or write the running image; auto-detects the source, preferring `usb0`; `auto` targets the only non-USB NebOS disk),
-`ps`, `renice <pid> <weight>`, `kill <pid>`, `idle [auto|hlt|c1e|c3|c6]`, `power`, `powerinfo`, `cpufreq [auto|min|max|off|<pstate>]`, `spin [ms]`, `backlight [0-100|up|down]` (also `probe`), `keytest` (raw scancode tap), `help`, `clear`, `serial test`, `kexec`, `dbg`, `view <file>` (image viewer; PNG, baseline/progressive JPEG, animated GIF, BMP, TGA, PNM, ICO, PAM), `browser [url|file]` (disk app launcher), and `chat [config-path]` (Cloudron Chat client).
+`ps`, `renice <pid> <weight>`, `kill <pid>`, `idle [auto|hlt|c1e|c3|c6]`, `power`, `powerinfo`, `cpufreq [auto|min|max|off|<pstate>]`, `spin [ms]`, `membench` (ring-3 speed check: TSC, ALU, heap/BSS memcpy, syscall rates), `backlight [0-100|up|down]` (also `probe`), `keytest` (raw scancode tap), `help`, `clear`, `serial test`, `kexec`, `dbg`, `view <file>` (image viewer; PNG, baseline/progressive JPEG, animated GIF, BMP, TGA, PNM, ICO, PAM), `browser [url|file]` (disk app launcher), and `chat [config-path]` (Cloudron Chat client).
 
 Network: `netinfo`, `netrecv`, `netsend`, `netip [a.b.c.d]`, `dhcp` (re-run the
 DHCP client), `netdev [name]` (kernel command: show the active NIC driver or

+ 5 - 1
Makefile

@@ -386,7 +386,7 @@ UCMDS = cpuinfo ls cat save rm mkdir rmdir \
         netconsole netregs netloop netreset net100 netmac curl \
         files view sensors powerinfo mines solitaire \
         settime date gallery websrv browser chat \
-        wifi netmenu disks
+        wifi netmenu disks membench
 
 app = $(U)/apps/$(1)/ucmd_$(1)
 UCMD_ELFS = $(foreach a,$(UCMDS),$(call app,$(a)).elf)
@@ -409,6 +409,10 @@ $(call app,settime).o $(call app,solitaire).o $(U)/apps/gallery/gallery.o \
 $(U)/apps/hello/hello.o: XFLAGS =
 $(call app,settime).o $(call app,solitaire).o: OPT = -Os
 
+# membench times memcpy/memset over a libc heap buffer.
+$(call app,membench).elf: $(call app,membench).o $(UCMD_LD) $(LIBC_A)
+	$(ULINK)
+
 # websrv buffers its whole PUT/GET body on the libc heap.
 $(call app,websrv).elf: $(call app,websrv).o $(UCMD_LD) $(LIBC_A)
 	$(ULINK)

+ 1 - 1
docs/userland.html

@@ -325,7 +325,7 @@
 			</div>
 			<div class="card">
 				<h3>System</h3>
-				<p><code>cpuinfo</code> <code>meminfo</code> <code>storage</code> <code>top</code> <code>process</code> <code>mouseinfo</code> <code>touchpad</code> <code>sensors</code> <code>powerinfo</code> <code>date</code> <code>spin</code> <code>clock</code> <code>colors</code></p>
+				<p><code>cpuinfo</code> <code>meminfo</code> <code>storage</code> <code>top</code> <code>process</code> <code>mouseinfo</code> <code>touchpad</code> <code>sensors</code> <code>powerinfo</code> <code>date</code> <code>spin</code> <code>membench</code> <code>clock</code> <code>colors</code></p>
 			</div>
 			<div class="card">
 				<h3>Network</h3>

+ 125 - 0
user/apps/membench/ucmd_membench.c

@@ -0,0 +1,125 @@
+/* membench: ring-3 speed check.  Times the CPU clock, an ALU loop, memcpy/
+ * memset over a heap buffer (the libc arena; highmem frames on a >1 GB
+ * machine) and over a BSS buffer (the ELF image), and the syscall rate of
+ * the lock-free clock against a syscall that takes the big kernel lock.
+ * Three rounds each, so a process moving between CPUs shows as spread.
+ * Written to find why ring-3 code ran ~100x slower on one board. */
+
+#include "ucmd_lib.h"
+#include <stdlib.h>
+#include <string.h>
+
+#define BUF_BYTES (1024u * 1024u)
+#define ROUND_MS  300u
+
+static unsigned char bss_a[64 * 1024], bss_b[64 * 1024];
+static volatile unsigned int sink;
+
+static unsigned long long rdtsc(void)
+{
+	unsigned int lo, hi;
+
+	__asm__ volatile ("rdtsc" : "=a"(lo), "=d"(hi));
+	return ((unsigned long long)hi << 32) | lo;
+}
+
+static void put_rate(const char *what, unsigned long long n, unsigned int ms,
+		     const char *unit)
+{
+	u_puts(what);
+	u_putdec(ms ? (unsigned int)(n * 1000ull / ms) : 0);
+	u_puts(unit);
+}
+
+/* Wait for the next millisecond edge so every round starts aligned. */
+static unsigned int ms_edge(void)
+{
+	unsigned int t = u_ticks_ms();
+
+	while (u_ticks_ms() == t)
+		;
+	return t + 1;
+}
+
+static void round_tsc(void)
+{
+	unsigned int t0 = ms_edge(), t1;
+	unsigned long long c0 = rdtsc(), c1;
+
+	while ((t1 = u_ticks_ms()) - t0 < ROUND_MS)
+		;
+	c1 = rdtsc();
+	put_rate("  tsc ", (c1 - c0) / 1000ull, t1 - t0, " kHz\n");
+}
+
+static void round_alu(void)
+{
+	unsigned int t0 = ms_edge(), t1, x = 1, i;
+	unsigned long long n = 0;
+
+	do {
+		for (i = 0; i < 10000; i++)
+			x = x * 1103515245u + 12345u;
+		n += 10000;
+	} while ((t1 = u_ticks_ms()) - t0 < ROUND_MS);
+	sink = x;
+	put_rate("  alu ", n / 1000ull, t1 - t0, " k-ops/s\n");
+}
+
+static void round_copy(const char *what, unsigned char *dst,
+		       const unsigned char *src, unsigned int len, int set)
+{
+	unsigned int t0 = ms_edge(), t1;
+	unsigned long long bytes = 0;
+
+	do {
+		if (set)
+			memset(dst, (int)bytes, len);
+		else
+			memcpy(dst, src, len);
+		bytes += len;
+	} while ((t1 = u_ticks_ms()) - t0 < ROUND_MS);
+	sink = dst[len / 2];
+	put_rate(what, bytes / 1024ull, t1 - t0, " KB/s\n");
+}
+
+static void round_sys(const char *what, int num)
+{
+	unsigned int t0 = ms_edge(), t1;
+	unsigned long long n = 0;
+
+	do {
+		u_syscall0(num);
+		n++;
+	} while ((t1 = u_ticks_ms()) - t0 < ROUND_MS);
+	put_rate(what, n, t1 - t0, " calls/s\n");
+}
+
+void _start(const char *arg)
+{
+	unsigned char *a, *b;
+	int r;
+
+	(void)arg;
+	a = (unsigned char *)malloc(BUF_BYTES);
+	b = (unsigned char *)malloc(BUF_BYTES);
+	if (!a || !b) {
+		u_puts("membench: out of memory\n");
+		u_exit(1);
+	}
+	memset(a, 1, BUF_BYTES);
+	memset(b, 2, BUF_BYTES);
+	for (r = 1; r <= 3; r++) {
+		u_puts("round ");
+		u_putdec((unsigned int)r);
+		u_puts("\n");
+		round_tsc();
+		round_alu();
+		round_copy("  heap memcpy 1M ", a, b, BUF_BYTES, 0);
+		round_copy("  heap memset 1M ", a, 0, BUF_BYTES, 1);
+		round_copy("  bss  memcpy 64K ", bss_a, bss_b, sizeof(bss_a), 0);
+		round_sys("  sys ticks (no BKL) ", SYS_GET_TICKS_MS);
+		round_sys("  sys getpid (BKL) ", SYS_GETPID);
+	}
+	u_exit(0);
+}