ProcessList.c 8.29 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"
Hisham Muhammad's avatar
Hisham Muhammad committed
9
10

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

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

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

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

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

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

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

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

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

   int cpuCount;
Hisham Muhammad's avatar
Hisham Muhammad committed
52
53

} ProcessList;
54

Hisham Muhammad's avatar
Hisham Muhammad committed
55
ProcessList* ProcessList_new(UsersTable* ut, Hashtable* pidWhiteList, uid_t userId);
56
void ProcessList_delete(ProcessList* pl);
57
58
void ProcessList_scan(ProcessList* pl);

Hisham Muhammad's avatar
Hisham Muhammad committed
59
60
}*/

Hisham Muhammad's avatar
Hisham Muhammad committed
61
ProcessList* ProcessList_init(ProcessList* this, UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
62
   this->processes = Vector_new(Class(Process), true, DEFAULT_SIZE);
63
   this->processTable = Hashtable_new(140, false);
Hisham Muhammad's avatar
Hisham Muhammad committed
64
   this->usersTable = usersTable;
65
   this->pidWhiteList = pidWhiteList;
Hisham Muhammad's avatar
Hisham Muhammad committed
66
   this->userId = userId;
Hisham Muhammad's avatar
Hisham Muhammad committed
67
   
68
   // tree-view auxiliary buffers
69
   this->processes2 = Vector_new(Class(Process), true, DEFAULT_SIZE);
Hisham Muhammad's avatar
Hisham Muhammad committed
70
   
71
72
   // set later by platform-specific code
   this->cpuCount = 0;
73

74
#ifdef HAVE_LIBHWLOC
75
76
77
78
   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
79
80
   }
   if (topoErr == 0) {
81
82
83
      this->topologyOk = true;
   }
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
84

85
   this->following = -1;
Hisham Muhammad's avatar
Hisham Muhammad committed
86
87
88
89

   return this;
}

90
void ProcessList_done(ProcessList* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
91
   Hashtable_delete(this->processTable);
92
93
   Vector_delete(this->processes);
   Vector_delete(this->processes2);
Hisham Muhammad's avatar
Hisham Muhammad committed
94
95
}

96
97
98
99
void ProcessList_setPanel(ProcessList* this, Panel* panel) {
   this->panel = panel;
}

100
101
void ProcessList_printHeader(ProcessList* this, RichString* header) {
   RichString_prune(header);
Hisham Muhammad's avatar
Hisham Muhammad committed
102
   ProcessField* fields = this->settings->fields;
Hisham Muhammad's avatar
Hisham Muhammad committed
103
   for (int i = 0; fields[i]; i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
104
105
106
      const char* field = Process_fields[fields[i]].title;
      if (!field) field = "- ";
      if (!this->settings->treeView && this->settings->sortKey == fields[i])
107
         RichString_append(header, CRT_colors[PANEL_HIGHLIGHT_FOCUS], field);
Hisham Muhammad's avatar
Hisham Muhammad committed
108
      else
109
         RichString_append(header, CRT_colors[PANEL_HEADER_FOCUS], field);
Hisham Muhammad's avatar
Hisham Muhammad committed
110
111
112
   }
}

113
void ProcessList_add(ProcessList* this, Process* p) {
114
115
   assert(Vector_indexOf(this->processes, p, Process_pidCompare) == -1);
   assert(Hashtable_get(this->processTable, p->pid) == NULL);
116
   
117
   Vector_add(this->processes, p);
Hisham Muhammad's avatar
Hisham Muhammad committed
118
   Hashtable_put(this->processTable, p->pid, p);
119
   
120
121
   assert(Vector_indexOf(this->processes, p, Process_pidCompare) != -1);
   assert(Hashtable_get(this->processTable, p->pid) != NULL);
122
   assert(Hashtable_count(this->processTable) == Vector_count(this->processes));
Hisham Muhammad's avatar
Hisham Muhammad committed
123
124
}

125
void ProcessList_remove(ProcessList* this, Process* p) {
126
127
128
   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);
129
   assert(pp == p); (void)pp;
130
   unsigned int pid = p->pid;
Hisham Muhammad's avatar
Hisham Muhammad committed
131
132
133
   int idx = Vector_indexOf(this->processes, p, Process_pidCompare);
   assert(idx != -1);
   if (idx >= 0) Vector_remove(this->processes, idx);
134
   assert(Hashtable_get(this->processTable, pid) == NULL); (void)pid;
135
   assert(Hashtable_count(this->processTable) == Vector_count(this->processes));
Hisham Muhammad's avatar
Hisham Muhammad committed
136
137
}

Hisham Muhammad's avatar
Hisham Muhammad committed
138
139
Process* ProcessList_get(ProcessList* this, int idx) {
   return (Process*) (Vector_get(this->processes, idx));
Hisham Muhammad's avatar
Hisham Muhammad committed
140
141
142
}

int ProcessList_size(ProcessList* this) {
143
   return (Vector_size(this->processes));
Hisham Muhammad's avatar
Hisham Muhammad committed
144
145
}

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

149
   for (int i = Vector_size(this->processes) - 1; i >= 0; i--) {
150
      Process* process = (Process*) (Vector_get(this->processes, i));
151
      if (process->tgid == pid || (process->tgid == process->pid && process->ppid == pid)) {
Hisham Muhammad's avatar
Hisham Muhammad committed
152
         process = (Process*) (Vector_take(this->processes, i));
153
         Vector_add(children, process);
Hisham Muhammad's avatar
Hisham Muhammad committed
154
155
      }
   }
156
   int size = Vector_size(children);
Hisham Muhammad's avatar
Hisham Muhammad committed
157
   for (int i = 0; i < size; i++) {
158
      Process* process = (Process*) (Vector_get(children, i));
159
160
161
162
163
164
165
166
167
      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);
168
      ProcessList_buildTree(this, process->pid, level+1, (i < size - 1) ? nextIndent : indent, direction, show ? process->showChildren : false);
169
170
171
172
      if (i == size - 1)
         process->indent = -nextIndent;
      else
         process->indent = nextIndent;
Hisham Muhammad's avatar
Hisham Muhammad committed
173
   }
174
   Vector_delete(children);
Hisham Muhammad's avatar
Hisham Muhammad committed
175
176
177
}

void ProcessList_sort(ProcessList* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
178
   if (!this->settings->treeView) {
179
      Vector_insertionSort(this->processes);
Hisham Muhammad's avatar
Hisham Muhammad committed
180
   } else {
181
      // Save settings
Hisham Muhammad's avatar
Hisham Muhammad committed
182
183
      int direction = this->settings->direction;
      int sortKey = this->settings->sortKey;
184
      // Sort by PID
Hisham Muhammad's avatar
Hisham Muhammad committed
185
186
      this->settings->sortKey = PID;
      this->settings->direction = 1;
187
      Vector_quickSort(this->processes);
188
      // Restore settings
Hisham Muhammad's avatar
Hisham Muhammad committed
189
190
      this->settings->sortKey = sortKey;
      this->settings->direction = direction;
191
      // Take PID 1 as root and add to the new listing
192
      int vsize = Vector_size(this->processes);
193
      Process* init = (Process*) (Vector_take(this->processes, 0));
194
      if (!init) return;
195
196
197
      // 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
198
      init->indent = 0;
199
      Vector_add(this->processes2, init);
200
      // Recursively empty list
Hisham Muhammad's avatar
Hisham Muhammad committed
201
      ProcessList_buildTree(this, init->pid, 0, 0, direction, true);
202
      // Add leftovers
203
204
205
206
      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
207
         ProcessList_buildTree(this, p->pid, 0, 0, direction, p->showChildren);
208
209
210
      }
      assert(Vector_size(this->processes2) == vsize); (void)vsize;
      assert(Vector_size(this->processes) == 0);
211
      // Swap listings around
212
      Vector* t = this->processes;
Hisham Muhammad's avatar
Hisham Muhammad committed
213
214
215
216
217
      this->processes = this->processes2;
      this->processes2 = t;
   }
}

218
219
220

ProcessField ProcessList_keyAt(ProcessList* this, int at) {
   int x = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
221
   ProcessField* fields = this->settings->fields;
222
223
   ProcessField field;
   for (int i = 0; (field = fields[i]); i++) {
Hisham Muhammad's avatar
Hisham Muhammad committed
224
225
226
      const char* title = Process_fields[field].title;
      if (!title) title = "- ";
      int len = strlen(title);
227
228
229
230
231
232
233
      if (at >= x && at <= x + len) {
         return field;
      }
      x += len;
   }
   return COMM;
}
234
235
236
237
238
239
240
241

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;
   }
}
242

Hisham Muhammad's avatar
Hisham Muhammad committed
243
244
void ProcessList_rebuildPanel(ProcessList* this) {
   const char* incFilter = this->incFilter;
245
246

   int currPos = Panel_getSelectedIndex(this->panel);
Hisham Muhammad's avatar
Hisham Muhammad committed
247
   pid_t currPid = this->following != -1 ? this->following : 0;
248
249
250
251
252
253
254
255
256
257
   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
258
         || (this->userId != (uid_t) -1 && (p->st_uid != this->userId))
259
         || (incFilter && !(String_contains_i(p->comm, incFilter)))
260
         || (this->pidWhiteList && !Hashtable_get(this->pidWhiteList, p->pid)) )
261
262
263
264
         hidden = true;

      if (!hidden) {
         Panel_set(this->panel, idx, (Object*)p);
Hisham Muhammad's avatar
Hisham Muhammad committed
265
         if ((this->following == -1 && idx == currPos) || (this->following != -1 && p->pid == currPid)) {
266
267
268
269
270
271
272
            Panel_setSelected(this->panel, idx);
            this->panel->scrollV = currScrollV;
         }
         idx++;
      }
   }
}
273