LinuxProcess.c 13.4 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
16
#include <unistd.h>
#include <sys/syscall.h>
17
18
19

/*{

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#define PROCESS_FLAG_LINUX_IOPRIO   0x0100
#define PROCESS_FLAG_LINUX_OPENVZ   0x0200
#define PROCESS_FLAG_LINUX_VSERVER  0x0400
#define PROCESS_FLAG_LINUX_CGROUP   0x0800

typedef enum LinuxProcessFields {
   PID = 1, COMM, 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, SSIGIGNORE, 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, NLWP, TGID,
   #ifdef HAVE_OPENVZ
   CTID, VPID,
   #endif
   #ifdef HAVE_VSERVER
   VXID,
   #endif
   #ifdef HAVE_TASKSTATS
   RCHAR, WCHAR, SYSCR, SYSCW, RBYTES, WBYTES, CNCLWB, IO_READ_RATE, IO_WRITE_RATE, IO_RATE,
   #endif
   #ifdef HAVE_CGROUP
   CGROUP,
   #endif
   #ifdef HAVE_OOM
   OOM,
   #endif
   IO_PRIORITY,
   LAST_PROCESSFIELD
} LinuxProcessField;

Hisham Muhammad's avatar
Hisham Muhammad committed
50
51
#include "IOPriority.h"

52
53
54
55
56
typedef struct LinuxProcess_ {
   Process super;
   IOPriority ioPriority;
} LinuxProcess;

57
58
#define Process_delete LinuxProcess_delete

59
60
}*/

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
ProcessFieldData Process_fields[] = {
   { .name = "", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "PID", .title = "    PID ", .description = "Process/thread ID", .flags = 0, },
   { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, },
   { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, D disk, Z zombie, T traced, W paging)", .flags = 0, },
   { .name = "PPID", .title = "   PPID ", .description = "Parent process ID", .flags = 0, },
   { .name = "PGRP", .title = "   PGRP ", .description = "Process group ID", .flags = 0, },
   { .name = "SESSION", .title = "   SESN ", .description = "Process's session ID", .flags = 0, },
   { .name = "TTY_NR", .title = "  TTY ", .description = "Controlling terminal", .flags = 0, },
   { .name = "TPGID", .title = "  TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, },
   { .name = "FLAGS", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "MINFLT", .title = "     MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, },
   { .name = "CMINFLT", .title = "    CMINFLT ", .description = "Children processes' minor faults", .flags = 0, },
   { .name = "MAJFLT", .title = "     MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, },
   { .name = "CMAJFLT", .title = "    CMAJFLT ", .description = "Children processes' major faults", .flags = 0, },
   { .name = "UTIME", .title = " UTIME+  ", .description = "User CPU time - time the process spent executing in user mode", .flags = 0, },
   { .name = "STIME", .title = " STIME+  ", .description = "System CPU time - time the kernel spent running system calls for this process", .flags = 0, },
   { .name = "CUTIME", .title = " CUTIME+ ", .description = "Children processes' user CPU time", .flags = 0, },
   { .name = "CSTIME", .title = " CSTIME+ ", .description = "Children processes' system CPU time", .flags = 0, },
   { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, },
   { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, },
   { .name = "ITREALVALUE", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, },
   { .name = "VSIZE", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "RSS", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "RLIM", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "STARTCODE", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "ENDCODE", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "STARTSTACK", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "KSTKESP", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "KSTKEIP", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "SIGNAL", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "BLOCKED", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "SIGIGNORE", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "SIGCATCH", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "WCHAN", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "NSWAP", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "CNSWAP", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "EXIT_SIGNAL", .title = NULL, .description = NULL, .flags = 0, },
   { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, },
   { .name = "M_SIZE", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, },
   { .name = "M_RESIDENT", .title = "  RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, },
   { .name = "M_SHARE", .title = "  SHR ", .description = "Size of the process's shared pages", .flags = 0, },
   { .name = "M_TRS", .title = " CODE ", .description = "Size of the text segment of the process", .flags = 0, },
   { .name = "M_DRS", .title = " DATA ", .description = "Size of the data segment plus stack usage of the process", .flags = 0, },
   { .name = "M_LRS", .title = " LIB ", .description = "The library size of the process", .flags = 0, },
   { .name = "M_DT", .title = " DIRTY ", .description = "Size of the dirty pages of the process", .flags = 0, },
   { .name = "ST_UID", .title = " UID ", .description = "User ID of the process owner", .flags = 0, },
   { .name = "PERCENT_CPU", .title = "CPU% ", .description = "Percentage of the CPU time the process used in the last sampling", .flags = 0, },
   { .name = "PERCENT_MEM", .title = "MEM% ", .description = "Percentage of the memory the process is using, based on resident memory size", .flags = 0, },
   { .name = "USER", .title = "USER      ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, },
   { .name = "TIME", .title = "  TIME+  ", .description = "Total time the process has spent in user and system time", .flags = 0, },
   { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, },
   { .name = "TGID", .title = "   TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, },
#ifdef HAVE_OPENVZ
   { .name = "CTID", .title = "   CTID ", .description = "OpenVZ container ID (a.k.a. virtual environment ID)", .flags = PROCESS_FLAG_LINUX_OPENVZ, },
   { .name = "VPID", .title = " VPID ", .description = "OpenVZ process ID", .flags = PROCESS_FLAG_LINUX_OPENVZ, },
#endif
#ifdef HAVE_VSERVER
   { .name = "VXID", .title = " VXID ", .description = "VServer process ID", .flags = PROCESS_FLAG_LINUX_VSERVER, },
#endif
#ifdef HAVE_TASKSTATS
   { .name = "RCHAR", .title = "    RD_CHAR ", .description = "Number of bytes the process has read", .flags = PROCESS_FLAG_IO, },
   { .name = "WCHAR", .title = "    WR_CHAR ", .description = "Number of bytes the process has written", .flags = PROCESS_FLAG_IO, },
   { .name = "SYSCR", .title = "    RD_SYSC ", .description = "Number of read(2) syscalls for the process", .flags = PROCESS_FLAG_IO, },
   { .name = "SYSCW", .title = "    WR_SYSC ", .description = "Number of write(2) syscalls for the process", .flags = PROCESS_FLAG_IO, },
   { .name = "RBYTES", .title = "  IO_RBYTES ", .description = "Bytes of read(2) I/O for the process", .flags = PROCESS_FLAG_IO, },
   { .name = "WBYTES", .title = "  IO_WBYTES ", .description = "Bytes of write(2) I/O for the process", .flags = PROCESS_FLAG_IO, },
   { .name = "CNCLWB", .title = "  IO_CANCEL ", .description = "Bytes of cancelled write(2) I/O", .flags = PROCESS_FLAG_IO, },
   { .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, },
   { .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, },
   { .name = "IO_RATE", .title = "   DISK R/W ", .description = "Total I/O rate in bytes per second", .flags = PROCESS_FLAG_IO, },
#endif
#ifdef HAVE_CGROUP
   { .name = "CGROUP", .title = "    CGROUP ", .description = "Which cgroup the process is in", .flags = PROCESS_FLAG_LINUX_CGROUP, },
#endif
#ifdef HAVE_OOM
   { .name = "OOM", .title = "    OOM ", .description = "OOM (Out-of-Memory) killer score", .flags = 0, },
#endif
   { .name = "IO_PRIORITY", .title = "IO ", .description = "I/O priority", .flags = PROCESS_FLAG_LINUX_IOPRIO, },
   { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, },
};

char* Process_pidFormat = "%7u ";
char* Process_tpgidFormat = "%7u ";

void Process_setupColumnWidths() {
   int maxPid = Platform_getMaxPid();
   if (maxPid == -1) return;
   if (maxPid > 99999) {
      Process_fields[PID].title =     "    PID ";
      Process_fields[PPID].title =    "   PPID ";
      #ifdef HAVE_OPENVZ
      Process_fields[VPID].title =    "   VPID ";
      #endif
      Process_fields[TPGID].title =   "  TPGID ";
      Process_fields[TGID].title =    "   TGID ";
      Process_fields[PGRP].title =    "   PGRP ";
      Process_fields[SESSION].title = "   SESN ";
      #ifdef HAVE_OOM
      Process_fields[OOM].title =     "    OOM ";
      #endif
      Process_pidFormat = "%7u ";
      Process_tpgidFormat = "%7d ";
   } else {
      Process_fields[PID].title =     "  PID ";
      Process_fields[PPID].title =    " PPID ";
      #ifdef HAVE_OPENVZ
      Process_fields[VPID].title =    " VPID ";
      #endif
      Process_fields[TPGID].title =   "TPGID ";
      Process_fields[TGID].title =    " TGID ";
      Process_fields[PGRP].title =    " PGRP ";
      Process_fields[SESSION].title = " SESN ";
      #ifdef HAVE_OOM
      Process_fields[OOM].title =     "  OOM ";
      #endif
      Process_pidFormat = "%5u ";
      Process_tpgidFormat = "%5d ";
   }
}

183
184
LinuxProcess* LinuxProcess_new(Settings* settings) {
   LinuxProcess* this = calloc(sizeof(LinuxProcess), 1);
185
   Object_setClass(this, Class(Process));
186
187
188
189
190
   Process_init(&this->super, settings);
   return this;
}

void LinuxProcess_delete(Object* cast) {
191
   LinuxProcess* this = (LinuxProcess*) cast;
192
193
194
195
   Process_done((Process*)cast);
   free(this);
}

196
197
198
199
200
201
202
203
/*
[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
204
#define LinuxProcess_effectiveIOPriority(p_) (IOPriority_class(p_->ioPriority) == IOPRIO_CLASS_NONE ? IOPriority_tuple(IOPRIO_CLASS_BE, (p_->super.nice + 20) / 5) : p_->ioPriority)
205

Hisham Muhammad's avatar
Hisham Muhammad committed
206
207
IOPriority LinuxProcess_updateIOPriority(LinuxProcess* this) {
   IOPriority ioprio = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, this->super.pid);
208
209
210
211
   this->ioPriority = ioprio;
   return ioprio;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
212
213
214
bool LinuxProcess_setIOPriority(LinuxProcess* this, IOPriority ioprio) {
   syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, this->super.pid, ioprio);
   return (LinuxProcess_updateIOPriority(this) == ioprio);
215
216
}

Hisham Muhammad's avatar
Hisham Muhammad committed
217
218
void Process_writeField(Process* this, RichString* str, ProcessField field) {
   LinuxProcess* lp = (LinuxProcess*) this;
Hisham Muhammad's avatar
Hisham Muhammad committed
219
220
221
   char buffer[256]; buffer[255] = '\0';
   int attr = CRT_colors[DEFAULT_COLOR];
   int n = sizeof(buffer) - 1;
222
223
   switch (field) {
   case IO_PRIORITY: {
Hisham Muhammad's avatar
Hisham Muhammad committed
224
      int klass = IOPriority_class(lp->ioPriority);
225
226
      if (klass == IOPRIO_CLASS_NONE) {
         // see note [1] above
Hisham Muhammad's avatar
Hisham Muhammad committed
227
         snprintf(buffer, n, "B%1d ", (int) (this->nice + 20) / 5);
228
      } else if (klass == IOPRIO_CLASS_BE) {
Hisham Muhammad's avatar
Hisham Muhammad committed
229
         snprintf(buffer, n, "B%1d ", IOPriority_data(lp->ioPriority));
230
231
      } else if (klass == IOPRIO_CLASS_RT) {
         attr = CRT_colors[PROCESS_HIGH_PRIORITY];
Hisham Muhammad's avatar
Hisham Muhammad committed
232
233
         snprintf(buffer, n, "R%1d ", IOPriority_data(lp->ioPriority));
      } else if (lp->ioPriority == IOPriority_Idle) {
234
235
236
237
238
239
240
241
         attr = CRT_colors[PROCESS_LOW_PRIORITY]; 
         snprintf(buffer, n, "id ");
      } else {
         snprintf(buffer, n, "?? ");
      }
      break;
   }
   default:
Hisham Muhammad's avatar
Hisham Muhammad committed
242
243
      Process_writeDefaultField(this, str, field);
      return;
244
245
246
247
   }
   RichString_append(str, attr, buffer);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
248
long Process_compare(const void* v1, const void* v2) {
249
   LinuxProcess *p1, *p2;
Hisham Muhammad's avatar
Hisham Muhammad committed
250
251
   Settings *settings = ((Process*)v1)->settings;
   if (settings->direction == 1) {
252
253
254
255
256
257
      p1 = (LinuxProcess*)v1;
      p2 = (LinuxProcess*)v2;
   } else {
      p2 = (LinuxProcess*)v1;
      p1 = (LinuxProcess*)v2;
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
258
   switch (settings->sortKey) {
259
   case IO_PRIORITY:
Hisham Muhammad's avatar
Hisham Muhammad committed
260
      return LinuxProcess_effectiveIOPriority(p1) - LinuxProcess_effectiveIOPriority(p2);
261
   default:
Hisham Muhammad's avatar
Hisham Muhammad committed
262
      return Process_defaultCompare(v1, v2);
263
264
   }
}