ProcessList.c 8.31 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

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

} ProcessList;
55

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

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

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

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

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

   return this;
}

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

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

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

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

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

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

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

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

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

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

219
220
221

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

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

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

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

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