cgroups_rs/systemd/
cpu.rs

1// Copyright (c) 2025 Ant Group
2//
3// SPDX-License-Identifier: Apache-2.0 or MIT
4//
5
6use crate::systemd::error::{Error, Result};
7use crate::systemd::{
8    CPU_QUOTA_PERIOD_US, CPU_QUOTA_PER_SEC_US, CPU_SHARES, CPU_SYSTEMD_VERSION, CPU_WEIGHT,
9};
10
11/// Returns the property for CPU shares.
12///
13/// Please note that if the shares is obtained from OCI runtime spec, it
14/// MUST be converted, see [1] and `convert_shares_to_v2()`.
15///
16/// 1: https://github.com/containers/crun/blob/main/crun.1.md#cgroup-v2
17pub fn shares(shares: u64, v2: bool) -> Result<(&'static str, u64)> {
18    let id = if v2 { CPU_WEIGHT } else { CPU_SHARES };
19
20    Ok((id, shares))
21}
22
23/// Returns the property for CPU period.
24pub fn period(period: u64, systemd_version: usize) -> Result<(&'static str, u64)> {
25    if systemd_version < CPU_SYSTEMD_VERSION {
26        return Err(Error::ObsoleteSystemd);
27    }
28
29    Ok((CPU_QUOTA_PERIOD_US, period))
30}
31
32/// Return the property for CPU quota.
33pub fn quota(quota: u64) -> Result<(&'static str, u64)> {
34    Ok((CPU_QUOTA_PER_SEC_US, quota))
35}