scx_rlfifo: replace 1ms sleep with sched_yield()

Small improvement to make the scheduler a bit more responsive, without
introducing too much complexity or too much CPU overhead.

This can be achieved by replacing a sleep of 1ms with a sched_yield()
every time that the scheduler has finished to dispatch all the queued
tasks.

This also makes the code a bit smaller and easier to read.

Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
This commit is contained in:
Andrea Righi 2024-03-05 18:42:24 +01:00
parent db17905930
commit fe19754132

View File

@ -15,7 +15,7 @@ use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use std::time::SystemTime;
use anyhow::Result;
@ -46,17 +46,14 @@ impl<'a> Scheduler<'a> {
// case we can simply ignore the task.
if task.cpu >= 0 {
let _ = self.bpf.dispatch_task(&DispatchedTask::new(&task));
// Give the task a chance to run and prevent overflowing the dispatch queue.
std::thread::yield_now();
}
// Give the task a chance to run and prevent overflowing the dispatch queue.
std::thread::yield_now();
}
Ok(None) => {
// Notify the BPF component that all tasks have been scheduled and dispatched.
self.bpf.update_tasks(Some(0), Some(0));
// All queued tasks have been dipatched, add a short sleep to reduce
// scheduler's CPU consuption.
std::thread::sleep(Duration::from_millis(1));
break;
}
Err(_) => {
@ -64,6 +61,8 @@ impl<'a> Scheduler<'a> {
}
}
}
// All queued tasks have been dipatched, yield to reduce scheduler's CPU consumption.
std::thread::yield_now();
}
fn print_stats(&mut self) {