Skip to content

Commit

Permalink
total_rss_bytes and total_pss_bytes from smaps_rollup
Browse files Browse the repository at this point in the history
This commit adds collection of total PSS bytes for all processes from
smaps_rollup, adjusting total_rss_bytes to also be from the same source.

Signed-off-by: Brian L. Troutwine <brian.troutwine@datadoghq.com>
  • Loading branch information
blt committed Dec 6, 2024
1 parent e7ffc27 commit ce4fe14
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lading/src/observer/linux/procfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Sampler {
let mut total_processes: u64 = 0;
// We maintain a tally of the total RSS and PSS consumed by the parent
// process and its children.
let mut total_rss: u64 = 0;
let mut aggr = memory::smaps_rollup::Aggregator::default();

// Calculate the ticks since machine uptime. This will be important
// later for calculating per-process uptime. Because we capture this one
Expand Down Expand Up @@ -270,7 +270,6 @@ impl Sampler {
// Number of threads this process has active.
gauge!("num_threads", &labels).set(stats.num_threads as f64);

total_rss += rss;
total_processes += 1;

// Also report memory data from `proc/status` as a reference point
Expand Down Expand Up @@ -358,7 +357,7 @@ impl Sampler {
}

// `/proc/{pid}/smaps_rollup`
if let Err(err) = memory::smaps_rollup::poll(pid, &labels).await {
if let Err(err) = memory::smaps_rollup::poll(pid, &labels, &mut aggr).await {
// We don't want to bail out entirely if we can't read smap rollup
// which will happen if we don't have permissions or, more
// likely, the process has exited.
Expand Down Expand Up @@ -420,7 +419,8 @@ impl Sampler {

let totals = calculate_cpu_percentage(&total_sample, &self.previous_totals, self.num_cores);

gauge!("total_rss_bytes").set(total_rss as f64);
gauge!("total_rss_bytes").set(aggr.rss as f64);
gauge!("total_pss_bytes").set(aggr.pss as f64);
gauge!("total_utime").set(total_sample.utime as f64);
gauge!("total_stime").set(total_sample.stime as f64);

Expand Down
17 changes: 16 additions & 1 deletion lading/src/observer/linux/procfs/memory/smaps_rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ pub(crate) enum Error {
Parsing(String),
}

#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct Aggregator {
pub(crate) rss: u64,
pub(crate) pss: u64,
}

// Read `/proc/{pid}/smaps_rollup` and parse it directly into metrics.
pub(crate) async fn poll(pid: i32, labels: &[(String, String)]) -> Result<(), Error> {
pub(crate) async fn poll(
pid: i32,
labels: &[(String, String)],
aggr: &mut Aggregator,
) -> Result<(), Error> {
let path = format!("/proc/{pid}/smaps_rollup");
// NOTE `read_to_string` uses as few IO operations as possible in its
// implementation, so we might get the contents here in one go.
Expand Down Expand Up @@ -55,6 +65,11 @@ pub(crate) async fn poll(pid: i32, labels: &[(String, String)]) -> Result<(), Er
let name_len = name.len();
// Last character is a :, skip it.
let field = name[..name_len - 1].to_snake_case();
match field.as_str() {
"rss" => aggr.rss += value_kb,
"pss" => aggr.pss += value_kb,
_ => { /* ignore other fields */ }
}
let metric_name = format!("smaps_rollup.{field}");
gauge!(metric_name, labels).set(value_kb as f64);
}
Expand Down

0 comments on commit ce4fe14

Please sign in to comment.