Live Session Layout¶
A local live session has a session file, shared-memory segments and slots, and the Caliper viewer reads them in this layout.
flowchart TB
file[("Session File")] -->|names| ctl[("Control Table")]
file -->|names| view[("View Segment")]
ctl -->|up to 64 rows| row[View Row]
view -->|first page| head[Header Page]
head -->|then 3 slots, 1 for a series| slot[Slot]
slot -->|holds| pay[Payload]
slot -->|optional| aux[Aux Region]
slot -->|256 bytes| stats[Stats Region]
Parts of a live session, from the session file down to one slot.
Fields¶
Session File¶
Each live session has one session file, <data_root>/live/<pid>.json. The data_root is the data_dir of the client. If the client has no data_dir, it is $CALIPER_DATA_DIR, and if that variable is not set, it is the app-data folder of Caliper. On macOS, this folder is ~/Library/Application Support/Caliper by default, or the data_dir of an embedding core in the same process.
The client writes the file atomically when it opens the session and at each view change. It removes the file when it closes the session. The file gives the pid and its start time, which prevents an error when the system uses the pid again. It also gives the session token, the script, the views and the segment names.
Shared Memory¶
The client makes and unlinks page-aligned segments of two kinds. The control table is in /<session>-ctl. It has a session header and up to 64 view rows, and a generation seqlock controls access to them. The client stores magic last, and thus a magic of 0 shows that the session is "still starting".
Each view has one segment, /<session>-v<id>. It starts with a header page that contains the sync words, and then it has 3 slots, or 1 slot for a series. Each slot has a payload, an optional aux region and a 256-byte stats region.
Each process makes one shared-storage MTLBuffer from the segment that it maps, with newBufferWithBytesNoCopy. Live Streaming gives the file mode and the access rules.
The client creates and unlinks every name. A lister unlinks the names of a dead client, and the viewer only maps them. Each shared struct has a magic, a version and a struct_size, and a reader does not accept a struct that does not agree.
Protocol Constants¶
The sizes, the magic numbers and the other constants of the protocol are in include/caliper/live.h.
| Name | Value | Description |
|---|---|---|
CALIPER_LIVE_PROTOCOL_VERSION |
2u |
The version of the shared memory protocol. A reader refuses any other version. |
CALIPER_LIVE_CTL_MAGIC |
0x4c54434556494c43ull |
The magic number of the control segment, the ASCII text "CLIVECTL" in little-endian order. |
CALIPER_LIVE_SEG_MAGIC |
0x4745534556494c43ull |
The magic number of a view segment, the ASCII text "CLIVESEG" in little-endian order. |
CALIPER_LIVE_MAX_VIEWS |
64 |
The number of rows in the control table, and thus the largest number of views in one session. |
CALIPER_LIVE_SLOTS |
3 |
The number of slots of every view kind except a series, which has 1 slot. |
CALIPER_LIVE_NAME_MAX |
64 |
The size in bytes of a view name, with the terminating NUL. |
CALIPER_LIVE_SEGNAME_MAX |
32 |
The size in bytes of a shared memory name, with the terminating NUL. It fits the 31 characters that macOS allows. |
CALIPER_LIVE_SESSION_MAX |
20 |
The largest number of characters of a session token. The characters are A-Z, a-z, 0-9, ., _ and -. |
CALIPER_LIVE_SCRIPT_MAX |
128 |
The size in bytes of a script name, with the terminating NUL. Added in protocol v2. |
CALIPER_LIVE_STATS_BYTES |
256 |
The size in bytes of the stats region of one slot. It holds 64 float values. |
CALIPER_LIVE_SLOT_NONE |
0xffffffffu |
The slot index that means no slot. A claim or a published slot has this value when it is free or empty. |
CALIPER_LIVE_ATTACH_WINDOW_NS |
1000000000ull |
The age in nanoseconds below which a viewer heartbeat counts as attached. caliper_live_client_attached uses it. |
Enums¶
The kind of a view, the state of a table row and the state of a session are enums.
CaliperLiveKind¶
The kind of a view, which fixes the shape and the type of its payload.
The values are the same as the kinds of caliper/live_views.h.
| Constant | Value | Description |
|---|---|---|
CALIPER_LIVE_KIND_NONE |
0 |
No kind. |
CALIPER_LIVE_KIND_HEATMAP |
1 |
A heatmap, an (H,W) array of float that the viewer colormaps. |
CALIPER_LIVE_KIND_IMAGE |
2 |
An image, an (H,W,4) array of uint8 in RGBA order. |
CALIPER_LIVE_KIND_POINTS |
3 |
A point cloud, an (N,3) array of float positions with an optional (N,) array of float in the aux region. |
CALIPER_LIVE_KIND_SERIES |
4 |
A series, an (M,) array of float that is an append-only ring in one slot. |
CALIPER_LIVE_KIND_HISTOGRAM |
5 |
A histogram, a (B,) array of float bin counts over the range from stats[0] to stats[1]. |
CaliperLiveEntryState¶
The state of one row of the control table.
| Constant | Value | Description |
|---|---|---|
CALIPER_LIVE_ENTRY_FREE |
0 |
The row is free. |
CALIPER_LIVE_ENTRY_ACTIVE |
1 |
The row describes a live view. |
CaliperLiveSessionState¶
The state of a session, which the client keeps in the control segment.
| Constant | Value | Description |
|---|---|---|
CALIPER_LIVE_SESSION_OPEN |
1 |
The session is open. |
CALIPER_LIVE_SESSION_CLOSED |
2 |
The client closed the session cleanly and unlinked the shared memory names. |
Snapshot Flags¶
The flags field of a slot holds these bits.
| Name | Value | Description |
|---|---|---|
CALIPER_LIVE_SNAP_AUTO_VMIN |
1u << 0 |
Bit flag for a lower end of the range that comes from stats[0], the GPU minimum. Without it, the range uses vmin. |
CALIPER_LIVE_SNAP_AUTO_VMAX |
1u << 1 |
Bit flag for an upper end of the range that comes from stats[1], the GPU maximum. Without it, the range uses vmax. |
CALIPER_LIVE_SNAP_RAW |
1u << 2 |
Bit flag for a heatmap or an image that the viewer draws 1:1 at the origin. Tests use it. Added in protocol v2. |
Shared Structs¶
The control segment and each view segment hold these structs. They are append-only, as the section on versions explains.
CaliperLiveViewEntry¶
One row of the control table.
The client writes a row while the generation of the control segment is odd. The viewer reads a row as a snapshot between two equal even generations.
| Field | Type | Description |
|---|---|---|
struct_size |
uint32_t |
sizeof(CaliperLiveViewEntry) as the client built it. |
state |
uint32_t |
The state of the row, a CaliperLiveEntryState value. |
kind |
uint32_t |
The kind of the view, a CaliperLiveKind value. |
dtype |
int32_t |
The CaliperDType of the payload. |
ndim |
uint32_t |
The number of dimensions of the payload. |
slot_count |
uint32_t |
The number of slots of the view, CALIPER_LIVE_SLOTS, or 1 for a series. |
shape |
int64_t[4] |
The extent of each dimension of the payload. |
view_id |
uint64_t |
The id of the view. It is unique in the session, the client never reuses it, and it names the view segment. |
slot_bytes |
uint64_t |
The size in bytes of the payload of one snapshot, as an exact extent. |
stats_bytes |
uint64_t |
The size in bytes of the stats region of one slot, CALIPER_LIVE_STATS_BYTES. |
segment_bytes |
uint64_t |
The length in bytes of the whole mapping of the view segment. |
name |
char[CALIPER_LIVE_NAME_MAX] |
The name of the view, NUL-terminated. |
segment |
char[CALIPER_LIVE_SEGNAME_MAX] |
The shared memory name of the view segment, NUL-terminated. |
aux_bytes |
uint64_t |
The size in bytes of the aux region of one slot, which holds float values, or 0 for no aux region. Added in protocol v2. |
aux_count |
int64_t |
The number of float elements in the aux region, or 0 for none. Added in protocol v2. |
CaliperLiveControl¶
The header of the control segment /<session>-ctl, which holds the session header and a table of up to CALIPER_LIVE_MAX_VIEWS views.
The client stores magic last, with release order, when it opens the session. Thus a magic of 0 means that the session is still starting. The segment has one viewer at most, because the viewer fields have one writer.
| Field | Type | Description |
|---|---|---|
magic |
uint64_t |
CALIPER_LIVE_CTL_MAGIC. The client stores it last, with release order. |
version |
uint32_t |
CALIPER_LIVE_PROTOCOL_VERSION. |
struct_size |
uint32_t |
sizeof(CaliperLiveControl) as the client built it. |
entry_size |
uint32_t |
sizeof(CaliperLiveViewEntry) as the client built it. |
max_views |
uint32_t |
The number of rows in views, CALIPER_LIVE_MAX_VIEWS. |
client_pid |
int32_t |
The process id of the client. |
state |
uint32_t |
The state of the session, a CaliperLiveSessionState value. Access it with atomic operations. |
generation |
uint64_t |
A seqlock over views. The client makes it odd while it edits the table. Access it with atomic operations. |
viewer_pid |
int32_t |
The process id of the attached viewer, or 0 for no viewer. The viewer writes it with atomic operations, and a detaching viewer sets it and viewer_heartbeat_ns to 0. |
reserved0 |
uint32_t |
Reserved. |
viewer_heartbeat_ns |
uint64_t |
The time of the last poll of the viewer, in nanoseconds of CLOCK_UPTIME_RAW, or 0 for no viewer. |
reserved |
uint64_t[6] |
Reserved for later versions. |
views |
CaliperLiveViewEntry[CALIPER_LIVE_MAX_VIEWS] |
The table of views. The viewer reads it again when generation changes. |
start_unix_ms |
uint64_t |
The wall clock time of the opening of the session, in milliseconds since the Unix epoch. The client writes it before magic. |
session |
char[CALIPER_LIVE_SESSION_MAX+4] |
The session token, NUL-terminated. The client writes it before magic. |
script |
char[CALIPER_LIVE_SCRIPT_MAX] |
The name of the script, for example train.py, NUL-terminated. The client writes it before magic. |
CaliperLiveSlotMeta¶
The metadata of one slot. The client writes it before it publishes the slot.
| Field | Type | Description |
|---|---|---|
seq |
uint64_t |
The publish sequence number that this slot carries. |
stamp_ns |
uint64_t |
The CLOCK_UPTIME_RAW time of the client at the show() call, in nanoseconds. |
colormap |
int32_t |
The CaliperColormap of a heatmap or of continuous points. |
flags |
uint32_t |
The mapping flags, a combination of the CALIPER_LIVE_SNAP_* bits. |
vmin, vmax |
float |
The fixed range, vmin for the lower end and vmax for the upper end, for the ends that are not automatic. |
reserved |
uint64_t[2] |
Reserved for later versions. |
series_total |
uint64_t |
For a series, the number of values appended so far. Added in protocol v2. |
points_dims |
int32_t |
For points, the number of dimensions of a point, 2 or 3. Added in protocol v2. |
label_count |
int32_t |
For points, the number of categorical classes in the aux region, or 0 when the aux region holds values. Added in protocol v2. |
stats_count |
int32_t |
The number of meaningful float values in the stats region of the slot. Added in protocol v2. |
reserved1 |
uint32_t |
Reserved. |
CaliperLiveSegmentHeader¶
The header page of a view segment /<session>-v<id>.
The header page is followed by the slots. The sync words are accessed with atomic operations on the shared mapping.
| Field | Type | Description |
|---|---|---|
magic |
uint64_t |
CALIPER_LIVE_SEG_MAGIC. |
version |
uint32_t |
CALIPER_LIVE_PROTOCOL_VERSION. |
struct_size |
uint32_t |
sizeof(CaliperLiveSegmentHeader) as the writer built it. |
writer_pid |
int32_t |
The process id of the client that writes the segment. |
slot_count |
uint32_t |
The number of slots, CALIPER_LIVE_SLOTS, or 1 for a series. |
view_id |
uint64_t |
The id of the view, the same as view_id in its table row. |
payload_bytes |
uint64_t |
The size in bytes of the payload of one snapshot, as an exact extent. |
stats_bytes |
uint64_t |
The size in bytes of the stats region of one slot. |
segment_bytes |
uint64_t |
The length in bytes of the whole mapping of the segment. |
slot_offset |
uint64_t[CALIPER_LIVE_SLOTS] |
The byte offset of the payload of each slot. Each payload is page-aligned and row-major. |
stats_offset |
uint64_t[CALIPER_LIVE_SLOTS] |
The byte offset of the stats region of each slot. It holds float values, the minimum first and the maximum second, then values of the kind. |
seq |
uint64_t |
The number of publishes so far. The client stores it last, with release order. |
published_slot |
uint32_t |
The index of the newest completed snapshot, or CALIPER_LIVE_SLOT_NONE. |
viewer_reading_slot |
uint32_t |
The slot that the viewer applied last, which is its first claim, or CALIPER_LIVE_SLOT_NONE. |
stamp_ns |
uint64_t |
The stamp_ns of the latest publish. |
dropped_client |
uint64_t |
The number of snapshots that the client dropped because no slot was free. |
viewer_seq |
uint64_t |
The last seq that the viewer drew. The viewer writes it. |
skipped_viewer |
uint64_t |
The number of publishes that the viewer never drew. The viewer writes it. |
reserved |
uint64_t[4] |
Reserved for later versions. |
slot |
CaliperLiveSlotMeta[CALIPER_LIVE_SLOTS] |
The metadata of each slot. |
kind |
uint32_t |
The kind of the view, a CaliperLiveKind value. Added in protocol v2. |
viewer_prev_slot |
uint32_t |
The slot that the viewer applied before viewer_reading_slot, which is its second claim, or CALIPER_LIVE_SLOT_NONE. Access it with atomic operations. Added in protocol v2. |
aux_bytes |
uint64_t |
The size in bytes of the aux region of one slot, or 0 for none. Added in protocol v2. |
aux_offset |
uint64_t[CALIPER_LIVE_SLOTS] |
The byte offset of the aux region of each slot. The region holds point labels or values as float. Added in protocol v2. |
Slot Synchronization¶
The viewer writes a heartbeat into the control table each time it polls. caliper_live_client_attached reads it, and with no viewer the client takes no snapshot at all. A snapshot is one GPU-to-GPU copy on the torch stream into a free slot, and the viewer samples at its own frame rate. Thus it can skip snapshots by design.
Publish and claim are lock-free, and the sync words use the atomic builtins of the compiler on the shared mapping. The two sides store with seq_cst. Thus the client sees the viewer's claim and does not use the slot, or the viewer sees the new publish.
The viewer holds at most two claims for each view. viewer_reading_slot is the snapshot that it applied last, and a points view draws it every frame. viewer_prev_slot is the one before it, until the GPU read of it retires. The client may write a slot that is not published_slot, not a claim and not one of its own pending slots. If no such slot is left, the client drops the new snapshot and counts it in dropped_client. Thus it never overwrites a snapshot in flight, and the oldest pending snapshot always completes and publishes.
The client publishes after the GPU work of the snapshot completes, which it finds with a polled torch.mps.Event. It fills the slot metadata, stores published_slot with seq_cst, and then adds 1 to seq with release order. To take a snapshot, the viewer sees that seq changed and stores viewer_prev_slot as its current claim. Then it loads published_slot, stores viewer_reading_slot with seq_cst, and loads published_slot again. If the slot moved, it tries again. It takes a new snapshot only when viewer_prev_slot is free again, and it polls for this and never waits.
caliper_core_live_view_read_done polls the completion handles of the renderer, and thus no part of Caliper waits for the viewer's GPU reads. The claim words have one reader only, and thus a session has one viewer.
Slot Claims¶
The time that the viewer holds a slot is different for each view kind, because it changes with how the kind reads the payload.
| Kind | Payload Read | Slot Release |
|---|---|---|
| Heatmap, image | Once per apply, into the view's texture | When every apply's device update has completed, the last one included |
| Points | Redrawn on the GPU when payload, camera or window size changes | The current payload stays claimed; a superseded one is free once the last frame that drew it retired |
| Series | One append-only ring slot; the viewer copies the window it draws | Never claimed (an overwrite during the copy is a one-sample glitch at the old end) |
| Histogram | Bin counts copied at apply | At once |
Versions¶
The live protocol is at v2 in include/caliper/live.h, and the embed typed views are at v1.3 in include/caliper/live_views.h.
The fields of the shared structs are append-only. Version 2 appended the aux region, the second claim, the slot metadata for points, series and histograms, and the session identity. A reader refuses another magic or protocol version, and it refuses a struct_size that is smaller than its own. The dtype values are the CaliperDType values of caliper/tensor.h.
Threading¶
A client function may run on any one thread at a time for each client, and the Python glue serializes them with the GIL. caliper_live_client_attached may run on any thread, and so may the listing functions. The viewer functions, caliper_core_live_*, run on the frame thread only, like the embed views that they drive.
Source Files¶
| Component | Identifier | Source |
|---|---|---|
| Snapshot Ring | SnapshotRing |
python/caliper/_ring.py |
| Session Transport | SessionTransport |
python/caliper/live.py |
| Window Transport | WindowTransport |
python/caliper/_inproc.py |
| Session Reader | ShmSource |
src/host/live/live_session.mm; a Source as declared in src/host/live/live_source.h, read by live_core |
| Viewer Core | live_core |
src/host/live/live_core.cpp |
| Hosted Viewer | The Caliper app's hosted Caliper Embed | src/host/live/live_attach.h |
| Caliper Embed | caliper_core_live_apply(), where both transports end |
include/caliper/live_views.h |