From df568a576f7b44ac5a2b9b7222c7f39d9932f626 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Wed, 11 Apr 2018 01:26:28 +0200 Subject: Imported Upstream version 2.2.0 --- solaris/Battery.c | 8 + solaris/Battery.h | 9 ++ solaris/Platform.c | 257 +++++++++++++++++++++++++++++ solaris/Platform.h | 65 ++++++++ solaris/SolarisCRT.c | 32 ++++ solaris/SolarisCRT.h | 18 +++ solaris/SolarisProcess.c | 214 +++++++++++++++++++++++++ solaris/SolarisProcess.h | 72 +++++++++ solaris/SolarisProcessList.c | 373 +++++++++++++++++++++++++++++++++++++++++++ solaris/SolarisProcessList.h | 64 ++++++++ 10 files changed, 1112 insertions(+) create mode 100644 solaris/Battery.c create mode 100644 solaris/Battery.h create mode 100644 solaris/Platform.c create mode 100644 solaris/Platform.h create mode 100644 solaris/SolarisCRT.c create mode 100644 solaris/SolarisCRT.h create mode 100644 solaris/SolarisProcess.c create mode 100644 solaris/SolarisProcess.h create mode 100644 solaris/SolarisProcessList.c create mode 100644 solaris/SolarisProcessList.h (limited to 'solaris') diff --git a/solaris/Battery.c b/solaris/Battery.c new file mode 100644 index 0000000..6d6e94b --- /dev/null +++ b/solaris/Battery.c @@ -0,0 +1,8 @@ + +#include "BatteryMeter.h" + +void Battery_getData(double* level, ACPresence* isOnAC) { + *level = -1; + *isOnAC = AC_ERROR; +} + diff --git a/solaris/Battery.h b/solaris/Battery.h new file mode 100644 index 0000000..8dc0cef --- /dev/null +++ b/solaris/Battery.h @@ -0,0 +1,9 @@ +/* Do not edit this file. It was automatically generated. */ + +#ifndef HEADER_Battery +#define HEADER_Battery + +void Battery_getData(double* level, ACPresence* isOnAC); + + +#endif diff --git a/solaris/Platform.c b/solaris/Platform.c new file mode 100644 index 0000000..a29fcb4 --- /dev/null +++ b/solaris/Platform.c @@ -0,0 +1,257 @@ +/* +htop - solaris/Platform.c +(C) 2014 Hisham H. Muhammad +(C) 2015 David C. Hunt +(C) 2017,2018 Guy M. Broome +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#include "Platform.h" +#include "Meter.h" +#include "CPUMeter.h" +#include "MemoryMeter.h" +#include "SwapMeter.h" +#include "TasksMeter.h" +#include "LoadAverageMeter.h" +#include "ClockMeter.h" +#include "HostnameMeter.h" +#include "UptimeMeter.h" +#include "SolarisProcess.h" +#include "SolarisProcessList.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/*{ +#include "Action.h" +#include "BatteryMeter.h" +#include "SignalsPanel.h" +#include +#include +#include +#include + +#define kill(pid, signal) kill(pid / 1024, signal) + +extern ProcessFieldData Process_fields[]; +typedef struct var kvar_t; + +typedef struct envAccum_ { + size_t capacity; + size_t size; + size_t bytes; + char *env; +} envAccum; + +}*/ + +double plat_loadavg[3] = {0}; + +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 = sizeof(Platform_signals)/sizeof(SignalItem); + +ProcessField Platform_defaultFields[] = { PID, LWPID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; + +MeterClass* Platform_meterTypes[] = { + &CPUMeter_class, + &ClockMeter_class, + &LoadAverageMeter_class, + &LoadMeter_class, + &MemoryMeter_class, + &SwapMeter_class, + &TasksMeter_class, + &BatteryMeter_class, + &HostnameMeter_class, + &UptimeMeter_class, + &AllCPUsMeter_class, + &AllCPUs2Meter_class, + &LeftCPUsMeter_class, + &RightCPUsMeter_class, + &LeftCPUs2Meter_class, + &RightCPUs2Meter_class, + &BlankMeter_class, + NULL +}; + +void Platform_setBindings(Htop_Action* keys) { + (void) keys; +} + +int Platform_numberOfFields = LAST_PROCESSFIELD; + +extern char Process_pidFormat[20]; + +int Platform_getUptime() { + int boot_time = 0; + int curr_time = time(NULL); + struct utmpx * ent; + + while (( ent = getutxent() )) { + if ( !strcmp("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) { + getloadavg( plat_loadavg, 3 ); + *one = plat_loadavg[LOADAVG_1MIN]; + *five = plat_loadavg[LOADAVG_5MIN]; + *fifteen = plat_loadavg[LOADAVG_15MIN]; +} + +int Platform_getMaxPid() { + kstat_ctl_t *kc = NULL; + kstat_t *kshandle = NULL; + kvar_t *ksvar = NULL; + int vproc = 32778; // Reasonable Solaris default + kc = kstat_open(); + if (kc != NULL) { kshandle = kstat_lookup(kc,"unix",0,"var"); } + if (kshandle != NULL) { kstat_read(kc,kshandle,NULL); } + ksvar = kshandle->ks_data; + if (ksvar->v_proc > 0 ) { + vproc = ksvar->v_proc; + } + if (kc != NULL) { kstat_close(kc); } + return vproc; +} + +double Platform_setCPUValues(Meter* this, int cpu) { + SolarisProcessList* spl = (SolarisProcessList*) this->pl; + int cpus = this->pl->cpuCount; + CPUData* cpuData = NULL; + + if (cpus == 1) { + // single CPU box has everything in spl->cpus[0] + cpuData = &(spl->cpus[0]); + } else { + cpuData = &(spl->cpus[cpu]); + } + + double percent; + double* v = this->values; + + v[CPU_METER_NICE] = cpuData->nicePercent; + v[CPU_METER_NORMAL] = cpuData->userPercent; + if (this->pl->settings->detailedCPUTime) { + v[CPU_METER_KERNEL] = cpuData->systemPercent; + v[CPU_METER_IRQ] = cpuData->irqPercent; + Meter_setItems(this, 4); + percent = v[0]+v[1]+v[2]+v[3]; + } else { + v[2] = cpuData->systemAllPercent; + Meter_setItems(this, 3); + percent = v[0]+v[1]+v[2]; + } + + percent = CLAMP(percent, 0.0, 100.0); + if (isnan(percent)) percent = 0.0; + return percent; +} + +void Platform_setMemoryValues(Meter* this) { + ProcessList* pl = (ProcessList*) this->pl; + this->total = pl->totalMem; + this->values[0] = pl->usedMem; + this->values[1] = pl->buffersMem; + this->values[2] = pl->cachedMem; +} + +void Platform_setSwapValues(Meter* this) { + ProcessList* pl = (ProcessList*) this->pl; + this->total = pl->totalSwap; + this->values[0] = pl->usedSwap; +} + +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); + if ((thissz + 2) > (accump->capacity - accump->size)) + accump->env = xRealloc(accump->env, accump->capacity *= 2); + if ((thissz + 2) > (accump->capacity - accump->size)) + return 1; + strlcpy( accump->env + accump->size, str, (accump->capacity - accump->size)); + strncpy( accump->env + accump->size + thissz + 1, "\n", 1); + accump->size = 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 "Unable to read process environment."; + + 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 envBuilder.env; +} diff --git a/solaris/Platform.h b/solaris/Platform.h new file mode 100644 index 0000000..f961b91 --- /dev/null +++ b/solaris/Platform.h @@ -0,0 +1,65 @@ +/* Do not edit this file. It was automatically generated. */ + +#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 GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#include "Action.h" +#include "BatteryMeter.h" +#include "SignalsPanel.h" +#include +#include +#include +#include + +#define kill(pid, signal) kill(pid / 1024, signal) + +extern ProcessFieldData Process_fields[]; +typedef struct var kvar_t; + +typedef struct envAccum_ { + size_t capacity; + size_t size; + size_t bytes; + char *env; +} envAccum; + + +extern double plat_loadavg[3]; + +extern const SignalItem Platform_signals[]; + +extern const unsigned int Platform_numberOfSignals; + +extern ProcessField Platform_defaultFields[]; + +extern MeterClass* Platform_meterTypes[]; + +void Platform_setBindings(Htop_Action* keys); + +extern int Platform_numberOfFields; + +extern char Process_pidFormat[20]; + +int Platform_getUptime(); + +void Platform_getLoadAverage(double* one, double* five, double* fifteen); + +int Platform_getMaxPid(); + +double Platform_setCPUValues(Meter* this, int cpu); + +void Platform_setMemoryValues(Meter* this); + +void Platform_setSwapValues(Meter* this); + +char* Platform_getProcessEnv(pid_t pid); + +#endif diff --git a/solaris/SolarisCRT.c b/solaris/SolarisCRT.c new file mode 100644 index 0000000..d7f8f52 --- /dev/null +++ b/solaris/SolarisCRT.c @@ -0,0 +1,32 @@ +/* +htop - SolarisCRT.c +(C) 2014 Hisham H. Muhammad +(C) 2018 Guy M. Broome +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" +#include "CRT.h" +#include +#include +#ifdef HAVE_EXECINFO_H +#include +#endif + +void CRT_handleSIGSEGV(int sgn) { + (void) sgn; + CRT_done(); + fprintf(stderr, "\n\nhtop " VERSION " aborting. Please report bug at http://hisham.hm/htop\n"); + #ifdef HAVE_EXECINFO_H + size_t size = backtrace(backtraceArray, sizeof(backtraceArray) / sizeof(void *)); + fprintf(stderr, "\n Please include in your report the following backtrace: \n"); + backtrace_symbols_fd(backtraceArray, size, 2); + fprintf(stderr, "\nAdditionally, in order to make the above backtrace useful,"); + fprintf(stderr, "\nplease also run the following command to generate a disassembly of your binary:"); + fprintf(stderr, "\n\n objdump -d `which htop` > ~/htop.objdump"); + fprintf(stderr, "\n\nand then attach the file ~/htop.objdump to your bug report."); + fprintf(stderr, "\n\nThank you for helping to improve htop!\n\n"); + #endif + abort(); +} diff --git a/solaris/SolarisCRT.h b/solaris/SolarisCRT.h new file mode 100644 index 0000000..6ab6dfc --- /dev/null +++ b/solaris/SolarisCRT.h @@ -0,0 +1,18 @@ +/* Do not edit this file. It was automatically generated. */ + +#ifndef HEADER_SolarisCRT +#define HEADER_SolarisCRT +/* +htop - SolarisCRT.h +(C) 2014 Hisham H. Muhammad +(C) 2018 Guy M. Broome +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#ifdef HAVE_EXECINFO_H +#endif + +void CRT_handleSIGSEGV(int sgn); + +#endif diff --git a/solaris/SolarisProcess.c b/solaris/SolarisProcess.c new file mode 100644 index 0000000..31f488e --- /dev/null +++ b/solaris/SolarisProcess.c @@ -0,0 +1,214 @@ +/* +htop - SolarisProcess.c +(C) 2015 Hisham H. Muhammad +(C) 2017,2018 Guy M. Broome +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#include "Process.h" +#include "ProcessList.h" +#include "SolarisProcess.h" +#include "Platform.h" +#include "CRT.h" + +#include +#include +#include +#include + +/*{ +#include "Settings.h" +#include +#include +#include + +typedef enum SolarisProcessFields { + // Add platform-specific fields here, with ids >= 100 + ZONEID = 100, + ZONE = 101, + PROJID = 102, + TASKID = 103, + POOLID = 104, + CONTID = 105, + LWPID = 106, + LAST_PROCESSFIELD = 107, +} SolarisProcessField; + + +typedef struct SolarisProcess_ { + Process super; + int kernel; + zoneid_t zoneid; + char* zname; + taskid_t taskid; + projid_t projid; + poolid_t poolid; + ctid_t contid; + bool is_lwp; + pid_t realpid; + pid_t realppid; + pid_t lwpid; +} SolarisProcess; + + +#ifndef Process_isKernelThread +#define Process_isKernelThread(_process) (_process->kernel == 1) +#endif + +#ifndef Process_isUserlandThread +#define Process_isUserlandThread(_process) (_process->pid != _process->tgid) +#endif + +}*/ + +ProcessClass SolarisProcess_class = { + .super = { + .extends = Class(Process), + .display = Process_display, + .delete = Process_delete, + .compare = SolarisProcess_compare + }, + .writeField = (Process_WriteField) SolarisProcess_writeField, +}; + +ProcessFieldData Process_fields[] = { + [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, + [PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, }, + [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, }, + [PGRP] = { .name = "PGRP", .title = " PGRP ", .description = "Process group ID", .flags = 0, }, + [SESSION] = { .name = "SESSION", .title = " SID ", .description = "Process's session ID", .flags = 0, }, + [TTY_NR] = { .name = "TTY_NR", .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, }, + [MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, }, + [MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, }, + [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, }, + [PROCESSOR] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, }, + [M_SIZE] = { .name = "M_SIZE", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, }, + [M_RESIDENT] = { .name = "M_RESIDENT", .title = " RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, }, + [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, }, + [PERCENT_MEM] = { .name = "PERCENT_MEM", .title = "MEM% ", .description = "Percentage of the memory the process is using, based on resident memory size", .flags = 0, }, + [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, }, + [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, }, + [ZONEID] = { .name = "ZONEID", .title = " ZONEID ", .description = "Zone ID", .flags = 0, }, + [ZONE] = { .name = "ZONE", .title = "ZONE ", .description = "Zone name", .flags = 0, }, + [PROJID] = { .name = "PROJID", .title = " PRJID ", .description = "Project ID", .flags = 0, }, + [TASKID] = { .name = "TASKID", .title = " TSKID ", .description = "Task ID", .flags = 0, }, + [POOLID] = { .name = "POOLID", .title = " POLID ", .description = "Pool ID", .flags = 0, }, + [CONTID] = { .name = "CONTID", .title = " CNTID ", .description = "Contract ID", .flags = 0, }, + [LWPID] = { .name = "LWPID", .title = " LWPID ", .description = "LWP ID", .flags = 0, }, + [LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, }, +}; + +ProcessPidColumn Process_pidColumns[] = { + { .id = ZONEID, .label = "ZONEID" }, + { .id = TASKID, .label = "TSKID" }, + { .id = PROJID, .label = "PRJID" }, + { .id = POOLID, .label = "POLID" }, + { .id = CONTID, .label = "CNTID" }, + { .id = PID, .label = "PID" }, + { .id = PPID, .label = "PPID" }, + { .id = LWPID, .label = "LWPID" }, + { .id = TPGID, .label = "TPGID" }, + { .id = TGID, .label = "TGID" }, + { .id = PGRP, .label = "PGRP" }, + { .id = SESSION, .label = "SID" }, + { .id = 0, .label = NULL }, +}; + +SolarisProcess* SolarisProcess_new(Settings* settings) { + SolarisProcess* this = xCalloc(1, sizeof(SolarisProcess)); + Object_setClass(this, Class(SolarisProcess)); + Process_init(&this->super, settings); + return this; +} + +void Process_delete(Object* cast) { + SolarisProcess* sp = (SolarisProcess*) cast; + Process_done((Process*)cast); + free(sp->zname); + free(sp); +} + +void SolarisProcess_writeField(Process* this, RichString* str, ProcessField field) { + SolarisProcess* sp = (SolarisProcess*) this; + char buffer[256]; buffer[255] = '\0'; + int attr = CRT_colors[DEFAULT_COLOR]; + int n = sizeof(buffer) - 1; + switch ((int) field) { + // add Solaris-specific fields here + case ZONEID: xSnprintf(buffer, n, Process_pidFormat, sp->zoneid); break; + case PROJID: xSnprintf(buffer, n, Process_pidFormat, sp->projid); break; + case TASKID: xSnprintf(buffer, n, Process_pidFormat, sp->taskid); break; + case POOLID: xSnprintf(buffer, n, Process_pidFormat, sp->poolid); break; + case CONTID: xSnprintf(buffer, n, Process_pidFormat, sp->contid); break; + case ZONE:{ + xSnprintf(buffer, n, "%-*s ", ZONENAME_MAX/4, sp->zname); break; + if (buffer[ZONENAME_MAX/4] != '\0') { + buffer[ZONENAME_MAX/4] = ' '; + buffer[(ZONENAME_MAX/4)+1] = '\0'; + } + break; + } + case PID: xSnprintf(buffer, n, Process_pidFormat, sp->realpid); break; + case PPID: xSnprintf(buffer, n, Process_pidFormat, sp->realppid); break; + case LWPID: xSnprintf(buffer, n, Process_pidFormat, sp->lwpid); break; + default: + Process_writeField(this, str, field); + return; + } + RichString_append(str, attr, buffer); +} + +long SolarisProcess_compare(const void* v1, const void* v2) { + SolarisProcess *p1, *p2; + Settings* settings = ((Process*)v1)->settings; + if (settings->direction == 1) { + p1 = (SolarisProcess*)v1; + p2 = (SolarisProcess*)v2; + } else { + p2 = (SolarisProcess*)v1; + p1 = (SolarisProcess*)v2; + } + switch ((int) settings->sortKey) { + case ZONEID: + return (p1->zoneid - p2->zoneid); + case PROJID: + return (p1->projid - p2->projid); + case TASKID: + return (p1->taskid - p2->taskid); + case POOLID: + return (p1->poolid - p2->poolid); + case CONTID: + return (p1->contid - p2->contid); + case ZONE: + return strcmp(p1->zname ? p1->zname : "global", p2->zname ? p2->zname : "global"); + case PID: + return (p1->realpid - p2->realpid); + case PPID: + return (p1->realppid - p2->realppid); + case LWPID: + return (p1->lwpid - p2->lwpid); + default: + return Process_compare(v1, v2); + } +} + +bool Process_isThread(Process* this) { + SolarisProcess* fp = (SolarisProcess*) this; + + if (fp->kernel == 1 ) { + return 1; + } else if (fp->is_lwp) { + return 1; + } else { + return 0; + } +} diff --git a/solaris/SolarisProcess.h b/solaris/SolarisProcess.h new file mode 100644 index 0000000..1b3492a --- /dev/null +++ b/solaris/SolarisProcess.h @@ -0,0 +1,72 @@ +/* Do not edit this file. It was automatically generated. */ + +#ifndef HEADER_SolarisProcess +#define HEADER_SolarisProcess +/* +htop - SolarisProcess.h +(C) 2015 Hisham H. Muhammad +(C) 2017,2018 Guy M. Broome +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#include "Settings.h" +#include +#include +#include + +typedef enum SolarisProcessFields { + // Add platform-specific fields here, with ids >= 100 + ZONEID = 100, + ZONE = 101, + PROJID = 102, + TASKID = 103, + POOLID = 104, + CONTID = 105, + LWPID = 106, + LAST_PROCESSFIELD = 107, +} SolarisProcessField; + + +typedef struct SolarisProcess_ { + Process super; + int kernel; + zoneid_t zoneid; + char* zname; + taskid_t taskid; + projid_t projid; + poolid_t poolid; + ctid_t contid; + bool is_lwp; + pid_t realpid; + pid_t realppid; + pid_t lwpid; +} SolarisProcess; + + +#ifndef Process_isKernelThread +#define Process_isKernelThread(_process) (_process->kernel == 1) +#endif + +#ifndef Process_isUserlandThread +#define Process_isUserlandThread(_process) (_process->pid != _process->tgid) +#endif + + +extern ProcessClass SolarisProcess_class; + +extern ProcessFieldData Process_fields[]; + +extern ProcessPidColumn Process_pidColumns[]; + +SolarisProcess* SolarisProcess_new(Settings* settings); + +void Process_delete(Object* cast); + +void SolarisProcess_writeField(Process* this, RichString* str, ProcessField field); + +long SolarisProcess_compare(const void* v1, const void* v2); + +bool Process_isThread(Process* this); + +#endif diff --git a/solaris/SolarisProcessList.c b/solaris/SolarisProcessList.c new file mode 100644 index 0000000..2c68185 --- /dev/null +++ b/solaris/SolarisProcessList.c @@ -0,0 +1,373 @@ +/* +htop - SolarisProcessList.c +(C) 2014 Hisham H. Muhammad +(C) 2017,2018 Guy M. Broome +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#include "ProcessList.h" +#include "SolarisProcess.h" +#include "SolarisProcessList.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAXCMDLINE 255 + +/*{ + +#include +#include +#include +#include +#include +#include +#include + +#define ZONE_ERRMSGLEN 1024 +char zone_errmsg[ZONE_ERRMSGLEN]; + +typedef struct CPUData_ { + double userPercent; + double nicePercent; + double systemPercent; + double irqPercent; + double idlePercent; + double systemAllPercent; + uint64_t luser; + uint64_t lkrnl; + uint64_t lintr; + uint64_t lidle; +} CPUData; + +typedef struct SolarisProcessList_ { + ProcessList super; + kstat_ctl_t* kd; + CPUData* cpus; +} SolarisProcessList; + +}*/ + +char* SolarisProcessList_readZoneName(kstat_ctl_t* kd, SolarisProcess* sproc) { + char* zname; + if ( sproc->zoneid == 0 ) { + zname = xStrdup("global "); + } else if ( kd == NULL ) { + zname = xStrdup("unknown "); + } else { + kstat_t* ks = kstat_lookup( kd, "zones", sproc->zoneid, NULL ); + zname = xStrdup(ks->ks_name); + } + return zname; +} + +ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { + SolarisProcessList* spl = xCalloc(1, sizeof(SolarisProcessList)); + ProcessList* pl = (ProcessList*) spl; + ProcessList_init(pl, Class(SolarisProcess), usersTable, pidWhiteList, userId); + + spl->kd = kstat_open(); + + pl->cpuCount = sysconf(_SC_NPROCESSORS_ONLN); + + if (pl->cpuCount == 1 ) { + spl->cpus = xRealloc(spl->cpus, sizeof(CPUData)); + } else { + spl->cpus = xRealloc(spl->cpus, (pl->cpuCount + 1) * sizeof(CPUData)); + } + + return pl; +} + +static inline void SolarisProcessList_scanCPUTime(ProcessList* pl) { + const SolarisProcessList* spl = (SolarisProcessList*) pl; + int cpus = pl->cpuCount; + kstat_t *cpuinfo = NULL; + int kchain = 0; + kstat_named_t *idletime = NULL; + kstat_named_t *intrtime = NULL; + kstat_named_t *krnltime = NULL; + kstat_named_t *usertime = NULL; + double idlebuf = 0; + double intrbuf = 0; + double krnlbuf = 0; + double userbuf = 0; + uint64_t totaltime = 0; + int arrskip = 0; + + assert(cpus > 0); + + if (cpus > 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 (int i = 0; i < cpus; i++) { + if (spl->kd != NULL) { cpuinfo = kstat_lookup(spl->kd,"cpu",i,"sys"); } + if (cpuinfo != NULL) { kchain = kstat_read(spl->kd,cpuinfo,NULL); } + if (kchain != -1 ) { + idletime = kstat_data_lookup(cpuinfo,"cpu_nsec_idle"); + intrtime = kstat_data_lookup(cpuinfo,"cpu_nsec_intr"); + krnltime = kstat_data_lookup(cpuinfo,"cpu_nsec_kernel"); + usertime = kstat_data_lookup(cpuinfo,"cpu_nsec_user"); + } + + assert( (idletime != NULL) && (intrtime != NULL) + && (krnltime != NULL) && (usertime != NULL) ); + + CPUData* cpuData = &(spl->cpus[i+arrskip]); + 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; + // Accumulate the current percentages into buffers for later average calculation + if (cpus > 1) { + userbuf += cpuData->userPercent; + krnlbuf += cpuData->systemPercent; + intrbuf += cpuData->irqPercent; + idlebuf += cpuData->idlePercent; + } + } + + if (cpus > 1) { + CPUData* cpuData = &(spl->cpus[0]); + cpuData->userPercent = userbuf / cpus; + cpuData->nicePercent = (double)0.0; // Not implemented on Solaris + cpuData->systemPercent = krnlbuf / cpus; + cpuData->irqPercent = intrbuf / cpus; + cpuData->systemAllPercent = cpuData->systemPercent + cpuData->irqPercent; + cpuData->idlePercent = idlebuf / cpus; + } +} + +static inline void SolarisProcessList_scanMemoryInfo(ProcessList* pl) { + SolarisProcessList* spl = (SolarisProcessList*) pl; + kstat_t *meminfo = NULL; + int ksrphyserr = -1; + kstat_named_t *totalmem_pgs = NULL; + kstat_named_t *lockedmem_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 = kstat_lookup(spl->kd,"unix",0,"system_pages"); } + if (meminfo != NULL) { ksrphyserr = kstat_read(spl->kd,meminfo,NULL); } + if (ksrphyserr != -1) { + totalmem_pgs = kstat_data_lookup( meminfo, "physmem" ); + lockedmem_pgs = kstat_data_lookup( meminfo, "pageslocked" ); + pages = kstat_data_lookup( meminfo, "pagestotal" ); + + pl->totalMem = totalmem_pgs->value.ui64 * PAGE_SIZE_KB; + pl->usedMem = lockedmem_pgs->value.ui64 * PAGE_SIZE_KB; + // 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) * PAGE_SIZE_KB; + } else { + // Fall back to basic sysconf if kstat isn't working + pl->totalMem = sysconf(_SC_PHYS_PAGES) * PAGE_SIZE; + pl->buffersMem = 0; + pl->cachedMem = 0; + pl->usedMem = pl->totalMem - (sysconf(_SC_AVPHYS_PAGES) * PAGE_SIZE); + } + + // 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 * PAGE_SIZE_KB; + pl->usedSwap = pl->totalSwap - (totalfree * PAGE_SIZE_KB); +} + +void ProcessList_delete(ProcessList* pl) { + SolarisProcessList* spl = (SolarisProcessList*) pl; + ProcessList_done(pl); + free(spl->cpus); + if (spl->kd) kstat_close(spl->kd); + free(spl); +} + +/* 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. + */ + +int SolarisProcessList_walkproc(psinfo_t *_psinfo, lwpsinfo_t *_lwpsinfo, void *listptr) { + struct timeval tv; + struct tm date; + 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, (Process_New) SolarisProcess_new); + SolarisProcess *sproc = (SolarisProcess*) proc; + + gettimeofday(&tv, NULL); + + // 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; + proc->processor = _lwpsinfo->pr_onpro; + proc->state = _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->st_uid = _psinfo->pr_euid; + proc->pgrp = _psinfo->pr_pgid; + proc->nlwp = _psinfo->pr_nlwp; + proc->tty_nr = _psinfo->pr_ttydev; + proc->m_resident = _psinfo->pr_rssize/PAGE_SIZE_KB; + proc->m_size = _psinfo->pr_size/PAGE_SIZE_KB; + + if (!preExisting) { + sproc->realpid = _psinfo->pr_pid; + sproc->lwpid = lwpid_real; + sproc->zoneid = _psinfo->pr_zoneid; + sproc->zname = SolarisProcessList_readZoneName(spl->kd,sproc); + proc->user = UsersTable_getRef(pl->usersTable, proc->st_uid); + proc->comm = xStrdup(_psinfo->pr_fname); + proc->commLen = strnlen(_psinfo->pr_fname,PRFNSZ); + } + + // 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; + // See note above (in common section) about this BINARY FRACTION + proc->percent_cpu = ((uint16_t)_psinfo->pr_pctcpu/(double)32768)*(double)100.0; + proc->time = _psinfo->pr_time.tv_sec; + if(!preExisting) { // Tasks done only for NEW processes + sproc->is_lwp = false; + proc->starttime_ctime = _psinfo->pr_start.tv_sec; + } + + // Update proc and thread counts based on settings + if (sproc->kernel && !pl->settings->hideKernelThreads) { + pl->kernelThreads += proc->nlwp; + pl->totalTasks += proc->nlwp+1; + if (proc->state == 'O') pl->runningTasks++; + } else if (!sproc->kernel) { + if (proc->state == 'O') pl->runningTasks++; + if (pl->settings->hideUserlandThreads) { + pl->totalTasks++; + } else { + pl->userlandThreads += proc->nlwp; + pl->totalTasks += proc->nlwp+1; + } + } + proc->show = !(pl->settings->hideKernelThreads && sproc->kernel); + } 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; + proc->time = _lwpsinfo->pr_time.tv_sec; + if (!preExisting) { // Tasks done only for NEW LWPs + sproc->is_lwp = true; + proc->basenameOffset = -1; + proc->ppid = _psinfo->pr_pid * 1024; + proc->tgid = _psinfo->pr_pid * 1024; + sproc->realppid = _psinfo->pr_pid; + proc->starttime_ctime = _lwpsinfo->pr_start.tv_sec; + } + + // Top-level process only gets this for the representative LWP + if (sproc->kernel && !pl->settings->hideKernelThreads) proc->show = true; + if (!sproc->kernel && !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)) { + sproc->kernel = true; + } else { + sproc->kernel = false; + } + (void) localtime_r((time_t*) &proc->starttime_ctime, &date); + strftime(proc->starttime_show, 7, ((proc->starttime_ctime > tv.tv_sec - 86400) ? "%R " : "%b%d "), &date); + ProcessList_add(pl, proc); + } + proc->updated = true; + + // End common code pass 2 + + return 0; +} + +void ProcessList_goThroughEntries(ProcessList* this) { + SolarisProcessList_scanCPUTime(this); + SolarisProcessList_scanMemoryInfo(this); + this->kernelThreads = 1; + proc_walk(&SolarisProcessList_walkproc, this, PR_WALK_LWP); +} + diff --git a/solaris/SolarisProcessList.h b/solaris/SolarisProcessList.h new file mode 100644 index 0000000..a5f2fbc --- /dev/null +++ b/solaris/SolarisProcessList.h @@ -0,0 +1,64 @@ +/* Do not edit this file. It was automatically generated. */ + +#ifndef HEADER_SolarisProcessList +#define HEADER_SolarisProcessList +/* +htop - SolarisProcessList.h +(C) 2014 Hisham H. Muhammad +(C) 2017,2018 Guy M. Broome +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#define MAXCMDLINE 255 + + +#include +#include +#include +#include +#include +#include +#include + +#define ZONE_ERRMSGLEN 1024 +char zone_errmsg[ZONE_ERRMSGLEN]; + +typedef struct CPUData_ { + double userPercent; + double nicePercent; + double systemPercent; + double irqPercent; + double idlePercent; + double systemAllPercent; + uint64_t luser; + uint64_t lkrnl; + uint64_t lintr; + uint64_t lidle; +} CPUData; + +typedef struct SolarisProcessList_ { + ProcessList super; + kstat_ctl_t* kd; + CPUData* cpus; +} SolarisProcessList; + + +char* SolarisProcessList_readZoneName(kstat_ctl_t* kd, SolarisProcess* sproc); + +ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId); + +void ProcessList_delete(ProcessList* pl); + +/* 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. + */ + +int SolarisProcessList_walkproc(psinfo_t *_psinfo, lwpsinfo_t *_lwpsinfo, void *listptr); + +void ProcessList_goThroughEntries(ProcessList* this); + + +#endif -- cgit v1.2.3