aboutsummaryrefslogtreecommitdiffstats
path: root/ProcessList.c
diff options
context:
space:
mode:
authorDaniel Lange <DLange@git.local>2016-04-11 13:00:16 +0200
committerDaniel Lange <DLange@git.local>2016-04-11 13:00:16 +0200
commitc868e3f8d629e1caddff1a51a7d2dd04f49994a3 (patch)
treedee0881088acc628312dcaaff8209184f4a2560b /ProcessList.c
parentd3c9975943df58e293359b87905d19ff1fd52061 (diff)
downloaddebian_htop-c868e3f8d629e1caddff1a51a7d2dd04f49994a3.tar.gz
debian_htop-c868e3f8d629e1caddff1a51a7d2dd04f49994a3.tar.bz2
debian_htop-c868e3f8d629e1caddff1a51a7d2dd04f49994a3.zip
Imported Upstream version 0.5.1upstream/0.5.1
Diffstat (limited to 'ProcessList.c')
-rw-r--r--ProcessList.c209
1 files changed, 118 insertions, 91 deletions
diff --git a/ProcessList.c b/ProcessList.c
index 9f09f48..61241a4 100644
--- a/ProcessList.c
+++ b/ProcessList.c
@@ -83,6 +83,7 @@ typedef struct ProcessList_ {
bool hideThreads;
bool shadowOtherUsers;
bool hideKernelThreads;
+ bool hideUserlandThreads;
bool treeView;
bool highlightBaseName;
bool highlightMegabytes;
@@ -157,6 +158,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable) {
this->hideThreads = false;
this->shadowOtherUsers = false;
this->hideKernelThreads = false;
+ this->hideUserlandThreads = false;
this->treeView = false;
this->highlightBaseName = false;
this->highlightMegabytes = false;
@@ -343,94 +345,14 @@ int ProcessList_readStatFile(Process *proc, FILE *f, char *command) {
return 1;
}
-void ProcessList_scan(ProcessList* this) {
- DIR* proc;
+void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, float period) {
+ DIR* dir;
struct dirent* entry;
Process* prototype = this->prototype;
- long int usertime, nicetime, systemtime, idletime, totaltime;
-
- FILE* status;
- char buffer[128];
- status = fopen(PROCMEMINFOFILE, "r");
- assert(status != NULL);
- while (!feof(status)) {
- fgets(buffer, 128, status);
- if (String_startsWith(buffer, "MemTotal:")) {
- sscanf(buffer, "MemTotal: %ld kB", &this->totalMem);
- } else if (String_startsWith(buffer, "MemFree:")) {
- sscanf(buffer, "MemFree: %ld kB", &this->freeMem);
- this->usedMem = this->totalMem - this->freeMem;
- } else if (String_startsWith(buffer, "MemShared:")) {
- sscanf(buffer, "MemShared: %ld kB", &this->sharedMem);
- } else if (String_startsWith(buffer, "Buffers:")) {
- sscanf(buffer, "Buffers: %ld kB", &this->buffersMem);
- } else if (String_startsWith(buffer, "Cached:")) {
- sscanf(buffer, "Cached: %ld kB", &this->cachedMem);
- } else if (String_startsWith(buffer, "SwapTotal:")) {
- sscanf(buffer, "SwapTotal: %ld kB", &this->totalSwap);
- } else if (String_startsWith(buffer, "SwapFree:")) {
- long int swapFree;
- sscanf(buffer, "SwapFree: %ld kB", &swapFree);
- this->usedSwap = this->totalSwap - swapFree;
- }
- }
- fclose(status);
-
- status = fopen(PROCSTATFILE, "r");
- assert(status != NULL);
- for (int i = 0; i <= this->processorCount; i++) {
- int cpuid;
- if (this->kernelMajor == 2 && this->kernelMiddle <= 4) {
- if (i == 0) {
- fscanf(status, "cpu %ld %ld %ld %ld\n", &usertime, &nicetime, &systemtime, &idletime);
- } else {
- fscanf(status, "cpu%d %ld %ld %ld %ld\n", &cpuid, &usertime, &nicetime, &systemtime, &idletime);
- assert(cpuid == i - 1);
- }
- totaltime = usertime + nicetime + systemtime + idletime;
- } else {
- long int ioWait, irq, softIrq;
- if (i == 0)
- fscanf(status, "cpu %ld %ld %ld %ld %ld %ld %ld\n", &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq);
- else {
- fscanf(status, "cpu%d %ld %ld %ld %ld %ld %ld %ld\n", &cpuid, &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq);
- assert(cpuid == i - 1);
- }
- systemtime += ioWait + irq + softIrq;
- totaltime = usertime + nicetime + systemtime + idletime;
- }
- assert (usertime >= this->userTime[i]);
- assert (nicetime >= this->niceTime[i]);
- assert (systemtime >= this->systemTime[i]);
- assert (idletime >= this->idleTime[i]);
- assert (totaltime >= this->totalTime[i]);
- this->userPeriod[i] = usertime - this->userTime[i];
- this->nicePeriod[i] = nicetime - this->niceTime[i];
- this->systemPeriod[i] = systemtime - this->systemTime[i];
- this->idlePeriod[i] = idletime - this->idleTime[i];
- this->totalPeriod[i] = totaltime - this->totalTime[i];
- this->userTime[i] = usertime;
- this->niceTime[i] = nicetime;
- this->systemTime[i] = systemtime;
- this->idleTime[i] = idletime;
- this->totalTime[i] = totaltime;
- }
- float period = (float)this->totalPeriod[0] / this->processorCount;
- fclose(status);
- // mark all process as "dirty"
- for (int i = 0; i < TypedVector_size(this->processes); i++) {
- Process* p = (Process*) TypedVector_get(this->processes, i);
- p->updated = false;
- }
-
- this->totalTasks = 0;
- this->runningTasks = 0;
-
- proc = opendir(PROCDIR);
- assert(proc != NULL);
- signal(11, ProcessList_dontCrash);
- while ((entry = readdir(proc)) != NULL) {
+ dir = opendir(dirname);
+ assert(dir != NULL);
+ while ((entry = readdir(dir)) != NULL) {
char* name = entry->d_name;
int pid;
// filename is a number: process directory
@@ -446,9 +368,18 @@ void ProcessList_scan(ProcessList* this) {
isThread = true;
}
- if (pid > 0) {
- FILE* status;
+ if (pid > 0 && pid != parent) {
const int MAX_NAME = 128;
+ if (!this->hideUserlandThreads) {
+ char subdirname[MAX_NAME+1];
+ snprintf(subdirname, MAX_NAME, "%s/%s/task", dirname, name);
+
+ if (access(subdirname, X_OK) == 0) {
+ ProcessList_processEntries(this, subdirname, pid, period);
+ }
+ }
+
+ FILE* status;
char statusfilename[MAX_NAME+1];
char command[PROCESS_COMM_LEN + 1];
@@ -464,7 +395,7 @@ void ProcessList_scan(ProcessList* this) {
process->updated = true;
struct stat sstat;
- snprintf(statusfilename, MAX_NAME, "%s/%s/stat", PROCDIR, name);
+ snprintf(statusfilename, MAX_NAME, "%s/%s/stat", dirname, name);
int statok = stat(statusfilename, &sstat);
if (statok == -1)
goto errorReadingProcess;
@@ -493,7 +424,7 @@ void ProcessList_scan(ProcessList* this) {
period * 100.0;
if(!existingProcess) {
- snprintf(statusfilename, MAX_NAME, "%s/%s/cmdline", PROCDIR, name);
+ snprintf(statusfilename, MAX_NAME, "%s/%s/cmdline", dirname, name);
status = fopen(statusfilename, "r");
if (!status) {
goto errorReadingProcess;
@@ -514,7 +445,7 @@ void ProcessList_scan(ProcessList* this) {
fclose(status);
}
- snprintf(statusfilename, MAX_NAME, "%s/%s/statm", PROCDIR, name);
+ snprintf(statusfilename, MAX_NAME, "%s/%s/statm", dirname, name);
status = fopen(statusfilename, "r");
if(!status) {
goto errorReadingProcess;
@@ -547,8 +478,104 @@ void ProcessList_scan(ProcessList* this) {
}
}
}
+ closedir(dir);
+}
+
+void ProcessList_scan(ProcessList* this) {
+ long int usertime, nicetime, systemtime, idletime, totaltime;
+ long int swapFree;
+
+ FILE* status;
+ char buffer[128];
+ status = fopen(PROCMEMINFOFILE, "r");
+ assert(status != NULL);
+ while (!feof(status)) {
+ fgets(buffer, 128, status);
+
+ switch (buffer[0]) {
+ case 'M':
+ if (String_startsWith(buffer, "MemTotal:"))
+ sscanf(buffer, "MemTotal: %ld kB", &this->totalMem);
+ else if (String_startsWith(buffer, "MemFree:"))
+ sscanf(buffer, "MemFree: %ld kB", &this->freeMem);
+ else if (String_startsWith(buffer, "MemShared:"))
+ sscanf(buffer, "MemShared: %ld kB", &this->sharedMem);
+ break;
+ case 'B':
+ if (String_startsWith(buffer, "Buffers:"))
+ sscanf(buffer, "Buffers: %ld kB", &this->buffersMem);
+ break;
+ case 'C':
+ if (String_startsWith(buffer, "Cached:"))
+ sscanf(buffer, "Cached: %ld kB", &this->cachedMem);
+ break;
+ case 'S':
+ if (String_startsWith(buffer, "SwapTotal:"))
+ sscanf(buffer, "SwapTotal: %ld kB", &this->totalSwap);
+ if (String_startsWith(buffer, "SwapFree:"))
+ sscanf(buffer, "SwapFree: %ld kB", &swapFree);
+ break;
+ }
+ }
+ this->usedMem = this->totalMem - this->freeMem;
+ this->usedSwap = this->totalSwap - swapFree;
+ fclose(status);
+
+ status = fopen(PROCSTATFILE, "r");
+ assert(status != NULL);
+ for (int i = 0; i <= this->processorCount; i++) {
+ int cpuid;
+ if (this->kernelMajor == 2 && this->kernelMiddle <= 4) {
+ if (i == 0) {
+ fscanf(status, "cpu %ld %ld %ld %ld\n", &usertime, &nicetime, &systemtime, &idletime);
+ } else {
+ fscanf(status, "cpu%d %ld %ld %ld %ld\n", &cpuid, &usertime, &nicetime, &systemtime, &idletime);
+ assert(cpuid == i - 1);
+ }
+ totaltime = usertime + nicetime + systemtime + idletime;
+ } else {
+ long int ioWait, irq, softIrq;
+ if (i == 0)
+ fscanf(status, "cpu %ld %ld %ld %ld %ld %ld %ld\n", &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq);
+ else {
+ fscanf(status, "cpu%d %ld %ld %ld %ld %ld %ld %ld\n", &cpuid, &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq);
+ assert(cpuid == i - 1);
+ }
+ systemtime += ioWait + irq + softIrq;
+ totaltime = usertime + nicetime + systemtime + idletime;
+ }
+ assert (usertime >= this->userTime[i]);
+ assert (nicetime >= this->niceTime[i]);
+ assert (systemtime >= this->systemTime[i]);
+ assert (idletime >= this->idleTime[i]);
+ assert (totaltime >= this->totalTime[i]);
+ this->userPeriod[i] = usertime - this->userTime[i];
+ this->nicePeriod[i] = nicetime - this->niceTime[i];
+ this->systemPeriod[i] = systemtime - this->systemTime[i];
+ this->idlePeriod[i] = idletime - this->idleTime[i];
+ this->totalPeriod[i] = totaltime - this->totalTime[i];
+ this->userTime[i] = usertime;
+ this->niceTime[i] = nicetime;
+ this->systemTime[i] = systemtime;
+ this->idleTime[i] = idletime;
+ this->totalTime[i] = totaltime;
+ }
+ float period = (float)this->totalPeriod[0] / this->processorCount;
+ fclose(status);
+
+ // mark all process as "dirty"
+ for (int i = 0; i < TypedVector_size(this->processes); i++) {
+ Process* p = (Process*) TypedVector_get(this->processes, i);
+ p->updated = false;
+ }
+
+ this->totalTasks = 0;
+ this->runningTasks = 0;
+
+ signal(11, ProcessList_dontCrash);
+
+ ProcessList_processEntries(this, PROCDIR, 0, period);
signal(11, SIG_DFL);
- closedir(proc);
for (int i = TypedVector_size(this->processes) - 1; i >= 0; i--) {
Process* p = (Process*) TypedVector_get(this->processes, i);

© 2014-2024 Faster IT GmbH | imprint | privacy policy