| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 1 | /* |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 2 | htop - DarwinProcessTable.c |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 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 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 8 | #include "darwin/DarwinProcessTable.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" |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 22 | #include "ProcessTable.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 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 31 | static struct kinfo_proc* ProcessTable_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 | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 56 | ProcessTable* ProcessTable_new(Machine* host, Hashtable* pidMatchList) { |
| 57 | DarwinProcessTable* this = xCalloc(1, sizeof(DarwinProcessTable)); |
| 58 | Object_setClass(this, Class(ProcessTable)); |
| David Hunt | 43ef703 | 2015-07-13 01:17:14 -0500 | [diff] [blame] | 59 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 60 | ProcessTable* super = &this->super; |
| 61 | ProcessTable_init(super, Class(DarwinProcess), host, pidMatchList); |
| David Hunt | 57ab332 | 2015-07-13 21:02:40 -0500 | [diff] [blame] | 62 | |
| Nathan Scott | 72235d8 | 2023-05-02 16:56:18 +1000 | [diff] [blame] | 63 | return super; |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 66 | void ProcessTable_delete(Object* cast) { |
| 67 | DarwinProcessTable* this = (DarwinProcessTable*) cast; |
| 68 | ProcessTable_done(&this->super); |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 69 | free(this); |
| 70 | } |
| 71 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 72 | void ProcessTable_goThroughEntries(ProcessTable* super) { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 73 | const Machine* host = super->super.host; |
| Nathan Scott | 72235d8 | 2023-05-02 16:56:18 +1000 | [diff] [blame] | 74 | const DarwinMachine* dhost = (const DarwinMachine*) host; |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 75 | DarwinProcessTable* dpt = (DarwinProcessTable*) super; |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 76 | bool preExisting = true; |
| 77 | struct kinfo_proc* ps; |
| 78 | size_t count; |
| 79 | DarwinProcess* proc; |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 80 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 81 | /* Get the time difference */ |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 82 | dpt->global_diff = 0; |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 83 | for (unsigned int i = 0; i < host->existingCPUs; ++i) { |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 84 | for (size_t j = 0; j < CPU_STATE_MAX; ++j) { |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 85 | dpt->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] | 86 | } |
| 87 | } |
| David Hunt | 907f829 | 2015-07-14 11:46:16 -0500 | [diff] [blame] | 88 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 89 | const double time_interval_ns = Platform_schedulerTicksToNanoseconds(dpt->global_diff) / (double) host->activeCPUs; |
| Alexander Momchilov | 67ccd6b | 2020-12-08 23:12:44 -0500 | [diff] [blame] | 90 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 91 | /* We use kinfo_procs for initial data since : |
| 92 | * |
| 93 | * 1) They always succeed. |
| Guillaume Gomez | 8a8df71 | 2023-05-05 11:28:41 +0200 | [diff] [blame] | 94 | * 2) They contain the basic information. |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 95 | * |
| 96 | * We attempt to fill-in additional information with libproc. |
| 97 | */ |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 98 | ps = ProcessTable_getKInfoProcs(&count); |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 99 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 100 | for (size_t i = 0; i < count; ++i) { |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 101 | proc = (DarwinProcess*)ProcessTable_getProcess(super, ps[i].kp_proc.p_pid, &preExisting, DarwinProcess_new); |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 102 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 103 | DarwinProcess_setFromKInfoProc(&proc->super, &ps[i], preExisting); |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 104 | DarwinProcess_setFromLibprocPidinfo(proc, dpt, time_interval_ns); |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 105 | |
| aestriplex | 847547c | 2025-02-25 11:14:49 +0100 | [diff] [blame] | 106 | // Deduce further process states not covered in the libproc call above |
| 107 | if (ps[i].kp_proc.p_stat == SZOMB) { |
| 108 | proc->super.state = ZOMBIE; |
| 109 | } else if (ps[i].kp_proc.p_stat == SSTOP) { |
| 110 | proc->super.state = STOPPED; |
| 111 | } |
| 112 | |
| Benny Baumann | 6a6b09b | 2021-06-18 20:42:04 +0200 | [diff] [blame] | 113 | if (proc->super.st_uid != ps[i].kp_eproc.e_ucred.cr_uid) { |
| 114 | proc->super.st_uid = ps[i].kp_eproc.e_ucred.cr_uid; |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 115 | proc->super.user = UsersTable_getRef(host->usersTable, proc->super.st_uid); |
| Benny Baumann | 6a6b09b | 2021-06-18 20:42:04 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 118 | // Disabled for High Sierra due to bug in macOS High Sierra |
| Nathan Scott | 72235d8 | 2023-05-02 16:56:18 +1000 | [diff] [blame] | 119 | bool isScanThreadSupported = !Platform_KernelVersionIsBetween((KernelVersion) {17, 0, 0}, (KernelVersion) {17, 5, 0}); |
| pmalhaire | 23f9604 | 2018-03-25 22:04:16 +0200 | [diff] [blame] | 120 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 121 | if (isScanThreadSupported) { |
| robaho | 4c0e965 | 2021-02-28 18:37:44 +0100 | [diff] [blame] | 122 | DarwinProcess_scanThreads(proc, dpt); |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 123 | } |
| David Hunt | 57ab332 | 2015-07-13 21:02:40 -0500 | [diff] [blame] | 124 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 125 | super->totalTasks += 1; |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 126 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 127 | if (!preExisting) { |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 128 | ProcessTable_add(super, &proc->super); |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 129 | } |
| 130 | } |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 131 | |
| Benny Baumann | b23f823 | 2020-10-31 22:14:27 +0100 | [diff] [blame] | 132 | free(ps); |
| David Hunt | 70e7c8d | 2015-07-12 13:47:43 -0500 | [diff] [blame] | 133 | } |