1use crate::error::{platform_err, Result};
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum MlockallPreset {
8 None,
10 On,
12}
13
14impl MlockallPreset {
15 pub fn parse(s: &str) -> Option<Self> {
17 match s {
18 "none" => Some(Self::None),
19 "on" => Some(Self::On),
20 _ => None,
21 }
22 }
23
24 pub fn as_str(self) -> &'static str {
26 match self {
27 Self::None => "none",
28 Self::On => "on",
29 }
30 }
31}
32
33#[derive(Clone, Copy, Debug, PartialEq, Eq)]
37pub enum SchedPreset {
38 Other,
40 Fifo {
42 prio: u8,
44 },
45 Rr {
47 prio: u8,
49 },
50}
51
52impl SchedPreset {
53 pub fn parse_kind(s: &str) -> Option<Self> {
55 match s {
56 "other" => Some(Self::Other),
57 "fifo" => Some(Self::Fifo { prio: 80 }),
58 "rr" => Some(Self::Rr { prio: 80 }),
59 _ => None,
60 }
61 }
62
63 pub fn kind_str(self) -> &'static str {
65 match self {
66 Self::Other => "other",
67 Self::Fifo { .. } => "fifo",
68 Self::Rr { .. } => "rr",
69 }
70 }
71
72 pub fn is_realtime(self) -> bool {
74 matches!(self, Self::Fifo { .. } | Self::Rr { .. })
75 }
76
77 pub fn rt_prio(self) -> Option<u8> {
79 match self {
80 Self::Other => None,
81 Self::Fifo { prio } => Some(prio),
82 Self::Rr { prio } => Some(prio),
83 }
84 }
85
86 pub fn with_rt_prio(self, prio: u8) -> Self {
88 match self {
89 Self::Other => Self::Other,
90 Self::Fifo { .. } => Self::Fifo { prio },
91 Self::Rr { .. } => Self::Rr { prio },
92 }
93 }
94}
95
96pub fn apply_sched(preset: SchedPreset) -> Result<()> {
98 #[cfg(target_os = "linux")]
99 {
100 match preset {
101 SchedPreset::Other => {
102 indexbus_platform_ops::linux::sched::set_current_thread_sched_other()
103 .map_err(|e| platform_err("sched other failed", e))
104 }
105 SchedPreset::Fifo { prio } => {
106 indexbus_platform_ops::linux::sched::set_current_thread_sched_fifo(prio as i32)
107 .map_err(|e| platform_err("sched fifo failed", e))
108 }
109 SchedPreset::Rr { prio } => {
110 indexbus_platform_ops::linux::sched::set_current_thread_sched_rr(prio as i32)
111 .map_err(|e| platform_err("sched rr failed", e))
112 }
113 }
114 }
115
116 #[cfg(not(target_os = "linux"))]
117 {
118 let _ = preset;
119 Err(crate::OpsError::Invalid {
120 what: "scheduling presets are only supported on Linux".to_string(),
121 })
122 }
123}
124
125pub fn apply_mlockall(preset: MlockallPreset) -> Result<()> {
127 match preset {
128 MlockallPreset::None => Ok(()),
129 MlockallPreset::On => indexbus_platform_ops::memory::mlockall_current_and_future()
130 .map_err(|e| platform_err("mlockall failed", e)),
131 }
132}