blob: e5ca5ad8bf524032d7a82ca9faf1d9dbe886813e [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öttschee3af8d02020-11-17 15:10:44 +010010#include <err.h>
Christian Göttsche72df9302020-11-17 15:17:19 +010011#include <errno.h>
Christian Göttschee3af8d02020-11-17 15:10:44 +010012#include <libproc.h>
13#include <stdbool.h>
14#include <stdio.h>
David Hunt70e7c8d2015-07-12 13:47:43 -050015#include <stdlib.h>
16#include <string.h>
David Hunt43ef7032015-07-13 01:17:14 -050017#include <unistd.h>
David Hunt57ab3322015-07-13 21:02:40 -050018#include <utmpx.h>
Christian Göttschee3af8d02020-11-17 15:10:44 +010019#include <sys/mman.h>
pmalhaire23f96042018-03-25 22:04:16 +020020#include <sys/sysctl.h>
Christian Göttschee3af8d02020-11-17 15:10:44 +010021
22#include "CRT.h"
23#include "DarwinProcess.h"
Alexander Momchilov67ccd6b2020-12-08 23:12:44 -050024#include "Platform.h"
Christian Göttschee3af8d02020-11-17 15:10:44 +010025#include "ProcessList.h"
26#include "zfs/openzfs_sysctl.h"
27#include "zfs/ZfsArcStats.h"
28
pmalhaire23f96042018-03-25 22:04:16 +020029
30struct kern {
Benny Baumannb23f8232020-10-31 22:14:27 +010031 short int version[3];
pmalhaire23f96042018-03-25 22:04:16 +020032};
33
Christian Göttschee3af8d02020-11-17 15:10:44 +010034static void GetKernelVersion(struct kern* k) {
pmalhaire23f96042018-03-25 22:04:16 +020035 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 Baumannb23f8232020-10-31 22:14:27 +010042 if (ret == 0) {
43 sscanf(str, "%hd.%hd.%hd", &version_[0], &version_[1], &version_[2]);
44 }
45 }
46 memcpy(k->version, version_, sizeof(version_));
pmalhaire23f96042018-03-25 22:04:16 +020047}
48
pmalhaire0505a7c2018-04-06 00:41:22 +020049/* compare the given os version with the one installed returns:
500 if equals the installed version
51positive value if less than the installed version
52negative value if more than the installed version
53*/
Christian Göttschee3af8d02020-11-17 15:10:44 +010054static int CompareKernelVersion(short int major, short int minor, short int component) {
Benny Baumannb23f8232020-10-31 22:14:27 +010055 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;
pmalhaire23f96042018-03-25 22:04:16 +020069}
David Hunt70e7c8d2015-07-12 13:47:43 -050070
Christian Göttschee3af8d02020-11-17 15:10:44 +010071static void ProcessList_getHostInfo(host_basic_info_data_t* p) {
David Hunt43ef7032015-07-13 01:17:14 -050072 mach_msg_type_number_t info_size = HOST_BASIC_INFO_COUNT;
73
Benny Baumann374edb92020-10-31 20:52:20 +010074 if (0 != host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)p, &info_size)) {
Benny Baumannb23f8232020-10-31 22:14:27 +010075 CRT_fatalError("Unable to retrieve host info\n");
David Hunt43ef7032015-07-13 01:17:14 -050076 }
77}
78
Christian Göttschee3af8d02020-11-17 15:10:44 +010079static void ProcessList_freeCPULoadInfo(processor_cpu_load_info_t* p) {
Benny Baumann374edb92020-10-31 20:52:20 +010080 if (NULL != p && NULL != *p) {
Benny Baumannb23f8232020-10-31 22:14:27 +010081 if (0 != munmap(*p, vm_page_size)) {
82 CRT_fatalError("Unable to free old CPU load information\n");
83 }
84 *p = NULL;
David Hunt7f3faa22015-07-13 16:53:46 -050085 }
David Hunt6463ea22015-07-13 17:09:18 -050086}
87
Christian Göttschee3af8d02020-11-17 15:10:44 +010088static unsigned ProcessList_allocateCPULoadInfo(processor_cpu_load_info_t* p) {
David Hunt6463ea22015-07-13 17:09:18 -050089 mach_msg_type_number_t info_size = sizeof(processor_cpu_load_info_t);
90 unsigned cpu_count;
91
David Hunt907f8292015-07-14 11:46:16 -050092 // TODO Improving the accuracy of the load counts woule help a lot.
Benny Baumannb23f8232020-10-31 22:14:27 +010093 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 Hunt43ef7032015-07-13 01:17:14 -050095 }
96
97 return cpu_count;
98}
99
Christian Göttschee3af8d02020-11-17 15:10:44 +0100100static void ProcessList_getVMStats(vm_statistics_t p) {
Benny Baumannb23f8232020-10-31 22:14:27 +0100101 mach_msg_type_number_t info_size = HOST_VM_INFO_COUNT;
David Hunt57ab3322015-07-13 21:02:40 -0500102
Benny Baumannb23f8232020-10-31 22:14:27 +0100103 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 Hunt57ab3322015-07-13 21:02:40 -0500106}
107
Christian Göttschee3af8d02020-11-17 15:10:44 +0100108static struct kinfo_proc* ProcessList_getKInfoProcs(size_t* count) {
David Hunt43ef7032015-07-13 01:17:14 -0500109 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
Benny Baumann61e14d42020-10-31 23:28:02 +0100110 struct kinfo_proc* processes = NULL;
David Hunt43ef7032015-07-13 01:17:14 -0500111
Christian Göttsche72df9302020-11-17 15:17:19 +0100112 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 Baumann45869512020-11-01 01:09:51 +0100127 }
David Hunt43ef7032015-07-13 01:17:14 -0500128
Christian Göttsche72df9302020-11-17 15:17:19 +0100129 CRT_fatalError("Unable to get kinfo_procs");
David Hunt43ef7032015-07-13 01:17:14 -0500130}
131
Nathan Scott45973322020-09-09 19:38:15 +1000132ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) {
Hishamb54d2dd2016-02-02 15:53:02 +0100133 DarwinProcessList* this = xCalloc(1, sizeof(DarwinProcessList));
David Hunt43ef7032015-07-13 01:17:14 -0500134
Dániel Bakai3655b6c2020-12-13 15:54:13 +0100135 ProcessList_init(&this->super, Class(DarwinProcess), usersTable, pidMatchList, userId);
David Hunt57ab3322015-07-13 21:02:40 -0500136
137 /* Initialize the CPU information */
David Hunt6463ea22015-07-13 17:09:18 -0500138 this->super.cpuCount = ProcessList_allocateCPULoadInfo(&this->prev_load);
David Hunt7f3faa22015-07-13 16:53:46 -0500139 ProcessList_getHostInfo(&this->host_info);
David Hunt6463ea22015-07-13 17:09:18 -0500140 ProcessList_allocateCPULoadInfo(&this->curr_load);
David Hunt43ef7032015-07-13 01:17:14 -0500141
David Hunt57ab3322015-07-13 21:02:40 -0500142 /* Initialize the VM statistics */
143 ProcessList_getVMStats(&this->vm_stats);
144
Ross Williamsfc8e9a22019-07-07 17:30:37 -0400145 /* Initialize the ZFS kstats, if zfs.kext loaded */
Ross Williamse450b582019-09-03 18:21:33 +0000146 openzfs_sysctl_init(&this->zfs);
Ross Williamsa88d2e32019-07-07 23:27:00 +0000147 openzfs_sysctl_updateArcStats(&this->zfs);
Ross Williamsfc8e9a22019-07-07 17:30:37 -0400148
David Hunt57ab3322015-07-13 21:02:40 -0500149 this->super.kernelThreads = 0;
150 this->super.userlandThreads = 0;
151 this->super.totalTasks = 0;
152 this->super.runningTasks = 0;
153
David Hunt43ef7032015-07-13 01:17:14 -0500154 return &this->super;
David Hunt70e7c8d2015-07-12 13:47:43 -0500155}
156
157void ProcessList_delete(ProcessList* this) {
158 ProcessList_done(this);
159 free(this);
160}
161
Alexander Momchilov67ccd6b2020-12-08 23:12:44 -0500162static 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öttsche96e2a422020-10-13 16:03:37 +0200167void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
Benny Baumannb23f8232020-10-31 22:14:27 +0100168 DarwinProcessList* dpl = (DarwinProcessList*)super;
169 bool preExisting = true;
170 struct kinfo_proc* ps;
171 size_t count;
172 DarwinProcess* proc;
David Hunt70e7c8d2015-07-12 13:47:43 -0500173
Benny Baumannb23f8232020-10-31 22:14:27 +0100174 /* 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 Hunt57ab3322015-07-13 21:02:40 -0500180
Benny Baumannb23f8232020-10-31 22:14:27 +0100181 // in pause mode only gather global data for meters (CPU/memory/...)
182 if (pauseProcessUpdate) {
183 return;
184 }
Christian Göttsche96e2a422020-10-13 16:03:37 +0200185
Benny Baumannb23f8232020-10-31 22:14:27 +0100186 /* 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 Hunt907f8292015-07-14 11:46:16 -0500193
Alexander Momchilov67ccd6b2020-12-08 23:12:44 -0500194 const double time_interval = ticksToNanoseconds(dpl->global_diff) / (double) dpl->super.cpuCount;
195
Benny Baumannb23f8232020-10-31 22:14:27 +0100196 /* Clear the thread counts */
197 super->kernelThreads = 0;
198 super->userlandThreads = 0;
199 super->totalTasks = 0;
200 super->runningTasks = 0;
David Hunt7f3faa22015-07-13 16:53:46 -0500201
Benny Baumannb23f8232020-10-31 22:14:27 +0100202 /* 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 Hunt70e7c8d2015-07-12 13:47:43 -0500210
Benny Baumannb23f8232020-10-31 22:14:27 +0100211 for (size_t i = 0; i < count; ++i) {
212 proc = (DarwinProcess*)ProcessList_getProcess(super, ps[i].kp_proc.p_pid, &preExisting, DarwinProcess_new);
David Hunt70e7c8d2015-07-12 13:47:43 -0500213
Benny Baumannb23f8232020-10-31 22:14:27 +0100214 DarwinProcess_setFromKInfoProc(&proc->super, &ps[i], preExisting);
Alexander Momchilov67ccd6b2020-12-08 23:12:44 -0500215 DarwinProcess_setFromLibprocPidinfo(proc, dpl, time_interval);
pmalhaire23f96042018-03-25 22:04:16 +0200216
Benny Baumannb23f8232020-10-31 22:14:27 +0100217 // Disabled for High Sierra due to bug in macOS High Sierra
218 bool isScanThreadSupported = ! ( CompareKernelVersion(17, 0, 0) >= 0 && CompareKernelVersion(17, 5, 0) < 0);
pmalhaire23f96042018-03-25 22:04:16 +0200219
Benny Baumannb23f8232020-10-31 22:14:27 +0100220 if (isScanThreadSupported) {
221 DarwinProcess_scanThreads(proc);
222 }
David Hunt57ab3322015-07-13 21:02:40 -0500223
Benny Baumannb23f8232020-10-31 22:14:27 +0100224 super->totalTasks += 1;
David Hunt70e7c8d2015-07-12 13:47:43 -0500225
Benny Baumannb23f8232020-10-31 22:14:27 +0100226 if (!preExisting) {
227 proc->super.user = UsersTable_getRef(super->usersTable, proc->super.st_uid);
David Hunt70e7c8d2015-07-12 13:47:43 -0500228
Benny Baumannb23f8232020-10-31 22:14:27 +0100229 ProcessList_add(super, &proc->super);
230 }
231 }
David Hunt70e7c8d2015-07-12 13:47:43 -0500232
Benny Baumannb23f8232020-10-31 22:14:27 +0100233 free(ps);
David Hunt70e7c8d2015-07-12 13:47:43 -0500234}