False Sharing Finder
Prerequisites: False Sharing, Cache Coherency and MESI, Copies, Moves, and Object Layout
The AAPL feed handler, the strategy, and the risk thread share a few small structs: a top-of-book snapshot, some counters, a block of limits. Each thread touches only some fields, and none races on a field.
Cache lines can still bounce between cores, depending only on where the fields land. Build a tool that reports every 64-byte line one thread's writes take from another, and computes a padded layout that keeps only the sharing the program needs.
What to implement
process_commands(commands)returns the printed lines, in command order. Each command is one line.- A test can define several structs one after another.
COUNTERSdoes not change the current struct. - There are three parts, each adding a command. One solution must pass all eleven tests.
| Command | Meaning | Prints |
|---|---|---|
STRUCT S k, then k field lines | Define struct S; it replaces the previous struct and all of its accesses | nothing |
thread WRITES field | thread writes field of the current struct | nothing |
thread READS field | thread reads field of the current struct | nothing |
SHARING | Report every line where a write invalidates another thread's copy | a header, then one line per finding |
PAD | Regroup and pad the current struct (Part 2) | a header, then one line per group |
COUNTERS n size start | Check an array of per-thread counters (Part 3) | one line per shared line, then one summary line |
Field lines
A field line is type name, or type name[N] for an array of N elements, optionally preceded by alignas(64). Each type's alignment equals its size:
| Type | Size and alignment, in bytes |
|---|---|
bool, char, int8_t, uint8_t | 1 |
int16_t, uint16_t | 2 |
int32_t, uint32_t, float | 4 |
int64_t, uint64_t, double | 8 |
- An array field
name[N]hasNtimes the element's size and the element's alignment. It is named and printed asname, without the[N]. - A field declared
alignas(64)has alignment 64 instead of its type's alignment.
Layout and lines
Fields are laid out with the rules from Copies, Moves, and Object Layout:
- Fields are placed in declaration order, starting at offset 0. Each field starts at the smallest multiple of its alignment at or after the end of the previous field (its offset plus its size).
alignof(S)is the largest alignment among the fields.sizeof(S)is the end of the last field rounded up to a multiple ofalignof(S).
- The struct starts at a multiple of 64, so line
Lholds offsets64 * Lthrough64 * L + 63. - A field at offset
owith sizeshas bytes on lineso / 64through(o + s - 1) / 64, using integer division. - The struct spans
(sizeof(S) + 63) / 64lines. - A thread uses a field if it reads or writes it. It then touches all of that field's bytes on every line the field covers.
Part 1: Find the shared lines
MESI tracks lines, not fields. When thread A writes field f, every other thread holding a copy of a line that f touches loses that copy, whether or not it uses f. SHARING reports each of these invalidations.
- There is one finding for every line
L, every fieldfwith a byte onL, every threadAthat writesf, and every threadBother thanAthat uses at least one field with a byte onL. KINDisTRUEwhenBusesfitself: true sharing.Bneeds the valueAwrites, so the line has to move between their caches however the fields are arranged.KINDisFALSEwhenBdoes not usef: false sharing.Bloses its copy because of bytes it never touches, the layout problem from the article.- A field that no thread writes never causes a finding, so read-only data, however many threads read it, is never reported on its own.
- Findings are ordered by
L, then byfin declaration order, then byA, then byB. Threads are ordered by their first appearance among the struct's access lines.
Output of SHARING: a header, then one line per finding.
struct S size N lines K line L KIND A f B
Nissizeof(S);Kis the number of lines.- With no findings, the header is followed by the single line
no sharing.
Limits and guarantees
- Struct, field, and thread names are identifiers made of letters, digits, and underscores. Thread names are never
STRUCT,SHARING,PAD, orCOUNTERS. The fields of a struct have distinct names. - Every access line names a field of the current struct. A struct's access lines all come after its field lines and before its first
SHARINGorPAD. - An access may be declared twice, and a thread may both read and write the same field.
- A struct has 1 to 32 fields and at most 8 threads. An array length
Nis between 1 and 64. At most 200 commands.
Tests 1–4 and 11 cover this part.
Part 2: Pad the struct
PAD rewrites the current struct so that no line is falsely shared.
- A field that no thread writes goes into the cold group, whether some threads read it or no thread uses it.
- A field that at least one thread writes goes into a group keyed by the set of threads that use it. Two written fields share a group exactly when the same threads use them.
- Groups are ordered by the position of their first field in the current field order. Fields inside a group keep that order.
- The rewritten struct lists the groups one after another. The first field of every group is marked
alignas(64), and every other field loses anyalignas(64)it had. - Laid out with the Part 1 rules, every group therefore starts on a fresh line,
alignof(S)is 64, andsizeof(S)rounds up to a multiple of 64. No group shares a line with another group or with whatever follows the struct in memory. - The rewritten struct replaces the current one, so a later
SHARINGreports on the padded layout and a laterPADstarts from it.
Output of PAD: a header, then one line per group, in group order.
padded S size N added D group name@offset name@offset ...
Nis the newsizeof(S).Dis the new size minus the size before thisPAD.Dcan be zero or negative, for example when the old struct hadalignas(64)marks that the regrouping drops.- A
groupline listsname@offsetfor each field of the group.
For the example's TopOfBook, PAD gives three groups and sizeof 192:
| Fields | Used by | Padded layout |
|---|---|---|
bid, ask | written by feed, read by strategy | bid@0 ask@8 |
seq, updates | feed alone | seq@64 updates@72 |
orders_sent | strategy alone | orders_sent@128 |
A SHARING after the PAD reports only the two TRUE findings for bid and ask.
Tests 5–8 cover this part.
Part 3: Per-thread counter arrays
SHARING works field by field, so an array field written by two threads shows up as true sharing even when each thread writes only its own element. COUNTERS n size start checks such an array element by element.
- There are
ncounters ofsizebytes each. Counteriis written by threadiand used by no other thread. - The array starts
startbytes after a line boundary, so counterioccupies offsetsstart + i * sizethroughstart + (i + 1) * size - 1. - Every line that holds bytes of two or more counters is reported, in increasing order.
- The fix is to declare the counter type
alignas(64). Each counter then takesPbytes, the smallest multiple of 64 that is at leastsize, and the array starts on a line boundary.
Output of COUNTERS: the shared lines, then one summary line.
line L elements a-b padded stride P added D
aandbare the lowest and highest counters with a byte on lineL.- If no line holds bytes of two counters,
no sharingreplaces thelinelines. D = n * (P - size)is the number of bytes the padding adds.
Limits: n is between 1 and 64, size is between 1 and 256, and start is between 0 and 63.
Tests 9–10 cover this part.
26 STRUCT Counters 2 int64_t a int64_t b t1 WRITES a t2 WRITES b SHARING STRUCT PaddedCounters 2 alignas(64) int64_t a alignas(64) int64_t b t1 WRITES a t2 WRITES b SHARING STRUCT TopOfBook 5 int64_t bid int64_t ask uint64_t seq uint32_t updates uint32_t orders_sent feed WRITES bid feed WRITES ask feed WRITES seq feed WRITES updates strategy READS bid strategy READS ask strategy WRITES orders_sent SHARING
struct Counters size 16 lines 1 line 0 FALSE t1 a t2 line 0 FALSE t2 b t1 struct PaddedCounters size 128 lines 2 no sharing struct TopOfBook size 32 lines 1 line 0 TRUE feed bid strategy line 0 TRUE feed ask strategy line 0 FALSE feed seq strategy line 0 FALSE feed updates strategy line 0 FALSE strategy orders_sent feed
Counters is the packed struct from the article: a sits at offset 0 and b at offset 8, both on line 0. Each thread writes a field the other never uses, so both findings are FALSE.
In PaddedCounters both fields are alignas(64), so b starts at 64, alignof is 64, and sizeof rounds 72 up to 128. Each counter has a line to itself and nothing is reported.
TopOfBook fits in 32 bytes on one line. strategy reads bid and ask, so feed's writes to them are true sharing. strategy never touches seq or updates, so feed's writes to those are false sharing.
strategy's writes to orders_sent invalidate the copy of the line that feed needs for its own four fields.