Skip to content

Commit

Permalink
add metrics-rs integration
Browse files Browse the repository at this point in the history
Signed-off-by: Brian L. Troutwine <brian.troutwine@datadoghq.com>
  • Loading branch information
blt committed Dec 5, 2024
1 parent e971df1 commit 1bd074a
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## Added
- Parse nearly the complete field list of smaps/smaps_rollup in the Linux observer.
## Removed
- Removed the unused target-rss-byte-limit from the command line, internal stub of.
## Changed
Expand Down
98 changes: 95 additions & 3 deletions lading/src/observer/linux/procfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl Sampler {
// then we assume smaps operations will fail as well.
if has_ptrace_perm {
joinset.spawn(async move {
// TODO this code reads smaps
// `/proc/{pid}/smaps`
let memory_regions = match memory::smaps::Regions::from_pid(pid) {
Ok(memory_regions) => memory_regions,
Err(e) => {
Expand All @@ -315,6 +315,32 @@ impl Sampler {
gauge!("smaps.pss.by_pathname", &labels).set(measures.pss as f64);
gauge!("smaps.size.by_pathname", &labels).set(measures.size as f64);
gauge!("smaps.swap.by_pathname", &labels).set(measures.swap as f64);
gauge!("smaps.private_clean.by_pathname", &labels)
.set(measures.private_clean as f64);
gauge!("smaps.private_dirty.by_pathname", &labels)
.set(measures.private_dirty as f64);
gauge!("smaps.shared_clean.by_pathname", &labels)
.set(measures.shared_clean as f64);
gauge!("smaps.shared_dirty.by_pathname", &labels)
.set(measures.shared_dirty as f64);
gauge!("smaps.referenced.by_pathname", &labels)
.set(measures.referenced as f64);
gauge!("smaps.anonymous.by_pathname", &labels)
.set(measures.anonymous as f64);
gauge!("smaps.lazy_free.by_pathname", &labels)
.set(measures.lazy_free as f64);
gauge!("smaps.anon_huge_pages.by_pathname", &labels)
.set(measures.anon_huge_pages as f64);
gauge!("smaps.shmem_pmd_mapped.by_pathname", &labels)
.set(measures.shmem_pmd_mapped as f64);
gauge!("smaps.shared_hugetlb.by_pathname", &labels)
.set(measures.shared_hugetlb as f64);
gauge!("smaps.private_hugetlb.by_pathname", &labels)
.set(measures.private_hugetlb as f64);
gauge!("smaps.file_pmd_mapped.by_pathname", &labels)
.set(measures.file_pmd_mapped as f64);
gauge!("smaps.locked.by_pathname", &labels).set(measures.locked as f64);
gauge!("smaps.swap_pss.by_pathname", &labels).set(measures.swap_pss as f64);
}

let measures = memory_regions.aggregate();
Expand All @@ -329,8 +355,26 @@ impl Sampler {
gauge!("smaps.pss.sum", &labels).set(measures.pss as f64);
gauge!("smaps.size.sum", &labels).set(measures.size as f64);
gauge!("smaps.swap.sum", &labels).set(measures.swap as f64);

// This code reads smaps_rollup
gauge!("smaps.shared_clean.sum", &labels).set(measures.shared_clean as f64);
gauge!("smaps.shared_dirty.sum", &labels).set(measures.shared_dirty as f64);
gauge!("smaps.private_clean.sum", &labels).set(measures.private_clean as f64);
gauge!("smaps.private_dirty.sum", &labels).set(measures.private_dirty as f64);
gauge!("smaps.referenced.sum", &labels).set(measures.referenced as f64);
gauge!("smaps.anonymous.sum", &labels).set(measures.anonymous as f64);
gauge!("smaps.lazy_free.sum", &labels).set(measures.lazy_free as f64);
gauge!("smaps.anon_huge_pages.sum", &labels)
.set(measures.anon_huge_pages as f64);
gauge!("smaps.shmem_pmd_mapped.sum", &labels)
.set(measures.shmem_pmd_mapped as f64);
gauge!("smaps.file_pmd_mapped.sum", &labels)
.set(measures.file_pmd_mapped as f64);
gauge!("smaps.shared_hugetlb.sum", &labels).set(measures.shared_hugetlb as f64);
gauge!("smaps.private_hugetlb.sum", &labels)
.set(measures.private_hugetlb as f64);
gauge!("smaps.swap_pss.sum", &labels).set(measures.swap_pss as f64);
gauge!("smaps.locked.sum", &labels).set(measures.locked as f64);

// `/proc/{pid}/smaps_rollup`
let rollup = match memory::smaps_rollup::Rollup::from_pid(pid) {
Ok(rollup) => rollup,
Err(e) => {
Expand All @@ -356,6 +400,54 @@ impl Sampler {
if let Some(v) = rollup.pss_shmem {
gauge!("smaps_rollup.pss_shmem", &labels).set(v as f64);
}
if let Some(v) = rollup.swap {
gauge!("smaps_rollup.swap", &labels).set(v as f64);
}
if let Some(v) = rollup.swap_pss {
gauge!("smaps_rollup.swap_pss", &labels).set(v as f64);
}
if let Some(v) = rollup.shared_clean {
gauge!("smaps_rollup.shared_clean", &labels).set(v as f64);
}
if let Some(v) = rollup.shared_dirty {
gauge!("smaps_rollup.shared_dirty", &labels).set(v as f64);
}
if let Some(v) = rollup.private_clean {
gauge!("smaps_rollup.private_clean", &labels).set(v as f64);
}
if let Some(v) = rollup.private_dirty {
gauge!("smaps_rollup.private_dirty", &labels).set(v as f64);
}
if let Some(v) = rollup.referenced {
gauge!("smaps_rollup.referenced", &labels).set(v as f64);
}
if let Some(v) = rollup.anonymous {
gauge!("smaps_rollup.anonymous", &labels).set(v as f64);
}
if let Some(v) = rollup.lazy_free {
gauge!("smaps_rollup.lazy_free", &labels).set(v as f64);
}
if let Some(v) = rollup.anon_huge_pages {
gauge!("smaps_rollup.anon_huge_pages", &labels).set(v as f64);
}
if let Some(v) = rollup.shmem_pmd_mapped {
gauge!("smaps_rollup.shmem_pmd_mapped", &labels).set(v as f64);
}
if let Some(v) = rollup.shared_hugetlb {
gauge!("smaps_rollup.shared_hugetlb", &labels).set(v as f64);
}
if let Some(v) = rollup.private_hugetlb {
gauge!("smaps_rollup.private_hugetlb", &labels).set(v as f64);
}
if let Some(v) = rollup.shared_hugetlb {
gauge!("smaps_rollup.shared_hugetlb", &labels).set(v as f64);
}
if let Some(v) = rollup.file_pmd_mapped {
gauge!("smaps_rollup.file_pmd_mapped", &labels).set(v as f64);
}
if let Some(v) = rollup.locked {
gauge!("smaps_rollup.locked", &labels).set(v as f64);
}
});
}
}
Expand Down
111 changes: 77 additions & 34 deletions lading/src/observer/linux/procfs/memory/smaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ pub(crate) struct Region {
pub(crate) swap: u64,
pub(crate) rss: u64,
pub(crate) pss_dirty: Option<u64>, // only present in 6.0+
pub(crate) shared_clean: Option<u64>,
pub(crate) shared_dirty: Option<u64>,
pub(crate) private_clean: Option<u64>,
pub(crate) private_dirty: Option<u64>,
pub(crate) referenced: Option<u64>,
pub(crate) anonymous: Option<u64>,
pub(crate) lazy_free: Option<u64>,
pub(crate) anon_huge_pages: Option<u64>,
pub(crate) shmem_pmd_mapped: Option<u64>,
pub(crate) file_pmd_mapped: Option<u64>,
pub(crate) shared_hugetlb: Option<u64>,
pub(crate) private_hugetlb: Option<u64>,
pub(crate) swap_pss: Option<u64>,
pub(crate) locked: Option<u64>,
pub(crate) shared_clean: u64,
pub(crate) shared_dirty: u64,
pub(crate) private_clean: u64,
pub(crate) private_dirty: u64,
pub(crate) referenced: u64,
pub(crate) anonymous: u64,
pub(crate) lazy_free: u64,
pub(crate) anon_huge_pages: u64,
pub(crate) shmem_pmd_mapped: u64,
pub(crate) file_pmd_mapped: u64,
pub(crate) shared_hugetlb: u64,
pub(crate) private_hugetlb: u64,
pub(crate) swap_pss: u64,
pub(crate) locked: u64,
}

impl Region {
Expand Down Expand Up @@ -245,20 +245,20 @@ impl Region {
swap,
rss,
pss_dirty,
shared_clean,
shared_dirty,
private_clean,
private_dirty,
referenced,
anonymous,
lazy_free,
anon_huge_pages,
shmem_pmd_mapped,
file_pmd_mapped,
shared_hugetlb,
private_hugetlb,
swap_pss,
locked,
shared_clean: shared_clean.unwrap_or(0),
shared_dirty: shared_dirty.unwrap_or(0),
private_clean: private_clean.unwrap_or(0),
private_dirty: private_dirty.unwrap_or(0),
referenced: referenced.unwrap_or(0),
anonymous: anonymous.unwrap_or(0),
lazy_free: lazy_free.unwrap_or(0),
anon_huge_pages: anon_huge_pages.unwrap_or(0),
shmem_pmd_mapped: shmem_pmd_mapped.unwrap_or(0),
file_pmd_mapped: file_pmd_mapped.unwrap_or(0),
shared_hugetlb: shared_hugetlb.unwrap_or(0),
private_hugetlb: private_hugetlb.unwrap_or(0),
swap_pss: swap_pss.unwrap_or(0),
locked: locked.unwrap_or(0),
})
}
}
Expand All @@ -269,6 +269,21 @@ pub(crate) struct AggrMeasure {
pub(crate) pss: u64,
pub(crate) swap: u64,
pub(crate) rss: u64,
pub(crate) pss_dirty: u64,
pub(crate) shared_clean: u64,
pub(crate) shared_dirty: u64,
pub(crate) private_clean: u64,
pub(crate) private_dirty: u64,
pub(crate) referenced: u64,
pub(crate) anonymous: u64,
pub(crate) lazy_free: u64,
pub(crate) anon_huge_pages: u64,
pub(crate) shmem_pmd_mapped: u64,
pub(crate) file_pmd_mapped: u64,
pub(crate) shared_hugetlb: u64,
pub(crate) private_hugetlb: u64,
pub(crate) swap_pss: u64,
pub(crate) locked: u64,
}

impl Regions {
Expand All @@ -285,6 +300,24 @@ impl Regions {
aggr.pss = aggr.pss.saturating_add(region.pss);
aggr.swap = aggr.swap.saturating_add(region.swap);
aggr.rss = aggr.rss.saturating_add(region.rss);

aggr.pss_dirty = aggr.pss_dirty.saturating_add(region.pss_dirty.unwrap_or(0));
aggr.shared_clean = aggr.shared_clean.saturating_add(region.shared_clean);
aggr.shared_dirty = aggr.shared_dirty.saturating_add(region.shared_dirty);
aggr.private_clean = aggr.private_clean.saturating_add(region.private_clean);
aggr.private_dirty = aggr.private_dirty.saturating_add(region.private_dirty);
aggr.referenced = aggr.referenced.saturating_add(region.referenced);
aggr.anonymous = aggr.anonymous.saturating_add(region.anonymous);
aggr.lazy_free = aggr.lazy_free.saturating_add(region.lazy_free);
aggr.anon_huge_pages = aggr.anon_huge_pages.saturating_add(region.anon_huge_pages);
aggr.shmem_pmd_mapped = aggr
.shmem_pmd_mapped
.saturating_add(region.shmem_pmd_mapped);
aggr.file_pmd_mapped = aggr.file_pmd_mapped.saturating_add(region.file_pmd_mapped);
aggr.shared_hugetlb = aggr.shared_hugetlb.saturating_add(region.shared_hugetlb);
aggr.private_hugetlb = aggr.private_hugetlb.saturating_add(region.private_hugetlb);
aggr.swap_pss = aggr.swap_pss.saturating_add(region.swap_pss);
aggr.locked = aggr.locked.saturating_add(region.locked);
}

aggr
Expand All @@ -296,16 +329,26 @@ impl Regions {
for region in &self.0 {
let pathname = region.pathname.clone();

let entry = map.entry(pathname).or_insert_with(|| AggrMeasure {
size: 0,
pss: 0,
swap: 0,
rss: 0,
});
let entry = map.entry(pathname).or_default();
entry.size += region.size;
entry.pss += region.pss;
entry.swap += region.swap;
entry.rss += region.rss;
entry.pss_dirty += region.pss_dirty.unwrap_or(0);
entry.shared_clean += region.shared_clean;
entry.shared_dirty += region.shared_dirty;
entry.private_clean += region.private_clean;
entry.private_dirty += region.private_dirty;
entry.referenced += region.referenced;
entry.anonymous += region.anonymous;
entry.lazy_free += region.lazy_free;
entry.anon_huge_pages += region.anon_huge_pages;
entry.shmem_pmd_mapped += region.shmem_pmd_mapped;
entry.file_pmd_mapped += region.file_pmd_mapped;
entry.shared_hugetlb += region.shared_hugetlb;
entry.private_hugetlb += region.private_hugetlb;
entry.swap_pss += region.swap_pss;
entry.locked += region.locked;
}

map.into_iter().collect()
Expand Down

0 comments on commit 1bd074a

Please sign in to comment.