Skip to content

Commit ad8d0b3

Browse files
committed
Fix panic when using --per-cpu-threads.
1 parent 4a5afec commit ad8d0b3

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

samply/src/linux_shared/converter.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,6 @@ where
878878
}
879879
if let (Some(cpus), Some(cpu_index)) = (&mut self.cpus, common.cpu) {
880880
let combined_thread = cpus.combined_thread_handle();
881-
let idle_frame_label = cpus.idle_frame_label();
882881
let cpu = cpus.get_mut(cpu_index as usize, &mut self.profile);
883882
if let Some(idle_cpu_sample) = self
884883
.context_switch_handler
@@ -902,7 +901,7 @@ where
902901
UnresolvedStackHandle::EMPTY,
903902
cpu_delta,
904903
0,
905-
Some(idle_frame_label),
904+
Some(cpu.idle_frame),
906905
);
907906

908907
// Emit a "rest sample" with a CPU delta of zero covering the rest of the paused range.
@@ -916,7 +915,7 @@ where
916915
UnresolvedStackHandle::EMPTY,
917916
CpuDelta::from_nanos(0),
918917
0,
919-
Some(idle_frame_label),
918+
Some(cpu.idle_frame),
920919
);
921920
}
922921
if self.should_emit_cswitch_markers {

samply/src/shared/per_cpu.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,23 @@ pub struct Cpus {
1212
process_handle: ProcessHandle,
1313
combined_thread_handle: ThreadHandle,
1414
cpus: Vec<Cpu>,
15-
idle_frame_label: FrameHandle,
1615
}
1716

1817
pub struct Cpu {
1918
pub name: StringHandle,
2019
pub thread_handle: ThreadHandle,
2120
pub context_switch_data: ThreadContextSwitchData,
21+
pub idle_frame: FrameHandle,
2222
current_tid: Option<(i32, StringHandle, u64)>,
2323
}
2424

2525
impl Cpu {
26-
pub fn new(name: StringHandle, thread_handle: ThreadHandle) -> Self {
26+
pub fn new(name: StringHandle, thread_handle: ThreadHandle, idle_frame: FrameHandle) -> Self {
2727
Self {
2828
name,
2929
thread_handle,
3030
context_switch_data: Default::default(),
31+
idle_frame,
3132
current_tid: None,
3233
}
3334
}
@@ -113,38 +114,36 @@ impl Cpus {
113114
pub fn new(start_time: Timestamp, profile: &mut Profile) -> Self {
114115
let process_handle = profile.add_process("CPU", 0, start_time);
115116
let combined_thread_handle = profile.add_thread(process_handle, 0, start_time, true);
116-
let idle_string = profile.handle_for_string("<Idle>");
117-
let idle_frame_label = profile.handle_for_frame_with_label(
118-
combined_thread_handle,
119-
idle_string,
120-
CategoryHandle::OTHER,
121-
FrameFlags::empty(),
122-
);
123117
Self {
124118
start_time,
125119
process_handle,
126120
combined_thread_handle,
127121
cpus: Vec::new(),
128-
idle_frame_label,
129122
}
130123
}
131124

132125
pub fn combined_thread_handle(&self) -> ThreadHandle {
133126
self.combined_thread_handle
134127
}
135128

136-
pub fn idle_frame_label(&self) -> FrameHandle {
137-
self.idle_frame_label
138-
}
139-
140129
pub fn get_mut(&mut self, cpu: usize, profile: &mut Profile) -> &mut Cpu {
141130
while self.cpus.len() <= cpu {
142131
let i = self.cpus.len();
143132
let thread = profile.add_thread(self.process_handle, i as u32, self.start_time, false);
144133
let name = format!("CPU {i}");
145134
profile.set_thread_name(thread, &name);
146-
self.cpus
147-
.push(Cpu::new(profile.handle_for_string(&name), thread));
135+
let idle_string = profile.handle_for_string("<Idle>");
136+
let idle_frame = profile.handle_for_frame_with_label(
137+
thread,
138+
idle_string,
139+
CategoryHandle::OTHER,
140+
FrameFlags::empty(),
141+
);
142+
self.cpus.push(Cpu::new(
143+
profile.handle_for_string(&name),
144+
thread,
145+
idle_frame,
146+
));
148147
}
149148
&mut self.cpus[cpu]
150149
}

samply/src/windows/profile_context.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,6 @@ impl ProfileContext {
16401640
}
16411641
if let Some(cpus) = &mut self.cpus {
16421642
let combined_thread = cpus.combined_thread_handle();
1643-
let idle_frame_label = cpus.idle_frame_label();
16441643
let cpu = cpus.get_mut(cpu_index as usize, &mut self.profile);
16451644

16461645
if let Some(idle_cpu_sample) = self
@@ -1660,7 +1659,7 @@ impl ProfileContext {
16601659
.convert_time(idle_cpu_sample.begin_timestamp);
16611660
let stack =
16621661
self.profile
1663-
.handle_for_stack(cpu.thread_handle, idle_frame_label, None);
1662+
.handle_for_stack(cpu.thread_handle, cpu.idle_frame, None);
16641663
self.profile.add_sample(
16651664
cpu.thread_handle,
16661665
begin_timestamp,

0 commit comments

Comments
 (0)