diff --git a/sys-tuner/src/main.rs b/sys-tuner/src/main.rs index 739b1b2b91..374e76be2e 100644 --- a/sys-tuner/src/main.rs +++ b/sys-tuner/src/main.rs @@ -1,40 +1,28 @@ use log::*; -use std::{fs, io}; - -use solana_sys_tuner::SOLANA_SYS_TUNER_PATH; - -#[cfg(unix)] -use unix_socket::UnixListener; #[cfg(target_os = "linux")] -use std::fs::DirEntry; -#[cfg(target_os = "linux")] -use std::path::Path; - -#[cfg(target_os = "linux")] -fn find_pid, F>(name: &str, path: P, processor: F) -> Option -where - F: Fn(&DirEntry) -> Option, -{ - for entry in fs::read_dir(path).expect("Failed to read /proc folder") { - if let Ok(dir) = entry { - let mut path = dir.path(); - path.push("comm"); - if let Ok(comm) = fs::read_to_string(path.as_path()) { - if comm.starts_with(name) { - if let Some(pid) = processor(&dir) { - return Some(pid); +fn tune_system() { + fn find_pid, F>(name: &str, path: P, processor: F) -> Option + where + F: Fn(&std::fs::DirEntry) -> Option, + { + for entry in std::fs::read_dir(path).expect("Failed to read /proc folder") { + if let Ok(dir) = entry { + let mut path = dir.path(); + path.push("comm"); + if let Ok(comm) = std::fs::read_to_string(path.as_path()) { + if comm.starts_with(name) { + if let Some(pid) = processor(&dir) { + return Some(pid); + } } } } } + + None } - None -} - -#[cfg(target_os = "linux")] -fn tune_system() { use std::process::Command; use std::str::from_utf8; @@ -49,7 +37,7 @@ fn tune_system() { } }) }) { - info!("POH thread PID is {}", pid); + info!("PoH thread PID is {}", pid); let pid = format!("{}", pid); let output = Command::new("chrt") .args(&["-r", "-p", "99", pid.as_str()]) @@ -61,53 +49,52 @@ fn tune_system() { error!("chrt stderr: {}", from_utf8(&output.stderr).unwrap_or("?")); } } else { - error!("Could not find pid for POH thread"); + error!("Could not find pid for PoH thread"); } } -#[cfg(any(not(unix), target_os = "macos"))] -fn tune_system() {} - -#[allow(dead_code)] -#[cfg(target_os = "linux")] -fn set_socket_permissions() { - if let Some(user) = users::get_user_by_name("solana") { - let uid = format!("{}", user.uid()); - info!("UID for solana is {}", uid); - nix::unistd::chown( - SOLANA_SYS_TUNER_PATH, - Some(nix::unistd::Uid::from_raw(user.uid())), - None, - ) - .expect("Expected to change UID of the socket file"); - } else { - error!("Could not find UID for solana user"); - } -} - -#[cfg(any(not(unix), target_os = "macos"))] -fn set_socket_permissions() {} - +#[cfg(unix)] fn main() { solana_logger::setup(); - if let Err(e) = fs::remove_file(SOLANA_SYS_TUNER_PATH) { - if e.kind() != io::ErrorKind::NotFound { + if let Err(e) = std::fs::remove_file(solana_sys_tuner::SOLANA_SYS_TUNER_PATH) { + if e.kind() != std::io::ErrorKind::NotFound { panic!("Failed to remove stale socket file: {:?}", e) } } - let listener = - UnixListener::bind(SOLANA_SYS_TUNER_PATH).expect("Failed to bind to the socket file"); + let listener = unix_socket::UnixListener::bind(solana_sys_tuner::SOLANA_SYS_TUNER_PATH) + .expect("Failed to bind to the socket file"); - set_socket_permissions(); + // set socket permission + #[cfg(target_os = "linux")] + { + if let Some(user) = users::get_user_by_name("solana") { + let uid = format!("{}", user.uid()); + info!("UID for solana is {}", uid); + nix::unistd::chown( + solana_sys_tuner::SOLANA_SYS_TUNER_PATH, + Some(nix::unistd::Uid::from_raw(user.uid())), + None, + ) + .expect("Expected to change UID of the socket file"); + } else { + error!("Could not find UID for solana user"); + } + } info!("Waiting for tuning requests"); for stream in listener.incoming() { if stream.is_ok() { info!("Tuning the system now"); + #[cfg(target_os = "linux")] tune_system(); } } info!("exiting"); } + +#[cfg(not(unix))] +fn main() { + error!("Unsupported platform"); +}