summaryrefslogtreecommitdiff
path: root/src/energy.rs
blob: b9cfcedac5b38995dbaba5df18747b10ca1508ad (plain)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
mod budget;
pub mod estimator;
pub mod rapl;
mod trackers;

use crate::energy::estimator::Estimator;
use std::collections::{BTreeSet, HashMap};
use std::ops::RangeInclusive;
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicI64, AtomicU64};
use std::sync::{mpsc, Arc, RwLock};
use std::thread;
use std::time::{Duration, Instant};

use crate::freq::FrequencyKHZ;
use crate::socket;
use crate::Pid;

pub use budget::BudgetPolicy;
pub use trackers::{KernelDriver, PerfEstimator};

const IDLE_CONSUMPTION_W: f64 = 7.;
const UPDATE_INTERVAL: Duration = Duration::from_millis(10);

pub enum Request {
    NewTask(Pid, Arc<TaskInfo>),
    RemoveTask(Pid),
}
pub struct TaskInfo {
    pub cpu: AtomicI32,
    pub budget: AtomicU64,
    pub running_on_e_core: AtomicBool,
    pub last_scheduled: AtomicI64,
}

impl TaskInfo {
    pub fn read_cpu(&self) -> i32 {
        self.cpu.load(std::sync::atomic::Ordering::Relaxed)
    }
    pub fn read_budget(&self) -> u64 {
        self.budget.load(std::sync::atomic::Ordering::Relaxed)
    }
    pub fn is_running_on_e_core(&self) -> bool {
        self.running_on_e_core
            .load(std::sync::atomic::Ordering::Relaxed)
    }
    pub fn read_time_since_last_schedule(&self) -> Option<Duration> {
        let old_time = self
            .last_scheduled
            .load(std::sync::atomic::Ordering::Relaxed);
        if old_time == -1 {
            None
        } else {
            let now = chrono::Utc::now().timestamp_micros();
            Some(Duration::from_micros((now - old_time) as u64))
        }
    }
    pub fn read_time_since_last_schedule_raw(&self) -> i64 {
        self.last_scheduled
            .load(std::sync::atomic::Ordering::Relaxed)
    }
    pub fn set_cpu(&self, cpu: i32) {
        self.cpu.store(cpu, std::sync::atomic::Ordering::Relaxed);
    }
    pub fn set_last_scheduled_to_now(&self) {
        self.last_scheduled.store(
            chrono::Utc::now().timestamp_micros(),
            std::sync::atomic::Ordering::Relaxed,
        );
    }
    pub fn set_last_scheduled_raw(&self, last_scheduled: i64) {
        self.last_scheduled
            .store(last_scheduled, std::sync::atomic::Ordering::Relaxed);
    }
    pub fn set_budget(&self, budget: u64) {
        self.budget
            .store(budget, std::sync::atomic::Ordering::Relaxed);
    }
    pub fn set_running_on_e_core(&self, running_on_e_core: bool) {
        self.running_on_e_core
            .store(running_on_e_core, std::sync::atomic::Ordering::Relaxed);
    }
}

impl Default for TaskInfo {
    fn default() -> Self {
        Self {
            cpu: Default::default(),
            budget: AtomicU64::new(u64::MAX),
            running_on_e_core: Default::default(),
            last_scheduled: AtomicI64::new(-1),
        }
    }
}

#[derive(Clone)]
pub struct ProcessInfo {
    pub energy: f64,
    pub energy_delta: f64,
    pub tree_energy: f64,
    pub last_update: std::time::Instant,
    pub parent: Pid,
    pub task_info: Arc<TaskInfo>,
}

pub struct EnergyService {
    estimator: Box<dyn Estimator>,
    budget_policy: Option<Box<dyn BudgetPolicy>>,
    // contains the same data as the keys of process_info but having this reduces contention and
    // avoids unnecessary clone
    active_processes: BTreeSet<Pid>,
    process_info: Arc<RwLock<HashMap<Pid, ProcessInfo>>>,
    request_receiver: mpsc::Receiver<Request>,
    update_interval: Duration,
    shared_cpu_frequency_ranges: Arc<RwLock<Vec<RangeInclusive<FrequencyKHZ>>>>,
    shared_policy_frequency_ranges: Arc<RwLock<Vec<RangeInclusive<FrequencyKHZ>>>>,
    shared_cpu_current_frequencies: Arc<RwLock<Vec<FrequencyKHZ>>>,
    rapl_offset: f64,
    last_energy_diff: f64,
    last_time_between_measurements: Duration,
    old_rapl: f64,
    system_energy: f64,
    bias: f64,
    graveyard: Vec<i32>,
    last_measurement: Instant,
}

impl EnergyService {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        estimator: Box<dyn Estimator>,
        budget_policy: Box<dyn BudgetPolicy>,
        process_info: Arc<RwLock<HashMap<Pid, ProcessInfo>>>,
        request_receiver: mpsc::Receiver<Request>,
        update_interval: Duration,
        shared_cpu_frequency_ranges: Arc<RwLock<Vec<RangeInclusive<FrequencyKHZ>>>>,
        shared_policy_frequency_ranges: Arc<RwLock<Vec<RangeInclusive<FrequencyKHZ>>>>,
        shared_cpu_current_frequencies: Arc<RwLock<Vec<FrequencyKHZ>>>,
    ) -> Self {
        Self {
            estimator,
            budget_policy: Some(budget_policy),
            active_processes: BTreeSet::new(),
            process_info,
            request_receiver,
            update_interval,
            shared_cpu_frequency_ranges,
            shared_policy_frequency_ranges,
            shared_cpu_current_frequencies,
            rapl_offset: rapl::read_package_energy().unwrap(),
            last_energy_diff: 0f64,
            last_time_between_measurements: Duration::new(0, 0),
            old_rapl: 0.,
            system_energy: 0.,
            bias: 1.,
            graveyard: Vec::with_capacity(100),
            last_measurement: Instant::now(),
        }
    }

    pub fn run(mut self) {
        thread::spawn(move || {
            let mut i = 0;
            loop {
                i += 1;
                // Process any incoming requests
                self.handle_requests();

                if i % 30 == 0 {
                    // Update energy measurements
                    self.update_measurements();

                    self.clear_graveyeard();

                    // Calculate and update budgets
                    self.update_budgets();
                }

                // Sleep for update interval
                thread::sleep(self.update_interval);
            }
        });
    }

    fn handle_requests(&mut self) {
        while let Ok(request) = self.request_receiver.try_recv() {
            self.handle_request(request);
        }
    }

    fn handle_request(&mut self, request: Request) {
        match request {
            Request::NewTask(pid, task_info) => {
                if let Some(info) = self.process_info.write().unwrap().get_mut(&pid) {
                    let old_budget = task_info.read_budget();
                    let old_time = task_info.read_time_since_last_schedule_raw();
                    info.task_info = task_info.clone();
                    info.task_info.set_budget(old_budget);
                    info.task_info.set_last_scheduled_raw(old_time);
                    return;
                }
                if self
                    .estimator
                    .start_trace(
                        pid as u64,
                        task_info.read_cpu(),
                        task_info.is_running_on_e_core(),
                    )
                    .is_err()
                {
                    return;
                }
                let parent = (|| {
                    let process = procfs::process::Process::new(pid)?;
                    process.stat().map(|stat| stat.ppid)
                })()
                .unwrap_or_default();
                // We don't care whether the task has been scheduled before the counters are set up
                task_info.set_last_scheduled_raw(-1);
                self.process_info.write().unwrap().insert(
                    pid,
                    ProcessInfo {
                        energy: 0.,
                        energy_delta: 0.,
                        tree_energy: 0.,
                        last_update: std::time::Instant::now(),
                        parent,
                        task_info: task_info.clone(),
                    },
                );
                self.active_processes.insert(pid);
                if !self.process_info.read().unwrap().contains_key(&parent) && parent != 0 {
                    self.handle_request(Request::NewTask(parent, task_info));
                }
            }
            Request::RemoveTask(pid) => {
                if procfs::process::Process::new(pid).is_ok() {
                    return;
                }
                self.graveyard.push(pid);
            }
        }
    }

    fn update_measurements(&mut self) {
        let old_energy = self
            .process_info
            .read()
            .unwrap()
            .get(&1)
            .map(|info| info.tree_energy)
            .unwrap_or(0.);
        for pid in &self.active_processes {
            let mut process_info = self.process_info.write().unwrap();
            if let Some(info) = process_info.get_mut(pid) {
                if info
                    .task_info
                    .read_time_since_last_schedule()
                    .unwrap_or(UPDATE_INTERVAL)
                    >= UPDATE_INTERVAL
                {
                    continue;
                }
                if let Some(energy) = self.estimator.read_consumption(*pid as u64) {
                    info.energy_delta = energy * self.bias;
                    info.energy += energy * self.bias;
                    info.tree_energy += energy * self.bias;
                    self.estimator.update_information(
                        *pid as u64,
                        info.task_info.read_cpu(),
                        info.task_info.is_running_on_e_core(),
                    );
                    let mut parent = info.parent;
                    while let Some(info) = process_info.get_mut(&parent) {
                        info.tree_energy += energy * self.bias;
                        info.last_update = std::time::Instant::now();
                        parent = info.parent;
                    }
                }
            }
        }
        let elapsed = self.last_measurement.elapsed();
        self.last_time_between_measurements = elapsed;
        self.last_measurement = Instant::now();
        let rapl = rapl::read_package_energy().unwrap() - self.rapl_offset;
        let rapl_diff = rapl - self.old_rapl;
        self.last_energy_diff = rapl_diff;
        self.old_rapl = rapl;
        let power_comsumption_watt = rapl_diff / elapsed.as_secs_f64();
        let idle_consumption = elapsed.as_secs_f64() * IDLE_CONSUMPTION_W;
        if let Some(init) = self.process_info.write().unwrap().get_mut(&1) {
            let est_diff = init.tree_energy - old_energy + idle_consumption;
            // let offset_bias = (rapl / (init.tree_energy + idle_consumption)).clamp(0.1, 2.);
            let current_bias = if init.tree_energy - old_energy > idle_consumption * 0.5 {
                (rapl_diff / est_diff).clamp(0.1, 2.)
            } else {
                1.
            };
            // let current_bias = (offset_bias + diff_bias) * 0.5;
            let alpha: f64 = 10. * elapsed.as_secs_f64().recip();
            self.bias = (self.bias * (alpha.recip() * current_bias + ((alpha - 1.) / alpha)))
                .clamp(0.1, 5.);
            self.system_energy += est_diff;
            println!(
                "Energy estimation: {:.1} rapl: {:.1}, est diff: {:.1} rapl diff: {:.1}, bias: {:.1}, power consumption: {:.1}",
                self.system_energy, rapl, est_diff, rapl_diff, self.bias, power_comsumption_watt,
            );
        }
    }

    fn update_budgets(&mut self) {
        // We can't call self.budget_policy.calculate_budgets(self) directly because the first self borrows immutable and the self second borrows mutable
        let policy = self.budget_policy.take().unwrap();
        let budgets = policy.calculate_budgets(self);
        self.budget_policy = Some(policy);

        // Update the shared budgets map
        for (pid, budget) in budgets {
            if let Some(entry) = self.process_info.write().unwrap().get(&pid) {
                entry.task_info.set_budget(budget);
            }
        }
    }

    fn clear_graveyeard(&mut self) {
        for pid in self.graveyard.drain(..) {
            self.estimator.stop_trace(pid as u64);
            self.active_processes.remove(&pid);
            self.process_info.write().unwrap().remove(&pid);
        }
    }

    // Accessor methods for BudgetPolicy
    pub fn active_processes(&self) -> &BTreeSet<Pid> {
        &self.active_processes
    }

    fn package_energy(&mut self) -> f64 {
        rapl::read_package_energy().unwrap()
    }

    pub fn process_energy(&self, pid: Pid) -> Option<f64> {
        self.process_info
            .read()
            .unwrap()
            .get(&pid)
            .map(|info| info.energy)
    }

    pub fn all_process_energy_deltas(&self) -> HashMap<Pid, f64> {
        self.process_info
            .read()
            .unwrap()
            .iter()
            .map(|(&key, info)| (key, info.energy_delta))
            .collect()
    }
}

pub fn start_energy_service(
    use_mocking: bool,
    shared_cpu_frequency_ranges: Arc<RwLock<Vec<RangeInclusive<FrequencyKHZ>>>>,
    shared_policy_frequency_ranges: Arc<RwLock<Vec<RangeInclusive<FrequencyKHZ>>>>,
    shared_cpu_current_frequencies: Arc<RwLock<Vec<FrequencyKHZ>>>,
) -> std::io::Result<mpsc::SyncSender<Request>> {
    // Potentially convert back to bounded channel
    let (request_sender, request_receiver) = mpsc::sync_channel(10000);

    // Create the appropriate estimator based on configuration
    let estimator: Box<dyn Estimator> = if use_mocking {
        Box::new(PerfEstimator::new(shared_cpu_current_frequencies.clone()))
    } else {
        Box::new(KernelDriver::default())
    };

    let process_info = Arc::new(RwLock::new(HashMap::new()));

    let power_cap = socket::start_logging_socket_service("/tmp/pm-sched", process_info.clone())?;

    // Create budget policy
    let budget_policy = Box::new(budget::SimpleCappingPolicy::new(power_cap));

    // shouldn't be a problem because we are privileged
    // if PackageEnergy::check_paranoid().unwrap_or(3) > 0 {}

    // Create and start the energy service
    let service = EnergyService::new(
        estimator,
        budget_policy,
        process_info.clone(),
        request_receiver,
        UPDATE_INTERVAL,
        shared_cpu_frequency_ranges,
        shared_policy_frequency_ranges,
        shared_cpu_current_frequencies,
    );

    service.run();

    Ok(request_sender)
}