# The data API

LedgerMath's data API, at `https://api.ledgermath.com`, is a tree of static JSON files: the table of contents, the list of every Series, the benchmarks' lists, a file per Series and per month of it, and the price files, at most one a day, each kind with a JSON schema, which a reader reaches by following links from the table of contents; and, linked from them, the evidence behind the numbers: Run files, boot summaries and blobs.

## What it is

The table of contents describes LedgerMath and the API in its own words:

> LedgerMath runs small benchmarks on real AWS machines several times a day and keeps every execution as a Run. A Series is the Runs of one benchmark, parameter set, build, instance type and Availability Zone ID; a versioned Method turns its Runs into a Derivation. This is version 1 of the data API: static JSON files cut from the Derivations and the daily price files, each kind with a JSON schema. A Series' file holds each Method's summary and its newest Runs; every Run's point is in the file of the month it started in, and months says when such a file changes.
>
> (`about`, in the table of contents)

There is no server: every file is a static file. LedgerMath is scheduled to update the files several times a day. Run files, boot summaries, blobs and price files are written once and not changed; a month file's bytes change only for the causes the table of contents' `months` gives (quoted below). Every address a reader needs is written in the files: a reader never builds one from an id, but for three templates the table of contents gives, the address of a blob from its hash, of a Series' file from its id, and of a benchmark's list from the benchmark's name.

## How a reader walks it

1. **The table of contents**, [https://api.ledgermath.com/v1/index.json](https://api.ledgermath.com/v1/index.json) ([the page](/docs/api/table-of-contents)). It links the list of every Series (`series.url`), the price files (`prices`) and the schemas (`schemas`); it names benchmarks' lists, as many as fit its budget, each by the benchmark's name (`benchmarks.list[].name`) with its address (`benchmarks.list[].url`) and how many Series it holds (`benchmarks.list[].series`), and `benchmarks.unlisted` counts the lists it does not name; and it gives the newest rounds of boots (`recent_rounds`).
2. **A benchmark's list**, `https://api.ledgermath.com/v1/benchmarks/<benchmark>.json` ([the page](/docs/api/benchmark-list)), at the address `benchmarks.list[].url` gives, or at `benchmarks.benchmark_file_url_template` with the benchmark's name. One row per Series it lists, under `series`: its id, parameters, build, instance type and zone ID, its counts, a summary of each Method's figures, and the address of the Series' file (`url`). A list may leave rows out, and says how many (`left_out`), and a benchmark whose name is not 1 to 64 characters, a lower-case letter, then lower-case letters, digits and underscores, has no list; [the list of every Series](/docs/api/series-list), `https://api.ledgermath.com/v1/series.json`, is the complete list.
3. **A Series' file**, `https://api.ledgermath.com/v1/series/<id>.json` ([the page](/docs/api/series)), at its row's `url`, or at `series.series_file_url_template` with the Series' id. Each Method's summary figures and definitions, its groups, the points of its newest Runs, and `months`: each month a Run of the Series started in, with the month file's address, sha256 and size.
4. **A month file**, `https://api.ledgermath.com/v1/series/<id>/<yyyy-mm>.json` ([the page](/docs/api/series-month)), at `months[].url`. Every point of the Runs that started that month. Check its sha256 against the one `months` lists before using it (below).
5. **The evidence.** Each point links its Run file (`evidence`), `https://api.ledgermath.com/runs/<yyyy>/<mm>/<dd>/<id>.json` ([the page](/docs/api/run)), and its boot's summary (`boot_summary`), `https://api.ledgermath.com/boots/<yyyy>/<mm>/<dd>/<boot>.json` (below), which is null when there is no boot summary the API can link. A Run file names by hash, `sha256:<hex>`, its binary, its source archive, its runner, its resolved compiler flags, its stdout, its stderr and its dumps; each is a blob at `https://api.ledgermath.com/blobs/sha256/<hex>`, the table of contents' `evidence.blob_url_template`. The stdout blob is what the binary printed, each line meant to be one event ([the page](/docs/api/events)).

The prices are beside the Series: at most one file a day, `https://api.ledgermath.com/v1/prices/<yyyy-mm-dd>.json` ([the page](/docs/api/prices)), listed in the table of contents (`prices.files`, the newest also as `prices.newest`); a price is cited by the file's path, the SKU and the rate code ([Prices](/docs/prices)). The machines LedgerMath measures are on [Machines](/docs/machines); the words it uses, such as Run, Series, boot and Method, in the [glossary](/docs/glossary); how each Method makes its numbers, on [Methods](/docs/methods).

## When a month file does not match its Series file

A month file changes in place, at its address, so a Series' file and a month file fetched one after the other can be of different updates: the update may have stopped between them, or a cache may hold one of them from before the other changed. That is why each entry of a Series file's `months` gives the month file's sha256 and size. The rule a reader follows:

1. Compute the sha256 of the month file's bytes as fetched, and compare it with the `sha256` its Series file lists for that month (and the size with `bytes`).
2. If they differ, fetch the Series file again, then the month file, and compare again.
3. While they still differ, use neither, and try again later.

A copy of a month file kept from earlier is current while its sha256 is the one the Series file lists now. The table of contents gives the contract in full, with when a month file changes:

> Each Series is split by month: every Run's point is in the file of the month the Run started in (UTC, by the Run's start), with series v1's entry for each pair of consecutive Runs under the month of the later Run, and the Series' file lists its months. For a given layout (the layout of v1/index.json), a month file's bytes change only when (a) a Run that started in that month is added, (b) a boot summary that was missing arrives, (c) a Run arrives late, having started before a Run of the Series already in the files, in that month or an earlier one, or (d) a Method first applies to the Series; otherwise they do not change. These four are the only causes for a given layout: layout increases when LedgerMath changes how the files are written, and only then may a file's bytes change without one of them. A past month is therefore not immutable: each entry of the Series file's months gives its month file's sha256 and size in bytes, so a reader holding a copy can tell whether it is current, and fetches the file again only when the sha256 differs. A Series file and a month file fetched together can still be from different updates of the files: a cache may hold one of them from before the other changed, and an update that stopped halfway leaves them so until the next one. So a reader checks the sha256 of every month file it fetches against the one its Series file lists; on a mismatch, it fetches the Series file again, then the month file, and compares again; while they still differ, it uses neither, and tries again later. What each cause changes: (a) the month gains the Run's point, its pair entry and its count and span; (b) the point's boot_summary turns from null into the summary's address, and nothing else; (c) as (a), and in that month and every later one the points after the late Run may change only what depends on earlier Runs: their group, and requests v1's through and group_through_as_of_run (through's counts rise by exactly what the late Runs it pools add, and not at all when none of them is counted), and the pair entry of the Run right after it then comes from the late Run; (d) the Method's block appears, under its key, in every month file of the Series.
>
> (`months`, in the table of contents)

The table of contents, a benchmark's list, the list of every Series and a Series' file can be of different updates too, and the table of contents says what a reader does then:

> The table of contents, a benchmark's list, the list of every Series and a Series' file are each whole, but fetched together they can be of different updates of the files: an update writes them one after another, a cache may hold one of them from before another changed, and an update that stopped part way leaves them so until the next one. Their counts and rows can then differ: one may hold a Series, or count a Run, that another does not. So a reader takes each file as true of its own update; where two of them differ, it fetches them again later, and they agree once both are of one update.
>
> (from `benchmarks.about`, in the table of contents)

## Versioning

> Version 1 only ever gains fields. A field, once in a v1 file, keeps its name, its type and its meaning, and a reader ignores any field it does not know: that is why no object in the schemas index, series-list, benchmark, series and series-month forbids members it does not list. A new Method, or a new version of one, is a new key beside the old ones. A change that would break a reader is published as v2, beside v1. The price files are the exception: each is the day's price file as LedgerMath keeps it, byte for byte, and follows its own schema, ledgermath.prices/1, whose root and LedgerMath's own objects are closed (AWS's objects within it are open).
>
> (`versioning`, in the table of contents)

Beside that promise, the table of contents gives `layout`, a number that rises when LedgerMath changes how the files are written. A reader holding copies of month files made under another layout fetches them again ([the table of contents](/docs/api/table-of-contents)).

## The figures

> Every number is exactly as the Methods' Derivations hold it: nothing is rounded, and nothing is computed that the Derivations do not hold, apart from counts of a Series' Runs (in all, in a month, in a group, counted and not) and of the boots they were made on, the start times of its oldest and newest Run (in all, in a month, in a group), taken from its points, the counts of each benchmark's Series and of their Runs and the start of their newest Run, and how many Series are in no benchmark's list (benchmarks), each month file's sha256 and size in bytes, and the counts of each of the newest rounds of boots (recent_rounds), taken from the boot summaries and the points. A duration is in nanoseconds of the Run's clock unless its field's name says otherwise; a moment is a UTC time, to the nanosecond, as the Run records it.
>
> (`figures`, in the table of contents)

## The evidence

> The evidence is served under the same address as the API: each point links its Run file (evidence), at runs/\<yyyy\>/\<mm\>/\<dd\>/\<id\>.json, and its boot's summary (boot_summary), at boots/\<yyyy\>/\<mm\>/\<dd\>/\<boot\>.json. A Run file names its binary, streams and dumps by hash, sha256:\<hex\>; each is a blob at blob_url_template with {hex} replaced by the 64 hex digits. The blob its stdout names is what the binary printed, one event a line; the schema events, in schemas, gives the shape each line must have for a Method to count the Run.
>
> (`evidence.about`, in the table of contents)

**The boot summary.** A boot is one fresh launch of an EC2 instance of one instance type: the box builds and runs the executions LedgerMath's schedule names for its type, then shuts down. An execution that runs normally leaves a Run. At the end, the box writes one boot summary, at `https://api.ledgermath.com/boots/<yyyy>/<mm>/<dd>/<boot>.json`, dated by the UTC day the runner started. Its parts are `runs`, the Runs the boot left; `not_run`, the executions it did not start because it was told to stop or a Service's inputs could not be had, each with the reason; `build_failures`, the builds that failed and the binaries that could not be started; `left_out`, the executions of entries that are for other instance types; and `error`, when the runner stopped on one. The summary is not a complete account in every case: a boot that was cut off, or that stopped on an error, can leave executions it does not list, Runs with no summary, or no summary, so an absence proves nothing. A point links its boot's summary as `boot_summary`, null when there is no boot summary the API can link. The boot summary has no schema yet, so it has no page of its own here.

**The environment dump.** Around every Run the box records what it reports about itself, raw: a Run file names two dumps by hash, `environment.before`, taken just before the binary was launched, and `environment.after`, taken just after it exited. Each hash names the dump's index, a blob at `https://api.ledgermath.com/blobs/sha256/<hex>`: a JSON object whose `schema` is `ledgermath.dump/1`, with `taken_at` and `sections`, which names each section by hash, `sha256:<hex>`; each section is a blob of its own, at the same template. So a reader goes from a Run file to a section in two fetches: the index, by `environment.before` or `environment.after`, then the section, by its hash in `sections`. A section is a JSON object of what the box read: mostly each file's text under the file's path, and each command's exit code and output under its command line, written `$ …`. The sections taken around every Run are `cpuid` (the raw cpuid leaves of every CPU, when the runner has its tool for them), `cpuinfo` (`/proc/cpuinfo`), `lscpu` (the output of `lscpu -J`: the processor's model, its count of CPUs, cores per socket and threads per core, its caches), `sys_cpu` (the CPUs' topology, caches, vulnerabilities, microcode, frequency scaling and idle states, from `/sys/devices/system/cpu`), `kernel` (its version and command line, the operating system's release, settings of the kernel and of its memory management, its clock source, `uname -a`), `memory` (`/proc/meminfo`, the load average, uptime, swap, pressure, and each memory node's meminfo), `devices` (the NVMe controllers, the disks and their queues, the machine's vendor, product and BIOS as its firmware names them, `lsblk`, `findmnt`, `xfs_info`), `block_stat` (each block device's I/O counters), `nvme_stats` (each Amazon NVMe disk's own counters and latency histograms), `network` (routes, MTUs, TCP and socket settings, resolver files, the network driver), `net_counters` (the network card's and the kernel's traffic counters), `process` (the runner's limits, cgroup and the CPUs it may run on), `packages` (every installed package and version) and `provider` (what the instance metadata service answered). Taken once per boot, and named in every index of that boot: `ec2_instance_type`, `ec2_volumes`, `ec2_instance`, `ec2_route_tables` and `ec2_vpc_endpoints` (what the EC2 API says of the instance type, the volumes attached, the instance and the path out of its subnet), and `provision` (the record of preparing the disks at boot). Not every index names all of them, and a section an index names may hold, in place of what it reads, a reason it was not taken or the error its command met, so a reader looks inside it. Method `series` v1 reads six sections of the before-dump (`cpuid`, `lscpu`, `sys_cpu`, `kernel`, `process`, `packages`) to put Runs into groups, and compares the hash of every section of the before-dump between consecutive Runs (its `split_on`, `not_split_on` and `differed`, on [Methods](/docs/methods)). The dump has no schema yet, so it has no page of its own here.

## The files

- [The table of contents](/docs/api/table-of-contents): `v1/index.json` is where a reader of LedgerMath's data API starts. It gives the API's rules in words; the addresses of the list of every Series, of the price files and of the schemas; benchmarks' lists of Series with their counts, as many as fit its budget; templates for the address of a benchmark's list, of a Series file and of a blob; and the newest rounds of boots.
- [The list of Series](/docs/api/series-list): `v1/series.json` lists every Series, one row each with what the Series is, its counts, a summary of each Method's figures and the address of its own file, and gives each Method's own words for how its numbers are made, with the Run files the Method could not read.
- [A benchmark's list of Series](/docs/api/benchmark-list): `v1/benchmarks/<benchmark>.json` holds one benchmark's rows of the list of every Series, the same byte for byte when both files are of one update, so that a reader can find the benchmark's Series and their figures without fetching the whole list; it may leave rows out, and says how many (`left_out`).
- [A Series file](/docs/api/series): `v1/series/<id>.json` holds one Series: what it is, each Method's summary figures and definitions, the points of its newest Runs, and the list of its month files, each with the file's sha256 and size.
- [A month file](/docs/api/series-month): `v1/series/<id>/<yyyy-mm>.json` holds every point of the Runs of one Series that started in one month (UTC), each with its Run file's sha256 and address, and its boot summary's address when there is one the API can link; and Method `series` v1's entries of what differed between consecutive Runs.
- [A price file](/docs/api/prices): `v1/prices/<yyyy-mm-dd>.json` is one day's copy of AWS's public On-Demand prices for what LedgerMath measures: the records a versioned rule set selected from AWS's price lists, kept verbatim, added once and never edited.
- [A Run file](/docs/api/run): A Run file records one execution of one benchmark binary, with one parameter set, on one box, whether it succeeded or failed: the evidence each Method's figures rest on.
- [An event: one line of a Run's stdout](/docs/api/events): A benchmark prints a stream of events to its stdout, one JSON object per line; the events schema gives the shape of each line, and a Method counts a Run only when every line of its stdout has that shape.
