blob: 673d9e614f31f5aa74c1e36e71006c7acb495031 [file] [log] [blame]
David Hunt70e7c8d2015-07-12 13:47:43 -05001/*
2htop - DarwinProcessList.c
3(C) 2014 Hisham H. Muhammad
Daniel Lange079c2ab2020-10-05 09:51:32 +02004Released under the GNU GPLv2, see the COPYING file
David Hunt70e7c8d2015-07-12 13:47:43 -05005in the source distribution for its full text.
6*/
7
8#include "ProcessList.h"
9#include "DarwinProcess.h"
10#include "DarwinProcessList.h"
Hisham Muhammad5e0f1782015-11-02 10:22:10 -050011#include "CRT.h"
Ross Williamsa88d2e32019-07-07 23:27:00 +000012#include "zfs/ZfsArcStats.h"
13#include "zfs/openzfs_sysctl.h"
David Hunt70e7c8d2015-07-12 13:47:43 -050014
15#include <stdlib.h>
16#include <string.h>
David Hunt43ef7032015-07-13 01:17:14 -050017#include <unistd.h>
18#include <stdio.h>
19#include <libproc.h>
David Hunt7f3faa22015-07-13 16:53:46 -050020#include <sys/mman.h>
David Hunt57ab3322015-07-13 21:02:40 -050021#include <utmpx.h>
Hisham Muhammadbd93b2e2015-10-24 23:28:29 -040022#include <err.h>
pmalhaire23f96042018-03-25 22:04:16 +020023#include <sys/sysctl.h>
24#include <stdbool.h>
25
26struct kern {
Benny Baumannb23f8232020-10-31 22:14:27 +010027 short int version[3];
pmalhaire23f96042018-03-25 22:04:16 +020028};
29
Benny Baumann61e14d42020-10-31 23:28:02 +010030void GetKernelVersion(struct kern* k) {
pmalhaire23f96042018-03-25 22:04:16 +020031 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 Baumannb23f8232020-10-31 22:14:27 +010038 if (ret == 0) {
39 sscanf(str, "%hd.%hd.%hd", &version_[0], &version_[1], &version_[2]);
40 }
41 }
42 memcpy(k->version, version_, sizeof(version_));
pmalhaire23f96042018-03-25 22:04:16 +020043}
44
pmalhaire0505a7c2018-04-06 00:41:22 +020045/* compare the given os version with the one installed returns:
460 if equals the installed version
47positive value if less than the installed version
48negative value if more than the installed version
49*/
pmalhaire23f96042018-03-25 22:04:16 +020050int CompareKernelVersion(short int major, short int minor, short int component) {
Benny Baumannb23f8232020-10-31 22:14:27 +010051 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;
pmalhaire23f96042018-03-25 22:04:16 +020065}
David Hunt70e7c8d2015-07-12 13:47:43 -050066
Benny Baumann61e14d42020-10-31 23:28:02 +010067void ProcessList_getHostInfo(host_basic_info_data_t* p) {
David Hunt43ef7032015-07-13 01:17:14 -050068 mach_msg_type_number_t info_size = HOST_BASIC_INFO_COUNT;
69
Benny Baumann374edb92020-10-31 20:52:20 +010070 if (0 != host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)p, &info_size)) {
Benny Baumannb23f8232020-10-31 22:14:27 +010071 CRT_fatalError("Unable to retrieve host info\n");
David Hunt43ef7032015-07-13 01:17:14 -050072 }
73}
74
Benny Baumann61e14d42020-10-31 23:28:02 +010075void ProcessList_freeCPULoadInfo(processor_cpu_load_info_t* p) {
Benny Baumann374edb92020-10-31 20:52:20 +010076 if (NULL != p && NULL != *p) {
Benny Baumannb23f8232020-10-31 22:14:27 +010077 if (0 != munmap(*p, vm_page_size)) {
78 CRT_fatalError("Unable to free old CPU load information\n");
79 }
80 *p = NULL;
David Hunt7f3faa22015-07-13 16:53:46 -050081 }
David Hunt6463ea22015-07-13 17:09:18 -050082}
83
Benny Baumann61e14d42020-10-31 23:28:02 +010084unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t* p) {
David Hunt6463ea22015-07-13 17:09:18 -050085 mach_msg_type_number_t info_size = sizeof(processor_cpu_load_info_t);
86 unsigned cpu_count;
87
David Hunt907f8292015-07-14 11:46:16 -050088 // TODO Improving the accuracy of the load counts woule help a lot.
Benny Baumannb23f8232020-10-31 22:14:27 +010089 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 Hunt43ef7032015-07-13 01:17:14 -050091 }
92
93 return cpu_count;
94}
95
Michael Kleind3125102016-01-13 20:57:29 +010096void ProcessList_getVMStats(vm_statistics_t p) {
Benny Baumannb23f8232020-10-31 22:14:27 +010097 mach_msg_type_number_t info_size = HOST_VM_INFO_COUNT;
David Hunt57ab3322015-07-13 21:02:40 -050098
Benny Baumannb23f8232020-10-31 22:14:27 +010099 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 Hunt57ab3322015-07-13 21:02:40 -0500102}
103
Benny Baumann61e14d42020-10-31 23:28:02 +0100104struct kinfo_proc* ProcessList_getKInfoProcs(size_t* count) {
David Hunt43ef7032015-07-13 01:17:14 -0500105 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
Benny Baumann61e14d42020-10-31 23:28:02 +0100106 struct kinfo_proc* processes = NULL;
David Hunt43ef7032015-07-13 01:17:14 -0500107
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 McConville445222e2015-09-16 23:42:36 -0400113 if (sysctl(mib, 4, NULL, count, NULL, 0) < 0)
Michael McConvillecd3d2332015-11-01 13:26:57 -0500114 CRT_fatalError("Unable to get size of kproc_infos");
David Hunt43ef7032015-07-13 01:17:14 -0500115
Hishamb54d2dd2016-02-02 15:53:02 +0100116 processes = xMalloc(*count);
Michael McConville445222e2015-09-16 23:42:36 -0400117 if (processes == NULL)
Michael McConvillecd3d2332015-11-01 13:26:57 -0500118 CRT_fatalError("Out of memory for kproc_infos");
David Hunt43ef7032015-07-13 01:17:14 -0500119
Michael McConville445222e2015-09-16 23:42:36 -0400120 if (sysctl(mib, 4, processes, count, NULL, 0) < 0)
Michael McConvillecd3d2332015-11-01 13:26:57 -0500121 CRT_fatalError("Unable to get kinfo_procs");
David Hunt43ef7032015-07-13 01:17:14 -0500122
123 *count = *count / sizeof(struct kinfo_proc);
124
125 return processes;
126}
127
Nathan Scott45973322020-09-09 19:38:15 +1000128ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) {
Hishamb54d2dd2016-02-02 15:53:02 +0100129 DarwinProcessList* this = xCalloc(1, sizeof(DarwinProcessList));
David Hunt43ef7032015-07-13 01:17:14 -0500130
Nathan Scott45973322020-09-09 19:38:15 +1000131 ProcessList_init(&this->super, Class(Process), usersTable, pidMatchList, userId);
David Hunt57ab3322015-07-13 21:02:40 -0500132
133 /* Initialize the CPU information */
David Hunt6463ea22015-07-13 17:09:18 -0500134 this->super.cpuCount = ProcessList_allocateCPULoadInfo(&this->prev_load);
David Hunt7f3faa22015-07-13 16:53:46 -0500135 ProcessList_getHostInfo(&this->host_info);
David Hunt6463ea22015-07-13 17:09:18 -0500136 ProcessList_allocateCPULoadInfo(&this->curr_load);
David Hunt43ef7032015-07-13 01:17:14 -0500137
David Hunt57ab3322015-07-13 21:02:40 -0500138 /* Initialize the VM statistics */
139 ProcessList_getVMStats(&this->vm_stats);
140
Ross Williamsfc8e9a22019-07-07 17:30:37 -0400141 /* Initialize the ZFS kstats, if zfs.kext loaded */
Ross Williamse450b582019-09-03 18:21:33 +0000142 openzfs_sysctl_init(&this->zfs);
Ross Williamsa88d2e32019-07-07 23:27:00 +0000143 openzfs_sysctl_updateArcStats(&this->zfs);
Ross Williamsfc8e9a22019-07-07 17:30:37 -0400144
David Hunt57ab3322015-07-13 21:02:40 -0500145 this->super.kernelThreads = 0;
146 this->super.userlandThreads = 0;
147 this->super.totalTasks = 0;
148 this->super.runningTasks = 0;
149
David Hunt43ef7032015-07-13 01:17:14 -0500150 return &this->super;
David Hunt70e7c8d2015-07-12 13:47:43 -0500151}
152
153void ProcessList_delete(ProcessList* this) {
154 ProcessList_done(this);
155 free(this);
156}
157
Christian Göttsche96e2a422020-10-13 16:03:37 +0200158void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
Benny Baumannb23f8232020-10-31 22:14:27 +0100159 DarwinProcessList* dpl = (DarwinProcessList*)super;
160 bool preExisting = true;
161 struct kinfo_proc* ps;
162 size_t count;
163 DarwinProcess* proc;
David Hunt70e7c8d2015-07-12 13:47:43 -0500164
Benny Baumannb23f8232020-10-31 22:14:27 +0100165 /* 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 Hunt57ab3322015-07-13 21:02:40 -0500171
Benny Baumannb23f8232020-10-31 22:14:27 +0100172 // in pause mode only gather global data for meters (CPU/memory/...)
173 if (pauseProcessUpdate) {
174 return;
175 }
Christian Göttsche96e2a422020-10-13 16:03:37 +0200176
Benny Baumannb23f8232020-10-31 22:14:27 +0100177 /* 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 Hunt907f8292015-07-14 11:46:16 -0500184
Benny Baumannb23f8232020-10-31 22:14:27 +0100185 /* Clear the thread counts */
186 super->kernelThreads = 0;
187 super->userlandThreads = 0;
188 super->totalTasks = 0;
189 super->runningTasks = 0;
David Hunt7f3faa22015-07-13 16:53:46 -0500190
Benny Baumannb23f8232020-10-31 22:14:27 +0100191 /* 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 Hunt70e7c8d2015-07-12 13:47:43 -0500199
Benny Baumannb23f8232020-10-31 22:14:27 +0100200 for (size_t i = 0; i < count; ++i) {
201 proc = (DarwinProcess*)ProcessList_getProcess(super, ps[i].kp_proc.p_pid, &preExisting, DarwinProcess_new);
David Hunt70e7c8d2015-07-12 13:47:43 -0500202
Benny Baumannb23f8232020-10-31 22:14:27 +0100203 DarwinProcess_setFromKInfoProc(&proc->super, &ps[i], preExisting);
204 DarwinProcess_setFromLibprocPidinfo(proc, dpl);
pmalhaire23f96042018-03-25 22:04:16 +0200205
Benny Baumannb23f8232020-10-31 22:14:27 +0100206 // Disabled for High Sierra due to bug in macOS High Sierra
207 bool isScanThreadSupported = ! ( CompareKernelVersion(17, 0, 0) >= 0 && CompareKernelVersion(17, 5, 0) < 0);
pmalhaire23f96042018-03-25 22:04:16 +0200208
Benny Baumannb23f8232020-10-31 22:14:27 +0100209 if (isScanThreadSupported) {
210 DarwinProcess_scanThreads(proc);
211 }
David Hunt57ab3322015-07-13 21:02:40 -0500212
Benny Baumannb23f8232020-10-31 22:14:27 +0100213 super->totalTasks += 1;
David Hunt70e7c8d2015-07-12 13:47:43 -0500214
Benny Baumannb23f8232020-10-31 22:14:27 +0100215 if (!preExisting) {
216 proc->super.user = UsersTable_getRef(super->usersTable, proc->super.st_uid);
David Hunt70e7c8d2015-07-12 13:47:43 -0500217
Benny Baumannb23f8232020-10-31 22:14:27 +0100218 ProcessList_add(super, &proc->super);
219 }
220 }
David Hunt70e7c8d2015-07-12 13:47:43 -0500221
Benny Baumannb23f8232020-10-31 22:14:27 +0100222 free(ps);
David Hunt70e7c8d2015-07-12 13:47:43 -0500223}