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::Result;
7use crate::systemd::{CPU_QUOTA_PERIOD_US, CPU_QUOTA_PER_SEC_US, CPU_SHARES, CPU_WEIGHT};
8
9/// Returns the property for CPU shares.
10///
11/// Please note that if the shares is obtained from OCI runtime spec, it
12/// MUST be converted, see [1] and `convert_shares_to_v2()`.
13///
14/// 1: https://github.com/containers/crun/blob/main/crun.1.md#cgroup-v2
15pub fn shares(shares: u64, v2: bool) -> Result<(&'static str, u64)> {
16    let id = if v2 { CPU_WEIGHT } else { CPU_SHARES };
17
18    Ok((id, shares))
19}
20
21/// Returns the property for CPU period.
22pub fn period(period: u64) -> Result<(&'static str, u64)> {
23    Ok((CPU_QUOTA_PERIOD_US, period))
24}
25
26/// Return the property for CPU quota.
27pub fn quota(quota: u64) -> Result<(&'static str, u64)> {
28    Ok((CPU_QUOTA_PER_SEC_US, quota))
29}