From 1eb2aeeecf5aee4aec52d08ab66df77172df0b8f Mon Sep 17 00:00:00 2001 From: aestriplex Date: Wed, 19 Feb 2025 17:03:00 +0100 Subject: [PATCH] darwin: fix process state visualization in DarwinProcessTable --- darwin/DarwinProcess.c | 24 ------------------------ darwin/DarwinProcessTable.c | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index d08061f7b..a9359599c 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -404,22 +404,6 @@ void DarwinProcess_setFromLibprocPidinfo(DarwinProcess* proc, DarwinProcessTable } } -static ProcessState stateToChar(int run_state) { - switch (run_state) { - case TH_STATE_RUNNING: - return RUNNING; - case TH_STATE_STOPPED: - return STOPPED; - case TH_STATE_WAITING: - return WAITING; - case TH_STATE_UNINTERRUPTIBLE: - return UNINTERRUPTIBLE_WAIT; - case TH_STATE_HALTED: - return BLOCKED; - } - return UNKNOWN; -} - /* * Scan threads for process state information. * Based on: http://stackoverflow.com/questions/6788274/ios-mac-cpu-usage-for-thread @@ -473,7 +457,6 @@ void DarwinProcess_scanThreads(DarwinProcess* dp, DarwinProcessTable* dpt) { const bool hideUserlandThreads = dpt->super.super.host->settings->hideUserlandThreads; - integer_t run_state = 999; for (mach_msg_type_number_t i = 0; i < thread_count; i++) { thread_identifier_info_data_t identifer_info; @@ -517,16 +500,12 @@ void DarwinProcess_scanThreads(DarwinProcess* dp, DarwinProcessTable* dpt) { } DarwinProcess* tdproc = (DarwinProcess*)tprocess; - tdproc->super.state = stateToChar(extended_info.pth_run_state); tdproc->super.percent_cpu = extended_info.pth_cpu_usage / 10.0; tdproc->stime = extended_info.pth_system_time; tdproc->utime = extended_info.pth_user_time; tdproc->super.time = (extended_info.pth_system_time + extended_info.pth_user_time) / 10000000; tdproc->super.priority = extended_info.pth_curpri; - if (extended_info.pth_run_state < run_state) - run_state = extended_info.pth_run_state; - // TODO: depend on setting const char* name = extended_info.pth_name[0] != '\0' ? extended_info.pth_name : proc->procComm; Process_updateCmdline(tprocess, name, 0, strlen(name)); @@ -537,9 +516,6 @@ void DarwinProcess_scanThreads(DarwinProcess* dp, DarwinProcessTable* dpt) { vm_deallocate(mach_task_self(), (vm_address_t) thread_list, sizeof(thread_port_array_t) * thread_count); mach_port_deallocate(mach_task_self(), task); - - if (run_state != 999) - proc->state = stateToChar(run_state); } diff --git a/darwin/DarwinProcessTable.c b/darwin/DarwinProcessTable.c index a08c2669a..7fb0d4d69 100644 --- a/darwin/DarwinProcessTable.c +++ b/darwin/DarwinProcessTable.c @@ -28,6 +28,23 @@ in the source distribution for its full text. #include "zfs/ZfsArcStats.h" +static ProcessState ProcessTable_mapDarwinProcessState(int kinfo_proc_state) { + switch(kinfo_proc_state) { + case SRUN: + return RUNNING; + case SSLEEP: + return SLEEPING; + case SSTOP: + return STOPPED; + case SZOMB: + return ZOMBIE; + case SIDL: + return IDLE; + default: + return UNKNOWN; + } +} + static struct kinfo_proc* ProcessTable_getKInfoProcs(size_t* count) { int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; struct kinfo_proc* processes = NULL; @@ -107,6 +124,7 @@ void ProcessTable_goThroughEntries(ProcessTable* super) { proc->super.st_uid = ps[i].kp_eproc.e_ucred.cr_uid; proc->super.user = UsersTable_getRef(host->usersTable, proc->super.st_uid); } + proc->super.state = ProcessTable_mapDarwinProcessState(ps[i].kp_proc.p_stat); // Disabled for High Sierra due to bug in macOS High Sierra bool isScanThreadSupported = !Platform_KernelVersionIsBetween((KernelVersion) {17, 0, 0}, (KernelVersion) {17, 5, 0});