Performance: cycles and cache

Measuring cycles

RDTSC reads the time-stamp counter into EDX:EAX: the number of cycles since reset. Sandwich the code you measure between two RDTSC reads and subtract, and the difference is the cycle count:

rdtsc mov r8, rdx shl r8, 32 or r8, rax ; start ; ... measured code ... rdtsc shl rdx, 32 or rax, rdx sub rax, r8 ; cycles elapsed

Cache lines

Memory moves in cache lines of 64 bytes. Touching one byte loads its whole line, so the next 63 bytes are nearly free. A stride that skips lines (every 1024th byte, say) touches a new line every time and runs far slower.

Sequential versus random

A sequential scan of a large array runs at memory bandwidth; a random walk of the same array stalls on misses. When a profiler says a program is memory-bound, the fix is usually a better layout - arrays of structs instead of linked lists, smaller elements, or a sorted index.

This complete program reads the counter twice, subtracts the readings and prints a marker; the count itself changes on every run, so the page does not show it:

default rel %define SYS_WRITE 1 %define SYS_EXIT 60 section .data msg db "rdtsc", 10 section .text global _start _start: rdtsc shl rdx, 32 or rax, rdx mov r8, rax ; start rdtsc shl rdx, 32 or rax, rdx sub rax, r8 ; cycles elapsed mov rax, SYS_WRITE mov rdi, 1 mov rsi, msg mov rdx, 6 syscall mov rax, SYS_EXIT xor rdi, rdi syscall

nasm -f elf64 -o perf.o perf.asm
ld -o perf perf.o
./perf
echo $?

rdtsc
0

The first snippet is a fragment: it measures the elapsed cycles, and its count changes on every run. The complete program was assembled with NASM 3.01, linked with GNU ld 2.45.1, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which registers does RDTSC fill with the time-stamp counter?

    Answer

    EDX:EAX

    The 64-bit counter comes back split across EDX and EAX.

  2. How do you build the full counter after RDTSC?

    Answer

    Shift RDX left by 32 and OR it with RAX

    The high half is RDX, so it moves into the top 32 bits before the OR.

  3. What is the difference of two RDTSC readings?

    Answer

    The number of cycles that elapsed

    The counter ticks with the reference clock, so the difference counts cycles.

  4. How large is a cache line on x86-64?

    Answer

    64 bytes

    One load brings a 64-byte line into the cache.

  5. Why is touching one byte followed by the next 63 nearly free?

    Answer

    The whole cache line is already loaded

    The line arrived as one unit, so neighbouring accesses hit the cache.

  6. Why is a stride of 1024 bytes slow?

    Answer

    Every step touches a new cache line

    Sixteen lines fit in 1024 bytes, so each access brings a line with no reuse.

  7. What does a memory-bound program wait for?

    Answer

    Data from memory

    The arithmetic units are idle while the load misses the cache.

  8. Which layout is usually friendlier to the cache?

    Answer

    An array of structs with the used field first

    Sequential, dense data uses every byte of each line it loads.

  9. What does this program print? default rel %define SYS_WRITE 1 %define SYS_EXIT 60 section .data msg db "rdtsc", 10 section .text global _start _start: rdtsc shl rdx, 32 or rax, rdx mov r8, rax rdtsc shl rdx, 32 or rax, rdx sub rax, r8 mov rax, SYS_WRITE mov rdi, 1 mov rsi, msg mov rdx, 6 syscall mov rax, SYS_EXIT xor rdi, rdi syscall

    Answer

    rdtsc

    The cycle count is computed but not printed; the program prints its marker.

  10. What exit status does that program return?

    Answer

    0

    The program clears rdi before sys_exit, so the status is 0.

Article author: Arthur Isaev