Skip to content

Commit 5182128

Browse files
authored
Merge pull request #70 from michaelwoerister/raii-recording
Add RAII-based API for recording events and bump version to 0.4.0
2 parents a545e46 + c05eb6d commit 5182128

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Changelog
22

3-
## Unreleased
3+
## [0.4.0] - 2019-10-24
44
### Added
5+
- `measureme`: Added RAII-based API for recording events ([GH-70])
56
- `measureme`: Added support for compiling the library under wasm/wasi ([GH-43])
67
- `mmview`: Added the `-t` flag to limit output to results on the specified thread id ([GH-49])
78
- `summarize`: Added the `diff` sub command to compare two profiles ([GH-50])
@@ -22,7 +23,7 @@
2223

2324
## [0.2.0] - 2019-04-10
2425

25-
26+
[0.4.0]: https://github.com/rust-lang/measureme/releases/tag/0.4.0
2627
[0.3.0]: https://github.com/rust-lang/measureme/releases/tag/0.3.0
2728
[0.2.1]: https://github.com/rust-lang/measureme/releases/tag/0.2.1
2829
[0.2.0]: https://github.com/rust-lang/measureme/releases/tag/0.2.0
@@ -35,3 +36,4 @@
3536
[GH-56]: https://github.com/rust-lang/measureme/pull/56
3637
[GH-59]: https://github.com/rust-lang/measureme/pull/59
3738
[GH-60]: https://github.com/rust-lang/measureme/pull/60
39+
[GH-70]: https://github.com/rust-lang/measureme/pull/70

measureme/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "measureme"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
authors = ["Wesley Wiser <[email protected]>", "Michael Woerister <michaelwoerister@posteo>"]
55
edition = "2018"
66
description = "Support crate for rustc's self-profiling feature"

measureme/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
//! - `thread_id`: a `u64` id of the thread which is recording this event
2222
//! - `timestamp_kind`: a [`TimestampKind`] which specifies how this event should be treated by `measureme` tooling
2323
//!
24+
//! Alternatively, events can also be recorded via the [`Profiler::start_recording_interval_event()`] method. This
25+
//! method records a "start" event and returns a `TimingGuard` object that will automatically record
26+
//! the corresponding "end" event when it is dropped.
27+
//!
2428
//! To create a [`StringId`], call one of the string allocation methods:
2529
//! - [`Profiler::alloc_string()`]: allocates a string and returns the [`StringId`] that refers to it
2630
//! - [`Profiler::alloc_string_with_reserved_id()`]: allocates a string using the specified [`StringId`].
@@ -41,6 +45,7 @@
4145
//! [`Profiler::alloc_string_with_reserved_id()`]: struct.Profiler.html#method.alloc_string_with_reserved_id
4246
//! [`Profiler::new()`]: struct.Profiler.html#method.new
4347
//! [`Profiler::record_event()`]: struct.Profiler.html#method.record_event
48+
//! [`Profiler::start_recording_interval_event()`]: struct.Profiler.html#method.start_recording_interval_event
4449
//! [`ProfilingData`]: struct.ProfilingData.html
4550
//! [`ProfilingData::iter()`]: struct.ProfilingData.html#method.iter
4651
//! [`ProfilingData::iter_matching_events()`]: struct.ProfilingData.html#method.iter_matching_events
@@ -69,7 +74,7 @@ pub use crate::event::Event;
6974
pub use crate::file_serialization_sink::FileSerializationSink;
7075
#[cfg(not(target_arch = "wasm32"))]
7176
pub use crate::mmap_serialization_sink::MmapSerializationSink;
72-
pub use crate::profiler::{Profiler, ProfilerFiles};
77+
pub use crate::profiler::{Profiler, ProfilerFiles, TimingGuard};
7378
pub use crate::profiling_data::{MatchingEvent, ProfilingData};
7479
pub use crate::raw_event::{RawEvent, Timestamp, TimestampKind};
7580
pub use crate::serialization::{Addr, SerializationSink};

measureme/src/profiler.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ impl<S: SerializationSink> Profiler<S> {
7878
self.string_table.alloc(s)
7979
}
8080

81+
/// Records an event with the given parameters. The event time is computed
82+
/// automatically.
8183
pub fn record_event(
8284
&self,
8385
event_kind: StringId,
@@ -90,17 +92,17 @@ impl<S: SerializationSink> Profiler<S> {
9092
+ duration_since_start.subsec_nanos() as u64;
9193
let timestamp = Timestamp::new(nanos_since_start, timestamp_kind);
9294

95+
let raw_event = RawEvent {
96+
event_kind,
97+
id: event_id,
98+
thread_id,
99+
timestamp,
100+
};
101+
93102
self.event_sink
94103
.write_atomic(std::mem::size_of::<RawEvent>(), |bytes| {
95104
debug_assert_eq!(bytes.len(), std::mem::size_of::<RawEvent>());
96105

97-
let raw_event = RawEvent {
98-
event_kind,
99-
id: event_id,
100-
thread_id,
101-
timestamp,
102-
};
103-
104106
let raw_event_bytes: &[u8] = unsafe {
105107
std::slice::from_raw_parts(
106108
&raw_event as *const _ as *const u8,
@@ -111,4 +113,44 @@ impl<S: SerializationSink> Profiler<S> {
111113
bytes.copy_from_slice(raw_event_bytes);
112114
});
113115
}
116+
117+
/// Creates a "start" event and returns a `TimingGuard` that will create
118+
/// the corresponding "end" event when it is dropped.
119+
pub fn start_recording_interval_event<'a>(
120+
&'a self,
121+
event_kind: StringId,
122+
event_id: StringId,
123+
thread_id: u64,
124+
) -> TimingGuard<'a, S> {
125+
self.record_event(event_kind, event_id, thread_id, TimestampKind::Start);
126+
127+
TimingGuard {
128+
profiler: self,
129+
event_id,
130+
event_kind,
131+
thread_id,
132+
}
133+
}
134+
}
135+
136+
/// When dropped, this `TimingGuard` will record an "end" event in the
137+
/// `Profiler` it was created by.
138+
#[must_use]
139+
pub struct TimingGuard<'a, S: SerializationSink> {
140+
profiler: &'a Profiler<S>,
141+
event_id: StringId,
142+
event_kind: StringId,
143+
thread_id: u64,
144+
}
145+
146+
impl<'a, S: SerializationSink> Drop for TimingGuard<'a, S> {
147+
#[inline]
148+
fn drop(&mut self) {
149+
self.profiler.record_event(
150+
self.event_kind,
151+
self.event_id,
152+
self.thread_id,
153+
TimestampKind::End
154+
);
155+
}
114156
}

0 commit comments

Comments
 (0)