|
@@ -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);
|
|
|
|
|
+}
|