Skip to content

Commit 5e68ea2

Browse files
committed
Allow recording interval event wthout drop guard
This makes it easier to record events when the `Profiler` and the `TimingGuard` would need to be stored different structs.
1 parent 28d8633 commit 5e68ea2

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

measureme/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub mod stringtable;
5050
pub mod rustc;
5151

5252
pub use crate::event_id::{EventId, EventIdBuilder};
53-
pub use crate::profiler::{Profiler, TimingGuard};
53+
pub use crate::profiler::{Profiler, TimingGuard, DetachedTiming};
5454
pub use crate::raw_event::{RawEvent, MAX_INSTANT_TIMESTAMP, MAX_INTERVAL_TIMESTAMP};
5555
pub use crate::serialization::{
5656
split_streams, Addr, PageTag, SerializationSink, SerializationSinkBuilder,

measureme/src/profiler.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,41 @@ impl Profiler {
122122
}
123123
}
124124

125+
/// Creates a "start" event and returns a `DetachedTiming`.
126+
/// To create the corresponding "event" event, yuu must call
127+
/// `finish_recording_internal_event` with the returned
128+
/// `DetachedTiming`
129+
#[inline]
130+
pub fn start_recording_interval_event_detached(
131+
&self,
132+
event_kind: StringId,
133+
event_id: EventId,
134+
thread_id: u32
135+
) -> DetachedTiming {
136+
DetachedTiming {
137+
event_id,
138+
event_kind,
139+
thread_id,
140+
start_count: self.counter.since_start(),
141+
}
142+
}
143+
144+
/// Creates the corresponding "end" event for
145+
/// the "start" event represented by `timing`. You
146+
/// must have obtained `timing` from the same `Profiler`
147+
pub fn finish_recording_interval_event(
148+
&self,
149+
timing: DetachedTiming
150+
) {
151+
drop(TimingGuard {
152+
profiler: self,
153+
event_id: timing.event_id,
154+
event_kind: timing.event_kind,
155+
thread_id: timing.thread_id,
156+
start_count: timing.start_count,
157+
});
158+
}
159+
125160
fn record_raw_event(&self, raw_event: &RawEvent) {
126161
self.event_sink
127162
.write_atomic(std::mem::size_of::<RawEvent>(), |bytes| {
@@ -130,6 +165,17 @@ impl Profiler {
130165
}
131166
}
132167

168+
/// Created by `Profiler::start_recording_interval_event_detached`.
169+
/// Must be passed to `finish_recording_interval_event` to record an
170+
/// "end" event.
171+
#[must_use]
172+
pub struct DetachedTiming {
173+
event_id: EventId,
174+
event_kind: StringId,
175+
thread_id: u32,
176+
start_count: u64,
177+
}
178+
133179
/// When dropped, this `TimingGuard` will record an "end" event in the
134180
/// `Profiler` it was created by.
135181
#[must_use]

0 commit comments

Comments
 (0)