dpzmick.com

LFSRs and Bit Mixers for Random Block Orders

In my previous post, we accidentally invented a "Galois LFSR," sort of. At the SC26 conference this year, fascinatingly, LFSRs showed up again, this time as a random number generator for the IO500 storage benchmark suite. During the IO500 session, the team asked for feedback on their approach. Claude and I tried out a different technique I'm familiar with from random number generation, and I think the result is pretty neat.

Storage systems are good at guessing what you'll read next (readahead, prefetching, caching). Random(ish) workloads are common, so IO500 has a random read test (ior-rnd4K-easy-read) that reads 4 KiB blocks of a file in random order. The random order defeats readahead and prefetching, so the test isn't just measuring how well the filesystem predicts the workload.

In the test, every process in the cluster reads a file that another process wrote earlier in the benchmark, 4 KiB at a time, all in parallel. The read order needs a few properties:

The order doesn't need to be cryptographically secure, though. It just needs to look random enough that the filesystem can't predict it.

Ideally, you'd make a list of all the blocks and shuffle it. But IO500 has to handle files of up to 2^34 blocks (64 TiB), so the list could need 128 GiB per process. A node can be running dozens of processes.

Instead, IO500 generates this order with a linear feedback shift register (LFSR). An LFSR is a register of k bits. On each step, we XOR a few of the register's bits together; those bit positions are called the "taps". Then every bit shifts left by one, the top bit falls off, and the XOR result goes into the empty bottom position.

Here's a 3-bit LFSR with taps on its top two bits:

uint32_t x = 1;
for (int i = 0; i < 7; i++) {
  printf("%u ", x);
  // read the taps, bits 2 and 1
  uint32_t tap2 = (x >> 2) & 1;
  uint32_t tap1 = (x >> 1) & 1;
  // xor the taps
  uint32_t bit = tap2 ^ tap1;
  // shift; keeping only the low
  // 3 bits drops the top bit
  x = (x << 1) & 0b111;
  // xor result into the bottom
  x = x | bit;
}
// prints: 1 2 5 3 7 6 4

With the right taps, the LFSR steps through every value in [1, 2^k - 1] exactly once before repeating, using only k bits of state. This satisfies the "each block once" property. But an LFSR always covers a power-of-two range, and most files aren't a power-of-two number of blocks. To handle this, IO500 splits the file into power-of-two sub-ranges, each with its own LFSR. To pick the next block, it first selects a sub-range at random, then clocks that sub-range's LFSR. This is a bit complex!

The LFSR's order also has some visible structure. If we plot each block read against the block read right after it, we see:

LFSR: each block read plotted against the next block read, 1000 blocks LFSR 0 999 this read's block next read's block 0 999

The technique I had in mind shuffles with a bit mixer instead. Claude dug up a nice reference for it: Kensler's correlated multi-jittered sampling memo from Pixar, which uses the same trick to shuffle sample points. The mixer is built from reversible operations, similar to the finalizers at the end of hash functions. David Stafford's Better Bit Mixing is one of my favorite posts about how these mixers are built.

For example, this could be a bit mixer:

x ^= key;     // undo: xor again
x ^= x >> s;  // undo: any s >= 1 works
x = x * odd;  // undo: odd has an inverse

Since every step is reversible, we can take the output, run the steps backwards, and get the original x back. So for k-bit numbers, the mixer maps each x in [0, 2^k) to a different y in [0, 2^k). In other words, the mixer is a bijection. If we sweep x from 0 to 2^k - 1, we get every value exactly once, in a scrambled order!

Files usually don't have exactly 2^k blocks, though. For a file with N blocks, round N up to the next power of two, 2^k, and use a k-bit mixer (for 10 blocks, that's a 4-bit mixer over 16 values).

Then, for the i'th block, if mixer(i) lands outside the range, apply the mixer again until it lands inside the range (this is called cycle walking). The result is still a bijection on [0, N), and since 2^k is less than 2N, it takes fewer than two tries on average:

Cycle walking: the mixer shuffles 0..15; block 0 maps to 13, which is outside 0..9, so the mixer is applied again until it lands on 7 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 the 10 real blocks outside, keep going position 0: 0 → 13 → 10 → 7

Kensler's version uses fixed 32-bit constants. For the example PR I put up, Claude built a 64-bit mixer from the constants in the xxhash64 finalizer. Here's the core of it (the PR has a few more details, linked at the end):

x ^= key & mask;
x ^= x >> sa;
x = (x * PRIME64_2) & mask;
x ^= x >> sb;
x = (x * PRIME64_3) & mask;
x ^= x >> sa;

The mask keeps the low k bits, and the shifts scale with k (sa is about k/2, sb is about k/3), so the same code works for any file size.

Here's the same plot as before, this time for the bit mixer:

bit mixer: each block read plotted against the next block read, 1000 blocks bit mixer 0 999 this read's block next read's block 0 999

For the full details, see the writeup I posted for the IO500 folks in IO500/rnd-test#1. I also put up an example PR for IOR, the I/O benchmark tool IO500 uses to run its tests.

Bit mixers for random (enough) numbers are a useful technique to know about!

home switch-color-mode