Skip to content

Commit c06a1ce

Browse files
nickysnalehander92
authored andcommitted
feat: added events for thread support - ThreadStart, ThreadExit, ThreadSwitch
1 parent 6e9f03a commit c06a1ce

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

runtime_tracing/src/abstract_trace_writer.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use std::{
77
};
88

99
use crate::{
10-
AssignCellRecord, AssignCompoundItemRecord, AssignmentRecord, CallRecord, CellValueRecord, CompoundValueRecord, FullValueRecord, FunctionId,
11-
FunctionRecord, Line, NONE_TYPE_ID, PathId, RValue, RecordEvent, ReturnRecord, StepRecord, TraceLowLevelEvent, TraceMetadata, TypeId, TypeKind,
12-
TypeRecord, TypeSpecificInfo, VariableCellRecord, VariableId, tracer::TOP_LEVEL_FUNCTION_ID,
10+
tracer::TOP_LEVEL_FUNCTION_ID, AssignCellRecord, AssignCompoundItemRecord, AssignmentRecord, CallRecord, CellValueRecord, CompoundValueRecord, FullValueRecord, FunctionId, FunctionRecord, Line, PathId, RValue, RecordEvent, ReturnRecord, StepRecord, ThreadId, TraceLowLevelEvent, TraceMetadata, TypeId, TypeKind, TypeRecord, TypeSpecificInfo, VariableCellRecord, VariableId, NONE_TYPE_ID
1311
};
1412

1513
pub struct AbstractTraceWriterData {
@@ -280,6 +278,18 @@ pub trait AbstractTraceWriter {
280278
RValue::Compound(variable_ids)
281279
}
282280

281+
fn thread_start(&mut self, thread_id: ThreadId) {
282+
self.add_event(TraceLowLevelEvent::ThreadStart(thread_id));
283+
}
284+
285+
fn thread_exit(&mut self, thread_id: ThreadId) {
286+
self.add_event(TraceLowLevelEvent::ThreadExit(thread_id));
287+
}
288+
289+
fn thread_switch(&mut self, thread_id: ThreadId) {
290+
self.add_event(TraceLowLevelEvent::ThreadSwitch(thread_id));
291+
}
292+
283293
fn drop_last_step(&mut self) {
284294
self.add_event(TraceLowLevelEvent::DropLastStep);
285295
}

runtime_tracing/src/capnptrace.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,18 @@ pub fn write_trace(q: &[crate::TraceLowLevelEvent], output: &mut impl std::io::W
441441
let mut ret_place = ret.init_place();
442442
ret_place.set_p(vcr.place.0.try_into().unwrap());
443443
}
444+
TraceLowLevelEvent::ThreadStart(tid) => {
445+
let mut ret = event.init_thread_start();
446+
ret.set_i(tid.0);
447+
}
448+
TraceLowLevelEvent::ThreadExit(tid) => {
449+
let mut ret = event.init_thread_exit();
450+
ret.set_i(tid.0);
451+
}
452+
TraceLowLevelEvent::ThreadSwitch(tid) => {
453+
let mut ret = event.init_thread_switch();
454+
ret.set_i(tid.0);
455+
}
444456
}
445457
}
446458

@@ -702,6 +714,15 @@ pub fn read_trace(input: &mut impl std::io::BufRead) -> ::capnp::Result<Vec<crat
702714
Ok(trace::trace_low_level_event::Which::DropVariable(variable_id)) => {
703715
TraceLowLevelEvent::DropVariable(crate::VariableId(variable_id?.get_i().try_into().unwrap()))
704716
}
717+
Ok(trace::trace_low_level_event::Which::ThreadStart(thread_id)) => {
718+
TraceLowLevelEvent::ThreadStart(crate::ThreadId(thread_id?.get_i()))
719+
}
720+
Ok(trace::trace_low_level_event::Which::ThreadExit(thread_id)) => {
721+
TraceLowLevelEvent::ThreadExit(crate::ThreadId(thread_id?.get_i()))
722+
}
723+
Ok(trace::trace_low_level_event::Which::ThreadSwitch(thread_id)) => {
724+
TraceLowLevelEvent::ThreadSwitch(crate::ThreadId(thread_id?.get_i()))
725+
}
705726
Ok(trace::trace_low_level_event::Which::DropLastStep(())) => TraceLowLevelEvent::DropLastStep,
706727
Err(_) => {
707728
panic!()

runtime_tracing/src/trace.capnp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ struct Trace {
3131
variableCell @18 :VariableCellRecord;
3232
dropVariable @19 :VariableId;
3333

34+
threadStart @21 :ThreadId;
35+
threadExit @22 :ThreadId;
36+
threadSwitch @23 :ThreadId;
37+
3438
dropLastStep @20 :Void;
3539
}
3640
}
@@ -115,6 +119,10 @@ struct Trace {
115119
i @0 :Int64;
116120
}
117121

122+
struct ThreadId {
123+
i @0 :UInt64;
124+
}
125+
118126
struct CallRecord {
119127
functionId @0 :FunctionId;
120128
args @1 :List(FullValueRecord);

runtime_tracing/src/types.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ pub enum TraceLowLevelEvent {
4949
VariableCell(VariableCellRecord),
5050
DropVariable(VariableId),
5151

52+
ThreadStart(ThreadId),
53+
ThreadExit(ThreadId),
54+
ThreadSwitch(ThreadId),
55+
5256
// normal event, workaround for cases when we need to drop
5357
// a step event, but the trace needs to be append-only
5458
DropLastStep,
@@ -245,6 +249,15 @@ impl Into<usize> for FunctionId {
245249
}
246250
}
247251

252+
#[derive(Hash, Debug, Default, Copy, Clone, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)]
253+
pub struct ThreadId(pub u64);
254+
255+
impl Into<u64> for ThreadId {
256+
fn into(self) -> u64 {
257+
self.0
258+
}
259+
}
260+
248261
#[derive(Debug, Clone, Serialize, Deserialize)]
249262
pub struct CallRecord {
250263
// pub key: CallKey,

0 commit comments

Comments
 (0)