fxprof_processed_profile/
cpu_delta.rs

1use serde::ser::{Serialize, Serializer};
2use std::time::Duration;
3
4/// The amount of CPU time between thread samples.
5///
6/// This is used in the Firefox Profiler UI to draw an activity graph per thread.
7///
8/// A thread only runs on one CPU at any time, and can get scheduled off and on
9/// the CPU between two samples. The CPU delta is the accumulation of time it
10/// was running on the CPU.
11#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
12pub struct CpuDelta {
13    micros: u64,
14}
15
16impl From<Duration> for CpuDelta {
17    fn from(duration: Duration) -> Self {
18        Self {
19            micros: duration.as_micros() as u64,
20        }
21    }
22}
23
24impl CpuDelta {
25    /// A CPU delta of zero.
26    pub const ZERO: Self = Self { micros: 0 };
27
28    /// Create a CPU delta from integer nanoseconds.
29    pub fn from_nanos(nanos: u64) -> Self {
30        Self {
31            micros: nanos / 1000,
32        }
33    }
34
35    /// Create a CPU delta from integer microseconds.
36    pub fn from_micros(micros: u64) -> Self {
37        Self { micros }
38    }
39
40    /// Create a CPU delta from float milliseconds.
41    pub fn from_millis(millis: f64) -> Self {
42        Self {
43            micros: (millis * 1_000.0) as u64,
44        }
45    }
46
47    /// Whether the CPU delta is zero.
48    pub fn is_zero(&self) -> bool {
49        self.micros == 0
50    }
51}
52
53impl Serialize for CpuDelta {
54    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
55        // CPU deltas are serialized as float microseconds, because
56        // we set profile.meta.sampleUnits.threadCPUDelta to "µs".
57        self.micros.serialize(serializer)
58    }
59}