| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 1 | /* |
| 2 | htop - DarwinProcessList.c |
| 3 | (C) 2014 Hisham H. Muhammad |
| Daniel Lange | 079c2ab | 2020-10-05 09:51:32 +0200 | [diff] [blame] | 4 | Released under the GNU GPLv2, see the COPYING file |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 5 | in the source distribution for its full text. |
| 6 | */ |
| 7 | |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 8 | #include "DarwinProcessList.h" |
| 9 | |
| Christian Göttsche | 72df930 | 2020-11-17 15:17:19 +0100 | [diff] [blame] | 10 | #include <errno.h> |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 11 | #include <libproc.h> |
| 12 | #include <stdbool.h> |
| 13 | #include <stdio.h> |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 16 | #include <unistd.h> |
| David Hunt | 57ab332 | 2015-07-13 21:02:40 -0500 | [diff] [blame] | 17 | #include <utmpx.h> |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 18 | #include <sys/mman.h> |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 19 | #include <sys/sysctl.h> |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 20 | |
| 21 | #include "CRT.h" |
| 22 | #include "DarwinProcess.h" |
| Alexander Momchilov | 67ccd6b | 2020-12-08 23:12:44 -0500 | [diff] [blame] | 23 | #include "Platform.h" |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 24 | #include "ProcessList.h" |
| Nathan Scott | adaf748 | 2021-03-04 09:00:34 +1100 | [diff] [blame^] | 25 | #include "generic/openzfs_sysctl.h" |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 26 | #include "zfs/ZfsArcStats.h" |
| 27 | |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 28 | |
| 29 | struct kern { |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 30 | short int version[3]; |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 31 | }; |
| 32 | |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 33 | static void GetKernelVersion(struct kern* k) { |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 34 | static short int version_[3] = {0}; |
| 35 | if (!version_[0]) { |
| 36 | // just in case it fails someday |
| 37 | version_[0] = version_[1] = version_[2] = -1; |
| 38 | char str[256] = {0}; |
| 39 | size_t size = sizeof(str); |
| 40 | int ret = sysctlbyname("kern.osrelease", str, &size, NULL, 0); |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 41 | if (ret == 0) { |
| 42 | sscanf(str, "%hd.%hd.%hd", &version_[0], &version_[1], &version_[2]); |
| 43 | } |
| 44 | } |
| 45 | memcpy(k->version, version_, sizeof(version_)); |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| pmalhaire | 0505a7c | 2018-04-06 00:41:22 +0200 | [diff] [blame] | 48 | /* compare the given os version with the one installed returns: |
| 49 | 0 if equals the installed version |
| 50 | positive value if less than the installed version |
| 51 | negative value if more than the installed version |
| 52 | */ |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 53 | static int CompareKernelVersion(short int major, short int minor, short int component) { |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 54 | struct kern k; |
| 55 | GetKernelVersion(&k); |
| 56 | |
| 57 | if (k.version[0] != major) { |
| 58 | return k.version[0] - major; |
| 59 | } |
| 60 | if (k.version[1] != minor) { |
| 61 | return k.version[1] - minor; |
| 62 | } |
| 63 | if (k.version[2] != component) { |
| 64 | return k.version[2] - component; |
| 65 | } |
| 66 | |
| 67 | return 0; |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 68 | } |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 69 | |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 70 | static void ProcessList_getHostInfo(host_basic_info_data_t* p) { |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 71 | mach_msg_type_number_t info_size = HOST_BASIC_INFO_COUNT; |
| 72 | |
| Benny Baumann | 374edb9 | 2020-10-31 20:52:20 +0100 | [diff] [blame] | 73 | if (0 != host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)p, &info_size)) { |
| Christian Göttsche | f878f30 | 2020-12-19 21:25:18 +0100 | [diff] [blame] | 74 | CRT_fatalError("Unable to retrieve host info"); |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 78 | static void ProcessList_freeCPULoadInfo(processor_cpu_load_info_t* p) { |
| Benny Baumann | 374edb9 | 2020-10-31 20:52:20 +0100 | [diff] [blame] | 79 | if (NULL != p && NULL != *p) { |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 80 | if (0 != munmap(*p, vm_page_size)) { |
| Christian Göttsche | f878f30 | 2020-12-19 21:25:18 +0100 | [diff] [blame] | 81 | CRT_fatalError("Unable to free old CPU load information"); |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 82 | } |
| 83 | *p = NULL; |
| David Hunt | 7f3faa2 | 2015-07-13 16:53:46 -0500 | [diff] [blame] | 84 | } |
| David Hunt | 6463ea2 | 2015-07-13 17:09:18 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 87 | static unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t* p) { |
| David Hunt | 6463ea2 | 2015-07-13 17:09:18 -0500 | [diff] [blame] | 88 | mach_msg_type_number_t info_size = sizeof(processor_cpu_load_info_t); |
| 89 | unsigned cpu_count; |
| 90 | |
| David Hunt | 907f829 | 2015-07-14 11:46:16 -0500 | [diff] [blame] | 91 | // TODO Improving the accuracy of the load counts woule help a lot. |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 92 | if (0 != host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, (processor_info_array_t*)p, &info_size)) { |
| Christian Göttsche | f878f30 | 2020-12-19 21:25:18 +0100 | [diff] [blame] | 93 | CRT_fatalError("Unable to retrieve CPU info"); |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return cpu_count; |
| 97 | } |
| 98 | |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 99 | static void ProcessList_getVMStats(vm_statistics_t p) { |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 100 | mach_msg_type_number_t info_size = HOST_VM_INFO_COUNT; |
| David Hunt | 57ab332 | 2015-07-13 21:02:40 -0500 | [diff] [blame] | 101 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 102 | if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)p, &info_size) != 0) { |
| Christian Göttsche | f878f30 | 2020-12-19 21:25:18 +0100 | [diff] [blame] | 103 | CRT_fatalError("Unable to retrieve VM statistics"); |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 104 | } |
| David Hunt | 57ab332 | 2015-07-13 21:02:40 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 107 | static struct kinfo_proc* ProcessList_getKInfoProcs(size_t* count) { |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 108 | int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; |
| Benny Baumann | 61e14d4 | 2020-10-31 23:28:02 +0100 | [diff] [blame] | 109 | struct kinfo_proc* processes = NULL; |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 110 | |
| Christian Göttsche | 72df930 | 2020-11-17 15:17:19 +0100 | [diff] [blame] | 111 | for (int retry = 3; retry > 0; retry--) { |
| 112 | size_t size = 0; |
| 113 | if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0 || size == 0) { |
| 114 | CRT_fatalError("Unable to get size of kproc_infos"); |
| 115 | } |
| 116 | |
| 117 | processes = xRealloc(processes, size); |
| 118 | |
| 119 | if (sysctl(mib, 4, processes, &size, NULL, 0) == 0) { |
| 120 | *count = size / sizeof(struct kinfo_proc); |
| 121 | return processes; |
| 122 | } |
| 123 | |
| 124 | if (errno != ENOMEM) |
| 125 | break; |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 126 | } |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 127 | |
| Christian Göttsche | 72df930 | 2020-11-17 15:17:19 +0100 | [diff] [blame] | 128 | CRT_fatalError("Unable to get kinfo_procs"); |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| Nathan Scott | 4597332 | 2020-09-09 19:38:15 +1000 | [diff] [blame] | 131 | ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) { |
| Hisham | b54d2dd | 2016-02-02 15:53:02 +0100 | [diff] [blame] | 132 | DarwinProcessList* this = xCalloc(1, sizeof(DarwinProcessList)); |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 133 | |
| Dániel Bakai | 3655b6c | 2020-12-13 15:54:13 +0100 | [diff] [blame] | 134 | ProcessList_init(&this->super, Class(DarwinProcess), usersTable, pidMatchList, userId); |
| David Hunt | 57ab332 | 2015-07-13 21:02:40 -0500 | [diff] [blame] | 135 | |
| 136 | /* Initialize the CPU information */ |
| David Hunt | 6463ea2 | 2015-07-13 17:09:18 -0500 | [diff] [blame] | 137 | this->super.cpuCount = ProcessList_allocateCPULoadInfo(&this->prev_load); |
| David Hunt | 7f3faa2 | 2015-07-13 16:53:46 -0500 | [diff] [blame] | 138 | ProcessList_getHostInfo(&this->host_info); |
| David Hunt | 6463ea2 | 2015-07-13 17:09:18 -0500 | [diff] [blame] | 139 | ProcessList_allocateCPULoadInfo(&this->curr_load); |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 140 | |
| David Hunt | 57ab332 | 2015-07-13 21:02:40 -0500 | [diff] [blame] | 141 | /* Initialize the VM statistics */ |
| 142 | ProcessList_getVMStats(&this->vm_stats); |
| 143 | |
| Ross Williams | fc8e9a2 | 2019-07-07 17:30:37 -0400 | [diff] [blame] | 144 | /* Initialize the ZFS kstats, if zfs.kext loaded */ |
| Ross Williams | e450b58 | 2019-09-03 18:21:33 +0000 | [diff] [blame] | 145 | openzfs_sysctl_init(&this->zfs); |
| Ross Williams | a88d2e3 | 2019-07-07 23:27:00 +0000 | [diff] [blame] | 146 | openzfs_sysctl_updateArcStats(&this->zfs); |
| Ross Williams | fc8e9a2 | 2019-07-07 17:30:37 -0400 | [diff] [blame] | 147 | |
| David Hunt | 57ab332 | 2015-07-13 21:02:40 -0500 | [diff] [blame] | 148 | this->super.kernelThreads = 0; |
| 149 | this->super.userlandThreads = 0; |
| 150 | this->super.totalTasks = 0; |
| 151 | this->super.runningTasks = 0; |
| 152 | |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 153 | return &this->super; |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void ProcessList_delete(ProcessList* this) { |
| 157 | ProcessList_done(this); |
| 158 | free(this); |
| 159 | } |
| 160 | |
| Alexander Momchilov | 67ccd6b | 2020-12-08 23:12:44 -0500 | [diff] [blame] | 161 | static double ticksToNanoseconds(const double ticks) { |
| 162 | const double nanos_per_sec = 1e9; |
| Luke Groeninger | a150a81 | 2021-01-02 00:45:53 -0600 | [diff] [blame] | 163 | return (ticks / Platform_timebaseToNS) * (nanos_per_sec / (double) Platform_clockTicksPerSec); |
| Alexander Momchilov | 67ccd6b | 2020-12-08 23:12:44 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| Christian Göttsche | 96e2a42 | 2020-10-13 16:03:37 +0200 | [diff] [blame] | 166 | void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 167 | DarwinProcessList* dpl = (DarwinProcessList*)super; |
| 168 | bool preExisting = true; |
| 169 | struct kinfo_proc* ps; |
| 170 | size_t count; |
| 171 | DarwinProcess* proc; |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 172 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 173 | /* Update the global data (CPU times and VM stats) */ |
| 174 | ProcessList_freeCPULoadInfo(&dpl->prev_load); |
| 175 | dpl->prev_load = dpl->curr_load; |
| 176 | ProcessList_allocateCPULoadInfo(&dpl->curr_load); |
| 177 | ProcessList_getVMStats(&dpl->vm_stats); |
| 178 | openzfs_sysctl_updateArcStats(&dpl->zfs); |
| David Hunt | 57ab332 | 2015-07-13 21:02:40 -0500 | [diff] [blame] | 179 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 180 | // in pause mode only gather global data for meters (CPU/memory/...) |
| 181 | if (pauseProcessUpdate) { |
| 182 | return; |
| 183 | } |
| Christian Göttsche | 96e2a42 | 2020-10-13 16:03:37 +0200 | [diff] [blame] | 184 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 185 | /* Get the time difference */ |
| 186 | dpl->global_diff = 0; |
| 187 | for (int i = 0; i < dpl->super.cpuCount; ++i) { |
| 188 | for (size_t j = 0; j < CPU_STATE_MAX; ++j) { |
| 189 | dpl->global_diff += dpl->curr_load[i].cpu_ticks[j] - dpl->prev_load[i].cpu_ticks[j]; |
| 190 | } |
| 191 | } |
| David Hunt | 907f829 | 2015-07-14 11:46:16 -0500 | [diff] [blame] | 192 | |
| Alexander Momchilov | 67ccd6b | 2020-12-08 23:12:44 -0500 | [diff] [blame] | 193 | const double time_interval = ticksToNanoseconds(dpl->global_diff) / (double) dpl->super.cpuCount; |
| 194 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 195 | /* Clear the thread counts */ |
| 196 | super->kernelThreads = 0; |
| 197 | super->userlandThreads = 0; |
| 198 | super->totalTasks = 0; |
| 199 | super->runningTasks = 0; |
| David Hunt | 7f3faa2 | 2015-07-13 16:53:46 -0500 | [diff] [blame] | 200 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 201 | /* We use kinfo_procs for initial data since : |
| 202 | * |
| 203 | * 1) They always succeed. |
| 204 | * 2) The contain the basic information. |
| 205 | * |
| 206 | * We attempt to fill-in additional information with libproc. |
| 207 | */ |
| 208 | ps = ProcessList_getKInfoProcs(&count); |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 209 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 210 | for (size_t i = 0; i < count; ++i) { |
| 211 | proc = (DarwinProcess*)ProcessList_getProcess(super, ps[i].kp_proc.p_pid, &preExisting, DarwinProcess_new); |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 212 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 213 | DarwinProcess_setFromKInfoProc(&proc->super, &ps[i], preExisting); |
| Alexander Momchilov | 67ccd6b | 2020-12-08 23:12:44 -0500 | [diff] [blame] | 214 | DarwinProcess_setFromLibprocPidinfo(proc, dpl, time_interval); |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 215 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 216 | // Disabled for High Sierra due to bug in macOS High Sierra |
| 217 | bool isScanThreadSupported = ! ( CompareKernelVersion(17, 0, 0) >= 0 && CompareKernelVersion(17, 5, 0) < 0); |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 218 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 219 | if (isScanThreadSupported) { |
| 220 | DarwinProcess_scanThreads(proc); |
| 221 | } |
| David Hunt | 57ab332 | 2015-07-13 21:02:40 -0500 | [diff] [blame] | 222 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 223 | super->totalTasks += 1; |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 224 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 225 | if (!preExisting) { |
| 226 | proc->super.user = UsersTable_getRef(super->usersTable, proc->super.st_uid); |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 227 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 228 | ProcessList_add(super, &proc->super); |
| 229 | } |
| 230 | } |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 231 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 232 | free(ps); |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 233 | } |