Skip to content

Commit

Permalink
Record: Handle Kernel config not available
Browse files Browse the repository at this point in the history
Also,
* use WARN in Perf Stat when asking to change system settings.
* use WARN in SystemInfo when IMDS fails.
  • Loading branch information
janaknat committed Nov 29, 2023
1 parent 2675a19 commit 7c3b860
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 46 deletions.
48 changes: 16 additions & 32 deletions src/data/kernel_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ use crate::{PDError, PERFORMANCE_DATA, VISUALIZATION_DATA};
use anyhow::Result;
use chrono::prelude::*;
use ctor::ctor;
use log::trace;
use log::{debug, trace};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::fs::OpenOptions;
use std::io::{self, BufRead, BufReader};
use std::path::Path;

pub static KERNEL_CONFIG_FILE_NAME: &str = "kernel_config";
const PROC_CONFIG_GZ: &str = "/proc/config.gz";
const BOOT_CONFIG: &str = "/boot/config";

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Entry {
Expand Down Expand Up @@ -75,36 +73,22 @@ fn get_kernel_config_data() -> Result<Box<dyn BufRead>> {
/* This is the same as procfs crate. We need access to the commented out CONFIGs and
* headings in the Config file.
*/
let reader: Box<dyn BufRead> =
if Path::new(PROC_CONFIG_GZ).exists() && cfg!(features = "flate2") {
#[cfg(feature = "flate2")]
{
let file = OpenOptions::new().read(true).open(PROC_CONFIG_GZ);
let decoder = flate2::read::GzDecoder::new(file);
Box::new(BufReader::new(decoder))
}
#[cfg(not(feature = "flate2"))]
{
unreachable!("flate2 feature not enabled")
}
} else {
let kernel = rustix::process::uname();
let filename = format!("{}-{}", BOOT_CONFIG, kernel.release().to_string_lossy());
let file = OpenOptions::new().read(true).open(filename);
match file {
Ok(file) => Box::new(BufReader::new(file)),
Err(e) => match e.kind() {
io::ErrorKind::NotFound => {
let backup_config_file = OpenOptions::new()
.read(true)
.open(BOOT_CONFIG)
.expect("Could not open file");
Box::new(BufReader::new(backup_config_file))
}
_ => return Err(e.into()),
},
let mut conf = format!(
"/boot/config-{}",
rustix::process::uname().release().to_string_lossy()
);
let reader: Box<dyn BufRead> = {
if !Path::new(&conf).exists() {
conf = "/boot/config".to_string();
}
match OpenOptions::new().read(true).open(&conf) {
Ok(file) => Box::new(BufReader::new(file)),
Err(e) => {
debug!("Error: {} when opening {}", e, conf);
Box::new(io::Cursor::new(b"KERNEL_CONFIG=NOT FOUND"))
}
};
}
};
Ok(reader)
}

Expand Down
24 changes: 12 additions & 12 deletions src/data/perf_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{PERFORMANCE_DATA, VISUALIZATION_DATA};
use anyhow::Result;
use chrono::prelude::*;
use ctor::ctor;
use log::{error, trace};
use log::{trace, warn};
use perf_event::events::{Raw, Software};
use perf_event::{Builder, Counter, Group, ReadFormat};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -144,10 +144,10 @@ impl CollectData for PerfStatRaw {
Err(e) => {
match e.kind() {
ErrorKind::PermissionDenied => {
error!("Set /proc/sys/kernel/perf_event_paranoid to 0")
warn!("Set /proc/sys/kernel/perf_event_paranoid to 0")
}
ErrorKind::NotFound => error!("Instance does not expose Perf counters"),
_ => error!("Unknown error when trying to use Perf API"),
ErrorKind::NotFound => warn!("Instance does not expose Perf counters"),
_ => warn!("Unknown error when trying to use Perf API"),
}
return Err(e.into());
}
Expand All @@ -174,13 +174,13 @@ impl CollectData for PerfStatRaw {
if let Some(os_error) = e.downcast_ref::<std::io::Error>() {
match os_error.kind() {
ErrorKind::NotFound => {
error!("Instance does not expose Perf counters")
warn!("Instance does not expose Perf counters")
}
_ => match os_error.raw_os_error().unwrap() {
libc::EMFILE => error!(
"Too many open files. Increase limit with `ulimit -n`"
libc::EMFILE => warn!(
"Too many open files. Increase limit with `ulimit -n 65536`"
),
_ => error!("Unknown error when trying to use Perf API."),
_ => warn!("Unknown error when trying to use Perf API."),
},
}
return Err(e);
Expand All @@ -202,13 +202,13 @@ impl CollectData for PerfStatRaw {
if let Some(os_error) = e.downcast_ref::<std::io::Error>() {
match os_error.kind() {
ErrorKind::NotFound => {
error!("Instance does not expose Perf counters")
warn!("Instance does not expose Perf counters")
}
_ => match os_error.raw_os_error().unwrap() {
libc::EMFILE => error!(
"Too many open files. Increase limit with `ulimit -n`"
libc::EMFILE => warn!(
"Too many open files. Increase limit with `ulimit -n 65536`"
),
_ => error!("Unknown error when trying to use Perf API."),
_ => warn!("Unknown error when trying to use Perf API."),
},
}
return Err(e);
Expand Down
14 changes: 12 additions & 2 deletions src/data/systeminfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{PERFORMANCE_DATA, VISUALIZATION_DATA};
use anyhow::Result;
use chrono::prelude::*;
use ctor::ctor;
use log::{error, trace};
use log::{trace, warn};
use serde::{Deserialize, Serialize};
use sysinfo::{System, SystemExt};

Expand Down Expand Up @@ -121,7 +121,17 @@ impl CollectData for SystemInfo {

match rt.block_on(EC2Metadata::get_instance_metadata()) {
Ok(s) => self.set_instance_metadata(s),
Err(e) => error!("An error occurred: {}", e),
Err(e) => {
warn!("Unable to get instance metadata: {}", e);
let s = EC2Metadata {
instance_id: "N/A".to_string(),
local_hostname: "N/A".to_string(),
ami_id: "N/A".to_string(),
region: "N/A".to_string(),
instance_type: "N/A".to_string(),
};
self.set_instance_metadata(s);
}
};

trace!("SysInfo:\n{:#?}", self);
Expand Down

0 comments on commit 7c3b860

Please sign in to comment.