Solaris: get completely out of the file handling business using libproc

This commit is contained in:
Guy M. Broome 2018-03-28 12:48:37 -04:00 committed by Hisham Muhammad
parent 4ba06c51e5
commit 45fad05b4a
5 changed files with 127 additions and 197 deletions

View File

@ -243,6 +243,7 @@ fi
if test "$my_htop_platform" = "solaris"; then if test "$my_htop_platform" = "solaris"; then
AC_CHECK_LIB([kstat], [kstat_open], [], [missing_libraries="$missing_libraries libkstat"]) AC_CHECK_LIB([kstat], [kstat_open], [], [missing_libraries="$missing_libraries libkstat"])
AC_CHECK_LIB([proc], [Pgrab_error], [], [missing_libraries="$missing_libraries libproc"])
AC_CHECK_LIB([malloc], [free], [], [missing_libraries="$missing_libraries libmalloc"]) AC_CHECK_LIB([malloc], [free], [], [missing_libraries="$missing_libraries libmalloc"])
fi fi

View File

@ -20,6 +20,8 @@ in the source distribution for its full text.
/*{ /*{
#include "Settings.h" #include "Settings.h"
#include <zone.h> #include <zone.h>
#include <sys/proc.h>
#include <libproc.h>
typedef enum SolarisProcessFields { typedef enum SolarisProcessFields {
// Add platform-specific fields here, with ids >= 100 // Add platform-specific fields here, with ids >= 100

View File

@ -12,6 +12,8 @@ in the source distribution for its full text.
#include "Settings.h" #include "Settings.h"
#include <zone.h> #include <zone.h>
#include <sys/proc.h>
#include <libproc.h>
typedef enum SolarisProcessFields { typedef enum SolarisProcessFields {
// Add platform-specific fields here, with ids >= 100 // Add platform-specific fields here, with ids >= 100

View File

@ -31,7 +31,6 @@ in the source distribution for its full text.
#include <kstat.h> #include <kstat.h>
#include <sys/param.h> #include <sys/param.h>
#include <zone.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/sysconf.h> #include <sys/sysconf.h>
@ -239,205 +238,129 @@ void ProcessList_delete(ProcessList* this) {
free(this); free(this);
} }
void ProcessList_enumerateLWPs(Process* proc, char* name, ProcessList* pl, struct timeval tv) { void ProcessList_enumerateLWPs(Process* proc, ProcessList* pl, lwpsinfo_t *_lwpsinfo, struct timeval tv) {
Process *lwp; Process *lwp;
SolarisProcess *slwp; SolarisProcess *slwp;
SolarisProcess *sproc = (SolarisProcess*) proc; SolarisProcess *sproc = (SolarisProcess*) proc;
char lwpdir[MAX_NAME+1]; id_t lwpid_real;
DIR* dir = NULL;
FILE* fp = NULL;
pid_t lwpid; pid_t lwpid;
bool preExisting = false; bool preExisting = false;
char filename[MAX_NAME+1];
lwpsinfo_t _lwpsinfo;
prusage_t _lwprusage;
struct tm date; struct tm date;
xSnprintf(lwpdir, MAX_NAME, "%s/%s/lwp", PROCDIR, name);
struct dirent* entry;
char* lwpname;
bool haveUsage = false;
dir = opendir(lwpdir); // With 10 bits to spare, we can only list up to 1023 unique LWPs per process
if (!dir) return; lwpid_real = _lwpsinfo->pr_lwpid;
while ((entry = readdir(dir)) != NULL) { if (lwpid_real > 1023) return;
lwpname = entry->d_name; lwpid = proc->pid + lwpid_real;
// With 10 bits to spare, we can only list up to 1023 unique LWPs per process lwp = ProcessList_getProcess(pl, lwpid, &preExisting, (Process_New) SolarisProcess_new);
if (atoi(lwpname) > 1023) break; slwp = (SolarisProcess*) lwp;
lwpid = proc->pid + atoi(lwpname);
lwp = ProcessList_getProcess(pl, lwpid, &preExisting, (Process_New) SolarisProcess_new);
slwp = (SolarisProcess*) lwp;
xSnprintf(filename, MAX_NAME, "%s/%s/lwp/%s/lwpsinfo", PROCDIR, name, lwpname);
fp = fopen(filename, "r");
if ( fp == NULL ) continue;
fread(&_lwpsinfo,sizeof(lwpsinfo_t),1,fp);
fclose(fp);
xSnprintf(filename, MAX_NAME, "%s/%s/lwp/%s/lwpusage", PROCDIR, name, lwpname);
fp = fopen(filename, "r");
if ( fp != NULL ) {
haveUsage = true;
fread(&_lwprusage,sizeof(prusage_t),1,fp);
fclose(fp);
}
// Common items set for both new and refreshed LWPs // Common items set for both new and refreshed LWPs
slwp->zoneid = sproc->zoneid; slwp->zoneid = sproc->zoneid;
lwp->percent_cpu = ((uint16_t)_lwpsinfo.pr_pctcpu/(double)32768)*(double)100.0; lwp->percent_cpu = ((uint16_t)_lwpsinfo->pr_pctcpu/(double)32768)*(double)100.0;
lwp->pgrp = proc->pgrp; lwp->pgrp = proc->pgrp;
lwp->st_uid = proc->st_uid; lwp->st_uid = proc->st_uid;
lwp->user = UsersTable_getRef(pl->usersTable, lwp->st_uid); lwp->user = UsersTable_getRef(pl->usersTable, lwp->st_uid);
lwp->session = proc->session; lwp->session = proc->session;
lwp->comm = xStrdup(proc->comm); lwp->comm = xStrdup(proc->comm);
lwp->commLen = strnlen(proc->comm,PRFNSZ); lwp->commLen = strnlen(proc->comm,PRFNSZ);
slwp->zname = sproc->zname; slwp->zname = sproc->zname;
lwp->tty_nr = proc->tty_nr; lwp->tty_nr = proc->tty_nr;
if (haveUsage) { lwp->priority = _lwpsinfo->pr_pri;
lwp->majflt = _lwprusage.pr_majf; lwp->nice = _lwpsinfo->pr_nice;
lwp->minflt = _lwprusage.pr_minf; lwp->processor = _lwpsinfo->pr_onpro;
} else { lwp->state = _lwpsinfo->pr_sname;
lwp->majflt = 0; lwp->time = _lwpsinfo->pr_time.tv_sec;
lwp->minflt = 0; slwp->taskid = sproc->taskid;
} slwp->projid = sproc->projid;
lwp->priority = _lwpsinfo.pr_pri; slwp->poolid = sproc->poolid;
lwp->nice = _lwpsinfo.pr_nice; slwp->contid = sproc->contid;
lwp->processor = _lwpsinfo.pr_onpro; lwp->show = false;
lwp->state = _lwpsinfo.pr_sname;
lwp->time = _lwpsinfo.pr_time.tv_sec;
slwp->taskid = sproc->taskid;
slwp->projid = sproc->projid;
slwp->poolid = sproc->poolid;
slwp->contid = sproc->contid;
lwp->show = false;
// Tasks done only for NEW LWPs
if (!preExisting) {
slwp->is_lwp = true;
lwp->basenameOffset = -1;
slwp->kernel = sproc->kernel;
// Fake values used for sorting
// Only set once because threads don't generally
// move... between... processes.
lwp->pid = lwpid;
lwp->ppid = proc->pid;
lwp->tgid = proc->pid;
// Corresponding real values used for display
slwp->realpid = sproc->realpid;
slwp->realppid = sproc->realpid;
slwp->lwpid = atoi(lwpname);
// Not tracked per thread
lwp->percent_mem = (double)0.0;
lwp->nlwp = 0;
lwp->m_resident = 0;
lwp->m_size = 0;
lwp->starttime_ctime = _lwpsinfo.pr_start.tv_sec;
(void) localtime_r((time_t*) &lwp->starttime_ctime, &date);
strftime(lwp->starttime_show, 7, ((lwp->starttime_ctime > tv.tv_sec - 86400) ? "%R " : "%b%d "), &date);
ProcessList_add(pl, lwp);
}
// Top-level process only gets this for the representative LWP
if (lwp->state == 'O') proc->state = 'O';
if (slwp->kernel && !pl->settings->hideKernelThreads) lwp->show = true;
if (!slwp->kernel && !pl->settings->hideUserlandThreads) lwp->show = true;
lwp->updated = true;
// Tasks done only for NEW LWPs
if (!preExisting) {
slwp->is_lwp = true;
lwp->basenameOffset = -1;
slwp->kernel = sproc->kernel;
// Fake values used for sorting
// Only set once because threads don't generally
// move... between... processes.
lwp->pid = lwpid;
lwp->ppid = proc->pid;
lwp->tgid = proc->pid;
// Corresponding real values used for display
slwp->realpid = sproc->realpid;
slwp->realppid = sproc->realpid;
slwp->lwpid = lwpid_real;
// Not tracked per thread
lwp->percent_mem = (double)0.0;
lwp->nlwp = 0;
lwp->m_resident = 0;
lwp->m_size = 0;
lwp->starttime_ctime = _lwpsinfo->pr_start.tv_sec;
(void) localtime_r((time_t*) &lwp->starttime_ctime, &date);
strftime(lwp->starttime_show, 7, ((lwp->starttime_ctime > tv.tv_sec - 86400) ? "%R " : "%b%d "), &date);
ProcessList_add(pl, lwp);
} }
closedir(dir); // Top-level process only gets this for the representative LWP
if (lwp->state == 'O') proc->state = 'O';
if (slwp->kernel && !pl->settings->hideKernelThreads) lwp->show = true;
if (!slwp->kernel && !pl->settings->hideUserlandThreads) lwp->show = true;
lwp->updated = true;
return;
} }
int SolarisProcessList_walkproc(psinfo_t *_psinfo, lwpsinfo_t *_lwpsinfo, void *listptr) {
void ProcessList_goThroughEntries(ProcessList* this) { ProcessList *pl = (ProcessList*) listptr;
SolarisProcessList* spl = (SolarisProcessList*) this; SolarisProcessList *spl = (SolarisProcessList*) listptr;
DIR* dir = NULL; bool preExisting = false;
struct dirent* entry = NULL; Process *proc = ProcessList_getProcess(pl, _psinfo->pr_pid * 1024, &preExisting, (Process_New) SolarisProcess_new);
char* name = NULL; SolarisProcess *sproc = (SolarisProcess*) proc;
int pid;
bool preExisting = false;
Process* proc = NULL;
SolarisProcess* sproc = NULL;
psinfo_t _psinfo;
pstatus_t _pstatus;
prusage_t _prusage;
char filename[MAX_NAME+1];
FILE *fp = NULL;
struct timeval tv; struct timeval tv;
struct tm date; struct tm date;
gettimeofday(&tv, NULL); // Are we on the representative LWP?
if (_lwpsinfo->pr_lwpid == _psinfo->pr_lwp.pr_lwpid) {
// If these fail, then the relevant metrics will simply display as zero
SolarisProcessList_scanCPUTime(this);
SolarisProcessList_scanMemoryInfo(this);
dir = opendir(PROCDIR);
if (!dir) return; // Is proc mounted?
// We always count the scheduler
this->kernelThreads = 1;
while ((entry = readdir(dir)) != NULL) {
name = entry->d_name;
pid = atoi(name);
proc = ProcessList_getProcess(this, pid, &preExisting, (Process_New) SolarisProcess_new);
sproc = (SolarisProcess *) proc;
xSnprintf(filename, MAX_NAME, "%s/%s/psinfo", PROCDIR, name);
fp = fopen(filename, "r");
if ( fp == NULL ) continue;
fread(&_psinfo,sizeof(psinfo_t),1,fp);
fclose(fp);
xSnprintf(filename, MAX_NAME, "%s/%s/status", PROCDIR, name);
fp = fopen(filename, "r");
if ( fp != NULL ) {
fread(&_pstatus,sizeof(pstatus_t),1,fp);
}
fclose(fp);
xSnprintf(filename, MAX_NAME, "%s/%s/usage", PROCDIR, name);
fp = fopen(filename,"r");
if ( fp == NULL ) continue;
fread(&_prusage,sizeof(prusage_t),1,fp);
fclose(fp);
// Common items set for both new and refreshed processes // Common items set for both new and refreshed processes
proc->ppid = (_psinfo.pr_ppid * 1024); proc->ppid = (_psinfo->pr_ppid * 1024);
proc->tgid = (_psinfo.pr_ppid * 1024); proc->tgid = (_psinfo->pr_ppid * 1024);
sproc->realppid = _psinfo.pr_ppid; sproc->realppid = _psinfo->pr_ppid;
sproc->zoneid = _psinfo.pr_zoneid; sproc->zoneid = _psinfo->pr_zoneid;
sproc->zname = SolarisProcessList_readZoneName(spl->kd,sproc); sproc->zname = SolarisProcessList_readZoneName(spl->kd,sproc);
// NOTE: These 'percentages' are 16-bit BINARY FRACTIONS where 1.0 = 0x8000 // NOTE: These 'percentages' are 16-bit BINARY FRACTIONS where 1.0 = 0x8000
// Source: https://docs.oracle.com/cd/E19253-01/816-5174/proc-4/index.html // Source: https://docs.oracle.com/cd/E19253-01/816-5174/proc-4/index.html
// (accessed on 18 November 2017) // (accessed on 18 November 2017)
proc->percent_cpu = ((uint16_t)_psinfo.pr_pctcpu/(double)32768)*(double)100.0; proc->percent_cpu = ((uint16_t)_psinfo->pr_pctcpu/(double)32768)*(double)100.0;
proc->percent_mem = ((uint16_t)_psinfo.pr_pctmem/(double)32768)*(double)100.0; proc->percent_mem = ((uint16_t)_psinfo->pr_pctmem/(double)32768)*(double)100.0;
proc->st_uid = _psinfo.pr_euid; proc->st_uid = _psinfo->pr_euid;
proc->user = UsersTable_getRef(this->usersTable, proc->st_uid); proc->user = UsersTable_getRef(pl->usersTable, proc->st_uid);
proc->pgrp = _psinfo.pr_pgid; proc->pgrp = _psinfo->pr_pgid;
proc->nlwp = _psinfo.pr_nlwp; proc->nlwp = _psinfo->pr_nlwp;
proc->session = _pstatus.pr_sid; proc->comm = xStrdup(_psinfo->pr_fname);
proc->comm = xStrdup(_psinfo.pr_fname); proc->commLen = strnlen(_psinfo->pr_fname,PRFNSZ);
proc->commLen = strnlen(_psinfo.pr_fname,PRFNSZ); proc->tty_nr = _psinfo->pr_ttydev;
proc->tty_nr = _psinfo.pr_ttydev; proc->m_resident = _psinfo->pr_rssize/PAGE_SIZE_KB;
proc->majflt = _prusage.pr_majf; proc->m_size = _psinfo->pr_size/PAGE_SIZE_KB;
proc->minflt = _prusage.pr_minf; proc->priority = _psinfo->pr_lwp.pr_pri;
proc->m_resident = _psinfo.pr_rssize/PAGE_SIZE_KB; proc->nice = _psinfo->pr_lwp.pr_nice;
proc->m_size = _psinfo.pr_size/PAGE_SIZE_KB; proc->processor = _psinfo->pr_lwp.pr_onpro;
proc->priority = _psinfo.pr_lwp.pr_pri; proc->state = _psinfo->pr_lwp.pr_sname;
proc->nice = _psinfo.pr_lwp.pr_nice; proc->time = _psinfo->pr_time.tv_sec;
proc->processor = _psinfo.pr_lwp.pr_onpro; sproc->taskid = _psinfo->pr_taskid;
proc->state = _psinfo.pr_lwp.pr_sname; sproc->projid = _psinfo->pr_projid;
proc->time = _psinfo.pr_time.tv_sec; sproc->poolid = _psinfo->pr_poolid;
sproc->taskid = _psinfo.pr_taskid; sproc->contid = _psinfo->pr_contract;
sproc->projid = _psinfo.pr_projid;
sproc->poolid = _psinfo.pr_poolid;
sproc->contid = _psinfo.pr_contract;
// Tasks done only for NEW processes // Tasks done only for NEW processes
if(!preExisting) { if(!preExisting) {
sproc->is_lwp = false; sproc->is_lwp = false;
// Fake PID values used for sorting, since Solaris LWPs lack unique PIDs // Fake PID values used for sorting, since Solaris LWPs lack unique PIDs
proc->pid = (_psinfo.pr_pid * 1024); proc->pid = (_psinfo->pr_pid * 1024);
// Corresponding real values used for display // Corresponding real values used for display
sproc->realpid = _psinfo.pr_pid; sproc->realpid = _psinfo->pr_pid;
sproc->lwpid = 0; sproc->lwpid = 1;
proc->starttime_ctime = _psinfo.pr_start.tv_sec; proc->starttime_ctime = _psinfo->pr_start.tv_sec;
if ((sproc->realppid <= 0) && !(sproc->realpid <= 1)) { if ((sproc->realppid <= 0) && !(sproc->realpid <= 1)) {
sproc->kernel = true; sproc->kernel = true;
} else { } else {
@ -445,34 +368,35 @@ void ProcessList_goThroughEntries(ProcessList* this) {
} }
(void) localtime_r((time_t*) &proc->starttime_ctime, &date); (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); strftime(proc->starttime_show, 7, ((proc->starttime_ctime > tv.tv_sec - 86400) ? "%R " : "%b%d "), &date);
ProcessList_add(this, proc); ProcessList_add(pl, proc);
} }
if (proc->nlwp > 1) { if (sproc->kernel && !pl->settings->hideKernelThreads) {
ProcessList_enumerateLWPs(proc, name, this, tv); pl->kernelThreads += proc->nlwp;
} pl->totalTasks += proc->nlwp+1;
if (proc->state == 'O') pl->runningTasks++;
proc->show = !(this->settings->hideKernelThreads && sproc->kernel);
if (sproc->kernel && !this->settings->hideKernelThreads) {
this->kernelThreads += proc->nlwp;
this->totalTasks += proc->nlwp+1;
if (proc->state == 'O') this->runningTasks++;
} else if (!sproc->kernel) { } else if (!sproc->kernel) {
if (proc->state == 'O') this->runningTasks++; if (proc->state == 'O') pl->runningTasks++;
if (this->settings->hideUserlandThreads) { if (pl->settings->hideUserlandThreads) {
this->totalTasks++; pl->totalTasks++;
} else { } else {
this->userlandThreads += proc->nlwp; pl->userlandThreads += proc->nlwp;
this->totalTasks += proc->nlwp+1; pl->totalTasks += proc->nlwp+1;
} }
} }
proc->show = !(pl->settings->hideKernelThreads && sproc->kernel);
proc->updated = true; proc->updated = true;
} else {
} // while ((entry = readdir(dir)) != NULL) // We are not in the rep. LWP, so jump to the LWP handling code
ProcessList_enumerateLWPs(proc, pl, _lwpsinfo, tv);
closedir(dir); }
return 0;
}
void ProcessList_goThroughEntries(ProcessList* this) {
SolarisProcessList_scanCPUTime(this);
SolarisProcessList_scanMemoryInfo(this);
this->kernelThreads = 1;
proc_walk( &SolarisProcessList_walkproc, this, PR_WALK_LWP );
} }

View File

@ -15,7 +15,6 @@ in the source distribution for its full text.
#include <kstat.h> #include <kstat.h>
#include <sys/param.h> #include <sys/param.h>
#include <zone.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/sysconf.h> #include <sys/sysconf.h>
@ -51,7 +50,9 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
void ProcessList_delete(ProcessList* this); void ProcessList_delete(ProcessList* this);
void ProcessList_enumerateLWPs(Process* proc, char* name, ProcessList* pl, struct timeval tv); void ProcessList_enumerateLWPs(Process* proc, ProcessList* pl, lwpsinfo_t *_lwpsinfo, struct timeval tv);
int SolarisProcessList_walkproc(psinfo_t *_psinfo, lwpsinfo_t *_lwpsinfo, void *listptr);
void ProcessList_goThroughEntries(ProcessList* this); void ProcessList_goThroughEntries(ProcessList* this);