From b1d81bf8c936509b6f83b2eac98da8ae72e0a4e3 Mon Sep 17 00:00:00 2001 From: Mustafa Salih <37256744+MustafaSalih1993@users.noreply.github.com> Date: Wed, 12 May 2021 16:46:48 +0300 Subject: [PATCH] getting data programmatically (#56) * getting loadavg programmatically * added missing uptime in rsblock.yml * using clock_gettime syscall to get uptime --- rsblocks.yml | 13 ++++++++++--- src/utils/load_average.rs | 8 ++++++-- src/utils/uptime.rs | 34 +++++++++++++++++++++++----------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/rsblocks.yml b/rsblocks.yml index 32902be..f730df6 100644 --- a/rsblocks.yml +++ b/rsblocks.yml @@ -11,6 +11,13 @@ time: icon: '' +# Time of the system has been running +uptime: + delay: 15.0 + enabled: false + icon: '' + + # public ip address pub_ip: delay: 120.0 @@ -18,7 +25,7 @@ pub_ip: icon: '' -# ethernet/wifi bandwith +# ethernet/wifi bandwith (no delay for this since it will calculate the bandwith every second) netspeed: enabled: false interface: 'wlan0' @@ -50,9 +57,9 @@ cpu_temperature: icon: '' -# cpu load average +# cpu load average from the last minute loadavg: - delay: 2.0 + delay: 15.0 enabled: false icon: '' diff --git a/src/utils/load_average.rs b/src/utils/load_average.rs index 414ab6f..43dc88b 100644 --- a/src/utils/load_average.rs +++ b/src/utils/load_average.rs @@ -1,12 +1,16 @@ use crate::config::CONFIG; use crate::types::ThreadsData; -use nix::sys::sysinfo; + +use nix::libc::{c_double, c_int, getloadavg}; pub fn get_load_avg() -> ThreadsData { - let load = sysinfo::sysinfo().unwrap().load_average().0; + let mut data: [c_double; 3] = [0f64; 3]; + unsafe { getloadavg(data.as_mut_ptr(), data.len() as c_int) }; + let [load, _, _] = data; let data = format!( " {} {:.2} {}", CONFIG.loadavg.icon, load, CONFIG.seperator ); + ThreadsData::LoadAvg(data) } diff --git a/src/utils/uptime.rs b/src/utils/uptime.rs index 4bb06cc..f635b6b 100644 --- a/src/utils/uptime.rs +++ b/src/utils/uptime.rs @@ -1,21 +1,33 @@ use crate::config::CONFIG; use crate::types::ThreadsData; -use chrono::Duration; -use nix::sys::sysinfo; +use nix::time::clock_gettime; pub fn get_uptime() -> ThreadsData { - let duration = sysinfo::sysinfo().unwrap().uptime(); - let uptime_sec = Duration::from_std(duration).unwrap().num_seconds(); - - let hour = uptime_sec / 3600; - let rem = uptime_sec - hour * 3600; - let minutes = rem / 60; - + let (_, hour, minutes, seconds) = get_uptime_data(); let uptime = if hour > 0 { - format!("{}:{}", hour, minutes) + format!("{}:{}:{}", hour, minutes, seconds) } else { - format!("{} min", minutes) + format!("{} min, {} sec", minutes, seconds) }; let result = format!(" {} {} {}", CONFIG.uptime.icon, uptime, CONFIG.seperator); ThreadsData::Uptime(result) } + +// This helper function will use the system call clock_gettime +// it will return a tuple of (days, hours, minutes, seconds) +fn get_uptime_data() -> (i64, i64, i64, i64) { + let mut uptime = clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC) + .unwrap() + .tv_sec(); + + if uptime > 60 { + uptime += 30; + } + let days = uptime / 86400; + uptime %= 86400; + let hours = uptime / 3600; + uptime %= 3600; + let minutes = uptime / 60; + let seconds = uptime % 60; + (days, hours, minutes, seconds) +}