@@ -78,6 +78,8 @@ impl<S: SerializationSink> Profiler<S> {
78
78
self . string_table . alloc ( s)
79
79
}
80
80
81
+ /// Records an event with the given parameters. The event time is computed
82
+ /// automatically.
81
83
pub fn record_event (
82
84
& self ,
83
85
event_kind : StringId ,
@@ -90,17 +92,17 @@ impl<S: SerializationSink> Profiler<S> {
90
92
+ duration_since_start. subsec_nanos ( ) as u64 ;
91
93
let timestamp = Timestamp :: new ( nanos_since_start, timestamp_kind) ;
92
94
95
+ let raw_event = RawEvent {
96
+ event_kind,
97
+ id : event_id,
98
+ thread_id,
99
+ timestamp,
100
+ } ;
101
+
93
102
self . event_sink
94
103
. write_atomic ( std:: mem:: size_of :: < RawEvent > ( ) , |bytes| {
95
104
debug_assert_eq ! ( bytes. len( ) , std:: mem:: size_of:: <RawEvent >( ) ) ;
96
105
97
- let raw_event = RawEvent {
98
- event_kind,
99
- id : event_id,
100
- thread_id,
101
- timestamp,
102
- } ;
103
-
104
106
let raw_event_bytes: & [ u8 ] = unsafe {
105
107
std:: slice:: from_raw_parts (
106
108
& raw_event as * const _ as * const u8 ,
@@ -111,4 +113,44 @@ impl<S: SerializationSink> Profiler<S> {
111
113
bytes. copy_from_slice ( raw_event_bytes) ;
112
114
} ) ;
113
115
}
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
+ }
114
156
}
0 commit comments