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