Skip to content

Commit

Permalink
cgroupfs: Add Pids controller (#8215)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #8215

Reviewed By: antonis-m

Differential Revision: D52055696

Pulled By: brianc118

fbshipit-source-id: 4c6afc22c89ab21d18b018c5b8294f1f2420375e
  • Loading branch information
ton31337 authored and facebook-github-bot committed Dec 14, 2023
1 parent 2a93635 commit 1654f18
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 2 deletions.
10 changes: 10 additions & 0 deletions below/cgroupfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,16 @@ impl CgroupReader {
self.read_singleline_controllers("cgroup.subtree_control")
}

/// Read pids.current - returning current cgroup number of processes
pub fn read_pids_current(&self) -> Result<u64> {
self.read_singleline_file("pids.current")
}

/// Read pids.max - returning max cgroup number of processes
pub fn read_pids_max(&self) -> Result<i64> {
self.read_singleline_integer_or_max_stat_file("pids.max")
}

/// Read memory.min - returning memory.min limit in bytes
/// Will return -1 if the content is max
pub fn read_memory_min(&self) -> Result<i64> {
Expand Down
2 changes: 2 additions & 0 deletions below/cgroupfs/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ macro_rules! singleline_integer_or_max_test {
};
}

singleline_integer_or_max_test!(read_pids_max, "pids.max");
singleline_integer_or_max_test!(read_memory_min, "memory.min");
singleline_integer_or_max_test!(read_memory_low, "memory.low");
singleline_integer_or_max_test!(read_memory_high, "memory.high");
singleline_integer_or_max_test!(read_memory_max, "memory.max");
singleline_integer_or_max_test!(read_memory_swap_max, "memory.swap.max");
singleline_integer_or_max_test!(read_memory_zswap_max, "memory.zswap.max");

test_success!(read_pids_current, "pids.current", b"10000\n", 10000);
test_success!(read_cpu_weight, "cpu.weight", b"10000\n", 10000);
test_failure!(read_cpu_weight, "cpu.weight", b"5000000000\n");
test_success!(
Expand Down
4 changes: 4 additions & 0 deletions below/dump/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ pub enum CgroupAggField {
Cpu,
Mem,
Io,
Pids,
Pressure,
}

Expand All @@ -557,6 +558,7 @@ impl AggField<SingleCgroupModelFieldId> for CgroupAggField {
use model::CgroupCpuModelFieldId as Cpu;
use model::CgroupIoModelFieldId as Io;
use model::CgroupMemoryModelFieldId as Mem;
use model::CgroupPidsModelFieldId as Pid;
use model::CgroupPressureModelFieldId as Pressure;
use model::SingleCgroupModelFieldId as FieldId;

Expand All @@ -565,6 +567,7 @@ impl AggField<SingleCgroupModelFieldId> for CgroupAggField {
Self::Cpu => enum_iterator::all::<Cpu>().map(FieldId::Cpu).collect(),
Self::Mem => enum_iterator::all::<Mem>().map(FieldId::Mem).collect(),
Self::Io => enum_iterator::all::<Io>().map(FieldId::Io).collect(),
Self::Pids => enum_iterator::all::<Pid>().map(FieldId::Pids).collect(),
Self::Pressure => enum_iterator::all::<Pressure>()
.map(FieldId::Pressure)
.collect(),
Expand All @@ -575,6 +578,7 @@ impl AggField<SingleCgroupModelFieldId> for CgroupAggField {
Self::Cpu => vec![FieldId::Cpu(Cpu::UsagePct)],
Self::Mem => vec![FieldId::Mem(Mem::Total)],
Self::Io => vec![FieldId::Io(Io::RbytesPerSec), FieldId::Io(Io::WbytesPerSec)],
Self::Pids => vec![FieldId::Pids(Pid::TidsCurrent)],
Self::Pressure => vec![
FieldId::Pressure(Pressure::CpuSomePct),
FieldId::Pressure(Pressure::MemoryFullPct),
Expand Down
40 changes: 39 additions & 1 deletion below/model/src/cgroup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use super::*;

/// Collection of all data local to the cgroup, e.g. its memory/io/cpu usage.
/// Collection of all data local to the cgroup, e.g. its memory/io/cpu/pids usage.
/// Nothing about child cgroups or siblings, and therefore "Single" in its name.
#[derive(Clone, Debug, Default, Serialize, Deserialize, below_derive::Queriable)]
pub struct SingleCgroupModel {
Expand All @@ -32,6 +32,9 @@ pub struct SingleCgroupModel {
#[queriable(preferred_name = mem)]
pub memory: Option<CgroupMemoryModel>,
#[queriable(subquery)]
#[queriable(preferred_name = pids)]
pub pids: Option<CgroupPidsModel>,
#[queriable(subquery)]
#[queriable(preferred_name = io_details)]
pub io: Option<BTreeMap<String, CgroupIoModel>>,
#[queriable(subquery)]
Expand Down Expand Up @@ -220,6 +223,8 @@ impl CgroupModel {

let memory = Some(CgroupMemoryModel::new(sample, last));

let pids = Some(CgroupPidsModel::new(sample));

let pressure = sample
.pressure
.as_ref()
Expand Down Expand Up @@ -278,6 +283,7 @@ impl CgroupModel {
properties,
cpu,
memory,
pids,
io,
io_total,
pressure,
Expand Down Expand Up @@ -711,6 +717,36 @@ impl CgroupMemoryModel {
}
}

#[derive(
Clone,
Debug,
Default,
PartialEq,
Serialize,
Deserialize,
below_derive::Queriable
)]
pub struct CgroupPidsModel {
pub tids_current: Option<u64>,
}

impl std::ops::Add for CgroupPidsModel {
type Output = Self;

fn add(self, other: Self) -> Self::Output {
Self {
tids_current: opt_add(self.tids_current, other.tids_current),
}
}
}

impl CgroupPidsModel {
pub fn new(sample: &CgroupSample) -> Self {
let tids_current = sample.tids_current;
CgroupPidsModel { tids_current }
}
}

#[derive(
Clone,
Debug,
Expand Down Expand Up @@ -876,6 +912,7 @@ impl CgroupMemoryNumaModel {
pub struct CgroupProperties {
pub cgroup_controllers: Option<BTreeSet<String>>,
pub cgroup_subtree_control: Option<BTreeSet<String>>,
pub tids_max: Option<i64>,
pub memory_min: Option<i64>,
pub memory_low: Option<i64>,
pub memory_high: Option<i64>,
Expand All @@ -896,6 +933,7 @@ impl CgroupProperties {
Self {
cgroup_controllers: sample.cgroup_controllers.clone(),
cgroup_subtree_control: sample.cgroup_subtree_control.clone(),
tids_max: sample.tids_max,
memory_min: sample.memory_min,
memory_low: sample.memory_low,
memory_high: sample.memory_high,
Expand Down
2 changes: 2 additions & 0 deletions below/model/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ fn collect_cgroup_sample(
Ok(CgroupSample {
cpu_stat: wrap(reader.read_cpu_stat())?.map(Into::into),
io_stat,
tids_current: wrap(reader.read_pids_current())?,
tids_max: wrap(reader.read_pids_max())?,
memory_current: wrap(reader.read_memory_current().map(|v| v as i64))?,
memory_stat: wrap(reader.read_memory_stat())?.map(Into::into),
pressure: pressure_wrap(reader.read_pressure())?.map(Into::into),
Expand Down
4 changes: 3 additions & 1 deletion below/model/src/common_field_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
///
/// This list also servers as documentation for available field ids that could
/// be used in other below crates. A test ensures that this list is up-to-date.
pub const COMMON_MODEL_FIELD_IDS: [&str; 408] = [
pub const COMMON_MODEL_FIELD_IDS: [&str; 410] = [
"system.hostname",
"system.kernel_version",
"system.os_release",
Expand Down Expand Up @@ -140,6 +140,7 @@ pub const COMMON_MODEL_FIELD_IDS: [&str; 408] = [
"cgroup.[path:/<cgroup_path>/.]props.cpuset_cpus_effective",
"cgroup.[path:/<cgroup_path>/.]props.cpuset_mems",
"cgroup.[path:/<cgroup_path>/.]props.cpuset_mems_effective",
"cgroup.[path:/<cgroup_path>/.]props.tids_max",
"cgroup.[path:/<cgroup_path>/.]props.memory_high",
"cgroup.[path:/<cgroup_path>/.]props.memory_low",
"cgroup.[path:/<cgroup_path>/.]props.memory_max",
Expand All @@ -152,6 +153,7 @@ pub const COMMON_MODEL_FIELD_IDS: [&str; 408] = [
"cgroup.[path:/<cgroup_path>/.]cpu.nr_periods_per_sec",
"cgroup.[path:/<cgroup_path>/.]cpu.nr_throttled_per_sec",
"cgroup.[path:/<cgroup_path>/.]cpu.throttled_pct",
"cgroup.[path:/<cgroup_path>/.]pids.tids_current",
"cgroup.[path:/<cgroup_path>/.]mem.total",
"cgroup.[path:/<cgroup_path>/.]mem.swap",
"cgroup.[path:/<cgroup_path>/.]mem.anon",
Expand Down
2 changes: 2 additions & 0 deletions below/model/src/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct Sample {
pub struct CgroupSample {
pub cpu_stat: Option<cgroupfs::CpuStat>,
pub io_stat: Option<BTreeMap<String, cgroupfs::IoStat>>,
pub tids_current: Option<u64>,
pub tids_max: Option<i64>,
pub memory_current: Option<i64>,
pub memory_stat: Option<cgroupfs::MemoryStat>,
pub pressure: Option<cgroupfs::Pressure>,
Expand Down
16 changes: 16 additions & 0 deletions below/render/src/default_configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl HasRenderConfig for model::SingleCgroupModel {
model::CgroupMemoryNumaModel::get_render_config_builder(&field_id.subquery_id)
}
Props(field_id) => model::CgroupProperties::get_render_config_builder(field_id),
Pids(field_id) => model::CgroupPidsModel::get_render_config_builder(field_id),
}
}
}
Expand Down Expand Up @@ -154,6 +155,7 @@ impl HasRenderConfigForDump for model::SingleCgroupModel {
use model::CgroupCpuModelFieldId::*;
use model::CgroupIoModelFieldId::*;
use model::CgroupMemoryModelFieldId::*;
use model::CgroupPidsModelFieldId::*;
use model::CgroupPressureModelFieldId::*;
use model::CgroupStatModelFieldId::*;
use model::SingleCgroupModelFieldId::*;
Expand All @@ -175,6 +177,9 @@ impl HasRenderConfigForDump for model::SingleCgroupModel {
NrThrottledPerSec => Some(gauge),
ThrottledPct => Some(gauge.unit("percent")),
},
Pids(field_id) => match field_id {
TidsCurrent => Some(counter.unit("count")),
},
Io(field_id) => match field_id {
RbytesPerSec => Some(gauge.unit("bytes_per_second")),
WbytesPerSec => Some(gauge.unit("bytes_per_second")),
Expand Down Expand Up @@ -276,6 +281,16 @@ impl HasRenderConfig for model::CgroupCpuModel {
}
}

impl HasRenderConfig for model::CgroupPidsModel {
fn get_render_config_builder(field_id: &Self::FieldId) -> RenderConfigBuilder {
use model::CgroupPidsModelFieldId::*;
let rc = RenderConfigBuilder::new();
match field_id {
TidsCurrent => rc.title("Tids Current").format(Precision(1)),
}
}
}

impl HasRenderConfig for model::CgroupIoModel {
fn get_render_config_builder(field_id: &Self::FieldId) -> RenderConfigBuilder {
use model::CgroupIoModelFieldId::*;
Expand Down Expand Up @@ -1476,6 +1491,7 @@ impl HasRenderConfig for model::CgroupProperties {
// "cpu cpuset hugetlb io memory pids" is 33 chars
CgroupControllers => rc.title("Controllers").width(35),
CgroupSubtreeControl => rc.title("SubtreeControl").width(35),
TidsMax => rc.title("Tids Max").format(MaxOrReadableSize),
MemoryMin => rc.title("Mem Min").format(MaxOrReadableSize),
MemoryLow => rc.title("Mem Low").format(MaxOrReadableSize),
MemoryHigh => rc.title("Mem High").format(MaxOrReadableSize),
Expand Down
5 changes: 5 additions & 0 deletions below/view/src/cgroup_tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ pub mod default_tabs {
use model::CgroupMemoryModelFieldId::WorkingsetRestoreFile;
use model::CgroupMemoryModelFieldId::Zswap;
use model::CgroupMemoryModelFieldId::Zswapped;
use model::CgroupPidsModelFieldId::TidsCurrent;
use model::CgroupPressureModelFieldId::CpuFullPct;
use model::CgroupPressureModelFieldId::CpuSomePct;
use model::CgroupPressureModelFieldId::IoFullPct;
Expand All @@ -301,13 +302,15 @@ pub mod default_tabs {
use model::CgroupPropertiesFieldId::MemoryMin;
use model::CgroupPropertiesFieldId::MemorySwapMax;
use model::CgroupPropertiesFieldId::MemoryZswapMax;
use model::CgroupPropertiesFieldId::TidsMax;
use model::CgroupStatModelFieldId::NrDescendants;
use model::CgroupStatModelFieldId::NrDyingDescendants;
use model::SingleCgroupModelFieldId::CgroupStat;
use model::SingleCgroupModelFieldId::Cpu;
use model::SingleCgroupModelFieldId::Io;
use model::SingleCgroupModelFieldId::Mem;
use model::SingleCgroupModelFieldId::Name;
use model::SingleCgroupModelFieldId::Pids;
use model::SingleCgroupModelFieldId::Pressure;
use model::SingleCgroupModelFieldId::Props;
use once_cell::sync::Lazy;
Expand All @@ -333,6 +336,7 @@ pub mod default_tabs {
ViewItem::from_default(Io(RwbytesPerSec)),
ViewItem::from_default(CgroupStat(NrDescendants)),
ViewItem::from_default(CgroupStat(NrDyingDescendants)),
ViewItem::from_default(Pids(TidsCurrent)),
])
});

Expand Down Expand Up @@ -437,6 +441,7 @@ pub mod default_tabs {
ViewItem::from_default(Props(CpuWeight)),
ViewItem::from_default(Props(CpusetCpus)),
ViewItem::from_default(Props(CpusetCpusEffective)),
ViewItem::from_default(Props(TidsMax)),
ViewItem::from_default(Props(CgroupControllers)),
])
});
Expand Down

0 comments on commit 1654f18

Please sign in to comment.