1use super::{
6 Advice, AutoPlan, JobSizing, MemoryBound, MemoryHold, PAGE_CACHE_ROOM, Peaks, ProfileSizing,
7 Sizing, SizingChange, SizingHold, SizingRefused, Starts,
8};
9use crate::api::{
10 Check, CheckStatus, JOBS_HEADING, LabelledFigure, Tone, Toned, cores_used, duration,
11};
12use crate::config::ByteSize;
13
14pub const GRANULE: u64 = 256 << 20;
16
17pub const SAMPLE_INTERVAL: u64 = 2;
20
21pub fn need(peak: u64, margin: u32) -> u64 {
25 let room = (peak.saturating_mul(u64::from(margin)) / 100).max(PAGE_CACHE_ROOM);
26 peak.saturating_add(room)
27 .div_ceil(GRANULE)
28 .saturating_mul(GRANULE)
29 .max(GRANULE)
30}
31
32pub fn sampled_note(interval: u64) -> String {
35 format!("memory peaks are sampled every {interval} s; spikes shorter than that do not show")
36}
37
38#[derive(Debug, Clone, PartialEq, Eq)]
40pub struct Need {
41 pub memory: u64,
43 pub reason: String,
45}
46
47impl Sizing {
48 pub fn need(&self, profile: &ProfileSizing) -> Need {
51 let peaks = &profile.peaks;
52 let memory = match profile.advice {
53 _ if let Some(hold) = profile.hold => hold.need,
54 Advice::Keep { memory }
55 | Advice::Lower { memory, .. }
56 | Advice::Raise { memory, .. }
57 | Advice::SliceTooSmall { memory } => memory,
58 Advice::TooFew { .. } => need(peaks.memory.max, self.margin),
59 };
60 Need {
61 memory,
62 reason: self.need_reason(peaks),
63 }
64 }
65
66 pub(crate) fn need_reason(&self, peaks: &Peaks) -> String {
69 if peaks.memory.max * u64::from(self.margin) / 100 >= PAGE_CACHE_ROOM {
70 format!("its largest peak + {}%", self.margin)
71 } else {
72 format!(
73 "its largest peak + {} for page cache",
74 ByteSize(PAGE_CACHE_ROOM)
75 )
76 }
77 }
78
79 pub fn method_line(&self) -> String {
83 format!(
84 "peaks are sampled every {} s, so a shorter spike goes unseen: advice sizes for the largest + {}%, at \
85 least {} for page cache, rounded up to {}, once a profile ran {} jobs",
86 self.sample_interval,
87 self.margin,
88 ByteSize(PAGE_CACHE_ROOM),
89 ByteSize(GRANULE),
90 self.min_jobs
91 )
92 }
93
94 pub fn method_parts(&self) -> Vec<String> {
97 vec![
98 format!("peaks sampled every {} s", self.sample_interval),
99 format!(
100 "+{}%, at least {}, in {} steps",
101 self.margin,
102 ByteSize(PAGE_CACHE_ROOM),
103 ByteSize(GRANULE)
104 ),
105 format!("advice after {} jobs", self.min_jobs),
106 ]
107 }
108
109 pub fn covered(&self) -> u64 {
111 self.until.saturating_sub(self.since)
112 }
113
114 pub fn doctor_check(&self) -> Check {
117 let changes: Vec<_> = self
118 .profiles
119 .iter()
120 .filter(|profile| {
121 profile.advice.change().is_some()
122 || matches!(profile.advice, Advice::SliceTooSmall { .. })
123 })
124 .collect();
125 let raise = changes
126 .iter()
127 .any(|profile| profile.advice.tone() == Tone::Bad);
128 let detail = if changes.is_empty() {
129 let too_few = self
130 .profiles
131 .iter()
132 .any(|profile| matches!(profile.advice, Advice::TooFew { .. }));
133 if too_few {
134 format!(
135 "some profiles ran fewer than {} jobs; runnerctl sizing shows their peaks",
136 self.min_jobs
137 )
138 } else {
139 format!(
140 "every profile's memory fits its jobs' largest peak + {}%, at least {} for page cache",
141 self.margin,
142 ByteSize(PAGE_CACHE_ROOM)
143 )
144 }
145 } else {
146 changes
147 .iter()
148 .map(|profile| format!("{}: {}", profile.name, self.advice_line(profile)))
149 .collect::<Vec<_>>()
150 .join("; ")
151 };
152 Check {
153 name: "sizing".to_owned(),
154 status: if raise {
155 CheckStatus::Warn
156 } else {
157 CheckStatus::Info
158 },
159 detail,
160 fix: (!changes.is_empty()).then(|| {
161 changes
162 .iter()
163 .map(|profile| match profile.advice {
164 Advice::SliceTooSmall { .. } => match profile.held().map(|hold| hold.by) {
165 Some(MemoryBound::Ceiling { .. }) => format!(
166 "raise [host] memory_ceiling if the host can grow further, then: runnerctl \
167 sizing apply {}",
168 profile.name
169 ),
170 Some(MemoryBound::MaxMemory | MemoryBound::MinMemory) => format!(
171 "raise max_memory in [profiles.{0}], then: runnerctl sizing apply {0}",
172 profile.name
173 ),
174 None => format!(
175 "raise MemoryMax in the slice's unit file, sudo systemctl daemon-reload, then: \
176 runnerctl sizing apply {}",
177 profile.name
178 ),
179 },
180 _ => format!("runnerctl sizing apply {}", profile.name),
181 })
182 .collect::<Vec<_>>()
183 .join("\n")
184 }),
185 }
186 }
187}
188
189impl Advice {
190 pub fn tone(&self) -> Tone {
193 match self {
194 Advice::Raise { .. } | Advice::SliceTooSmall { .. } => Tone::Bad,
195 Advice::Lower { .. } => Tone::Warn,
196 Advice::Keep { .. } => Tone::Good,
197 Advice::TooFew { .. } => Tone::Quiet,
198 }
199 }
200
201 pub fn word(&self) -> String {
203 match *self {
204 Advice::TooFew { .. } => "too few".to_owned(),
205 Advice::Keep { .. } => "keep".to_owned(),
206 Advice::SliceTooSmall { .. } => "grow slice".to_owned(),
207 Advice::Lower { memory, slots } | Advice::Raise { memory, slots } => {
208 format!("{} × {slots}", ByteSize(memory))
209 }
210 }
211 }
212}
213
214impl ProfileSizing {
215 pub fn held(&self) -> Option<MemoryHold> {
218 self.hold.filter(|hold| {
219 matches!(self.advice, Advice::SliceTooSmall { .. })
220 && hold.by != MemoryBound::MinMemory
221 && self.memory >= hold.bound
222 })
223 }
224
225 pub fn figures(&self) -> Vec<LabelledFigure> {
228 let starts = &self.starts;
229 let warm = if starts.warm > 0 {
230 LabelledFigure::new("Warm starts", starts.warm.to_string())
231 .of(format!("of {}", starts.total()))
232 } else {
233 LabelledFigure::new("Warm starts", "–")
234 };
235 let cpu = match self.peaks.cpu_p99() {
236 Some(p99) => {
237 LabelledFigure::new("CPU p99", cores_used(p99)).of(format!("of {}", self.cpus))
238 }
239 None => LabelledFigure::new("CPU p99", "–"),
240 };
241 vec![
242 LabelledFigure::new(JOBS_HEADING, starts.total().to_string()),
243 LabelledFigure::new("Slots busy", format!("{}%", self.busy)),
244 LabelledFigure::new(
245 "Waited p50 / p95",
246 format!("{} / {}", duration(self.wait.p50), duration(self.wait.p95)),
247 ),
248 warm,
249 cpu,
250 ]
251 }
252
253 pub fn advice_word(&self) -> String {
256 match self.held().map(|hold| hold.by) {
257 Some(MemoryBound::Ceiling { .. }) => "at ceiling".to_owned(),
258 Some(MemoryBound::MaxMemory | MemoryBound::MinMemory) => "at max_memory".to_owned(),
259 None => self.advice.word(),
260 }
261 }
262}
263
264impl JobSizing {
265 pub fn needs(&self) -> Toned {
268 let (text, tone) = match self.advice {
269 Advice::TooFew { .. } => (
270 format!("max {}", ByteSize(self.peaks.memory.max)),
271 Tone::Quiet,
272 ),
273 Advice::Raise { memory, .. } | Advice::SliceTooSmall { memory } => {
274 (format!("{} > cap", ByteSize(memory)), Tone::Bad)
275 }
276 Advice::Keep { memory } | Advice::Lower { memory, .. } => {
277 (ByteSize(memory).to_string(), Tone::Neutral)
278 }
279 };
280 Toned { text, tone }
281 }
282}
283
284impl Peaks {
285 pub fn cpu_p99(&self) -> Option<u64> {
287 (self.cpu_runs > 0).then_some(self.cpu.p99)
288 }
289
290 pub fn cpu_runs_line(&self, cpus: u32) -> String {
293 match self.cpu_runs {
294 0 => format!("no run at {cpus} CPUs yet"),
295 runs => format!("of the {runs} at {cpus} CPUs"),
296 }
297 }
298}
299
300impl Starts {
301 pub fn total(&self) -> u32 {
303 self.warm + self.cold
304 }
305}
306
307#[derive(Debug, Clone, Copy, PartialEq, Eq)]
309pub enum ProfilePlan<'plan> {
310 Next {
312 change: &'plan SizingChange,
314 blocked: bool,
316 },
317 Held {
319 hold: &'plan SizingHold,
321 },
322 LowerHeld {
324 hold: &'plan SizingHold,
326 },
327 Nothing,
329}
330
331impl AutoPlan {
332 pub fn refusals(&self, profile: &str) -> Vec<String> {
336 let lines = |refused: &[SizingRefused], what: &str| -> Vec<String> {
337 refused
338 .iter()
339 .filter(|refused| refused.profile == profile)
340 .map(|refused| format!("No {what} raise: {}", refused.reason))
341 .collect()
342 };
343 let mut refusals = lines(&self.refused, "CPU");
344 refusals.extend(lines(&self.refused_slots, "slot"));
345 refusals
346 }
347
348 pub fn blocked_line(&self) -> Option<String> {
351 self.blocked
352 .as_ref()
353 .map(|blocked| format!("No look changes anything now: {blocked}"))
354 }
355
356 pub fn profile_plan(&self, profile: &str, now: u64) -> ProfilePlan<'_> {
358 if let Some(change) = self.next.iter().find(|change| change.profile == profile) {
359 return ProfilePlan::Next {
360 change,
361 blocked: self.blocked.is_some(),
362 };
363 }
364 match self.held.iter().find(|hold| hold.profile == profile) {
365 Some(hold) if now < hold.until => ProfilePlan::Held { hold },
366 Some(hold) if now < hold.lowered_after() => ProfilePlan::LowerHeld { hold },
367 _ => ProfilePlan::Nothing,
368 }
369 }
370}
371
372impl ProfilePlan<'_> {
373 pub fn until(&self) -> Option<u64> {
375 match self {
376 ProfilePlan::Held { hold } => Some(hold.until),
377 ProfilePlan::LowerHeld { hold } => Some(hold.lowered_after()),
378 ProfilePlan::Next { .. } | ProfilePlan::Nothing => None,
379 }
380 }
381
382 pub fn headline(&self, plan: &AutoPlan) -> String {
385 match self {
386 ProfilePlan::Next {
387 change,
388 blocked: true,
389 } => format!("Once that is fixed: {}", change.line()),
390 ProfilePlan::Next { change, .. } => format!(
391 "Next look, within {}: {}",
392 super::period(plan.every),
393 change.line()
394 ),
395 ProfilePlan::Held { .. } => "No change".to_owned(),
396 ProfilePlan::LowerHeld { .. } => "No lowering".to_owned(),
397 ProfilePlan::Nothing => "Nothing to change".to_owned(),
398 }
399 }
400
401 pub fn reason(&self, sizing: &Sizing, profile: &ProfileSizing) -> String {
404 match self {
405 ProfilePlan::Next { change, .. } => change.reason.clone(),
406 ProfilePlan::Held { .. } => "the interval holds it until then".to_owned(),
407 ProfilePlan::LowerHeld { .. } => "a raise may come before then".to_owned(),
408 ProfilePlan::Nothing => sizing.advice_line(profile),
409 }
410 }
411
412 pub fn changed(&self) -> Option<u64> {
414 match self {
415 ProfilePlan::Held { hold } | ProfilePlan::LowerHeld { hold } => Some(hold.changed),
416 ProfilePlan::Next { .. } | ProfilePlan::Nothing => None,
417 }
418 }
419}
420
421#[cfg(test)]
422mod tests;