Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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. COUNTERS does not change the current struct.
  • There are three parts, each adding a command. One solution must pass all eleven tests.
CommandMeaningPrints
STRUCT S k, then k field linesDefine struct S; it replaces the previous struct and all of its accessesnothing
thread WRITES fieldthread writes field of the current structnothing
thread READS fieldthread reads field of the current structnothing
SHARINGReport every line where a write invalidates another thread's copya header, then one line per finding
PADRegroup and pad the current struct (Part 2)a header, then one line per group
COUNTERS n size startCheck 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:

TypeSize and alignment, in bytes
bool, char, int8_t, uint8_t1
int16_t, uint16_t2
int32_t, uint32_t, float4
int64_t, uint64_t, double8
  • An array field name[N] has N times the element's size and the element's alignment. It is named and printed as name, 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:

  1. 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).
  2. alignof(S) is the largest alignment among the fields. sizeof(S) is the end of the last field rounded up to a multiple of alignof(S).
  • The struct starts at a multiple of 64, so line L holds offsets 64 * L through 64 * L + 63.
  • A field at offset o with size s has bytes on lines o / 64 through (o + s - 1) / 64, using integer division.
  • The struct spans (sizeof(S) + 63) / 64 lines.
  • 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.

  1. There is one finding for every line L, every field f with a byte on L, every thread A that writes f, and every thread B other than A that uses at least one field with a byte on L.
  2. KIND is TRUE when B uses f itself: true sharing. B needs the value A writes, so the line has to move between their caches however the fields are arranged.
  3. KIND is FALSE when B does not use f: false sharing. B loses its copy because of bytes it never touches, the layout problem from the article.
  4. 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.
  5. Findings are ordered by L, then by f in declaration order, then by A, then by B. 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
  • N is sizeof(S); K is 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, or COUNTERS. 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 SHARING or PAD.
  • 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 N is 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.

  1. A field that no thread writes goes into the cold group, whether some threads read it or no thread uses it.
  2. 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.
  3. Groups are ordered by the position of their first field in the current field order. Fields inside a group keep that order.
  4. The rewritten struct lists the groups one after another. The first field of every group is marked alignas(64), and every other field loses any alignas(64) it had.
  5. Laid out with the Part 1 rules, every group therefore starts on a fresh line, alignof(S) is 64, and sizeof(S) rounds up to a multiple of 64. No group shares a line with another group or with whatever follows the struct in memory.
  6. The rewritten struct replaces the current one, so a later SHARING reports on the padded layout and a later PAD starts 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 ...
  • N is the new sizeof(S). D is the new size minus the size before this PAD.
  • D can be zero or negative, for example when the old struct had alignas(64) marks that the regrouping drops.
  • A group line lists name@offset for each field of the group.

For the example's TopOfBook, PAD gives three groups and sizeof 192:

FieldsUsed byPadded layout
bid, askwritten by feed, read by strategybid@0 ask@8
seq, updatesfeed aloneseq@64 updates@72
orders_sentstrategy aloneorders_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.

  1. There are n counters of size bytes each. Counter i is written by thread i and used by no other thread.
  2. The array starts start bytes after a line boundary, so counter i occupies offsets start + i * size through start + (i + 1) * size - 1.
  3. Every line that holds bytes of two or more counters is reported, in increasing order.
  4. The fix is to declare the counter type alignas(64). Each counter then takes P bytes, the smallest multiple of 64 that is at least size, 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
  • a and b are the lowest and highest counters with a byte on line L.
  • If no line holds bytes of two counters, no sharing replaces the line lines.
  • 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.

Example
Input
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
Output
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
Explanation

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.