1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#[cfg(feature = "systemd_252")]
use std::num::NonZeroU8;

pub enum CpuSchedulingPolicy {
    Other = 0,
    Batch = 3,
    Idle = 5,
    Fifo = 1,
    RoundRobin = 2,
}

/// The CPU scheduling for running a transient service on the system service
/// manager.
/// See `CPUSchedulingPolicy=`, `CPUSchedulingPriority=`, and
/// `CPUSchedulingResetOnFork=` in [systemd.exec(5)](man:systemd.exec(5))
/// and [sched_setscheduler(2)](man:sched_setscheduler(2)) for details.
pub struct CpuScheduling {
    policy: CpuSchedulingPolicy,
    real_time_priority: Option<u8>,
    reset_on_fork: bool,
}

pub fn marshal(sched: CpuScheduling) -> (i32, Option<i32>, bool) {
    let a = sched.policy as i32;
    let b = sched.real_time_priority.map(u8::into);
    (a, b, sched.reset_on_fork)
}

impl Default for CpuScheduling {
    /// The default CPU scheduling policy, `SCHED_OTHER`.
    fn default() -> Self {
        Self {
            policy: CpuSchedulingPolicy::Other,
            real_time_priority: None,
            reset_on_fork: false,
        }
    }
}

impl CpuScheduling {
    /// For "batch" style execution of processes, `SCHED_BATCH`.
    pub fn batch() -> Self {
        Self {
            policy: CpuSchedulingPolicy::Batch,
            ..Self::default()
        }
    }

    /// For running very low priority background jobs, `SCHED_IDLE`.
    pub fn idle() -> Self {
        Self {
            policy: CpuSchedulingPolicy::Idle,
            ..Self::default()
        }
    }

    /// A first-in, first-out real-time policy, `SCHED_FIFO`, with specified
    /// priority. The priority must be in [1, 99].
    ///
    /// This would be unavailable if the feature `systemd_252` is disabled
    /// because it was broken by
    /// [systemd #20320](https://github.com/systemd/systemd/issues/20320).
    /// Use [Self::fifo_default_priority] instead.
    #[cfg(feature = "systemd_252")]
    pub fn fifo(p: NonZeroU8) -> Self {
        Self {
            policy: CpuSchedulingPolicy::Fifo,
            real_time_priority: Some(p.into()),
            reset_on_fork: false,
        }
    }

    /// Like [Self::fifo] but with default priority.
    pub fn fifo_default_priority() -> Self {
        Self {
            policy: CpuSchedulingPolicy::Fifo,
            ..Self::default()
        }
    }
    /// A round-robin real-time policy, `SCHED_RR`, with specified priority.
    /// The priority must be in [1, 99].
    ///
    /// This would be unavailable if the feature `systemd_252` is disabled
    /// because it was broken by
    /// [systemd #20320](https://github.com/systemd/systemd/issues/20320).
    /// Use [Self::round_robin_default_priority] instead.
    #[cfg(feature = "systemd_252")]
    pub fn round_robin(p: NonZeroU8) -> Self {
        Self {
            policy: CpuSchedulingPolicy::RoundRobin,
            real_time_priority: Some(p.into()),
            reset_on_fork: false,
        }
    }

    /// Like [Self::round_robin] but with default priority.
    pub fn round_robin_default_priority() -> Self {
        Self {
            policy: CpuSchedulingPolicy::RoundRobin,
            ..Self::default()
        }
    }

    /// Make the children created by fork(2) do not inherit privileged
    /// scheduling policies.
    pub fn reset_on_fork(self) -> Self {
        Self {
            reset_on_fork: true,
            ..self
        }
    }
}