Skip to content

Commit

Permalink
Don't spawn a new thread pool for every sample
Browse files Browse the repository at this point in the history
Summary:
below was creating and destroying a large number of very short lived
threads. This was creating enough threads that just the creation of
the threads was consuming 0.1% CPU and spawning ~60 threads a minute
on average.

Reviewed By: dschatzberg

Differential Revision: D45800206

fbshipit-source-id: c56c3eb224c1ea413fbd2e44d555872aea3ef368
  • Loading branch information
brianc118 authored and facebook-github-bot committed Jan 9, 2024
1 parent 7f7d0fd commit 15db2a4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions below/model/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl Default for CollectorOptions {
/// Collects data samples and maintains the latest data
pub struct Collector {
logger: slog::Logger,
proc_reader: procfs::ProcReader,
prev_sample: Option<(Sample, Instant)>,
collector_options: CollectorOptions,
}
Expand All @@ -67,13 +68,14 @@ impl Collector {
pub fn new(logger: slog::Logger, collector_options: CollectorOptions) -> Self {
Self {
logger,
proc_reader: procfs::ProcReader::new(),
prev_sample: None,
collector_options,
}
}

pub fn collect_sample(&self) -> Result<Sample> {
collect_sample(&self.logger, &self.collector_options)
pub fn collect_sample(&mut self) -> Result<Sample> {
collect_sample(&self.logger, &mut self.proc_reader, &self.collector_options)
}

/// Collect a new `Sample`, returning an updated Model
Expand Down Expand Up @@ -171,8 +173,11 @@ fn is_all_zero_disk_stats(disk_stats: &procfs::DiskStat) -> bool {
&& disk_stats.time_spend_discard_ms == Some(0)
}

fn collect_sample(logger: &slog::Logger, options: &CollectorOptions) -> Result<Sample> {
let mut reader = procfs::ProcReader::new();
fn collect_sample(
logger: &slog::Logger,
reader: &mut procfs::ProcReader,
options: &CollectorOptions,
) -> Result<Sample> {
let btrfs_reader =
btrfs::BtrfsReader::new(options.btrfs_samples, options.btrfs_min_pct, logger.clone());
let ethtool_reader = ethtool::EthtoolReader::new();
Expand Down
2 changes: 1 addition & 1 deletion below/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ fn record(
None
};

let collector = model::Collector::new(
let mut collector = model::Collector::new(
logger.clone(),
model::CollectorOptions {
cgroup_root: below_config.cgroup_root.clone(),
Expand Down

0 comments on commit 15db2a4

Please sign in to comment.