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
|
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<AtomicU32>,
process_info: Arc<RwLock<HashMap<Pid, ProcessInfo>>>,
}
impl LoggingSocketService {
pub fn new(
path: &str,
process_info: Arc<RwLock<HashMap<Pid, ProcessInfo>>>,
power_limit: Arc<AtomicU32>,
) -> 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<RwLock<HashMap<Pid, ProcessInfo>>>,
) -> io::Result<Arc<AtomicU32>> {
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)
}
|