Skip to content

Commit

Permalink
更新cpu占用率计算方式
Browse files Browse the repository at this point in the history
增加参数--interval控制终端信息更新间隔
  • Loading branch information
wkmyws committed Dec 27, 2023
1 parent e6b0e11 commit cac07f2
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "fan-rs"
authors = ["SuperYY"]
version = "0.0.1"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ fn main() {
Err(err) => println!("err:{}", err),
}
} else { // server
server::server::main();
let log_interval = cli::get_log_interval_millis();
server::server::main(log_interval);
}
exit(0);
}
7 changes: 7 additions & 0 deletions src/modules/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub struct Cli {
#[arg(long,default_value_t=String::from("127.0.0.1:8567"))]
addr: String,

#[arg(long, default_value_t = 1000)]
interval: u64, // 终端信息刷新间隔ms

/// Automatically adjust the fan speed
#[arg(short = 'a', long)]
auto: Option<bool>, // 根据温度自动调整风速
Expand Down Expand Up @@ -50,3 +53,7 @@ pub fn get_temp() -> bool {
pub fn get_server_addr() -> String {
return Cli::parse().addr;
}

pub fn get_log_interval_millis() -> u64 {
return Cli::parse().interval;
}
22 changes: 17 additions & 5 deletions src/modules/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::{thread, time};

static mut AUTO_MODE: bool = false;
static mut SCREEN_Y: u16 = 0;
static mut CPU_USAGE_INTERVAL_MILLIS: u64 = 100;

fn clear() {
execute!(stdout(), terminal::Clear(terminal::ClearType::All)).unwrap();
Expand Down Expand Up @@ -51,7 +52,10 @@ fn render(force: bool) -> u16 {
let fan_level = fan::fan(10).unwrap();
let help_body = http_server_help();
let tmp = format!("{:>5.2}°C", temp::get_temp().unwrap_or(0.0));
let usage = format!("{:>6.2}%", temp::get_cpu_usage(100));
let usage = format!(
"{:>6.2}%",
temp::get_cpu_usage(unsafe { CPU_USAGE_INTERVAL_MILLIS })
);

if part == false {
// 不是展示部分信息
Expand All @@ -63,7 +67,8 @@ fn render(force: bool) -> u16 {
ii += 1;
}
}
let __version = format!("| {:<20} {:>18} |",
let __version = format!(
"| {:<20} {:>18} |",
"Author: ".to_string() + env!("CARGO_PKG_AUTHORS"),
"Version: ".to_string() + env!("CARGO_PKG_VERSION")
);
Expand Down Expand Up @@ -101,7 +106,7 @@ fn render(force: bool) -> u16 {
return ii;
}

fn print_events(shraed_automode:&Arc<Mutex<bool>>) -> io::Result<()> {
fn print_events(shraed_automode: &Arc<Mutex<bool>>) -> io::Result<()> {
loop {
let event = read()?;
if let Event::Key(KeyEvent {
Expand Down Expand Up @@ -143,10 +148,17 @@ fn print_events(shraed_automode:&Arc<Mutex<bool>>) -> io::Result<()> {
Ok(())
}

pub fn main(shraed_automode: Arc<Mutex<bool>>) {
pub fn main(shraed_automode: Arc<Mutex<bool>>, flash_interval_millis: u64) {
unsafe {
AUTO_MODE = cli::is_auto();
}
let flash_interval_millis = unsafe {
if CPU_USAGE_INTERVAL_MILLIS >= flash_interval_millis {
1
} else {
flash_interval_millis - CPU_USAGE_INTERVAL_MILLIS
}
};
enable_raw_mode().unwrap();
let mut stdout = io::stdout();
queue!(
Expand All @@ -161,7 +173,7 @@ pub fn main(shraed_automode: Arc<Mutex<bool>>) {
clear();
render(true);
loop {
thread::sleep(time::Duration::from_millis(1000));
thread::sleep(time::Duration::from_millis(flash_interval_millis));
let auto_mode = *(shared_automode2.lock().unwrap());
unsafe {
AUTO_MODE = auto_mode;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub mod server {
use super::{auto_fan, http_server};
use std::{thread, sync::{Arc, Mutex}};

pub fn main() {
pub fn main(flash_interval_millis: u64) {
let addr = cli::get_server_addr();
let auto_mode = cli::is_auto();

Expand Down Expand Up @@ -177,7 +177,7 @@ pub mod server {
let thread_render = thread::spawn(move || {
// 绘制控制台
let shraed_automode = Arc::clone(&shraed_automode3);
console::main(shraed_automode);
console::main(shraed_automode, flash_interval_millis);
});
thread_render.join().unwrap();
}
Expand Down
59 changes: 22 additions & 37 deletions src/modules/temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,38 @@ use std::thread;
use std::time::Duration;

pub fn get_cpu_usage(millis: u64) -> f32 {
let times = 5; // 统计5次的均值
let interval = Duration::from_millis(millis / times);
let mut arr_usage: Vec<f32> = vec![];

let mut total_time_prev: u64 = 0;
let mut idle_time_prev: u64 = 0;

for _ in 0..times {
fn read_cpu_stat() -> Vec<u64> {
let file = File::open("/proc/stat").expect("Failed to open /proc/stat");
let reader = BufReader::new(file);

let mut total_time: u64 = 0;
let mut idle_time: u64 = 0;

for line in reader.lines() {
let line = line.expect("Failed to read line from /proc/stat");
let fields: Vec<&str> = line.split_whitespace().collect();

if fields.len() > 0 && fields[0] == "cpu" {
for (i, &field) in fields.iter().enumerate() {
if i > 0 {
let time: u64 = field.parse().expect("Failed to parse CPU time");
total_time += time;
if i == 4 {
idle_time = time;
}
}
}
break;
if fields.len() > 0 && fields[0] == "cpu" { // 找到cpu所在的行
// cpu user nice system idle iowait irq softirq steal guest guest_nice
// ->
// user nice system idle iowait irq softirq steal guest guest_nice
return fields.iter().skip(1).map(|s| s.parse::<u64>().unwrap()).collect::<Vec<u64>>();
}
}
panic!("Failed to read cpu stat");
}

let total_delta = total_time - total_time_prev;
let idle_delta = idle_time - idle_time_prev;

let usage = 1.0 - (idle_delta as f32) / (total_delta as f32);

// println!("CPU Usage: {:.2}%", usage * 100.0);
arr_usage.push(usage);

total_time_prev = total_time;
idle_time_prev = idle_time;

thread::sleep(interval);
fn calc_cpu(nums: Vec<u64>) -> (u64, u64) {
// nums: user nice system idle iowait irq softirq steal guest guest_nice
// ref : https://stackoverflow.com/questions/23367857/accurate-calculation-of-cpu-usage-given-in-percentage-in-linux
let total = nums.iter().sum::<u64>();
let idle = nums[3] + nums[4]; // idle + iowait
// let non_idle = total - idle;
return (total, idle);
}
let ans = arr_usage.iter().sum::<f32>() / arr_usage.len() as f32 * 100 as f32;
return ans;

let (pre_total, pre_idle) = calc_cpu(read_cpu_stat());
thread::sleep(Duration::from_millis(millis));
let (cur_total, cur_idle) = calc_cpu(read_cpu_stat());
let diff_total = cur_total - pre_total;
let diff_idle = cur_idle - pre_idle;
return 100.0 * (diff_total - diff_idle) as f32 / diff_total as f32;
}

pub fn get_temp() -> Option<f32> {
Expand Down

0 comments on commit cac07f2

Please sign in to comment.