diff --git a/komorebi-recorder/src/analysis/mod.rs b/komorebi-recorder/src/analysis/mod.rs index 173d34d4..da95f5d6 100644 --- a/komorebi-recorder/src/analysis/mod.rs +++ b/komorebi-recorder/src/analysis/mod.rs @@ -26,6 +26,7 @@ pub mod ids; pub mod invariants; pub mod logparse; pub mod lookup; +pub mod os_trace; pub mod overview; pub mod schemas; pub mod search; diff --git a/komorebi-recorder/src/analysis/os_trace.rs b/komorebi-recorder/src/analysis/os_trace.rs new file mode 100644 index 00000000..672dd990 --- /dev/null +++ b/komorebi-recorder/src/analysis/os_trace.rs @@ -0,0 +1,737 @@ +//! The OS trace around a komorebi event: what the rest of the system did because of it. +//! +//! A komorebi event is the start of a conversation: komorebi calls into the OS to move windows and +//! the OS answers, the applications whose windows moved react, and some of that reaches the OS +//! trace the recorder captured next to komorebi's own output. [`Analysis::os_trace`] answers "what +//! happened around this event?" from that trace, in two stretches: +//! +//! - **during the handler**, from the moment its handler began (where komorebi's log says) to the +//! moment the notification was received, which komorebi sends once the handler is done: +//! komorebi's own calls into the OS, and whatever the system did meanwhile; +//! - **after it**, for a short while: the applications and the system reacting. +//! +//! The records are grouped by where they came from (komorebi's own process, the system, each +//! application). That the records fall in the window does not prove the event caused them: another +//! event's handler may overlap it, and an application does things on its own. Both are said, with +//! the other events that were received in the window, rather than left to be assumed. +//! +//! It is what `trace --os` adds to a trace and what the viewer's inspector shows under an event. +//! +//! # Flavor +//! +//! The OS trace is whatever the recording host captured, and [`Analysis::os_trace`] groups it by +//! the flavor of komorebi the session recorded, because that decides what the streams hold: +//! +//! | session | the trace has | groups | +//! |---|---|---| +//! | komorebi for Mac (recorded on a Mac) | a stream for komorebi's process, one for the system, one for each application | one per stream: `komorebi`, `system`, each application by name | +//! | komorebi for Windows (recorded on Windows) | one stream of what the OS told every window (`WinEvent`s), and the Event Log | one per program found in the record, with komorebi's own windows (`komorebi.exe`) apart from everyone else's; the Event Log as `eventlog` | +//! +//! A group's [`pid`](OsGroup::pid) is set only for an application, since the system is many +//! processes. Records from the recorder itself (`komorebi-recorder`) are left out, the same way +//! the recorder is kept out of the video. +//! +//! # Limits +//! +//! The window is the handler's duration plus one second after the notification. At most 40 +//! records are listed for each group and 400 in all, in time order; the counts are of everything, +//! and the notes say when records were left out. At most 12 overlapping events are named. +//! +//! The trace has to have been recorded: a session made without one (`--no-oslog`), or on a host +//! that has none, answers with no groups and a note saying so. + +use super::Analysis; +use super::envelope; +use super::timespec::Edge; +use super::timespec::Lookup; +use crate::timeline; +use crate::timeline::Filter; +use color_eyre::eyre; +use schemars::JsonSchema; +use serde::Serialize; + +/// How long after a notification the reaction to it is looked for. +const REACTION_NS: u64 = 1_000_000_000; +/// How far back from the notification to look when komorebi's log does not say when the handler +/// began (it logs the start of a handler only at a verbose level). +const UNKNOWN_HANDLER_NS: u64 = 250_000_000; +/// Most records listed for one source; the rest are counted. +const PER_GROUP: usize = 40; +/// Most records listed in all. +const TOTAL: usize = 400; +/// Most other events named as having overlapped the window. +const OVERLAPS_LISTED: usize = 12; + +/// Where a group of OS trace records came from. +/// +/// The groups of an [`OsTraceData`] are ordered by kind in the order of the variants: komorebi +/// first, then the system, then applications, then the rest. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, JsonSchema)] +#[serde(rename_all = "kebab-case")] +pub enum GroupKind { + /// komorebi's own process: its calls into the OS. + Komorebi, + /// System daemons and frameworks: the accessibility and window-server conversation. + System, + /// One application komorebi manages (or was told to trace). + App, + /// Anything else the recording traced. + Other, +} + +/// Whether a record fell while komorebi's handler was running or after its notification. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, JsonSchema)] +#[serde(rename_all = "kebab-case")] +pub enum Phase { + /// While komorebi's handler was running. + During, + /// After the notification was sent. + After, +} + +/// One record of the OS trace. +#[derive(Debug, Clone, PartialEq, Serialize, JsonSchema)] +pub struct OsRecord { + /// When the record was logged, in seconds of video time. + pub video_s: f64, + /// Whether it fell during the handler or after the notification. + pub phase: Phase, + /// The process that logged it, if the record has one. + pub pid: Option, + /// The record as `at` would show it: time, pid, process, subsystem and message. + pub text: String, + /// The file of the session the record is in, relative to the session directory, to read the + /// whole record. + pub file: String, + /// The line of `file` the record is on, counting from 1. + pub line: u64, +} + +/// The records of one source. +#[derive(Debug, Clone, PartialEq, Serialize, JsonSchema)] +pub struct OsGroup { + /// Where the records came from. + pub kind: GroupKind, + /// `komorebi`, `system` or the application's name; on a Windows session, the program's name + /// (`chrome.exe`) or `eventlog`. + pub label: String, + /// The source as `at --source` takes it, e.g. `oslog:app:Ghostty`. On a Windows session + /// several groups share the one stream `oslog:system`, so it does not tell them apart: the + /// `label` does. + pub source: String, + /// The process of an application's records (that of the group's first record); `None` for the + /// other kinds of group, which are many processes. + pub pid: Option, + /// How many records the group had while komorebi's handler ran. + pub during: usize, + /// How many records the group had after the notification, within the reaction window. + pub after: usize, + /// The first of them in time order: at most 40 for a group, and 400 over all groups. Fewer + /// than `during + after` when some were left out ([`OsGroup::omitted`]). + pub records: Vec, +} + +impl OsGroup { + /// Returns how many records the group had in the whole window, `during + after`, whether or + /// not they are listed. + #[must_use] + pub fn total(&self) -> usize { + self.during + self.after + } + + /// Returns how many records were counted but not listed. + #[must_use] + pub fn omitted(&self) -> usize { + self.total().saturating_sub(self.records.len()) + } +} + +/// The OS trace around one komorebi event: the `os_trace` of a `trace --os`. +#[derive(Debug, Clone, PartialEq, Serialize, JsonSchema)] +pub struct OsTraceData { + /// The event. + pub seq: u64, + /// When the handler began, in seconds of video time. If komorebi's log does not say, it is + /// taken to be 250 ms before the notification. + pub started_s: f64, + /// When the notification was received, which is when komorebi finished the handler, in seconds + /// of video time. + pub received_s: f64, + /// When the reaction window ended, 1 s after the notification, in seconds of video time. + pub until_s: f64, + /// Every record in the window, listed or not, without the recorder's own. + pub total: usize, + /// The records by source: komorebi's own first, then the system, then the applications, then + /// anything else, the busiest first within each. + pub groups: Vec, + /// Other events received inside the window, whose handlers may have caused some of this: the + /// first 12, in the order received. + pub overlapping_events: Vec, + /// Anything missing or worth knowing about the answer: that the handler's start is a guess, + /// that the session has no OS trace, that records were left out. + pub notes: Vec, +} + +/// Whether a rendered record is from a recorder process (this one, or another): what the recorder +/// itself does while it watches is not part of what happened, and it is left out of the video the +/// same way. A rendered record reads `