cardamon 0.0.9

Cardamon is a tool to help development teams measure the power consumption and carbon emissions of their software.
Documentation
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use anyhow::Context;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::{
    collections::HashSet,
    fs::{self, File},
    io::{Read, Write},
};

#[cfg(not(windows))]
static EXAMPLE_CONFIG: &str = include_str!("templates/cardamon.unix.toml");
#[cfg(windows)]
static EXAMPLE_CONFIG: &str = include_str!("templates/cardamon.win.toml");

#[cfg(not(windows))]
static LINE_ENDING: &str = "\n";
#[cfg(windows)]
static LINE_ENDING: &str = "\r\n";

// ******** ******** ********
// **    CONFIGURATION     **
// ******** ******** ********
#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
    pub cpu: Cpu,
    #[serde(rename(serialize = "process", deserialize = "process"))]
    pub processes: Vec<Process>,
    #[serde(rename(serialize = "scenario", deserialize = "scenario"))]
    pub scenarios: Vec<Scenario>,
    #[serde(rename(serialize = "observation", deserialize = "observation"))]
    pub observations: Vec<Observation>,
}
impl Config {
    pub fn write_example_to_file(
        cpu_name: &str,
        cpu_avg_power: f64,
        path: &std::path::Path,
    ) -> anyhow::Result<File> {
        // remove the line containing tdp
        let mut lines = EXAMPLE_CONFIG.lines().map(|s| s.to_string()).collect_vec();

        // add a line at the top of the file containing the new tdp
        let mut new_conf_lines = vec![
            "[cpu]".to_string(),
            format!("name = \"{}\"", cpu_name),
            format!("avg_power = {}", cpu_avg_power),
            "".to_string(),
        ];
        new_conf_lines.append(&mut lines);
        let conf_str = new_conf_lines.join(LINE_ENDING);

        // write to file
        let mut file = File::create_new(path)?;
        File::write_all(&mut file, conf_str.as_bytes())?;
        Ok(file)
    }

    pub fn try_from_path(path: &std::path::Path) -> anyhow::Result<Config> {
        let mut config_str = String::new();
        fs::File::open(path)?.read_to_string(&mut config_str)?;
        Config::try_from_str(&config_str)
    }

    pub fn try_from_str(conf_str: &str) -> anyhow::Result<Config> {
        toml::from_str::<Config>(conf_str).map_err(|e| anyhow::anyhow!("TOML parsing error: {}", e))
    }

    fn find_observation(&self, obs_name: &str) -> Option<&Observation> {
        self.observations.iter().find(|obs| match obs {
            Observation::LiveMonitor { name, processes: _ } => name == obs_name,
            Observation::ScenarioRunner { name, scenarios: _ } => name == obs_name,
        })
    }

    pub fn find_scenario(&self, scenario_name: &str) -> anyhow::Result<&Scenario> {
        self.scenarios
            .iter()
            .find(|scenario| scenario.name == scenario_name)
            .context(format!(
                "Unable to find scenario with name {}",
                scenario_name
            ))
    }

    pub fn find_scenarios(&self, scenario_names: &[&String]) -> anyhow::Result<Vec<&Scenario>> {
        let mut scenarios = vec![];
        for scenario_name in scenario_names {
            let scenario = self.find_scenario(&scenario_name)?;
            scenarios.push(scenario);
        }
        Ok(scenarios)
    }

    /// Finds a process in the config with the given name.
    ///
    /// # Arguments
    /// * proc_name - the name of the process to find
    ///
    /// # Returns
    /// Some process if it can be found, None otherwise
    fn find_process(&self, proc_name: &str) -> anyhow::Result<&Process> {
        self.processes
            .iter()
            .find(|proc| proc.name == proc_name)
            .context(format!("Unable to find process with name {}", proc_name))
    }

    fn find_processes(&self, proc_names: &[&String]) -> anyhow::Result<Vec<&Process>> {
        let mut processes = vec![];
        for proc_name in proc_names {
            let proc = self.find_process(&proc_name)?;
            processes.push(proc);
        }
        Ok(processes)
    }

    pub fn create_execution_plan(
        &self,
        obs_name: &str,
        external_only: bool,
    ) -> anyhow::Result<ExecutionPlan> {
        let obs = self.find_observation(obs_name).context(format!(
            "Couldn't find an observation with name {}",
            obs_name
        ))?;

        let mut processes_to_execute = vec![];

        let exec_plan = match &obs {
            Observation::ScenarioRunner { name: _, scenarios } => {
                let scenario_names = scenarios.iter().collect_vec();
                let scenarios = self.find_scenarios(&scenario_names)?;

                // find the intersection of processes between all the scenarios
                if !external_only {
                    let mut proc_set: HashSet<String> = HashSet::new();
                    for scenario_name in scenario_names {
                        let scenario = self.find_scenario(&scenario_name).context(format!(
                            "Unable to find scenario with name {}",
                            scenario_name
                        ))?;
                        for proc_name in &scenario.processes {
                            proc_set.insert(proc_name.clone());
                        }
                    }

                    let proc_names = proc_set.iter().collect_vec();
                    processes_to_execute = self.find_processes(&proc_names)?;
                }
                ExecutionPlan::new(processes_to_execute, ExecutionMode::Observation(scenarios))
            }

            Observation::LiveMonitor { name: _, processes } => {
                if !external_only {
                    let proc_names = processes.iter().collect_vec();
                    processes_to_execute = self.find_processes(&proc_names)?;
                }
                ExecutionPlan::new(processes_to_execute, ExecutionMode::Live)
            }
        };

        Ok(exec_plan)
    }
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct Cpu {
    pub name: String,
    pub avg_power: f32,
}

#[derive(Debug, Deserialize, PartialEq, Clone, Copy, Serialize)]
#[serde(tag = "to", rename_all = "lowercase")]
pub enum Redirect {
    Null,
    Parent,
    File,
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum ProcessType {
    BareMetal,
    Docker { containers: Vec<String> },
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct Process {
    pub name: String,
    pub up: String,
    pub down: Option<String>,
    pub redirect: Option<Redirect>,
    #[serde(rename = "process")]
    pub process_type: ProcessType,
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct Scenario {
    pub name: String,
    pub desc: String,
    pub command: String,
    pub iterations: i32,
    pub processes: Vec<String>,
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(untagged)]
pub enum Observation {
    LiveMonitor {
        name: String,
        processes: Vec<String>,
    },
    ScenarioRunner {
        name: String,
        scenarios: Vec<String>,
    },
}

// #[derive(Debug, Deserialize, Serialize)]
// pub struct Observation {
//     pub name: String,
//     #[serde(rename = "observe")]
//     pub observation_mode: ObservationMode,
// }

// ******** ******** ********
// **    EXECUTION PLAN    **
// ******** ******** ********
#[derive(Debug, Clone)]
pub enum ProcessToObserve {
    ExternalPid(u32),
    ExternalContainers(Vec<String>),

    /// ManagedPid represents a baremetal processes started by Cardamon
    ManagedPid {
        process_name: String,
        pid: u32,
        down: Option<String>,
    },

    /// ManagedContainers represents a docker processes started by Cardamon
    ManagedContainers {
        process_name: String,
        container_names: Vec<String>,
        down: Option<String>,
    },
}

#[derive(Debug)]
pub enum ExecutionMode<'a> {
    Live,
    Observation(Vec<&'a Scenario>),
}

#[derive(Debug)]
pub struct ExecutionPlan<'a> {
    pub external_processes_to_observe: Option<Vec<ProcessToObserve>>,
    pub processes_to_execute: Vec<&'a Process>,
    pub execution_mode: ExecutionMode<'a>,
}
impl<'a> ExecutionPlan<'a> {
    pub fn new(processes_to_execute: Vec<&'a Process>, execution_mode: ExecutionMode<'a>) -> Self {
        ExecutionPlan {
            external_processes_to_observe: None,
            processes_to_execute,
            execution_mode,
        }
    }

    /// Adds a process that has not been started by Cardamon to this execution plan for observation.
    ///
    /// # Arguments
    /// * process_to_observe - A process which has been started externally to Cardamon.
    pub fn observe_external_process(&mut self, process_to_observe: ProcessToObserve) {
        match &mut self.external_processes_to_observe {
            None => self.external_processes_to_observe = Some(vec![process_to_observe]),
            Some(vec) => {
                vec.push(process_to_observe);
                Some(vec);
            }
        };
    }
}

#[cfg(test)]
mod tests {
    use itertools::Itertools;

    use super::*;
    use std::path::Path;

    #[test]
    fn can_load_config_file() -> anyhow::Result<()> {
        Config::try_from_path(Path::new("./fixtures/cardamon.success.toml"))?;
        Ok(())
    }

    #[test]
    fn can_find_observation_by_name() -> anyhow::Result<()> {
        let cfg = Config::try_from_path(Path::new("./fixtures/cardamon.success.toml"))?;
        let observation = cfg.find_observation("checkout");
        assert!(observation.is_some());

        let observation = cfg.find_observation("nope");
        assert!(observation.is_none());

        Ok(())
    }

    #[test]
    fn can_find_scenario_by_name() -> anyhow::Result<()> {
        let cfg = Config::try_from_path(Path::new("./fixtures/cardamon.multiple_scenarios.toml"))?;
        let scenario = cfg.find_scenario("user_signup");
        assert!(scenario.is_ok());

        let scenario = cfg.find_scenario("nope");
        assert!(scenario.is_err());

        Ok(())
    }

    #[test]
    fn can_find_process_by_name() -> anyhow::Result<()> {
        let cfg = Config::try_from_path(Path::new("./fixtures/cardamon.success.toml"))?;
        let process = cfg.find_process("server");
        assert!(process.is_ok());

        let process = cfg.find_process("nope");
        assert!(process.is_err());

        Ok(())
    }

    // #[test]
    // fn collecting_processes_works() -> anyhow::Result<()> {
    //     let cfg = Config::try_from_path(Path::new("./fixtures/cardamon.multiple_scenarios.toml"))?;
    //
    //     let obs_name = "test_app";
    //     let obs = cfg.find_observation(obs_name).context("")?;
    //
    //     let process_names = cfg
    //         .collect_processes(obs.)?
    //         .into_iter()
    //         .map(|proc_to_exec| match proc_to_exec.process.process_type {
    //             ProcessType::BareMetal => proc_to_exec.process.name.as_str(),
    //             ProcessType::Docker { containers: _ } => proc_to_exec.process.name.as_str(),
    //         })
    //         .sorted()
    //         .collect::<Vec<_>>();
    //
    //     assert_eq!(process_names, ["db", "mailgun", "server"]);
    //
    //     Ok(())
    // }

    // #[test]
    // fn multiple_iterations_should_create_more_scenarios_to_execute() -> anyhow::Result<()> {
    //     let cfg = Config::try_from_path(Path::new("./fixtures/cardamon.multiple_iterations.toml"))?;
    //     let scenario = cfg
    //         .find_scenario("basket_10")
    //         .expect("scenario 'basket_10' should exist!");
    //     let scenarios_to_execute = vec![ScenarioToExecute::new(scenario)];
    //     assert_eq!(scenarios_to_execute.len(), 2);
    //     Ok(())
    // }

    #[test]
    fn can_create_exec_plan_for_observation() -> anyhow::Result<()> {
        let cfg = Config::try_from_path(Path::new("./fixtures/cardamon.multiple_scenarios.toml"))?;

        let exec_plan = cfg.create_execution_plan("checkout", false)?;
        match exec_plan.execution_mode {
            ExecutionMode::Observation(scenarios) => {
                let scenario_names = scenarios
                    .iter()
                    .map(|s| s.name.as_str())
                    .sorted()
                    .collect_vec();

                let process_names: Vec<&str> = exec_plan
                    .processes_to_execute
                    .into_iter()
                    .map(|proc| match proc.process_type {
                        ProcessType::Docker { containers: _ } => proc.name.as_str(),
                        ProcessType::BareMetal => proc.name.as_str(),
                    })
                    .sorted()
                    .collect();

                assert_eq!(scenario_names, ["basket_10", "user_signup"]);
                assert_eq!(process_names, ["db", "mailgun", "server"]);
            }

            _ => panic!("oops! was expecting a ObservationMode::Scenarios"),
        }

        Ok(())
    }

    #[test]
    fn can_create_exec_plan_for_monitor() -> anyhow::Result<()> {
        let cfg = Config::try_from_path(Path::new("./fixtures/cardamon.multiple_scenarios.toml"))?;

        let exec_plan = cfg.create_execution_plan("live_monitor", false)?;
        match exec_plan.execution_mode {
            ExecutionMode::Live => {
                let process_names: Vec<&str> = exec_plan
                    .processes_to_execute
                    .into_iter()
                    .map(|proc| proc.name.as_str())
                    .sorted()
                    .collect();

                assert_eq!(process_names, ["db", "mailgun", "server"]);
            }

            _ => panic!("oops! was expecting a ObservationMode::Monitor"),
        }
        Ok(())
    }
}