add awareness for threads
This commit is contained in:
parent
349b37edda
commit
349c0e688c
1 changed files with 46 additions and 26 deletions
72
src/lib.rs
72
src/lib.rs
|
|
@ -1,6 +1,7 @@
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
collections::{HashMap, HashSet},
|
||||
sync::{atomic::AtomicU64, Mutex},
|
||||
thread::ThreadId,
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
|
|
@ -33,6 +34,7 @@ struct SpanInfo {
|
|||
struct InnerPerfettoSubscriber {
|
||||
buffer: Vec<perfetto::TracePacket>,
|
||||
spans: HashMap<u64, SpanInfo>,
|
||||
threads: HashMap<ThreadId, u64>,
|
||||
}
|
||||
|
||||
impl InnerPerfettoSubscriber {
|
||||
|
|
@ -40,6 +42,42 @@ impl InnerPerfettoSubscriber {
|
|||
Self {
|
||||
buffer,
|
||||
spans: HashMap::new(),
|
||||
threads: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_track_id(&mut self) -> u64 {
|
||||
let current = std::thread::current();
|
||||
let nextfree = self.threads.len() as u64;
|
||||
match self.threads.entry(current.id()) {
|
||||
std::collections::hash_map::Entry::Occupied(occupied_entry) => *occupied_entry.get(),
|
||||
std::collections::hash_map::Entry::Vacant(vacant_entry) => {
|
||||
vacant_entry.insert(nextfree);
|
||||
|
||||
self.buffer.push(perfetto::TracePacket {
|
||||
timestamp: None,
|
||||
data: Some(perfetto::trace_packet::Data::TrackDescriptor(
|
||||
perfetto::TrackDescriptor {
|
||||
uuid: Some(nextfree),
|
||||
parent_uuid: None,
|
||||
static_or_dynamic_name: None,
|
||||
process: None,
|
||||
thread: Some(perfetto::ThreadDescriptor {
|
||||
pid: Some(1234),
|
||||
thread_name: current.name().map(|s| s.to_owned()),
|
||||
tid: Some(nextfree as i32),
|
||||
}),
|
||||
},
|
||||
)),
|
||||
optional_trusted_packet_sequence_id: Some(
|
||||
perfetto::trace_packet::OptionalTrustedPacketSequenceId::TrustedPacketSequenceId(
|
||||
3903809,
|
||||
),
|
||||
),
|
||||
});
|
||||
|
||||
nextfree
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -151,7 +189,7 @@ impl PerfettoSubscriber {
|
|||
static_or_dynamic_name: None,
|
||||
process: Some(perfetto::ProcessDescriptor {
|
||||
pid: Some(1234),
|
||||
process_name: Some(String::from("test")),
|
||||
process_name: Some(String::from("process")),
|
||||
}),
|
||||
thread: None,
|
||||
},
|
||||
|
|
@ -162,27 +200,6 @@ impl PerfettoSubscriber {
|
|||
),
|
||||
),
|
||||
});
|
||||
packets.push(perfetto::TracePacket {
|
||||
timestamp: None,
|
||||
data: Some(perfetto::trace_packet::Data::TrackDescriptor(
|
||||
perfetto::TrackDescriptor {
|
||||
uuid: Some(49083589894),
|
||||
parent_uuid: None,
|
||||
static_or_dynamic_name: None,
|
||||
process: None,
|
||||
thread: Some(perfetto::ThreadDescriptor {
|
||||
pid: Some(1234),
|
||||
thread_name: Some(String::from("test_thread")),
|
||||
tid: Some(10),
|
||||
}),
|
||||
},
|
||||
)),
|
||||
optional_trusted_packet_sequence_id: Some(
|
||||
perfetto::trace_packet::OptionalTrustedPacketSequenceId::TrustedPacketSequenceId(
|
||||
3903809,
|
||||
),
|
||||
),
|
||||
});
|
||||
Self {
|
||||
id_counter: AtomicU64::new(1),
|
||||
inner: Mutex::new(InnerPerfettoSubscriber::new(packets)),
|
||||
|
|
@ -255,11 +272,12 @@ impl Subscriber for PerfettoSubscriber {
|
|||
let mut debug_annotations = DebugAnnotationVisitor::new_with_metadata(event.metadata());
|
||||
event.record(&mut debug_annotations);
|
||||
|
||||
let track_id = inner.get_track_id();
|
||||
inner.buffer.push(perfetto::TracePacket {
|
||||
timestamp: Some(time),
|
||||
data: Some(perfetto::trace_packet::Data::TrackEvent(
|
||||
perfetto::TrackEvent {
|
||||
track_uuid: Some(49083589894),
|
||||
track_uuid: Some(track_id),
|
||||
categories: vec![event.metadata().level().as_str().to_owned()],
|
||||
category_iids: vec![],
|
||||
name_field: Some(perfetto::track_event::NameField::Name(String::from(event.metadata().name()))),
|
||||
|
|
@ -287,11 +305,12 @@ impl Subscriber for PerfettoSubscriber {
|
|||
let level = inner.spans[&span.into_u64()].level;
|
||||
let name = inner.spans[&span.into_u64()].name.clone();
|
||||
|
||||
let track_id = inner.get_track_id();
|
||||
inner.buffer.push(perfetto::TracePacket {
|
||||
timestamp: Some(time),
|
||||
data: Some(perfetto::trace_packet::Data::TrackEvent(
|
||||
perfetto::TrackEvent {
|
||||
track_uuid: Some(49083589894),
|
||||
track_uuid: Some(track_id),
|
||||
categories: vec![level.as_str().to_owned()],
|
||||
category_iids: vec![],
|
||||
name_field: Some(perfetto::track_event::NameField::Name(name)),
|
||||
|
|
@ -319,11 +338,12 @@ impl Subscriber for PerfettoSubscriber {
|
|||
let level = inner.spans[&span.into_u64()].level;
|
||||
let name = inner.spans[&span.into_u64()].name.clone();
|
||||
|
||||
let track_id = inner.get_track_id();
|
||||
inner.buffer.push(perfetto::TracePacket {
|
||||
timestamp: Some(time),
|
||||
data: Some(perfetto::trace_packet::Data::TrackEvent(
|
||||
perfetto::TrackEvent {
|
||||
track_uuid: Some(49083589894),
|
||||
track_uuid: Some(track_id),
|
||||
categories: vec![level.as_str().to_owned()],
|
||||
category_iids: vec![],
|
||||
name_field: Some(perfetto::track_event::NameField::Name(name)),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue