Skip to content

Commit

Permalink
fix panic when no perf groups can be read during sampling (#185)
Browse files Browse the repository at this point in the history
Fixes a panic when no perf groups can be read during sampling. This
condition results in a divide-by-zero error.

We skip the calculations and metrics updates when the number of
active perf groups is zero during any sampling period.
  • Loading branch information
brayniac authored Mar 20, 2024
1 parent 1a48b24 commit 4c5387a
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/samplers/cpu/linux/perf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,18 @@ impl Sampler for Perf {
}
}

// we increase the total cycles executed in the last sampling period instead of using the cycle perf event value to handle offlined CPUs.
CPU_CYCLES.add(total_cycles);
CPU_INSTRUCTIONS.add(total_instructions);
CPU_PERF_GROUPS_ACTIVE.set(nr_active_groups as i64);
CPU_IPKC_AVERAGE.set((avg_ipkc / nr_active_groups) as i64);
CPU_IPUS_AVERAGE.set((avg_ipus / nr_active_groups) as i64);
CPU_BASE_FREQUENCY_AVERAGE.set((avg_base_frequency / nr_active_groups) as i64);
CPU_FREQUENCY_AVERAGE.set((avg_running_frequency / nr_active_groups) as i64);
CPU_CORES.set(nr_active_groups as _);
if nr_active_groups > 0 {
// we increase the total cycles executed in the last sampling period
// instead of using the cycle perf event value to handle offlined CPUs.
CPU_CYCLES.add(total_cycles);
CPU_INSTRUCTIONS.add(total_instructions);
CPU_PERF_GROUPS_ACTIVE.set(nr_active_groups as i64);
CPU_IPKC_AVERAGE.set((avg_ipkc / nr_active_groups) as i64);
CPU_IPUS_AVERAGE.set((avg_ipus / nr_active_groups) as i64);
CPU_BASE_FREQUENCY_AVERAGE.set((avg_base_frequency / nr_active_groups) as i64);
CPU_FREQUENCY_AVERAGE.set((avg_running_frequency / nr_active_groups) as i64);
CPU_CORES.set(nr_active_groups as _);
}

// determine when to sample next
let next = self.next + self.interval;
Expand Down

0 comments on commit 4c5387a

Please sign in to comment.