blob: 3d87ec009cd0d5651a2621a055447eb90c299f3e [file] [log] [blame]
Hisham Muhammadd6231ba2006-03-04 18:16:49 +00001/*
2htop - ProcessList.c
3(C) 2004,2005 Hisham H. Muhammad
4Released under the GNU GPL, see the COPYING file
5in the source distribution for its full text.
6*/
7
8#include "ProcessList.h"
9#include "Process.h"
10#include "TypedVector.h"
11#include "UsersTable.h"
12#include "Hashtable.h"
13
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <unistd.h>
17#include <dirent.h>
18#include <stdlib.h>
19#include <stdio.h>
20#include <signal.h>
21#include <stdbool.h>
22#include <sys/utsname.h>
23
24#include "debug.h"
25#include <assert.h>
26
27/*{
28#ifndef PROCDIR
29#define PROCDIR "/proc"
30#endif
31
32#ifndef PROCSTATFILE
33#define PROCSTATFILE "/proc/stat"
34#endif
35
36#ifndef PROCMEMINFOFILE
37#define PROCMEMINFOFILE "/proc/meminfo"
38#endif
39
40#ifndef MAX_NAME
41#define MAX_NAME 128;
42#endif
43
44}*/
45
46/*{
47
48typedef struct ProcessList_ {
49 TypedVector* processes;
50 TypedVector* processes2;
51 Hashtable* processTable;
52 Process* prototype;
53 UsersTable* usersTable;
54
55 int processorCount;
56 int totalTasks;
57 int runningTasks;
58
59 long int* totalTime;
60 long int* userTime;
61 long int* systemTime;
62 long int* idleTime;
63 long int* niceTime;
64 long int* totalPeriod;
65 long int* userPeriod;
66 long int* systemPeriod;
67 long int* idlePeriod;
68 long int* nicePeriod;
69
70 long int totalMem;
71 long int usedMem;
72 long int freeMem;
73 long int sharedMem;
74 long int buffersMem;
75 long int cachedMem;
76 long int totalSwap;
77 long int usedSwap;
78 long int freeSwap;
79
80 ProcessField* fields;
81 ProcessField sortKey;
82 int direction;
83 bool hideThreads;
84 bool shadowOtherUsers;
85 bool hideKernelThreads;
86 bool hideUserlandThreads;
87 bool treeView;
88 bool highlightBaseName;
89 bool highlightMegabytes;
90
91} ProcessList;
92}*/
93
94/* private property */
95ProcessField defaultHeaders[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, M_SHARE, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, LAST_PROCESSFIELD, 0 };
96
97ProcessList* ProcessList_new(UsersTable* usersTable) {
98 ProcessList* this;
99 this = malloc(sizeof(ProcessList));
100 this->processes = TypedVector_new(PROCESS_CLASS, true, DEFAULT_SIZE);
101 this->processTable = Hashtable_new(20, false);
102 this->prototype = Process_new(this);
103 this->usersTable = usersTable;
104
105 /* tree-view auxiliary buffers */
106 this->processes2 = TypedVector_new(PROCESS_CLASS, true, DEFAULT_SIZE);
107
108 FILE* status = fopen(PROCSTATFILE, "r");
109 assert(status != NULL);
110 char buffer[256];
111 int procs = -1;
112 do {
113 procs++;
114 fgets(buffer, 255, status);
115 } while (String_startsWith(buffer, "cpu"));
116 fclose(status);
117 this->processorCount = procs - 1;
118 this->totalTime = calloc(procs, sizeof(long int));
119 this->userTime = calloc(procs, sizeof(long int));
120 this->systemTime = calloc(procs, sizeof(long int));
121 this->niceTime = calloc(procs, sizeof(long int));
122 this->idleTime = calloc(procs, sizeof(long int));
123 this->totalPeriod = calloc(procs, sizeof(long int));
124 this->userPeriod = calloc(procs, sizeof(long int));
125 this->systemPeriod = calloc(procs, sizeof(long int));
126 this->nicePeriod = calloc(procs, sizeof(long int));
127 this->idlePeriod = calloc(procs, sizeof(long int));
128 for (int i = 0; i < procs; i++) {
129 this->totalTime[i] = 1;
130 this->totalPeriod[i] = 1;
131 }
132
133 this->fields = calloc(sizeof(ProcessField), LAST_PROCESSFIELD+1);
134 // TODO: turn 'fields' into a TypedVector,
135 // (and ProcessFields into proper objects).
136 for (int i = 0; defaultHeaders[i]; i++) {
137 this->fields[i] = defaultHeaders[i];
138 }
139 this->sortKey = PERCENT_CPU;
140 this->direction = 1;
141 this->hideThreads = false;
142 this->shadowOtherUsers = false;
143 this->hideKernelThreads = false;
144 this->hideUserlandThreads = false;
145 this->treeView = false;
146 this->highlightBaseName = false;
147 this->highlightMegabytes = false;
148
149 return this;
150}
151
152void ProcessList_delete(ProcessList* this) {
153 Hashtable_delete(this->processTable);
154 TypedVector_delete(this->processes);
155 TypedVector_delete(this->processes2);
156 Process_delete((Object*)this->prototype);
157
158 free(this->totalTime);
159 free(this->userTime);
160 free(this->systemTime);
161 free(this->niceTime);
162 free(this->idleTime);
163 free(this->totalPeriod);
164 free(this->userPeriod);
165 free(this->systemPeriod);
166 free(this->nicePeriod);
167 free(this->idlePeriod);
168
169 free(this->fields);
170 free(this);
171}
172
173void ProcessList_invertSortOrder(ProcessList* this) {
174 if (this->direction == 1)
175 this->direction = -1;
176 else
177 this->direction = 1;
178}
179
180RichString ProcessList_printHeader(ProcessList* this) {
181 RichString out = RichString_new();
182 ProcessField* fields = this->fields;
183 for (int i = 0; fields[i]; i++) {
184 char* field = Process_printField(fields[i]);
185 if (this->sortKey == fields[i])
186 RichString_append(&out, CRT_colors[PANEL_HIGHLIGHT_FOCUS], field);
187 else
188 RichString_append(&out, CRT_colors[PANEL_HEADER_FOCUS], field);
189 }
190 return out;
191}
192
193
194void ProcessList_prune(ProcessList* this) {
195 TypedVector_prune(this->processes);
196}
197
198void ProcessList_add(ProcessList* this, Process* p) {
199 TypedVector_add(this->processes, p);
200 Hashtable_put(this->processTable, p->pid, p);
201}
202
203void ProcessList_remove(ProcessList* this, Process* p) {
204 Hashtable_remove(this->processTable, p->pid);
205 ProcessField pf = this->sortKey;
206 this->sortKey = PID;
207 int index = TypedVector_indexOf(this->processes, p);
208 TypedVector_remove(this->processes, index);
209 this->sortKey = pf;
210}
211
212Process* ProcessList_get(ProcessList* this, int index) {
213 return (Process*) (TypedVector_get(this->processes, index));
214}
215
216int ProcessList_size(ProcessList* this) {
217 return (TypedVector_size(this->processes));
218}
219
220/* private */
221void ProcessList_buildTree(ProcessList* this, int pid, int level, int indent, int direction) {
222 TypedVector* children = TypedVector_new(PROCESS_CLASS, false, DEFAULT_SIZE);
223
224 for (int i = 0; i < TypedVector_size(this->processes); i++) {
225 Process* process = (Process*) (TypedVector_get(this->processes, i));
226 if (process->ppid == pid) {
227 Process* process = (Process*) (TypedVector_take(this->processes, i));
228 TypedVector_add(children, process);
229 i--;
230 }
231 }
232 int size = TypedVector_size(children);
233 for (int i = 0; i < size; i++) {
234 Process* process = (Process*) (TypedVector_get(children, i));
235 if (direction == 1)
236 TypedVector_add(this->processes2, process);
237 else
238 TypedVector_insert(this->processes2, 0, process);
239 int nextIndent = indent;
240 if (i < size - 1)
241 nextIndent = indent | (1 << level);
242 ProcessList_buildTree(this, process->pid, level+1, nextIndent, direction);
243 process->indent = indent | (1 << level);
244 }
245 TypedVector_delete(children);
246}
247
248void ProcessList_sort(ProcessList* this) {
249 if (!this->treeView) {
250 TypedVector_sort(this->processes);
251 } else {
252 int direction = this->direction;
253 int sortKey = this->sortKey;
254 this->sortKey = PID;
255 this->direction = 1;
256 TypedVector_sort(this->processes);
257 this->sortKey = sortKey;
258 this->direction = direction;
259 Process* init = (Process*) (TypedVector_take(this->processes, 0));
260 assert(init->pid == 1);
261 init->indent = 0;
262 TypedVector_add(this->processes2, init);
263 ProcessList_buildTree(this, init->pid, 0, 0, direction);
264 TypedVector* t = this->processes;
265 this->processes = this->processes2;
266 this->processes2 = t;
267 }
268}
269
270/* private */
271int ProcessList_readStatFile(Process *proc, FILE *f, char *command) {
272 #define MAX_READ 8192
273 static char buf[MAX_READ];
274 long int zero;
275
276 int size = fread(buf, 1, MAX_READ, f);
277 if(!size) return 0;
278
279 proc->pid = atoi(buf);
280 char *location = strchr(buf, ' ');
281 if(!location) return 0;
282
283 location += 2;
284 char *end = strrchr(location, ')');
285 if(!end) return 0;
286
287 int commsize = end - location;
288 memcpy(command, location, commsize);
289 command[commsize] = '\0';
290 location = end + 2;
291
292 int num = sscanf(location,
293 "%c %d %d %d %d %d %lu %lu %lu %lu "
294 "%lu %lu %lu %ld %ld %ld %ld %ld %ld "
295 "%lu %lu %ld %lu %lu %lu %lu %lu "
296 "%lu %lu %lu %lu %lu %lu %lu %lu "
297 "%d %d",
298 &proc->state, &proc->ppid, &proc->pgrp, &proc->session, &proc->tty_nr,
299 &proc->tpgid, &proc->flags, &proc->minflt, &proc->cminflt, &proc->majflt,
300 &proc->cmajflt, &proc->utime, &proc->stime, &proc->cutime, &proc->cstime,
301 &proc->priority, &proc->nice, &zero, &proc->itrealvalue,
302 &proc->starttime, &proc->vsize, &proc->rss, &proc->rlim,
303 &proc->startcode, &proc->endcode, &proc->startstack, &proc->kstkesp,
304 &proc->kstkeip, &proc->signal, &proc->blocked, &proc->sigignore,
305 &proc->sigcatch, &proc->wchan, &proc->nswap, &proc->cnswap,
306 &proc->exit_signal, &proc->processor);
307
308 // This assert is always valid on 2.4, but reportedly not always valid on 2.6.
309 // TODO: Check if the semantics of this field has changed.
310 // assert(zero == 0);
311
312 if(num != 37) return 0;
313 return 1;
314}
315
316bool ProcessList_readStatusFile(Process* proc, char* dirname, char* name) {
317 char statusfilename[MAX_NAME+1];
318 statusfilename[MAX_NAME] = '\0';
319 snprintf(statusfilename, MAX_NAME, "%s/%s/status", dirname, name);
320 FILE* status = fopen(statusfilename, "r");
321 bool success = false;
322 if (status) {
323 char buffer[1024];
324 buffer[1023] = '\0';
325 while (!feof(status)) {
326 char* ok = fgets(buffer, 1023, status);
327 if (!ok)
328 break;
329 if (String_startsWith(buffer, "Uid:")) {
330 int uid1, uid2, uid3, uid4;
331 // TODO: handle other uid's.
332 int ok = sscanf(buffer, "Uid:\t%d\t%d\t%d\t%d\n", &uid1, &uid2, &uid3, &uid4);
333 if (ok >= 1) {
334 proc->st_uid = uid1;
335 success = true;
336 }
337 break;
338 }
339 }
340 fclose(status);
341 }
342 if (!success) {
343 snprintf(statusfilename, MAX_NAME, "%s/%s/stat", dirname, name);
344 struct stat sstat;
345 int statok = stat(statusfilename, &sstat);
346 if (statok == -1)
347 return false;
348 proc->st_uid = sstat.st_uid;
349 }
350 return success;
351}
352
353void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, float period) {
354 DIR* dir;
355 struct dirent* entry;
356 Process* prototype = this->prototype;
357
358 dir = opendir(dirname);
359 assert(dir != NULL);
360 while ((entry = readdir(dir)) != NULL) {
361 char* name = entry->d_name;
362 int pid;
363 // filename is a number: process directory
364 pid = atoi(name);
365
366 // The RedHat kernel hides threads with a dot.
367 // I believe this is non-standard.
368 bool isThread = false;
369 if ((!this->hideThreads) && pid == 0 && name[0] == '.') {
370 char* tname = name + 1;
371 pid = atoi(tname);
372 if (pid > 0)
373 isThread = true;
374 }
375
376 if (pid > 0 && pid != parent) {
377 if (!this->hideUserlandThreads) {
378 char subdirname[MAX_NAME+1];
379 snprintf(subdirname, MAX_NAME, "%s/%s/task", dirname, name);
380
381 if (access(subdirname, X_OK) == 0) {
382 ProcessList_processEntries(this, subdirname, pid, period);
383 }
384 }
385
386 FILE* status;
387 char statusfilename[MAX_NAME+1];
388 char command[PROCESS_COMM_LEN + 1];
389
390 Process* process;
391 Process* existingProcess = (Process*) Hashtable_get(this->processTable, pid);
392 if (!existingProcess) {
393 process = Process_clone(prototype);
394 process->pid = pid;
395 ProcessList_add(this, process);
396 if (! ProcessList_readStatusFile(process, dirname, name))
397 goto errorReadingProcess;
398 } else {
399 process = existingProcess;
400 }
401 process->updated = true;
402
403 char* username = UsersTable_getRef(this->usersTable, process->st_uid);
404 if (username) {
405 strncpy(process->user, username, PROCESS_USER_LEN);
406 } else {
407 snprintf(process->user, PROCESS_USER_LEN, "%d", process->st_uid);
408 }
409
410 int lasttimes = (process->utime + process->stime);
411
412 snprintf(statusfilename, MAX_NAME, "%s/%s/stat", dirname, name);
413 status = fopen(statusfilename, "r");
414 if (status == NULL)
415 goto errorReadingProcess;
416
417 int success = ProcessList_readStatFile(process, status, command);
418 fclose(status);
419 if(!success) {
420 goto errorReadingProcess;
421 }
422
423 process->percent_cpu = (process->utime + process->stime - lasttimes) /
424 period * 100.0;
425
426 if(!existingProcess) {
427 snprintf(statusfilename, MAX_NAME, "%s/%s/cmdline", dirname, name);
428 status = fopen(statusfilename, "r");
429 if (!status) {
430 goto errorReadingProcess;
431 }
432
433 int amtRead = fread(command, 1, PROCESS_COMM_LEN - 1, status);
434 if (amtRead > 0) {
435 for (int i = 0; i < amtRead; i++)
436 if (command[i] == '\0' || command[i] == '\n')
437 command[i] = ' ';
438 command[amtRead] = '\0';
439 }
440 command[PROCESS_COMM_LEN] = '\0';
441 process->comm = String_copy(command);
442 fclose(status);
443 }
444
445 snprintf(statusfilename, MAX_NAME, "%s/%s/statm", dirname, name);
446 status = fopen(statusfilename, "r");
447 if(!status) {
448 goto errorReadingProcess;
449 }
450 int num = fscanf(status, "%d %d %d %d %d %d %d",
451 &process->m_size, &process->m_resident, &process->m_share,
452 &process->m_trs, &process->m_drs, &process->m_lrs,
453 &process->m_dt);
454 fclose(status);
455 if(num != 7)
456 goto errorReadingProcess;
457
458 process->percent_mem = process->m_resident /
459 (float)(this->usedMem - this->cachedMem - this->buffersMem) *
460 100.0;
461
462 this->totalTasks++;
463 if (process->state == 'R') {
464 this->runningTasks++;
465 }
466
467 if (this->hideKernelThreads && process->m_size == 0)
468 ProcessList_remove(this, process);
469
470 continue;
471
472 // Exception handler.
473 errorReadingProcess: {
474 ProcessList_remove(this, process);
475 }
476 }
477 }
478 closedir(dir);
479}
480
481void ProcessList_scan(ProcessList* this) {
482 long int usertime, nicetime, systemtime, idletime, totaltime;
483 long int swapFree;
484
485 FILE* status;
486 char buffer[128];
487 status = fopen(PROCMEMINFOFILE, "r");
488 assert(status != NULL);
489 while (!feof(status)) {
490 fgets(buffer, 128, status);
491
492 switch (buffer[0]) {
493 case 'M':
494 if (String_startsWith(buffer, "MemTotal:"))
495 sscanf(buffer, "MemTotal: %ld kB", &this->totalMem);
496 else if (String_startsWith(buffer, "MemFree:"))
497 sscanf(buffer, "MemFree: %ld kB", &this->freeMem);
498 else if (String_startsWith(buffer, "MemShared:"))
499 sscanf(buffer, "MemShared: %ld kB", &this->sharedMem);
500 break;
501 case 'B':
502 if (String_startsWith(buffer, "Buffers:"))
503 sscanf(buffer, "Buffers: %ld kB", &this->buffersMem);
504 break;
505 case 'C':
506 if (String_startsWith(buffer, "Cached:"))
507 sscanf(buffer, "Cached: %ld kB", &this->cachedMem);
508 break;
509 case 'S':
510 if (String_startsWith(buffer, "SwapTotal:"))
511 sscanf(buffer, "SwapTotal: %ld kB", &this->totalSwap);
512 if (String_startsWith(buffer, "SwapFree:"))
513 sscanf(buffer, "SwapFree: %ld kB", &swapFree);
514 break;
515 }
516 }
517 this->usedMem = this->totalMem - this->freeMem;
518 this->usedSwap = this->totalSwap - swapFree;
519 fclose(status);
520
521 status = fopen(PROCSTATFILE, "r");
522 assert(status != NULL);
523 for (int i = 0; i <= this->processorCount; i++) {
524 char buffer[256];
525 int cpuid;
526 long int ioWait, irq, softIrq, steal;
527 ioWait = irq = softIrq = steal = 0;
528 // Dependending on your kernel version,
529 // 5, 7 or 8 of these fields will be set.
530 // The rest will remain at zero.
531 fgets(buffer, 255, status);
532 if (i == 0)
533 sscanf(buffer, "cpu %ld %ld %ld %ld %ld %ld %ld %ld\n", &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal);
534 else {
535 sscanf(buffer, "cpu%d %ld %ld %ld %ld %ld %ld %ld %ld\n", &cpuid, &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal);
536 assert(cpuid == i - 1);
537 }
538 // Fields existing on kernels >= 2.6
539 // (and RHEL's patched kernel 2.4...)
540 systemtime += ioWait + irq + softIrq + steal;
541 totaltime = usertime + nicetime + systemtime + idletime;
542 assert (usertime >= this->userTime[i]);
543 assert (nicetime >= this->niceTime[i]);
544 assert (systemtime >= this->systemTime[i]);
545 assert (idletime >= this->idleTime[i]);
546 assert (totaltime >= this->totalTime[i]);
547 this->userPeriod[i] = usertime - this->userTime[i];
548 this->nicePeriod[i] = nicetime - this->niceTime[i];
549 this->systemPeriod[i] = systemtime - this->systemTime[i];
550 this->idlePeriod[i] = idletime - this->idleTime[i];
551 this->totalPeriod[i] = totaltime - this->totalTime[i];
552 this->userTime[i] = usertime;
553 this->niceTime[i] = nicetime;
554 this->systemTime[i] = systemtime;
555 this->idleTime[i] = idletime;
556 this->totalTime[i] = totaltime;
557 }
558 float period = (float)this->totalPeriod[0] / this->processorCount;
559 fclose(status);
560
561 // mark all process as "dirty"
562 for (int i = 0; i < TypedVector_size(this->processes); i++) {
563 Process* p = (Process*) TypedVector_get(this->processes, i);
564 p->updated = false;
565 }
566
567 this->totalTasks = 0;
568 this->runningTasks = 0;
569
570 signal(11, ProcessList_dontCrash);
571
572 ProcessList_processEntries(this, PROCDIR, 0, period);
573 signal(11, SIG_DFL);
574
575 for (int i = TypedVector_size(this->processes) - 1; i >= 0; i--) {
576 Process* p = (Process*) TypedVector_get(this->processes, i);
577 if (p->updated == false)
578 ProcessList_remove(this, p);
579 else
580 p->updated = false;
581 }
582
583}
584
585void ProcessList_dontCrash(int signal) {
586 // This ugly hack was added because I suspect some
587 // crashes were caused by contents of /proc vanishing
588 // away while we read them.
589}