Sorry about the mega-patch.

This is a work-in-progress, code is currently broken.
(Some actions, and notably, the header, are missing.)
This commit is contained in:
Hisham Muhammad
2015-01-21 23:27:31 -02:00
parent 36b7832884
commit 3383d8e556
51 changed files with 2011 additions and 1777 deletions

View File

@ -45,22 +45,23 @@ bool LinuxProcess_setIOPriority(LinuxProcess* this, IOPriority ioprio) {
return (LinuxProcess_updateIOPriority(this) == ioprio);
}
void LinuxProcess_writeField(LinuxProcess* this, RichString* str, ProcessField field) {
void Process_writeField(Process* this, RichString* str, ProcessField field) {
LinuxProcess* lp = (LinuxProcess*) this;
char buffer[256]; buffer[255] = '\0';
int attr = CRT_colors[DEFAULT_COLOR];
int n = sizeof(buffer) - 1;
switch (field) {
case IO_PRIORITY: {
int klass = IOPriority_class(this->ioPriority);
int klass = IOPriority_class(lp->ioPriority);
if (klass == IOPRIO_CLASS_NONE) {
// see note [1] above
snprintf(buffer, n, "B%1d ", (int) (this->super.nice + 20) / 5);
snprintf(buffer, n, "B%1d ", (int) (this->nice + 20) / 5);
} else if (klass == IOPRIO_CLASS_BE) {
snprintf(buffer, n, "B%1d ", IOPriority_data(this->ioPriority));
snprintf(buffer, n, "B%1d ", IOPriority_data(lp->ioPriority));
} else if (klass == IOPRIO_CLASS_RT) {
attr = CRT_colors[PROCESS_HIGH_PRIORITY];
snprintf(buffer, n, "R%1d ", IOPriority_data(this->ioPriority));
} else if (this->ioPriority == IOPriority_Idle) {
snprintf(buffer, n, "R%1d ", IOPriority_data(lp->ioPriority));
} else if (lp->ioPriority == IOPriority_Idle) {
attr = CRT_colors[PROCESS_LOW_PRIORITY];
snprintf(buffer, n, "id ");
} else {
@ -69,25 +70,26 @@ void LinuxProcess_writeField(LinuxProcess* this, RichString* str, ProcessField f
break;
}
default:
snprintf(buffer, n, "- ");
Process_writeDefaultField(this, str, field);
return;
}
RichString_append(str, attr, buffer);
}
long LinuxProcess_compare(const void* v1, const void* v2) {
long Process_compare(const void* v1, const void* v2) {
LinuxProcess *p1, *p2;
ProcessList *pl = ((Process*)v1)->pl;
if (pl->direction == 1) {
Settings *settings = ((Process*)v1)->settings;
if (settings->direction == 1) {
p1 = (LinuxProcess*)v1;
p2 = (LinuxProcess*)v2;
} else {
p2 = (LinuxProcess*)v1;
p1 = (LinuxProcess*)v2;
}
switch (pl->sortKey) {
switch (settings->sortKey) {
case IO_PRIORITY:
return LinuxProcess_effectiveIOPriority(p1) - LinuxProcess_effectiveIOPriority(p2);
default:
return (p1->super.pid - p2->super.pid);
return Process_defaultCompare(v1, v2);
}
}

View File

@ -32,8 +32,8 @@ IOPriority LinuxProcess_updateIOPriority(LinuxProcess* this);
bool LinuxProcess_setIOPriority(LinuxProcess* this, IOPriority ioprio);
void LinuxProcess_writeField(LinuxProcess* this, RichString* str, ProcessField field);
void Process_writeField(Process* this, RichString* str, ProcessField field);
long LinuxProcess_compare(const void* v1, const void* v2);
long Process_compare(const void* v1, const void* v2);
#endif

View File

@ -31,6 +31,56 @@ in the source distribution for its full text.
#include "ProcessList.h"
typedef struct CPUData_ {
unsigned long long int totalTime;
unsigned long long int userTime;
unsigned long long int systemTime;
unsigned long long int systemAllTime;
unsigned long long int idleAllTime;
unsigned long long int idleTime;
unsigned long long int niceTime;
unsigned long long int ioWaitTime;
unsigned long long int irqTime;
unsigned long long int softIrqTime;
unsigned long long int stealTime;
unsigned long long int guestTime;
unsigned long long int totalPeriod;
unsigned long long int userPeriod;
unsigned long long int systemPeriod;
unsigned long long int systemAllPeriod;
unsigned long long int idleAllPeriod;
unsigned long long int idlePeriod;
unsigned long long int nicePeriod;
unsigned long long int ioWaitPeriod;
unsigned long long int irqPeriod;
unsigned long long int softIrqPeriod;
unsigned long long int stealPeriod;
unsigned long long int guestPeriod;
} CPUData;
typedef struct LinuxProcessList_ {
ProcessList super;
int totalTasks;
int userlandThreads;
int kernelThreads;
int runningTasks;
CPUData* cpus;
unsigned long long int totalMem;
unsigned long long int usedMem;
unsigned long long int freeMem;
unsigned long long int sharedMem;
unsigned long long int buffersMem;
unsigned long long int cachedMem;
unsigned long long int totalSwap;
unsigned long long int usedSwap;
unsigned long long int freeSwap;
} LinuxProcessList;
#ifndef PROCDIR
#define PROCDIR "/proc"
#endif
@ -45,9 +95,10 @@ in the source distribution for its full text.
}*/
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList) {
ProcessList* this = calloc(1, sizeof(ProcessList));
ProcessList_init(this, usersTable, pidWhiteList);
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
LinuxProcessList* this = calloc(1, sizeof(LinuxProcessList));
ProcessList* pl = &(this->super);
ProcessList_init(pl, usersTable, pidWhiteList, userId);
// Update CPU count:
FILE* file = fopen(PROCSTATFILE, "r");
@ -62,7 +113,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList) {
} while (String_startsWith(buffer, "cpu"));
fclose(file);
this->cpuCount = MAX(cpus - 1, 1);
pl->cpuCount = MAX(cpus - 1, 1);
this->cpus = calloc(cpus, sizeof(CPUData));
for (int i = 0; i < cpus; i++) {
@ -74,11 +125,13 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList) {
this->flags |= PROCESS_FLAG_OPENVZ;
#endif
return this;
return pl;
}
void ProcessList_delete(ProcessList* this) {
ProcessList_done(this);
void ProcessList_delete(ProcessList* pl) {
LinuxProcessList* this = (LinuxProcessList*) pl;
ProcessList_done(pl);
free(this->cpus);
free(this);
}
@ -166,7 +219,6 @@ static bool LinuxProcessList_readStatFile(Process *process, const char* dirname,
location += 1;
assert(location != NULL);
process->processor = strtol(location, &location, 10);
assert(location == NULL);
return true;
}
@ -200,8 +252,11 @@ static void LinuxProcessList_readIoFile(Process* process, const char* dirname, c
snprintf(filename, MAX_NAME, "%s/%s/io", dirname, name);
int fd = open(filename, O_RDONLY);
if (fd == -1)
if (fd == -1) {
process->io_rate_read_bps = -1;
process->io_rate_write_bps = -1;
return;
}
char buffer[1024];
ssize_t buflen = xread(fd, buffer, 1023);
@ -420,9 +475,10 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, const char* dirna
return true;
}
static bool LinuxProcessList_processEntries(ProcessList* this, const char* dirname, Process* parent, double period, struct timeval tv) {
static bool LinuxProcessList_processEntries(LinuxProcessList* this, const char* dirname, Process* parent, double period, struct timeval tv) {
DIR* dir;
struct dirent* entry;
Settings* settings = this->super.settings;
time_t curTime = tv.tv_sec;
#ifdef HAVE_TASKSTATS
@ -431,15 +487,15 @@ static bool LinuxProcessList_processEntries(ProcessList* this, const char* dirna
dir = opendir(dirname);
if (!dir) return false;
int cpus = this->cpuCount;
bool hideKernelThreads = this->hideKernelThreads;
bool hideUserlandThreads = this->hideUserlandThreads;
int cpus = this->super.cpuCount;
bool hideKernelThreads = settings->hideKernelThreads;
bool hideUserlandThreads = settings->hideUserlandThreads;
while ((entry = readdir(dir)) != NULL) {
char* name = entry->d_name;
// The RedHat kernel hides threads with a dot.
// I believe this is non-standard.
if ((!this->hideThreads) && name[0] == '.') {
if ((!settings->hideThreads) && name[0] == '.') {
name++;
}
@ -458,14 +514,14 @@ static bool LinuxProcessList_processEntries(ProcessList* this, const char* dirna
continue;
Process* process = NULL;
Process* existingProcess = (Process*) Hashtable_get(this->processTable, pid);
Process* existingProcess = (Process*) Hashtable_get(this->super.processTable, pid);
if (existingProcess) {
assert(Vector_indexOf(this->processes, existingProcess, Process_pidCompare) != -1);
process = existingProcess;
assert(process->pid == pid);
} else {
process = Process_new(this);
process = Process_new(settings);
assert(process->comm == NULL);
process->pid = pid;
process->tgid = parent ? parent->pid : pid;
@ -476,7 +532,7 @@ static bool LinuxProcessList_processEntries(ProcessList* this, const char* dirna
LinuxProcessList_processEntries(this, subdirname, process, period, tv);
#ifdef HAVE_TASKSTATS
if (this->flags & PROCESS_FLAG_IO)
if (settings->flags & PROCESS_FLAG_IO)
LinuxProcessList_readIoFile(process, dirname, name, now);
#endif
@ -489,7 +545,7 @@ static bool LinuxProcessList_processEntries(ProcessList* this, const char* dirna
unsigned long long int lasttimes = (process->utime + process->stime);
if (! LinuxProcessList_readStatFile(process, dirname, name, command))
goto errorReadingProcess;
if (this->flags & PROCESS_FLAG_IOPRIO)
if (settings->flags & PROCESS_FLAG_IOPRIO)
LinuxProcess_updateIOPriority((LinuxProcess*)process);
float percent_cpu = (process->utime + process->stime - lasttimes) / period * 100.0;
process->percent_cpu = MAX(MIN(percent_cpu, cpus*100.0), 0.0);
@ -501,30 +557,30 @@ static bool LinuxProcessList_processEntries(ProcessList* this, const char* dirna
if (! LinuxProcessList_statProcessDir(process, dirname, name, curTime))
goto errorReadingProcess;
process->user = UsersTable_getRef(this->usersTable, process->st_uid);
process->user = UsersTable_getRef(this->super.usersTable, process->st_uid);
#ifdef HAVE_OPENVZ
LinuxProcessList_readOpenVZData(this, process, dirname, name);
#endif
#ifdef HAVE_VSERVER
if (this->flags & PROCESS_FLAG_VSERVER)
if (settings->flags & PROCESS_FLAG_VSERVER)
LinuxProcessList_readVServerData(process, dirname, name);
#endif
if (! LinuxProcessList_readCmdlineFile(process, dirname, name))
goto errorReadingProcess;
ProcessList_add(this, process);
ProcessList_add((ProcessList*)this, process);
} else {
if (this->updateProcessNames) {
if (settings->updateProcessNames) {
if (! LinuxProcessList_readCmdlineFile(process, dirname, name))
goto errorReadingProcess;
}
}
#ifdef HAVE_CGROUP
if (this->flags & PROCESS_FLAG_CGROUP)
if (settings->flags & PROCESS_FLAG_CGROUP)
LinuxProcessList_readCGroupFile(process, dirname, name);
#endif
@ -537,11 +593,11 @@ static bool LinuxProcessList_processEntries(ProcessList* this, const char* dirna
process->basenameOffset = -1;
process->comm = strdup(command);
} else if (Process_isThread(process)) {
if (this->showThreadNames || Process_isKernelThread(process) || process->state == 'Z') {
if (settings->showThreadNames || Process_isKernelThread(process) || process->state == 'Z') {
free(process->comm);
process->basenameOffset = -1;
process->comm = strdup(command);
} else if (this->showingThreadNames) {
} else if (settings->showThreadNames) {
if (! LinuxProcessList_readCmdlineFile(process, dirname, name))
goto errorReadingProcess;
}
@ -567,7 +623,7 @@ static bool LinuxProcessList_processEntries(ProcessList* this, const char* dirna
process->comm = NULL;
}
if (existingProcess)
ProcessList_remove(this, process);
ProcessList_remove((ProcessList*)this, process);
else
Process_delete((Object*)process);
}
@ -576,7 +632,7 @@ static bool LinuxProcessList_processEntries(ProcessList* this, const char* dirna
return true;
}
static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) {
static inline void LinuxProcessList_scanMemoryInfo(LinuxProcessList* this) {
unsigned long long int swapFree = 0;
FILE* file = fopen(PROCMEMINFOFILE, "r");
@ -617,14 +673,14 @@ static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) {
fclose(file);
}
static inline double LinuxProcessList_scanCPUTime(ProcessList* this) {
static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this) {
unsigned long long int usertime, nicetime, systemtime, idletime;
FILE* file = fopen(PROCSTATFILE, "r");
if (file == NULL) {
CRT_fatalError("Cannot open " PROCSTATFILE);
}
int cpus = this->cpuCount;
int cpus = this->super.cpuCount;
assert(cpus > 0);
for (int i = 0; i <= cpus; i++) {
char buffer[256];
@ -693,15 +749,16 @@ static inline double LinuxProcessList_scanCPUTime(ProcessList* this) {
return period;
}
void ProcessList_scan(ProcessList* this) {
void ProcessList_scan(ProcessList* super) {
LinuxProcessList* this = (LinuxProcessList*) super;
LinuxProcessList_scanMemoryInfo(this);
double period = LinuxProcessList_scanCPUTime(this);
// mark all process as "dirty"
for (int i = 0; i < Vector_size(this->processes); i++) {
Process* p = (Process*) Vector_get(this->processes, i);
for (int i = 0; i < Vector_size(super->processes); i++) {
Process* p = (Process*) Vector_get(super->processes, i);
p->updated = false;
}
@ -714,12 +771,10 @@ void ProcessList_scan(ProcessList* this) {
gettimeofday(&tv, NULL);
LinuxProcessList_processEntries(this, PROCDIR, NULL, period, tv);
this->showingThreadNames = this->showThreadNames;
for (int i = Vector_size(this->processes) - 1; i >= 0; i--) {
Process* p = (Process*) Vector_get(this->processes, i);
for (int i = Vector_size(this->super.processes) - 1; i >= 0; i--) {
Process* p = (Process*) Vector_get(this->super.processes, i);
if (p->updated == false)
ProcessList_remove(this, p);
ProcessList_remove(super, p);
else
p->updated = false;
}

View File

@ -12,6 +12,56 @@ in the source distribution for its full text.
#include "ProcessList.h"
typedef struct CPUData_ {
unsigned long long int totalTime;
unsigned long long int userTime;
unsigned long long int systemTime;
unsigned long long int systemAllTime;
unsigned long long int idleAllTime;
unsigned long long int idleTime;
unsigned long long int niceTime;
unsigned long long int ioWaitTime;
unsigned long long int irqTime;
unsigned long long int softIrqTime;
unsigned long long int stealTime;
unsigned long long int guestTime;
unsigned long long int totalPeriod;
unsigned long long int userPeriod;
unsigned long long int systemPeriod;
unsigned long long int systemAllPeriod;
unsigned long long int idleAllPeriod;
unsigned long long int idlePeriod;
unsigned long long int nicePeriod;
unsigned long long int ioWaitPeriod;
unsigned long long int irqPeriod;
unsigned long long int softIrqPeriod;
unsigned long long int stealPeriod;
unsigned long long int guestPeriod;
} CPUData;
typedef struct LinuxProcessList_ {
ProcessList super;
int totalTasks;
int userlandThreads;
int kernelThreads;
int runningTasks;
CPUData* cpus;
unsigned long long int totalMem;
unsigned long long int usedMem;
unsigned long long int freeMem;
unsigned long long int sharedMem;
unsigned long long int buffersMem;
unsigned long long int cachedMem;
unsigned long long int totalSwap;
unsigned long long int usedSwap;
unsigned long long int freeSwap;
} LinuxProcessList;
#ifndef PROCDIR
#define PROCDIR "/proc"
#endif
@ -25,9 +75,9 @@ in the source distribution for its full text.
#endif
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList);
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId);
void ProcessList_delete(ProcessList* this);
void ProcessList_delete(ProcessList* pl);
#ifdef HAVE_TASKSTATS
@ -50,7 +100,7 @@ void ProcessList_delete(ProcessList* this);
#endif
void ProcessList_scan(ProcessList* this);
void ProcessList_scan(ProcessList* super);
#endif

View File

@ -9,6 +9,7 @@ in the source distribution for its full text.
#include "IOPriority.h"
#include "IOPriorityPanel.h"
#include "LinuxProcess.h"
#include "LinuxProcessList.h"
#include "Battery.h"
#include "Meter.h"
@ -29,14 +30,15 @@ in the source distribution for its full text.
#include "BatteryMeter.h"
}*/
static Htop_Reaction Platform_actionSetIOPriority(Panel* panel, ProcessList* pl, Header* header) {
(void) panel, (void) pl;
static Htop_Reaction Platform_actionSetIOPriority(State* st) {
Panel* panel = st->panel;
LinuxProcess* p = (LinuxProcess*) Panel_getSelected(panel);
if (!p) return HTOP_OK;
IOPriority ioprio = p->ioPriority;
Panel* ioprioPanel = IOPriorityPanel_new(ioprio);
const char* fuFunctions[] = {"Set ", "Cancel ", NULL};
void* set = Action_pickFromVector(panel, ioprioPanel, 21, fuFunctions, header);
void* set = Action_pickFromVector(st, ioprioPanel, 21, fuFunctions);
if (set) {
IOPriority ioprio = IOPriorityPanel_getIOPriority(ioprioPanel);
bool ok = Action_foreachProcess(panel, (Action_ForeachProcessFn) LinuxProcess_setIOPriority, (size_t) ioprio, NULL);
@ -120,3 +122,63 @@ void Platform_getBatteryLevel(double* level, ACPresence* isOnAC) {
*isOnAC = Battery_isOnAC();
*level = percent;
}
double Platform_setCPUValues(Meter* this, int cpu) {
LinuxProcessList* pl = (LinuxProcessList*) this->pl;
CPUData* cpuData = &(pl->cpus[cpu]);
double total = (double) ( cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod);
double percent;
double* v = this->values;
v[0] = cpuData->nicePeriod / total * 100.0;
v[1] = cpuData->userPeriod / total * 100.0;
if (this->pl->settings->detailedCPUTime) {
v[2] = cpuData->systemPeriod / total * 100.0;
v[3] = cpuData->irqPeriod / total * 100.0;
v[4] = cpuData->softIrqPeriod / total * 100.0;
v[5] = cpuData->stealPeriod / total * 100.0;
v[6] = cpuData->guestPeriod / total * 100.0;
v[7] = cpuData->ioWaitPeriod / total * 100.0;
Meter_setItems(this, 8);
if (this->pl->settings->accountGuestInCPUMeter) {
percent = v[0]+v[1]+v[2]+v[3]+v[4]+v[5]+v[6];
} else {
percent = v[0]+v[1]+v[2]+v[3]+v[4];
}
} else {
v[2] = cpuData->systemAllPeriod / total * 100.0;
v[3] = (cpuData->stealPeriod + cpuData->guestPeriod) / total * 100.0;
Meter_setItems(this, 4);
percent = v[0]+v[1]+v[2]+v[3];
}
percent = MIN(100.0, MAX(0.0, percent));
if (isnan(percent)) percent = 0.0;
return percent;
}
void Platform_setMemoryValues(Meter* this) {
LinuxProcessList* pl = (LinuxProcessList*) this->pl;
long int usedMem = pl->usedMem;
long int buffersMem = pl->buffersMem;
long int cachedMem = pl->cachedMem;
usedMem -= buffersMem + cachedMem;
this->total = pl->totalMem;
this->values[0] = usedMem;
this->values[1] = buffersMem;
this->values[2] = cachedMem;
}
void Platform_setSwapValues(Meter* this) {
LinuxProcessList* pl = (LinuxProcessList*) this->pl;
this->total = pl->totalSwap;
this->values[0] = pl->usedSwap;
}
void Platform_setTasksValues(Meter* this) {
LinuxProcessList* pl = (LinuxProcessList*) this->pl;
this->values[0] = pl->kernelThreads;
this->values[1] = pl->userlandThreads;
this->values[2] = pl->totalTasks - pl->kernelThreads - pl->userlandThreads;
this->values[3] = pl->runningTasks;
if (pl->totalTasks > this->total)
this->total = pl->totalTasks;
}

View File

@ -24,4 +24,12 @@ int Platform_getMaxPid();
void Platform_getBatteryLevel(double* level, ACPresence* isOnAC);
double Platform_setCPUValues(Meter* this, int cpu);
void Platform_setMemoryValues(Meter* this);
void Platform_setSwapValues(Meter* this);
void Platform_setTasksValues(Meter* this);
#endif