From 65357c8c46154de4e4eca14075bfe5523bb5fc14 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Mon, 7 Dec 2020 10:26:01 +0100 Subject: New upstream version 3.0.3 --- openbsd/OpenBSDProcessList.c | 210 +++++++++++++++++++++---------------------- 1 file changed, 101 insertions(+), 109 deletions(-) (limited to 'openbsd/OpenBSDProcessList.c') diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index c5d79f5..5412030 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -2,49 +2,53 @@ htop - OpenBSDProcessList.c (C) 2014 Hisham H. Muhammad (C) 2015 Michael McConville -Released under the GNU GPL, see the COPYING file +Released under the GNU GPLv2, see the COPYING file in the source distribution for its full text. */ -#include "CRT.h" -#include "ProcessList.h" #include "OpenBSDProcessList.h" -#include "OpenBSDProcess.h" #include -#include -#include +#include +#include +#include +#include +#include #include #include #include -#include #include +#include #include #include -#include -#include -#include -#include -#include +#include + +#include "CRT.h" +#include "Macros.h" +#include "Object.h" +#include "OpenBSDProcess.h" +#include "Process.h" +#include "ProcessList.h" +#include "Settings.h" +#include "XUtils.h" + static long fscale; ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) { - int mib[] = { CTL_HW, HW_NCPU }; - int fmib[] = { CTL_KERN, KERN_FSCALE }; - int i, e; - OpenBSDProcessList* opl; - ProcessList* pl; + const int mib[] = { CTL_HW, HW_NCPU }; + const int fmib[] = { CTL_KERN, KERN_FSCALE }; + int r; size_t size; char errbuf[_POSIX2_LINE_MAX]; - opl = xCalloc(1, sizeof(OpenBSDProcessList)); - pl = (ProcessList*) opl; - size = sizeof(pl->cpuCount); + OpenBSDProcessList* opl = xCalloc(1, sizeof(OpenBSDProcessList)); + ProcessList* pl = (ProcessList*) opl; ProcessList_init(pl, Class(OpenBSDProcess), usersTable, pidMatchList, userId); - e = sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0); - if (e == -1 || pl->cpuCount < 1) { + size = sizeof(pl->cpuCount); + r = sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0); + if (r < 0 || pl->cpuCount < 1) { pl->cpuCount = 1; } opl->cpus = xCalloc(pl->cpuCount + 1, sizeof(CPUData)); @@ -54,8 +58,8 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, ui err(1, "fscale sysctl call failed"); } - for (i = 0; i <= pl->cpuCount; i++) { - CPUData *d = opl->cpus + i; + for (int i = 0; i <= pl->cpuCount; i++) { + CPUData* d = opl->cpus + i; d->totalTime = 1; d->totalPeriod = 1; } @@ -69,7 +73,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, ui } void ProcessList_delete(ProcessList* this) { - const OpenBSDProcessList* opl = (OpenBSDProcessList*) this; + OpenBSDProcessList* opl = (OpenBSDProcessList*) this; if (opl->kd) { kvm_close(opl->kd); @@ -81,8 +85,8 @@ void ProcessList_delete(ProcessList* this) { free(this); } -static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { - static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP}; +static void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { + const int uvmexp_mib[] = { CTL_VM, VM_UVMEXP }; struct uvmexp uvmexp; size_t size_uvmexp = sizeof(uvmexp); @@ -90,10 +94,11 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { err(1, "uvmexp sysctl call failed"); } - pl->totalMem = uvmexp.npages * PAGE_SIZE_KB; + pl->totalMem = uvmexp.npages * CRT_pageSizeKB; + pl->usedMem = (uvmexp.npages - uvmexp.free - uvmexp.paging) * CRT_pageSizeKB; // Taken from OpenBSD systat/iostat.c, top/machine.c and uvm_sysctl(9) - static int bcache_mib[] = {CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT}; + const int bcache_mib[] = { CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT }; struct bcachestats bcstats; size_t size_bcstats = sizeof(bcstats); @@ -101,56 +106,56 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { err(1, "cannot get vfs.bcachestat"); } - pl->cachedMem = bcstats.numbufpages * PAGE_SIZE_KB; - pl->freeMem = uvmexp.free * PAGE_SIZE_KB; - pl->usedMem = (uvmexp.npages - uvmexp.free - uvmexp.paging) * PAGE_SIZE_KB; + pl->cachedMem = bcstats.numbufpages * CRT_pageSizeKB; /* - const OpenBSDProcessList* opl = (OpenBSDProcessList*) pl; - - size_t len = sizeof(pl->totalMem); - sysctl(MIB_hw_physmem, 2, &(pl->totalMem), &len, NULL, 0); - pl->totalMem /= 1024; - sysctl(MIB_vm_stats_vm_v_wire_count, 4, &(pl->usedMem), &len, NULL, 0); - pl->usedMem *= PAGE_SIZE_KB; - pl->freeMem = pl->totalMem - pl->usedMem; - sysctl(MIB_vm_stats_vm_v_cache_count, 4, &(pl->cachedMem), &len, NULL, 0); - pl->cachedMem *= PAGE_SIZE_KB; - - struct kvm_swap swap[16]; - int nswap = kvm_getswapinfo(opl->kd, swap, sizeof(swap)/sizeof(swap[0]), 0); - pl->totalSwap = 0; - pl->usedSwap = 0; - for (int i = 0; i < nswap; i++) { - pl->totalSwap += swap[i].ksw_total; - pl->usedSwap += swap[i].ksw_used; - } - pl->totalSwap *= PAGE_SIZE_KB; - pl->usedSwap *= PAGE_SIZE_KB; + * Copyright (c) 1994 Thorsten Lockert + * All rights reserved. + * + * Taken almost directly from OpenBSD's top(1) + * + * Originally released under a BSD-3 license + * Modified through htop developers applying GPL-2 + */ + int nswap = swapctl(SWAP_NSWAP, 0, 0); + if (nswap > 0) { + struct swapent swdev[nswap]; + int rnswap = swapctl(SWAP_STATS, swdev, nswap); + + /* Total things up */ + unsigned long long int total = 0, used = 0; + for (int i = 0; i < rnswap; i++) { + if (swdev[i].se_flags & SWF_ENABLE) { + used += (swdev[i].se_inuse / (1024 / DEV_BSIZE)); + total += (swdev[i].se_nblks / (1024 / DEV_BSIZE)); + } + } - pl->sharedMem = 0; // currently unused - pl->buffersMem = 0; // not exposed to userspace - */ + pl->totalSwap = total; + pl->usedSwap = used; + } else { + pl->totalSwap = pl->usedSwap = 0; + } } -char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd) { - char *s, **arg; - size_t len = 0, n; - int i; - +static char* OpenBSDProcessList_readProcessName(kvm_t* kd, const struct kinfo_proc* kproc, int* basenameEnd) { /* * Like OpenBSD's top(1), we try to fall back to the command name * (argv[0]) if we fail to construct the full command. */ - arg = kvm_getargv(kd, kproc, 500); + char** arg = kvm_getargv(kd, kproc, 500); if (arg == NULL || *arg == NULL) { *basenameEnd = strlen(kproc->p_comm); return xStrdup(kproc->p_comm); } - for (i = 0; arg[i] != NULL; i++) { + + size_t len = 0; + for (int i = 0; arg[i] != NULL; i++) { len += strlen(arg[i]) + 1; /* room for arg and trailing space or NUL */ } + /* don't use xMalloc here - we want to handle huge argv's gracefully */ + char* s; if ((s = malloc(len)) == NULL) { *basenameEnd = strlen(kproc->p_comm); return xStrdup(kproc->p_comm); @@ -158,11 +163,10 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in *s = '\0'; - for (i = 0; arg[i] != NULL; i++) { - n = strlcat(s, arg[i], len); + for (int i = 0; arg[i] != NULL; i++) { + size_t n = strlcat(s, arg[i], len); if (i == 0) { - /* TODO: rename all basenameEnd to basenameLen, make size_t */ - *basenameEnd = MINIMUM(n, len-1); + *basenameEnd = MINIMUM(n, len - 1); } /* the trailing space should get truncated anyway */ strlcat(s, " ", len); @@ -174,43 +178,29 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in /* * Taken from OpenBSD's ps(1). */ -static double getpcpu(const struct kinfo_proc *kp) { +static double getpcpu(const struct kinfo_proc* kp) { if (fscale == 0) - return (0.0); + return 0.0; -#define fxtofl(fixpt) ((double)(fixpt) / fscale) - - return (100.0 * fxtofl(kp->p_pctcpu)); + return 100.0 * (double)kp->p_pctcpu / fscale; } -static inline void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { - Settings* settings = this->super.settings; +static void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { + const Settings* settings = this->super.settings; bool hideKernelThreads = settings->hideKernelThreads; bool hideUserlandThreads = settings->hideUserlandThreads; - struct kinfo_proc* kproc; - bool preExisting; - Process* proc; - OpenBSDProcess* fp; - struct tm date; - struct timeval tv; int count = 0; - int i; - - // use KERN_PROC_KTHREAD to also include kernel threads - struct kinfo_proc* kprocs = kvm_getprocs(this->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count); - //struct kinfo_proc* kprocs = getprocs(KERN_PROC_ALL, 0, &count); - gettimeofday(&tv, NULL); + const struct kinfo_proc* kprocs = kvm_getprocs(this->kd, KERN_PROC_KTHREAD, 0, sizeof(struct kinfo_proc), &count); - for (i = 0; i < count; i++) { - kproc = &kprocs[i]; + for (int i = 0; i < count; i++) { + const struct kinfo_proc* kproc = &kprocs[i]; - preExisting = false; - proc = ProcessList_getProcess(&this->super, kproc->p_pid, &preExisting, (Process_New) OpenBSDProcess_new); - fp = (OpenBSDProcess*) proc; + bool preExisting = false; + Process* proc = ProcessList_getProcess(&this->super, kproc->p_pid, &preExisting, OpenBSDProcess_new); + //OpenBSDProcess* fp = (OpenBSDProcess*) proc; - proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) - || (hideUserlandThreads && Process_isUserlandThread(proc))); + proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc))); if (!preExisting) { proc->ppid = kproc->p_ppid; @@ -221,11 +211,10 @@ static inline void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { proc->pgrp = kproc->p__pgid; proc->st_uid = kproc->p_uid; proc->starttime_ctime = kproc->p_ustart_sec; + Process_fillStarttimeBuffer(proc); proc->user = UsersTable_getRef(this->super.usersTable, proc->st_uid); ProcessList_add(&this->super, proc); proc->comm = OpenBSDProcessList_readProcessName(this->kd, kproc, &proc->basenameOffset); - (void) localtime_r((time_t*) &kproc->p_ustart_sec, &date); - strftime(proc->starttime_show, 7, ((proc->starttime_ctime > tv.tv_sec - 86400) ? "%R " : "%b%d "), &date); } else { if (settings->updateProcessNames) { free(proc->comm); @@ -233,15 +222,13 @@ static inline void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { } } - proc->m_size = kproc->p_vm_dsize; + proc->m_virt = kproc->p_vm_dsize; proc->m_resident = kproc->p_vm_rssize; - proc->percent_mem = (proc->m_resident * PAGE_SIZE_KB) / (double)(this->super.totalMem) * 100.0; - proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0, this->super.cpuCount*100.0); + proc->percent_mem = (proc->m_resident * CRT_pageSizeKB) / (double)(this->super.totalMem) * 100.0; + proc->percent_cpu = CLAMP(getpcpu(kproc), 0.0, this->super.cpuCount * 100.0); //proc->nlwp = kproc->p_numthreads; - //proc->time = kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 10); proc->nice = kproc->p_nice - 20; - proc->time = kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 1000000); - proc->time *= 100; + proc->time = 100 * (kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 1000000)); proc->priority = kproc->p_priority - PZERO; switch (kproc->p_stat) { @@ -273,10 +260,9 @@ static unsigned long long saturatingSub(unsigned long long a, unsigned long long } static void getKernelCPUTimes(int cpuId, u_int64_t* times) { - int mib[] = { CTL_KERN, KERN_CPTIME2, cpuId }; - size_t length = sizeof(u_int64_t) * CPUSTATES; - if (sysctl(mib, 3, times, &length, NULL, 0) == -1 || - length != sizeof(u_int64_t) * CPUSTATES) { + const int mib[] = { CTL_KERN, KERN_CPTIME2, cpuId }; + size_t length = sizeof(*times) * CPUSTATES; + if (sysctl(mib, 3, times, &length, NULL, 0) == -1 || length != sizeof(*times) * CPUSTATES) { CRT_fatalError("sysctl kern.cp_time2 failed"); } } @@ -344,10 +330,16 @@ static void OpenBSDProcessList_scanCPUTime(OpenBSDProcessList* this) { kernelCPUTimesToHtop(avg, this->cpus); } -void ProcessList_goThroughEntries(ProcessList* this) { - OpenBSDProcessList* opl = (OpenBSDProcessList*) this; +void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { + OpenBSDProcessList* opl = (OpenBSDProcessList*) super; - OpenBSDProcessList_scanMemoryInfo(this); - OpenBSDProcessList_scanProcs(opl); + OpenBSDProcessList_scanMemoryInfo(super); OpenBSDProcessList_scanCPUTime(opl); + + // in pause mode only gather global data for meters (CPU/memory/...) + if (pauseProcessUpdate) { + return; + } + + OpenBSDProcessList_scanProcs(opl); } -- cgit v1.2.3