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 --- darwin/Battery.c | 74 ------------- darwin/Battery.h | 6 -- darwin/DarwinCRT.c | 34 ------ darwin/DarwinCRT.h | 12 --- darwin/DarwinProcess.c | 73 ++++++------- darwin/DarwinProcess.h | 23 ++--- darwin/DarwinProcessList.c | 253 ++++++++++++++++++++++++--------------------- darwin/DarwinProcessList.h | 29 +----- darwin/Platform.c | 194 +++++++++++++++++++++++++++------- darwin/Platform.h | 40 +++++-- 10 files changed, 371 insertions(+), 367 deletions(-) delete mode 100644 darwin/Battery.c delete mode 100644 darwin/Battery.h delete mode 100644 darwin/DarwinCRT.c delete mode 100644 darwin/DarwinCRT.h (limited to 'darwin') diff --git a/darwin/Battery.c b/darwin/Battery.c deleted file mode 100644 index d52a595..0000000 --- a/darwin/Battery.c +++ /dev/null @@ -1,74 +0,0 @@ - -#include "BatteryMeter.h" - -#include -#include -#include -#include - -void Battery_getData(double* level, ACPresence* isOnAC) { - CFTypeRef power_sources = IOPSCopyPowerSourcesInfo(); - - *level = -1; - *isOnAC = AC_ERROR; - - if(NULL == power_sources) { - return; - } - - if(power_sources != NULL) { - CFArrayRef list = IOPSCopyPowerSourcesList(power_sources); - CFDictionaryRef battery = NULL; - int len; - - if(NULL == list) { - CFRelease(power_sources); - - return; - } - - len = CFArrayGetCount(list); - - /* Get the battery */ - for(int i = 0; i < len && battery == NULL; ++i) { - CFDictionaryRef candidate = IOPSGetPowerSourceDescription(power_sources, - CFArrayGetValueAtIndex(list, i)); /* GET rule */ - CFStringRef type; - - if(NULL != candidate) { - type = (CFStringRef) CFDictionaryGetValue(candidate, - CFSTR(kIOPSTransportTypeKey)); /* GET rule */ - - if(kCFCompareEqualTo == CFStringCompare(type, CFSTR(kIOPSInternalType), 0)) { - CFRetain(candidate); - battery = candidate; - } - } - } - - if(NULL != battery) { - /* Determine the AC state */ - CFStringRef power_state = CFDictionaryGetValue(battery, CFSTR(kIOPSPowerSourceStateKey)); - - *isOnAC = (kCFCompareEqualTo == CFStringCompare(power_state, CFSTR(kIOPSACPowerValue), 0)) - ? AC_PRESENT - : AC_ABSENT; - - /* Get the percentage remaining */ - double current; - double max; - - CFNumberGetValue(CFDictionaryGetValue(battery, CFSTR(kIOPSCurrentCapacityKey)), - kCFNumberDoubleType, ¤t); - CFNumberGetValue(CFDictionaryGetValue(battery, CFSTR(kIOPSMaxCapacityKey)), - kCFNumberDoubleType, &max); - - *level = (current * 100.0) / max; - - CFRelease(battery); - } - - CFRelease(list); - CFRelease(power_sources); - } -} diff --git a/darwin/Battery.h b/darwin/Battery.h deleted file mode 100644 index 21a1579..0000000 --- a/darwin/Battery.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef HEADER_Battery -#define HEADER_Battery - -void Battery_getData(double* level, ACPresence* isOnAC); - -#endif diff --git a/darwin/DarwinCRT.c b/darwin/DarwinCRT.c deleted file mode 100644 index 2191f30..0000000 --- a/darwin/DarwinCRT.c +++ /dev/null @@ -1,34 +0,0 @@ -/* -htop - DarwinCRT.c -(C) 2014 Hisham H. Muhammad -Released under the GNU GPL, see the COPYING file -in the source distribution for its full text. -*/ - -#include "config.h" -#include "CRT.h" -#include -#include -#include - -void CRT_handleSIGSEGV(int sgn) { - (void) sgn; - CRT_done(); - #ifdef __APPLE__ - fprintf(stderr, "\n\nhtop " VERSION " aborting. Please report bug at https://htop.dev\n"); - #ifdef HAVE_EXECINFO_H - size_t size = backtrace(backtraceArray, sizeof(backtraceArray) / sizeof(void *)); - fprintf(stderr, "\n Please include in your report the following backtrace: \n"); - backtrace_symbols_fd(backtraceArray, size, 2); - fprintf(stderr, "\nAdditionally, in order to make the above backtrace useful,"); - fprintf(stderr, "\nplease also run the following command to generate a disassembly of your binary:"); - fprintf(stderr, "\n\n otool -tvV `which htop` > ~/htop.otool"); - fprintf(stderr, "\n\nand then attach the file ~/htop.otool to your bug report."); - fprintf(stderr, "\n\nThank you for helping to improve htop!\n\n"); - #endif - #else - fprintf(stderr, "\nUnfortunately, you seem to be using an unsupported platform!"); - fprintf(stderr, "\nPlease contact your platform package maintainer!\n\n"); - #endif - abort(); -} diff --git a/darwin/DarwinCRT.h b/darwin/DarwinCRT.h deleted file mode 100644 index 5e08422..0000000 --- a/darwin/DarwinCRT.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef HEADER_DarwinCRT -#define HEADER_DarwinCRT -/* -htop - DarwinCRT.h -(C) 2014 Hisham H. Muhammad -Released under the GNU GPL, see the COPYING file -in the source distribution for its full text. -*/ - -void CRT_handleSIGSEGV(int sgn); - -#endif diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index 430b2b4..f3c3425 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -1,22 +1,23 @@ /* htop - DarwinProcess.c (C) 2015 Hisham H. Muhammad -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 "Process.h" #include "DarwinProcess.h" -#include #include -#include #include - +#include +#include #include +#include "CRT.h" +#include "Process.h" + -ProcessClass DarwinProcess_class = { +const ProcessClass DarwinProcess_class = { .super = { .extends = Class(Process), .display = Process_display, @@ -26,7 +27,7 @@ ProcessClass DarwinProcess_class = { .writeField = Process_writeField, }; -DarwinProcess* DarwinProcess_new(Settings* settings) { +Process* DarwinProcess_new(const Settings* settings) { DarwinProcess* this = xCalloc(1, sizeof(DarwinProcess)); Object_setClass(this, Class(DarwinProcess)); Process_init(&this->super, settings); @@ -35,7 +36,7 @@ DarwinProcess* DarwinProcess_new(Settings* settings) { this->stime = 0; this->taskAccess = true; - return this; + return &this->super; } void Process_delete(Object* cast) { @@ -45,20 +46,12 @@ void Process_delete(Object* cast) { free(this); } -bool Process_isThread(Process* this) { +bool Process_isThread(const Process* this) { (void) this; return false; } -void DarwinProcess_setStartTime(Process *proc, struct extern_proc *ep, time_t now) { - struct tm date; - - proc->starttime_ctime = ep->p_starttime.tv_sec; - (void) localtime_r(&proc->starttime_ctime, &date); - strftime(proc->starttime_show, 7, ((proc->starttime_ctime > now - 86400) ? "%R " : "%b%d "), &date); -} - -char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int* basenameOffset) { +static char* DarwinProcess_getCmdLine(const struct kinfo_proc* k, int* basenameOffset) { /* This function is from the old Mac version of htop. Originally from ps? */ int mib[3], argmax, nargs, c = 0; size_t size; @@ -74,7 +67,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int* basenameOffset) { } /* Allocate space for the arguments. */ - procargs = ( char * ) xMalloc( argmax ); + procargs = (char*)xMalloc(argmax); if ( procargs == NULL ) { goto ERROR_A; } @@ -164,12 +157,12 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int* basenameOffset) { /* Convert previous '\0'. */ *np = ' '; } - /* Note location of current '\0'. */ - np = cp; - if (*basenameOffset == 0) { - *basenameOffset = cp - sp; - } - } + /* Note location of current '\0'. */ + np = cp; + if (*basenameOffset == 0) { + *basenameOffset = cp - sp; + } + } } /* @@ -201,8 +194,8 @@ ERROR_A: return retval; } -void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t now, bool exists) { - struct extern_proc *ep = &ps->kp_proc; +void DarwinProcess_setFromKInfoProc(Process* proc, const struct kinfo_proc* ps, bool exists) { + const struct extern_proc* ep = &ps->kp_proc; /* UNSET HERE : * @@ -211,14 +204,14 @@ void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t * nlwp * percent_cpu * percent_mem - * m_size + * m_virt * m_resident * minflt * majflt */ /* First, the "immutable" parts */ - if(!exists) { + if (!exists) { /* Set the PID/PGID/etc. */ proc->pid = ep->p_pid; proc->ppid = ps->kp_eproc.e_ppid; @@ -231,7 +224,9 @@ void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t /* e_tdev == -1 for "no device" */ proc->tty_nr = ps->kp_eproc.e_tdev & 0xff; /* TODO tty_nr is unsigned */ - DarwinProcess_setStartTime(proc, ep, now); + proc->starttime_ctime = ep->p_starttime.tv_sec; + Process_fillStarttimeBuffer(proc); + proc->comm = DarwinProcess_getCmdLine(ps, &(proc->basenameOffset)); } @@ -245,16 +240,16 @@ void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t proc->updated = true; } -void DarwinProcess_setFromLibprocPidinfo(DarwinProcess *proc, DarwinProcessList *dpl) { +void DarwinProcess_setFromLibprocPidinfo(DarwinProcess* proc, DarwinProcessList* dpl) { struct proc_taskinfo pti; - if(sizeof(pti) == proc_pidinfo(proc->super.pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti))) { - if(0 != proc->utime || 0 != proc->stime) { + if (sizeof(pti) == proc_pidinfo(proc->super.pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti))) { + if (0 != proc->utime || 0 != proc->stime) { uint64_t diff = (pti.pti_total_system - proc->stime) - + (pti.pti_total_user - proc->utime); + + (pti.pti_total_user - proc->utime); proc->super.percent_cpu = (double)diff * (double)dpl->super.cpuCount - / ((double)dpl->global_diff * 100000.0); + / ((double)dpl->global_diff * 100000.0); // fprintf(stderr, "%f %llu %llu %llu %llu %llu\n", proc->super.percent_cpu, // proc->stime, proc->utime, pti.pti_total_system, pti.pti_total_user, dpl->global_diff); @@ -263,11 +258,11 @@ void DarwinProcess_setFromLibprocPidinfo(DarwinProcess *proc, DarwinProcessList proc->super.time = (pti.pti_total_system + pti.pti_total_user) / 10000000; proc->super.nlwp = pti.pti_threadnum; - proc->super.m_size = pti.pti_virtual_size / 1024 / PAGE_SIZE_KB; - proc->super.m_resident = pti.pti_resident_size / 1024 / PAGE_SIZE_KB; + proc->super.m_virt = pti.pti_virtual_size / CRT_pageSize; + proc->super.m_resident = pti.pti_resident_size / CRT_pageSize; proc->super.majflt = pti.pti_faults; proc->super.percent_mem = (double)pti.pti_resident_size * 100.0 - / (double)dpl->host_info.max_mem; + / (double)dpl->host_info.max_mem; proc->stime = pti.pti_total_system; proc->utime = pti.pti_total_user; @@ -284,7 +279,7 @@ void DarwinProcess_setFromLibprocPidinfo(DarwinProcess *proc, DarwinProcessList * Based on: http://stackoverflow.com/questions/6788274/ios-mac-cpu-usage-for-thread * and https://github.com/max-horvath/htop-osx/blob/e86692e869e30b0bc7264b3675d2a4014866ef46/ProcessList.c */ -void DarwinProcess_scanThreads(DarwinProcess *dp) { +void DarwinProcess_scanThreads(DarwinProcess* dp) { Process* proc = (Process*) dp; kern_return_t ret; diff --git a/darwin/DarwinProcess.h b/darwin/DarwinProcess.h index cf76fa8..98897c9 100644 --- a/darwin/DarwinProcess.h +++ b/darwin/DarwinProcess.h @@ -3,14 +3,15 @@ /* htop - DarwinProcess.h (C) 2015 Hisham H. Muhammad -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 "Settings.h" +#include + #include "DarwinProcessList.h" +#include "Settings.h" -#include typedef struct DarwinProcess_ { Process super; @@ -20,27 +21,23 @@ typedef struct DarwinProcess_ { bool taskAccess; } DarwinProcess; -extern ProcessClass DarwinProcess_class; +extern const ProcessClass DarwinProcess_class; -DarwinProcess* DarwinProcess_new(Settings* settings); +Process* DarwinProcess_new(const Settings* settings); void Process_delete(Object* cast); -bool Process_isThread(Process* this); - -void DarwinProcess_setStartTime(Process *proc, struct extern_proc *ep, time_t now); - -char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int* basenameOffset); +bool Process_isThread(const Process* this); -void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t now, bool exists); +void DarwinProcess_setFromKInfoProc(Process* proc, const struct kinfo_proc* ps, bool exists); -void DarwinProcess_setFromLibprocPidinfo(DarwinProcess *proc, DarwinProcessList *dpl); +void DarwinProcess_setFromLibprocPidinfo(DarwinProcess* proc, DarwinProcessList* dpl); /* * Scan threads for process state information. * Based on: http://stackoverflow.com/questions/6788274/ios-mac-cpu-usage-for-thread * and https://github.com/max-horvath/htop-osx/blob/e86692e869e30b0bc7264b3675d2a4014866ef46/ProcessList.c */ -void DarwinProcess_scanThreads(DarwinProcess *dp); +void DarwinProcess_scanThreads(DarwinProcess* dp); #endif diff --git a/darwin/DarwinProcessList.c b/darwin/DarwinProcessList.c index 39d99c1..ae1efb1 100644 --- a/darwin/DarwinProcessList.c +++ b/darwin/DarwinProcessList.c @@ -1,33 +1,36 @@ /* htop - DarwinProcessList.c (C) 2014 Hisham H. Muhammad -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 "ProcessList.h" -#include "DarwinProcess.h" #include "DarwinProcessList.h" -#include "CRT.h" -#include "zfs/ZfsArcStats.h" -#include "zfs/openzfs_sysctl.h" +#include +#include +#include +#include +#include #include #include #include -#include -#include -#include #include -#include +#include #include -#include + +#include "CRT.h" +#include "DarwinProcess.h" +#include "ProcessList.h" +#include "zfs/openzfs_sysctl.h" +#include "zfs/ZfsArcStats.h" + struct kern { - short int version[3]; + short int version[3]; }; -void GetKernelVersion(struct kern *k) { +static void GetKernelVersion(struct kern* k) { static short int version_[3] = {0}; if (!version_[0]) { // just in case it fails someday @@ -35,9 +38,11 @@ void GetKernelVersion(struct kern *k) { char str[256] = {0}; size_t size = sizeof(str); int ret = sysctlbyname("kern.osrelease", str, &size, NULL, 0); - if (ret == 0) sscanf(str, "%hd.%hd.%hd", &version_[0], &version_[1], &version_[2]); - } - memcpy(k->version, version_, sizeof(version_)); + if (ret == 0) { + sscanf(str, "%hd.%hd.%hd", &version_[0], &version_[1], &version_[2]); + } + } + memcpy(k->version, version_, sizeof(version_)); } /* compare the given os version with the one installed returns: @@ -45,77 +50,85 @@ void GetKernelVersion(struct kern *k) { positive value if less than the installed version negative value if more than the installed version */ -int CompareKernelVersion(short int major, short int minor, short int component) { - struct kern k; - GetKernelVersion(&k); - if ( k.version[0] != major) return k.version[0] - major; - if ( k.version[1] != minor) return k.version[1] - minor; - if ( k.version[2] != component) return k.version[2] - component; - return 0; +static int CompareKernelVersion(short int major, short int minor, short int component) { + struct kern k; + GetKernelVersion(&k); + + if (k.version[0] != major) { + return k.version[0] - major; + } + if (k.version[1] != minor) { + return k.version[1] - minor; + } + if (k.version[2] != component) { + return k.version[2] - component; + } + + return 0; } -void ProcessList_getHostInfo(host_basic_info_data_t *p) { +static void ProcessList_getHostInfo(host_basic_info_data_t* p) { mach_msg_type_number_t info_size = HOST_BASIC_INFO_COUNT; - if(0 != host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)p, &info_size)) { - CRT_fatalError("Unable to retrieve host info\n"); + if (0 != host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)p, &info_size)) { + CRT_fatalError("Unable to retrieve host info\n"); } } -void ProcessList_freeCPULoadInfo(processor_cpu_load_info_t *p) { - if(NULL != p && NULL != *p) { - if(0 != munmap(*p, vm_page_size)) { - CRT_fatalError("Unable to free old CPU load information\n"); - } - *p = NULL; +static void ProcessList_freeCPULoadInfo(processor_cpu_load_info_t* p) { + if (NULL != p && NULL != *p) { + if (0 != munmap(*p, vm_page_size)) { + CRT_fatalError("Unable to free old CPU load information\n"); + } + *p = NULL; } } -unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t *p) { +static unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t* p) { mach_msg_type_number_t info_size = sizeof(processor_cpu_load_info_t); unsigned cpu_count; // TODO Improving the accuracy of the load counts woule help a lot. - if(0 != host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, (processor_info_array_t *)p, &info_size)) { - CRT_fatalError("Unable to retrieve CPU info\n"); + if (0 != host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, (processor_info_array_t*)p, &info_size)) { + CRT_fatalError("Unable to retrieve CPU info\n"); } return cpu_count; } -void ProcessList_getVMStats(vm_statistics_t p) { - mach_msg_type_number_t info_size = HOST_VM_INFO_COUNT; +static void ProcessList_getVMStats(vm_statistics_t p) { + mach_msg_type_number_t info_size = HOST_VM_INFO_COUNT; - if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)p, &info_size) != 0) - CRT_fatalError("Unable to retrieve VM statistics\n"); + if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)p, &info_size) != 0) { + CRT_fatalError("Unable to retrieve VM statistics\n"); + } } -struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) { +static struct kinfo_proc* ProcessList_getKInfoProcs(size_t* count) { int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; - struct kinfo_proc *processes = NULL; + struct kinfo_proc* processes = NULL; - /* Note the two calls to sysctl(). One to get length and one to get the - * data. This -does- mean that the second call could end up with a missing - * process entry or two. - */ - *count = 0; - if (sysctl(mib, 4, NULL, count, NULL, 0) < 0) - CRT_fatalError("Unable to get size of kproc_infos"); + for (int retry = 3; retry > 0; retry--) { + size_t size = 0; + if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0 || size == 0) { + CRT_fatalError("Unable to get size of kproc_infos"); + } - processes = xMalloc(*count); - if (processes == NULL) - CRT_fatalError("Out of memory for kproc_infos"); + processes = xRealloc(processes, size); - if (sysctl(mib, 4, processes, count, NULL, 0) < 0) - CRT_fatalError("Unable to get kinfo_procs"); + if (sysctl(mib, 4, processes, &size, NULL, 0) == 0) { + *count = size / sizeof(struct kinfo_proc); + return processes; + } - *count = *count / sizeof(struct kinfo_proc); + if (errno != ENOMEM) + break; + } - return processes; + CRT_fatalError("Unable to get kinfo_procs"); } ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) { - size_t len; DarwinProcessList* this = xCalloc(1, sizeof(DarwinProcessList)); ProcessList_init(&this->super, Class(Process), usersTable, pidMatchList, userId); @@ -145,67 +158,69 @@ void ProcessList_delete(ProcessList* this) { free(this); } -void ProcessList_goThroughEntries(ProcessList* super) { - DarwinProcessList *dpl = (DarwinProcessList *)super; - bool preExisting = true; - struct kinfo_proc *ps; - size_t count; - DarwinProcess *proc; - struct timeval tv; - - gettimeofday(&tv, NULL); /* Start processing time */ - - /* Update the global data (CPU times and VM stats) */ - ProcessList_freeCPULoadInfo(&dpl->prev_load); - dpl->prev_load = dpl->curr_load; - ProcessList_allocateCPULoadInfo(&dpl->curr_load); - ProcessList_getVMStats(&dpl->vm_stats); - openzfs_sysctl_updateArcStats(&dpl->zfs); - - /* Get the time difference */ - dpl->global_diff = 0; - for(int i = 0; i < dpl->super.cpuCount; ++i) { - for(size_t j = 0; j < CPU_STATE_MAX; ++j) { - dpl->global_diff += dpl->curr_load[i].cpu_ticks[j] - dpl->prev_load[i].cpu_ticks[j]; - } - } - - /* Clear the thread counts */ - super->kernelThreads = 0; - super->userlandThreads = 0; - super->totalTasks = 0; - super->runningTasks = 0; - - /* We use kinfo_procs for initial data since : - * - * 1) They always succeed. - * 2) The contain the basic information. - * - * We attempt to fill-in additional information with libproc. - */ - ps = ProcessList_getKInfoProcs(&count); - - for(size_t i = 0; i < count; ++i) { - proc = (DarwinProcess *)ProcessList_getProcess(super, ps[i].kp_proc.p_pid, &preExisting, (Process_New)DarwinProcess_new); - - DarwinProcess_setFromKInfoProc(&proc->super, &ps[i], tv.tv_sec, preExisting); - DarwinProcess_setFromLibprocPidinfo(proc, dpl); - - // Disabled for High Sierra due to bug in macOS High Sierra - bool isScanThreadSupported = ! ( CompareKernelVersion(17, 0, 0) >= 0 && CompareKernelVersion(17, 5, 0) < 0); - - if (isScanThreadSupported){ - DarwinProcess_scanThreads(proc); - } - - super->totalTasks += 1; - - if(!preExisting) { - proc->super.user = UsersTable_getRef(super->usersTable, proc->super.st_uid); - - ProcessList_add(super, &proc->super); - } - } - - free(ps); +void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { + DarwinProcessList* dpl = (DarwinProcessList*)super; + bool preExisting = true; + struct kinfo_proc* ps; + size_t count; + DarwinProcess* proc; + + /* Update the global data (CPU times and VM stats) */ + ProcessList_freeCPULoadInfo(&dpl->prev_load); + dpl->prev_load = dpl->curr_load; + ProcessList_allocateCPULoadInfo(&dpl->curr_load); + ProcessList_getVMStats(&dpl->vm_stats); + openzfs_sysctl_updateArcStats(&dpl->zfs); + + // in pause mode only gather global data for meters (CPU/memory/...) + if (pauseProcessUpdate) { + return; + } + + /* Get the time difference */ + dpl->global_diff = 0; + for (int i = 0; i < dpl->super.cpuCount; ++i) { + for (size_t j = 0; j < CPU_STATE_MAX; ++j) { + dpl->global_diff += dpl->curr_load[i].cpu_ticks[j] - dpl->prev_load[i].cpu_ticks[j]; + } + } + + /* Clear the thread counts */ + super->kernelThreads = 0; + super->userlandThreads = 0; + super->totalTasks = 0; + super->runningTasks = 0; + + /* We use kinfo_procs for initial data since : + * + * 1) They always succeed. + * 2) The contain the basic information. + * + * We attempt to fill-in additional information with libproc. + */ + ps = ProcessList_getKInfoProcs(&count); + + for (size_t i = 0; i < count; ++i) { + proc = (DarwinProcess*)ProcessList_getProcess(super, ps[i].kp_proc.p_pid, &preExisting, DarwinProcess_new); + + DarwinProcess_setFromKInfoProc(&proc->super, &ps[i], preExisting); + DarwinProcess_setFromLibprocPidinfo(proc, dpl); + + // Disabled for High Sierra due to bug in macOS High Sierra + bool isScanThreadSupported = ! ( CompareKernelVersion(17, 0, 0) >= 0 && CompareKernelVersion(17, 5, 0) < 0); + + if (isScanThreadSupported) { + DarwinProcess_scanThreads(proc); + } + + super->totalTasks += 1; + + if (!preExisting) { + proc->super.user = UsersTable_getRef(super->usersTable, proc->super.st_uid); + + ProcessList_add(super, &proc->super); + } + } + + free(ps); } diff --git a/darwin/DarwinProcessList.h b/darwin/DarwinProcessList.h index c6b2966..1ae2f2b 100644 --- a/darwin/DarwinProcessList.h +++ b/darwin/DarwinProcessList.h @@ -3,25 +3,16 @@ /* htop - DarwinProcessList.h (C) 2014 Hisham H. Muhammad -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. */ -struct kern; - -void GetKernelVersion(struct kern *k); - -/* compare the given os version with the one installed returns: -0 if equals the installed version -positive value if less than the installed version -negative value if more than the installed version -*/ -int CompareKernelVersion(short int major, short int minor, short int component); +#include +#include #include "ProcessList.h" #include "zfs/ZfsArcStats.h" -#include -#include + typedef struct DarwinProcessList_ { ProcessList super; @@ -37,20 +28,10 @@ typedef struct DarwinProcessList_ { ZfsArcStats zfs; } DarwinProcessList; -void ProcessList_getHostInfo(host_basic_info_data_t *p); - -void ProcessList_freeCPULoadInfo(processor_cpu_load_info_t *p); - -unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t *p); - -void ProcessList_getVMStats(vm_statistics_t p); - -struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count); - ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId); void ProcessList_delete(ProcessList* this); -void ProcessList_goThroughEntries(ProcessList* super); +void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate); #endif diff --git a/darwin/Platform.c b/darwin/Platform.c index a8ca45b..e24b67b 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -2,27 +2,36 @@ htop - darwin/Platform.c (C) 2014 Hisham H. Muhammad (C) 2015 David C. Hunt -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 "Platform.h" +#include "Macros.h" #include "CPUMeter.h" #include "MemoryMeter.h" #include "SwapMeter.h" #include "TasksMeter.h" #include "LoadAverageMeter.h" #include "ClockMeter.h" +#include "DateMeter.h" +#include "DateTimeMeter.h" #include "HostnameMeter.h" +#include "ProcessLocksScreen.h" #include "UptimeMeter.h" #include "zfs/ZfsArcMeter.h" #include "zfs/ZfsCompressedArcMeter.h" #include "DarwinProcessList.h" +#include #include +#include +#include +#include +#include -ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; +ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; const SignalItem Platform_signals[] = { { .name = " 0 Cancel", .number = 0 }, @@ -60,7 +69,7 @@ const SignalItem Platform_signals[] = { { .name = "31 SIGUSR2", .number = 31 }, }; -const unsigned int Platform_numberOfSignals = sizeof(Platform_signals)/sizeof(SignalItem); +const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals); ProcessFieldData Process_fields[] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, @@ -79,7 +88,7 @@ ProcessFieldData Process_fields[] = { [STARTTIME] = { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, }, [PROCESSOR] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, }, - [M_SIZE] = { .name = "M_SIZE", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, }, + [M_VIRT] = { .name = "M_VIRT", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, }, [M_RESIDENT] = { .name = "M_RESIDENT", .title = " RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, }, [ST_UID] = { .name = "ST_UID", .title = " UID ", .description = "User ID of the process owner", .flags = 0, }, [PERCENT_CPU] = { .name = "PERCENT_CPU", .title = "CPU% ", .description = "Percentage of the CPU time the process used in the last sampling", .flags = 0, }, @@ -91,9 +100,11 @@ ProcessFieldData Process_fields[] = { [100] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, }; -MeterClass* Platform_meterTypes[] = { +const MeterClass* const Platform_meterTypes[] = { &CPUMeter_class, &ClockMeter_class, + &DateMeter_class, + &DateTimeMeter_class, &LoadAverageMeter_class, &LoadMeter_class, &MemoryMeter_class, @@ -104,22 +115,37 @@ MeterClass* Platform_meterTypes[] = { &UptimeMeter_class, &AllCPUsMeter_class, &AllCPUs2Meter_class, + &AllCPUs4Meter_class, + &AllCPUs8Meter_class, &LeftCPUsMeter_class, &RightCPUsMeter_class, &LeftCPUs2Meter_class, &RightCPUs2Meter_class, + &LeftCPUs4Meter_class, + &RightCPUs4Meter_class, + &LeftCPUs8Meter_class, + &RightCPUs8Meter_class, &ZfsArcMeter_class, &ZfsCompressedArcMeter_class, &BlankMeter_class, NULL }; +int Platform_numberOfFields = 100; + +void Platform_init(void) { + /* no platform-specific setup needed */ +} + +void Platform_done(void) { + /* no platform-specific cleanup needed */ +} + void Platform_setBindings(Htop_Action* keys) { + /* no platform-specific key bindings */ (void) keys; } -int Platform_numberOfFields = 100; - int Platform_getUptime() { struct timeval bootTime, currTime; int mib[2] = { CTL_KERN, KERN_BOOTTIME }; @@ -137,7 +163,7 @@ int Platform_getUptime() { void Platform_getLoadAverage(double* one, double* five, double* fifteen) { double results[3]; - if(3 == getloadavg(results, 3)) { + if (3 == getloadavg(results, 3)) { *one = results[0]; *five = results[1]; *fifteen = results[2]; @@ -164,8 +190,8 @@ ProcessPidColumn Process_pidColumns[] = { }; static double Platform_setCPUAverageValues(Meter* mtr) { - DarwinProcessList *dpl = (DarwinProcessList *)mtr->pl; - int cpus = dpl->super.cpuCount; + const ProcessList* dpl = mtr->pl; + int cpus = dpl->cpuCount; double sumNice = 0.0; double sumNormal = 0.0; double sumKernel = 0.0; @@ -188,36 +214,37 @@ double Platform_setCPUValues(Meter* mtr, int cpu) { return Platform_setCPUAverageValues(mtr); } - DarwinProcessList *dpl = (DarwinProcessList *)mtr->pl; - processor_cpu_load_info_t prev = &dpl->prev_load[cpu-1]; - processor_cpu_load_info_t curr = &dpl->curr_load[cpu-1]; + const DarwinProcessList* dpl = (const DarwinProcessList*)mtr->pl; + const processor_cpu_load_info_t prev = &dpl->prev_load[cpu - 1]; + const processor_cpu_load_info_t curr = &dpl->curr_load[cpu - 1]; double total = 0; /* Take the sums */ - for(size_t i = 0; i < CPU_STATE_MAX; ++i) { + for (size_t i = 0; i < CPU_STATE_MAX; ++i) { total += (double)curr->cpu_ticks[i] - (double)prev->cpu_ticks[i]; } mtr->values[CPU_METER_NICE] - = ((double)curr->cpu_ticks[CPU_STATE_NICE] - (double)prev->cpu_ticks[CPU_STATE_NICE])* 100.0 / total; + = ((double)curr->cpu_ticks[CPU_STATE_NICE] - (double)prev->cpu_ticks[CPU_STATE_NICE]) * 100.0 / total; mtr->values[CPU_METER_NORMAL] - = ((double)curr->cpu_ticks[CPU_STATE_USER] - (double)prev->cpu_ticks[CPU_STATE_USER])* 100.0 / total; + = ((double)curr->cpu_ticks[CPU_STATE_USER] - (double)prev->cpu_ticks[CPU_STATE_USER]) * 100.0 / total; mtr->values[CPU_METER_KERNEL] - = ((double)curr->cpu_ticks[CPU_STATE_SYSTEM] - (double)prev->cpu_ticks[CPU_STATE_SYSTEM])* 100.0 / total; + = ((double)curr->cpu_ticks[CPU_STATE_SYSTEM] - (double)prev->cpu_ticks[CPU_STATE_SYSTEM]) * 100.0 / total; - Meter_setItems(mtr, 3); + mtr->curItems = 3; /* Convert to percent and return */ total = mtr->values[CPU_METER_NICE] + mtr->values[CPU_METER_NORMAL] + mtr->values[CPU_METER_KERNEL]; - mtr->values[CPU_METER_FREQUENCY] = -1; + mtr->values[CPU_METER_FREQUENCY] = NAN; + mtr->values[CPU_METER_TEMPERATURE] = NAN; return CLAMP(total, 0.0, 100.0); } void Platform_setMemoryValues(Meter* mtr) { - DarwinProcessList *dpl = (DarwinProcessList *)mtr->pl; - vm_statistics_t vm = &dpl->vm_stats; + const DarwinProcessList* dpl = (const DarwinProcessList*)mtr->pl; + const struct vm_statistics* vm = &dpl->vm_stats; double page_K = (double)vm_page_size / (double)1024; mtr->total = dpl->host_info.max_mem / 1024; @@ -227,23 +254,23 @@ void Platform_setMemoryValues(Meter* mtr) { } void Platform_setSwapValues(Meter* mtr) { - int mib[2] = {CTL_VM, VM_SWAPUSAGE}; - struct xsw_usage swapused; - size_t swlen = sizeof(swapused); - sysctl(mib, 2, &swapused, &swlen, NULL, 0); + int mib[2] = {CTL_VM, VM_SWAPUSAGE}; + struct xsw_usage swapused; + size_t swlen = sizeof(swapused); + sysctl(mib, 2, &swapused, &swlen, NULL, 0); - mtr->total = swapused.xsu_total / 1024; - mtr->values[0] = swapused.xsu_used / 1024; + mtr->total = swapused.xsu_total / 1024; + mtr->values[0] = swapused.xsu_used / 1024; } void Platform_setZfsArcValues(Meter* this) { - DarwinProcessList* dpl = (DarwinProcessList*) this->pl; + const DarwinProcessList* dpl = (const DarwinProcessList*) this->pl; ZfsArcMeter_readStats(this, &(dpl->zfs)); } void Platform_setZfsCompressedArcValues(Meter* this) { - DarwinProcessList* dpl = (DarwinProcessList*) this->pl; + const DarwinProcessList* dpl = (const DarwinProcessList*) this->pl; ZfsCompressedArcMeter_readStats(this, &(dpl->zfs)); } @@ -263,33 +290,33 @@ char* Platform_getProcessEnv(pid_t pid) { mib[0] = CTL_KERN; mib[1] = KERN_PROCARGS2; mib[2] = pid; - size_t bufsz = argmax; + bufsz = argmax; if (sysctl(mib, 3, buf, &bufsz, 0, 0) == 0) { if (bufsz > sizeof(int)) { char *p = buf, *endp = buf + bufsz; - int argc = *(int*)p; + int argc = *(int*)(void*)p; p += sizeof(int); // skip exe - p = strchr(p, 0)+1; + p = strchr(p, 0) + 1; // skip padding - while(!*p && p < endp) + while (!*p && p < endp) ++p; // skip argv - for (; argc-- && p < endp; p = strrchr(p, 0)+1) + for (; argc-- && p < endp; p = strrchr(p, 0) + 1) ; // skip padding - while(!*p && p < endp) + while (!*p && p < endp) ++p; size_t size = endp - p; - env = xMalloc(size+2); + env = xMalloc(size + 2); memcpy(env, p, size); env[size] = 0; - env[size+1] = 0; + env[size + 1] = 0; } } free(buf); @@ -298,3 +325,96 @@ char* Platform_getProcessEnv(pid_t pid) { return env; } + +char* Platform_getInodeFilename(pid_t pid, ino_t inode) { + (void)pid; + (void)inode; + return NULL; +} + +FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) { + (void)pid; + return NULL; +} + +bool Platform_getDiskIO(DiskIOData* data) { + // TODO + (void)data; + return false; +} + +bool Platform_getNetworkIO(unsigned long int* bytesReceived, + unsigned long int* packetsReceived, + unsigned long int* bytesTransmitted, + unsigned long int* packetsTransmitted) { + // TODO + *bytesReceived = 0; + *packetsReceived = 0; + *bytesTransmitted = 0; + *packetsTransmitted = 0; + return false; +} + +void Platform_getBattery(double* percent, ACPresence* isOnAC) { + CFTypeRef power_sources = IOPSCopyPowerSourcesInfo(); + + *percent = NAN; + *isOnAC = AC_ERROR; + + if (NULL == power_sources) + return; + + CFArrayRef list = IOPSCopyPowerSourcesList(power_sources); + CFDictionaryRef battery = NULL; + int len; + + if (NULL == list) { + CFRelease(power_sources); + + return; + } + + len = CFArrayGetCount(list); + + /* Get the battery */ + for (int i = 0; i < len && battery == NULL; ++i) { + CFDictionaryRef candidate = IOPSGetPowerSourceDescription(power_sources, + CFArrayGetValueAtIndex(list, i)); /* GET rule */ + CFStringRef type; + + if (NULL != candidate) { + type = (CFStringRef) CFDictionaryGetValue(candidate, + CFSTR(kIOPSTransportTypeKey)); /* GET rule */ + + if (kCFCompareEqualTo == CFStringCompare(type, CFSTR(kIOPSInternalType), 0)) { + CFRetain(candidate); + battery = candidate; + } + } + } + + if (NULL != battery) { + /* Determine the AC state */ + CFStringRef power_state = CFDictionaryGetValue(battery, CFSTR(kIOPSPowerSourceStateKey)); + + *isOnAC = (kCFCompareEqualTo == CFStringCompare(power_state, CFSTR(kIOPSACPowerValue), 0)) + ? AC_PRESENT + : AC_ABSENT; + + /* Get the percentage remaining */ + double current; + double max; + + CFNumberGetValue(CFDictionaryGetValue(battery, CFSTR(kIOPSCurrentCapacityKey)), + kCFNumberDoubleType, ¤t); + CFNumberGetValue(CFDictionaryGetValue(battery, CFSTR(kIOPSMaxCapacityKey)), + kCFNumberDoubleType, &max); + + *percent = (current * 100.0) / max; + + CFRelease(battery); + } + + CFRelease(list); + CFRelease(power_sources); +} diff --git a/darwin/Platform.h b/darwin/Platform.h index 7dd4ae6..e1f8355 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -4,35 +4,44 @@ htop - darwin/Platform.h (C) 2014 Hisham H. Muhammad (C) 2015 David C. Hunt -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 +#include + #include "Action.h" -#include "SignalsPanel.h" -#include "CPUMeter.h" #include "BatteryMeter.h" +#include "CPUMeter.h" #include "DarwinProcess.h" +#include "DiskIOMeter.h" +#include "ProcessLocksScreen.h" +#include "SignalsPanel.h" + +extern ProcessFieldData Process_fields[]; extern ProcessField Platform_defaultFields[]; +extern int Platform_numberOfFields; + extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals; -extern ProcessFieldData Process_fields[]; +extern const MeterClass* const Platform_meterTypes[]; -extern MeterClass* Platform_meterTypes[]; +void Platform_init(void); -void Platform_setBindings(Htop_Action* keys); +void Platform_done(void); -extern int Platform_numberOfFields; +void Platform_setBindings(Htop_Action* keys); -int Platform_getUptime(); +int Platform_getUptime(void); void Platform_getLoadAverage(double* one, double* five, double* fifteen); -int Platform_getMaxPid(); +int Platform_getMaxPid(void); extern ProcessPidColumn Process_pidColumns[]; @@ -48,4 +57,17 @@ void Platform_setZfsCompressedArcValues(Meter* this); char* Platform_getProcessEnv(pid_t pid); +char* Platform_getInodeFilename(pid_t pid, ino_t inode); + +FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid); + +bool Platform_getDiskIO(DiskIOData* data); + +bool Platform_getNetworkIO(unsigned long int* bytesReceived, + unsigned long int* packetsReceived, + unsigned long int* bytesTransmitted, + unsigned long int* packetsTransmitted); + +void Platform_getBattery(double *percent, ACPresence *isOnAC); + #endif -- cgit v1.2.3