Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add csv logging from daemon #432

Merged
merged 7 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ debug/
target/
.idea/
pkg/


# Auto-generated by autoignore:
# --------------- #
test.csv
# ----------------#
15 changes: 14 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,16 @@ enum ACSCommand {
no_animation: bool,

/// Graph
#[structopt(short = "g", long = "--graph")]
#[structopt(short = "g", long = "graph")]
graph_type: Option<String>,

/// Commit hash
#[structopt(short, long)]
commit: bool,

/// Write to a csv file
#[structopt(long = "csv")]
csv_file: Option<String>,
},

/// Monitor each cpu, it's min, max, and current speed, along with the governor
Expand Down Expand Up @@ -220,6 +224,10 @@ enum ACSCommand {
/// Commit hash
#[structopt(short, long)]
commit: bool,

/// Write to a csv file
#[structopt(long = "csv")]
csv_file: Option<String>,
},
}

Expand All @@ -234,6 +242,7 @@ pub fn parse_args(config: config::Config) {
graph: GraphType::Hidden,
commit: false,
testing: false,
csv_file: None,
};

let int = Interface {
Expand Down Expand Up @@ -280,6 +289,7 @@ pub fn parse_args(config: config::Config) {
no_animation,
graph_type,
commit,
csv_file,
} => {
if !config_dir_exists() {
warn_user!("Config directory '/etc/acs' does not exist!");
Expand Down Expand Up @@ -315,6 +325,7 @@ pub fn parse_args(config: config::Config) {
graph: parsed_graph_type,
commit,
testing: false,
csv_file,
};

match daemon_init(settings, config) {
Expand All @@ -333,6 +344,7 @@ pub fn parse_args(config: config::Config) {
graph_type,
hook,
commit,
csv_file,
} => {
if !config_dir_exists() {
warn_user!("Config directory '/etc/acs' does not exist!");
Expand Down Expand Up @@ -367,6 +379,7 @@ pub fn parse_args(config: config::Config) {
graph: parsed_graph_type,
commit,
testing: false,
csv_file,
};

match daemon_init(settings, config) {
Expand Down
15 changes: 15 additions & 0 deletions src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub trait Speed {
fn get_temp(&mut self) -> Result<(), Error>;
fn get_gov(&mut self) -> Result<(), Error>;
fn set_gov(&mut self, gov: String) -> Result<(), Error>;
fn to_csv(&self) -> String;
fn random() -> CPU;
}

Expand Down Expand Up @@ -205,6 +206,20 @@ impl Speed for CPU {
},
}
}

fn to_csv(&self) -> String {
format!(
"{},{},{},{},{},{},{},{}\n",
self.name,
self.number,
self.max_freq,
self.min_freq,
self.cur_freq,
self.cur_temp,
self.cur_usage,
self.gov
JakeRoggenbuck marked this conversation as resolved.
Show resolved Hide resolved
)
}
}

impl fmt::Display for CPU {
Expand Down
33 changes: 33 additions & 0 deletions src/daemon.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::power::battery::{has_battery, Battery};
use std::convert::TryInto;
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::time::SystemTime;
use std::{thread, time};
Expand Down Expand Up @@ -69,6 +72,7 @@ pub trait Checker {
fn update_all(&mut self) -> Result<(), Error>;

fn run_state_machine(&mut self) -> State;
fn write_csv(&self);

fn preprint_render(&mut self) -> String;
fn postprint_render(&mut self) -> String;
Expand Down Expand Up @@ -184,6 +188,23 @@ impl Checker for Daemon {
state
}

fn write_csv(&self) {
let lines = &self.cpus.iter().map(|c| c.to_csv()).collect::<String>();

if let Some(name) = &self.settings.csv_file {
let mut file = OpenOptions::new()
.write(true)
.append(true) // This is needed to append to file
.open(name)
.unwrap();

match write!(file, "{}", lines) {
Ok(_) => {}
Err(..) => warn_user!("CSV Writer not working."),
};
}
}

fn init(&mut self) {
// Get the commit hash from the compile time env variable
if self.settings.commit {
Expand All @@ -192,6 +213,12 @@ impl Checker for Daemon {

self.timeout_battery = time::Duration::from_millis(self.settings.delay_battery);
self.timeout = time::Duration::from_millis(self.settings.delay);

if let Some(name) = &self.settings.csv_file {
if !Path::new(name).exists() {
File::create(name).expect("CSV file could not be created.");
JakeRoggenbuck marked this conversation as resolved.
Show resolved Hide resolved
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should have an option to just override it on startup so u dont have to go in and delete the previous one every time

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New folders for each day? We could create a new CSV file hourly. We don't want massive individual files.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep sounds good, we can also just use the timestamp as the file name

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point about the size limit. It just writes out the current cpu objects with the to_csv to format them as strings then write it to a file. It does not store the whole thing in memory. I think a size limit would be very good so it does not break a hard drive.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file was getting multiple kilobytes in a few seconds

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, also the size limits are acutally all in mebibytes instead of megabites

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea

}

fn start_loop(&mut self) -> Result<(), Error> {
Expand All @@ -204,6 +231,8 @@ impl Checker for Daemon {
self.lid_state = read_lid_state()?;
self.usage = calculate_average_usage(&self.cpus) * 100.0;

self.write_csv();

Ok(())
}

Expand Down Expand Up @@ -484,6 +513,7 @@ pub fn daemon_init(settings: Settings, config: Config) -> Result<Arc<Mutex<Daemo
graph: settings.graph,
commit: settings.commit,
testing: settings.testing,
csv_file: settings.csv_file,
};

// Attempt to create battery object
Expand Down Expand Up @@ -640,6 +670,7 @@ mod tests {
graph: GraphType::Hidden,
commit: false,
testing: true,
csv_file: None,
};

let config = default_config();
Expand Down Expand Up @@ -668,6 +699,7 @@ mod tests {
graph: GraphType::Hidden,
commit: false,
testing: true,
csv_file: None,
};

let config = default_config();
Expand Down Expand Up @@ -699,6 +731,7 @@ mod tests {
graph: GraphType::Hidden,
commit: false,
testing: true,
csv_file: None,
};

let config = default_config();
Expand Down
1 change: 1 addition & 0 deletions src/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub fn interactive() {
graph: GraphType::Hidden,
commit: false,
testing: false,
csv_file: None,
};

loop {
Expand Down
1 change: 1 addition & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ pub struct Settings {
pub graph: GraphType,
pub commit: bool,
pub testing: bool,
pub csv_file: Option<String>,
}