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