From 6a3c5d9b976c711461d08c373483c84bb69db257 Mon Sep 17 00:00:00 2001 From: aristocratos Date: Thu, 7 Oct 2021 13:20:30 +0200 Subject: [PATCH] Proc::collect() better cpu percent accurazy --- src/osx/btop_collect.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index ce90b3c..8280f76 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -1029,22 +1029,26 @@ namespace Proc { found.push_back(pid); //? Check if pid already exists in current_procs + bool no_cache = false; auto find_old = rng::find(current_procs, pid, &proc_info::pid); if (find_old == current_procs.end()) { current_procs.push_back({pid}); find_old = current_procs.end() - 1; + no_cache = true; } SRUN; auto &new_proc = *find_old; //? Get program name, command, username, parent pid, nice and status - char fullname[PROC_PIDPATHINFO_MAXSIZE]; - proc_pidpath(pid, fullname, sizeof(fullname)); - new_proc.cmd = std::string(fullname); - size_t lastSlash = new_proc.cmd.find_last_of('/'); - new_proc.name = new_proc.cmd.substr(lastSlash + 1); - new_proc.ppid = kproc.kp_eproc.e_ppid; + if (no_cache) { + char fullname[PROC_PIDPATHINFO_MAXSIZE]; + proc_pidpath(pid, fullname, sizeof(fullname)); + new_proc.cmd = std::string(fullname); + size_t lastSlash = new_proc.cmd.find_last_of('/'); + new_proc.name = new_proc.cmd.substr(lastSlash + 1); + new_proc.ppid = kproc.kp_eproc.e_ppid; + } new_proc.p_nice = kproc.kp_proc.p_nice; new_proc.state = kproc.kp_proc.p_stat; @@ -1053,16 +1057,16 @@ namespace Proc { if (sizeof(pti) == proc_pidinfo(new_proc.pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti))) { new_proc.threads = pti.pti_threadnum; new_proc.mem = pti.pti_resident_size; - cpu_t = (pti.pti_total_user + pti.pti_total_system) / 1'000'000; + cpu_t = (pti.pti_total_user + pti.pti_total_system) / 1'000; if (new_proc.cpu_t == 0) new_proc.cpu_t = cpu_t; - new_proc.cpu_s = pti.pti_total_system / 1'000'000; + new_proc.cpu_s = pti.pti_total_system / 1'000; new_proc.cpu_c = (double)new_proc.cpu_t / max(1.0, uptime - new_proc.cpu_s); } struct passwd *pwd = getpwuid(kproc.kp_eproc.e_ucred.cr_uid); new_proc.user = pwd->pw_name; //? Process cpu usage since last update - new_proc.cpu_p = clamp(round(cmult * 1000 * (cpu_t - new_proc.cpu_t) / max((uint64_t)1, cputimes - old_cputimes)) / 10.0, 0.0, 100.0 * Shared::coreCount); + new_proc.cpu_p = clamp(round(cmult * (cpu_t - new_proc.cpu_t) / max((uint64_t)1, cputimes - old_cputimes)) / 10.0, 0.0, 100.0 * Shared::coreCount); //? Process cumulative cpu usage since process start new_proc.cpu_c = (double)cpu_t / max(1.0, uptime - new_proc.cpu_s);