From e7372d18a1a661d8c3dba9f51e1f17b5f94171a7 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Wed, 10 Jan 2024 11:17:08 +0100 Subject: New upstream version 3.3.0 --- solaris/Platform.c | 71 +++--- solaris/Platform.h | 38 ++- solaris/SolarisMachine.c | 336 +++++++++++++++++++++++++ solaris/SolarisMachine.h | 57 +++++ solaris/SolarisProcess.c | 38 ++- solaris/SolarisProcess.h | 6 +- solaris/SolarisProcessList.c | 560 ------------------------------------------ solaris/SolarisProcessList.h | 65 ----- solaris/SolarisProcessTable.c | 268 ++++++++++++++++++++ solaris/SolarisProcessTable.h | 31 +++ 10 files changed, 785 insertions(+), 685 deletions(-) create mode 100644 solaris/SolarisMachine.c create mode 100644 solaris/SolarisMachine.h delete mode 100644 solaris/SolarisProcessList.c delete mode 100644 solaris/SolarisProcessList.h create mode 100644 solaris/SolarisProcessTable.c create mode 100644 solaris/SolarisProcessTable.h (limited to 'solaris') diff --git a/solaris/Platform.c b/solaris/Platform.c index 9c5acb5..5faa91a 100644 --- a/solaris/Platform.c +++ b/solaris/Platform.c @@ -7,6 +7,8 @@ Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ +#include "config.h" // IWYU pragma: keep + #include "solaris/Platform.h" #include @@ -34,10 +36,12 @@ in the source distribution for its full text. #include "HostnameMeter.h" #include "SysArchMeter.h" #include "UptimeMeter.h" +#include "XUtils.h" + +#include "solaris/SolarisMachine.h" + #include "zfs/ZfsArcMeter.h" #include "zfs/ZfsCompressedArcMeter.h" -#include "SolarisProcess.h" -#include "SolarisProcessList.h" const ScreenDefaults Platform_defaultScreens[] = { @@ -144,7 +148,7 @@ void Platform_setBindings(Htop_Action* keys) { (void) keys; } -int Platform_getUptime() { +int Platform_getUptime(void) { int boot_time = 0; int curr_time = time(NULL); struct utmpx* ent; @@ -173,7 +177,7 @@ void Platform_getLoadAverage(double* one, double* five, double* fifteen) { *fifteen = plat_loadavg[LOADAVG_15MIN]; } -int Platform_getMaxPid() { +pid_t Platform_getMaxPid(void) { int vproc = 32778; // Reasonable Solaris default kstat_ctl_t* kc = kstat_open(); @@ -194,15 +198,16 @@ int Platform_getMaxPid() { } double Platform_setCPUValues(Meter* this, unsigned int cpu) { - const SolarisProcessList* spl = (const SolarisProcessList*) this->pl; - unsigned int cpus = this->pl->existingCPUs; + const Machine* host = this->host; + const SolarisMachine* shost = (const SolarisMachine*) host; + unsigned int cpus = host->existingCPUs; const CPUData* cpuData = NULL; if (cpus == 1) { // single CPU box has everything in spl->cpus[0] - cpuData = &(spl->cpus[0]); + cpuData = &(shost->cpus[0]); } else { - cpuData = &(spl->cpus[cpu]); + cpuData = &(shost->cpus[cpu]); } if (!cpuData->online) { @@ -215,18 +220,17 @@ double Platform_setCPUValues(Meter* this, unsigned int cpu) { v[CPU_METER_NICE] = cpuData->nicePercent; v[CPU_METER_NORMAL] = cpuData->userPercent; - if (this->pl->settings->detailedCPUTime) { + if (super->settings->detailedCPUTime) { v[CPU_METER_KERNEL] = cpuData->systemPercent; v[CPU_METER_IRQ] = cpuData->irqPercent; this->curItems = 4; - percent = v[0] + v[1] + v[2] + v[3]; } else { - v[2] = cpuData->systemAllPercent; + v[CPU_METER_KERNEL] = cpuData->systemAllPercent; this->curItems = 3; - percent = v[0] + v[1] + v[2]; } - percent = isnan(percent) ? 0.0 : CLAMP(percent, 0.0, 100.0); + percent = sumPositiveValues(v, this->curItems); + percent = MINIMUM(percent, 100.0); v[CPU_METER_FREQUENCY] = cpuData->frequency; v[CPU_METER_TEMPERATURE] = NAN; @@ -235,32 +239,34 @@ double Platform_setCPUValues(Meter* this, unsigned int cpu) { } void Platform_setMemoryValues(Meter* this) { - const ProcessList* pl = this->pl; - this->total = pl->totalMem; - this->values[0] = pl->usedMem; - this->values[1] = pl->buffersMem; - // this->values[2] = "shared memory, like tmpfs and shm" - this->values[3] = pl->cachedMem; - // this->values[4] = "available memory" + const Machine* host = this->host; + this->total = host->totalMem; + this->values[MEMORY_METER_USED] = host->usedMem; + // this->values[MEMORY_METER_SHARED] = "shared memory, like tmpfs and shm" + // this->values[MEMORY_METER_COMPRESSED] = "compressed memory, like zswap on linux" + this->values[MEMORY_METER_BUFFERS] = host->buffersMem; + this->values[MEMORY_METER_CACHE] = host->cachedMem; + // this->values[MEMORY_METER_AVAILABLE] = "available memory" } void Platform_setSwapValues(Meter* this) { - const ProcessList* pl = this->pl; - this->total = pl->totalSwap; - this->values[0] = pl->usedSwap; - this->values[1] = NAN; + const Machine* host = this->host; + this->total = host->totalSwap; + this->values[SWAP_METER_USED] = host->usedSwap; + // this->values[SWAP_METER_CACHE] = "pages that are both in swap and RAM, like SwapCached on linux" + // this->values[SWAP_METER_FRONTSWAP] = "pages that are accounted to swap but stored elsewhere, like frontswap on linux" } void Platform_setZfsArcValues(Meter* this) { - const SolarisProcessList* spl = (const SolarisProcessList*) this->pl; + const SolarisMachine* shost = (SolarisMachine*) this->host; - ZfsArcMeter_readStats(this, &(spl->zfs)); + ZfsArcMeter_readStats(this, &shost->zfs); } void Platform_setZfsCompressedArcValues(Meter* this) { - const SolarisProcessList* spl = (const SolarisProcessList*) this->pl; + const SolarisMachine* shost = (SolarisMachine*) this->host; - ZfsCompressedArcMeter_readStats(this, &(spl->zfs)); + ZfsCompressedArcMeter_readStats(this, &shost->zfs); } static int Platform_buildenv(void* accum, struct ps_prochandle* Phandle, uintptr_t addr, const char* str) { @@ -308,15 +314,14 @@ char* Platform_getProcessEnv(pid_t pid) { return xRealloc(envBuilder.env, envBuilder.size + 1); } -char* Platform_getInodeFilename(pid_t pid, ino_t inode) { +FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) { (void)pid; - (void)inode; return NULL; } -FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) { - (void)pid; - return NULL; +void Platform_getFileDescriptors(double* used, double* max) { + *used = NAN; + *max = NAN; } bool Platform_getDiskIO(DiskIOData* data) { diff --git a/solaris/Platform.h b/solaris/Platform.h index 1b3dc9f..1a31c2e 100644 --- a/solaris/Platform.h +++ b/solaris/Platform.h @@ -9,8 +9,6 @@ Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ -#include "config.h" // IWYU pragma: keep - #include /* On OmniOS /usr/include/sys/regset.h redefines ERR to 13 - \r, breaking the Enter key. @@ -30,12 +28,12 @@ in the source distribution for its full text. #include "Action.h" #include "BatteryMeter.h" +#include "CommandLine.h" #include "DiskIOMeter.h" #include "Hashtable.h" #include "NetworkIOMeter.h" #include "ProcessLocksScreen.h" #include "SignalsPanel.h" -#include "CommandLine.h" #include "generic/gettime.h" #include "generic/hostname.h" #include "generic/uname.h" @@ -72,7 +70,7 @@ int Platform_getUptime(void); void Platform_getLoadAverage(double* one, double* five, double* fifteen); -int Platform_getMaxPid(void); +pid_t Platform_getMaxPid(void); double Platform_setCPUValues(Meter* this, unsigned int cpu); @@ -86,10 +84,10 @@ void Platform_setZfsCompressedArcValues(Meter* this); char* Platform_getProcessEnv(pid_t pid); -char* Platform_getInodeFilename(pid_t pid, ino_t inode); - FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid); +void Platform_getFileDescriptors(double* used, double* max); + bool Platform_getDiskIO(DiskIOData* data); bool Platform_getNetworkIO(NetworkIOData* data); @@ -132,7 +130,9 @@ IGNORE_WCASTQUAL_BEGIN IGNORE_WCASTQUAL_END } -static inline Hashtable* Platform_dynamicMeters(void) { return NULL; } +static inline Hashtable* Platform_dynamicMeters(void) { + return NULL; +} static inline void Platform_dynamicMetersDone(ATTR_UNUSED Hashtable* table) { } @@ -142,12 +142,30 @@ static inline void Platform_dynamicMeterUpdateValues(ATTR_UNUSED Meter* meter) { static inline void Platform_dynamicMeterDisplay(ATTR_UNUSED const Meter* meter, ATTR_UNUSED RichString* out) { } -static inline Hashtable* Platform_dynamicColumns(void) { return NULL; } +static inline Hashtable* Platform_dynamicColumns(void) { + return NULL; +} static inline void Platform_dynamicColumnsDone(ATTR_UNUSED Hashtable* table) { } -static inline const char* Platform_dynamicColumnInit(ATTR_UNUSED unsigned int key) { return NULL; } +static inline const char* Platform_dynamicColumnName(ATTR_UNUSED unsigned int key) { + return NULL; +} + +static inline bool Platform_dynamicColumnWriteField(ATTR_UNUSED const Process* proc, ATTR_UNUSED RichString* str, ATTR_UNUSED unsigned int key) { + return false; +} + +static inline Hashtable* Platform_dynamicScreens(void) { + return NULL; +} + +static inline void Platform_defaultDynamicScreens(ATTR_UNUSED Settings* settings) { } + +static inline void Platform_addDynamicScreen(ATTR_UNUSED ScreenSettings* ss) { } + +static inline void Platform_addDynamicScreenAvailableColumns(ATTR_UNUSED Panel* availableColumns, ATTR_UNUSED const char* screen) { } -static inline bool Platform_dynamicColumnWriteField(ATTR_UNUSED const Process* proc, ATTR_UNUSED RichString* str, ATTR_UNUSED unsigned int key) { return false; } +static inline void Platform_dynamicScreensDone(ATTR_UNUSED Hashtable* screens) { } #endif diff --git a/solaris/SolarisMachine.c b/solaris/SolarisMachine.c new file mode 100644 index 0000000..4f740d3 --- /dev/null +++ b/solaris/SolarisMachine.c @@ -0,0 +1,336 @@ +/* +htop - SolarisMachine.c +(C) 2014 Hisham H. Muhammad +(C) 2017,2018 Guy M. Broome +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" // IWYU pragma: keep + +#include "solaris/SolarisMachine.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "CRT.h" +#include "solaris/Platform.h" + + +static void SolarisMachine_updateCPUcount(SolarisMachine* this) { + Machine* super = &this->super; + long int s; + bool change = false; + + s = sysconf(_SC_NPROCESSORS_CONF); + if (s < 1) + CRT_fatalError("Cannot get existing CPU count by sysconf(_SC_NPROCESSORS_CONF)"); + + if (s != super->existingCPUs) { + if (s == 1) { + this->cpus = xRealloc(this->cpus, sizeof(CPUData)); + this->cpus[0].online = true; + } else { + this->cpus = xReallocArray(this->cpus, s + 1, sizeof(CPUData)); + this->cpus[0].online = true; /* average is always "online" */ + for (int i = 1; i < s + 1; i++) { + this->cpus[i].online = false; + } + } + + change = true; + super->existingCPUs = s; + } + + s = sysconf(_SC_NPROCESSORS_ONLN); + if (s < 1) + CRT_fatalError("Cannot get active CPU count by sysconf(_SC_NPROCESSORS_ONLN)"); + + if (s != super->activeCPUs) { + change = true; + hsuper->activeCPUs = s; + } + + if (change) { + kstat_close(this->kd); + this->kd = kstat_open(); + if (!this->kd) + CRT_fatalError("Cannot open kstat handle"); + } +} + + +static void SolarisMachine_scanCPUTime(SolarisMachine* this) { + Machine* super = &this->super; + unsigned int activeCPUs = super->activeCPUs; + unsigned int existingCPUs = super->existingCPUs; + kstat_t* cpuinfo = NULL; + kstat_named_t* idletime = NULL; + kstat_named_t* intrtime = NULL; + kstat_named_t* krnltime = NULL; + kstat_named_t* usertime = NULL; + kstat_named_t* cpu_freq = NULL; + double idlebuf = 0; + double intrbuf = 0; + double krnlbuf = 0; + double userbuf = 0; + int arrskip = 0; + + assert(existingCPUs > 0); + assert(this->kd); + + if (existingCPUs > 1) { + // Store values for the stats loop one extra element up in the array + // to leave room for the average to be calculated afterwards + arrskip++; + } + + // Calculate per-CPU statistics first + for (unsigned int i = 0; i < existingCPUs; i++) { + CPUData* cpuData = &(this->cpus[i + arrskip]); + + if ((cpuinfo = kstat_lookup_wrapper(this->kd, "cpu", i, "sys")) != NULL) { + cpuData->online = true; + if (kstat_read(this->kd, cpuinfo, NULL) != -1) { + idletime = kstat_data_lookup_wrapper(cpuinfo, "cpu_nsec_idle"); + intrtime = kstat_data_lookup_wrapper(cpuinfo, "cpu_nsec_intr"); + krnltime = kstat_data_lookup_wrapper(cpuinfo, "cpu_nsec_kernel"); + usertime = kstat_data_lookup_wrapper(cpuinfo, "cpu_nsec_user"); + } + } else { + cpuData->online = false; + continue; + } + + assert( (idletime != NULL) && (intrtime != NULL) + && (krnltime != NULL) && (usertime != NULL) ); + + if (super->settings->showCPUFrequency) { + if ((cpuinfo = kstat_lookup_wrapper(this->kd, "cpu_info", i, NULL)) != NULL) { + if (kstat_read(this->kd, cpuinfo, NULL) != -1) { + cpu_freq = kstat_data_lookup_wrapper(cpuinfo, "current_clock_Hz"); + } + } + + assert( cpu_freq != NULL ); + } + + uint64_t totaltime = (idletime->value.ui64 - cpuData->lidle) + + (intrtime->value.ui64 - cpuData->lintr) + + (krnltime->value.ui64 - cpuData->lkrnl) + + (usertime->value.ui64 - cpuData->luser); + + // Calculate percentages of deltas since last reading + cpuData->userPercent = ((usertime->value.ui64 - cpuData->luser) / (double)totaltime) * 100.0; + cpuData->nicePercent = (double)0.0; // Not implemented on Solaris + cpuData->systemPercent = ((krnltime->value.ui64 - cpuData->lkrnl) / (double)totaltime) * 100.0; + cpuData->irqPercent = ((intrtime->value.ui64 - cpuData->lintr) / (double)totaltime) * 100.0; + cpuData->systemAllPercent = cpuData->systemPercent + cpuData->irqPercent; + cpuData->idlePercent = ((idletime->value.ui64 - cpuData->lidle) / (double)totaltime) * 100.0; + // Store current values to use for the next round of deltas + cpuData->luser = usertime->value.ui64; + cpuData->lkrnl = krnltime->value.ui64; + cpuData->lintr = intrtime->value.ui64; + cpuData->lidle = idletime->value.ui64; + // Add frequency in MHz + cpuData->frequency = super->settings->showCPUFrequency ? (double)cpu_freq->value.ui64 / 1E6 : NAN; + // Accumulate the current percentages into buffers for later average calculation + if (existingCPUs > 1) { + userbuf += cpuData->userPercent; + krnlbuf += cpuData->systemPercent; + intrbuf += cpuData->irqPercent; + idlebuf += cpuData->idlePercent; + } + } + + if (existingCPUs > 1) { + CPUData* cpuData = &(this->cpus[0]); + cpuData->userPercent = userbuf / activeCPUs; + cpuData->nicePercent = (double)0.0; // Not implemented on Solaris + cpuData->systemPercent = krnlbuf / activeCPUs; + cpuData->irqPercent = intrbuf / activeCPUs; + cpuData->systemAllPercent = cpuData->systemPercent + cpuData->irqPercent; + cpuData->idlePercent = idlebuf / activeCPUs; + } +} + +static void SolarisMachine_scanMemoryInfo(SolarisMachine* this) { + Machine* super = &this->super; + static kstat_t *meminfo = NULL; + int ksrphyserr = -1; + kstat_named_t *totalmem_pgs = NULL; + kstat_named_t *freemem_pgs = NULL; + kstat_named_t *pages = NULL; + struct swaptable *sl = NULL; + struct swapent *swapdev = NULL; + uint64_t totalswap = 0; + uint64_t totalfree = 0; + int nswap = 0; + char *spath = NULL; + char *spathbase = NULL; + + // Part 1 - physical memory + if (this->kd != NULL && meminfo == NULL) { + // Look up the kstat chain just once, it never changes + meminfo = kstat_lookup_wrapper(this->kd, "unix", 0, "system_pages"); + } + if (meminfo != NULL) { + ksrphyserr = kstat_read(this->kd, meminfo, NULL); + } + if (ksrphyserr != -1) { + totalmem_pgs = kstat_data_lookup_wrapper(meminfo, "physmem"); + freemem_pgs = kstat_data_lookup_wrapper(meminfo, "freemem"); + pages = kstat_data_lookup_wrapper(meminfo, "pagestotal"); + + super->totalMem = totalmem_pgs->value.ui64 * this->pageSizeKB; + if (super->totalMem > freemem_pgs->value.ui64 * this->pageSizeKB) { + super->usedMem = super->totalMem - freemem_pgs->value.ui64 * this->pageSizeKB; + } else { + super->usedMem = 0; // This can happen in non-global zone (in theory) + } + // Not sure how to implement this on Solaris - suggestions welcome! + super->cachedMem = 0; + // Not really "buffers" but the best Solaris analogue that I can find to + // "memory in use but not by programs or the kernel itself" + super->buffersMem = (totalmem_pgs->value.ui64 - pages->value.ui64) * this->pageSizeKB; + } else { + // Fall back to basic sysconf if kstat isn't working + super->totalMem = sysconf(_SC_PHYS_PAGES) * this->pageSize; + super->buffersMem = 0; + super->cachedMem = 0; + super->usedMem = super->totalMem - (sysconf(_SC_AVPHYS_PAGES) * this->pageSize); + } + + // Part 2 - swap + nswap = swapctl(SC_GETNSWP, NULL); + if (nswap > 0) { + sl = xMalloc((nswap * sizeof(swapent_t)) + sizeof(int)); + } + if (sl != NULL) { + spathbase = xMalloc( nswap * MAXPATHLEN ); + } + if (spathbase != NULL) { + spath = spathbase; + swapdev = sl->swt_ent; + for (int i = 0; i < nswap; i++, swapdev++) { + swapdev->ste_path = spath; + spath += MAXPATHLEN; + } + sl->swt_n = nswap; + } + nswap = swapctl(SC_LIST, sl); + if (nswap > 0) { + swapdev = sl->swt_ent; + for (int i = 0; i < nswap; i++, swapdev++) { + totalswap += swapdev->ste_pages; + totalfree += swapdev->ste_free; + } + } + free(spathbase); + free(sl); + super->totalSwap = totalswap * this->pageSizeKB; + super->usedSwap = super->totalSwap - (totalfree * this->pageSizeKB); +} + +static void SolarisMachine_scanZfsArcstats(SolarisMachine* this) { + kstat_named_t *cur_kstat; + kstat_t *arcstats; + int ksrphyserr; + + if (this->kd == NULL) + return; + + arcstats = kstat_lookup_wrapper(this->kd, "zfs", 0, "arcstats"); + if (arcstats == NULL) + return; + + ksrphyserr = kstat_read(this->kd, arcstats, NULL); + if (ksrphyserr == -1) + return; + + cur_kstat = kstat_data_lookup_wrapper( arcstats, "size" ); + this->zfs.size = cur_kstat->value.ui64 / 1024; + this->zfs.enabled = this->zfs.size > 0 ? 1 : 0; + + cur_kstat = kstat_data_lookup_wrapper( arcstats, "c_max" ); + this->zfs.max = cur_kstat->value.ui64 / 1024; + + cur_kstat = kstat_data_lookup_wrapper( arcstats, "mfu_size" ); + this->zfs.MFU = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0; + + cur_kstat = kstat_data_lookup_wrapper( arcstats, "mru_size" ); + this->zfs.MRU = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0; + + cur_kstat = kstat_data_lookup_wrapper( arcstats, "anon_size" ); + this->zfs.anon = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0; + + cur_kstat = kstat_data_lookup_wrapper( arcstats, "hdr_size" ); + this->zfs.header = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0; + + cur_kstat = kstat_data_lookup_wrapper( arcstats, "other_size" ); + this->zfs.other = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0; + + if ((cur_kstat = kstat_data_lookup_wrapper( arcstats, "compressed_size" )) != NULL) { + this->zfs.compressed = cur_kstat->value.ui64 / 1024; + this->zfs.isCompressed = 1; + + cur_kstat = kstat_data_lookup_wrapper( arcstats, "uncompressed_size" ); + this->zfs.uncompressed = cur_kstat->value.ui64 / 1024; + } else { + this->zfs.isCompressed = 0; + } +} + +void Machine_scan(Machine* super) { + SolarisMachine* this = (SolarisMachine*) super; + + SolarisMachine_updateCPUcount(this); + SolarisMachine_scanCPUTime(this); + SolarisMachine_scanMemoryInfo(this); + SolarisMachine_scanZfsArcstats(this); +} + +Machine* Machine_new(UsersTable* usersTable, uid_t userId) { + SolarisMachine* this = xCalloc(1, sizeof(SolarisMachine)); + Machine* super = &this->super; + + Machine_init(super, usersTable, userId); + + this->pageSize = sysconf(_SC_PAGESIZE); + if (this->pageSize == -1) + CRT_fatalError("Cannot get pagesize by sysconf(_SC_PAGESIZE)"); + this->pageSizeKB = this->pageSize / 1024; + + SolarisMachine_updateCPUcount(this); + + return super; +} + +void Machine_delete(Machine* super) { + SolarisMachine* this = (SolarisMachine*) super; + + Machine_done(super); + + free(this->cpus); + if (this->kd) { + kstat_close(this->kd); + } + free(this); +} + +bool Machine_isCPUonline(const Machine* super, unsigned int id) { + assert(id < super->existingCPUs); + + const SolarisMachine* this = (const SolarisMachine*) super; + + return (super->existingCPUs == 1) ? true : this->cpus[id + 1].online; +} diff --git a/solaris/SolarisMachine.h b/solaris/SolarisMachine.h new file mode 100644 index 0000000..2208a88 --- /dev/null +++ b/solaris/SolarisMachine.h @@ -0,0 +1,57 @@ +#ifndef HEADER_SolarisMachine +#define HEADER_SolarisMachine +/* +htop - SolarisMachine.h +(C) 2014 Hisham H. Muhammad +(C) 2017,2018 Guy M. Broome +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Hashtable.h" +#include "UsersTable.h" + +#include "zfs/ZfsArcStats.h" + + +#define ZONE_ERRMSGLEN 1024 +extern char zone_errmsg[ZONE_ERRMSGLEN]; + +typedef struct CPUData_ { + double userPercent; + double nicePercent; + double systemPercent; + double irqPercent; + double idlePercent; + double systemAllPercent; + double frequency; + uint64_t luser; + uint64_t lkrnl; + uint64_t lintr; + uint64_t lidle; + bool online; +} CPUData; + +typedef struct SolarisMachine_ { + Machine super; + + kstat_ctl_t* kd; + CPUData* cpus; + + int pageSize; + int pageSizeKB; + + ZfsArcStats zfs; +} SolarisMachine; + +#endif diff --git a/solaris/SolarisProcess.c b/solaris/SolarisProcess.c index 840757e..449861b 100644 --- a/solaris/SolarisProcess.c +++ b/solaris/SolarisProcess.c @@ -6,6 +6,8 @@ Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ +#include "config.h" // IWYU pragma: keep + #include "solaris/SolarisProcess.h" #include @@ -14,7 +16,7 @@ in the source distribution for its full text. #include #include "Process.h" -#include "ProcessList.h" +#include "ProcessTable.h" #include "CRT.h" #include "solaris/Platform.h" @@ -59,10 +61,10 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [LWPID] = { .name = "LWPID", .title = "LWPID", .description = "LWP ID", .flags = 0, .pidColumn = true, }, }; -Process* SolarisProcess_new(const Settings* settings) { +Process* SolarisProcess_new(const Machine* host) { SolarisProcess* this = xCalloc(1, sizeof(SolarisProcess)); Object_setClass(this, Class(SolarisProcess)); - Process_init(&this->super, settings); + Process_init(&this->super, host); return &this->super; } @@ -73,11 +75,13 @@ void Process_delete(Object* cast) { free(sp); } -static void SolarisProcess_writeField(const Process* this, RichString* str, ProcessField field) { - const SolarisProcess* sp = (const SolarisProcess*) this; +static void SolarisProcess_rowWriteField(const Row* super, RichString* str, ProcessField field) { + const SolarisProcess* sp = (const SolarisProcess*) super; + char buffer[256]; buffer[255] = '\0'; int attr = CRT_colors[DEFAULT_COLOR]; - int n = sizeof(buffer) - 1; + size_t n = sizeof(buffer) - 1; + switch (field) { // add Solaris-specific fields here case ZONEID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->zoneid); break; @@ -85,15 +89,16 @@ static void SolarisProcess_writeField(const Process* this, RichString* str, Proc case TASKID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->taskid); break; case POOLID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->poolid); break; case CONTID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->contid); break; - case ZONE: Process_printLeftAlignedField(str, attr, sp->zname ? sp->zname : "global", ZONENAME_MAX/4); return; + case ZONE: Row_printLeftAlignedField(str, attr, sp->zname ? sp->zname : "global", ZONENAME_MAX/4); return; case PID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->realpid); break; case PPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->realppid); break; case TGID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->realtgid); break; case LWPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->lwpid); break; default: - Process_writeField(this, str, field); + Process_writeField(&sp->super, str, field); return; } + RichString_appendWide(str, attr, buffer); } @@ -127,11 +132,18 @@ static int SolarisProcess_compareByKey(const Process* v1, const Process* v2, Pro const ProcessClass SolarisProcess_class = { .super = { - .extends = Class(Process), - .display = Process_display, - .delete = Process_delete, - .compare = Process_compare + .super = { + .extends = Class(Process), + .display = Row_display, + .delete = Process_delete, + .compare = Process_compare + }, + .isHighlighted = Process_rowIsHighlighted, + .isVisible = Process_rowIsVisible, + .matchesFilter = Process_rowMatchesFilter, + .compareByParent = Process_compareByParent, + .sortKeyString = Process_rowGetSortKey, + .writeField = SolarisProcess_rowWriteField }, - .writeField = SolarisProcess_writeField, .compareByKey = SolarisProcess_compareByKey }; diff --git a/solaris/SolarisProcess.h b/solaris/SolarisProcess.h index 13a2bc1..1a8d18c 100644 --- a/solaris/SolarisProcess.h +++ b/solaris/SolarisProcess.h @@ -8,8 +8,6 @@ Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ -#include "config.h" // IWYU pragma: keep - #include #include @@ -21,7 +19,7 @@ in the source distribution for its full text. #undef ERR #define ERR (-1) -#include "Settings.h" +#include "Machine.h" typedef struct SolarisProcess_ { @@ -42,7 +40,7 @@ extern const ProcessClass SolarisProcess_class; extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD]; -Process* SolarisProcess_new(const Settings* settings); +Process* SolarisProcess_new(const Machine* host); void Process_delete(Object* cast); diff --git a/solaris/SolarisProcessList.c b/solaris/SolarisProcessList.c deleted file mode 100644 index 905cfbd..0000000 --- a/solaris/SolarisProcessList.c +++ /dev/null @@ -1,560 +0,0 @@ -/* -htop - SolarisProcessList.c -(C) 2014 Hisham H. Muhammad -(C) 2017,2018 Guy M. Broome -Released under the GNU GPLv2+, see the COPYING file -in the source distribution for its full text. -*/ - - -#include "solaris/SolarisProcessList.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "CRT.h" -#include "solaris/Platform.h" -#include "solaris/SolarisProcess.h" - - -#define GZONE "global " -#define UZONE "unknown " - -static int pageSize; -static int pageSizeKB; - -static char* SolarisProcessList_readZoneName(kstat_ctl_t* kd, SolarisProcess* sproc) { - char* zname; - - if ( sproc->zoneid == 0 ) { - zname = xStrdup(GZONE); - } else if ( kd == NULL ) { - zname = xStrdup(UZONE); - } else { - kstat_t* ks = kstat_lookup_wrapper( kd, "zones", sproc->zoneid, NULL ); - zname = xStrdup(ks == NULL ? UZONE : ks->ks_name); - } - - return zname; -} - -static void SolarisProcessList_updateCPUcount(ProcessList* super) { - SolarisProcessList* spl = (SolarisProcessList*) super; - long int s; - bool change = false; - - s = sysconf(_SC_NPROCESSORS_CONF); - if (s < 1) - CRT_fatalError("Cannot get existing CPU count by sysconf(_SC_NPROCESSORS_CONF)"); - - if (s != super->existingCPUs) { - if (s == 1) { - spl->cpus = xRealloc(spl->cpus, sizeof(CPUData)); - spl->cpus[0].online = true; - } else { - spl->cpus = xReallocArray(spl->cpus, s + 1, sizeof(CPUData)); - spl->cpus[0].online = true; /* average is always "online" */ - for (int i = 1; i < s + 1; i++) { - spl->cpus[i].online = false; - } - } - - change = true; - super->existingCPUs = s; - } - - s = sysconf(_SC_NPROCESSORS_ONLN); - if (s < 1) - CRT_fatalError("Cannot get active CPU count by sysconf(_SC_NPROCESSORS_ONLN)"); - - if (s != super->activeCPUs) { - change = true; - super->activeCPUs = s; - } - - if (change) { - kstat_close(spl->kd); - spl->kd = kstat_open(); - if (!spl->kd) - CRT_fatalError("Cannot open kstat handle"); - } -} - -ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* pidMatchList, uid_t userId) { - SolarisProcessList* spl = xCalloc(1, sizeof(SolarisProcessList)); - ProcessList* pl = (ProcessList*) spl; - ProcessList_init(pl, Class(SolarisProcess), usersTable, dynamicMeters, dynamicColumns, pidMatchList, userId); - - spl->kd = kstat_open(); - if (!spl->kd) - CRT_fatalError("Cannot open kstat handle"); - - pageSize = sysconf(_SC_PAGESIZE); - if (pageSize == -1) - CRT_fatalError("Cannot get pagesize by sysconf(_SC_PAGESIZE)"); - pageSizeKB = pageSize / 1024; - - SolarisProcessList_updateCPUcount(pl); - - return pl; -} - -static inline void SolarisProcessList_scanCPUTime(ProcessList* pl) { - const SolarisProcessList* spl = (SolarisProcessList*) pl; - unsigned int activeCPUs = pl->activeCPUs; - unsigned int existingCPUs = pl->existingCPUs; - kstat_t* cpuinfo = NULL; - kstat_named_t* idletime = NULL; - kstat_named_t* intrtime = NULL; - kstat_named_t* krnltime = NULL; - kstat_named_t* usertime = NULL; - kstat_named_t* cpu_freq = NULL; - double idlebuf = 0; - double intrbuf = 0; - double krnlbuf = 0; - double userbuf = 0; - int arrskip = 0; - - assert(existingCPUs > 0); - assert(spl->kd); - - if (existingCPUs > 1) { - // Store values for the stats loop one extra element up in the array - // to leave room for the average to be calculated afterwards - arrskip++; - } - - // Calculate per-CPU statistics first - for (unsigned int i = 0; i < existingCPUs; i++) { - CPUData* cpuData = &(spl->cpus[i + arrskip]); - - if ((cpuinfo = kstat_lookup_wrapper(spl->kd, "cpu", i, "sys")) != NULL) { - cpuData->online = true; - if (kstat_read(spl->kd, cpuinfo, NULL) != -1) { - idletime = kstat_data_lookup_wrapper(cpuinfo, "cpu_nsec_idle"); - intrtime = kstat_data_lookup_wrapper(cpuinfo, "cpu_nsec_intr"); - krnltime = kstat_data_lookup_wrapper(cpuinfo, "cpu_nsec_kernel"); - usertime = kstat_data_lookup_wrapper(cpuinfo, "cpu_nsec_user"); - } - } else { - cpuData->online = false; - continue; - } - - assert( (idletime != NULL) && (intrtime != NULL) - && (krnltime != NULL) && (usertime != NULL) ); - - if (pl->settings->showCPUFrequency) { - if ((cpuinfo = kstat_lookup_wrapper(spl->kd, "cpu_info", i, NULL)) != NULL) { - if (kstat_read(spl->kd, cpuinfo, NULL) != -1) { - cpu_freq = kstat_data_lookup_wrapper(cpuinfo, "current_clock_Hz"); - } - } - - assert( cpu_freq != NULL ); - } - - uint64_t totaltime = (idletime->value.ui64 - cpuData->lidle) - + (intrtime->value.ui64 - cpuData->lintr) - + (krnltime->value.ui64 - cpuData->lkrnl) - + (usertime->value.ui64 - cpuData->luser); - - // Calculate percentages of deltas since last reading - cpuData->userPercent = ((usertime->value.ui64 - cpuData->luser) / (double)totaltime) * 100.0; - cpuData->nicePercent = (double)0.0; // Not implemented on Solaris - cpuData->systemPercent = ((krnltime->value.ui64 - cpuData->lkrnl) / (double)totaltime) * 100.0; - cpuData->irqPercent = ((intrtime->value.ui64 - cpuData->lintr) / (double)totaltime) * 100.0; - cpuData->systemAllPercent = cpuData->systemPercent + cpuData->irqPercent; - cpuData->idlePercent = ((idletime->value.ui64 - cpuData->lidle) / (double)totaltime) * 100.0; - // Store current values to use for the next round of deltas - cpuData->luser = usertime->value.ui64; - cpuData->lkrnl = krnltime->value.ui64; - cpuData->lintr = intrtime->value.ui64; - cpuData->lidle = idletime->value.ui64; - // Add frequency in MHz - cpuData->frequency = pl->settings->showCPUFrequency ? (double)cpu_freq->value.ui64 / 1E6 : NAN; - // Accumulate the current percentages into buffers for later average calculation - if (existingCPUs > 1) { - userbuf += cpuData->userPercent; - krnlbuf += cpuData->systemPercent; - intrbuf += cpuData->irqPercent; - idlebuf += cpuData->idlePercent; - } - } - - if (existingCPUs > 1) { - CPUData* cpuData = &(spl->cpus[0]); - cpuData->userPercent = userbuf / activeCPUs; - cpuData->nicePercent = (double)0.0; // Not implemented on Solaris - cpuData->systemPercent = krnlbuf / activeCPUs; - cpuData->irqPercent = intrbuf / activeCPUs; - cpuData->systemAllPercent = cpuData->systemPercent + cpuData->irqPercent; - cpuData->idlePercent = idlebuf / activeCPUs; - } -} - -static inline void SolarisProcessList_scanMemoryInfo(ProcessList* pl) { - SolarisProcessList* spl = (SolarisProcessList*) pl; - static kstat_t *meminfo = NULL; - int ksrphyserr = -1; - kstat_named_t *totalmem_pgs = NULL; - kstat_named_t *freemem_pgs = NULL; - kstat_named_t *pages = NULL; - struct swaptable *sl = NULL; - struct swapent *swapdev = NULL; - uint64_t totalswap = 0; - uint64_t totalfree = 0; - int nswap = 0; - char *spath = NULL; - char *spathbase = NULL; - - // Part 1 - physical memory - if (spl->kd != NULL && meminfo == NULL) { - // Look up the kstat chain just once, it never changes - meminfo = kstat_lookup_wrapper(spl->kd, "unix", 0, "system_pages"); - } - if (meminfo != NULL) { - ksrphyserr = kstat_read(spl->kd, meminfo, NULL); - } - if (ksrphyserr != -1) { - totalmem_pgs = kstat_data_lookup_wrapper(meminfo, "physmem"); - freemem_pgs = kstat_data_lookup_wrapper(meminfo, "freemem"); - pages = kstat_data_lookup_wrapper(meminfo, "pagestotal"); - - pl->totalMem = totalmem_pgs->value.ui64 * pageSizeKB; - if (pl->totalMem > freemem_pgs->value.ui64 * pageSizeKB) { - pl->usedMem = pl->totalMem - freemem_pgs->value.ui64 * pageSizeKB; - } else { - pl->usedMem = 0; // This can happen in non-global zone (in theory) - } - // Not sure how to implement this on Solaris - suggestions welcome! - pl->cachedMem = 0; - // Not really "buffers" but the best Solaris analogue that I can find to - // "memory in use but not by programs or the kernel itself" - pl->buffersMem = (totalmem_pgs->value.ui64 - pages->value.ui64) * pageSizeKB; - } else { - // Fall back to basic sysconf if kstat isn't working - pl->totalMem = sysconf(_SC_PHYS_PAGES) * pageSize; - pl->buffersMem = 0; - pl->cachedMem = 0; - pl->usedMem = pl->totalMem - (sysconf(_SC_AVPHYS_PAGES) * pageSize); - } - - // Part 2 - swap - nswap = swapctl(SC_GETNSWP, NULL); - if (nswap > 0) { - sl = xMalloc((nswap * sizeof(swapent_t)) + sizeof(int)); - } - if (sl != NULL) { - spathbase = xMalloc( nswap * MAXPATHLEN ); - } - if (spathbase != NULL) { - spath = spathbase; - swapdev = sl->swt_ent; - for (int i = 0; i < nswap; i++, swapdev++) { - swapdev->ste_path = spath; - spath += MAXPATHLEN; - } - sl->swt_n = nswap; - } - nswap = swapctl(SC_LIST, sl); - if (nswap > 0) { - swapdev = sl->swt_ent; - for (int i = 0; i < nswap; i++, swapdev++) { - totalswap += swapdev->ste_pages; - totalfree += swapdev->ste_free; - } - } - free(spathbase); - free(sl); - pl->totalSwap = totalswap * pageSizeKB; - pl->usedSwap = pl->totalSwap - (totalfree * pageSizeKB); -} - -static inline void SolarisProcessList_scanZfsArcstats(ProcessList* pl) { - SolarisProcessList* spl = (SolarisProcessList*) pl; - kstat_t *arcstats = NULL; - int ksrphyserr = -1; - kstat_named_t *cur_kstat = NULL; - - if (spl->kd != NULL) { - arcstats = kstat_lookup_wrapper(spl->kd, "zfs", 0, "arcstats"); - } - if (arcstats != NULL) { - ksrphyserr = kstat_read(spl->kd, arcstats, NULL); - } - if (ksrphyserr != -1) { - cur_kstat = kstat_data_lookup_wrapper( arcstats, "size" ); - spl->zfs.size = cur_kstat->value.ui64 / 1024; - spl->zfs.enabled = spl->zfs.size > 0 ? 1 : 0; - - cur_kstat = kstat_data_lookup_wrapper( arcstats, "c_max" ); - spl->zfs.max = cur_kstat->value.ui64 / 1024; - - cur_kstat = kstat_data_lookup_wrapper( arcstats, "mfu_size" ); - spl->zfs.MFU = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0; - - cur_kstat = kstat_data_lookup_wrapper( arcstats, "mru_size" ); - spl->zfs.MRU = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0; - - cur_kstat = kstat_data_lookup_wrapper( arcstats, "anon_size" ); - spl->zfs.anon = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0; - - cur_kstat = kstat_data_lookup_wrapper( arcstats, "hdr_size" ); - spl->zfs.header = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0; - - cur_kstat = kstat_data_lookup_wrapper( arcstats, "other_size" ); - spl->zfs.other = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0; - - if ((cur_kstat = kstat_data_lookup_wrapper( arcstats, "compressed_size" )) != NULL) { - spl->zfs.compressed = cur_kstat->value.ui64 / 1024; - spl->zfs.isCompressed = 1; - - cur_kstat = kstat_data_lookup_wrapper( arcstats, "uncompressed_size" ); - spl->zfs.uncompressed = cur_kstat->value.ui64 / 1024; - } else { - spl->zfs.isCompressed = 0; - } - } -} - -void ProcessList_delete(ProcessList* pl) { - SolarisProcessList* spl = (SolarisProcessList*) pl; - ProcessList_done(pl); - free(spl->cpus); - if (spl->kd) { - kstat_close(spl->kd); - } - free(spl); -} - -static void SolarisProcessList_updateExe(pid_t pid, Process* proc) { - char path[32]; - xSnprintf(path, sizeof(path), "/proc/%d/path/a.out", pid); - - char target[PATH_MAX]; - ssize_t ret = readlink(path, target, sizeof(target) - 1); - if (ret <= 0) - return; - - target[ret] = '\0'; - Process_updateExe(proc, target); -} - -static void SolarisProcessList_updateCwd(pid_t pid, Process* proc) { - char path[32]; - xSnprintf(path, sizeof(path), "/proc/%d/cwd", pid); - - char target[PATH_MAX]; - ssize_t ret = readlink(path, target, sizeof(target) - 1); - if (ret <= 0) - return; - - target[ret] = '\0'; - free_and_xStrdup(&proc->procCwd, target); -} - -/* Taken from: https://docs.oracle.com/cd/E19253-01/817-6223/6mlkidlom/index.html#tbl-sched-state */ -static inline ProcessState SolarisProcessList_getProcessState(char state) { - switch (state) { - case 'S': return SLEEPING; - case 'R': return RUNNABLE; - case 'O': return RUNNING; - case 'Z': return ZOMBIE; - case 'T': return STOPPED; - case 'I': return IDLE; - default: return UNKNOWN; - } -} - -/* NOTE: the following is a callback function of type proc_walk_f - * and MUST conform to the appropriate definition in order - * to work. See libproc(3LIB) on a Solaris or Illumos - * system for more info. - */ - -static int SolarisProcessList_walkproc(psinfo_t* _psinfo, lwpsinfo_t* _lwpsinfo, void* listptr) { - bool preExisting; - pid_t getpid; - - // Setup process list - ProcessList* pl = (ProcessList*) listptr; - SolarisProcessList* spl = (SolarisProcessList*) listptr; - - id_t lwpid_real = _lwpsinfo->pr_lwpid; - if (lwpid_real > 1023) { - return 0; - } - - pid_t lwpid = (_psinfo->pr_pid * 1024) + lwpid_real; - bool onMasterLWP = (_lwpsinfo->pr_lwpid == _psinfo->pr_lwp.pr_lwpid); - if (onMasterLWP) { - getpid = _psinfo->pr_pid * 1024; - } else { - getpid = lwpid; - } - - Process* proc = ProcessList_getProcess(pl, getpid, &preExisting, SolarisProcess_new); - SolarisProcess* sproc = (SolarisProcess*) proc; - - // Common code pass 1 - proc->show = false; - sproc->taskid = _psinfo->pr_taskid; - sproc->projid = _psinfo->pr_projid; - sproc->poolid = _psinfo->pr_poolid; - sproc->contid = _psinfo->pr_contract; - proc->priority = _lwpsinfo->pr_pri; - proc->nice = _lwpsinfo->pr_nice - NZERO; - proc->processor = _lwpsinfo->pr_onpro; - proc->state = SolarisProcessList_getProcessState(_lwpsinfo->pr_sname); - // NOTE: This 'percentage' is a 16-bit BINARY FRACTIONS where 1.0 = 0x8000 - // Source: https://docs.oracle.com/cd/E19253-01/816-5174/proc-4/index.html - // (accessed on 18 November 2017) - proc->percent_mem = ((uint16_t)_psinfo->pr_pctmem / (double)32768) * (double)100.0; - proc->pgrp = _psinfo->pr_pgid; - proc->nlwp = _psinfo->pr_nlwp; - proc->session = _psinfo->pr_sid; - - proc->tty_nr = _psinfo->pr_ttydev; - const char* name = (_psinfo->pr_ttydev != PRNODEV) ? ttyname(_psinfo->pr_ttydev) : NULL; - if (!name) { - free(proc->tty_name); - proc->tty_name = NULL; - } else { - free_and_xStrdup(&proc->tty_name, name); - } - - proc->m_resident = _psinfo->pr_rssize; // KB - proc->m_virt = _psinfo->pr_size; // KB - - if (proc->st_uid != _psinfo->pr_euid) { - proc->st_uid = _psinfo->pr_euid; - proc->user = UsersTable_getRef(pl->usersTable, proc->st_uid); - } - - if (!preExisting) { - sproc->realpid = _psinfo->pr_pid; - sproc->lwpid = lwpid_real; - sproc->zoneid = _psinfo->pr_zoneid; - sproc->zname = SolarisProcessList_readZoneName(spl->kd, sproc); - SolarisProcessList_updateExe(_psinfo->pr_pid, proc); - - Process_updateComm(proc, _psinfo->pr_fname); - Process_updateCmdline(proc, _psinfo->pr_psargs, 0, 0); - - if (proc->settings->ss->flags & PROCESS_FLAG_CWD) { - SolarisProcessList_updateCwd(_psinfo->pr_pid, proc); - } - } - - // End common code pass 1 - - if (onMasterLWP) { // Are we on the representative LWP? - proc->ppid = (_psinfo->pr_ppid * 1024); - proc->tgid = (_psinfo->pr_ppid * 1024); - sproc->realppid = _psinfo->pr_ppid; - sproc->realtgid = _psinfo->pr_ppid; - - // See note above (in common section) about this BINARY FRACTION - proc->percent_cpu = ((uint16_t)_psinfo->pr_pctcpu / (double)32768) * (double)100.0; - Process_updateCPUFieldWidths(proc->percent_cpu); - - proc->time = _psinfo->pr_time.tv_sec * 100 + _psinfo->pr_time.tv_nsec / 10000000; - if (!preExisting) { // Tasks done only for NEW processes - proc->isUserlandThread = false; - proc->starttime_ctime = _psinfo->pr_start.tv_sec; - } - - // Update proc and thread counts based on settings - if (proc->isKernelThread && !pl->settings->hideKernelThreads) { - pl->kernelThreads += proc->nlwp; - pl->totalTasks += proc->nlwp + 1; - if (proc->state == RUNNING) { - pl->runningTasks++; - } - } else if (!proc->isKernelThread) { - if (proc->state == RUNNING) { - pl->runningTasks++; - } - if (pl->settings->hideUserlandThreads) { - pl->totalTasks++; - } else { - pl->userlandThreads += proc->nlwp; - pl->totalTasks += proc->nlwp + 1; - } - } - proc->show = !(pl->settings->hideKernelThreads && proc->isKernelThread); - } else { // We are not in the master LWP, so jump to the LWP handling code - proc->percent_cpu = ((uint16_t)_lwpsinfo->pr_pctcpu / (double)32768) * (double)100.0; - Process_updateCPUFieldWidths(proc->percent_cpu); - - proc->time = _lwpsinfo->pr_time.tv_sec * 100 + _lwpsinfo->pr_time.tv_nsec / 10000000; - if (!preExisting) { // Tasks done only for NEW LWPs - proc->isUserlandThread = true; - proc->ppid = _psinfo->pr_pid * 1024; - proc->tgid = _psinfo->pr_pid * 1024; - sproc->realppid = _psinfo->pr_pid; - sproc->realtgid = _psinfo->pr_pid; - proc->starttime_ctime = _lwpsinfo->pr_start.tv_sec; - } - - // Top-level process only gets this for the representative LWP - if (proc->isKernelThread && !pl->settings->hideKernelThreads) { - proc->show = true; - } - if (!proc->isKernelThread && !pl->settings->hideUserlandThreads) { - proc->show = true; - } - } // Top-level LWP or subordinate LWP - - // Common code pass 2 - - if (!preExisting) { - if ((sproc->realppid <= 0) && !(sproc->realpid <= 1)) { - proc->isKernelThread = true; - } else { - proc->isKernelThread = false; - } - - Process_fillStarttimeBuffer(proc); - ProcessList_add(pl, proc); - } - - proc->updated = true; - - // End common code pass 2 - - return 0; -} - -void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { - SolarisProcessList_updateCPUcount(super); - SolarisProcessList_scanCPUTime(super); - SolarisProcessList_scanMemoryInfo(super); - SolarisProcessList_scanZfsArcstats(super); - - // in pause mode only gather global data for meters (CPU/memory/...) - if (pauseProcessUpdate) { - return; - } - - super->kernelThreads = 1; - proc_walk(&SolarisProcessList_walkproc, super, PR_WALK_LWP); -} - -bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id) { - assert(id < super->existingCPUs); - - const SolarisProcessList* spl = (const SolarisProcessList*) super; - - return (super->existingCPUs == 1) ? true : spl->cpus[id + 1].online; -} diff --git a/solaris/SolarisProcessList.h b/solaris/SolarisProcessList.h deleted file mode 100644 index 91fd4f4..0000000 --- a/solaris/SolarisProcessList.h +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef HEADER_SolarisProcessList -#define HEADER_SolarisProcessList -/* -htop - SolarisProcessList.h -(C) 2014 Hisham H. Muhammad -(C) 2017,2018 Guy M. Broome -Released under the GNU GPLv2+, see the COPYING file -in the source distribution for its full text. -*/ - -#include "config.h" // IWYU pragma: keep - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "Hashtable.h" -#include "ProcessList.h" -#include "UsersTable.h" - -#include "solaris/SolarisProcess.h" - -#include "zfs/ZfsArcStats.h" - - -#define ZONE_ERRMSGLEN 1024 -extern char zone_errmsg[ZONE_ERRMSGLEN]; - -typedef struct CPUData_ { - double userPercent; - double nicePercent; - double systemPercent; - double irqPercent; - double idlePercent; - double systemAllPercent; - double frequency; - uint64_t luser; - uint64_t lkrnl; - uint64_t lintr; - uint64_t lidle; - bool online; -} CPUData; - -typedef struct SolarisProcessList_ { - ProcessList super; - kstat_ctl_t* kd; - CPUData* cpus; - ZfsArcStats zfs; -} SolarisProcessList; - -ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* pidMatchList, uid_t userId); - -void ProcessList_delete(ProcessList* pl); - -void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate); - -bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id); - -#endif diff --git a/solaris/SolarisProcessTable.c b/solaris/SolarisProcessTable.c new file mode 100644 index 0000000..549c753 --- /dev/null +++ b/solaris/SolarisProcessTable.c @@ -0,0 +1,268 @@ +/* +htop - SolarisProcessTable.c +(C) 2014 Hisham H. Muhammad +(C) 2017,2018 Guy M. Broome +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" // IWYU pragma: keep + +#include "solaris/SolarisProcessTable.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "CRT.h" +#include "solaris/Platform.h" +#include "solaris/SolarisProcess.h" + + +#define GZONE "global " +#define UZONE "unknown " + +static char* SolarisProcessTable_readZoneName(kstat_ctl_t* kd, SolarisProcess* sproc) { + char* zname; + + if ( sproc->zoneid == 0 ) { + zname = xStrdup(GZONE); + } else if ( kd == NULL ) { + zname = xStrdup(UZONE); + } else { + kstat_t* ks = kstat_lookup_wrapper( kd, "zones", sproc->zoneid, NULL ); + zname = xStrdup(ks == NULL ? UZONE : ks->ks_name); + } + + return zname; +} + +ProcessTable* ProcessTable_new(Machine* host, Hashtable* pidMatchList) { + SolarisProcessTable* this = xCalloc(1, sizeof(SolarisProcessTable)); + Object_setClass(this, Class(ProcessTable)); + + ProcessTable* super = &this->super; + ProcessTable_init(super, Class(SolarisProcess), host, pidMatchList); + + return super; +} + +void ProcessTable_delete(Object* cast) { + SolarisProcessTable* this = (SolarisProcessTable*) cast; + ProcessTable_done(&this->super); + free(this); +} + +static void SolarisProcessTable_updateExe(pid_t pid, Process* proc) { + char path[32]; + xSnprintf(path, sizeof(path), "/proc/%d/path/a.out", pid); + + char target[PATH_MAX]; + ssize_t ret = readlink(path, target, sizeof(target) - 1); + if (ret <= 0) + return; + + target[ret] = '\0'; + Process_updateExe(proc, target); +} + +static void SolarisProcessTable_updateCwd(pid_t pid, Process* proc) { + char path[32]; + xSnprintf(path, sizeof(path), "/proc/%d/cwd", pid); + + char target[PATH_MAX]; + ssize_t ret = readlink(path, target, sizeof(target) - 1); + if (ret <= 0) + return; + + target[ret] = '\0'; + free_and_xStrdup(&proc->procCwd, target); +} + +/* Taken from: https://docs.oracle.com/cd/E19253-01/817-6223/6mlkidlom/index.html#tbl-sched-state */ +static inline ProcessState SolarisProcessTable_getProcessState(char state) { + switch (state) { + case 'S': return SLEEPING; + case 'R': return RUNNABLE; + case 'O': return RUNNING; + case 'Z': return ZOMBIE; + case 'T': return STOPPED; + case 'I': return IDLE; + default: return UNKNOWN; + } +} + +/* NOTE: the following is a callback function of type proc_walk_f + * and MUST conform to the appropriate definition in order + * to work. See libproc(3LIB) on a Solaris or Illumos + * system for more info. + */ + +static int SolarisProcessTable_walkproc(psinfo_t* _psinfo, lwpsinfo_t* _lwpsinfo, void* listptr) { + bool preExisting; + pid_t getpid; + + // Setup process list + ProcessTable* pt = (ProcessTable*) listptr; + SolarisProcessTable* spt = (SolarisProcessTable*) listptr; + Machine* host = pt->host; + + id_t lwpid_real = _lwpsinfo->pr_lwpid; + if (lwpid_real > 1023) { + return 0; + } + + pid_t lwpid = (_psinfo->pr_pid * 1024) + lwpid_real; + bool onMasterLWP = (_lwpsinfo->pr_lwpid == _psinfo->pr_lwp.pr_lwpid); + if (onMasterLWP) { + getpid = _psinfo->pr_pid * 1024; + } else { + getpid = lwpid; + } + + Process* proc = ProcessTable_getProcess(pt, getpid, &preExisting, SolarisProcess_new); + SolarisProcess* sproc = (SolarisProcess*) proc; + const Settings* settings = host->settings; + + // Common code pass 1 + proc->show = false; + sproc->taskid = _psinfo->pr_taskid; + sproc->projid = _psinfo->pr_projid; + sproc->poolid = _psinfo->pr_poolid; + sproc->contid = _psinfo->pr_contract; + proc->priority = _lwpsinfo->pr_pri; + proc->nice = _lwpsinfo->pr_nice - NZERO; + proc->processor = _lwpsinfo->pr_onpro; + proc->state = SolarisProcessTable_getProcessState(_lwpsinfo->pr_sname); + // NOTE: This 'percentage' is a 16-bit BINARY FRACTIONS where 1.0 = 0x8000 + // Source: https://docs.oracle.com/cd/E19253-01/816-5174/proc-4/index.html + // (accessed on 18 November 2017) + proc->percent_mem = ((uint16_t)_psinfo->pr_pctmem / (double)32768) * (double)100.0; + proc->pgrp = _psinfo->pr_pgid; + proc->nlwp = _psinfo->pr_nlwp; + proc->session = _psinfo->pr_sid; + + proc->tty_nr = _psinfo->pr_ttydev; + const char* name = (_psinfo->pr_ttydev != PRNODEV) ? ttyname(_psinfo->pr_ttydev) : NULL; + if (!name) { + free(proc->tty_name); + proc->tty_name = NULL; + } else { + free_and_xStrdup(&proc->tty_name, name); + } + + proc->m_resident = _psinfo->pr_rssize; // KB + proc->m_virt = _psinfo->pr_size; // KB + + if (proc->st_uid != _psinfo->pr_euid) { + proc->st_uid = _psinfo->pr_euid; + proc->user = UsersTable_getRef(host->usersTable, proc->st_uid); + } + + if (!preExisting) { + sproc->realpid = _psinfo->pr_pid; + sproc->lwpid = lwpid_real; + sproc->zoneid = _psinfo->pr_zoneid; + sproc->zname = SolarisProcessTable_readZoneName(spt->kd, sproc); + SolarisProcessTable_updateExe(_psinfo->pr_pid, proc); + + Process_updateComm(proc, _psinfo->pr_fname); + Process_updateCmdline(proc, _psinfo->pr_psargs, 0, 0); + + if (settings->ss->flags & PROCESS_FLAG_CWD) { + SolarisProcessTable_updateCwd(_psinfo->pr_pid, proc); + } + } + + // End common code pass 1 + + if (onMasterLWP) { // Are we on the representative LWP? + Process_setParent(proc, (_psinfo->pr_ppid * 1024)); + Process_setThreadGroup(proc, (_psinfo->pr_ppid * 1024)); + sproc->realppid = _psinfo->pr_ppid; + sproc->realtgid = _psinfo->pr_ppid; + + // See note above (in common section) about this BINARY FRACTION + proc->percent_cpu = ((uint16_t)_psinfo->pr_pctcpu / (double)32768) * (double)100.0; + Process_updateCPUFieldWidths(proc->percent_cpu); + + proc->time = _psinfo->pr_time.tv_sec * 100 + _psinfo->pr_time.tv_nsec / 10000000; + if (!preExisting) { // Tasks done only for NEW processes + proc->isUserlandThread = false; + proc->starttime_ctime = _psinfo->pr_start.tv_sec; + } + + // Update proc and thread counts based on settings + if (proc->isKernelThread && !settings->hideKernelThreads) { + pt->kernelThreads += proc->nlwp; + pt->totalTasks += proc->nlwp + 1; + if (proc->state == RUNNING) { + pt->runningTasks++; + } + } else if (!proc->isKernelThread) { + if (proc->state == RUNNING) { + pt->runningTasks++; + } + if (settings->hideUserlandThreads) { + pt->totalTasks++; + } else { + pt->userlandThreads += proc->nlwp; + pt->totalTasks += proc->nlwp + 1; + } + } + proc->show = !(settings->hideKernelThreads && proc->isKernelThread); + } else { // We are not in the master LWP, so jump to the LWP handling code + proc->percent_cpu = ((uint16_t)_lwpsinfo->pr_pctcpu / (double)32768) * (double)100.0; + Process_updateCPUFieldWidths(proc->percent_cpu); + + proc->time = _lwpsinfo->pr_time.tv_sec * 100 + _lwpsinfo->pr_time.tv_nsec / 10000000; + if (!preExisting) { // Tasks done only for NEW LWPs + proc->isUserlandThread = true; + Process_setParent(proc, _psinfo->pr_pid * 1024); + Process_setThreadGroup(proc, _psinfo->pr_pid * 1024); + sproc->realppid = _psinfo->pr_pid; + sproc->realtgid = _psinfo->pr_pid; + proc->starttime_ctime = _lwpsinfo->pr_start.tv_sec; + } + + // Top-level process only gets this for the representative LWP + if (proc->isKernelThread && !settings->hideKernelThreads) { + proc->super.show = true; + } + if (!proc->isKernelThread && !settings->hideUserlandThreads) { + proc->super.show = true; + } + } // Top-level LWP or subordinate LWP + + // Common code pass 2 + + if (!preExisting) { + if ((sproc->realppid <= 0) && !(sproc->realpid <= 1)) { + proc->isKernelThread = true; + } else { + proc->isKernelThread = false; + } + + Process_fillStarttimeBuffer(proc); + ProcessTable_add(pt, proc); + } + + proc->super.updated = true; + + // End common code pass 2 + + return 0; +} + +void ProcessTable_goThroughEntries(ProcessTable* super) { + super->kernelThreads = 1; + proc_walk(&SolarisProcessTable_walkproc, super, PR_WALK_LWP); +} diff --git a/solaris/SolarisProcessTable.h b/solaris/SolarisProcessTable.h new file mode 100644 index 0000000..7c5ae8f --- /dev/null +++ b/solaris/SolarisProcessTable.h @@ -0,0 +1,31 @@ +#ifndef HEADER_SolarisProcessTable +#define HEADER_SolarisProcessTable +/* +htop - SolarisProcessTable.h +(C) 2014 Hisham H. Muhammad +(C) 2017,2018 Guy M. Broome +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Hashtable.h" +#include "ProcessTable.h" +#include "UsersTable.h" + +#include "solaris/SolarisProcess.h" + + +typedef struct SolarisProcessTable_ { + ProcessTable super; +} SolarisProcessTable; + +#endif -- cgit v1.2.3