Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow recording interval event wthout drop guard #159

Merged
merged 2 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion measureme/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub mod stringtable;
pub mod rustc;

pub use crate::event_id::{EventId, EventIdBuilder};
pub use crate::profiler::{Profiler, TimingGuard};
pub use crate::profiler::{Profiler, TimingGuard, DetachedTiming};
pub use crate::raw_event::{RawEvent, MAX_INSTANT_TIMESTAMP, MAX_INTERVAL_TIMESTAMP};
pub use crate::serialization::{
split_streams, Addr, PageTag, SerializationSink, SerializationSinkBuilder,
Expand Down
46 changes: 46 additions & 0 deletions measureme/src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,41 @@ impl Profiler {
}
}

/// Creates a "start" event and returns a `DetachedTiming`.
/// To create the corresponding "event" event, yuu must call
Aaron1011 marked this conversation as resolved.
Show resolved Hide resolved
/// `finish_recording_internal_event` with the returned
/// `DetachedTiming`
Aaron1011 marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
pub fn start_recording_interval_event_detached(
&self,
event_kind: StringId,
event_id: EventId,
thread_id: u32
) -> DetachedTiming {
DetachedTiming {
event_id,
event_kind,
thread_id,
start_count: self.counter.since_start(),
}
}

/// Creates the corresponding "end" event for
/// the "start" event represented by `timing`. You
/// must have obtained `timing` from the same `Profiler`
pub fn finish_recording_interval_event(
&self,
timing: DetachedTiming
) {
drop(TimingGuard {
profiler: self,
event_id: timing.event_id,
event_kind: timing.event_kind,
thread_id: timing.thread_id,
start_count: timing.start_count,
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would

let start_count = profiler.counter.since_start();
// some work
let raw_event = RawEvent::new_interval(
    event_kind,
    event_id,
    thread_id,
    start_count,
    profiler.counter.since_start(),
);
profiler.record_raw_event(&raw_event);

work if you make profiler.counter.since_start() public? That is what the drop impl of TimingGuard does.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would, but I think this API is more consistent with the existing one (hiding the underlying counter). I don't have a strong opinion, though.


fn record_raw_event(&self, raw_event: &RawEvent) {
self.event_sink
.write_atomic(std::mem::size_of::<RawEvent>(), |bytes| {
Expand All @@ -130,6 +165,17 @@ impl Profiler {
}
}

/// Created by `Profiler::start_recording_interval_event_detached`.
/// Must be passed to `finish_recording_interval_event` to record an
/// "end" event.
#[must_use]
pub struct DetachedTiming {
event_id: EventId,
event_kind: StringId,
thread_id: u32,
start_count: u64,
}

/// When dropped, this `TimingGuard` will record an "end" event in the
/// `Profiler` it was created by.
#[must_use]
Expand Down