Skip to content

Commit

Permalink
(incomplete) move individual systemd components to dbus_native
Browse files Browse the repository at this point in the history
Signed-off-by: Yashodhan Joshi <[email protected]>
  • Loading branch information
YJDoc2 committed Sep 21, 2023
1 parent b678337 commit 982ec4b
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 76 deletions.
4 changes: 2 additions & 2 deletions crates/libcgroups/src/systemd/controller.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use dbus::arg::RefArg;
use super::dbus_native::serialize::DbusSerialize;

use crate::common::ControllerOpt;

Expand All @@ -10,6 +10,6 @@ pub(super) trait Controller {
fn apply(
options: &ControllerOpt,
systemd_version: u32,
properties: &mut HashMap<&str, Box<dyn RefArg>>,
properties: &mut HashMap<&str, Box<dyn DbusSerialize>>,
) -> Result<(), Self::Error>;
}
26 changes: 14 additions & 12 deletions crates/libcgroups/src/systemd/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use dbus::arg::RefArg;
use super::dbus_native::serialize::DbusSerialize;
use oci_spec::runtime::LinuxCpu;

use super::controller::Controller;
Expand All @@ -25,7 +25,7 @@ impl Controller for Cpu {
fn apply(
options: &ControllerOpt,
_: u32,
properties: &mut HashMap<&str, Box<dyn RefArg>>,
properties: &mut HashMap<&str, Box<dyn DbusSerialize>>,
) -> Result<(), Self::Error> {
if let Some(cpu) = options.resources.cpu() {
tracing::debug!("Applying cpu resource restrictions");
Expand All @@ -39,7 +39,7 @@ impl Controller for Cpu {
impl Cpu {
fn apply(
cpu: &LinuxCpu,
properties: &mut HashMap<&str, Box<dyn RefArg>>,
properties: &mut HashMap<&str, Box<dyn DbusSerialize>>,
) -> Result<(), SystemdCpuError> {
if Self::is_realtime_requested(cpu) {
return Err(SystemdCpuError::RealtimeSystemd);
Expand Down Expand Up @@ -95,6 +95,8 @@ mod tests {
use dbus::arg::ArgType;
use oci_spec::runtime::LinuxCpuBuilder;

use crate::recast;

use super::*;

#[test]
Expand All @@ -104,7 +106,7 @@ mod tests {
.shares(22000u64)
.build()
.context("build cpu spec")?;
let mut properties: HashMap<&str, Box<dyn RefArg>> = HashMap::new();
let mut properties: HashMap<&str, Box<dyn DbusSerialize>> = HashMap::new();

// act
Cpu::apply(&cpu, &mut properties)?;
Expand All @@ -113,8 +115,8 @@ mod tests {
assert!(properties.contains_key(CPU_WEIGHT));

let cpu_weight = &properties[CPU_WEIGHT];
assert_eq!(cpu_weight.arg_type(), ArgType::UInt64);
assert_eq!(cpu_weight.as_u64().unwrap(), 840u64);
let val = recast!(cpu_weight, u64)?;
assert_eq!(val, 840u64);

Ok(())
}
Expand All @@ -126,16 +128,16 @@ mod tests {
for quota in quotas {
// arrange
let cpu = LinuxCpuBuilder::default().quota(quota.0).build().unwrap();
let mut properties: HashMap<&str, Box<dyn RefArg>> = HashMap::new();
let mut properties: HashMap<&str, Box<dyn DbusSerialize>> = HashMap::new();

// act
Cpu::apply(&cpu, &mut properties)?;

// assert
assert!(properties.contains_key(CPU_QUOTA));
let cpu_quota = &properties[CPU_QUOTA];
assert_eq!(cpu_quota.arg_type(), ArgType::UInt64);
assert_eq!(cpu_quota.as_u64().unwrap(), quota.1);
let val = recast!(cpu_quota, u64)?;
assert_eq!(val, quota.1);
}

Ok(())
Expand All @@ -150,16 +152,16 @@ mod tests {
.period(period.0)
.build()
.context("build cpu spec")?;
let mut properties: HashMap<&str, Box<dyn RefArg>> = HashMap::new();
let mut properties: HashMap<&str, Box<dyn DbusSerialize>> = HashMap::new();

// act
Cpu::apply(&cpu, &mut properties)?;

// assert
assert!(properties.contains_key(CPU_PERIOD));
let cpu_quota = &properties[CPU_PERIOD];
assert_eq!(cpu_quota.arg_type(), ArgType::UInt64);
assert_eq!(cpu_quota.as_u64().unwrap(), period.1);
let val = recast!(cpu_quota, u64)?;
assert_eq!(val, period.1);
}

Ok(())
Expand Down
16 changes: 9 additions & 7 deletions crates/libcgroups/src/systemd/cpuset.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use dbus::arg::RefArg;
use super::dbus_native::serialize::DbusSerialize;
use fixedbitset::FixedBitSet;
use oci_spec::runtime::LinuxCpu;

Expand Down Expand Up @@ -29,7 +29,7 @@ impl Controller for CpuSet {
fn apply(
options: &ControllerOpt,
systemd_version: u32,
properties: &mut HashMap<&str, Box<dyn RefArg>>,
properties: &mut HashMap<&str, Box<dyn DbusSerialize>>,
) -> Result<(), Self::Error> {
if let Some(cpu) = options.resources.cpu() {
tracing::debug!("Applying cpuset resource restrictions");
Expand All @@ -44,7 +44,7 @@ impl CpuSet {
fn apply(
cpu: &LinuxCpu,
systemd_version: u32,
properties: &mut HashMap<&str, Box<dyn RefArg>>,
properties: &mut HashMap<&str, Box<dyn DbusSerialize>>,
) -> Result<(), SystemdCpuSetError> {
if systemd_version <= 243 {
return Err(SystemdCpuSetError::OldSystemd);
Expand Down Expand Up @@ -131,6 +131,8 @@ mod tests {
use dbus::arg::{ArgType, RefArg};
use oci_spec::runtime::LinuxCpuBuilder;

use crate::recast;

use super::*;

#[test]
Expand Down Expand Up @@ -217,7 +219,7 @@ mod tests {
let cpu = LinuxCpuBuilder::default()
.build()
.context("build cpu spec")?;
let mut properties: HashMap<&str, Box<dyn RefArg>> = HashMap::new();
let mut properties: HashMap<&str, Box<dyn DbusSerialize>> = HashMap::new();

let result = CpuSet::apply(&cpu, systemd_version, &mut properties);

Expand All @@ -233,18 +235,18 @@ mod tests {
.mems("0-3")
.build()
.context("build cpu spec")?;
let mut properties: HashMap<&str, Box<dyn RefArg>> = HashMap::new();
let mut properties: HashMap<&str, Box<dyn DbusSerialize>> = HashMap::new();

CpuSet::apply(&cpu, systemd_version, &mut properties).context("apply cpuset")?;

assert_eq!(properties.len(), 2);
assert!(properties.contains_key(ALLOWED_CPUS));
let cpus = properties.get(ALLOWED_CPUS).unwrap();
assert_eq!(cpus.arg_type(), ArgType::Array);
recast!(cpus, Vec<u8>)?;

assert!(properties.contains_key(ALLOWED_NODES));
let mems = properties.get(ALLOWED_NODES).unwrap();
assert_eq!(mems.arg_type(), ArgType::Array);
recast!(mems, Vec<u8>)?;

Ok(())
}
Expand Down
25 changes: 24 additions & 1 deletion crates/libcgroups/src/systemd/dbus_native/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::utils::{adjust_padding, align_counter, Result, SystemdClientError};

/// This indicates that given type can be serialized as dbus
/// message body, and has methods needed for that
pub trait DbusSerialize {
pub trait DbusSerialize: std::fmt::Debug {
/// Provide signature for the given type in the dbus signature format
fn get_signature() -> String
where
Expand All @@ -24,6 +24,7 @@ pub trait DbusSerialize {
#[derive(Debug)]
pub struct Variant<T>(pub T);

#[derive(Debug)]
pub struct Structure {
key: String,
val: Box<dyn DbusSerialize>,
Expand Down Expand Up @@ -113,6 +114,28 @@ impl DbusSerialize for bool {
}
}

impl DbusSerialize for u8 {
fn get_signature() -> String {
"y".to_string()
}

fn serialize(&self, buf: &mut Vec<u8>) {
adjust_padding(buf, 1);
buf.extend_from_slice(&self.to_le_bytes());
}
fn deserialize(buf: &[u8], counter: &mut usize) -> Result<Self> {
align_counter(counter, 1);
if buf.len() < *counter + 1 {
return Err(SystemdClientError::DeserializationError(
"incomplete u8 response : partial response".into(),
));
}
let ret = u8::from_le_bytes(buf[*counter..*counter + 1].try_into().unwrap());
*counter += 1;
Ok(ret)
}
}

impl DbusSerialize for u16 {
fn get_signature() -> String {
"q".to_string()
Expand Down
8 changes: 4 additions & 4 deletions crates/libcgroups/src/systemd/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
path::Component::RootDir,
};

use dbus::arg::RefArg;
use super::dbus_native::serialize::DbusSerialize;
use nix::{unistd::Pid, NixPath};
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -369,7 +369,7 @@ impl CgroupManager for Manager {
}

fn apply(&self, controller_opt: &ControllerOpt) -> Result<(), Self::Error> {
let mut properties: HashMap<&str, Box<dyn RefArg>> = HashMap::new();
let mut properties: HashMap<&str, Box<dyn DbusSerialize>> = HashMap::new();
let systemd_version = self.client.systemd_version()?;

for controller in CONTROLLER_TYPES {
Expand All @@ -393,8 +393,8 @@ impl CgroupManager for Manager {
};
}

tracing::debug!("applying properties {:?}", properties);
Unified::apply(controller_opt, systemd_version, &mut properties)?;
tracing::debug!("{:?}", properties);

if !properties.is_empty() {
self.ensure_controllers_attached()?;
Expand Down Expand Up @@ -464,7 +464,7 @@ mod tests {
fn set_unit_properties(
&self,
_unit_name: &str,
_properties: &HashMap<&str, Box<dyn RefArg>>,
_properties: &HashMap<&str, Box<dyn DbusSerialize>>,
) -> Result<(), SystemdClientError> {
Ok(())
}
Expand Down
25 changes: 13 additions & 12 deletions crates/libcgroups/src/systemd/memory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use dbus::arg::RefArg;
use super::dbus_native::serialize::DbusSerialize;
use oci_spec::runtime::LinuxMemory;

use crate::common::ControllerOpt;
Expand Down Expand Up @@ -31,7 +31,7 @@ impl Controller for Memory {
fn apply(
options: &ControllerOpt,
_: u32,
properties: &mut HashMap<&str, Box<dyn RefArg>>,
properties: &mut HashMap<&str, Box<dyn DbusSerialize>>,
) -> Result<(), Self::Error> {
if let Some(memory) = options.resources.memory() {
tracing::debug!("applying memory resource restrictions");
Expand All @@ -45,7 +45,7 @@ impl Controller for Memory {
impl Memory {
fn apply(
memory: &LinuxMemory,
properties: &mut HashMap<&str, Box<dyn RefArg>>,
properties: &mut HashMap<&str, Box<dyn DbusSerialize>>,
) -> Result<(), SystemdMemoryError> {
if let Some(reservation) = memory.reservation() {
match reservation {
Expand Down Expand Up @@ -83,9 +83,9 @@ impl Memory {
fn apply_swap(
swap: Option<i64>,
limit: Option<i64>,
properties: &mut HashMap<&str, Box<dyn RefArg>>,
properties: &mut HashMap<&str, Box<dyn DbusSerialize>>,
) -> Result<(), SystemdMemoryError> {
let value: Box<dyn RefArg> = match (limit, swap) {
let value: Box<dyn DbusSerialize> = match (limit, swap) {
// memory is unlimited and swap not specified -> assume swap unlimited
(Some(-1), None) => Box::new(u64::MAX),
// if swap is unlimited it can be set to unlimited regardless of memory limit value
Expand Down Expand Up @@ -115,9 +115,10 @@ impl Memory {
#[cfg(test)]
mod tests {
use anyhow::{Context, Result};
use dbus::arg::ArgType;
use oci_spec::runtime::LinuxMemoryBuilder;

use crate::recast;

use super::*;

#[test]
Expand All @@ -130,7 +131,7 @@ mod tests {
.reservation(reservation)
.build()
.context("build memory spec")?;
let mut properties: HashMap<&str, Box<dyn RefArg>> = HashMap::new();
let mut properties: HashMap<&str, Box<dyn DbusSerialize>> = HashMap::new();

// act
Memory::apply(&memory, &mut properties).context("apply memory")?;
Expand All @@ -139,8 +140,8 @@ mod tests {
assert_eq!(properties.len(), 1);
assert!(properties.contains_key(MEMORY_LOW));
let memory_low = &properties[MEMORY_LOW];
assert_eq!(memory_low.arg_type(), ArgType::UInt64);
assert_eq!(memory_low.as_u64().unwrap(), expected);
let val = recast!(memory_low, u64)?;
assert_eq!(val, expected);
}

Ok(())
Expand All @@ -156,7 +157,7 @@ mod tests {
.limit(reservation)
.build()
.context("build memory spec")?;
let mut properties: HashMap<&str, Box<dyn RefArg>> = HashMap::new();
let mut properties: HashMap<&str, Box<dyn DbusSerialize>> = HashMap::new();

// act
Memory::apply(&memory, &mut properties).context("apply memory")?;
Expand All @@ -165,8 +166,8 @@ mod tests {
assert_eq!(properties.len(), prop_count);
assert!(properties.contains_key(MEMORY_MAX));
let memory_low = &properties[MEMORY_MAX];
assert_eq!(memory_low.arg_type(), ArgType::UInt64);
assert_eq!(memory_low.as_u64().unwrap(), mem_low);
let val = recast!(mem_low, u64)?;
assert_eq!(val, mem_low);
}

Ok(())
Expand Down
13 changes: 13 additions & 0 deletions crates/libcgroups/src/systemd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@ pub fn booted() -> bool {
.map(|p| p.is_dir())
.unwrap_or_default()
}

#[macro_export]
macro_rules! recast {
($v:ident, $t:ty) => {{
let mut buf = Vec::new();
$v.serialize(&mut buf);
let mut ctr = 0;
let ret = <$t>::deserialize(&buf, &mut ctr);
ret
}};
}

pub(super) use recast;
Loading

0 comments on commit 982ec4b

Please sign in to comment.