diff options
| author | eunhhu <[email protected]> | 2026-09-30 17:59:47 +0100 |
|---|---|---|
| committer | eunhhu <[email protected]> | 2026-09-30 17:59:47 +0100 |
| commit | 402c19311b247889b36ff3be834d9fab19555af7 (patch) | |
| tree | e243c7b97b8bdaf894c0487ccbcfc4def633f5d4 /research/experiments/025-i64-cost/pcount.c | |
| parent | 07047c391f32c43827c0da145f6c89edb5007941 (diff) | |
| download | pure-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/pcount.c')
| -rw-r--r-- | research/experiments/025-i64-cost/pcount.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/research/experiments/025-i64-cost/pcount.c b/research/experiments/025-i64-cost/pcount.c new file mode 100644 index 0000000..77519b2 --- /dev/null +++ b/research/experiments/025-i64-cost/pcount.c @@ -0,0 +1,58 @@ +/* pcount PID: count user-mode instructions and cycles of every thread of + * PID from "ready" (on stderr) until stdin closes; prints them as JSON. + * perf_event_open alone: a perf_event_paranoid of 2 allows it for one's own + * processes, and no perf tool is needed. */ +#define _GNU_SOURCE +#include <dirent.h> +#include <linux/perf_event.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/ioctl.h> +#include <sys/syscall.h> +#include <unistd.h> + +static int open_counter(pid_t tid, uint64_t config) { + struct perf_event_attr attr; + memset(&attr, 0, sizeof(attr)); + attr.size = sizeof(attr); + attr.type = PERF_TYPE_HARDWARE; + attr.config = config; + attr.disabled = 1; + attr.exclude_kernel = 1; + attr.exclude_hv = 1; + return (int)syscall(SYS_perf_event_open, &attr, tid, -1, -1, 0); +} + +int main(int argc, char **argv) { + if (argc != 2) return 2; + char path[64]; + snprintf(path, sizeof(path), "/proc/%s/task", argv[1]); + DIR *dir = opendir(path); + if (!dir) { perror("opendir"); return 1; } + int fds[512][2], n = 0; + struct dirent *e; + while ((e = readdir(dir)) && n < 512) { + if (e->d_name[0] == '.') continue; + pid_t tid = atoi(e->d_name); + fds[n][0] = open_counter(tid, PERF_COUNT_HW_INSTRUCTIONS); + fds[n][1] = open_counter(tid, PERF_COUNT_HW_CPU_CYCLES); + if (fds[n][0] < 0 || fds[n][1] < 0) { perror("perf_event_open"); return 1; } + ++n; + } + closedir(dir); + for (int i = 0; i < n; ++i) for (int k = 0; k < 2; ++k) ioctl(fds[i][k], PERF_EVENT_IOC_RESET, 0), ioctl(fds[i][k], PERF_EVENT_IOC_ENABLE, 0); + fprintf(stderr, "ready\n"); + char buffer[64]; + while (fread(buffer, 1, sizeof(buffer), stdin) > 0) {} + uint64_t total[2] = {0, 0}; + for (int i = 0; i < n; ++i) + for (int k = 0; k < 2; ++k) { + uint64_t v = 0; + ioctl(fds[i][k], PERF_EVENT_IOC_DISABLE, 0); + if (read(fds[i][k], &v, sizeof(v)) == sizeof(v)) total[k] += v; + } + printf("{\"threads\": %d, \"instructions\": %llu, \"cycles\": %llu}\n", n, (unsigned long long)total[0], (unsigned long long)total[1]); + return 0; +} |
