1use super::{
2 BeginAsyncDynNamedSpanEvent, BeginAsyncNamedSpanEvent, BeginAsyncSpanEvent,
3 BeginThreadDynNamedSpanEvent, BeginThreadNamedSpanEvent, BeginThreadSpanEvent,
4 EndAsyncDynNamedSpanEvent, EndAsyncNamedSpanEvent, EndAsyncSpanEvent,
5 EndThreadDynNamedSpanEvent, EndThreadNamedSpanEvent, EndThreadSpanEvent, SpanLocation,
6 SpanLocationRecord, SpanMetadata, SpanRecord, TaggedEndAsyncNamedSpanEvent,
7 TaggedEndAsyncSpanEvent, TaggedEndThreadNamedSpanEvent, TaggedEndThreadSpanEvent,
8};
9use crate::{
10 dyn_deps::{DynDeps, merge_properties},
11 event::{EventBlock, EventStream, ExtractDeps},
12 property_set::{
13 DynProperty, PropertyDepsQueue, PropertySet, PropertySetDependency, PropertySetId,
14 StringDepsQueue, record_properties,
15 },
16 source_path::trim_source_path,
17 string_id::{DynStringId, StringId},
18};
19use micromegas_transit::{StaticStringDependency, Utf8StaticStringDependency, prelude::*};
20use rustc_hash::FxHashSet;
21
22declare_queue_struct!(
23 struct ThreadEventQueue<
24 BeginThreadSpanEvent,
25 EndThreadSpanEvent,
26 BeginThreadNamedSpanEvent,
27 EndThreadNamedSpanEvent,
28 BeginAsyncSpanEvent,
29 EndAsyncSpanEvent,
30 BeginAsyncNamedSpanEvent,
31 EndAsyncNamedSpanEvent,
32 BeginThreadDynNamedSpanEvent,
33 EndThreadDynNamedSpanEvent,
34 BeginAsyncDynNamedSpanEvent,
35 EndAsyncDynNamedSpanEvent,
36 TaggedEndThreadSpanEvent,
37 TaggedEndThreadNamedSpanEvent,
38 TaggedEndAsyncSpanEvent,
39 TaggedEndAsyncNamedSpanEvent,
40 > {}
41);
42
43declare_queue_struct!(
44 struct ThreadDepsQueue<
45 SpanRecord,
46 SpanLocationRecord,
47 Utf8StaticStringDependency,
48 StaticStringDependency,
49 PropertySetDependency,
50 > {}
51);
52
53impl StringDepsQueue for ThreadDepsQueue {
54 fn push_string(&mut self, dep: StaticStringDependency) {
55 self.push(dep);
56 }
57}
58
59impl PropertyDepsQueue for ThreadDepsQueue {
60 fn push_set(&mut self, dep: PropertySetDependency) {
61 self.push(dep);
62 }
63}
64
65fn record_scope_event_dependencies(
66 thread_span_desc: &'static SpanMetadata,
67 recorded_deps: &mut FxHashSet<u64>,
68 deps: &mut ThreadDepsQueue,
69) {
70 let thread_span_ptr = thread_span_desc as *const _ as u64;
71 if recorded_deps.insert(thread_span_ptr) {
72 let name = Utf8StaticStringDependency::from(thread_span_desc.name);
73 if recorded_deps.insert(name.ptr as u64) {
74 deps.push(name);
75 }
76 let target = Utf8StaticStringDependency::from(thread_span_desc.location.target);
77 if recorded_deps.insert(target.ptr as u64) {
78 deps.push(target);
79 }
80 let module_path = Utf8StaticStringDependency::from(thread_span_desc.location.module_path);
81 if recorded_deps.insert(module_path.ptr as u64) {
82 deps.push(module_path);
83 }
84 let file =
85 Utf8StaticStringDependency::from(trim_source_path(thread_span_desc.location.file));
86 let file_ptr = file.ptr;
87 if recorded_deps.insert(file.ptr as u64) {
88 deps.push(file);
89 }
90 deps.push(SpanRecord {
91 id: thread_span_ptr,
92 name: thread_span_desc.name.as_ptr(),
93 target: thread_span_desc.location.target.as_ptr(),
94 module_path: thread_span_desc.location.module_path.as_ptr(),
95 file: file_ptr,
96 line: thread_span_desc.location.line,
97 lod: thread_span_desc.location.lod as u32,
98 });
99 }
100}
101
102fn record_span_location_dependencies(
103 thread_span_location: &'static SpanLocation,
104 recorded_deps: &mut FxHashSet<u64>,
105 deps: &mut ThreadDepsQueue,
106) {
107 let location_id = thread_span_location as *const _ as u64;
108 if recorded_deps.insert(location_id) {
109 let target = Utf8StaticStringDependency::from(thread_span_location.target);
110 if recorded_deps.insert(target.ptr as u64) {
111 deps.push(target);
112 }
113 let module_path = Utf8StaticStringDependency::from(thread_span_location.module_path);
114 if recorded_deps.insert(module_path.ptr as u64) {
115 deps.push(module_path);
116 }
117 let file = Utf8StaticStringDependency::from(trim_source_path(thread_span_location.file));
118 let file_ptr = file.ptr;
119 if recorded_deps.insert(file.ptr as u64) {
120 deps.push(file);
121 }
122 deps.push(SpanLocationRecord {
123 id: location_id,
124 target: thread_span_location.target.as_ptr(),
125 module_path: thread_span_location.module_path.as_ptr(),
126 file: file_ptr,
127 line: thread_span_location.line,
128 lod: thread_span_location.lod as u32,
129 });
130 }
131}
132
133fn record_named_scope_event_dependencies(
134 thread_span_location: &'static SpanLocation,
135 name: &StringId,
136 recorded_deps: &mut FxHashSet<u64>,
137 deps: &mut ThreadDepsQueue,
138) {
139 record_span_location_dependencies(thread_span_location, recorded_deps, deps);
140 if recorded_deps.insert(name.id()) {
141 deps.push(Utf8StaticStringDependency::from(name));
142 }
143}
144
145fn record_last_set(
146 properties: PropertySetId,
147 last_set: &mut PropertySetId,
148 recorded_deps: &mut FxHashSet<u64>,
149 deps: &mut ThreadDepsQueue,
150) {
151 if properties != *last_set {
152 *last_set = properties;
153 if let Some(set) = properties.as_interned() {
154 record_properties(set, recorded_deps, deps);
155 }
156 }
157}
158
159impl ExtractDeps for ThreadEventQueue {
160 type DepsQueue = ThreadDepsQueue;
161
162 fn extract(&self, dyn_deps: &DynDeps<Self::DepsQueue>) -> Self::DepsQueue {
163 let mut deps = ThreadDepsQueue::new(1024 * 1024);
164 let mut recorded_deps = FxHashSet::default();
165 let mut last_set = PropertySetId(0);
166 for x in self.iter() {
167 match x {
168 ThreadEventQueueAny::BeginThreadSpanEvent(evt) => {
169 record_scope_event_dependencies(
170 evt.thread_span_desc,
171 &mut recorded_deps,
172 &mut deps,
173 );
174 }
175 ThreadEventQueueAny::EndThreadSpanEvent(evt) => {
176 record_scope_event_dependencies(
177 evt.thread_span_desc,
178 &mut recorded_deps,
179 &mut deps,
180 );
181 }
182 ThreadEventQueueAny::BeginThreadNamedSpanEvent(evt) => {
183 record_named_scope_event_dependencies(
184 evt.thread_span_location,
185 &evt.name,
186 &mut recorded_deps,
187 &mut deps,
188 );
189 }
190 ThreadEventQueueAny::EndThreadNamedSpanEvent(evt) => {
191 record_named_scope_event_dependencies(
192 evt.thread_span_location,
193 &evt.name,
194 &mut recorded_deps,
195 &mut deps,
196 );
197 }
198 ThreadEventQueueAny::BeginAsyncSpanEvent(evt) => {
199 record_scope_event_dependencies(evt.span_desc, &mut recorded_deps, &mut deps);
200 }
201 ThreadEventQueueAny::EndAsyncSpanEvent(evt) => {
202 record_scope_event_dependencies(evt.span_desc, &mut recorded_deps, &mut deps);
203 }
204 ThreadEventQueueAny::BeginAsyncNamedSpanEvent(evt) => {
205 record_named_scope_event_dependencies(
206 evt.span_location,
207 &evt.name,
208 &mut recorded_deps,
209 &mut deps,
210 );
211 }
212 ThreadEventQueueAny::EndAsyncNamedSpanEvent(evt) => {
213 record_named_scope_event_dependencies(
214 evt.span_location,
215 &evt.name,
216 &mut recorded_deps,
217 &mut deps,
218 );
219 }
220 ThreadEventQueueAny::BeginThreadDynNamedSpanEvent(evt) => {
221 record_span_location_dependencies(
222 evt.thread_span_location,
223 &mut recorded_deps,
224 &mut deps,
225 );
226 }
227 ThreadEventQueueAny::EndThreadDynNamedSpanEvent(evt) => {
228 record_span_location_dependencies(
229 evt.thread_span_location,
230 &mut recorded_deps,
231 &mut deps,
232 );
233 }
234 ThreadEventQueueAny::BeginAsyncDynNamedSpanEvent(evt) => {
235 record_span_location_dependencies(
236 evt.span_location,
237 &mut recorded_deps,
238 &mut deps,
239 );
240 }
241 ThreadEventQueueAny::EndAsyncDynNamedSpanEvent(evt) => {
242 record_span_location_dependencies(
243 evt.span_location,
244 &mut recorded_deps,
245 &mut deps,
246 );
247 }
248 ThreadEventQueueAny::TaggedEndThreadSpanEvent(evt) => {
249 record_scope_event_dependencies(
250 evt.thread_span_desc,
251 &mut recorded_deps,
252 &mut deps,
253 );
254 record_last_set(evt.properties, &mut last_set, &mut recorded_deps, &mut deps);
255 }
256 ThreadEventQueueAny::TaggedEndThreadNamedSpanEvent(evt) => {
257 record_span_location_dependencies(
258 evt.thread_span_location,
259 &mut recorded_deps,
260 &mut deps,
261 );
262 record_last_set(evt.properties, &mut last_set, &mut recorded_deps, &mut deps);
263 }
264 ThreadEventQueueAny::TaggedEndAsyncSpanEvent(evt) => {
265 record_scope_event_dependencies(evt.span_desc, &mut recorded_deps, &mut deps);
266 record_last_set(evt.properties, &mut last_set, &mut recorded_deps, &mut deps);
267 }
268 ThreadEventQueueAny::TaggedEndAsyncNamedSpanEvent(evt) => {
269 record_span_location_dependencies(
270 evt.span_location,
271 &mut recorded_deps,
272 &mut deps,
273 );
274 record_last_set(evt.properties, &mut last_set, &mut recorded_deps, &mut deps);
275 }
276 }
277 }
278 dyn_deps.record_static_refs(&mut recorded_deps, &mut deps);
281 deps.extend_from(dyn_deps.queue());
282 deps
283 }
284}
285
286pub type ThreadBlock = EventBlock<ThreadEventQueue>;
287pub type ThreadStream = EventStream<ThreadBlock>;
288
289impl ThreadStream {
290 pub fn push_dyn_named<T>(&mut self, name: &str, make: impl FnOnce(DynStringId) -> T)
292 where
293 T: InProcSerialize + ThreadEventQueueTypeIndex,
294 {
295 let (events, dyn_deps) = self.get_block_mut_parts();
296 let id = dyn_deps.intern_str(name);
297 events.push(make(DynStringId { id }));
298 }
299
300 pub fn push_tagged<T>(
302 &mut self,
303 set: &'static PropertySet,
304 dyn_properties: &[DynProperty<'_>],
305 make: impl FnOnce(PropertySetId) -> T,
306 ) where
307 T: InProcSerialize + ThreadEventQueueTypeIndex,
308 {
309 let (events, dyn_deps) = self.get_block_mut_parts();
310 let properties = tagged_set_id(dyn_deps, set, dyn_properties);
311 events.push(make(properties));
312 }
313
314 pub fn push_tagged_named<T>(
316 &mut self,
317 name: &str,
318 set: &'static PropertySet,
319 dyn_properties: &[DynProperty<'_>],
320 make: impl FnOnce(DynStringId, PropertySetId) -> T,
321 ) where
322 T: InProcSerialize + ThreadEventQueueTypeIndex,
323 {
324 let (events, dyn_deps) = self.get_block_mut_parts();
325 let id = dyn_deps.intern_str(name);
326 let properties = tagged_set_id(dyn_deps, set, dyn_properties);
327 events.push(make(DynStringId { id }, properties));
328 }
329}
330
331fn tagged_set_id(
332 dyn_deps: &mut DynDeps<ThreadDepsQueue>,
333 set: &'static PropertySet,
334 dyn_properties: &[DynProperty<'_>],
335) -> PropertySetId {
336 if dyn_properties.is_empty() {
337 return PropertySetId::interned(set);
338 }
339 dyn_deps.intern_set(&merge_properties(set, dyn_properties))
340}