Skip to content
Newer
Older
100644 606 lines (565 sloc) 18.9 KB
43ccb9c Initial import.
hisham authored Mar 4, 2006
1 /*
2 htop - Process.c
47e9f1d Fixes for header generation.
loderunner authored Mar 23, 2006
3 (C) 2004-2006 Hisham H. Muhammad
43ccb9c Initial import.
hisham authored Mar 4, 2006
4 Released under the GNU GPL, see the COPYING file
5 in 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"
ef72a25 Add Unicode support, enabled with the --enable-unicode
loderunner authored Mar 9, 2008
14 #include "RichString.h"
43ccb9c Initial import.
hisham authored Mar 4, 2006
15
16 #include "debug.h"
17
18 #include <stdio.h>
19 #include <sys/time.h>
20 #include <sys/resource.h>
21 #include <sys/param.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <stdbool.h>
28 #include <pwd.h>
d5fe357 Improve construction of tree view, properly nesting threads.
loderunner authored Nov 8, 2007
29 #include <sched.h>
43ccb9c Initial import.
hisham authored Mar 4, 2006
30
1e492e4 BSD related fixes:
loderunner authored Mar 11, 2009
31 #ifdef HAVE_PLPA
a1633e5 Embed PLPA (Portable Linux Processor Affinity) in order to support
loderunner authored Mar 5, 2008
32 #include <plpa.h>
1e492e4 BSD related fixes:
loderunner authored Mar 11, 2009
33 #endif
a1633e5 Embed PLPA (Portable Linux Processor Affinity) in order to support
loderunner authored Mar 5, 2008
34
43ccb9c Initial import.
hisham authored Mar 4, 2006
35 // This works only with glibc 2.1+. On earlier versions
36 // the behavior is similar to have a hardcoded page size.
b7be63b Fixes for /proc location, submitted by Florent Thoumie
loderunner authored May 17, 2007
37 #ifndef PAGE_SIZE
1e492e4 BSD related fixes:
loderunner authored Mar 11, 2009
38 #define PAGE_SIZE ( sysconf(_SC_PAGESIZE) )
b7be63b Fixes for /proc location, submitted by Florent Thoumie
loderunner authored May 17, 2007
39 #endif
1e492e4 BSD related fixes:
loderunner authored Mar 11, 2009
40 #define PAGE_SIZE_KB ( PAGE_SIZE / ONE_K )
43ccb9c Initial import.
hisham authored Mar 4, 2006
41
42 #define PROCESS_COMM_LEN 300
43
44 /*{
45
46 typedef enum ProcessField_ {
47 PID = 1, COMM, STATE, PPID, PGRP, SESSION, TTY_NR, TPGID, FLAGS, MINFLT, CMINFLT, MAJFLT, CMAJFLT, UTIME,
48 STIME, CUTIME, CSTIME, PRIORITY, NICE, ITREALVALUE, STARTTIME, VSIZE, RSS, RLIM, STARTCODE, ENDCODE,
49 STARTSTACK, KSTKESP, KSTKEIP, SIGNAL, BLOCKED, SSIGIGNORE, SIGCATCH, WCHAN, NSWAP, CNSWAP, EXIT_SIGNAL,
50 PROCESSOR, M_SIZE, M_RESIDENT, M_SHARE, M_TRS, M_DRS, M_LRS, M_DT, ST_UID, PERCENT_CPU, PERCENT_MEM,
e010bde Experimental feature: beep on permission failures.
loderunner authored Dec 17, 2007
51 USER, TIME, NLWP, TGID,
bc6b07a OpenVZ support, contributed by Sergey Lychko
loderunner authored Aug 10, 2007
52 #ifdef HAVE_OPENVZ
479f6c5 Rename VEID to CTID in OpenVZ systems
loderunner authored Mar 11, 2009
53 CTID, VPID,
bc6b07a OpenVZ support, contributed by Sergey Lychko
loderunner authored Aug 10, 2007
54 #endif
6d189ef Add Linux-VServer support,
loderunner authored Sep 23, 2008
55 #ifdef HAVE_VSERVER
56 VXID,
57 #endif
83d4ae6 Add support for Linux per-process IO statistics,
loderunner authored Mar 9, 2008
58 #ifdef HAVE_TASKSTATS
010364f Ability to change sort column with the mouse by
loderunner authored Mar 14, 2008
59 RCHAR, WCHAR, SYSCR, SYSCW, RBYTES, WBYTES, CNCLWB, IO_READ_RATE, IO_WRITE_RATE, IO_RATE,
83d4ae6 Add support for Linux per-process IO statistics,
loderunner authored Mar 9, 2008
60 #endif
bc6b07a OpenVZ support, contributed by Sergey Lychko
loderunner authored Aug 10, 2007
61 LAST_PROCESSFIELD
43ccb9c Initial import.
hisham authored Mar 4, 2006
62 } ProcessField;
63
64 struct ProcessList_;
65
66 typedef struct Process_ {
67 Object super;
68
69 struct ProcessList_ *pl;
70 bool updated;
71
525e355 Switch to unsigned keys in hash, according to issue #1688290
loderunner authored Apr 5, 2007
72 unsigned int pid;
43ccb9c Initial import.
hisham authored Mar 4, 2006
73 char* comm;
74 int indent;
75 char state;
76 bool tag;
525e355 Switch to unsigned keys in hash, according to issue #1688290
loderunner authored Apr 5, 2007
77 unsigned int ppid;
78 unsigned int pgrp;
79 unsigned int session;
80 unsigned int tty_nr;
d5fe357 Improve construction of tree view, properly nesting threads.
loderunner authored Nov 8, 2007
81 unsigned int tgid;
82 int tpgid;
43ccb9c Initial import.
hisham authored Mar 4, 2006
83 unsigned long int flags;
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
84 #ifdef DEBUG
43ccb9c Initial import.
hisham authored Mar 4, 2006
85 unsigned long int minflt;
86 unsigned long int cminflt;
87 unsigned long int majflt;
88 unsigned long int cmajflt;
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
89 #endif
43ccb9c Initial import.
hisham authored Mar 4, 2006
90 unsigned long int utime;
91 unsigned long int stime;
92 long int cutime;
93 long int cstime;
94 long int priority;
95 long int nice;
f41ea30 Add support of NLWP field, by Bert Wesarg
loderunner authored May 21, 2007
96 long int nlwp;
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
97 #ifdef DEBUG
43ccb9c Initial import.
hisham authored Mar 4, 2006
98 long int itrealvalue;
99 unsigned long int starttime;
100 unsigned long int vsize;
101 long int rss;
102 unsigned long int rlim;
103 unsigned long int startcode;
104 unsigned long int endcode;
105 unsigned long int startstack;
106 unsigned long int kstkesp;
107 unsigned long int kstkeip;
108 unsigned long int signal;
109 unsigned long int blocked;
110 unsigned long int sigignore;
111 unsigned long int sigcatch;
112 unsigned long int wchan;
113 unsigned long int nswap;
114 unsigned long int cnswap;
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
115 #endif
43ccb9c Initial import.
hisham authored Mar 4, 2006
116 int exit_signal;
117 int processor;
118 int m_size;
119 int m_resident;
120 int m_share;
121 int m_trs;
122 int m_drs;
123 int m_lrs;
124 int m_dt;
125 uid_t st_uid;
126 float percent_cpu;
127 float percent_mem;
68c067e Share name strings from UsersList
loderunner authored Jul 12, 2006
128 char* user;
bc6b07a OpenVZ support, contributed by Sergey Lychko
loderunner authored Aug 10, 2007
129 #ifdef HAVE_OPENVZ
479f6c5 Rename VEID to CTID in OpenVZ systems
loderunner authored Mar 11, 2009
130 unsigned int ctid;
bc6b07a OpenVZ support, contributed by Sergey Lychko
loderunner authored Aug 10, 2007
131 unsigned int vpid;
132 #endif
6d189ef Add Linux-VServer support,
loderunner authored Sep 23, 2008
133 #ifdef HAVE_VSERVER
134 unsigned int vxid;
135 #endif
83d4ae6 Add support for Linux per-process IO statistics,
loderunner authored Mar 9, 2008
136 #ifdef HAVE_TASKSTATS
137 unsigned long long io_rchar;
138 unsigned long long io_wchar;
139 unsigned long long io_syscr;
140 unsigned long long io_syscw;
141 unsigned long long io_read_bytes;
142 unsigned long long io_write_bytes;
143 unsigned long long io_cancelled_write_bytes;
144 double io_rate_read_bps;
145 unsigned long long io_rate_read_time;
146 double io_rate_write_bps;
147 unsigned long long io_rate_write_time;
148 #endif
43ccb9c Initial import.
hisham authored Mar 4, 2006
149 } Process;
150
151 }*/
152
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
153 #ifdef DEBUG
43ccb9c Initial import.
hisham authored Mar 4, 2006
154 char* PROCESS_CLASS = "Process";
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
155 #else
156 #define PROCESS_CLASS NULL
157 #endif
43ccb9c Initial import.
hisham authored Mar 4, 2006
158
10d0290 Updates for new version of the MakeHeader.py script.
loderunner authored Jun 6, 2006
159 char *Process_fieldNames[] = {
010364f Ability to change sort column with the mouse by
loderunner authored Mar 14, 2008
160 "", "PID", "Command", "STATE", "PPID", "PGRP", "SESSION",
161 "TTY_NR", "TPGID", "FLAGS", "MINFLT", "CMINFLT", "MAJFLT", "CMAJFLT",
162 "UTIME", "STIME", "CUTIME", "CSTIME", "PRIORITY", "NICE", "ITREALVALUE",
163 "STARTTIME", "VSIZE", "RSS", "RLIM", "STARTCODE", "ENDCODE", "STARTSTACK",
164 "KSTKESP", "KSTKEIP", "SIGNAL", "BLOCKED", "SIGIGNORE", "SIGCATCH", "WCHAN",
165 "NSWAP", "CNSWAP", "EXIT_SIGNAL", "PROCESSOR", "M_SIZE", "M_RESIDENT", "M_SHARE",
166 "M_TRS", "M_DRS", "M_LRS", "M_DT", "ST_UID", "PERCENT_CPU", "PERCENT_MEM",
167 "USER", "TIME", "NLWP", "TGID",
bc6b07a OpenVZ support, contributed by Sergey Lychko
loderunner authored Aug 10, 2007
168 #ifdef HAVE_OPENVZ
479f6c5 Rename VEID to CTID in OpenVZ systems
loderunner authored Mar 11, 2009
169 "CTID", "VPID",
83d4ae6 Add support for Linux per-process IO statistics,
loderunner authored Mar 9, 2008
170 #endif
6d189ef Add Linux-VServer support,
loderunner authored Sep 23, 2008
171 #ifdef HAVE_VSERVER
172 "VXID",
173 #endif
83d4ae6 Add support for Linux per-process IO statistics,
loderunner authored Mar 9, 2008
174 #ifdef HAVE_TASKSTATS
010364f Ability to change sort column with the mouse by
loderunner authored Mar 14, 2008
175 "RCHAR", "WCHAR", "SYSCR", "SYSCW", "RBYTES", "WBYTES", "CNCLWB",
176 "IO_READ_RATE", "IO_WRITE_RATE", "IO_RATE",
bc6b07a OpenVZ support, contributed by Sergey Lychko
loderunner authored Aug 10, 2007
177 #endif
178 "*** report bug! ***"
10d0290 Updates for new version of the MakeHeader.py script.
loderunner authored Jun 6, 2006
179 };
43ccb9c Initial import.
hisham authored Mar 4, 2006
180
010364f Ability to change sort column with the mouse by
loderunner authored Mar 14, 2008
181 char *Process_fieldTitles[] = {
182 "", " PID ", "Command ", "S ", " PPID ", " PGRP ", " SESN ",
183 " TTY ", "TPGID ", "- ", "- ", "- ", "- ", "- ",
184 " UTIME+ ", " STIME+ ", "- ", "- ", "PRI ", " NI ", "- ",
185 "- ", "- ", "- ", "- ", "- ", "- ", "- ",
186 "- ", "- ", "- ", "- ", "- ", "- ", "- ",
187 "- ", "- ", "- ", "CPU ", " VIRT ", " RES ", " SHR ",
188 " CODE ", " DATA ", " LIB ", " DIRTY ", " UID ", "CPU% ", "MEM% ",
189 "USER ", " TIME+ ", "NLWP ", " TGID ",
190 #ifdef HAVE_OPENVZ
479f6c5 Rename VEID to CTID in OpenVZ systems
loderunner authored Mar 11, 2009
191 " CTID ", " VPID ",
010364f Ability to change sort column with the mouse by
loderunner authored Mar 14, 2008
192 #endif
6d189ef Add Linux-VServer support,
loderunner authored Sep 23, 2008
193 #ifdef HAVE_VSERVER
194 " VXID ",
195 #endif
010364f Ability to change sort column with the mouse by
loderunner authored Mar 14, 2008
196 #ifdef HAVE_TASKSTATS
197 " RD_CHAR ", " WR_CHAR ", " RD_SYSC ", " WR_SYSC ", " IO_RD ", " IO_WR ", " IO_CANCEL ",
198 " IORR ", " IOWR ", " IO ",
199 #endif
200 };
201
68c067e Share name strings from UsersList
loderunner authored Jul 12, 2006
202 static int Process_getuid = -1;
203
43ccb9c Initial import.
hisham authored Mar 4, 2006
204 #define ONE_K 1024
205 #define ONE_M (ONE_K * ONE_K)
206 #define ONE_G (ONE_M * ONE_K)
207
c8b5b5d Changes suggested by Bo Liu
loderunner authored Nov 20, 2006
208 static void Process_printLargeNumber(Process* this, RichString *str, unsigned long number) {
43ccb9c Initial import.
hisham authored Mar 4, 2006
209 char buffer[11];
210 int len;
632cb86 changes for htop 0.8.2
loderunner authored Jun 2, 2009
211 if(number >= (10 * ONE_M)) {
212 len = snprintf(buffer, 10, "%3.1fG ", (float)number / ONE_M);
43ccb9c Initial import.
hisham authored Mar 4, 2006
213 RichString_appendn(str, CRT_colors[LARGE_NUMBER], buffer, len);
214 } else if(number >= (100000)) {
a02d818 Fix gcc warning.
loderunner authored Nov 29, 2006
215 len = snprintf(buffer, 10, "%4ldM ", number / ONE_K);
43ccb9c Initial import.
hisham authored Mar 4, 2006
216 int attr = this->pl->highlightMegabytes
217 ? CRT_colors[PROCESS_MEGABYTES]
218 : CRT_colors[PROCESS];
219 RichString_appendn(str, attr, buffer, len);
220 } else if (this->pl->highlightMegabytes && number >= 1000) {
a02d818 Fix gcc warning.
loderunner authored Nov 29, 2006
221 len = snprintf(buffer, 10, "%2ld", number/1000);
43ccb9c Initial import.
hisham authored Mar 4, 2006
222 RichString_appendn(str, CRT_colors[PROCESS_MEGABYTES], buffer, len);
223 number %= 1000;
a02d818 Fix gcc warning.
loderunner authored Nov 29, 2006
224 len = snprintf(buffer, 10, "%03ld ", number);
43ccb9c Initial import.
hisham authored Mar 4, 2006
225 RichString_appendn(str, CRT_colors[PROCESS], buffer, len);
226 } else {
a02d818 Fix gcc warning.
loderunner authored Nov 29, 2006
227 len = snprintf(buffer, 10, "%5ld ", number);
43ccb9c Initial import.
hisham authored Mar 4, 2006
228 RichString_appendn(str, CRT_colors[PROCESS], buffer, len);
229 }
230 }
231
10d0290 Updates for new version of the MakeHeader.py script.
loderunner authored Jun 6, 2006
232 static double jiffy = 0.0;
43ccb9c Initial import.
hisham authored Mar 4, 2006
233
234 static void Process_printTime(RichString* str, unsigned long t) {
235 if(jiffy == 0.0) jiffy = sysconf(_SC_CLK_TCK);
236 double jiffytime = 1.0 / jiffy;
237
238 double realTime = t * jiffytime;
239 int iRealTime = (int) realTime;
240
241 int hours = iRealTime / 3600;
242 int minutes = (iRealTime / 60) % 60;
243 int seconds = iRealTime % 60;
244 int hundredths = (realTime - iRealTime) * 100;
245 char buffer[11];
246 if (hours) {
247 snprintf(buffer, 10, "%2dh", hours);
248 RichString_append(str, CRT_colors[LARGE_NUMBER], buffer);
249 snprintf(buffer, 10, "%02d:%02d ", minutes, seconds);
250 } else {
251 snprintf(buffer, 10, "%2d:%02d.%02d ", minutes, seconds, hundredths);
252 }
253 RichString_append(str, CRT_colors[DEFAULT_COLOR], buffer);
254 }
255
816d42a BUGFIX: Fix display of CPU count for threaded processes.
loderunner authored Mar 8, 2008
256 static inline void Process_writeCommand(Process* this, int attr, int baseattr, RichString* str) {
ef72a25 Add Unicode support, enabled with the --enable-unicode
loderunner authored Mar 9, 2008
257 int start = str->len;
258 RichString_append(str, attr, this->comm);
43ccb9c Initial import.
hisham authored Mar 4, 2006
259 if (this->pl->highlightBaseName) {
ef72a25 Add Unicode support, enabled with the --enable-unicode
loderunner authored Mar 9, 2008
260 int finish = str->len - 1;
261 int space = RichString_findChar(str, ' ', start);
262 if (space != -1)
263 finish = space - 1;
264 for (;;) {
265 int slash = RichString_findChar(str, '/', start);
266 if (slash == -1 || slash > finish)
267 break;
268 start = slash + 1;
43ccb9c Initial import.
hisham authored Mar 4, 2006
269 }
ef72a25 Add Unicode support, enabled with the --enable-unicode
loderunner authored Mar 9, 2008
270 RichString_setAttrn(str, baseattr, start, finish);
43ccb9c Initial import.
hisham authored Mar 4, 2006
271 }
272 }
273
010364f Ability to change sort column with the mouse by
loderunner authored Mar 14, 2008
274 static inline void Process_outputRate(Process* this, RichString* str, int attr, char* buffer, int n, double rate) {
275 rate = rate / 1024;
276 if (rate < 0.01)
277 snprintf(buffer, n, " 0 ");
278 else if (rate <= 10)
279 snprintf(buffer, n, "%5.2f ", rate);
280 else if (rate <= 100)
281 snprintf(buffer, n, "%5.1f ", rate);
282 else {
283 Process_printLargeNumber(this, str, rate);
284 return;
285 }
286 RichString_append(str, attr, buffer);
287 }
288
770be30 Clean up headers by using 'static' whenever possible.
loderunner authored Mar 9, 2008
289 static void Process_writeField(Process* this, RichString* str, ProcessField field) {
43ccb9c Initial import.
hisham authored Mar 4, 2006
290 char buffer[PROCESS_COMM_LEN];
291 int attr = CRT_colors[DEFAULT_COLOR];
816d42a BUGFIX: Fix display of CPU count for threaded processes.
loderunner authored Mar 8, 2008
292 int baseattr = CRT_colors[PROCESS_BASENAME];
43ccb9c Initial import.
hisham authored Mar 4, 2006
293 int n = PROCESS_COMM_LEN;
294
295 switch (field) {
525e355 Switch to unsigned keys in hash, according to issue #1688290
loderunner authored Apr 5, 2007
296 case PID: snprintf(buffer, n, "%5u ", this->pid); break;
297 case PPID: snprintf(buffer, n, "%5u ", this->ppid); break;
298 case PGRP: snprintf(buffer, n, "%5u ", this->pgrp); break;
299 case SESSION: snprintf(buffer, n, "%5u ", this->session); break;
300 case TTY_NR: snprintf(buffer, n, "%5u ", this->tty_nr); break;
d5fe357 Improve construction of tree view, properly nesting threads.
loderunner authored Nov 8, 2007
301 case TGID: snprintf(buffer, n, "%5u ", this->tgid); break;
302 case TPGID: snprintf(buffer, n, "%5d ", this->tpgid); break;
43ccb9c Initial import.
hisham authored Mar 4, 2006
303 case PROCESSOR: snprintf(buffer, n, "%3d ", this->processor+1); break;
f41ea30 Add support of NLWP field, by Bert Wesarg
loderunner authored May 21, 2007
304 case NLWP: snprintf(buffer, n, "%4ld ", this->nlwp); break;
43ccb9c Initial import.
hisham authored Mar 4, 2006
305 case COMM: {
816d42a BUGFIX: Fix display of CPU count for threaded processes.
loderunner authored Mar 8, 2008
306 if (this->pl->highlightThreads && (this->pid != this->tgid || this->m_size == 0)) {
307 attr = CRT_colors[PROCESS_THREAD];
308 baseattr = CRT_colors[PROCESS_THREAD_BASENAME];
309 }
43ccb9c Initial import.
hisham authored Mar 4, 2006
310 if (!this->pl->treeView || this->indent == 0) {
816d42a BUGFIX: Fix display of CPU count for threaded processes.
loderunner authored Mar 8, 2008
311 Process_writeCommand(this, attr, baseattr, str);
43ccb9c Initial import.
hisham authored Mar 4, 2006
312 return;
313 } else {
314 char* buf = buffer;
315 int maxIndent = 0;
316 for (int i = 0; i < 32; i++)
317 if (this->indent & (1 << i))
318 maxIndent = i+1;
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
319 for (int i = 0; i < maxIndent - 1; i++) {
43ccb9c Initial import.
hisham authored Mar 4, 2006
320 if (this->indent & (1 << i))
321 snprintf(buf, n, " | ");
322 else
323 snprintf(buf, n, " ");
324 buf += 4;
325 n -= 4;
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
326 }
43ccb9c Initial import.
hisham authored Mar 4, 2006
327 if (this->pl->direction == 1)
328 snprintf(buf, n, " `- ");
329 else
330 snprintf(buf, n, " ,- ");
331 RichString_append(str, CRT_colors[PROCESS_TREE], buffer);
816d42a BUGFIX: Fix display of CPU count for threaded processes.
loderunner authored Mar 8, 2008
332 Process_writeCommand(this, attr, baseattr, str);
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
333 return;
43ccb9c Initial import.
hisham authored Mar 4, 2006
334 }
335 }
336 case STATE: {
337 snprintf(buffer, n, "%c ", this->state);
338 attr = this->state == 'R'
339 ? CRT_colors[PROCESS_R_STATE]
340 : attr;
341 break;
342 }
343 case PRIORITY: {
344 if(this->priority == -100)
345 snprintf(buffer, n, " RT ");
346 else
347 snprintf(buffer, n, "%3ld ", this->priority);
348 break;
349 }
350 case NICE: {
351 snprintf(buffer, n, "%3ld ", this->nice);
352 attr = this->nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY]
353 : this->nice > 0 ? CRT_colors[PROCESS_LOW_PRIORITY]
354 : attr;
355 break;
356 }
1e492e4 BSD related fixes:
loderunner authored Mar 11, 2009
357 case M_DRS: Process_printLargeNumber(this, str, this->m_drs * PAGE_SIZE_KB); return;
358 case M_DT: Process_printLargeNumber(this, str, this->m_dt * PAGE_SIZE_KB); return;
359 case M_LRS: Process_printLargeNumber(this, str, this->m_lrs * PAGE_SIZE_KB); return;
360 case M_TRS: Process_printLargeNumber(this, str, this->m_trs * PAGE_SIZE_KB); return;
361 case M_SIZE: Process_printLargeNumber(this, str, this->m_size * PAGE_SIZE_KB); return;
362 case M_RESIDENT: Process_printLargeNumber(this, str, this->m_resident * PAGE_SIZE_KB); return;
363 case M_SHARE: Process_printLargeNumber(this, str, this->m_share * PAGE_SIZE_KB); return;
43ccb9c Initial import.
hisham authored Mar 4, 2006
364 case ST_UID: snprintf(buffer, n, "%4d ", this->st_uid); break;
365 case USER: {
68c067e Share name strings from UsersList
loderunner authored Jul 12, 2006
366 if (Process_getuid != this->st_uid)
43ccb9c Initial import.
hisham authored Mar 4, 2006
367 attr = CRT_colors[PROCESS_SHADOW];
68c067e Share name strings from UsersList
loderunner authored Jul 12, 2006
368 if (this->user) {
43ccb9c Initial import.
hisham authored Mar 4, 2006
369 snprintf(buffer, n, "%-8s ", this->user);
68c067e Share name strings from UsersList
loderunner authored Jul 12, 2006
370 } else {
371 snprintf(buffer, n, "%-8d ", this->st_uid);
372 }
43ccb9c Initial import.
hisham authored Mar 4, 2006
373 if (buffer[8] != '\0') {
374 buffer[8] = ' ';
375 buffer[9] = '\0';
376 }
377 break;
378 }
379 case UTIME: Process_printTime(str, this->utime); return;
380 case STIME: Process_printTime(str, this->stime); return;
381 case CUTIME: Process_printTime(str, this->cutime); return;
382 case CSTIME: Process_printTime(str, this->cstime); return;
383 case TIME: Process_printTime(str, this->utime + this->stime); return;
384 case PERCENT_CPU: {
816d42a BUGFIX: Fix display of CPU count for threaded processes.
loderunner authored Mar 8, 2008
385 if (this->percent_cpu > 999.9) {
386 snprintf(buffer, n, "%4d ", (unsigned int)this->percent_cpu);
387 } else if (this->percent_cpu > 99.9) {
388 snprintf(buffer, n, "%3d. ", (unsigned int)this->percent_cpu);
43ccb9c Initial import.
hisham authored Mar 4, 2006
389 } else {
390 snprintf(buffer, n, "%4.1f ", this->percent_cpu);
391 }
392 break;
393 }
394 case PERCENT_MEM: {
395 if (this->percent_mem > 99.9) {
396 snprintf(buffer, n, "100. ");
397 } else {
398 snprintf(buffer, n, "%4.1f ", this->percent_mem);
399 }
400 break;
401 }
bc6b07a OpenVZ support, contributed by Sergey Lychko
loderunner authored Aug 10, 2007
402 #ifdef HAVE_OPENVZ
479f6c5 Rename VEID to CTID in OpenVZ systems
loderunner authored Mar 11, 2009
403 case CTID: snprintf(buffer, n, "%5u ", this->ctid); break;
bc6b07a OpenVZ support, contributed by Sergey Lychko
loderunner authored Aug 10, 2007
404 case VPID: snprintf(buffer, n, "%5u ", this->vpid); break;
405 #endif
6d189ef Add Linux-VServer support,
loderunner authored Sep 23, 2008
406 #ifdef HAVE_VSERVER
407 case VXID: snprintf(buffer, n, "%5u ", this->vxid); break;
408 #endif
83d4ae6 Add support for Linux per-process IO statistics,
loderunner authored Mar 9, 2008
409 #ifdef HAVE_TASKSTATS
410 case RCHAR: snprintf(buffer, n, "%10llu ", this->io_rchar); break;
411 case WCHAR: snprintf(buffer, n, "%10llu ", this->io_wchar); break;
412 case SYSCR: snprintf(buffer, n, "%10llu ", this->io_syscr); break;
413 case SYSCW: snprintf(buffer, n, "%10llu ", this->io_syscw); break;
414 case RBYTES: snprintf(buffer, n, "%10llu ", this->io_read_bytes); break;
415 case WBYTES: snprintf(buffer, n, "%10llu ", this->io_write_bytes); break;
416 case CNCLWB: snprintf(buffer, n, "%10llu ", this->io_cancelled_write_bytes); break;
010364f Ability to change sort column with the mouse by
loderunner authored Mar 14, 2008
417 case IO_READ_RATE: Process_outputRate(this, str, attr, buffer, n, this->io_rate_read_bps); return;
418 case IO_WRITE_RATE: Process_outputRate(this, str, attr, buffer, n, this->io_rate_write_bps); return;
419 case IO_RATE: Process_outputRate(this, str, attr, buffer, n, this->io_rate_read_bps + this->io_rate_write_bps); return;
83d4ae6 Add support for Linux per-process IO statistics,
loderunner authored Mar 9, 2008
420 #endif
421
43ccb9c Initial import.
hisham authored Mar 4, 2006
422 default:
423 snprintf(buffer, n, "- ");
424 }
425 RichString_append(str, attr, buffer);
426 }
427
770be30 Clean up headers by using 'static' whenever possible.
loderunner authored Mar 9, 2008
428 static void Process_display(Object* cast, RichString* out) {
429 Process* this = (Process*) cast;
430 ProcessField* fields = this->pl->fields;
431 RichString_init(out);
432 for (int i = 0; fields[i]; i++)
433 Process_writeField(this, out, fields[i]);
434 if (this->pl->shadowOtherUsers && this->st_uid != Process_getuid)
435 RichString_setAttr(out, CRT_colors[PROCESS_SHADOW]);
436 if (this->tag == true)
437 RichString_setAttr(out, CRT_colors[PROCESS_TAG]);
438 assert(out->len > 0);
439 }
440
441 void Process_delete(Object* cast) {
442 Process* this = (Process*) cast;
443 assert (this != NULL);
444 if (this->comm) free(this->comm);
445 free(this);
446 }
447
448 Process* Process_new(struct ProcessList_ *pl) {
449 Process* this = calloc(sizeof(Process), 1);
450 Object_setClass(this, PROCESS_CLASS);
451 ((Object*)this)->display = Process_display;
452 ((Object*)this)->delete = Process_delete;
453 this->pid = 0;
454 this->pl = pl;
455 this->tag = false;
456 this->updated = false;
457 this->utime = 0;
458 this->stime = 0;
459 this->comm = NULL;
460 this->indent = 0;
461 if (Process_getuid == -1) Process_getuid = getuid();
462 return this;
463 }
464
465 Process* Process_clone(Process* this) {
466 Process* clone = malloc(sizeof(Process));
467 #if HAVE_TASKSTATS
468 this->io_rchar = 0;
469 this->io_wchar = 0;
470 this->io_syscr = 0;
471 this->io_syscw = 0;
472 this->io_read_bytes = 0;
473 this->io_rate_read_bps = 0;
474 this->io_rate_read_time = 0;
475 this->io_write_bytes = 0;
476 this->io_rate_write_bps = 0;
477 this->io_rate_write_time = 0;
478 this->io_cancelled_write_bytes = 0;
479 #endif
480 memcpy(clone, this, sizeof(Process));
481 this->comm = NULL;
482 this->pid = 0;
483 return clone;
484 }
485
486 void Process_toggleTag(Process* this) {
487 this->tag = this->tag == true ? false : true;
488 }
489
490 bool Process_setPriority(Process* this, int priority) {
491 int old_prio = getpriority(PRIO_PROCESS, this->pid);
492 int err = setpriority(PRIO_PROCESS, this->pid, priority);
493 if (err == 0 && old_prio != getpriority(PRIO_PROCESS, this->pid)) {
494 this->nice = priority;
495 }
496 return (err == 0);
497 }
498
1e492e4 BSD related fixes:
loderunner authored Mar 11, 2009
499 #ifdef HAVE_PLPA
770be30 Clean up headers by using 'static' whenever possible.
loderunner authored Mar 9, 2008
500 unsigned long Process_getAffinity(Process* this) {
501 unsigned long mask = 0;
502 plpa_sched_getaffinity(this->pid, sizeof(unsigned long), (plpa_cpu_set_t*) &mask);
503 return mask;
504 }
505
506 bool Process_setAffinity(Process* this, unsigned long mask) {
507 return (plpa_sched_setaffinity(this->pid, sizeof(unsigned long), (plpa_cpu_set_t*) &mask) == 0);
508 }
1e492e4 BSD related fixes:
loderunner authored Mar 11, 2009
509 #endif
770be30 Clean up headers by using 'static' whenever possible.
loderunner authored Mar 9, 2008
510
511 void Process_sendSignal(Process* this, int signal) {
512 kill(this->pid, signal);
513 }
514
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
515 int Process_pidCompare(const void* v1, const void* v2) {
43ccb9c Initial import.
hisham authored Mar 4, 2006
516 Process* p1 = (Process*)v1;
517 Process* p2 = (Process*)v2;
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
518 return (p1->pid - p2->pid);
519 }
520
521 int Process_compare(const void* v1, const void* v2) {
522 Process *p1, *p2;
523 ProcessList *pl = ((Process*)v1)->pl;
524 if (pl->direction == 1) {
525 p1 = (Process*)v1;
526 p2 = (Process*)v2;
527 } else {
528 p2 = (Process*)v1;
529 p1 = (Process*)v2;
530 }
83d4ae6 Add support for Linux per-process IO statistics,
loderunner authored Mar 9, 2008
531 long long diff;
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
532 switch (pl->sortKey) {
43ccb9c Initial import.
hisham authored Mar 4, 2006
533 case PID:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
534 return (p1->pid - p2->pid);
43ccb9c Initial import.
hisham authored Mar 4, 2006
535 case PPID:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
536 return (p1->ppid - p2->ppid);
43ccb9c Initial import.
hisham authored Mar 4, 2006
537 case USER:
632cb86 changes for htop 0.8.2
loderunner authored Jun 2, 2009
538 return strcmp(p1->user ? p1->user : "", p2->user ? p2->user : "");
43ccb9c Initial import.
hisham authored Mar 4, 2006
539 case PRIORITY:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
540 return (p1->priority - p2->priority);
ac285aa sort by PROCESSOR and SESSION
loderunner authored Mar 11, 2009
541 case PROCESSOR:
542 return (p1->processor - p2->processor);
543 case SESSION:
544 return (p1->session - p2->session);
43ccb9c Initial import.
hisham authored Mar 4, 2006
545 case STATE:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
546 return (p1->state - p2->state);
43ccb9c Initial import.
hisham authored Mar 4, 2006
547 case NICE:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
548 return (p1->nice - p2->nice);
df1b00c Apply patches by Matthias Lederhofer adding extra
loderunner authored Apr 5, 2007
549 case M_DRS:
550 return (p2->m_drs - p1->m_drs);
551 case M_DT:
552 return (p2->m_dt - p1->m_dt);
553 case M_LRS:
554 return (p2->m_lrs - p1->m_lrs);
555 case M_TRS:
556 return (p2->m_trs - p1->m_trs);
43ccb9c Initial import.
hisham authored Mar 4, 2006
557 case M_SIZE:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
558 return (p2->m_size - p1->m_size);
43ccb9c Initial import.
hisham authored Mar 4, 2006
559 case M_RESIDENT:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
560 return (p2->m_resident - p1->m_resident);
43ccb9c Initial import.
hisham authored Mar 4, 2006
561 case M_SHARE:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
562 return (p2->m_share - p1->m_share);
43ccb9c Initial import.
hisham authored Mar 4, 2006
563 case PERCENT_CPU:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
564 return (p2->percent_cpu > p1->percent_cpu ? 1 : -1);
43ccb9c Initial import.
hisham authored Mar 4, 2006
565 case PERCENT_MEM:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
566 return (p2->m_resident - p1->m_resident);
43ccb9c Initial import.
hisham authored Mar 4, 2006
567 case UTIME:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
568 return (p2->utime - p1->utime);
43ccb9c Initial import.
hisham authored Mar 4, 2006
569 case STIME:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
570 return (p2->stime - p1->stime);
43ccb9c Initial import.
hisham authored Mar 4, 2006
571 case TIME:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
572 return ((p2->utime+p2->stime) - (p1->utime+p1->stime));
43ccb9c Initial import.
hisham authored Mar 4, 2006
573 case COMM:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
574 return strcmp(p1->comm, p2->comm);
f41ea30 Add support of NLWP field, by Bert Wesarg
loderunner authored May 21, 2007
575 case NLWP:
576 return (p1->nlwp - p2->nlwp);
bc6b07a OpenVZ support, contributed by Sergey Lychko
loderunner authored Aug 10, 2007
577 #ifdef HAVE_OPENVZ
479f6c5 Rename VEID to CTID in OpenVZ systems
loderunner authored Mar 11, 2009
578 case CTID:
579 return (p1->ctid - p2->ctid);
bc6b07a OpenVZ support, contributed by Sergey Lychko
loderunner authored Aug 10, 2007
580 case VPID:
581 return (p1->vpid - p2->vpid);
582 #endif
6d189ef Add Linux-VServer support,
loderunner authored Sep 23, 2008
583 #ifdef HAVE_VSERVER
584 case VXID:
585 return (p1->vxid - p2->vxid);
586 #endif
83d4ae6 Add support for Linux per-process IO statistics,
loderunner authored Mar 9, 2008
587 #ifdef HAVE_TASKSTATS
588 case RCHAR: diff = p2->io_rchar - p1->io_rchar; goto test_diff;
589 case WCHAR: diff = p2->io_wchar - p1->io_wchar; goto test_diff;
590 case SYSCR: diff = p2->io_syscr - p1->io_syscr; goto test_diff;
591 case SYSCW: diff = p2->io_syscw - p1->io_syscw; goto test_diff;
592 case RBYTES: diff = p2->io_read_bytes - p1->io_read_bytes; goto test_diff;
593 case WBYTES: diff = p2->io_write_bytes - p1->io_write_bytes; goto test_diff;
594 case CNCLWB: diff = p2->io_cancelled_write_bytes - p1->io_cancelled_write_bytes; goto test_diff;
595 case IO_READ_RATE: diff = p2->io_rate_read_bps - p1->io_rate_read_bps; goto test_diff;
596 case IO_WRITE_RATE: diff = p2->io_rate_write_bps - p1->io_rate_write_bps; goto test_diff;
010364f Ability to change sort column with the mouse by
loderunner authored Mar 14, 2008
597 case IO_RATE: diff = (p2->io_rate_read_bps + p2->io_rate_write_bps) - (p1->io_rate_read_bps + p1->io_rate_write_bps); goto test_diff;
83d4ae6 Add support for Linux per-process IO statistics,
loderunner authored Mar 9, 2008
598 #endif
599
43ccb9c Initial import.
hisham authored Mar 4, 2006
600 default:
8d8593b Performance improvement hackathon: improve process comparison routines,
loderunner authored Jul 11, 2006
601 return (p1->pid - p2->pid);
43ccb9c Initial import.
hisham authored Mar 4, 2006
602 }
83d4ae6 Add support for Linux per-process IO statistics,
loderunner authored Mar 9, 2008
603 test_diff:
604 return (diff > 0) ? 1 : (diff < 0 ? -1 : 0);
43ccb9c Initial import.
hisham authored Mar 4, 2006
605 }
Something went wrong with that request. Please try again.