use std::{ collections::HashMap, fs, io::{self, BufRead, BufReader, Write}, os::unix::net::UnixListener, path::Path, sync::{atomic::AtomicU32, Arc, RwLock}, thread, }; use crate::{energy::ProcessInfo, Pid}; pub struct LoggingSocketService { path: String, power_limit: Arc, process_info: Arc>>, } impl LoggingSocketService { pub fn new( path: &str, process_info: Arc>>, power_limit: Arc, ) -> Self { LoggingSocketService { path: path.to_string(), process_info, power_limit, } } pub fn run(self) { thread::spawn(move || { let listener = UnixListener::bind(self.path.clone()).unwrap(); for stream in listener.incoming() { let mut socket = stream.unwrap(); let mut reader = BufReader::new(socket.try_clone().unwrap()); let mut line = String::new(); while let Ok(bytes_read) = reader.read_line(&mut line) { if bytes_read == 0 { break; } let (command, args) = line.split_once(' ').unwrap_or((line.trim(), "")); let output = match command { "pid" => self.get_process(args.trim().parse().unwrap_or_default()), "list" => self.list_processes(), "limit" => self.set_power_limit(args), _ => "Unrecognized command#".into(), }; socket.write_all(output.as_bytes()).unwrap(); line.clear(); } } }); } fn list_processes(&self) -> String { let mut output = String::new(); use std::fmt::Write; for (pid, info) in self.process_info.read().unwrap().iter() { writeln!( &mut output, "{pid},{},{}", info.energy_j, info.tree_energy_j ) .unwrap(); } writeln!(&mut output, "#",).unwrap(); output } fn set_power_limit(&self, args: &str) -> String { if let Ok(power_limit) = args.trim().parse() { self.power_limit .store(power_limit, std::sync::atomic::Ordering::Relaxed); "#".into() } else { format!("failed to parse {args} as energy limit#") } } fn get_process(&self, pid: i32) -> String { if let Some(info) = self.process_info.read().unwrap().get(&pid) { format!( "pid: {pid} process: {}J process tree: {}J\n", info.energy_j, info.tree_energy_j ) } else { format!("Unknown pid: {pid}\n") } } } pub fn start_logging_socket_service( path: &str, process_info: Arc>>, ) -> io::Result> { if Path::new(path).exists() { fs::remove_file(path)?; } let arc = Arc::new(AtomicU32::new(50)); let socket = LoggingSocketService::new(path, process_info, arc.clone()); socket.run(); Ok(arc) }