blob: ab38620a69afe03ce638ac759fd14a08d0000576 [file] [log] [blame]
Hisham Muhammadd6231ba2006-03-04 18:16:49 +00001/*
2htop - Process.c
Hisham Muhammad130938f2006-03-23 18:55:29 +00003(C) 2004-2006 Hisham H. Muhammad
Hisham Muhammadd6231ba2006-03-04 18:16:49 +00004Released under the GNU GPL, see the COPYING file
5in the source distribution for its full text.
6*/
7
8#define _GNU_SOURCE
9#include "ProcessList.h"
10#include "Object.h"
11#include "CRT.h"
12#include "String.h"
13#include "Process.h"
14
15#include "debug.h"
16
17#include <stdio.h>
18#include <sys/time.h>
19#include <sys/resource.h>
20#include <sys/param.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <unistd.h>
24#include <signal.h>
25#include <string.h>
26#include <stdbool.h>
27#include <pwd.h>
28
29// This works only with glibc 2.1+. On earlier versions
30// the behavior is similar to have a hardcoded page size.
31#define PAGE_SIZE ( sysconf(_SC_PAGESIZE) / 1024 )
32
33#define PROCESS_COMM_LEN 300
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000034
35/*{
36
37typedef enum ProcessField_ {
38 PID = 1, COMM, STATE, PPID, PGRP, SESSION, TTY_NR, TPGID, FLAGS, MINFLT, CMINFLT, MAJFLT, CMAJFLT, UTIME,
39 STIME, CUTIME, CSTIME, PRIORITY, NICE, ITREALVALUE, STARTTIME, VSIZE, RSS, RLIM, STARTCODE, ENDCODE,
40 STARTSTACK, KSTKESP, KSTKEIP, SIGNAL, BLOCKED, SSIGIGNORE, SIGCATCH, WCHAN, NSWAP, CNSWAP, EXIT_SIGNAL,
41 PROCESSOR, M_SIZE, M_RESIDENT, M_SHARE, M_TRS, M_DRS, M_LRS, M_DT, ST_UID, PERCENT_CPU, PERCENT_MEM,
42 USER, TIME, LAST_PROCESSFIELD
43} ProcessField;
44
45struct ProcessList_;
46
47typedef struct Process_ {
48 Object super;
49
50 struct ProcessList_ *pl;
51 bool updated;
52
53 int pid;
54 char* comm;
55 int indent;
56 char state;
57 bool tag;
58 int ppid;
59 int pgrp;
60 int session;
61 int tty_nr;
62 int tpgid;
63 unsigned long int flags;
Hisham Muhammad5d48ab82006-07-11 06:13:32 +000064 #ifdef DEBUG
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000065 unsigned long int minflt;
66 unsigned long int cminflt;
67 unsigned long int majflt;
68 unsigned long int cmajflt;
Hisham Muhammad5d48ab82006-07-11 06:13:32 +000069 #endif
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000070 unsigned long int utime;
71 unsigned long int stime;
72 long int cutime;
73 long int cstime;
74 long int priority;
75 long int nice;
Hisham Muhammad5d48ab82006-07-11 06:13:32 +000076 #ifdef DEBUG
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000077 long int itrealvalue;
78 unsigned long int starttime;
79 unsigned long int vsize;
80 long int rss;
81 unsigned long int rlim;
82 unsigned long int startcode;
83 unsigned long int endcode;
84 unsigned long int startstack;
85 unsigned long int kstkesp;
86 unsigned long int kstkeip;
87 unsigned long int signal;
88 unsigned long int blocked;
89 unsigned long int sigignore;
90 unsigned long int sigcatch;
91 unsigned long int wchan;
92 unsigned long int nswap;
93 unsigned long int cnswap;
Hisham Muhammad5d48ab82006-07-11 06:13:32 +000094 #endif
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000095 int exit_signal;
96 int processor;
97 int m_size;
98 int m_resident;
99 int m_share;
100 int m_trs;
101 int m_drs;
102 int m_lrs;
103 int m_dt;
104 uid_t st_uid;
105 float percent_cpu;
106 float percent_mem;
Hisham Muhammadeb2803c2006-07-12 01:35:59 +0000107 char* user;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000108} Process;
109
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000110}*/
111
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000112#ifdef DEBUG
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000113char* PROCESS_CLASS = "Process";
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000114#else
115#define PROCESS_CLASS NULL
116#endif
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000117
Hisham Muhammad2f1f82e2006-06-06 20:41:01 +0000118char *Process_fieldNames[] = {
119 "", "PID", "Command", "STATE", "PPID", "PGRP", "SESSION", "TTY_NR", "TPGID", "FLAGS", "MINFLT", "CMINFLT", "MAJFLT", "CMAJFLT", "UTIME", "STIME", "CUTIME", "CSTIME", "PRIORITY", "NICE", "ITREALVALUE", "STARTTIME", "VSIZE", "RSS", "RLIM", "STARTCODE", "ENDCODE", "STARTSTACK", "KSTKESP", "KSTKEIP", "SIGNAL", "BLOCKED", "SIGIGNORE", "SIGCATCH", "WCHAN", "NSWAP", "CNSWAP", "EXIT_SIGNAL", "PROCESSOR", "M_SIZE", "M_RESIDENT", "M_SHARE", "M_TRS", "M_DRS", "M_LRS", "M_DT", "ST_UID", "PERCENT_CPU", "PERCENT_MEM", "USER", "TIME", "*** report bug! ***"
120};
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000121
Hisham Muhammadeb2803c2006-07-12 01:35:59 +0000122static int Process_getuid = -1;
123
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000124Process* Process_new(struct ProcessList_ *pl) {
125 Process* this = malloc(sizeof(Process));
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000126 Object_setClass(this, PROCESS_CLASS);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000127 ((Object*)this)->display = Process_display;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000128 ((Object*)this)->delete = Process_delete;
Hisham Muhammada8f45d52006-11-13 22:00:16 +0000129 this->pid = 0;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000130 this->pl = pl;
131 this->tag = false;
132 this->updated = false;
133 this->utime = 0;
134 this->stime = 0;
135 this->comm = NULL;
Hisham Muhammad46b35b22006-11-08 20:09:12 +0000136 this->indent = 0;
Hisham Muhammadeb2803c2006-07-12 01:35:59 +0000137 if (Process_getuid == -1) Process_getuid = getuid();
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000138 return this;
139}
140
141Process* Process_clone(Process* this) {
142 Process* clone = malloc(sizeof(Process));
143 memcpy(clone, this, sizeof(Process));
Hisham Muhammad97ea7a12006-11-08 22:16:46 +0000144 this->comm = NULL;
Hisham Muhammada8f45d52006-11-13 22:00:16 +0000145 this->pid = 0;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000146 return clone;
147}
148
149void Process_delete(Object* cast) {
150 Process* this = (Process*) cast;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000151 assert (this != NULL);
Hisham Muhammad6b6b4372006-11-20 16:42:03 +0000152 if (this->comm) free(this->comm);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000153 free(this);
154}
155
156void Process_display(Object* cast, RichString* out) {
157 Process* this = (Process*) cast;
158 ProcessField* fields = this->pl->fields;
Hisham Muhammaddbe26702006-07-12 01:16:03 +0000159 RichString_init(out);
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000160 for (int i = 0; fields[i]; i++)
161 Process_writeField(this, out, fields[i]);
Hisham Muhammadeb2803c2006-07-12 01:35:59 +0000162 if (this->pl->shadowOtherUsers && this->st_uid != Process_getuid)
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000163 RichString_setAttr(out, CRT_colors[PROCESS_SHADOW]);
164 if (this->tag == true)
165 RichString_setAttr(out, CRT_colors[PROCESS_TAG]);
166 assert(out->len > 0);
167}
168
169void Process_toggleTag(Process* this) {
170 this->tag = this->tag == true ? false : true;
171}
172
173void Process_setPriority(Process* this, int priority) {
174 int old_prio = getpriority(PRIO_PROCESS, this->pid);
175 int err = setpriority(PRIO_PROCESS, this->pid, priority);
176 if (err == 0 && old_prio != getpriority(PRIO_PROCESS, this->pid)) {
177 this->nice = priority;
178 }
179}
180
181void Process_sendSignal(Process* this, int signal) {
182 kill(this->pid, signal);
183}
184
185#define ONE_K 1024
186#define ONE_M (ONE_K * ONE_K)
187#define ONE_G (ONE_M * ONE_K)
188
Hisham Muhammad6b6b4372006-11-20 16:42:03 +0000189static void Process_printLargeNumber(Process* this, RichString *str, unsigned long number) {
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000190 char buffer[11];
191 int len;
192 if(number >= (1000 * ONE_M)) {
193 len = snprintf(buffer, 10, "%4.2fG ", (float)number / ONE_M);
194 RichString_appendn(str, CRT_colors[LARGE_NUMBER], buffer, len);
195 } else if(number >= (100000)) {
196 len = snprintf(buffer, 10, "%4dM ", number / ONE_K);
197 int attr = this->pl->highlightMegabytes
198 ? CRT_colors[PROCESS_MEGABYTES]
199 : CRT_colors[PROCESS];
200 RichString_appendn(str, attr, buffer, len);
201 } else if (this->pl->highlightMegabytes && number >= 1000) {
202 len = snprintf(buffer, 10, "%2d", number/1000);
203 RichString_appendn(str, CRT_colors[PROCESS_MEGABYTES], buffer, len);
204 number %= 1000;
205 len = snprintf(buffer, 10, "%03d ", number);
206 RichString_appendn(str, CRT_colors[PROCESS], buffer, len);
207 } else {
208 len = snprintf(buffer, 10, "%5d ", number);
209 RichString_appendn(str, CRT_colors[PROCESS], buffer, len);
210 }
211}
212
Hisham Muhammad2f1f82e2006-06-06 20:41:01 +0000213static double jiffy = 0.0;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000214
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000215static void Process_printTime(RichString* str, unsigned long t) {
216 if(jiffy == 0.0) jiffy = sysconf(_SC_CLK_TCK);
217 double jiffytime = 1.0 / jiffy;
218
219 double realTime = t * jiffytime;
220 int iRealTime = (int) realTime;
221
222 int hours = iRealTime / 3600;
223 int minutes = (iRealTime / 60) % 60;
224 int seconds = iRealTime % 60;
225 int hundredths = (realTime - iRealTime) * 100;
226 char buffer[11];
227 if (hours) {
228 snprintf(buffer, 10, "%2dh", hours);
229 RichString_append(str, CRT_colors[LARGE_NUMBER], buffer);
230 snprintf(buffer, 10, "%02d:%02d ", minutes, seconds);
231 } else {
232 snprintf(buffer, 10, "%2d:%02d.%02d ", minutes, seconds, hundredths);
233 }
234 RichString_append(str, CRT_colors[DEFAULT_COLOR], buffer);
235}
236
Hisham Muhammad2f1f82e2006-06-06 20:41:01 +0000237static inline void Process_writeCommand(Process* this, int attr, RichString* str) {
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000238 if (this->pl->highlightBaseName) {
239 char* firstSpace = strchr(this->comm, ' ');
240 if (firstSpace) {
241 char* slash = firstSpace;
242 while (slash > this->comm && *slash != '/')
243 slash--;
244 if (slash > this->comm) {
245 slash++;
246 RichString_appendn(str, attr, this->comm, slash - this->comm);
247 }
248 RichString_appendn(str, CRT_colors[PROCESS_BASENAME], slash, firstSpace - slash);
249 RichString_append(str, attr, firstSpace);
250 } else {
251 RichString_append(str, CRT_colors[PROCESS_BASENAME], this->comm);
252 }
253 } else {
254 RichString_append(str, attr, this->comm);
255 }
256}
257
258void Process_writeField(Process* this, RichString* str, ProcessField field) {
259 char buffer[PROCESS_COMM_LEN];
260 int attr = CRT_colors[DEFAULT_COLOR];
261 int n = PROCESS_COMM_LEN;
262
263 switch (field) {
264 case PID: snprintf(buffer, n, "%5d ", this->pid); break;
265 case PPID: snprintf(buffer, n, "%5d ", this->ppid); break;
266 case PGRP: snprintf(buffer, n, "%5d ", this->pgrp); break;
267 case SESSION: snprintf(buffer, n, "%5d ", this->session); break;
268 case TTY_NR: snprintf(buffer, n, "%5d ", this->tty_nr); break;
269 case TPGID: snprintf(buffer, n, "%5d ", this->tpgid); break;
270 case PROCESSOR: snprintf(buffer, n, "%3d ", this->processor+1); break;
271 case COMM: {
272 if (!this->pl->treeView || this->indent == 0) {
273 Process_writeCommand(this, attr, str);
274 return;
275 } else {
276 char* buf = buffer;
277 int maxIndent = 0;
278 for (int i = 0; i < 32; i++)
279 if (this->indent & (1 << i))
280 maxIndent = i+1;
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000281 for (int i = 0; i < maxIndent - 1; i++) {
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000282 if (this->indent & (1 << i))
283 snprintf(buf, n, " | ");
284 else
285 snprintf(buf, n, " ");
286 buf += 4;
287 n -= 4;
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000288 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000289 if (this->pl->direction == 1)
290 snprintf(buf, n, " `- ");
291 else
292 snprintf(buf, n, " ,- ");
293 RichString_append(str, CRT_colors[PROCESS_TREE], buffer);
294 Process_writeCommand(this, attr, str);
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000295 return;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000296 }
297 }
298 case STATE: {
299 snprintf(buffer, n, "%c ", this->state);
300 attr = this->state == 'R'
301 ? CRT_colors[PROCESS_R_STATE]
302 : attr;
303 break;
304 }
305 case PRIORITY: {
306 if(this->priority == -100)
307 snprintf(buffer, n, " RT ");
308 else
309 snprintf(buffer, n, "%3ld ", this->priority);
310 break;
311 }
312 case NICE: {
313 snprintf(buffer, n, "%3ld ", this->nice);
314 attr = this->nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY]
315 : this->nice > 0 ? CRT_colors[PROCESS_LOW_PRIORITY]
316 : attr;
317 break;
318 }
319 case M_SIZE: Process_printLargeNumber(this, str, this->m_size * PAGE_SIZE); return;
320 case M_RESIDENT: Process_printLargeNumber(this, str, this->m_resident * PAGE_SIZE); return;
321 case M_SHARE: Process_printLargeNumber(this, str, this->m_share * PAGE_SIZE); return;
322 case ST_UID: snprintf(buffer, n, "%4d ", this->st_uid); break;
323 case USER: {
Hisham Muhammadeb2803c2006-07-12 01:35:59 +0000324 if (Process_getuid != this->st_uid)
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000325 attr = CRT_colors[PROCESS_SHADOW];
Hisham Muhammadeb2803c2006-07-12 01:35:59 +0000326 if (this->user) {
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000327 snprintf(buffer, n, "%-8s ", this->user);
Hisham Muhammadeb2803c2006-07-12 01:35:59 +0000328 } else {
329 snprintf(buffer, n, "%-8d ", this->st_uid);
330 }
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000331 if (buffer[8] != '\0') {
332 buffer[8] = ' ';
333 buffer[9] = '\0';
334 }
335 break;
336 }
337 case UTIME: Process_printTime(str, this->utime); return;
338 case STIME: Process_printTime(str, this->stime); return;
339 case CUTIME: Process_printTime(str, this->cutime); return;
340 case CSTIME: Process_printTime(str, this->cstime); return;
341 case TIME: Process_printTime(str, this->utime + this->stime); return;
342 case PERCENT_CPU: {
343 if (this->percent_cpu > 99.9) {
344 snprintf(buffer, n, "100. ");
345 } else {
346 snprintf(buffer, n, "%4.1f ", this->percent_cpu);
347 }
348 break;
349 }
350 case PERCENT_MEM: {
351 if (this->percent_mem > 99.9) {
352 snprintf(buffer, n, "100. ");
353 } else {
354 snprintf(buffer, n, "%4.1f ", this->percent_mem);
355 }
356 break;
357 }
358 default:
359 snprintf(buffer, n, "- ");
360 }
361 RichString_append(str, attr, buffer);
362 return;
363}
364
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000365int Process_pidCompare(const void* v1, const void* v2) {
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000366 Process* p1 = (Process*)v1;
367 Process* p2 = (Process*)v2;
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000368 return (p1->pid - p2->pid);
369}
370
371int Process_compare(const void* v1, const void* v2) {
372 Process *p1, *p2;
373 ProcessList *pl = ((Process*)v1)->pl;
374 if (pl->direction == 1) {
375 p1 = (Process*)v1;
376 p2 = (Process*)v2;
377 } else {
378 p2 = (Process*)v1;
379 p1 = (Process*)v2;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000380 }
Hisham Muhammad5d48ab82006-07-11 06:13:32 +0000381 switch (pl->sortKey) {
382 case PID:
383 return (p1->pid - p2->pid);
384 case PPID:
385 return (p1->ppid - p2->ppid);
386 case USER:
387 return strcmp(p1->user, p2->user);
388 case PRIORITY:
389 return (p1->priority - p2->priority);
390 case STATE:
391 return (p1->state - p2->state);
392 case NICE:
393 return (p1->nice - p2->nice);
394 case M_SIZE:
395 return (p2->m_size - p1->m_size);
396 case M_RESIDENT:
397 return (p2->m_resident - p1->m_resident);
398 case M_SHARE:
399 return (p2->m_share - p1->m_share);
400 case PERCENT_CPU:
401 return (p2->percent_cpu > p1->percent_cpu ? 1 : -1);
402 case PERCENT_MEM:
403 return (p2->m_resident - p1->m_resident);
404 case UTIME:
405 return (p2->utime - p1->utime);
406 case STIME:
407 return (p2->stime - p1->stime);
408 case TIME:
409 return ((p2->utime+p2->stime) - (p1->utime+p1->stime));
410 case COMM:
411 return strcmp(p1->comm, p2->comm);
412 default:
413 return (p1->pid - p2->pid);
414 }
415
Hisham Muhammadd6231ba2006-03-04 18:16:49 +0000416}
417
418char* Process_printField(ProcessField field) {
419 switch (field) {
420 case PID: return " PID ";
421 case PPID: return " PPID ";
422 case PGRP: return " PGRP ";
423 case SESSION: return " SESN ";
424 case TTY_NR: return " TTY ";
425 case TPGID: return " TGID ";
426 case COMM: return "Command ";
427 case STATE: return "S ";
428 case PRIORITY: return "PRI ";
429 case NICE: return " NI ";
430 case M_SIZE: return " VIRT ";
431 case M_RESIDENT: return " RES ";
432 case M_SHARE: return " SHR ";
433 case ST_UID: return " UID ";
434 case USER: return "USER ";
435 case UTIME: return " UTIME+ ";
436 case STIME: return " STIME+ ";
437 case TIME: return " TIME+ ";
438 case PERCENT_CPU: return "CPU% ";
439 case PERCENT_MEM: return "MEM% ";
440 case PROCESSOR: return "CPU ";
441 default: return "- ";
442 }
443}