summaryrefslogtreecommitdiffstats
path: root/solaris
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 19:58:07 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 19:58:07 +0000
commit10eea2ab1bae2a8ec159d81c0446fd8061b33e2b (patch)
treee8270dd60ec096bee8157dbadf029e15ed584592 /solaris
parentInitial commit. (diff)
downloadhtop-10eea2ab1bae2a8ec159d81c0446fd8061b33e2b.tar.xz
htop-10eea2ab1bae2a8ec159d81c0446fd8061b33e2b.zip
Adding upstream version 3.3.0.upstream/3.3.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--solaris/Platform.c342
-rw-r--r--solaris/Platform.h171
-rw-r--r--solaris/ProcessField.h24
-rw-r--r--solaris/SolarisMachine.c336
-rw-r--r--solaris/SolarisMachine.h57
-rw-r--r--solaris/SolarisProcess.c149
-rw-r--r--solaris/SolarisProcess.h47
-rw-r--r--solaris/SolarisProcessTable.c268
-rw-r--r--solaris/SolarisProcessTable.h31
9 files changed, 1425 insertions, 0 deletions
diff --git a/solaris/Platform.c b/solaris/Platform.c
new file mode 100644
index 0000000..5faa91a
--- /dev/null
+++ b/solaris/Platform.c
@@ -0,0 +1,342 @@
+/*
+htop - solaris/Platform.c
+(C) 2014 Hisham H. Muhammad
+(C) 2015 David C. Hunt
+(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/Platform.h"
+
+#include <kstat.h>
+#include <math.h>
+#include <string.h>
+#include <time.h>
+#include <utmpx.h>
+#include <sys/loadavg.h>
+#include <sys/resource.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/var.h>
+
+#include "Macros.h"
+#include "Meter.h"
+#include "CPUMeter.h"
+#include "MemoryMeter.h"
+#include "MemorySwapMeter.h"
+#include "SwapMeter.h"
+#include "TasksMeter.h"
+#include "LoadAverageMeter.h"
+#include "ClockMeter.h"
+#include "DateMeter.h"
+#include "DateTimeMeter.h"
+#include "HostnameMeter.h"
+#include "SysArchMeter.h"
+#include "UptimeMeter.h"
+#include "XUtils.h"
+
+#include "solaris/SolarisMachine.h"
+
+#include "zfs/ZfsArcMeter.h"
+#include "zfs/ZfsCompressedArcMeter.h"
+
+
+const ScreenDefaults Platform_defaultScreens[] = {
+ {
+ .name = "Default",
+ .columns = "PID LWPID USER PRIORITY NICE M_VIRT M_RESIDENT STATE PERCENT_CPU PERCENT_MEM TIME Command",
+ .sortKey = "PERCENT_CPU",
+ },
+};
+
+const unsigned int Platform_numberOfDefaultScreens = ARRAYSIZE(Platform_defaultScreens);
+
+const SignalItem Platform_signals[] = {
+ { .name = " 0 Cancel", .number = 0 },
+ { .name = " 1 SIGHUP", .number = 1 },
+ { .name = " 2 SIGINT", .number = 2 },
+ { .name = " 3 SIGQUIT", .number = 3 },
+ { .name = " 4 SIGILL", .number = 4 },
+ { .name = " 5 SIGTRAP", .number = 5 },
+ { .name = " 6 SIGABRT/IOT", .number = 6 },
+ { .name = " 7 SIGEMT", .number = 7 },
+ { .name = " 8 SIGFPE", .number = 8 },
+ { .name = " 9 SIGKILL", .number = 9 },
+ { .name = "10 SIGBUS", .number = 10 },
+ { .name = "11 SIGSEGV", .number = 11 },
+ { .name = "12 SIGSYS", .number = 12 },
+ { .name = "13 SIGPIPE", .number = 13 },
+ { .name = "14 SIGALRM", .number = 14 },
+ { .name = "15 SIGTERM", .number = 15 },
+ { .name = "16 SIGUSR1", .number = 16 },
+ { .name = "17 SIGUSR2", .number = 17 },
+ { .name = "18 SIGCHLD/CLD", .number = 18 },
+ { .name = "19 SIGPWR", .number = 19 },
+ { .name = "20 SIGWINCH", .number = 20 },
+ { .name = "21 SIGURG", .number = 21 },
+ { .name = "22 SIGPOLL/IO", .number = 22 },
+ { .name = "23 SIGSTOP", .number = 23 },
+ { .name = "24 SIGTSTP", .number = 24 },
+ { .name = "25 SIGCONT", .number = 25 },
+ { .name = "26 SIGTTIN", .number = 26 },
+ { .name = "27 SIGTTOU", .number = 27 },
+ { .name = "28 SIGVTALRM", .number = 28 },
+ { .name = "29 SIGPROF", .number = 29 },
+ { .name = "30 SIGXCPU", .number = 30 },
+ { .name = "31 SIGXFSZ", .number = 31 },
+ { .name = "32 SIGWAITING", .number = 32 },
+ { .name = "33 SIGLWP", .number = 33 },
+ { .name = "34 SIGFREEZE", .number = 34 },
+ { .name = "35 SIGTHAW", .number = 35 },
+ { .name = "36 SIGCANCEL", .number = 36 },
+ { .name = "37 SIGLOST", .number = 37 },
+ { .name = "38 SIGXRES", .number = 38 },
+ { .name = "39 SIGJVM1", .number = 39 },
+ { .name = "40 SIGJVM2", .number = 40 },
+ { .name = "41 SIGINFO", .number = 41 },
+};
+
+const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals);
+
+const MeterClass* const Platform_meterTypes[] = {
+ &CPUMeter_class,
+ &ClockMeter_class,
+ &DateMeter_class,
+ &DateTimeMeter_class,
+ &LoadAverageMeter_class,
+ &LoadMeter_class,
+ &MemoryMeter_class,
+ &SwapMeter_class,
+ &MemorySwapMeter_class,
+ &TasksMeter_class,
+ &BatteryMeter_class,
+ &HostnameMeter_class,
+ &SysArchMeter_class,
+ &UptimeMeter_class,
+ &AllCPUsMeter_class,
+ &AllCPUs2Meter_class,
+ &AllCPUs4Meter_class,
+ &AllCPUs8Meter_class,
+ &LeftCPUsMeter_class,
+ &RightCPUsMeter_class,
+ &LeftCPUs2Meter_class,
+ &RightCPUs2Meter_class,
+ &LeftCPUs4Meter_class,
+ &RightCPUs4Meter_class,
+ &LeftCPUs8Meter_class,
+ &RightCPUs8Meter_class,
+ &ZfsArcMeter_class,
+ &ZfsCompressedArcMeter_class,
+ &BlankMeter_class,
+ NULL
+};
+
+bool Platform_init(void) {
+ /* no platform-specific setup needed */
+ return true;
+}
+
+void Platform_done(void) {
+ /* no platform-specific cleanup needed */
+}
+
+void Platform_setBindings(Htop_Action* keys) {
+ /* no platform-specific key bindings */
+ (void) keys;
+}
+
+int Platform_getUptime(void) {
+ int boot_time = 0;
+ int curr_time = time(NULL);
+ struct utmpx* ent;
+
+ while (( ent = getutxent() )) {
+ if ( String_eq("system boot", ent->ut_line )) {
+ boot_time = ent->ut_tv.tv_sec;
+ }
+ }
+
+ endutxent();
+
+ return (curr_time - boot_time);
+}
+
+void Platform_getLoadAverage(double* one, double* five, double* fifteen) {
+ double plat_loadavg[3];
+ if (getloadavg( plat_loadavg, 3 ) < 0) {
+ *one = NAN;
+ *five = NAN;
+ *fifteen = NAN;
+ return;
+ }
+ *one = plat_loadavg[LOADAVG_1MIN];
+ *five = plat_loadavg[LOADAVG_5MIN];
+ *fifteen = plat_loadavg[LOADAVG_15MIN];
+}
+
+pid_t Platform_getMaxPid(void) {
+ int vproc = 32778; // Reasonable Solaris default
+
+ kstat_ctl_t* kc = kstat_open();
+ if (kc != NULL) {
+ kstat_t* kshandle = kstat_lookup_wrapper(kc, "unix", 0, "var");
+ if (kshandle != NULL) {
+ kstat_read(kc, kshandle, NULL);
+
+ kvar_t* ksvar = kshandle->ks_data;
+ if (ksvar && ksvar->v_proc > 0) {
+ vproc = ksvar->v_proc;
+ }
+ }
+ kstat_close(kc);
+ }
+
+ return vproc;
+}
+
+double Platform_setCPUValues(Meter* this, unsigned int cpu) {
+ 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 = &(shost->cpus[0]);
+ } else {
+ cpuData = &(shost->cpus[cpu]);
+ }
+
+ if (!cpuData->online) {
+ this->curItems = 0;
+ return NAN;
+ }
+
+ double percent;
+ double* v = this->values;
+
+ v[CPU_METER_NICE] = cpuData->nicePercent;
+ v[CPU_METER_NORMAL] = cpuData->userPercent;
+ if (super->settings->detailedCPUTime) {
+ v[CPU_METER_KERNEL] = cpuData->systemPercent;
+ v[CPU_METER_IRQ] = cpuData->irqPercent;
+ this->curItems = 4;
+ } else {
+ v[CPU_METER_KERNEL] = cpuData->systemAllPercent;
+ this->curItems = 3;
+ }
+
+ percent = sumPositiveValues(v, this->curItems);
+ percent = MINIMUM(percent, 100.0);
+
+ v[CPU_METER_FREQUENCY] = cpuData->frequency;
+ v[CPU_METER_TEMPERATURE] = NAN;
+
+ return percent;
+}
+
+void Platform_setMemoryValues(Meter* this) {
+ 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 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 SolarisMachine* shost = (SolarisMachine*) this->host;
+
+ ZfsArcMeter_readStats(this, &shost->zfs);
+}
+
+void Platform_setZfsCompressedArcValues(Meter* this) {
+ const SolarisMachine* shost = (SolarisMachine*) this->host;
+
+ ZfsCompressedArcMeter_readStats(this, &shost->zfs);
+}
+
+static int Platform_buildenv(void* accum, struct ps_prochandle* Phandle, uintptr_t addr, const char* str) {
+ envAccum* accump = accum;
+ (void) Phandle;
+ (void) addr;
+
+ size_t thissz = strlen(str);
+
+ while ((thissz + 2) > (accump->capacity - accump->size)) {
+ if (accump->capacity > (SIZE_MAX / 2))
+ return 1;
+
+ accump->capacity *= 2;
+ accump->env = xRealloc(accump->env, accump->capacity);
+ }
+
+ strlcpy( accump->env + accump->size, str, accump->capacity - accump->size);
+ strncpy( accump->env + accump->size + thissz + 1, "\n", 2);
+
+ accump->size += thissz + 1;
+ return 0;
+}
+
+char* Platform_getProcessEnv(pid_t pid) {
+ envAccum envBuilder;
+ pid_t realpid = pid / 1024;
+ int graberr;
+ struct ps_prochandle* Phandle;
+
+ if ((Phandle = Pgrab(realpid, PGRAB_RDONLY, &graberr)) == NULL) {
+ return NULL;
+ }
+
+ envBuilder.capacity = 4096;
+ envBuilder.size = 0;
+ envBuilder.env = xMalloc(envBuilder.capacity);
+
+ (void) Penv_iter(Phandle, Platform_buildenv, &envBuilder);
+
+ Prelease(Phandle, 0);
+
+ strncpy( envBuilder.env + envBuilder.size, "\0", 1);
+
+ return xRealloc(envBuilder.env, envBuilder.size + 1);
+}
+
+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) {
+ // TODO
+ (void)data;
+ return false;
+}
+
+bool Platform_getNetworkIO(NetworkIOData* data) {
+ // TODO
+ (void)data;
+ return false;
+}
+
+void Platform_getBattery(double* percent, ACPresence* isOnAC) {
+ *percent = NAN;
+ *isOnAC = AC_ERROR;
+}
diff --git a/solaris/Platform.h b/solaris/Platform.h
new file mode 100644
index 0000000..1a31c2e
--- /dev/null
+++ b/solaris/Platform.h
@@ -0,0 +1,171 @@
+#ifndef HEADER_Platform
+#define HEADER_Platform
+/*
+htop - solaris/Platform.h
+(C) 2014 Hisham H. Muhammad
+(C) 2015 David C. Hunt
+(C) 2017,2018 Guy M. Broome
+Released under the GNU GPLv2+, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include <kstat.h>
+
+/* On OmniOS /usr/include/sys/regset.h redefines ERR to 13 - \r, breaking the Enter key.
+ * Since ncruses macros use the ERR macro, we can not use another name.
+ */
+#undef ERR
+#include <libproc.h>
+#undef ERR
+#define ERR (-1)
+
+#include <signal.h>
+#include <stdbool.h>
+
+#include <sys/mkdev.h>
+#include <sys/proc.h>
+#include <sys/types.h>
+
+#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 "generic/gettime.h"
+#include "generic/hostname.h"
+#include "generic/uname.h"
+
+
+#define kill(pid, signal) kill(pid / 1024, signal)
+
+typedef struct var kvar_t;
+
+typedef struct envAccum_ {
+ size_t capacity;
+ size_t size;
+ size_t bytes;
+ char* env;
+} envAccum;
+
+extern const ScreenDefaults Platform_defaultScreens[];
+
+extern const unsigned int Platform_numberOfDefaultScreens;
+
+extern const SignalItem Platform_signals[];
+
+extern const unsigned int Platform_numberOfSignals;
+
+extern const MeterClass* const Platform_meterTypes[];
+
+bool Platform_init(void);
+
+void Platform_done(void);
+
+void Platform_setBindings(Htop_Action* keys);
+
+int Platform_getUptime(void);
+
+void Platform_getLoadAverage(double* one, double* five, double* fifteen);
+
+pid_t Platform_getMaxPid(void);
+
+double Platform_setCPUValues(Meter* this, unsigned int cpu);
+
+void Platform_setMemoryValues(Meter* this);
+
+void Platform_setSwapValues(Meter* this);
+
+void Platform_setZfsArcValues(Meter* this);
+
+void Platform_setZfsCompressedArcValues(Meter* this);
+
+char* Platform_getProcessEnv(pid_t pid);
+
+FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
+
+void Platform_getFileDescriptors(double* used, double* max);
+
+bool Platform_getDiskIO(DiskIOData* data);
+
+bool Platform_getNetworkIO(NetworkIOData* data);
+
+void Platform_getBattery(double* percent, ACPresence* isOnAC);
+
+static inline void Platform_getHostname(char* buffer, size_t size) {
+ Generic_hostname(buffer, size);
+}
+
+static inline void Platform_getRelease(char** string) {
+ *string = Generic_uname();
+}
+
+#define PLATFORM_LONG_OPTIONS
+
+static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { }
+
+static inline CommandLineStatus Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
+ return STATUS_ERROR_EXIT;
+}
+
+static inline void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec) {
+ Generic_gettime_realtime(tv, msec);
+}
+
+static inline void Platform_gettime_monotonic(uint64_t* msec) {
+ Generic_gettime_monotonic(msec);
+}
+
+static inline void* kstat_data_lookup_wrapper(kstat_t* ksp, const char* name) {
+IGNORE_WCASTQUAL_BEGIN
+ return kstat_data_lookup(ksp, (char*)name);
+IGNORE_WCASTQUAL_END
+}
+
+static inline kstat_t* kstat_lookup_wrapper(kstat_ctl_t* kc, const char* ks_module, int ks_instance, const char* ks_name) {
+IGNORE_WCASTQUAL_BEGIN
+ return kstat_lookup(kc, (char*)ks_module, ks_instance, (char*)ks_name);
+IGNORE_WCASTQUAL_END
+}
+
+static inline Hashtable* Platform_dynamicMeters(void) {
+ return NULL;
+}
+
+static inline void Platform_dynamicMetersDone(ATTR_UNUSED Hashtable* table) { }
+
+static inline void Platform_dynamicMeterInit(ATTR_UNUSED Meter* meter) { }
+
+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 void Platform_dynamicColumnsDone(ATTR_UNUSED Hashtable* table) { }
+
+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 void Platform_dynamicScreensDone(ATTR_UNUSED Hashtable* screens) { }
+
+#endif
diff --git a/solaris/ProcessField.h b/solaris/ProcessField.h
new file mode 100644
index 0000000..65dbcdc
--- /dev/null
+++ b/solaris/ProcessField.h
@@ -0,0 +1,24 @@
+#ifndef HEADER_SolarisProcessField
+#define HEADER_SolarisProcessField
+/*
+htop - solaris/ProcessField.h
+(C) 2020 htop dev team
+Released under the GNU GPLv2+, see the COPYING file
+in the source distribution for its full text.
+*/
+
+
+#define PLATFORM_PROCESS_FIELDS \
+ ZONEID = 100, \
+ ZONE = 101, \
+ PROJID = 102, \
+ TASKID = 103, \
+ POOLID = 104, \
+ CONTID = 105, \
+ LWPID = 106, \
+ \
+ DUMMY_BUMP_FIELD = CWD, \
+ // End of list
+
+
+#endif /* HEADER_SolarisProcessField */
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 <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/user.h>
+#include <limits.h>
+#include <string.h>
+#include <procfs.h>
+#include <errno.h>
+#include <pwd.h>
+#include <math.h>
+#include <time.h>
+
+#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 <kstat.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <sys/param.h>
+#include <sys/resource.h>
+#include <sys/swap.h>
+#include <sys/sysconf.h>
+#include <sys/sysinfo.h>
+#include <sys/uio.h>
+
+#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
new file mode 100644
index 0000000..449861b
--- /dev/null
+++ b/solaris/SolarisProcess.c
@@ -0,0 +1,149 @@
+/*
+htop - SolarisProcess.c
+(C) 2015 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/SolarisProcess.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+
+#include "Process.h"
+#include "ProcessTable.h"
+#include "CRT.h"
+
+#include "solaris/Platform.h"
+
+
+const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
+ [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, },
+ [PID] = { .name = "PID", .title = "PID", .description = "Process/thread ID", .flags = 0, .pidColumn = true, },
+ [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, },
+ [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, O onproc, Z zombie, T stopped, W waiting)", .flags = 0, },
+ [PPID] = { .name = "PPID", .title = "PPID", .description = "Parent process ID", .flags = 0, .pidColumn = true, },
+ [PGRP] = { .name = "PGRP", .title = "PGRP", .description = "Process group ID", .flags = 0, .pidColumn = true, },
+ [SESSION] = { .name = "SESSION", .title = "SID", .description = "Process's session ID", .flags = 0, .pidColumn = true, },
+ [TTY] = { .name = "TTY", .title = "TTY ", .description = "Controlling terminal", .flags = 0, },
+ //[TPGID] = { .name = "TPGID", .title = "TPGID", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, .pidColumn = true, },
+ //[MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, .defaultSortDesc = true, },
+ //[MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, .defaultSortDesc = true, },
+ [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, },
+ [NICE] = { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, },
+ [STARTTIME] = { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, },
+ [ELAPSED] = { .name = "ELAPSED", .title = "ELAPSED ", .description = "Time since the process was started", .flags = 0, },
+ [PROCESSOR] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, },
+ [M_VIRT] = { .name = "M_VIRT", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, .defaultSortDesc = true, },
+ [M_RESIDENT] = { .name = "M_RESIDENT", .title = " RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, .defaultSortDesc = true, },
+ [ST_UID] = { .name = "ST_UID", .title = "UID", .description = "User ID of the process owner", .flags = 0, },
+ [PERCENT_CPU] = { .name = "PERCENT_CPU", .title = " CPU%", .description = "Percentage of the CPU time the process used in the last sampling", .flags = 0, .defaultSortDesc = true, .autoWidth = true, },
+ [PERCENT_NORM_CPU] = { .name = "PERCENT_NORM_CPU", .title = "NCPU%", .description = "Normalized percentage of the CPU time the process used in the last sampling (normalized by cpu count)", .flags = 0, .defaultSortDesc = true, .autoWidth = true, },
+ [PERCENT_MEM] = { .name = "PERCENT_MEM", .title = "MEM% ", .description = "Percentage of the memory the process is using, based on resident memory size", .flags = 0, .defaultSortDesc = true, },
+ [USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, },
+ [TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, .defaultSortDesc = true, },
+ [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, },
+ [TGID] = { .name = "TGID", .title = "TGID", .description = "Thread group ID (i.e. process ID)", .flags = 0, .pidColumn = true, },
+ [PROC_COMM] = { .name = "COMM", .title = "COMM ", .description = "comm string of the process", .flags = 0, },
+ [PROC_EXE] = { .name = "EXE", .title = "EXE ", .description = "Basename of exe of the process", .flags = 0, },
+ [CWD] = { .name = "CWD", .title = "CWD ", .description = "The current working directory of the process", .flags = PROCESS_FLAG_CWD, },
+ [ZONEID] = { .name = "ZONEID", .title = "ZONEID", .description = "Zone ID", .flags = 0, .pidColumn = true, },
+ [ZONE] = { .name = "ZONE", .title = "ZONE ", .description = "Zone name", .flags = 0, },
+ [PROJID] = { .name = "PROJID", .title = "PRJID", .description = "Project ID", .flags = 0, .pidColumn = true, },
+ [TASKID] = { .name = "TASKID", .title = "TSKID", .description = "Task ID", .flags = 0, .pidColumn = true, },
+ [POOLID] = { .name = "POOLID", .title = "POLID", .description = "Pool ID", .flags = 0, .pidColumn = true, },
+ [CONTID] = { .name = "CONTID", .title = "CNTID", .description = "Contract ID", .flags = 0, .pidColumn = true, },
+ [LWPID] = { .name = "LWPID", .title = "LWPID", .description = "LWP ID", .flags = 0, .pidColumn = true, },
+};
+
+Process* SolarisProcess_new(const Machine* host) {
+ SolarisProcess* this = xCalloc(1, sizeof(SolarisProcess));
+ Object_setClass(this, Class(SolarisProcess));
+ Process_init(&this->super, host);
+ return &this->super;
+}
+
+void Process_delete(Object* cast) {
+ SolarisProcess* sp = (SolarisProcess*) cast;
+ Process_done((Process*)cast);
+ free(sp->zname);
+ free(sp);
+}
+
+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];
+ size_t n = sizeof(buffer) - 1;
+
+ switch (field) {
+ // add Solaris-specific fields here
+ case ZONEID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->zoneid); break;
+ case PROJID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, sp->projid); break;
+ 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: 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(&sp->super, str, field);
+ return;
+ }
+
+ RichString_appendWide(str, attr, buffer);
+}
+
+static int SolarisProcess_compareByKey(const Process* v1, const Process* v2, ProcessField key) {
+ const SolarisProcess* p1 = (const SolarisProcess*)v1;
+ const SolarisProcess* p2 = (const SolarisProcess*)v2;
+
+ switch (key) {
+ case ZONEID:
+ return SPACESHIP_NUMBER(p1->zoneid, p2->zoneid);
+ case PROJID:
+ return SPACESHIP_NUMBER(p1->projid, p2->projid);
+ case TASKID:
+ return SPACESHIP_NUMBER(p1->taskid, p2->taskid);
+ case POOLID:
+ return SPACESHIP_NUMBER(p1->poolid, p2->poolid);
+ case CONTID:
+ return SPACESHIP_NUMBER(p1->contid, p2->contid);
+ case ZONE:
+ return strcmp(p1->zname ? p1->zname : "global", p2->zname ? p2->zname : "global");
+ case PID:
+ return SPACESHIP_NUMBER(p1->realpid, p2->realpid);
+ case PPID:
+ return SPACESHIP_NUMBER(p1->realppid, p2->realppid);
+ case LWPID:
+ return SPACESHIP_NUMBER(p1->lwpid, p2->lwpid);
+ default:
+ return Process_compareByKey_Base(v1, v2, key);
+ }
+}
+
+const ProcessClass SolarisProcess_class = {
+ .super = {
+ .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
+ },
+ .compareByKey = SolarisProcess_compareByKey
+};
diff --git a/solaris/SolarisProcess.h b/solaris/SolarisProcess.h
new file mode 100644
index 0000000..1a8d18c
--- /dev/null
+++ b/solaris/SolarisProcess.h
@@ -0,0 +1,47 @@
+#ifndef HEADER_SolarisProcess
+#define HEADER_SolarisProcess
+/*
+htop - SolarisProcess.h
+(C) 2015 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 <zone.h>
+#include <sys/proc.h>
+
+/* On OmniOS /usr/include/sys/regset.h redefines ERR to 13 - \r, breaking the Enter key.
+ * Since ncruses macros use the ERR macro, we can not use another name.
+ */
+#undef ERR
+#include <libproc.h>
+#undef ERR
+#define ERR (-1)
+
+#include "Machine.h"
+
+
+typedef struct SolarisProcess_ {
+ Process super;
+ zoneid_t zoneid;
+ char* zname;
+ taskid_t taskid;
+ projid_t projid;
+ poolid_t poolid;
+ ctid_t contid;
+ pid_t realpid;
+ pid_t realppid;
+ pid_t realtgid;
+ pid_t lwpid;
+} SolarisProcess;
+
+extern const ProcessClass SolarisProcess_class;
+
+extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD];
+
+Process* SolarisProcess_new(const Machine* host);
+
+void Process_delete(Object* cast);
+
+#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 <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/user.h>
+#include <limits.h>
+#include <string.h>
+#include <procfs.h>
+#include <errno.h>
+#include <pwd.h>
+#include <math.h>
+#include <time.h>
+
+#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 <kstat.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <sys/param.h>
+#include <sys/uio.h>
+#include <sys/resource.h>
+#include <sys/sysconf.h>
+#include <sys/sysinfo.h>
+
+#include "Hashtable.h"
+#include "ProcessTable.h"
+#include "UsersTable.h"
+
+#include "solaris/SolarisProcess.h"
+
+
+typedef struct SolarisProcessTable_ {
+ ProcessTable super;
+} SolarisProcessTable;
+
+#endif