LinuxProcess.c 25.6 KB
Newer Older
1
2
3
4
5
6
7
8
/*
htop - LinuxProcess.c
(C) 2014 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "Process.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
9
10
#include "ProcessList.h"
#include "LinuxProcess.h"
11
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
12
13
#include "CRT.h"

14
#include <stdlib.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
15
#include <unistd.h>
16
#include <string.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
17
#include <sys/syscall.h>
18
19
20

/*{

21
22
#include "PerfCounter.h"

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#define PROCESS_FLAG_LINUX_IOPRIO   0x0100L
#define PROCESS_FLAG_LINUX_OPENVZ   0x0200L
#define PROCESS_FLAG_LINUX_VSERVER  0x0400L
#define PROCESS_FLAG_LINUX_CGROUP   0x0800L
#define PROCESS_FLAG_LINUX_OOM      0x1000L

#define PROCESS_FLAG_LINUX_HPC       0xff0000L
#define PROCESS_FLAG_LINUX_HPC_CYCLE 0x10000L
#define PROCESS_FLAG_LINUX_HPC_INSN  0x20000L
#define PROCESS_FLAG_LINUX_HPC_MISS  0x40000L
#define PROCESS_FLAG_LINUX_HPC_BMISS 0x80000L

#define PROCESS_FLAG_LINUX_HPC_L1DR   0x100000L
#define PROCESS_FLAG_LINUX_HPC_L1DW   0x200000L
#define PROCESS_FLAG_LINUX_HPC_L1DRM  0x400000L
#define PROCESS_FLAG_LINUX_HPC_L1DWM  0x800000L
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
typedef enum UnsupportedProcessFields {
   FLAGS = 9,
   ITREALVALUE = 20,
   VSIZE = 22,
   RSS = 23,
   RLIM = 24,
   STARTCODE = 25,
   ENDCODE = 26,
   STARTSTACK = 27,
   KSTKESP = 28,
   KSTKEIP = 29,
   SIGNAL = 30,
   BLOCKED = 31,
   SSIGIGNORE = 32,
   SIGCATCH = 33,
   WCHAN = 34,
   NSWAP = 35,
   CNSWAP = 36,
   EXIT_SIGNAL = 37,
} UnsupportedProcessField;

61
typedef enum LinuxProcessFields {
62
63
64
65
66
67
68
69
70
71
72
   CMINFLT = 11,
   CMAJFLT = 13,
   UTIME = 14,
   STIME = 15,
   CUTIME = 16,
   CSTIME = 17,
   M_SHARE = 41,
   M_TRS = 42,
   M_DRS = 43,
   M_LRS = 44,
   M_DT = 45,
73
   #ifdef HAVE_OPENVZ
74
75
   CTID = 100,
   VPID = 101,
76
77
   #endif
   #ifdef HAVE_VSERVER
78
   VXID = 102,
79
80
   #endif
   #ifdef HAVE_TASKSTATS
81
82
83
84
85
86
87
88
89
90
   RCHAR = 103,
   WCHAR = 104,
   SYSCR = 105,
   SYSCW = 106,
   RBYTES = 107,
   WBYTES = 108,
   CNCLWB = 109,
   IO_READ_RATE = 110,
   IO_WRITE_RATE = 111,
   IO_RATE = 112,
91
92
   #endif
   #ifdef HAVE_CGROUP
93
   CGROUP = 113,
94
   #endif
95
96
   OOM = 114,
   IO_PRIORITY = 115,
97
98
99
100
101
   #ifdef HAVE_DELAYACCT
   PERCENT_CPU_DELAY = 116,
   PERCENT_IO_DELAY = 117,
   PERCENT_SWAP_DELAY = 118,
   #endif
102
103
   #ifdef HAVE_PERFCOUNTERS
   IPC = 119,
104
105
106
107
108
109
110
111
   MCYCLE = 120,
   MINSTR = 121,
   PERCENT_MISS = 122,
   PERCENT_BMISS = 123,
   L1DREADS = 124,
   L1DRMISSES = 125,
   L1DWRITES = 126,
   L1DWMISSES = 127,
112
   #endif
113
   LAST_PROCESSFIELD = 128,
114
115
} LinuxProcessField;

Hisham Muhammad's avatar
Hisham Muhammad committed
116
117
#include "IOPriority.h"

118
119
typedef struct LinuxProcess_ {
   Process super;
120
   bool isKernelThread;
121
   IOPriority ioPriority;
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
   unsigned long int cminflt;
   unsigned long int cmajflt;
   unsigned long long int utime;
   unsigned long long int stime;
   unsigned long long int cutime;
   unsigned long long int cstime;
   long m_share;
   long m_trs;
   long m_drs;
   long m_lrs;
   long m_dt;
   #ifdef HAVE_TASKSTATS
   unsigned long long io_rchar;
   unsigned long long io_wchar;
   unsigned long long io_syscr;
   unsigned long long io_syscw;
   unsigned long long io_read_bytes;
   unsigned long long io_write_bytes;
   unsigned long long io_cancelled_write_bytes;
   unsigned long long io_rate_read_time;
   unsigned long long io_rate_write_time;   
   double io_rate_read_bps;
   double io_rate_write_bps;
   #endif
   #ifdef HAVE_OPENVZ
   unsigned int ctid;
   unsigned int vpid;
   #endif
   #ifdef HAVE_VSERVER
   unsigned int vxid;
   #endif
   #ifdef HAVE_CGROUP
   char* cgroup;
   #endif
   unsigned int oom;
Hisham's avatar
Hisham committed
157
   char* ttyDevice;
158
159
160
161
162
163
164
165
166
   #ifdef HAVE_DELAYACCT
   unsigned long long int delay_read_time;
   unsigned long long cpu_delay_total;
   unsigned long long blkio_delay_total;
   unsigned long long swapin_delay_total;
   float cpu_delay_percent;
   float blkio_delay_percent;
   float swapin_delay_percent;
   #endif
167
168
169
   #ifdef HAVE_PERFCOUNTERS
   PerfCounter* cycleCounter;
   PerfCounter* insnCounter;
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
   PerfCounter* missCounter;
   PerfCounter* brCounter;
   PerfCounter* l1drCounter;
   PerfCounter* l1drmCounter;
   PerfCounter* l1dwCounter;
   PerfCounter* l1dwmCounter;
   float ipc;
   float mcycle;
   float minstr;
   float pMiss;
   float pBMiss;
   float l1dr;
   float l1drm;
   float l1dw;
   float l1dwm;
185
   #endif
186
187
} LinuxProcess;

188
#ifndef Process_isKernelThread
189
#define Process_isKernelThread(_process) ((LinuxProcess*)(_process)->isKernelThread)
190
191
192
193
194
#endif

#ifndef Process_isUserlandThread
#define Process_isUserlandThread(_process) (_process->pid != _process->tgid)
#endif
195

196
197
}*/

198
ProcessFieldData Process_fields[] = {
199
200
201
   [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, },
   [PID] = { .name = "PID", .title = "    PID ", .description = "Process/thread ID", .flags = 0, },
   [COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, },
202
   [STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, D disk, Z zombie, T traced, W paging, I idle)", .flags = 0, },
203
204
   [PPID] = { .name = "PPID", .title = "   PPID ", .description = "Parent process ID", .flags = 0, },
   [PGRP] = { .name = "PGRP", .title = "   PGRP ", .description = "Process group ID", .flags = 0, },
205
   [SESSION] = { .name = "SESSION", .title = "    SID ", .description = "Process's session ID", .flags = 0, },
Hisham's avatar
Hisham committed
206
   [TTY_NR] = { .name = "TTY_NR", .title = "TTY      ", .description = "Controlling terminal", .flags = 0, },
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
   [TPGID] = { .name = "TPGID", .title = "  TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, },
   [FLAGS] = { .name = "FLAGS", .title = NULL, .description = NULL, .flags = 0, },
   [MINFLT] = { .name = "MINFLT", .title = "     MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, },
   [CMINFLT] = { .name = "CMINFLT", .title = "    CMINFLT ", .description = "Children processes' minor faults", .flags = 0, },
   [MAJFLT] = { .name = "MAJFLT", .title = "     MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, },
   [CMAJFLT] = { .name = "CMAJFLT", .title = "    CMAJFLT ", .description = "Children processes' major faults", .flags = 0, },
   [UTIME] = { .name = "UTIME", .title = " UTIME+  ", .description = "User CPU time - time the process spent executing in user mode", .flags = 0, },
   [STIME] = { .name = "STIME", .title = " STIME+  ", .description = "System CPU time - time the kernel spent running system calls for this process", .flags = 0, },
   [CUTIME] = { .name = "CUTIME", .title = " CUTIME+ ", .description = "Children processes' user CPU time", .flags = 0, },
   [CSTIME] = { .name = "CSTIME", .title = " CSTIME+ ", .description = "Children processes' system CPU time", .flags = 0, },
   [PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, },
   [NICE] = { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, },
   [ITREALVALUE] = { .name = "ITREALVALUE", .title = NULL, .description = NULL, .flags = 0, },
   [STARTTIME] = { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, },
   [VSIZE] = { .name = "VSIZE", .title = NULL, .description = NULL, .flags = 0, },
   [RSS] = { .name = "RSS", .title = NULL, .description = NULL, .flags = 0, },
   [RLIM] = { .name = "RLIM", .title = NULL, .description = NULL, .flags = 0, },
   [STARTCODE] = { .name = "STARTCODE", .title = NULL, .description = NULL, .flags = 0, },
   [ENDCODE] = { .name = "ENDCODE", .title = NULL, .description = NULL, .flags = 0, },
   [STARTSTACK] = { .name = "STARTSTACK", .title = NULL, .description = NULL, .flags = 0, },
   [KSTKESP] = { .name = "KSTKESP", .title = NULL, .description = NULL, .flags = 0, },
   [KSTKEIP] = { .name = "KSTKEIP", .title = NULL, .description = NULL, .flags = 0, },
   [SIGNAL] = { .name = "SIGNAL", .title = NULL, .description = NULL, .flags = 0, },
   [BLOCKED] = { .name = "BLOCKED", .title = NULL, .description = NULL, .flags = 0, },
   [SSIGIGNORE] = { .name = "SIGIGNORE", .title = NULL, .description = NULL, .flags = 0, },
   [SIGCATCH] = { .name = "SIGCATCH", .title = NULL, .description = NULL, .flags = 0, },
   [WCHAN] = { .name = "WCHAN", .title = NULL, .description = NULL, .flags = 0, },
   [NSWAP] = { .name = "NSWAP", .title = NULL, .description = NULL, .flags = 0, },
   [CNSWAP] = { .name = "CNSWAP", .title = NULL, .description = NULL, .flags = 0, },
   [EXIT_SIGNAL] = { .name = "EXIT_SIGNAL", .title = NULL, .description = NULL, .flags = 0, },
   [PROCESSOR] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, },
   [M_SIZE] = { .name = "M_SIZE", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, },
   [M_RESIDENT] = { .name = "M_RESIDENT", .title = "  RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, },
   [M_SHARE] = { .name = "M_SHARE", .title = "  SHR ", .description = "Size of the process's shared pages", .flags = 0, },
   [M_TRS] = { .name = "M_TRS", .title = " CODE ", .description = "Size of the text segment of the process", .flags = 0, },
   [M_DRS] = { .name = "M_DRS", .title = " DATA ", .description = "Size of the data segment plus stack usage of the process", .flags = 0, },
   [M_LRS] = { .name = "M_LRS", .title = " LIB ", .description = "The library size of the process", .flags = 0, },
   [M_DT] = { .name = "M_DT", .title = " DIRTY ", .description = "Size of the dirty pages of the process", .flags = 0, },
   [ST_UID] = { .name = "ST_UID", .title = " UID ", .description = "User ID of the process owner", .flags = 0, },
   [PERCENT_CPU] = { .name = "PERCENT_CPU", .title = "CPU% ", .description = "Percentage of the CPU time the process used in the last sampling", .flags = 0, },
   [PERCENT_MEM] = { .name = "PERCENT_MEM", .title = "MEM% ", .description = "Percentage of the memory the process is using, based on resident memory size", .flags = 0, },
   [USER] = { .name = "USER", .title = "USER      ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, },
   [TIME] = { .name = "TIME", .title = "  TIME+  ", .description = "Total time the process has spent in user and system time", .flags = 0, },
   [NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, },
   [TGID] = { .name = "TGID", .title = "   TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, },
252
#ifdef HAVE_OPENVZ
253
254
   [CTID] = { .name = "CTID", .title = "   CTID ", .description = "OpenVZ container ID (a.k.a. virtual environment ID)", .flags = PROCESS_FLAG_LINUX_OPENVZ, },
   [VPID] = { .name = "VPID", .title = " VPID ", .description = "OpenVZ process ID", .flags = PROCESS_FLAG_LINUX_OPENVZ, },
255
256
#endif
#ifdef HAVE_VSERVER
257
   [VXID] = { .name = "VXID", .title = " VXID ", .description = "VServer process ID", .flags = PROCESS_FLAG_LINUX_VSERVER, },
258
259
#endif
#ifdef HAVE_TASKSTATS
260
261
262
263
264
265
266
267
268
269
   [RCHAR] = { .name = "RCHAR", .title = "    RD_CHAR ", .description = "Number of bytes the process has read", .flags = PROCESS_FLAG_IO, },
   [WCHAR] = { .name = "WCHAR", .title = "    WR_CHAR ", .description = "Number of bytes the process has written", .flags = PROCESS_FLAG_IO, },
   [SYSCR] = { .name = "SYSCR", .title = "    RD_SYSC ", .description = "Number of read(2) syscalls for the process", .flags = PROCESS_FLAG_IO, },
   [SYSCW] = { .name = "SYSCW", .title = "    WR_SYSC ", .description = "Number of write(2) syscalls for the process", .flags = PROCESS_FLAG_IO, },
   [RBYTES] = { .name = "RBYTES", .title = "  IO_RBYTES ", .description = "Bytes of read(2) I/O for the process", .flags = PROCESS_FLAG_IO, },
   [WBYTES] = { .name = "WBYTES", .title = "  IO_WBYTES ", .description = "Bytes of write(2) I/O for the process", .flags = PROCESS_FLAG_IO, },
   [CNCLWB] = { .name = "CNCLWB", .title = "  IO_CANCEL ", .description = "Bytes of cancelled write(2) I/O", .flags = PROCESS_FLAG_IO, },
   [IO_READ_RATE] = { .name = "IO_READ_RATE", .title = "  DISK READ ", .description = "The I/O rate of read(2) in bytes per second for the process", .flags = PROCESS_FLAG_IO, },
   [IO_WRITE_RATE] = { .name = "IO_WRITE_RATE", .title = " DISK WRITE ", .description = "The I/O rate of write(2) in bytes per second for the process", .flags = PROCESS_FLAG_IO, },
   [IO_RATE] = { .name = "IO_RATE", .title = "   DISK R/W ", .description = "Total I/O rate in bytes per second", .flags = PROCESS_FLAG_IO, },
270
271
#endif
#ifdef HAVE_CGROUP
272
   [CGROUP] = { .name = "CGROUP", .title = "    CGROUP ", .description = "Which cgroup the process is in", .flags = PROCESS_FLAG_LINUX_CGROUP, },
273
#endif
274
   [OOM] = { .name = "OOM", .title = "    OOM ", .description = "OOM (Out-of-Memory) killer score", .flags = PROCESS_FLAG_LINUX_OOM, },
275
   [IO_PRIORITY] = { .name = "IO_PRIORITY", .title = "IO ", .description = "I/O priority", .flags = PROCESS_FLAG_LINUX_IOPRIO, },
276
#ifdef HAVE_DELAYACCT
277
   [PERCENT_CPU_DELAY] = { .name = "PERCENT_CPU_DELAY", .title = "CPD% ", .description = "CPU delay %", .flags = 0, },
278
   [PERCENT_IO_DELAY] = { .name = "PERCENT_IO_DELAY", .title = "IOD% ", .description = "Block I/O delay %", .flags = 0, },
279
   [PERCENT_SWAP_DELAY] = { .name = "PERCENT_SWAP_DELAY", .title = "SWD% ", .description = "Swapin delay %", .flags = 0, },
280
281
#endif
#ifdef HAVE_PERFCOUNTERS
282
283
284
285
286
287
288
289
290
   [IPC] = { .name = "IPC", .title = "   IPC ", .description = "Executed instructions per cycle", .flags = PROCESS_FLAG_LINUX_HPC_CYCLE | PROCESS_FLAG_LINUX_HPC_INSN, },
   [MCYCLE] = { .name = "MCYCLE", .title = "  Mcycle ", .description = "Cycles (millions)", .flags = PROCESS_FLAG_LINUX_HPC_CYCLE, },
   [MINSTR] = { .name = "MINSTR", .title = "  Minstr ", .description = "Instructions (millions)", .flags = PROCESS_FLAG_LINUX_HPC_INSN, },
   [PERCENT_MISS] = { .name = "PERCENT_MISS", .title = "MIS% ", .description = "Cache misses per 100 instructions", .flags = PROCESS_FLAG_LINUX_HPC_MISS | PROCESS_FLAG_LINUX_HPC_INSN, },
   [PERCENT_BMISS] = { .name = "PERCENT_BMISS", .title = "BrM% ", .description = "Branch misprediction per 100 instructions", .flags = PROCESS_FLAG_LINUX_HPC_BMISS | PROCESS_FLAG_LINUX_HPC_INSN, },
   [L1DREADS] = { .name = "L1DREADS", .title =     "  L1Dread ", .description = "L1 data cache: reads (thousands)", .flags = PROCESS_FLAG_LINUX_HPC_L1DR, },
   [L1DRMISSES] = { .name = "L1DRMISSES", .title = "   R miss ", .description = "L1 data cache: reads misses (thousands)", .flags = PROCESS_FLAG_LINUX_HPC_L1DRM, },
   [L1DWRITES] = { .name = "L1DWRITES", .title =   " L1Dwrite ", .description = "L1D data cache: writes (thousands)", .flags = PROCESS_FLAG_LINUX_HPC_L1DW, },
   [L1DWMISSES] = { .name = "L1DWMISSES", .title = "   W miss ", .description = "L1D data cache: write misses (thousands)", .flags = PROCESS_FLAG_LINUX_HPC_L1DWM, },
291
#endif
292
   [LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, },
293
294
};

295
296
297
298
299
300
301
302
303
ProcessPidColumn Process_pidColumns[] = {
   { .id = PID, .label = "PID" },
   { .id = PPID, .label = "PPID" },
   #ifdef HAVE_OPENVZ
   { .id = VPID, .label = "VPID" },
   #endif
   { .id = TPGID, .label = "TPGID" },
   { .id = TGID, .label = "TGID" },
   { .id = PGRP, .label = "PGRP" },
304
   { .id = SESSION, .label = "SID" },
305
306
307
   { .id = OOM, .label = "OOM" },
   { .id = 0, .label = NULL },
};
308

309
310
311
312
313
314
315
316
317
318
ProcessClass LinuxProcess_class = {
   .super = {
      .extends = Class(Process),
      .display = Process_display,
      .delete = Process_delete,
      .compare = LinuxProcess_compare
   },
   .writeField = (Process_WriteField) LinuxProcess_writeField,
};

319
LinuxProcess* LinuxProcess_new(Settings* settings) {
Hisham's avatar
Hisham committed
320
   LinuxProcess* this = xCalloc(1, sizeof(LinuxProcess));
321
   Object_setClass(this, Class(LinuxProcess));
322
323
324
325
   Process_init(&this->super, settings);
   return this;
}

326
void Process_delete(Object* cast) {
327
   LinuxProcess* this = (LinuxProcess*) cast;
328
   Process_done((Process*)cast);
329
330
#ifdef HAVE_CGROUP
   free(this->cgroup);
331
332
333
334
#endif
#ifdef HAVE_PERFCOUNTERS
   PerfCounter_delete(this->cycleCounter);
   PerfCounter_delete(this->insnCounter);
335
#endif
Hisham's avatar
Hisham committed
336
   free(this->ttyDevice);
337
338
339
   free(this);
}

340
341
342
343
344
345
346
347
/*
[1] Note that before kernel 2.6.26 a process that has not asked for
an io priority formally uses "none" as scheduling class, but the
io scheduler will treat such processes as if it were in the best
effort class. The priority within the best effort class will  be
dynamically  derived  from  the  cpu  nice level of the process:
io_priority = (cpu_nice + 20) / 5. -- From ionice(1) man page
*/
Hisham Muhammad's avatar
Hisham Muhammad committed
348
#define LinuxProcess_effectiveIOPriority(p_) (IOPriority_class(p_->ioPriority) == IOPRIO_CLASS_NONE ? IOPriority_tuple(IOPRIO_CLASS_BE, (p_->super.nice + 20) / 5) : p_->ioPriority)
349

Hisham Muhammad's avatar
Hisham Muhammad committed
350
IOPriority LinuxProcess_updateIOPriority(LinuxProcess* this) {
351
352
353
354
355
   IOPriority ioprio = 0;
// Other OSes masquerading as Linux (NetBSD?) don't have this syscall
#ifdef SYS_ioprio_get
   ioprio = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, this->super.pid);
#endif
356
357
358
359
   this->ioPriority = ioprio;
   return ioprio;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
360
bool LinuxProcess_setIOPriority(LinuxProcess* this, IOPriority ioprio) {
361
362
// Other OSes masquerading as Linux (NetBSD?) don't have this syscall
#ifdef SYS_ioprio_set
Hisham Muhammad's avatar
Hisham Muhammad committed
363
   syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, this->super.pid, ioprio);
364
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
365
   return (LinuxProcess_updateIOPriority(this) == ioprio);
366
367
}

368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#if HAVE_DELAYACCT || HAVE_PERFCOUNTERS

static char* perfFmt[] = {
   "%6.2f ",
   NULL,
   NULL,
   NULL,
   NULL,
   NULL,
   "%6.1f ",
   "%7.1f ",
   "%8.2f ",
   "%9.1f ",
};

static char* perfNA[] = {
   "   N/A ",
   NULL,
   NULL,
   NULL,
   NULL,
   NULL,
   "   N/A ",
   "    N/A ",
   "     N/A ",
   "      N/A ",
};

static inline void LinuxProcess_printPerfCounter(float val, int len, char* buffer, int n, int* attr) {
   if (val != -1) {
      xSnprintf(buffer, n, perfFmt[len], val);
   } else {
      xSnprintf(buffer, n, perfNA[len]);
      *attr = CRT_colors[PROCESS_SHADOW];
   }
403
}
404

405
406
#endif

407
void LinuxProcess_writeField(Process* this, RichString* str, ProcessField field) {
Hisham Muhammad's avatar
Hisham Muhammad committed
408
   LinuxProcess* lp = (LinuxProcess*) this;
409
   bool coloring = this->settings->highlightMegabytes;
Hisham Muhammad's avatar
Hisham Muhammad committed
410
411
412
   char buffer[256]; buffer[255] = '\0';
   int attr = CRT_colors[DEFAULT_COLOR];
   int n = sizeof(buffer) - 1;
413
   switch ((int)field) {
Hisham's avatar
Hisham committed
414
415
   case TTY_NR: {
      if (lp->ttyDevice) {
416
         xSnprintf(buffer, n, "%-9s", lp->ttyDevice + 5 /* skip "/dev/" */);
Hisham's avatar
Hisham committed
417
418
      } else {
         attr = CRT_colors[PROCESS_SHADOW];
419
         xSnprintf(buffer, n, "?        ");
Hisham's avatar
Hisham committed
420
421
422
      }
      break;
   }
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
   case CMINFLT: Process_colorNumber(str, lp->cminflt, coloring); return;
   case CMAJFLT: Process_colorNumber(str, lp->cmajflt, coloring); return;
   case M_DRS: Process_humanNumber(str, lp->m_drs * PAGE_SIZE_KB, coloring); return;
   case M_DT: Process_humanNumber(str, lp->m_dt * PAGE_SIZE_KB, coloring); return;
   case M_LRS: Process_humanNumber(str, lp->m_lrs * PAGE_SIZE_KB, coloring); return;
   case M_TRS: Process_humanNumber(str, lp->m_trs * PAGE_SIZE_KB, coloring); return;
   case M_SHARE: Process_humanNumber(str, lp->m_share * PAGE_SIZE_KB, coloring); return;
   case UTIME: Process_printTime(str, lp->utime); return;
   case STIME: Process_printTime(str, lp->stime); return;
   case CUTIME: Process_printTime(str, lp->cutime); return;
   case CSTIME: Process_printTime(str, lp->cstime); return;
   #ifdef HAVE_TASKSTATS
   case RCHAR:  Process_colorNumber(str, lp->io_rchar, coloring); return;
   case WCHAR:  Process_colorNumber(str, lp->io_wchar, coloring); return;
   case SYSCR:  Process_colorNumber(str, lp->io_syscr, coloring); return;
   case SYSCW:  Process_colorNumber(str, lp->io_syscw, coloring); return;
   case RBYTES: Process_colorNumber(str, lp->io_read_bytes, coloring); return;
   case WBYTES: Process_colorNumber(str, lp->io_write_bytes, coloring); return;
   case CNCLWB: Process_colorNumber(str, lp->io_cancelled_write_bytes, coloring); return;
   case IO_READ_RATE:  Process_outputRate(str, buffer, n, lp->io_rate_read_bps, coloring); return;
   case IO_WRITE_RATE: Process_outputRate(str, buffer, n, lp->io_rate_write_bps, coloring); return;
444
445
446
447
448
449
   case IO_RATE: {
      double totalRate = (lp->io_rate_read_bps != -1)
                       ? (lp->io_rate_read_bps + lp->io_rate_write_bps)
                       : -1;
      Process_outputRate(str, buffer, n, totalRate, coloring); return;
   }
450
451
   #endif
   #ifdef HAVE_OPENVZ
452
453
   case CTID: xSnprintf(buffer, n, "%7u ", lp->ctid); break;
   case VPID: xSnprintf(buffer, n, Process_pidFormat, lp->vpid); break;
454
455
   #endif
   #ifdef HAVE_VSERVER
456
   case VXID: xSnprintf(buffer, n, "%5u ", lp->vxid); break;
457
458
   #endif
   #ifdef HAVE_CGROUP
459
   case CGROUP: xSnprintf(buffer, n, "%-10s ", lp->cgroup); break;
460
   #endif
461
   case OOM: xSnprintf(buffer, n, Process_pidFormat, lp->oom); break;
462
   case IO_PRIORITY: {
Hisham Muhammad's avatar
Hisham Muhammad committed
463
      int klass = IOPriority_class(lp->ioPriority);
464
465
      if (klass == IOPRIO_CLASS_NONE) {
         // see note [1] above
466
         xSnprintf(buffer, n, "B%1d ", (int) (this->nice + 20) / 5);
467
      } else if (klass == IOPRIO_CLASS_BE) {
468
         xSnprintf(buffer, n, "B%1d ", IOPriority_data(lp->ioPriority));
469
470
      } else if (klass == IOPRIO_CLASS_RT) {
         attr = CRT_colors[PROCESS_HIGH_PRIORITY];
471
         xSnprintf(buffer, n, "R%1d ", IOPriority_data(lp->ioPriority));
472
      } else if (klass == IOPRIO_CLASS_IDLE) {
473
         attr = CRT_colors[PROCESS_LOW_PRIORITY]; 
474
         xSnprintf(buffer, n, "id ");
475
      } else {
476
         xSnprintf(buffer, n, "?? ");
477
478
479
      }
      break;
   }
480
   #ifdef HAVE_DELAYACCT
481
482
483
   case PERCENT_CPU_DELAY:  Process_printPercentage(lp->cpu_delay_percent,    buffer, n, &attr); break;
   case PERCENT_IO_DELAY:   Process_printPercentage(lp->blkio_delay_percent,  buffer, n, &attr); break;
   case PERCENT_SWAP_DELAY: Process_printPercentage(lp->swapin_delay_percent, buffer, n, &attr); break;
484
   #endif
485
   #ifdef HAVE_PERFCOUNTERS
486
487
488
489
490
491
492
493
494
   case PERCENT_MISS:  Process_printPercentage(lp->pMiss,  buffer, n, &attr); break;
   case PERCENT_BMISS: Process_printPercentage(lp->pBMiss, buffer, n, &attr); break;
   case IPC:            LinuxProcess_printPerfCounter(lp->ipc,    0, buffer, n, &attr); break;
   case MCYCLE:         LinuxProcess_printPerfCounter(lp->mcycle, 8, buffer, n, &attr); break;
   case MINSTR:         LinuxProcess_printPerfCounter(lp->minstr, 8, buffer, n, &attr); break;
   case L1DREADS:       LinuxProcess_printPerfCounter(lp->l1dr,   9, buffer, n, &attr); break;
   case L1DRMISSES:     LinuxProcess_printPerfCounter(lp->l1drm,  9, buffer, n, &attr); break;
   case L1DWRITES:      LinuxProcess_printPerfCounter(lp->l1dw,   9, buffer, n, &attr); break;
   case L1DWMISSES:     LinuxProcess_printPerfCounter(lp->l1dwm,  9, buffer, n, &attr); break;
495
   #endif
496
   default:
497
      Process_writeField((Process*)this, str, field);
Hisham Muhammad's avatar
Hisham Muhammad committed
498
      return;
499
500
501
502
   }
   RichString_append(str, attr, buffer);
}

503
504
#define COMPARE_FIELD(_f) return (p2->_f > p1->_f ? 1 : -1)

505
506
long LinuxProcess_compare(const void* v1, const void* v2) {
   LinuxProcess *p1, *p2;
Hisham Muhammad's avatar
Hisham Muhammad committed
507
   Settings *settings = ((Process*)v1)->settings;
508
   if (settings->ss->direction == 1) {
509
510
511
512
513
514
      p1 = (LinuxProcess*)v1;
      p2 = (LinuxProcess*)v2;
   } else {
      p2 = (LinuxProcess*)v1;
      p1 = (LinuxProcess*)v2;
   }
515
   long long diff;
516
   switch ((int)settings->ss->sortKey) {
517
518
519
520
521
522
523
524
525
526
527
528
529
   case M_DRS:
      return (p2->m_drs - p1->m_drs);
   case M_DT:
      return (p2->m_dt - p1->m_dt);
   case M_LRS:
      return (p2->m_lrs - p1->m_lrs);
   case M_TRS:
      return (p2->m_trs - p1->m_trs);
   case M_SHARE:
      return (p2->m_share - p1->m_share);
   case UTIME:  diff = p2->utime - p1->utime; goto test_diff;
   case CUTIME: diff = p2->cutime - p1->cutime; goto test_diff;
   case STIME:  diff = p2->stime - p1->stime; goto test_diff;
Maks Naumov's avatar
Maks Naumov committed
530
   case CSTIME: diff = p2->cstime - p1->cstime; goto test_diff;
531
532
533
534
535
536
537
538
539
540
541
542
543
544
   #ifdef HAVE_TASKSTATS
   case RCHAR:  diff = p2->io_rchar - p1->io_rchar; goto test_diff;
   case WCHAR:  diff = p2->io_wchar - p1->io_wchar; goto test_diff;
   case SYSCR:  diff = p2->io_syscr - p1->io_syscr; goto test_diff;
   case SYSCW:  diff = p2->io_syscw - p1->io_syscw; goto test_diff;
   case RBYTES: diff = p2->io_read_bytes - p1->io_read_bytes; goto test_diff;
   case WBYTES: diff = p2->io_write_bytes - p1->io_write_bytes; goto test_diff;
   case CNCLWB: diff = p2->io_cancelled_write_bytes - p1->io_cancelled_write_bytes; goto test_diff;
   case IO_READ_RATE:  diff = p2->io_rate_read_bps - p1->io_rate_read_bps; goto test_diff;
   case IO_WRITE_RATE: diff = p2->io_rate_write_bps - p1->io_rate_write_bps; goto test_diff;
   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;
   #endif
   #ifdef HAVE_OPENVZ
   case CTID:
545
      return (p2->ctid - p1->ctid);
546
   case VPID:
547
      return (p2->vpid - p1->vpid);
548
549
550
   #endif
   #ifdef HAVE_VSERVER
   case VXID:
551
      return (p2->vxid - p1->vxid);
552
553
554
555
556
557
   #endif
   #ifdef HAVE_CGROUP
   case CGROUP:
      return strcmp(p1->cgroup ? p1->cgroup : "", p2->cgroup ? p2->cgroup : "");
   #endif
   case OOM:
558
      return (p2->oom - p1->oom);
559
   #ifdef HAVE_DELAYACCT
560
561
562
   case PERCENT_CPU_DELAY:  COMPARE_FIELD(cpu_delay_percent);
   case PERCENT_IO_DELAY:   COMPARE_FIELD(blkio_delay_percent);
   case PERCENT_SWAP_DELAY: COMPARE_FIELD(swapin_delay_percent);
563
   #endif
564
   #ifdef HAVE_PERFCOUNTERS
565
566
567
568
569
570
571
572
573
   case PERCENT_MISS:   COMPARE_FIELD(pMiss);
   case PERCENT_BMISS:  COMPARE_FIELD(pBMiss);
   case IPC:            COMPARE_FIELD(ipc);
   case MCYCLE:         COMPARE_FIELD(mcycle);
   case MINSTR:         COMPARE_FIELD(minstr);
   case L1DREADS:       COMPARE_FIELD(l1dr);
   case L1DRMISSES:     COMPARE_FIELD(l1drm);
   case L1DWRITES:      COMPARE_FIELD(l1dw);
   case L1DWMISSES:     COMPARE_FIELD(l1dwm);
574
   #endif
575
   case IO_PRIORITY:
Hisham Muhammad's avatar
Hisham Muhammad committed
576
      return LinuxProcess_effectiveIOPriority(p1) - LinuxProcess_effectiveIOPriority(p2);
577
   default:
578
      return Process_compare(v1, v2);
579
   }
580
581
582
583
584
585
   test_diff:
   return (diff > 0) ? 1 : (diff < 0 ? -1 : 0);
}

bool Process_isThread(Process* this) {
   return (Process_isUserlandThread(this) || Process_isKernelThread(this));
586
}