# lauriewired/tailslayer architecture

> Architecture diagram of the GitHub repository lauriewired/tailslayer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lauriewired/tailslayer
- Repository: https://github.com/lauriewired/tailslayer

## Overview

1. **Repository purpose**
   - This repo is centered on **Tailslayer**, a C++ library for reducing RAM read tail latency by **replicating data across DRAM channels** and issuing **hedged reads** so the fastest replica wins.
   - The architecture is split between a **small public library API**, an **example program**, and a **discovery/benchmark suite** used to study DRAM refresh timing and channel behavior.

2. **Primary library boundary**
   - The main reusable code lives under `include/tailslayer/`, especially `hedged_reader.hpp`.
   - This appears to be a **header-oriented library interface**: consumers include the header and instantiate `tailslayer::HedgedReader` with template parameters for signal and work callbacks.
   - The repo’s public boundary is therefore the `include/tailslayer` directory plus the documented helper types like `ArgList` and `pin_to_core`.

3. **Core runtime model**
   - The library’s execution model is:
     - the caller inserts logical values,
     - each insertion is **replicated N times** internally,
     - worker threads/cores spin on separate replicas,
     - a user-defined **signal function** determines when a read should occur,
     - the **first responsive replica** supplies the value,
     - a user-defined **final work function** processes that value immediately.
   - This is the key data flow for diagramming: **logical insert → replicated storage across channels → per-replica worker wait → hedged read → first-complete result → final processing**.

4. **Channel/replica strategy**
   - The library uses **channel scrambling offsets / channel bit selection** to distribute replicas across independent DRAM channels with uncorrelated refresh schedules.
   - The README notes current library usage is **two-channel focused**, while the benchmark code supports **full N-way replication**.
   - This is an important architectural distinction: the library API is a consumer-facing abstraction over a backend that performs **address calculation and placement**.

5. **Execution and CPU affinity**
   - The repo emphasizes **core pinning** (`tailslayer::pin_to_core`) and per-replica worker placement.
   - That means the system is not just a data structure; it is a **latency-sensitive concurrent runtime pattern** where thread affinity is part of the design.
   - In an architecture diagram, this is a boundary between **application logic** and **hardware-aware execution**.

6. **Example application**
   - `tailslayer_example.cpp` is the simplest integration point and demonstrates how an application supplies:
     - the value type,
     - the signal function,
     - the final work function,
     - optional argument lists for both callbacks.
   - This example likely serves as the “reference integration path” for library consumers and shows the public API without the benchmarking/discovery machinery.

7. **Discovery/benchmark subsystem**
   - `discovery/benchmark/` is a separate subsystem for **measuring and validating hedged-read behavior**.
   - Files like `benchmark.cpp`, `stats.cpp`, `app_config.cpp`, and `hw_utils.hpp` suggest a standalone benchmark harness with:
     - configuration parsing,
     - hardware/affinity utilities,
     - benchmark execution,
     - statistics aggregation.
   - This subsystem is used to characterize performance rather than provide the reusable library API.

8. **Refresh-timing probe**
   - `discovery/trefi_probe.c` is a lower-level probe for **measuring DRAM refresh cycle timing**.
   - This is a supporting diagnostic tool, likely used to understand refresh-induced spikes and to inform the channel-scrambling / hedging strategy.
   - It sits outside the C++ library boundary and functions as hardware characterization tooling.

9. **Build and packaging structure**
   - The repo uses both **CMakeLists.txt** and **Makefile**, with an additional `discovery/benchmark/Makefile`.
   - That suggests two build surfaces:
     - the main library/example build,
     - a specialized build for discovery tooling.
   - For architecture diagrams, this is a clean separation between **library consumption** and **research/benchmark executables**.

10. **Technology profile**
   - The project is primarily **C++**, with one C probe utility.
   - It is tightly coupled to **hardware topology, DRAM channels, CPU affinity, and real-time scheduling** behavior rather than network or web infrastructure.
   - The benchmark command shown in the README uses `sudo chrt -f 99`, indicating the discovery tooling may require **real-time process priority** and elevated privileges to get stable measurements.

11. **Data and control boundaries**
   - The main data boundary is between:
     - **logical elements** managed by `HedgedReader`,
     - and the **physical replicated memory layout** hidden behind the API.
   - The main control boundary is between:
     - user-supplied callback code (`signal`, `work`),
     - and Tailslayer’s internal orchestration of replicas, placement, and read racing.
   - This separation is central to the repo’s architecture.

12. **How to draw the architecture**
   - A good diagram would show three layers:
     - **Application layer**: user code or `tailslayer_example.cpp`
     - **Library layer**: `include/tailslayer/hedged_reader.hpp` plus affinity helpers
     - **Hardware discovery layer**: `discovery/benchmark` and `discovery/trefi_probe.c`
   - The main arrows should emphasize that the application supplies callbacks and values, while the library manages replication, placement, and hedged reads across DRAM channels, and the discovery tools feed hardware characterization back into that design.

Main entry points: [`discovery/benchmark/benchmark.cpp`](https://github.com/lauriewired/tailslayer/blob/main/discovery/benchmark/benchmark.cpp) (Benchmark Run), [`tailslayer_example.cpp`](https://github.com/lauriewired/tailslayer/blob/main/tailslayer_example.cpp) (tailslayer_example.cpp), [`discovery/trefi_probe.c`](https://github.com/lauriewired/tailslayer/blob/main/discovery/trefi_probe.c) (TREFI Probe), [`include/tailslayer/hedged_reader.hpp`](https://github.com/lauriewired/tailslayer/blob/main/include/tailslayer/hedged_reader.hpp) (Hedged API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_library["Library"]
  node_api["Hedged API<br/>public header<br/>[hedged_reader.hpp]"]
  node_reader["HedgedReader<br/>template runtime"]
  node_replicas[("Replica Store<br/>memory replication")]
  node_placement["Channel Placement<br/>address mapping"]
  node_affinity["Core Pinning<br/>affinity helper"]
  node_signal["Signal Callback<br/>user callback"]
  node_work["Work Callback<br/>user callback"]
  node_race(("Hedged Read<br/>read race"))
  node_result["Result Handoff<br/>completion path"]
end

subgraph group_example["Example"]
  node_example_file["tailslayer_example.cpp<br/>example app"]
  node_example_cfg["Example Args<br/>callback args"]
end

subgraph group_discovery["Discovery"]
  node_benchmark_main["Benchmark Main<br/>benchmark entrypoint<br/>[main.cpp]"]
  node_benchmark_core["Benchmark Run<br/>measurement harness<br/>[benchmark.cpp]"]
  node_config["App Config<br/>configuration<br/>[app_config.cpp]"]
  node_stats["Stats<br/>analysis<br/>[stats.cpp]"]
  node_hw["HW Utils<br/>hardware helper<br/>[hw_utils.hpp]"]
  node_trefi["TREFI Probe<br/>c probe<br/>[trefi_probe.c]"]
end

node_api -->|"declares"| node_reader
node_reader -->|"stores"| node_replicas
node_reader -->|"maps"| node_placement
node_reader -->|"pins"| node_affinity
node_reader -->|"invokes"| node_signal
node_reader -->|"invokes"| node_work
node_replicas -->|"feeds"| node_race
node_signal -->|"triggers"| node_race
node_race -->|"selects"| node_result
node_result -->|"delivers"| node_work
node_example_file -->|"uses"| node_api
node_example_file -->|"passes"| node_example_cfg
node_benchmark_main -->|"starts"| node_benchmark_core
node_benchmark_core -->|"loads"| node_config
node_benchmark_core -->|"reports"| node_stats
node_benchmark_core -->|"uses"| node_hw
node_trefi -->|"informs"| node_hw
node_trefi -.->|"validates"| node_placement
node_benchmark_core -->|"exercises"| node_reader

click node_api "https://github.com/lauriewired/tailslayer/blob/main/include/tailslayer/hedged_reader.hpp"
click node_example_file "https://github.com/lauriewired/tailslayer/blob/main/tailslayer_example.cpp"
click node_benchmark_main "https://github.com/lauriewired/tailslayer/blob/main/discovery/benchmark/main.cpp"
click node_benchmark_core "https://github.com/lauriewired/tailslayer/blob/main/discovery/benchmark/benchmark.cpp"
click node_config "https://github.com/lauriewired/tailslayer/blob/main/discovery/benchmark/app_config.cpp"
click node_stats "https://github.com/lauriewired/tailslayer/blob/main/discovery/benchmark/stats.cpp"
click node_hw "https://github.com/lauriewired/tailslayer/blob/main/discovery/benchmark/hw_utils.hpp"
click node_trefi "https://github.com/lauriewired/tailslayer/blob/main/discovery/trefi_probe.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_reader,node_replicas,node_placement,node_affinity,node_signal,node_work,node_race,node_result toneBlue
class node_example_file,node_example_cfg toneAmber
class node_benchmark_main,node_benchmark_core,node_config,node_stats,node_hw,node_trefi toneMint
```

## Components

### Library

Public header-oriented hedged-read API and runtime behavior

- [Hedged API](https://github.com/lauriewired/tailslayer/blob/main/include/tailslayer/hedged_reader.hpp): public header · Main consumer-facing library surface for hedged reads and affinity helpers · `include/tailslayer/hedged_reader.hpp`
- **HedgedReader**: template runtime · Orchestrates inserts, replica setup, callback execution, and result selection
- **Replica Store**: memory replication · Holds each logical value in multiple physical copies
- **Channel Placement**: address mapping · Maps replicas onto DRAM channels using scrambling and offsets
- **Core Pinning**: affinity helper · Pins worker threads to specific CPU cores for latency-sensitive execution
- **Signal Callback**: user callback · User predicate that decides when a replica should be read
- **Work Callback**: user callback · User final-processing function run on the winning value
- **Hedged Read**: read race · Concurrent reads across replicas where the fastest response wins
- **Result Handoff**: completion path · Delivers the first-complete value into final processing

### Example

Reference integration path for consumers

- [tailslayer_example.cpp](https://github.com/lauriewired/tailslayer/blob/main/tailslayer_example.cpp): example app · Reference application showing how to instantiate and use the public API · `tailslayer_example.cpp`
- **Example Args**: callback args · Optional argument lists passed into signal and work callbacks

### Discovery

Benchmarking and hardware characterization tools

- [Benchmark Main](https://github.com/lauriewired/tailslayer/blob/main/discovery/benchmark/main.cpp): benchmark entrypoint · Standalone entrypoint for discovery benchmarks · `discovery/benchmark/main.cpp`
- [Benchmark Run](https://github.com/lauriewired/tailslayer/blob/main/discovery/benchmark/benchmark.cpp): measurement harness · Drives hedged-read experiments and timing runs · `discovery/benchmark/benchmark.cpp`
- [App Config](https://github.com/lauriewired/tailslayer/blob/main/discovery/benchmark/app_config.cpp): configuration · Parses and stores benchmark settings and run parameters · `discovery/benchmark/app_config.cpp`
- [Stats](https://github.com/lauriewired/tailslayer/blob/main/discovery/benchmark/stats.cpp): analysis · Aggregates benchmark results and summarizes observed behavior · `discovery/benchmark/stats.cpp`
- [HW Utils](https://github.com/lauriewired/tailslayer/blob/main/discovery/benchmark/hw_utils.hpp): hardware helper · Affinity and low-level hardware support for the benchmark harness · `discovery/benchmark/hw_utils.hpp`
- [TREFI Probe](https://github.com/lauriewired/tailslayer/blob/main/discovery/trefi_probe.c): c probe · Measures DRAM refresh timing to characterize refresh-induced latency spikes · `discovery/trefi_probe.c`

## Connections

- Hedged API → HedgedReader: declares · Public header exposes the main hedged-read type and helpers
- HedgedReader → Replica Store: stores · Each logical insert is replicated into multiple copies
- HedgedReader → Channel Placement: maps · Replica placement is distributed across DRAM channels
- HedgedReader → Core Pinning: pins · Worker execution is tied to specific CPU cores
- HedgedReader → Signal Callback: invokes · User callback decides when a read should be attempted
- HedgedReader → Work Callback: invokes · User callback processes the winning value
- Replica Store → Hedged Read: feeds · Replicated values become candidates in a hedged read
- Signal Callback → Hedged Read: triggers · Signal logic determines when the race begins
- Hedged Read → Result Handoff: selects · First responsive replica supplies the result
- Result Handoff → Work Callback: delivers · Winning value is handed to final processing immediately
- tailslayer_example.cpp → Hedged API: uses · Example consumes the public header interface
- tailslayer_example.cpp → Example Args: passes · Example wires callback argument lists into the API
- Benchmark Main → Benchmark Run: starts · Benchmark entrypoint launches the measurement harness
- Benchmark Run → App Config: loads · Run behavior is controlled by parsed configuration
- Benchmark Run → Stats: reports · Measured outcomes are aggregated into statistics
- Benchmark Run → HW Utils: uses · Benchmark harness relies on hardware-aware utilities
- TREFI Probe → HW Utils: informs · Refresh timing probe supports hardware characterization
- TREFI Probe → Channel Placement: validates · Probe data helps motivate channel placement strategy
- Benchmark Run → HedgedReader: exercises · Benchmark runs the hedged-read runtime under controlled conditions
