Skip to content

Commit

Permalink
Merge branch 'darwin-process-status' of aestriplex/htop
Browse files Browse the repository at this point in the history
Closes #622
  • Loading branch information
Daniel Lange authored and Daniel Lange committed Feb 20, 2025
2 parents b2e86aa + 1eb2aee commit e2f6be9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
24 changes: 0 additions & 24 deletions darwin/DarwinProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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);
}


Expand Down
18 changes: 18 additions & 0 deletions darwin/DarwinProcessTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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});
Expand Down

0 comments on commit e2f6be9

Please sign in to comment.