From c7701ebd199b7273e7d74cc6823e24ee85e0cd42 Mon Sep 17 00:00:00 2001 From: mpostma Date: Tue, 23 Jun 2020 14:31:29 +0200 Subject: [PATCH] partial sysinfo fix --- meilisearch-http/src/routes/stats.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/meilisearch-http/src/routes/stats.rs b/meilisearch-http/src/routes/stats.rs index 5f3e09dd5..fcbc8e453 100644 --- a/meilisearch-http/src/routes/stats.rs +++ b/meilisearch-http/src/routes/stats.rs @@ -202,10 +202,12 @@ async fn get_sys_info(data: web::Data) -> HttpResponse { let mut sys = System::new(); let mut info = SysInfo::new(); - info.memory_usage = sys.get_used_memory() as f64 / sys.get_total_memory() as f64 * 100.0; + // need to refresh twice for cpu usage + sys.refresh_all(); + sys.refresh_all(); for processor in sys.get_processors() { - info.processor_usage.push(processor.get_cpu_usage() * 100.0); + info.processor_usage.push(processor.get_cpu_usage()); } info.global.total_memory = sys.get_total_memory(); @@ -223,12 +225,14 @@ async fn get_sys_info(data: web::Data) -> HttpResponse { .map(|(_, n)| n.get_transmitted()) .sum::(); + info.memory_usage = sys.get_used_memory() as f64 / sys.get_total_memory() as f64 * 100.0; + if let Some(process) = sys.get_process(data.server_pid) { info.process.memory = process.memory(); - info.process.cpu = process.cpu_usage() * 100.0; + println!("cpu usafe: {}", process.cpu_usage()); + info.process.cpu = process.cpu_usage(); } - sys.refresh_all(); HttpResponse::Ok().json(info) } @@ -297,6 +301,8 @@ async fn get_sys_info_pretty(data: web::Data) -> HttpResponse { let mut sys = System::new(); let mut info = SysInfoPretty::new(); + sys.refresh_all(); + sys.refresh_all(); info.memory_usage = format!( "{:.1} %", sys.get_used_memory() as f64 / sys.get_total_memory() as f64 * 100.0 @@ -304,7 +310,7 @@ async fn get_sys_info_pretty(data: web::Data) -> HttpResponse { for processor in sys.get_processors() { info.processor_usage - .push(format!("{:.1} %", processor.get_cpu_usage() * 100.0)); + .push(format!("{:.1} %", processor.get_cpu_usage())); } info.global.total_memory = convert(sys.get_total_memory() as f64 * 1024.0); @@ -326,10 +332,8 @@ async fn get_sys_info_pretty(data: web::Data) -> HttpResponse { if let Some(process) = sys.get_process(data.server_pid) { info.process.memory = convert(process.memory() as f64 * 1024.0); - info.process.cpu = format!("{:.1} %", process.cpu_usage() * 100.0); + info.process.cpu = format!("{:.1} %", process.cpu_usage()); } - sys.refresh_all(); - HttpResponse::Ok().json(info) }