Skip to content

Commit 501d6a3

Browse files
authored
Merge pull request #159 from Aaron1011/raw
Allow recording interval event wthout drop guard
2 parents 28d8633 + 676cdf2 commit 501d6a3

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,45 @@ impl Profiler {
122122
}
123123
}
124124

125+
/// Creates a "start" event and returns a `DetachedTiming`.
126+
/// To create the corresponding "event" event, you must call
127+
/// `finish_recording_internal_event` with the returned
128+
/// `DetachedTiming`.
129+
/// Since `DetachedTiming` does not capture the lifetime of `&self`,
130+
/// this method can sometimes be more convenient than
131+
/// `start_recording_interval_event` - e.g. it can be stored
132+
/// in a struct without the need to add a lifetime parameter.
133+
#[inline]
134+
pub fn start_recording_interval_event_detached(
135+
&self,
136+
event_kind: StringId,
137+
event_id: EventId,
138+
thread_id: u32
139+
) -> DetachedTiming {
140+
DetachedTiming {
141+
event_id,
142+
event_kind,
143+
thread_id,
144+
start_count: self.counter.since_start(),
145+
}
146+
}
147+
148+
/// Creates the corresponding "end" event for
149+
/// the "start" event represented by `timing`. You
150+
/// must have obtained `timing` from the same `Profiler`
151+
pub fn finish_recording_interval_event(
152+
&self,
153+
timing: DetachedTiming
154+
) {
155+
drop(TimingGuard {
156+
profiler: self,
157+
event_id: timing.event_id,
158+
event_kind: timing.event_kind,
159+
thread_id: timing.thread_id,
160+
start_count: timing.start_count,
161+
});
162+
}
163+
125164
fn record_raw_event(&self, raw_event: &RawEvent) {
126165
self.event_sink
127166
.write_atomic(std::mem::size_of::<RawEvent>(), |bytes| {
@@ -130,6 +169,17 @@ impl Profiler {
130169
}
131170
}
132171

172+
/// Created by `Profiler::start_recording_interval_event_detached`.
173+
/// Must be passed to `finish_recording_interval_event` to record an
174+
/// "end" event.
175+
#[must_use]
176+
pub struct DetachedTiming {
177+
event_id: EventId,
178+
event_kind: StringId,
179+
thread_id: u32,
180+
start_count: u64,
181+
}
182+
133183
/// When dropped, this `TimingGuard` will record an "end" event in the
134184
/// `Profiler` it was created by.
135185
#[must_use]

0 commit comments

Comments
 (0)