ProcessList.c 8.63 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
3
4
5
6
7
8
/*
htop - ProcessList.c
(C) 2004,2005 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "ProcessList.h"
9
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
10
11

#include "CRT.h"
12
#include "String.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
13
14

#include <stdlib.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
15
#include <string.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
16
17

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
18
19
20
21
22
#include "Vector.h"
#include "Hashtable.h"
#include "UsersTable.h"
#include "Panel.h"
#include "Process.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
23
#include "Settings.h"
24

Hisham Muhammad's avatar
Hisham Muhammad committed
25
#ifndef MAX_NAME
Hisham Muhammad's avatar
Hisham Muhammad committed
26
27
28
29
#define MAX_NAME 128
#endif

#ifndef MAX_READ
30
#define MAX_READ 2048
Hisham Muhammad's avatar
Hisham Muhammad committed
31
32
33
#endif

typedef struct ProcessList_ {
Hisham Muhammad's avatar
Hisham Muhammad committed
34
35
   Settings* settings;

36
37
   Vector* processes;
   Vector* processes2;
Hisham Muhammad's avatar
Hisham Muhammad committed
38
39
40
   Hashtable* processTable;
   UsersTable* usersTable;

41
   Panel* panel;
42
   int following;
43
44
   uid_t userId;
   const char* incFilter;
45
   Hashtable* pidWhiteList;
46

47
   #ifdef HAVE_LIBHWLOC
48
49
50
   hwloc_topology_t topology;
   bool topologyOk;
   #endif
Hisham Muhammad's avatar
Hisham Muhammad committed
51

52
53
54
55
56
57
58
59
60
61
   unsigned long long int totalMem;
   unsigned long long int usedMem;
   unsigned long long int freeMem;
   unsigned long long int sharedMem;
   unsigned long long int buffersMem;
   unsigned long long int cachedMem;
   unsigned long long int totalSwap;
   unsigned long long int usedSwap;
   unsigned long long int freeSwap;

Hisham Muhammad's avatar
Hisham Muhammad committed
62
   int cpuCount;
Hisham Muhammad's avatar
Hisham Muhammad committed
63
64

} ProcessList;
65

Hisham Muhammad's avatar
Hisham Muhammad committed
66
ProcessList* ProcessList_new(UsersTable* ut, Hashtable* pidWhiteList, uid_t userId);
67
void ProcessList_delete(ProcessList* pl);
68
69
void ProcessList_scan(ProcessList* pl);

Hisham Muhammad's avatar
Hisham Muhammad committed
70
71
}*/

Hisham Muhammad's avatar
Hisham Muhammad committed
72
ProcessList* ProcessList_init(ProcessList* this, UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
73
   this->processes = Vector_new(Class(Process), true, DEFAULT_SIZE);
74
   this->processTable = Hashtable_new(140, false);
Hisham Muhammad's avatar
Hisham Muhammad committed
75
   this->usersTable = usersTable;
76
   this->pidWhiteList = pidWhiteList;
Hisham Muhammad's avatar
Hisham Muhammad committed
77
   this->userId = userId;
Hisham Muhammad's avatar
Hisham Muhammad committed
78
   
79
   // tree-view auxiliary buffers
80
   this->processes2 = Vector_new(Class(Process), true, DEFAULT_SIZE);
Hisham Muhammad's avatar
Hisham Muhammad committed
81
   
82
83
   // set later by platform-specific code
   this->cpuCount = 0;
84

85
#ifdef HAVE_LIBHWLOC
86
87
88
89
   this->topologyOk = false;
   int topoErr = hwloc_topology_init(&this->topology);
   if (topoErr == 0) {
      topoErr = hwloc_topology_load(this->topology);
Hisham Muhammad's avatar
Hisham Muhammad committed
90
91
   }
   if (topoErr == 0) {
92
93
94
      this->topologyOk = true;
   }
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
95

96
   this->following = -1;
Hisham Muhammad's avatar
Hisham Muhammad committed
97
98
99
100

   return this;
}

101
void ProcessList_done(ProcessList* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
102
   Hashtable_delete(this->processTable);
103
104
   Vector_delete(this->processes);
   Vector_delete(this->processes2);
Hisham Muhammad's avatar
Hisham Muhammad committed
105
106
}

107
108
109
110
void ProcessList_setPanel(ProcessList* this, Panel* panel) {
   this->panel = panel;
}

111
112
void ProcessList_printHeader(ProcessList* this, RichString* header) {
   RichString_prune(header);
Hisham Muhammad's avatar
Hisham Muhammad committed
113
   ProcessField* fields = this->settings->fields;
Hisham Muhammad's avatar
Hisham Muhammad committed
114
   for (int i = 0; fields[i]; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
115
116
117
      const char* field = Process_fields[fields[i]].title;
      if (!field) field = "- ";
      if (!this->settings->treeView && this->settings->sortKey == fields[i])
118
         RichString_append(header, CRT_colors[PANEL_HIGHLIGHT_FOCUS], field);
Hisham Muhammad's avatar
Hisham Muhammad committed
119
      else
120
         RichString_append(header, CRT_colors[PANEL_HEADER_FOCUS], field);
Hisham Muhammad's avatar
Hisham Muhammad committed
121
122
123
   }
}

124
void ProcessList_add(ProcessList* this, Process* p) {
125
126
   assert(Vector_indexOf(this->processes, p, Process_pidCompare) == -1);
   assert(Hashtable_get(this->processTable, p->pid) == NULL);
127
   
128
   Vector_add(this->processes, p);
Hisham Muhammad's avatar
Hisham Muhammad committed
129
   Hashtable_put(this->processTable, p->pid, p);
130
   
131
132
   assert(Vector_indexOf(this->processes, p, Process_pidCompare) != -1);
   assert(Hashtable_get(this->processTable, p->pid) != NULL);
133
   assert(Hashtable_count(this->processTable) == Vector_count(this->processes));
Hisham Muhammad's avatar
Hisham Muhammad committed
134
135
}

136
void ProcessList_remove(ProcessList* this, Process* p) {
137
138
139
   assert(Vector_indexOf(this->processes, p, Process_pidCompare) != -1);
   assert(Hashtable_get(this->processTable, p->pid) != NULL);
   Process* pp = Hashtable_remove(this->processTable, p->pid);
140
   assert(pp == p); (void)pp;
141
   unsigned int pid = p->pid;
Hisham Muhammad's avatar
Hisham Muhammad committed
142
143
144
   int idx = Vector_indexOf(this->processes, p, Process_pidCompare);
   assert(idx != -1);
   if (idx >= 0) Vector_remove(this->processes, idx);
145
   assert(Hashtable_get(this->processTable, pid) == NULL); (void)pid;
146
   assert(Hashtable_count(this->processTable) == Vector_count(this->processes));
Hisham Muhammad's avatar
Hisham Muhammad committed
147
148
}

Hisham Muhammad's avatar
Hisham Muhammad committed
149
150
Process* ProcessList_get(ProcessList* this, int idx) {
   return (Process*) (Vector_get(this->processes, idx));
Hisham Muhammad's avatar
Hisham Muhammad committed
151
152
153
}

int ProcessList_size(ProcessList* this) {
154
   return (Vector_size(this->processes));
Hisham Muhammad's avatar
Hisham Muhammad committed
155
156
}

Hisham Muhammad's avatar
Hisham Muhammad committed
157
static void ProcessList_buildTree(ProcessList* this, pid_t pid, int level, int indent, int direction, bool show) {
158
   Vector* children = Vector_new(Class(Process), false, DEFAULT_SIZE);
Hisham Muhammad's avatar
Hisham Muhammad committed
159

160
   for (int i = Vector_size(this->processes) - 1; i >= 0; i--) {
161
      Process* process = (Process*) (Vector_get(this->processes, i));
162
      if (process->tgid == pid || (process->tgid == process->pid && process->ppid == pid)) {
Hisham Muhammad's avatar
Hisham Muhammad committed
163
         process = (Process*) (Vector_take(this->processes, i));
164
         Vector_add(children, process);
Hisham Muhammad's avatar
Hisham Muhammad committed
165
166
      }
   }
167
   int size = Vector_size(children);
Hisham Muhammad's avatar
Hisham Muhammad committed
168
   for (int i = 0; i < size; i++) {
169
      Process* process = (Process*) (Vector_get(children, i));
170
171
172
173
174
175
176
177
178
      if (!show)
         process->show = false;
      int s = this->processes2->items;
      if (direction == 1)
         Vector_add(this->processes2, process);
      else
         Vector_insert(this->processes2, 0, process);
      assert(this->processes2->items == s+1); (void)s;
      int nextIndent = indent | (1 << level);
179
      ProcessList_buildTree(this, process->pid, level+1, (i < size - 1) ? nextIndent : indent, direction, show ? process->showChildren : false);
180
181
182
183
      if (i == size - 1)
         process->indent = -nextIndent;
      else
         process->indent = nextIndent;
Hisham Muhammad's avatar
Hisham Muhammad committed
184
   }
185
   Vector_delete(children);
Hisham Muhammad's avatar
Hisham Muhammad committed
186
187
188
}

void ProcessList_sort(ProcessList* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
189
   if (!this->settings->treeView) {
190
      Vector_insertionSort(this->processes);
Hisham Muhammad's avatar
Hisham Muhammad committed
191
   } else {
192
      // Save settings
Hisham Muhammad's avatar
Hisham Muhammad committed
193
194
      int direction = this->settings->direction;
      int sortKey = this->settings->sortKey;
195
      // Sort by PID
Hisham Muhammad's avatar
Hisham Muhammad committed
196
197
      this->settings->sortKey = PID;
      this->settings->direction = 1;
198
      Vector_quickSort(this->processes);
199
      // Restore settings
Hisham Muhammad's avatar
Hisham Muhammad committed
200
201
      this->settings->sortKey = sortKey;
      this->settings->direction = direction;
202
      // Take PID 1 as root and add to the new listing
203
      int vsize = Vector_size(this->processes);
204
      Process* init = (Process*) (Vector_take(this->processes, 0));
205
      if (!init) return;
206
207
208
      // This assertion crashes on hardened kernels.
      // I wonder how well tree view works on those systems.
      // assert(init->pid == 1);
Hisham Muhammad's avatar
Hisham Muhammad committed
209
      init->indent = 0;
210
      Vector_add(this->processes2, init);
211
      // Recursively empty list
Hisham Muhammad's avatar
Hisham Muhammad committed
212
      ProcessList_buildTree(this, init->pid, 0, 0, direction, true);
213
      // Add leftovers
214
215
216
217
      while (Vector_size(this->processes)) {
         Process* p = (Process*) (Vector_take(this->processes, 0));
         p->indent = 0;
         Vector_add(this->processes2, p);
Hisham Muhammad's avatar
Hisham Muhammad committed
218
         ProcessList_buildTree(this, p->pid, 0, 0, direction, p->showChildren);
219
220
221
      }
      assert(Vector_size(this->processes2) == vsize); (void)vsize;
      assert(Vector_size(this->processes) == 0);
222
      // Swap listings around
223
      Vector* t = this->processes;
Hisham Muhammad's avatar
Hisham Muhammad committed
224
225
226
227
228
      this->processes = this->processes2;
      this->processes2 = t;
   }
}

229
230
231

ProcessField ProcessList_keyAt(ProcessList* this, int at) {
   int x = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
232
   ProcessField* fields = this->settings->fields;
233
234
   ProcessField field;
   for (int i = 0; (field = fields[i]); i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
235
236
237
      const char* title = Process_fields[field].title;
      if (!title) title = "- ";
      int len = strlen(title);
238
239
240
241
242
243
244
      if (at >= x && at <= x + len) {
         return field;
      }
      x += len;
   }
   return COMM;
}
245
246
247
248
249
250
251
252

void ProcessList_expandTree(ProcessList* this) {
   int size = Vector_size(this->processes);
   for (int i = 0; i < size; i++) {
      Process* process = (Process*) Vector_get(this->processes, i);
      process->showChildren = true;
   }
}
253

Hisham Muhammad's avatar
Hisham Muhammad committed
254
255
void ProcessList_rebuildPanel(ProcessList* this) {
   const char* incFilter = this->incFilter;
256
257

   int currPos = Panel_getSelectedIndex(this->panel);
Hisham Muhammad's avatar
Hisham Muhammad committed
258
   pid_t currPid = this->following != -1 ? this->following : 0;
259
260
261
262
263
264
265
266
267
268
   int currScrollV = this->panel->scrollV;

   Panel_prune(this->panel);
   int size = ProcessList_size(this);
   int idx = 0;
   for (int i = 0; i < size; i++) {
      bool hidden = false;
      Process* p = ProcessList_get(this, i);

      if ( (!p->show)
Hisham Muhammad's avatar
Hisham Muhammad committed
269
         || (this->userId != (uid_t) -1 && (p->st_uid != this->userId))
270
         || (incFilter && !(String_contains_i(p->comm, incFilter)))
271
         || (this->pidWhiteList && !Hashtable_get(this->pidWhiteList, p->pid)) )
272
273
274
275
         hidden = true;

      if (!hidden) {
         Panel_set(this->panel, idx, (Object*)p);
Hisham Muhammad's avatar
Hisham Muhammad committed
276
         if ((this->following == -1 && idx == currPos) || (this->following != -1 && p->pid == currPid)) {
277
278
279
280
281
282
283
            Panel_setSelected(this->panel, idx);
            this->panel->scrollV = currScrollV;
         }
         idx++;
      }
   }
}
284