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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
mod bpf_skel;
pub use bpf_skel::*;
pub mod bpf_intf;
mod e_core_selector;
mod mock;
mod task_state;
#[rustfmt::skip]
mod bpf;
use bpf::*;
use clap::{Arg, ArgAction, Command};
use dashmap::DashMap;
use e_core_selector::{ECoreSelector, RoundRobinSelector};
use mock::perf::PerfEstimator;
use scx_utils::{Topology, UserExitInfo};
use libbpf_rs::OpenObject;
use mock::{KernelDriver, KernelModule, MockModule};
use task_state::TaskState;
use std::any::Any;
use std::collections::{BTreeSet, HashMap, HashSet, VecDeque};
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::mem::MaybeUninit;
use std::ops::Range;
use std::sync::mpsc::{Receiver, Sender, SyncSender};
use std::sync::{Arc, OnceLock};
use std::time::Duration;
use std::{process, thread};
use anyhow::Result;
type Pid = i32;
const SLICE_US: u64 = 50000;
struct Scheduler<'a> {
bpf: BpfScheduler<'a>,
task_queue: VecDeque<QueuedTask>,
no_budget_task_queue: VecDeque<QueuedTask>,
managed_tasks: HashMap<Pid, TaskState>,
maximum_budget: u64,
power_cap: u64,
own_pid: Pid,
p_cores: Range<i32>,
e_cores: Range<i32>,
topology: Topology,
e_core_selector: Box<dyn ECoreSelector>,
// reciever: Receiver<(Pid, Response)>,
sender: SyncSender<(Pid, Request)>,
hashmap: Arc<DashMap<Pid, (u64, i32)>>,
}
enum Request {
NewTask,
RemoveTask,
}
impl<'a> Scheduler<'a> {
fn init(
open_object: &'a mut MaybeUninit<OpenObject>,
use_mocking: bool,
power_cap: u64,
) -> Result<Self> {
let bpf = BpfScheduler::init(
open_object,
0, // exit_dump_len (buffer size of exit info, 0 = default)
false, // partial (false = include all tasks)
false, // debug (false = debug mode off)
)?;
dbg!("registering rust user space scheduler");
let map: DashMap<i32, (u64, i32)> = DashMap::with_capacity(100000);
let map = Arc::new(map);
let thread_map = map.clone();
let topology = Topology::new().unwrap();
let read_cores = |core_type: &str| {
let e_cores_file = File::open(format!("/sys/devices/cpu_{core_type}/cpus"))?;
let e_cores_reader = BufReader::new(e_cores_file);
let e_cores = if let Some(Ok(line)) = e_cores_reader.lines().next() {
let cores: Vec<&str> = line.split('-').collect();
cores[0].parse::<i32>().unwrap()..cores[1].parse::<i32>().unwrap()
} else {
panic!(
"Was not able to differentiate between core types. Does the system have e cores?"
)
};
Ok::<_, std::io::Error>(e_cores)
};
let e_cores = read_cores("atom").unwrap_or(0..4);
let p_cores = read_cores("core").unwrap_or(4..8);
let selector = Box::new(RoundRobinSelector::new(&e_cores));
let (request_send, request_recieve) = std::sync::mpsc::sync_channel(1000);
std::thread::spawn(move || {
let mut module: Box<dyn KernelModule> = if use_mocking {
Box::new(PerfEstimator::default())
// Box::new(MockModule::default())
} else {
Box::new(KernelDriver::default())
};
let mut tasks = BTreeSet::new();
loop {
for pid in tasks.iter() {
let energy = module.read_consumption(*pid as u64);
if let Some(mut entry) = thread_map.try_get_mut(pid).try_unwrap() {
entry.0 = energy;
}
}
if let Ok((pid, request)) = request_recieve.try_recv() {
match request {
Request::NewTask => {
let parent = (|| {
let process = procfs::process::Process::new(pid)?;
process.stat().map(|stat| stat.ppid)
})()
.unwrap_or_default();
module.start_trace(pid as u64);
tasks.insert(pid);
thread_map.insert(pid, (0, parent));
}
Request::RemoveTask => {
tasks.remove(&pid);
module.stop_trace(pid as u64)
}
}
}
std::thread::sleep(Duration::from_micros(5000));
}
});
Ok(Self {
bpf,
task_queue: VecDeque::new(),
no_budget_task_queue: VecDeque::new(),
managed_tasks: HashMap::new(),
maximum_budget: u64::MAX,
power_cap,
own_pid: process::id() as i32,
p_cores,
e_cores,
topology,
e_core_selector: selector,
sender: request_send,
// reciever: response_recieve,
hashmap: map,
})
}
fn consume_all_tasks(&mut self) {
// Consume all tasks that are ready to run.
//
// Each task contains the following details:
//
// pub struct QueuedTask {
// pub pid: i32, // pid that uniquely identifies a task
// pub cpu: i32, // CPU where the task is running
// pub sum_exec_runtime: u64, // Total cpu time
// pub weight: u64, // Task static priority
// pub nvcsw: u64, // Total amount of voluntary context switches
// pub slice: u64, // Remaining time slice budget
// pub vtime: u64, // Current task vruntime / deadline (set by the scheduler)
// }
//
// Although the FIFO scheduler doesn't use these fields, they can provide valuable data for
// implementing more sophisticated scheduling policies.
// let mut contended = Vec::with_capacity(100);
while let Ok(Some(task)) = self.bpf.dequeue_task() {
// The scheduler itself has to be scheduled regardless of its energy usage
if task.pid == self.own_pid {
self.task_queue.push_front(task);
self.dispatch_next_task();
continue;
}
match self.hashmap.try_get(&task.pid) {
dashmap::try_result::TryResult::Present(value) => {
let (energy, parent) = *value;
// dbg!(energy);
// TODO: fix
if energy == 0 {
// println!("budget zero");
self.no_budget_task_queue.push_back(task);
} else {
self.task_queue.push_back(task);
}
}
dashmap::try_result::TryResult::Absent => {
self.hashmap.insert(task.pid, (0, 0));
self.sender.try_send((task.pid, Request::NewTask)).unwrap();
self.task_queue.push_back(task);
}
dashmap::try_result::TryResult::Locked => {
println!("locked");
self.task_queue.push_back(task);
}
}
}
}
fn dispatch_next_task(&mut self) {
if let Some(task) = self.task_queue.pop_front() {
// Create a new task to be dispatched, derived from the received enqueued task.
//
// pub struct DispatchedTask {
// pub pid: i32, // pid that uniquely identifies a task
// pub cpu: i32, // target CPU selected by the scheduler
// pub flags: u64, // special dispatch flags
// pub slice_ns: u64, // time slice assigned to the task (0 = default)
// }
//
// The dispatched task's information are pre-populated from the QueuedTask and they can
// be modified before dispatching it via self.bpf.dispatch_task().
let mut dispatched_task = DispatchedTask::new(&task);
// Decide where the task needs to run (target CPU).
//
// A call to select_cpu() will return the most suitable idle CPU for the task,
// considering its previously used CPU.
let cpu = self.bpf.select_cpu(task.pid, task.cpu, 0);
if cpu >= 0 {
dispatched_task.cpu = cpu;
} else {
dispatched_task.flags |= RL_CPU_ANY as u64;
}
// Decide for how long the task needs to run (time slice); if not specified
// SCX_SLICE_DFL will be used by default.
dispatched_task.slice_ns = SLICE_US;
// Dispatch the task on the target CPU.
self.bpf.dispatch_task(&dispatched_task).unwrap();
// Notify the BPF component of the number of pending tasks and immediately give a
// chance to run to the dispatched task.
self.bpf.notify_complete(
self.task_queue.len() as u64 + self.no_budget_task_queue.len() as u64,
);
} else if let Some(task) = self.no_budget_task_queue.pop_front() {
//TODO: do something with tasks that have no budget left
let mut dispatched_task = DispatchedTask::new(&task);
let cpu = self.e_core_selector.next_core();
if cpu >= 0 {
dispatched_task.cpu = cpu;
} else {
dispatched_task.flags |= RL_CPU_ANY as u64;
}
if task.pid == self.own_pid {
dispatched_task.slice_ns = SLICE_US * 1000;
}
dispatched_task.slice_ns = SLICE_US;
self.bpf.dispatch_task(&dispatched_task).unwrap();
self.bpf.notify_complete(
self.task_queue.len() as u64 + self.no_budget_task_queue.len() as u64,
);
}
}
fn dispatch_tasks(&mut self) {
//TODO: we should probably not do this every time, but instead wait a predefined amount of time between invocations
self.reset_budgets_and_garbage_collect();
loop {
// Consume all tasks before dispatching any.
self.consume_all_tasks();
// Dispatch one task from the queue.
self.dispatch_next_task();
// If no task is ready to run (or in case of error), stop dispatching tasks and notify
// the BPF component that all tasks have been scheduled / dispatched, with no remaining
// pending tasks.
//TODO: both queues?
if self.task_queue.is_empty() && self.no_budget_task_queue.is_empty() {
self.bpf.notify_complete(0);
break;
}
}
}
fn run(&mut self) -> Result<UserExitInfo> {
while !self.bpf.exited() {
self.dispatch_tasks();
}
self.bpf.shutdown_and_report()
}
fn get_new_budget(&self) -> u64 {
//TODO
u64::MAX
}
fn reset_budgets_and_garbage_collect(&mut self) {
let old_budget = self.maximum_budget;
self.maximum_budget = self.get_new_budget();
self.managed_tasks.retain(|key, value| {
let was_scheduled = value.budget == old_budget;
if was_scheduled {
value.budget = self.maximum_budget;
} else {
self.sender
.try_send(({ *key }, Request::RemoveTask))
.unwrap();
}
was_scheduled
});
}
}
fn main() -> Result<()> {
let matches = Command::new("Energy User Space Scheduler")
.arg(
Arg::new("mock")
.short('m')
.long("mock")
.help("Use this flag if to activate kernel module mocking")
.action(ArgAction::SetTrue)
.required(false),
)
.arg(
Arg::new("power_cap")
.long("energy_cap")
.help("Set a power cap for the processor")
.required(false)
.value_name("power in watts"),
)
.get_matches();
let power_cap = *matches.get_one::<u64>("power_cap").unwrap_or(&u64::MAX);
let use_mocking = matches.get_flag("mock");
// Initialize and load the FIFO scheduler.
let mut open_object = MaybeUninit::uninit();
loop {
let mut sched = Scheduler::init(&mut open_object, use_mocking, power_cap)?;
if !sched.run()?.should_restart() {
break;
}
}
Ok(())
}
|