fxprof_processed_profile/
counters.rs

1use serde::ser::{Serialize, SerializeMap, Serializer};
2
3use crate::serialization_helpers::SliceWithPermutation;
4use crate::timestamp::{
5    SerializableTimestampSliceAsDeltas, SerializableTimestampSliceAsDeltasWithPermutation,
6};
7use crate::{GraphColor, ProcessHandle, Timestamp};
8
9/// A counter. Can be created with [`Profile::add_counter`](crate::Profile::add_counter).
10#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
11pub struct CounterHandle(pub(crate) usize);
12
13#[derive(Debug)]
14pub struct Counter {
15    name: String,
16    category: String,
17    description: String,
18    process: ProcessHandle,
19    pid: String,
20    samples: CounterSamples,
21    color: Option<GraphColor>,
22}
23
24impl Counter {
25    pub fn new(
26        name: &str,
27        category: &str,
28        description: &str,
29        process: ProcessHandle,
30        pid: &str,
31    ) -> Self {
32        Counter {
33            name: name.to_owned(),
34            category: category.to_owned(),
35            description: description.to_owned(),
36            process,
37            pid: pid.to_owned(),
38            samples: CounterSamples::new(),
39            color: None,
40        }
41    }
42
43    pub fn process(&self) -> ProcessHandle {
44        self.process
45    }
46
47    pub fn add_sample(
48        &mut self,
49        timestamp: Timestamp,
50        value_delta: f64,
51        number_of_operations_delta: u32,
52    ) {
53        self.samples
54            .add_sample(timestamp, value_delta, number_of_operations_delta)
55    }
56
57    pub fn set_color(&mut self, color: GraphColor) {
58        self.color = Some(color);
59    }
60
61    pub fn as_serializable(&self, main_thread_index: usize) -> impl Serialize + '_ {
62        SerializableCounter {
63            counter: self,
64            main_thread_index,
65        }
66    }
67}
68
69struct SerializableCounter<'a> {
70    counter: &'a Counter,
71    /// The index of the main thread for the counter's process in the profile threads list.
72    main_thread_index: usize,
73}
74
75impl Serialize for SerializableCounter<'_> {
76    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
77        let mut map = serializer.serialize_map(None)?;
78        map.serialize_entry("category", &self.counter.category)?;
79        map.serialize_entry("name", &self.counter.name)?;
80        map.serialize_entry("description", &self.counter.description)?;
81        map.serialize_entry("mainThreadIndex", &self.main_thread_index)?;
82        map.serialize_entry("pid", &self.counter.pid)?;
83        map.serialize_entry("samples", &self.counter.samples)?;
84        if let Some(color) = self.counter.color {
85            map.serialize_entry("color", &color)?;
86        }
87        map.end()
88    }
89}
90
91#[derive(Debug)]
92struct CounterSamples {
93    time: Vec<Timestamp>,
94    number: Vec<u32>,
95    count: Vec<f64>,
96
97    is_sorted_by_time: bool,
98    last_sample_timestamp: Timestamp,
99}
100
101impl CounterSamples {
102    pub fn new() -> Self {
103        Self {
104            time: Vec::new(),
105            number: Vec::new(),
106            count: Vec::new(),
107
108            is_sorted_by_time: true,
109            last_sample_timestamp: Timestamp::from_nanos_since_reference(0),
110        }
111    }
112
113    pub fn add_sample(
114        &mut self,
115        timestamp: Timestamp,
116        value_delta: f64,
117        number_of_operations_delta: u32,
118    ) {
119        self.time.push(timestamp);
120        self.count.push(value_delta);
121        self.number.push(number_of_operations_delta);
122
123        if timestamp < self.last_sample_timestamp {
124            self.is_sorted_by_time = false;
125        }
126        self.last_sample_timestamp = timestamp;
127    }
128}
129
130impl Serialize for CounterSamples {
131    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
132        let len = self.time.len();
133        let mut map = serializer.serialize_map(None)?;
134        map.serialize_entry("length", &len)?;
135
136        if self.is_sorted_by_time {
137            map.serialize_entry("count", &self.count)?;
138            map.serialize_entry("number", &self.number)?;
139            map.serialize_entry(
140                "timeDeltas",
141                &SerializableTimestampSliceAsDeltas(&self.time),
142            )?;
143        } else {
144            let mut indexes: Vec<usize> = (0..self.time.len()).collect();
145            indexes.sort_unstable_by_key(|index| self.time[*index]);
146            map.serialize_entry("count", &SliceWithPermutation(&self.count, &indexes))?;
147            map.serialize_entry("number", &SliceWithPermutation(&self.number, &indexes))?;
148            map.serialize_entry(
149                "timeDeltas",
150                &SerializableTimestampSliceAsDeltasWithPermutation(&self.time, &indexes),
151            )?;
152        }
153
154        map.end()
155    }
156}