1. What you actually want to measure
| Goal | What you need to know | Typical “signal” you’ll see |
|---|
| Instruction latency | How many CPU cycles a single instruction takes | A single‑number (e.g., 4 cycles) |
| Cache size | The first array size that causes a capacity miss | A sudden jump in latency when the array grows past the cache |
| Cache associativity | How many lines can share the same set before conflict misses appear | A second jump in latency when you touch more than N lines that map to the same set |
Because the “signal” is a step (or a few steps) in a noisy measurement, you need a statistical test that can tell you whether a jump is real or just random fluctuation.
2. Basic statistical building blocks
| Concept | Formula | What it tells you |
|---|
| Mean | (\bar{x} = \frac{1}{n}\sum_{i=1}^{n}x_i) | Central tendency of your samples |
| Standard deviation | (s = \sqrt{\frac{1}{n-1}\sum_{i=1}^{n}(x_i-\bar{x})^2}) | How spread out the samples are |
| Standard error of the mean | (SE = \frac{s}{\sqrt{n}}) | How far the sample mean is likely to be from the true mean |
| t‑interval (99 % confidence) | (\bar{x} \pm t_{n-1,0.995},SE) | With 99 % probability the true mean lies in this interval |
| Margin of error | (ME = t_{n-1,0.995},SE) | The ± value you can quote (e.g., “± 2 cycles”) |
Why a t‑interval?
For small sample sizes (≤ 30) or when the population variance is unknown, the Student‑t distribution is the correct choice. For very large (n) the t‑value approaches 2.576 for 99 % confidence.
3. Designing the experiment
3.1 Measuring a single instruction
-
Isolate the instruction
// Example: measure latency of a single LDR
uint64_t start, end;
asm volatile(
"mov x0, #0\n" // dummy register
"rdcycle %0\n" // read cycle counter
"ldr x1, [x0]\n" // instruction under test
"rdcycle %1\n"
: "=r"(start), "=r"(end)
:
: "x0", "x1");
uint64_t latency = end - start; // in cycles
-
Repeat many times
const int N = 1'000'000;
uint64_t latencies[N];
for (int i=0; i<N; ++i) {
latencies[i] = measure_one();
}
-
Remove loop overhead
Measure an empty loop (no ldr) the same number of times and subtract its mean from the mean of the real loop.
-
Compute statistics
double mean = average(latencies, N);
double sd = stddev(latencies, N);
double se = sd / sqrt(N);
double me = 2.576 * se; // 99 % confidence
printf("Latency: %.2f ± %.2f cycles (99 %% CI)\n", mean, me);
3.2 Measuring cache size
- Choose a stride that is a multiple of the cache line size (e.g., 64 B).
- Vary the array size from a few lines up to a few megabytes.
- For each size run a tight loop that touches every line once (or a fixed number of times).
- Record the average latency per access.
- Plot latency vs. array size.
- Look for a step: the point where latency jumps indicates the cache capacity.
3.3 Measuring associativity
- Pick a set of addresses that all map to the same cache set.
On ARM, the set index is usually the low‑order bits of the address after masking out the line offset.
- Access
k different lines that map to that set, where k ranges from 1 up to, say, 16.
- Measure the latency for each
k.
- Plot latency vs.
k.
- The first
k where latency jumps is the associativity.
4. Statistical tests for “jumps”
4.1 Two‑sample t‑test (cache size)
Suppose you suspect a jump at size S.
- Group A: all measurements with array size < S.
- Group B: all measurements with array size ≥ S.
Compute the means (\bar{x}_A, \bar{x}_B) and standard deviations (s_A, s_B).
The t‑statistic:
[
t = \frac{\bar{x}_A - \bar{x}_B}
{\sqrt{\frac{s_A^2}{n_A} + \frac{s_B^2}{n_B}}}
]
Compare t to the critical value (t_{df,,0.975}) (two‑tailed, 95 % confidence).
If (|t|) is larger, you can reject the null hypothesis that the two groups have the same mean, i.e., the jump is statistically significant.
4.2 Change‑point detection (associativity)
A more robust way is to use a change‑point algorithm (e.g., Bayesian change‑point, CUSUM).
You feed the latency sequence (k = 1, 2, 3, …) and the algorithm returns the most probable point where the mean changes.
If the algorithm flags a change at k = A, you can say the associativity is A.
4.3 Confidence intervals for the jump size
Once you have a jump at k = A, you can compute the difference between the two means and its standard error:
[
SE_{\Delta} = \sqrt{\frac{s_A^2}{n_A} + \frac{s_B^2}{n_B}}
]
A 99 % confidence interval for the jump magnitude is:
[
(\bar{x}_A - \bar{x}B) \pm 2.576 , SE{\Delta}
]
If this interval does not contain zero, the jump is significant.
5. Practical tips to reduce noise
| Problem | Fix |
|---|
| CPU frequency scaling | Lock the CPU to a fixed frequency (e.g., via cpufreq on Linux or sysctl on macOS). |
| Other processes | Run the benchmark on a dedicated core (taskset or sched_setaffinity). |
| Cache warm‑up | Flush the cache before each run (e.g., clflush or dcacheflush). |
| Branch prediction | Use nop or pause to avoid mispredicted branches. |
| Memory ordering | Use dsb sy or isb to serialize memory accesses if needed. |
| Large sample size | 10⁶–10⁷ iterations gives a margin of error < 1 cycle for most cases. |
6. Example: 99 % confidence on a 4‑cycle latency
const int N = 5'000'000; // 5 M samples
double lat[N];
for (int i=0; i<N; ++i) lat[i] = measure_one();
double mean = average(lat, N);
double sd = stddev(lat, N);
double se = sd / sqrt(N);
double me = 2.576 * se; // 99 % CI
printf("Latency: %.2f ± %.2f cycles (99 %% CI)\n", mean, me);
If mean comes out to 4.02 cycles and me is 0.03 cycles, you can confidently say the instruction takes 4 ± 0.03 cycles with 99 % confidence.
7. Summary checklist
- Choose a high‑resolution counter (PMU or
rdcycle).
- Isolate the event and remove overhead.
- Run a very large number of iterations (≥ 10⁶).
- Compute mean, SD, SE, and 99 % CI.
- For cache size: plot latency vs. array size, use a two‑sample t‑test to confirm a jump.
- For associativity: plot latency vs. number of lines in the same set, use change‑point detection or a t‑test between successive
k values.
- Report the mean latency and the confidence interval; for cache properties, report the size/associativity and the statistical evidence (p‑value or confidence interval for the jump).
With these steps you can turn a coarse timer into a statistically rigorous micro‑architectural measurement.