aboutsummaryrefslogtreecommitdiffstats
path: root/research/experiments/025-i64-cost/psample.c
diff options
context:
space:
mode:
authoreunhhu <[email protected]>2026-09-30 17:59:47 +0100
committereunhhu <[email protected]>2026-09-30 17:59:47 +0100
commit402c19311b247889b36ff3be834d9fab19555af7 (patch)
treee243c7b97b8bdaf894c0487ccbcfc4def633f5d4 /research/experiments/025-i64-cost/psample.c
parent07047c391f32c43827c0da145f6c89edb5007941 (diff)
downloadpure-402c19311b247889b36ff3be834d9fab19555af7.tar.gz
pure-402c19311b247889b36ff3be834d9fab19555af7.zip
Count instructions where timing cannot tell 3% apart (110)
016's second stage passed its criterion: six interleaved pairs against ee21aec gave TPC-C (RAM, W=1) -0.6% at the median and the bank's queries 0.0% to +2.2%, no run interfered. The ranks query was slower all six times, and chasing it showed that on this Pi timing cannot separate 1-3%: two servers asked in turns disagree with themselves (ee21aec to e02bfc9 +1.6%, e02bfc9 to 481642f -1.2%, ee21aec to 481642f +2.8%), and a query whose instructions grew 0.6% ran 3.5% faster. Instructions do not move so: pcount.c counts the server's user-mode instructions with perf_event_open (no perf here), and psample.c samples where they go. ranks had grown 0.4% in instructions (the rest was where code and data landed), per_branch 3.6% and tables 20 instructions a row, which 07047c3 halved. The six pairs of 07047c3 were interfered (another session's build, load 4-12) and are kept apart, unused.
Diffstat (limited to 'research/experiments/025-i64-cost/psample.c')
-rw-r--r--research/experiments/025-i64-cost/psample.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/research/experiments/025-i64-cost/psample.c b/research/experiments/025-i64-cost/psample.c
new file mode 100644
index 0000000..308e8ff
--- /dev/null
+++ b/research/experiments/025-i64-cost/psample.c
@@ -0,0 +1,94 @@
+/* psample PID PERIOD: sample the user-mode instruction pointer of every
+ * thread of PID once per PERIOD instructions, from "ready" (on stderr) until
+ * stdin closes; prints one hex address per sample, then "lost N". Like
+ * pcount, perf_event_open alone (the threads PID has when it starts). */
+#define _GNU_SOURCE
+#include <dirent.h>
+#include <linux/perf_event.h>
+#include <poll.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+enum { PAGES = 64, MAX_THREADS = 256 };
+static size_t page;
+static uint64_t lost;
+
+typedef struct { int fd; struct perf_event_mmap_page *meta; unsigned char *data; } Ring;
+
+static void drain(Ring *r, FILE *out) {
+ uint64_t head = __atomic_load_n(&r->meta->data_head, __ATOMIC_ACQUIRE), tail = r->meta->data_tail;
+ size_t size = PAGES * page;
+ while (tail < head) {
+ struct perf_event_header h;
+ unsigned char record[256];
+ for (size_t i = 0; i < sizeof(h); ++i) ((unsigned char *)&h)[i] = r->data[(tail + i) % size];
+ if (h.size > sizeof(record) || h.size < sizeof(h)) break;
+ for (size_t i = 0; i < h.size; ++i) record[i] = r->data[(tail + i) % size];
+ if (h.type == PERF_RECORD_SAMPLE) {
+ uint64_t ip;
+ memcpy(&ip, record + sizeof(h), sizeof(ip));
+ fprintf(out, "%llx\n", (unsigned long long)ip);
+ } else if (h.type == PERF_RECORD_LOST) {
+ uint64_t n;
+ memcpy(&n, record + sizeof(h) + 8, sizeof(n));
+ lost += n;
+ }
+ tail += h.size;
+ }
+ __atomic_store_n(&r->meta->data_tail, tail, __ATOMIC_RELEASE);
+}
+
+int main(int argc, char **argv) {
+ if (argc != 3) return 2;
+ page = (size_t)sysconf(_SC_PAGESIZE);
+ char path[64];
+ snprintf(path, sizeof(path), "/proc/%s/task", argv[1]);
+ DIR *dir = opendir(path);
+ if (!dir) { perror("opendir"); return 1; }
+ Ring rings[MAX_THREADS];
+ int n = 0;
+ struct dirent *e;
+ while ((e = readdir(dir)) && n < MAX_THREADS) {
+ if (e->d_name[0] == '.') continue;
+ struct perf_event_attr attr;
+ memset(&attr, 0, sizeof(attr));
+ attr.size = sizeof(attr);
+ attr.type = PERF_TYPE_HARDWARE;
+ attr.config = PERF_COUNT_HW_INSTRUCTIONS;
+ attr.sample_period = strtoull(argv[2], NULL, 10);
+ attr.sample_type = PERF_SAMPLE_IP;
+ attr.disabled = 1;
+ attr.exclude_kernel = 1;
+ attr.exclude_hv = 1;
+ attr.wakeup_events = 1;
+ int fd = (int)syscall(SYS_perf_event_open, &attr, atoi(e->d_name), -1, -1, 0);
+ if (fd < 0) { perror("perf_event_open"); return 1; }
+ void *map = mmap(NULL, (PAGES + 1) * page, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (map == MAP_FAILED) { perror("mmap"); return 1; }
+ rings[n++] = (Ring){fd, map, (unsigned char *)map + page};
+ }
+ closedir(dir);
+ FILE *out = stdout;
+ for (int i = 0; i < n; ++i) ioctl(rings[i].fd, PERF_EVENT_IOC_ENABLE, 0);
+ fprintf(stderr, "ready\n");
+ struct pollfd fds[MAX_THREADS + 1];
+ for (;;) {
+ fds[0] = (struct pollfd){.fd = 0, .events = POLLIN};
+ for (int i = 0; i < n; ++i) fds[i + 1] = (struct pollfd){.fd = rings[i].fd, .events = POLLIN};
+ poll(fds, (nfds_t)n + 1, 100);
+ for (int i = 0; i < n; ++i) drain(&rings[i], out);
+ if (fds[0].revents) {
+ char buffer[64];
+ if (read(0, buffer, sizeof(buffer)) <= 0) break;
+ }
+ }
+ for (int i = 0; i < n; ++i) ioctl(rings[i].fd, PERF_EVENT_IOC_DISABLE, 0), drain(&rings[i], out);
+ fprintf(out, "lost %llu\n", (unsigned long long)lost);
+ return 0;
+}