mirror of
https://github.com/aristocratos/btop.git
synced 2024-09-28 22:21:35 +02:00
Proc::collect() fixed cputimes and cpu percentage calc
This commit is contained in:
parent
60c5636cd7
commit
3564f8e4c2
@ -988,6 +988,24 @@ namespace Proc {
|
|||||||
const int cmult = (per_core) ? Shared::coreCount : 1;
|
const int cmult = (per_core) ? Shared::coreCount : 1;
|
||||||
bool got_detailed = false;
|
bool got_detailed = false;
|
||||||
|
|
||||||
|
{ //* Get CPU totals
|
||||||
|
natural_t cpu_count;
|
||||||
|
processor_info_array_t info_array;
|
||||||
|
mach_msg_type_number_t info_count;
|
||||||
|
kern_return_t error;
|
||||||
|
processor_cpu_load_info_data_t *cpu_load_info = NULL;
|
||||||
|
mach_port_t host_port = mach_host_self();
|
||||||
|
error = host_processor_info(host_port, PROCESSOR_CPU_LOAD_INFO, &cpu_count, &info_array, &info_count);
|
||||||
|
if (error != KERN_SUCCESS) {
|
||||||
|
Logger::error("Failed getting CPU load info");
|
||||||
|
}
|
||||||
|
cpu_load_info = (processor_cpu_load_info_data_t *)info_array;
|
||||||
|
cputimes = cpu_load_info[0].cpu_ticks[CPU_STATE_USER]
|
||||||
|
+ cpu_load_info[0].cpu_ticks[CPU_STATE_NICE]
|
||||||
|
+ cpu_load_info[0].cpu_ticks[CPU_STATE_SYSTEM]
|
||||||
|
+ cpu_load_info[0].cpu_ticks[CPU_STATE_IDLE];
|
||||||
|
}
|
||||||
|
|
||||||
//* Use pids from last update if only changing filter, sorting or tree options
|
//* Use pids from last update if only changing filter, sorting or tree options
|
||||||
if (no_update and not current_procs.empty()) {
|
if (no_update and not current_procs.empty()) {
|
||||||
if (show_detailed and detailed_pid != detailed.last_pid) _collect_details(detailed_pid, round(uptime), current_procs);
|
if (show_detailed and detailed_pid != detailed.last_pid) _collect_details(detailed_pid, round(uptime), current_procs);
|
||||||
@ -1012,18 +1030,15 @@ namespace Proc {
|
|||||||
|
|
||||||
//? Check if pid already exists in current_procs
|
//? Check if pid already exists in current_procs
|
||||||
auto find_old = rng::find(current_procs, pid, &proc_info::pid);
|
auto find_old = rng::find(current_procs, pid, &proc_info::pid);
|
||||||
bool no_cache = false;
|
|
||||||
if (find_old == current_procs.end()) {
|
if (find_old == current_procs.end()) {
|
||||||
current_procs.push_back({pid});
|
current_procs.push_back({pid});
|
||||||
find_old = current_procs.end() - 1;
|
find_old = current_procs.end() - 1;
|
||||||
no_cache = true;
|
|
||||||
}
|
}
|
||||||
SRUN;
|
SRUN;
|
||||||
|
|
||||||
auto &new_proc = *find_old;
|
auto &new_proc = *find_old;
|
||||||
|
|
||||||
//? Get program name, command and username
|
//? Get program name, command, username, parent pid, nice and status
|
||||||
if (no_cache) {
|
|
||||||
char fullname[PROC_PIDPATHINFO_MAXSIZE];
|
char fullname[PROC_PIDPATHINFO_MAXSIZE];
|
||||||
proc_pidpath(pid, fullname, sizeof(fullname));
|
proc_pidpath(pid, fullname, sizeof(fullname));
|
||||||
new_proc.cmd = std::string(fullname);
|
new_proc.cmd = std::string(fullname);
|
||||||
@ -1032,23 +1047,25 @@ namespace Proc {
|
|||||||
new_proc.ppid = kproc.kp_eproc.e_ppid;
|
new_proc.ppid = kproc.kp_eproc.e_ppid;
|
||||||
new_proc.p_nice = kproc.kp_proc.p_nice;
|
new_proc.p_nice = kproc.kp_proc.p_nice;
|
||||||
new_proc.state = kproc.kp_proc.p_stat;
|
new_proc.state = kproc.kp_proc.p_stat;
|
||||||
cpu_t = (kproc.kp_proc.p_uticks + kproc.kp_proc.p_sticks) * Shared::clkTck;
|
|
||||||
new_proc.cpu_t = cpu_t;
|
//? Get threads, mem and cpu usage
|
||||||
new_proc.cpu_s = kproc.kp_proc.p_sticks * Shared::clkTck;
|
|
||||||
new_proc.cpu_c = (double)new_proc.cpu_t / max(1.0, (uptime * Shared::clkTck) - new_proc.cpu_s);
|
|
||||||
struct proc_taskinfo pti;
|
struct proc_taskinfo pti;
|
||||||
if (sizeof(pti) == proc_pidinfo(new_proc.pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti))) {
|
if (sizeof(pti) == proc_pidinfo(new_proc.pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti))) {
|
||||||
new_proc.threads = pti.pti_threadnum;
|
new_proc.threads = pti.pti_threadnum;
|
||||||
new_proc.mem = pti.pti_resident_size;
|
new_proc.mem = pti.pti_resident_size;
|
||||||
|
cpu_t = (pti.pti_total_user + pti.pti_total_system) / 1'000'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_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);
|
struct passwd *pwd = getpwuid(kproc.kp_eproc.e_ucred.cr_uid);
|
||||||
new_proc.user = pwd->pw_name;
|
new_proc.user = pwd->pw_name;
|
||||||
}
|
|
||||||
//? Process cpu usage since last update
|
//? 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 * 1000 * (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
|
//? Process cumulative cpu usage since process start
|
||||||
new_proc.cpu_c = (double)cpu_t / max(1.0, (uptime * Shared::clkTck) - new_proc.cpu_s);
|
new_proc.cpu_c = (double)cpu_t / max(1.0, uptime - new_proc.cpu_s);
|
||||||
|
|
||||||
//? Update cached value with latest cpu times
|
//? Update cached value with latest cpu times
|
||||||
new_proc.cpu_t = cpu_t;
|
new_proc.cpu_t = cpu_t;
|
||||||
|
Loading…
Reference in New Issue
Block a user