| 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 | 94ad111 | 2021-09-22 11:33:00 +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 | |
| mayurdahibhate | 3f86a01 | 2021-04-29 23:43:36 +0530 | [diff] [blame] | 8 | #include "darwin/DarwinProcessList.h" |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 9 | |
| Christian Göttsche | 72df930 | 2020-11-17 15:17:19 +0100 | [diff] [blame] | 10 | #include <errno.h> |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 11 | #include <libproc.h> |
| 12 | #include <stdbool.h> |
| 13 | #include <stdio.h> |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 16 | #include <unistd.h> |
| David Hunt | 57ab332 | 2015-07-13 21:02:40 -0500 | [diff] [blame] | 17 | #include <utmpx.h> |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 18 | #include <sys/mman.h> |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 19 | #include <sys/sysctl.h> |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 20 | |
| 21 | #include "CRT.h" |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 22 | #include "ProcessList.h" |
| Nathan Scott | 72235d8 | 2023-05-02 16:56:18 +1000 | [diff] [blame^] | 23 | #include "darwin/DarwinMachine.h" |
| mayurdahibhate | 3f86a01 | 2021-04-29 23:43:36 +0530 | [diff] [blame] | 24 | #include "darwin/DarwinProcess.h" |
| 25 | #include "darwin/Platform.h" |
| Alexander Momchilov | dadcb87 | 2021-08-22 12:30:08 -0400 | [diff] [blame] | 26 | #include "darwin/PlatformHelpers.h" |
| Nathan Scott | adaf748 | 2021-03-04 09:00:34 +1100 | [diff] [blame] | 27 | #include "generic/openzfs_sysctl.h" |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 28 | #include "zfs/ZfsArcStats.h" |
| 29 | |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 30 | |
| Christian Göttsche | e3af8d0 | 2020-11-17 15:10:44 +0100 | [diff] [blame] | 31 | static struct kinfo_proc* ProcessList_getKInfoProcs(size_t* count) { |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 32 | int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; |
| Benny Baumann | 61e14d4 | 2020-10-31 23:28:02 +0100 | [diff] [blame] | 33 | struct kinfo_proc* processes = NULL; |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 34 | |
| Christian Göttsche | bc22bee | 2022-09-20 21:40:02 +0200 | [diff] [blame] | 35 | for (unsigned int retry = 0; retry < 4; retry++) { |
| Christian Göttsche | 72df930 | 2020-11-17 15:17:19 +0100 | [diff] [blame] | 36 | size_t size = 0; |
| 37 | if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0 || size == 0) { |
| 38 | CRT_fatalError("Unable to get size of kproc_infos"); |
| 39 | } |
| 40 | |
| Christian Göttsche | bc22bee | 2022-09-20 21:40:02 +0200 | [diff] [blame] | 41 | size += 16 * retry * retry * sizeof(struct kinfo_proc); |
| Christian Göttsche | 72df930 | 2020-11-17 15:17:19 +0100 | [diff] [blame] | 42 | processes = xRealloc(processes, size); |
| 43 | |
| 44 | if (sysctl(mib, 4, processes, &size, NULL, 0) == 0) { |
| 45 | *count = size / sizeof(struct kinfo_proc); |
| 46 | return processes; |
| 47 | } |
| 48 | |
| 49 | if (errno != ENOMEM) |
| 50 | break; |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 51 | } |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 52 | |
| Christian Göttsche | 72df930 | 2020-11-17 15:17:19 +0100 | [diff] [blame] | 53 | CRT_fatalError("Unable to get kinfo_procs"); |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 56 | ProcessList* ProcessList_new(Machine* host, Hashtable* pidMatchList) { |
| Hisham | b54d2dd | 2016-02-02 15:53:02 +0100 | [diff] [blame] | 57 | DarwinProcessList* this = xCalloc(1, sizeof(DarwinProcessList)); |
| Nathan Scott | 72235d8 | 2023-05-02 16:56:18 +1000 | [diff] [blame^] | 58 | ProcessList* super = &this->super; |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 59 | |
| Nathan Scott | 72235d8 | 2023-05-02 16:56:18 +1000 | [diff] [blame^] | 60 | ProcessList_init(super, Class(DarwinProcess), host, pidMatchList); |
| David Hunt | 57ab332 | 2015-07-13 21:02:40 -0500 | [diff] [blame] | 61 | |
| Nathan Scott | 72235d8 | 2023-05-02 16:56:18 +1000 | [diff] [blame^] | 62 | return super; |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void ProcessList_delete(ProcessList* this) { |
| 66 | ProcessList_done(this); |
| 67 | free(this); |
| 68 | } |
| 69 | |
| Nathan Scott | 72235d8 | 2023-05-02 16:56:18 +1000 | [diff] [blame^] | 70 | void ProcessList_goThroughEntries(ProcessList* super) { |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 71 | const Machine* host = super->host; |
| Nathan Scott | 72235d8 | 2023-05-02 16:56:18 +1000 | [diff] [blame^] | 72 | const DarwinMachine* dhost = (const DarwinMachine*) host; |
| 73 | DarwinProcessList* dpl = (DarwinProcessList*) super; |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 74 | bool preExisting = true; |
| 75 | struct kinfo_proc* ps; |
| 76 | size_t count; |
| 77 | DarwinProcess* proc; |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 78 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 79 | /* Get the time difference */ |
| 80 | dpl->global_diff = 0; |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 81 | for (unsigned int i = 0; i < host->existingCPUs; ++i) { |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 82 | for (size_t j = 0; j < CPU_STATE_MAX; ++j) { |
| Nathan Scott | 72235d8 | 2023-05-02 16:56:18 +1000 | [diff] [blame^] | 83 | dpl->global_diff += dhost->curr_load[i].cpu_ticks[j] - dhost->prev_load[i].cpu_ticks[j]; |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 84 | } |
| 85 | } |
| David Hunt | 907f829 | 2015-07-14 11:46:16 -0500 | [diff] [blame] | 86 | |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 87 | const double time_interval_ns = Platform_schedulerTicksToNanoseconds(dpl->global_diff) / (double) host->activeCPUs; |
| Alexander Momchilov | 67ccd6b | 2020-12-08 23:12:44 -0500 | [diff] [blame] | 88 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 89 | /* Clear the thread counts */ |
| 90 | super->kernelThreads = 0; |
| 91 | super->userlandThreads = 0; |
| 92 | super->totalTasks = 0; |
| 93 | super->runningTasks = 0; |
| David Hunt | 7f3faa2 | 2015-07-13 16:53:46 -0500 | [diff] [blame] | 94 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 95 | /* We use kinfo_procs for initial data since : |
| 96 | * |
| 97 | * 1) They always succeed. |
| Guillaume Gomez | 8a8df71 | 2023-05-05 11:28:41 +0200 | [diff] [blame] | 98 | * 2) They contain the basic information. |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 99 | * |
| 100 | * We attempt to fill-in additional information with libproc. |
| 101 | */ |
| 102 | ps = ProcessList_getKInfoProcs(&count); |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 103 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 104 | for (size_t i = 0; i < count; ++i) { |
| 105 | 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] | 106 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 107 | DarwinProcess_setFromKInfoProc(&proc->super, &ps[i], preExisting); |
| Alexander Momchilov | 5b4d63d | 2021-08-20 17:52:07 -0400 | [diff] [blame] | 108 | DarwinProcess_setFromLibprocPidinfo(proc, dpl, time_interval_ns); |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 109 | |
| Benny Baumann | 6a6b09b | 2021-06-18 20:42:04 +0200 | [diff] [blame] | 110 | if (proc->super.st_uid != ps[i].kp_eproc.e_ucred.cr_uid) { |
| 111 | proc->super.st_uid = ps[i].kp_eproc.e_ucred.cr_uid; |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 112 | proc->super.user = UsersTable_getRef(host->usersTable, proc->super.st_uid); |
| Benny Baumann | 6a6b09b | 2021-06-18 20:42:04 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 115 | // Disabled for High Sierra due to bug in macOS High Sierra |
| Nathan Scott | 72235d8 | 2023-05-02 16:56:18 +1000 | [diff] [blame^] | 116 | bool isScanThreadSupported = !Platform_KernelVersionIsBetween((KernelVersion) {17, 0, 0}, (KernelVersion) {17, 5, 0}); |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 117 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 118 | if (isScanThreadSupported) { |
| 119 | DarwinProcess_scanThreads(proc); |
| 120 | } |
| David Hunt | 57ab332 | 2015-07-13 21:02:40 -0500 | [diff] [blame] | 121 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 122 | super->totalTasks += 1; |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 123 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 124 | if (!preExisting) { |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 125 | ProcessList_add(super, &proc->super); |
| 126 | } |
| 127 | } |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 128 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 129 | free(ps); |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 130 | } |