systemd_run/
cpu_sched.rs

1#[cfg(feature = "systemd_252")]
2use std::num::NonZeroU8;
3
4pub enum CpuSchedulingPolicy {
5    Other = 0,
6    Batch = 3,
7    Idle = 5,
8    Fifo = 1,
9    RoundRobin = 2,
10}
11
12/// The CPU scheduling for running a transient service on the system service
13/// manager.
14/// See `CPUSchedulingPolicy=`, `CPUSchedulingPriority=`, and
15/// `CPUSchedulingResetOnFork=` in [systemd.exec(5)](man:systemd.exec(5))
16/// and [sched_setscheduler(2)](man:sched_setscheduler(2)) for details.
17pub struct CpuScheduling {
18    policy: CpuSchedulingPolicy,
19    real_time_priority: Option<u8>,
20    reset_on_fork: bool,
21}
22
23pub fn marshal(sched: CpuScheduling) -> (i32, Option<i32>, bool) {
24    let a = sched.policy as i32;
25    let b = sched.real_time_priority.map(u8::into);
26    (a, b, sched.reset_on_fork)
27}
28
29impl Default for CpuScheduling {
30    /// The default CPU scheduling policy, `SCHED_OTHER`.
31    fn default() -> Self {
32        Self {
33            policy: CpuSchedulingPolicy::Other,
34            real_time_priority: None,
35            reset_on_fork: false,
36        }
37    }
38}
39
40impl CpuScheduling {
41    /// For "batch" style execution of processes, `SCHED_BATCH`.
42    pub fn batch() -> Self {
43        Self {
44            policy: CpuSchedulingPolicy::Batch,
45            ..Self::default()
46        }
47    }
48
49    /// For running very low priority background jobs, `SCHED_IDLE`.
50    pub fn idle() -> Self {
51        Self {
52            policy: CpuSchedulingPolicy::Idle,
53            ..Self::default()
54        }
55    }
56
57    /// A first-in, first-out real-time policy, `SCHED_FIFO`, with specified
58    /// priority. The priority must be in [1, 99].
59    ///
60    /// This would be unavailable if the feature `systemd_252` is disabled
61    /// because it was broken by
62    /// [systemd #20320](https://github.com/systemd/systemd/issues/20320).
63    /// Use [Self::fifo_default_priority] instead.
64    #[cfg(feature = "systemd_252")]
65    pub fn fifo(p: NonZeroU8) -> Self {
66        Self {
67            policy: CpuSchedulingPolicy::Fifo,
68            real_time_priority: Some(p.into()),
69            reset_on_fork: false,
70        }
71    }
72
73    /// Like [Self::fifo] but with default priority.
74    pub fn fifo_default_priority() -> Self {
75        Self {
76            policy: CpuSchedulingPolicy::Fifo,
77            ..Self::default()
78        }
79    }
80    /// A round-robin real-time policy, `SCHED_RR`, with specified priority.
81    /// The priority must be in [1, 99].
82    ///
83    /// This would be unavailable if the feature `systemd_252` is disabled
84    /// because it was broken by
85    /// [systemd #20320](https://github.com/systemd/systemd/issues/20320).
86    /// Use [Self::round_robin_default_priority] instead.
87    #[cfg(feature = "systemd_252")]
88    pub fn round_robin(p: NonZeroU8) -> Self {
89        Self {
90            policy: CpuSchedulingPolicy::RoundRobin,
91            real_time_priority: Some(p.into()),
92            reset_on_fork: false,
93        }
94    }
95
96    /// Like [Self::round_robin] but with default priority.
97    pub fn round_robin_default_priority() -> Self {
98        Self {
99            policy: CpuSchedulingPolicy::RoundRobin,
100            ..Self::default()
101        }
102    }
103
104    /// Make the children created by fork(2) do not inherit privileged
105    /// scheduling policies.
106    pub fn reset_on_fork(self) -> Self {
107        Self {
108            reset_on_fork: true,
109            ..self
110        }
111    }
112}