blob: 8bb2651a7bc250543efe9d2f6c3b2d7b266d2691 [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
David Hunt70e7c8d2015-07-12 13:47:43 -05008#include "DarwinProcessList.h"
9
Christian Göttsche72df9302020-11-17 15:17:19 +010010#include <errno.h>
Christian Göttschee3af8d02020-11-17 15:10:44 +010011#include <libproc.h>
12#include <stdbool.h>
13#include <stdio.h>
David Hunt70e7c8d2015-07-12 13:47:43 -050014#include <stdlib.h>
15#include <string.h>
David Hunt43ef7032015-07-13 01:17:14 -050016#include <unistd.h>
David Hunt57ab3322015-07-13 21:02:40 -050017#include <utmpx.h>
Christian Göttschee3af8d02020-11-17 15:10:44 +010018#include <sys/mman.h>
pmalhaire23f96042018-03-25 22:04:16 +020019#include <sys/sysctl.h>
Christian Göttschee3af8d02020-11-17 15:10:44 +010020
21#include "CRT.h"
22#include "DarwinProcess.h"
Alexander Momchilov67ccd6b2020-12-08 23:12:44 -050023#include "Platform.h"
Christian Göttschee3af8d02020-11-17 15:10:44 +010024#include "ProcessList.h"
Nathan Scottadaf7482021-03-04 09:00:34 +110025#include "generic/openzfs_sysctl.h"
Christian Göttschee3af8d02020-11-17 15:10:44 +010026#include "zfs/ZfsArcStats.h"
27
pmalhaire23f96042018-03-25 22:04:16 +020028
29struct kern {
Benny Baumannb23f8232020-10-31 22:14:27 +010030 short int version[3];
pmalhaire23f96042018-03-25 22:04:16 +020031};
32
Christian Göttschee3af8d02020-11-17 15:10:44 +010033static void GetKernelVersion(struct kern* k) {
pmalhaire23f96042018-03-25 22:04:16 +020034 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 Baumannb23f8232020-10-31 22:14:27 +010041 if (ret == 0) {
42 sscanf(str, "%hd.%hd.%hd", &version_[0], &version_[1], &version_[2]);
43 }
44 }
45 memcpy(k->version, version_, sizeof(version_));
pmalhaire23f96042018-03-25 22:04:16 +020046}
47
pmalhaire0505a7c2018-04-06 00:41:22 +020048/* compare the given os version with the one installed returns:
490 if equals the installed version
50positive value if less than the installed version
51negative value if more than the installed version
52*/
Christian Göttschee3af8d02020-11-17 15:10:44 +010053static int CompareKernelVersion(short int major, short int minor, short int component) {
Benny Baumannb23f8232020-10-31 22:14:27 +010054 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;
pmalhaire23f96042018-03-25 22:04:16 +020068}
David Hunt70e7c8d2015-07-12 13:47:43 -050069
Christian Göttschee3af8d02020-11-17 15:10:44 +010070static void ProcessList_getHostInfo(host_basic_info_data_t* p) {
David Hunt43ef7032015-07-13 01:17:14 -050071 mach_msg_type_number_t info_size = HOST_BASIC_INFO_COUNT;
72
Benny Baumann374edb92020-10-31 20:52:20 +010073 if (0 != host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)p, &info_size)) {
Christian Göttschef878f302020-12-19 21:25:18 +010074 CRT_fatalError("Unable to retrieve host info");
David Hunt43ef7032015-07-13 01:17:14 -050075 }
76}
77
Christian Göttschee3af8d02020-11-17 15:10:44 +010078static void ProcessList_freeCPULoadInfo(processor_cpu_load_info_t* p) {
Benny Baumann374edb92020-10-31 20:52:20 +010079 if (NULL != p && NULL != *p) {
Benny Baumannb23f8232020-10-31 22:14:27 +010080 if (0 != munmap(*p, vm_page_size)) {
Christian Göttschef878f302020-12-19 21:25:18 +010081 CRT_fatalError("Unable to free old CPU load information");
Benny Baumannb23f8232020-10-31 22:14:27 +010082 }
83 *p = NULL;
David Hunt7f3faa22015-07-13 16:53:46 -050084 }
David Hunt6463ea22015-07-13 17:09:18 -050085}
86
Christian Göttschee3af8d02020-11-17 15:10:44 +010087static unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t* p) {
David Hunt6463ea22015-07-13 17:09:18 -050088 mach_msg_type_number_t info_size = sizeof(processor_cpu_load_info_t);
89 unsigned cpu_count;
90
David Hunt907f8292015-07-14 11:46:16 -050091 // TODO Improving the accuracy of the load counts woule help a lot.
Benny Baumannb23f8232020-10-31 22:14:27 +010092 if (0 != host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, (processor_info_array_t*)p, &info_size)) {
Christian Göttschef878f302020-12-19 21:25:18 +010093 CRT_fatalError("Unable to retrieve CPU info");
David Hunt43ef7032015-07-13 01:17:14 -050094 }
95
96 return cpu_count;
97}
98
Christian Göttschee3af8d02020-11-17 15:10:44 +010099static void ProcessList_getVMStats(vm_statistics_t p) {
Benny Baumannb23f8232020-10-31 22:14:27 +0100100 mach_msg_type_number_t info_size = HOST_VM_INFO_COUNT;
David Hunt57ab3322015-07-13 21:02:40 -0500101
Benny Baumannb23f8232020-10-31 22:14:27 +0100102 if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)p, &info_size) != 0) {
Christian Göttschef878f302020-12-19 21:25:18 +0100103 CRT_fatalError("Unable to retrieve VM statistics");
Benny Baumannb23f8232020-10-31 22:14:27 +0100104 }
David Hunt57ab3322015-07-13 21:02:40 -0500105}
106
Christian Göttschee3af8d02020-11-17 15:10:44 +0100107static struct kinfo_proc* ProcessList_getKInfoProcs(size_t* count) {
David Hunt43ef7032015-07-13 01:17:14 -0500108 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
Benny Baumann61e14d42020-10-31 23:28:02 +0100109 struct kinfo_proc* processes = NULL;
David Hunt43ef7032015-07-13 01:17:14 -0500110
Christian Göttsche72df9302020-11-17 15:17:19 +0100111 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 Baumann45869512020-11-01 01:09:51 +0100126 }
David Hunt43ef7032015-07-13 01:17:14 -0500127
Christian Göttsche72df9302020-11-17 15:17:19 +0100128 CRT_fatalError("Unable to get kinfo_procs");
David Hunt43ef7032015-07-13 01:17:14 -0500129}
130
Nathan Scott45973322020-09-09 19:38:15 +1000131ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) {
Hishamb54d2dd2016-02-02 15:53:02 +0100132 DarwinProcessList* this = xCalloc(1, sizeof(DarwinProcessList));
David Hunt43ef7032015-07-13 01:17:14 -0500133
Dániel Bakai3655b6c2020-12-13 15:54:13 +0100134 ProcessList_init(&this->super, Class(DarwinProcess), usersTable, pidMatchList, userId);
David Hunt57ab3322015-07-13 21:02:40 -0500135
136 /* Initialize the CPU information */
David Hunt6463ea22015-07-13 17:09:18 -0500137 this->super.cpuCount = ProcessList_allocateCPULoadInfo(&this->prev_load);
David Hunt7f3faa22015-07-13 16:53:46 -0500138 ProcessList_getHostInfo(&this->host_info);
David Hunt6463ea22015-07-13 17:09:18 -0500139 ProcessList_allocateCPULoadInfo(&this->curr_load);
David Hunt43ef7032015-07-13 01:17:14 -0500140
David Hunt57ab3322015-07-13 21:02:40 -0500141 /* Initialize the VM statistics */
142 ProcessList_getVMStats(&this->vm_stats);
143
Ross Williamsfc8e9a22019-07-07 17:30:37 -0400144 /* Initialize the ZFS kstats, if zfs.kext loaded */
Ross Williamse450b582019-09-03 18:21:33 +0000145 openzfs_sysctl_init(&this->zfs);
Ross Williamsa88d2e32019-07-07 23:27:00 +0000146 openzfs_sysctl_updateArcStats(&this->zfs);
Ross Williamsfc8e9a22019-07-07 17:30:37 -0400147
David Hunt57ab3322015-07-13 21:02:40 -0500148 this->super.kernelThreads = 0;
149 this->super.userlandThreads = 0;
150 this->super.totalTasks = 0;
151 this->super.runningTasks = 0;
152
David Hunt43ef7032015-07-13 01:17:14 -0500153 return &this->super;
David Hunt70e7c8d2015-07-12 13:47:43 -0500154}
155
156void ProcessList_delete(ProcessList* this) {
157 ProcessList_done(this);
158 free(this);
159}
160
Alexander Momchilov67ccd6b2020-12-08 23:12:44 -0500161static double ticksToNanoseconds(const double ticks) {
162 const double nanos_per_sec = 1e9;
Luke Groeningera150a812021-01-02 00:45:53 -0600163 return (ticks / Platform_timebaseToNS) * (nanos_per_sec / (double) Platform_clockTicksPerSec);
Alexander Momchilov67ccd6b2020-12-08 23:12:44 -0500164}
165
Christian Göttsche96e2a422020-10-13 16:03:37 +0200166void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
Benny Baumannb23f8232020-10-31 22:14:27 +0100167 DarwinProcessList* dpl = (DarwinProcessList*)super;
168 bool preExisting = true;
169 struct kinfo_proc* ps;
170 size_t count;
171 DarwinProcess* proc;
David Hunt70e7c8d2015-07-12 13:47:43 -0500172
Benny Baumannb23f8232020-10-31 22:14:27 +0100173 /* 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 Hunt57ab3322015-07-13 21:02:40 -0500179
Benny Baumannb23f8232020-10-31 22:14:27 +0100180 // in pause mode only gather global data for meters (CPU/memory/...)
181 if (pauseProcessUpdate) {
182 return;
183 }
Christian Göttsche96e2a422020-10-13 16:03:37 +0200184
Benny Baumannb23f8232020-10-31 22:14:27 +0100185 /* 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 Hunt907f8292015-07-14 11:46:16 -0500192
Alexander Momchilov67ccd6b2020-12-08 23:12:44 -0500193 const double time_interval = ticksToNanoseconds(dpl->global_diff) / (double) dpl->super.cpuCount;
194
Benny Baumannb23f8232020-10-31 22:14:27 +0100195 /* Clear the thread counts */
196 super->kernelThreads = 0;
197 super->userlandThreads = 0;
198 super->totalTasks = 0;
199 super->runningTasks = 0;
David Hunt7f3faa22015-07-13 16:53:46 -0500200
Benny Baumannb23f8232020-10-31 22:14:27 +0100201 /* 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 Hunt70e7c8d2015-07-12 13:47:43 -0500209
Benny Baumannb23f8232020-10-31 22:14:27 +0100210 for (size_t i = 0; i < count; ++i) {
211 proc = (DarwinProcess*)ProcessList_getProcess(super, ps[i].kp_proc.p_pid, &preExisting, DarwinProcess_new);
David Hunt70e7c8d2015-07-12 13:47:43 -0500212
Benny Baumannb23f8232020-10-31 22:14:27 +0100213 DarwinProcess_setFromKInfoProc(&proc->super, &ps[i], preExisting);
Alexander Momchilov67ccd6b2020-12-08 23:12:44 -0500214 DarwinProcess_setFromLibprocPidinfo(proc, dpl, time_interval);
pmalhaire23f96042018-03-25 22:04:16 +0200215
Benny Baumannb23f8232020-10-31 22:14:27 +0100216 // Disabled for High Sierra due to bug in macOS High Sierra
217 bool isScanThreadSupported = ! ( CompareKernelVersion(17, 0, 0) >= 0 && CompareKernelVersion(17, 5, 0) < 0);
pmalhaire23f96042018-03-25 22:04:16 +0200218
Benny Baumannb23f8232020-10-31 22:14:27 +0100219 if (isScanThreadSupported) {
220 DarwinProcess_scanThreads(proc);
221 }
David Hunt57ab3322015-07-13 21:02:40 -0500222
Benny Baumannb23f8232020-10-31 22:14:27 +0100223 super->totalTasks += 1;
David Hunt70e7c8d2015-07-12 13:47:43 -0500224
Benny Baumannb23f8232020-10-31 22:14:27 +0100225 if (!preExisting) {
226 proc->super.user = UsersTable_getRef(super->usersTable, proc->super.st_uid);
David Hunt70e7c8d2015-07-12 13:47:43 -0500227
Benny Baumannb23f8232020-10-31 22:14:27 +0100228 ProcessList_add(super, &proc->super);
229 }
230 }
David Hunt70e7c8d2015-07-12 13:47:43 -0500231
Benny Baumannb23f8232020-10-31 22:14:27 +0100232 free(ps);
David Hunt70e7c8d2015-07-12 13:47:43 -0500233}