Skip to content

Commit

Permalink
Fix bug preventing running without threads
Browse files Browse the repository at this point in the history
Threads were added in the sample step irrespective of the boolean flag.
This commit fixes it.
It also adjusts the interactive mode to take threads into account.
  • Loading branch information
sgasse committed Apr 4, 2024
1 parent d45eb0b commit a36687c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ fn interactive(args: Cli, user_hz: f64) -> Result<()> {
print_proc_metrics(metrics, descriptions);

sampler.sample(sample_duration)?;
clear_n_lines(args.pids.len() + 1);
clear_n_lines(sampler.buffers().count() + 1);
}
}
40 changes: 22 additions & 18 deletions src/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ struct ProcSampler {
pid: usize,
proc_buf: Buffer,
thread_bufs: BTreeMap<usize, Buffer>,
with_threads: bool,
}

impl ProcSampler {
Expand All @@ -111,37 +112,40 @@ impl ProcSampler {
pid,
proc_buf,
thread_bufs,
with_threads,
})
}

pub fn sample(&mut self, start: Instant) -> Result<()> {
// Sample main process
self.proc_buf.sample(start)?;

// Find potentially new threads
let tids = get_threads(self.pid);
for tid in tids {
if let Entry::Vacant(e) = self.thread_bufs.entry(tid) {
if let Ok(buffer) =
Buffer::from_pid_tid(self.pid, tid, self.proc_buf.buf.capacity())
{
e.insert(buffer);
if self.with_threads {
// Find potentially new threads
let tids = get_threads(self.pid);
for tid in tids {
if let Entry::Vacant(e) = self.thread_bufs.entry(tid) {
if let Ok(buffer) =
Buffer::from_pid_tid(self.pid, tid, self.proc_buf.buf.capacity())
{
e.insert(buffer);
}
}
}
}

let mut pids_to_remove = vec![];
let mut pids_to_remove = vec![];

// Sample all threads
for (pid, buffer) in self.thread_bufs.iter_mut() {
if buffer.sample(start).is_err() {
pids_to_remove.push(*pid);
// Sample all threads
for (pid, buffer) in self.thread_bufs.iter_mut() {
if buffer.sample(start).is_err() {
pids_to_remove.push(*pid);
}
}
}

// Remove unreadable threads
for pid in pids_to_remove {
self.thread_bufs.remove(&pid);
// Remove unreadable threads
for pid in pids_to_remove {
self.thread_bufs.remove(&pid);
}
}

Ok(())
Expand Down

0 comments on commit a36687c

Please sign in to comment.