| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | htop - ProcessList.c |
| 3 | (C) 2004,2005 Hisham H. Muhammad |
| Daniel Lange | 079c2ab | 2020-10-05 09:51:32 +0200 | [diff] [blame] | 4 | Released under the GNU GPLv2, see the COPYING file |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 5 | in the source distribution for its full text. |
| 6 | */ |
| 7 | |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 8 | #include "ProcessList.h" |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 9 | |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 10 | #include <assert.h> |
| Hisham Muhammad | 84281bd | 2011-12-26 21:35:57 +0000 | [diff] [blame] | 11 | #include <string.h> |
| Adam Saponara | dde71c6 | 2020-10-30 21:56:16 -0400 | [diff] [blame] | 12 | #include <time.h> |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 13 | |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 14 | #include "CRT.h" |
| 15 | #include "XUtils.h" |
| 16 | |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 17 | |
| Christian Göttsche | 08d85e6 | 2020-10-04 17:55:08 +0200 | [diff] [blame] | 18 | ProcessList* ProcessList_init(ProcessList* this, const ObjectClass* klass, UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) { |
| Hisham Muhammad | b291fba | 2015-04-09 15:40:27 -0300 | [diff] [blame] | 19 | this->processes = Vector_new(klass, true, DEFAULT_SIZE); |
| Hisham Muhammad | 7ca1081 | 2011-11-18 06:08:56 +0000 | [diff] [blame] | 20 | this->processTable = Hashtable_new(140, false); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 21 | this->usersTable = usersTable; |
| Nathan Scott | 4597332 | 2020-09-09 19:38:15 +1000 | [diff] [blame] | 22 | this->pidMatchList = pidMatchList; |
| Hisham Muhammad | 3383d8e | 2015-01-21 23:27:31 -0200 | [diff] [blame] | 23 | this->userId = userId; |
| Daniel Flanagan | dd33444 | 2019-10-31 11:39:12 -0500 | [diff] [blame] | 24 | |
| Hisham Muhammad | b291fba | 2015-04-09 15:40:27 -0300 | [diff] [blame] | 25 | // tree-view auxiliary buffer |
| 26 | this->processes2 = Vector_new(klass, true, DEFAULT_SIZE); |
| Daniel Flanagan | dd33444 | 2019-10-31 11:39:12 -0500 | [diff] [blame] | 27 | |
| Hisham Muhammad | eb229d9 | 2014-11-24 18:55:03 -0200 | [diff] [blame] | 28 | // set later by platform-specific code |
| 29 | this->cpuCount = 0; |
| Hisham Muhammad | ec17b70 | 2011-09-24 00:30:47 +0000 | [diff] [blame] | 30 | |
| Adam Saponara | dde71c6 | 2020-10-30 21:56:16 -0400 | [diff] [blame] | 31 | this->scanTs = 0; |
| Adam Saponara | dde71c6 | 2020-10-30 21:56:16 -0400 | [diff] [blame] | 32 | |
| Hisham Muhammad | bc87a8f | 2011-11-21 02:52:41 +0000 | [diff] [blame] | 33 | #ifdef HAVE_LIBHWLOC |
| Hisham Muhammad | ec17b70 | 2011-09-24 00:30:47 +0000 | [diff] [blame] | 34 | this->topologyOk = false; |
| Nathan Scott | 728b04b | 2020-08-26 10:15:00 +1000 | [diff] [blame] | 35 | if (hwloc_topology_init(&this->topology) == 0) { |
| 36 | this->topologyOk = |
| 37 | #if HWLOC_API_VERSION < 0x00020000 |
| 38 | /* try to ignore the top-level machine object type */ |
| 39 | 0 == hwloc_topology_ignore_type_keep_structure(this->topology, HWLOC_OBJ_MACHINE) && |
| 40 | /* ignore caches, which don't add structure */ |
| 41 | 0 == hwloc_topology_ignore_type_keep_structure(this->topology, HWLOC_OBJ_CORE) && |
| 42 | 0 == hwloc_topology_ignore_type_keep_structure(this->topology, HWLOC_OBJ_CACHE) && |
| 43 | 0 == hwloc_topology_set_flags(this->topology, HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM) && |
| 44 | #else |
| 45 | 0 == hwloc_topology_set_all_types_filter(this->topology, HWLOC_TYPE_FILTER_KEEP_STRUCTURE) && |
| 46 | #endif |
| 47 | 0 == hwloc_topology_load(this->topology); |
| Hisham Muhammad | ec17b70 | 2011-09-24 00:30:47 +0000 | [diff] [blame] | 48 | } |
| 49 | #endif |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 50 | |
| Hisham Muhammad | 05a78c8 | 2012-03-30 01:20:32 +0000 | [diff] [blame] | 51 | this->following = -1; |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 52 | |
| 53 | return this; |
| 54 | } |
| 55 | |
| Hisham Muhammad | cda6bdd | 2014-11-27 17:48:38 -0200 | [diff] [blame] | 56 | void ProcessList_done(ProcessList* this) { |
| Hisham | 84a69b1 | 2016-02-29 21:57:27 -0300 | [diff] [blame] | 57 | #ifdef HAVE_LIBHWLOC |
| 58 | if (this->topologyOk) { |
| 59 | hwloc_topology_destroy(this->topology); |
| 60 | } |
| 61 | #endif |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 62 | Hashtable_delete(this->processTable); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 63 | Vector_delete(this->processes); |
| 64 | Vector_delete(this->processes2); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| Hisham Muhammad | bfd86a6 | 2011-12-01 12:31:57 +0000 | [diff] [blame] | 67 | void ProcessList_setPanel(ProcessList* this, Panel* panel) { |
| 68 | this->panel = panel; |
| 69 | } |
| 70 | |
| Hisham Muhammad | d8e1480 | 2010-11-22 12:40:20 +0000 | [diff] [blame] | 71 | void ProcessList_printHeader(ProcessList* this, RichString* header) { |
| 72 | RichString_prune(header); |
| Christian Göttsche | a3bb7cb | 2020-10-21 21:26:09 +0200 | [diff] [blame] | 73 | const ProcessField* fields = this->settings->fields; |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 74 | for (int i = 0; fields[i]; i++) { |
| Hisham Muhammad | 3383d8e | 2015-01-21 23:27:31 -0200 | [diff] [blame] | 75 | const char* field = Process_fields[fields[i]].title; |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 76 | if (!field) { |
| 77 | field = "- "; |
| 78 | } |
| 79 | |
| Narendran Gopalakrishnan | e33d4d9 | 2020-11-09 18:38:51 +0100 | [diff] [blame] | 80 | int color = (!this->settings->treeView && this->settings->sortKey == fields[i]) ? |
| 81 | CRT_colors[PANEL_SELECTION_FOCUS] : CRT_colors[PANEL_HEADER_FOCUS]; |
| 82 | RichString_append(header, color, field); |
| 83 | if (COMM == fields[i] && this->settings->showMergedCommand) { |
| 84 | RichString_append(header, color, "(merged)"); |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 85 | } |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 86 | } |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| Hisham Muhammad | eb229d9 | 2014-11-24 18:55:03 -0200 | [diff] [blame] | 89 | void ProcessList_add(ProcessList* this, Process* p) { |
| Hisham Muhammad | febe259 | 2006-11-08 20:12:57 +0000 | [diff] [blame] | 90 | assert(Vector_indexOf(this->processes, p, Process_pidCompare) == -1); |
| 91 | assert(Hashtable_get(this->processTable, p->pid) == NULL); |
| Adam Saponara | dde71c6 | 2020-10-30 21:56:16 -0400 | [diff] [blame] | 92 | p->processList = this; |
| 93 | |
| Daniel Lange | 8f2d129 | 2020-11-16 12:17:28 +0100 | [diff] [blame] | 94 | // highlighting processes found in first scan by first scan marked "far in the past" |
| 95 | p->seenTs = this->scanTs; |
| Daniel Flanagan | dd33444 | 2019-10-31 11:39:12 -0500 | [diff] [blame] | 96 | |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 97 | Vector_add(this->processes, p); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 98 | Hashtable_put(this->processTable, p->pid, p); |
| Daniel Flanagan | dd33444 | 2019-10-31 11:39:12 -0500 | [diff] [blame] | 99 | |
| Hisham Muhammad | febe259 | 2006-11-08 20:12:57 +0000 | [diff] [blame] | 100 | assert(Vector_indexOf(this->processes, p, Process_pidCompare) != -1); |
| 101 | assert(Hashtable_get(this->processTable, p->pid) != NULL); |
| Hisham Muhammad | 3d62edb | 2006-11-12 21:53:56 +0000 | [diff] [blame] | 102 | assert(Hashtable_count(this->processTable) == Vector_count(this->processes)); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| Hisham Muhammad | eb229d9 | 2014-11-24 18:55:03 -0200 | [diff] [blame] | 105 | void ProcessList_remove(ProcessList* this, Process* p) { |
| Hisham Muhammad | febe259 | 2006-11-08 20:12:57 +0000 | [diff] [blame] | 106 | assert(Vector_indexOf(this->processes, p, Process_pidCompare) != -1); |
| 107 | assert(Hashtable_get(this->processTable, p->pid) != NULL); |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 108 | |
| Hisham Muhammad | febe259 | 2006-11-08 20:12:57 +0000 | [diff] [blame] | 109 | Process* pp = Hashtable_remove(this->processTable, p->pid); |
| Hisham Muhammad | 8adc7ac | 2006-11-08 21:49:52 +0000 | [diff] [blame] | 110 | assert(pp == p); (void)pp; |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 111 | |
| Hisham Muhammad | a227b20 | 2007-04-05 19:53:23 +0000 | [diff] [blame] | 112 | unsigned int pid = p->pid; |
| Hisham Muhammad | 8f23092 | 2010-02-25 01:37:31 +0000 | [diff] [blame] | 113 | int idx = Vector_indexOf(this->processes, p, Process_pidCompare); |
| 114 | assert(idx != -1); |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 115 | |
| 116 | if (idx >= 0) { |
| 117 | Vector_remove(this->processes, idx); |
| 118 | } |
| 119 | |
| Hisham Muhammad | 8adc7ac | 2006-11-08 21:49:52 +0000 | [diff] [blame] | 120 | assert(Hashtable_get(this->processTable, pid) == NULL); (void)pid; |
| Hisham Muhammad | 3d62edb | 2006-11-12 21:53:56 +0000 | [diff] [blame] | 121 | assert(Hashtable_count(this->processTable) == Vector_count(this->processes)); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| Hisham Muhammad | 8f23092 | 2010-02-25 01:37:31 +0000 | [diff] [blame] | 124 | Process* ProcessList_get(ProcessList* this, int idx) { |
| 125 | return (Process*) (Vector_get(this->processes, idx)); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | int ProcessList_size(ProcessList* this) { |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 129 | return (Vector_size(this->processes)); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| Hisham Muhammad | 9eb9121 | 2010-06-17 19:02:03 +0000 | [diff] [blame] | 132 | static void ProcessList_buildTree(ProcessList* this, pid_t pid, int level, int indent, int direction, bool show) { |
| Hisham Muhammad | 00b324b | 2012-12-05 15:12:20 +0000 | [diff] [blame] | 133 | Vector* children = Vector_new(Class(Process), false, DEFAULT_SIZE); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 134 | |
| Hisham Muhammad | febe259 | 2006-11-08 20:12:57 +0000 | [diff] [blame] | 135 | for (int i = Vector_size(this->processes) - 1; i >= 0; i--) { |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 136 | Process* process = (Process*) (Vector_get(this->processes, i)); |
| Hisham Muhammad | e3f65c8 | 2017-09-14 17:10:39 -0300 | [diff] [blame] | 137 | if (process->show && Process_isChildOf(process, pid)) { |
| Hisham Muhammad | 8f23092 | 2010-02-25 01:37:31 +0000 | [diff] [blame] | 138 | process = (Process*) (Vector_take(this->processes, i)); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 139 | Vector_add(children, process); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 142 | int size = Vector_size(children); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 143 | for (int i = 0; i < size; i++) { |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 144 | Process* process = (Process*) (Vector_get(children, i)); |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 145 | if (!show) { |
| Hisham Muhammad | d8e1480 | 2010-11-22 12:40:20 +0000 | [diff] [blame] | 146 | process->show = false; |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| Christian Göttsche | e9246ab | 2020-10-15 21:45:38 +0200 | [diff] [blame] | 149 | int s = Vector_size(this->processes2); |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 150 | if (direction == 1) { |
| Hisham Muhammad | d8e1480 | 2010-11-22 12:40:20 +0000 | [diff] [blame] | 151 | Vector_add(this->processes2, process); |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 152 | } else { |
| Hisham Muhammad | d8e1480 | 2010-11-22 12:40:20 +0000 | [diff] [blame] | 153 | Vector_insert(this->processes2, 0, process); |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| Christian Göttsche | e9246ab | 2020-10-15 21:45:38 +0200 | [diff] [blame] | 156 | assert(Vector_size(this->processes2) == s+1); (void)s; |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 157 | |
| Hisham Muhammad | d8e1480 | 2010-11-22 12:40:20 +0000 | [diff] [blame] | 158 | int nextIndent = indent | (1 << level); |
| Hisham Muhammad | 5effb12 | 2010-11-24 12:00:34 +0000 | [diff] [blame] | 159 | ProcessList_buildTree(this, process->pid, level+1, (i < size - 1) ? nextIndent : indent, direction, show ? process->showChildren : false); |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 160 | |
| 161 | if (i == size - 1) { |
| Hisham Muhammad | ca6b923 | 2011-11-03 22:12:12 +0000 | [diff] [blame] | 162 | process->indent = -nextIndent; |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 163 | } else { |
| Hisham Muhammad | ca6b923 | 2011-11-03 22:12:12 +0000 | [diff] [blame] | 164 | process->indent = nextIndent; |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 165 | } |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 166 | } |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 167 | Vector_delete(children); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| Christian Göttsche | a3bb7cb | 2020-10-21 21:26:09 +0200 | [diff] [blame] | 170 | static long ProcessList_treeProcessCompare(const void* v1, const void* v2) { |
| 171 | const Process *p1 = (const Process*)v1; |
| 172 | const Process *p2 = (const Process*)v2; |
| 173 | |
| 174 | return p1->pid - p2->pid; |
| 175 | } |
| 176 | |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 177 | void ProcessList_sort(ProcessList* this) { |
| Hisham Muhammad | 3383d8e | 2015-01-21 23:27:31 -0200 | [diff] [blame] | 178 | if (!this->settings->treeView) { |
| Hisham Muhammad | 7ca1081 | 2011-11-18 06:08:56 +0000 | [diff] [blame] | 179 | Vector_insertionSort(this->processes); |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 180 | } else { |
| Hisham Muhammad | a7c2aed | 2007-11-08 23:23:01 +0000 | [diff] [blame] | 181 | // Save settings |
| Hisham Muhammad | 3383d8e | 2015-01-21 23:27:31 -0200 | [diff] [blame] | 182 | int direction = this->settings->direction; |
| Hisham Muhammad | a7c2aed | 2007-11-08 23:23:01 +0000 | [diff] [blame] | 183 | // Sort by PID |
| Christian Göttsche | a3bb7cb | 2020-10-21 21:26:09 +0200 | [diff] [blame] | 184 | Vector_quickSortCustomCompare(this->processes, ProcessList_treeProcessCompare); |
| Hisham Muhammad | 3d62edb | 2006-11-12 21:53:56 +0000 | [diff] [blame] | 185 | int vsize = Vector_size(this->processes); |
| wangqr | 584a9bc | 2017-09-01 21:27:24 +0800 | [diff] [blame] | 186 | // Find all processes whose parent is not visible |
| 187 | int size; |
| 188 | while ((size = Vector_size(this->processes))) { |
| 189 | int i; |
| 190 | for (i = 0; i < size; i++) { |
| 191 | Process* process = (Process*)(Vector_get(this->processes, i)); |
| 192 | // Immediately consume not shown processes |
| 193 | if (!process->show) { |
| 194 | process = (Process*)(Vector_take(this->processes, i)); |
| 195 | process->indent = 0; |
| 196 | Vector_add(this->processes2, process); |
| 197 | ProcessList_buildTree(this, process->pid, 0, 0, direction, false); |
| 198 | break; |
| 199 | } |
| Hisham Muhammad | 0dbedf9 | 2018-04-05 19:31:18 -0300 | [diff] [blame] | 200 | pid_t ppid = Process_getParentPid(process); |
| wangqr | 584a9bc | 2017-09-01 21:27:24 +0800 | [diff] [blame] | 201 | // Bisect the process vector to find parent |
| 202 | int l = 0, r = size; |
| Wataru Ashihara | b34d76c | 2018-01-02 23:15:45 +0900 | [diff] [blame] | 203 | // If PID corresponds with PPID (e.g. "kernel_task" (PID:0, PPID:0) |
| 204 | // on Mac OS X 10.11.6) cancel bisecting and regard this process as |
| 205 | // root. |
| 206 | if (process->pid == ppid) |
| 207 | r = 0; |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 208 | |
| wangqr | 584a9bc | 2017-09-01 21:27:24 +0800 | [diff] [blame] | 209 | while (l < r) { |
| 210 | int c = (l + r) / 2; |
| 211 | pid_t pid = ((Process*)(Vector_get(this->processes, c)))->pid; |
| Hisham Muhammad | b9934ff | 2017-09-04 13:53:03 -0300 | [diff] [blame] | 212 | if (ppid == pid) { |
| wangqr | 584a9bc | 2017-09-01 21:27:24 +0800 | [diff] [blame] | 213 | break; |
| Hisham Muhammad | b9934ff | 2017-09-04 13:53:03 -0300 | [diff] [blame] | 214 | } else if (ppid < pid) { |
| wangqr | 584a9bc | 2017-09-01 21:27:24 +0800 | [diff] [blame] | 215 | r = c; |
| Hisham Muhammad | b9934ff | 2017-09-04 13:53:03 -0300 | [diff] [blame] | 216 | } else { |
| wangqr | 584a9bc | 2017-09-01 21:27:24 +0800 | [diff] [blame] | 217 | l = c + 1; |
| Hisham Muhammad | b9934ff | 2017-09-04 13:53:03 -0300 | [diff] [blame] | 218 | } |
| wangqr | 584a9bc | 2017-09-01 21:27:24 +0800 | [diff] [blame] | 219 | } |
| 220 | // If parent not found, then construct the tree with this root |
| 221 | if (l >= r) { |
| 222 | process = (Process*)(Vector_take(this->processes, i)); |
| 223 | process->indent = 0; |
| 224 | Vector_add(this->processes2, process); |
| 225 | ProcessList_buildTree(this, process->pid, 0, 0, direction, process->showChildren); |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | // There should be no loop in the process tree |
| 230 | assert(i < size); |
| Hisham Muhammad | 3d62edb | 2006-11-12 21:53:56 +0000 | [diff] [blame] | 231 | } |
| 232 | assert(Vector_size(this->processes2) == vsize); (void)vsize; |
| 233 | assert(Vector_size(this->processes) == 0); |
| Hisham Muhammad | a7c2aed | 2007-11-08 23:23:01 +0000 | [diff] [blame] | 234 | // Swap listings around |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 235 | Vector* t = this->processes; |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 236 | this->processes = this->processes2; |
| 237 | this->processes2 = t; |
| 238 | } |
| 239 | } |
| 240 | |
| Hisham Muhammad | 2338ad5 | 2008-03-14 18:50:49 +0000 | [diff] [blame] | 241 | |
| Christian Göttsche | 9131732 | 2020-11-04 17:46:11 +0100 | [diff] [blame] | 242 | ProcessField ProcessList_keyAt(const ProcessList* this, int at) { |
| Hisham Muhammad | 2338ad5 | 2008-03-14 18:50:49 +0000 | [diff] [blame] | 243 | int x = 0; |
| Christian Göttsche | a3bb7cb | 2020-10-21 21:26:09 +0200 | [diff] [blame] | 244 | const ProcessField* fields = this->settings->fields; |
| Hisham Muhammad | 2338ad5 | 2008-03-14 18:50:49 +0000 | [diff] [blame] | 245 | ProcessField field; |
| 246 | for (int i = 0; (field = fields[i]); i++) { |
| Hisham Muhammad | 3383d8e | 2015-01-21 23:27:31 -0200 | [diff] [blame] | 247 | const char* title = Process_fields[field].title; |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 248 | if (!title) { |
| 249 | title = "- "; |
| 250 | } |
| 251 | |
| Hisham Muhammad | 3383d8e | 2015-01-21 23:27:31 -0200 | [diff] [blame] | 252 | int len = strlen(title); |
| Hisham Muhammad | 2338ad5 | 2008-03-14 18:50:49 +0000 | [diff] [blame] | 253 | if (at >= x && at <= x + len) { |
| 254 | return field; |
| 255 | } |
| 256 | x += len; |
| 257 | } |
| 258 | return COMM; |
| 259 | } |
| Hisham Muhammad | 6486229 | 2010-08-24 23:20:38 +0000 | [diff] [blame] | 260 | |
| 261 | void ProcessList_expandTree(ProcessList* this) { |
| 262 | int size = Vector_size(this->processes); |
| 263 | for (int i = 0; i < size; i++) { |
| 264 | Process* process = (Process*) Vector_get(this->processes, i); |
| 265 | process->showChildren = true; |
| 266 | } |
| 267 | } |
| Hisham Muhammad | bfd86a6 | 2011-12-01 12:31:57 +0000 | [diff] [blame] | 268 | |
| Hisham Muhammad | 3383d8e | 2015-01-21 23:27:31 -0200 | [diff] [blame] | 269 | void ProcessList_rebuildPanel(ProcessList* this) { |
| Hisham Muhammad | 3383d8e | 2015-01-21 23:27:31 -0200 | [diff] [blame] | 270 | const char* incFilter = this->incFilter; |
| Hisham Muhammad | bfd86a6 | 2011-12-01 12:31:57 +0000 | [diff] [blame] | 271 | |
| 272 | int currPos = Panel_getSelectedIndex(this->panel); |
| Hisham Muhammad | 3383d8e | 2015-01-21 23:27:31 -0200 | [diff] [blame] | 273 | pid_t currPid = this->following != -1 ? this->following : 0; |
| Hisham Muhammad | bfd86a6 | 2011-12-01 12:31:57 +0000 | [diff] [blame] | 274 | int currScrollV = this->panel->scrollV; |
| Hisham Muhammad | bfd86a6 | 2011-12-01 12:31:57 +0000 | [diff] [blame] | 275 | |
| 276 | Panel_prune(this->panel); |
| 277 | int size = ProcessList_size(this); |
| 278 | int idx = 0; |
| 279 | for (int i = 0; i < size; i++) { |
| 280 | bool hidden = false; |
| 281 | Process* p = ProcessList_get(this, i); |
| 282 | |
| 283 | if ( (!p->show) |
| Hisham Muhammad | 3383d8e | 2015-01-21 23:27:31 -0200 | [diff] [blame] | 284 | || (this->userId != (uid_t) -1 && (p->st_uid != this->userId)) |
| Narendran Gopalakrishnan | 09fe94d | 2020-10-17 16:24:45 +0530 | [diff] [blame] | 285 | || (incFilter && !(String_contains_i(Process_getCommand(p), incFilter))) |
| Nathan Scott | 4597332 | 2020-09-09 19:38:15 +1000 | [diff] [blame] | 286 | || (this->pidMatchList && !Hashtable_get(this->pidMatchList, p->tgid)) ) |
| Hisham Muhammad | bfd86a6 | 2011-12-01 12:31:57 +0000 | [diff] [blame] | 287 | hidden = true; |
| 288 | |
| 289 | if (!hidden) { |
| 290 | Panel_set(this->panel, idx, (Object*)p); |
| Hisham Muhammad | 3383d8e | 2015-01-21 23:27:31 -0200 | [diff] [blame] | 291 | if ((this->following == -1 && idx == currPos) || (this->following != -1 && p->pid == currPid)) { |
| Hisham Muhammad | bfd86a6 | 2011-12-01 12:31:57 +0000 | [diff] [blame] | 292 | Panel_setSelected(this->panel, idx); |
| 293 | this->panel->scrollV = currScrollV; |
| 294 | } |
| 295 | idx++; |
| 296 | } |
| 297 | } |
| 298 | } |
| Hisham Muhammad | eb229d9 | 2014-11-24 18:55:03 -0200 | [diff] [blame] | 299 | |
| Hisham Muhammad | d880def | 2015-04-02 01:57:37 -0300 | [diff] [blame] | 300 | Process* ProcessList_getProcess(ProcessList* this, pid_t pid, bool* preExisting, Process_New constructor) { |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 301 | Process* proc = (Process*) Hashtable_get(this->processTable, pid); |
| 302 | *preExisting = proc; |
| 303 | if (proc) { |
| 304 | assert(Vector_indexOf(this->processes, proc, Process_pidCompare) != -1); |
| 305 | assert(proc->pid == pid); |
| 306 | } else { |
| 307 | proc = constructor(this->settings); |
| 308 | assert(proc->comm == NULL); |
| 309 | proc->pid = pid; |
| 310 | } |
| 311 | return proc; |
| 312 | } |
| 313 | |
| Christian Göttsche | 96e2a42 | 2020-10-13 16:03:37 +0200 | [diff] [blame] | 314 | void ProcessList_scan(ProcessList* this, bool pauseProcessUpdate) { |
| Adam Saponara | dde71c6 | 2020-10-30 21:56:16 -0400 | [diff] [blame] | 315 | struct timespec now; |
| Christian Göttsche | 96e2a42 | 2020-10-13 16:03:37 +0200 | [diff] [blame] | 316 | |
| 317 | // in pause mode only gather global data for meters (CPU/memory/...) |
| 318 | if (pauseProcessUpdate) { |
| 319 | ProcessList_goThroughEntries(this, true); |
| 320 | return; |
| 321 | } |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 322 | |
| 323 | // mark all process as "dirty" |
| 324 | for (int i = 0; i < Vector_size(this->processes); i++) { |
| 325 | Process* p = (Process*) Vector_get(this->processes, i); |
| 326 | p->updated = false; |
| Adam Saponara | a83f515 | 2020-10-31 20:36:53 -0400 | [diff] [blame] | 327 | p->wasShown = p->show; |
| Hisham Muhammad | c183094 | 2016-02-18 14:32:49 -0200 | [diff] [blame] | 328 | p->show = true; |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | this->totalTasks = 0; |
| 332 | this->userlandThreads = 0; |
| 333 | this->kernelThreads = 0; |
| 334 | this->runningTasks = 0; |
| 335 | |
| Adam Saponara | dde71c6 | 2020-10-30 21:56:16 -0400 | [diff] [blame] | 336 | |
| 337 | // set scanTs |
| Daniel Lange | 8f2d129 | 2020-11-16 12:17:28 +0100 | [diff] [blame] | 338 | static bool firstScanDone = false; |
| 339 | if (!firstScanDone) { |
| 340 | this->scanTs = 0; |
| 341 | firstScanDone = true; |
| 342 | } else if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) { |
| Adam Saponara | dde71c6 | 2020-10-30 21:56:16 -0400 | [diff] [blame] | 343 | this->scanTs = now.tv_sec; |
| 344 | } |
| 345 | |
| Christian Göttsche | 96e2a42 | 2020-10-13 16:03:37 +0200 | [diff] [blame] | 346 | ProcessList_goThroughEntries(this, false); |
| Daniel Flanagan | dd33444 | 2019-10-31 11:39:12 -0500 | [diff] [blame] | 347 | |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 348 | for (int i = Vector_size(this->processes) - 1; i >= 0; i--) { |
| 349 | Process* p = (Process*) Vector_get(this->processes, i); |
| Adam Saponara | dde71c6 | 2020-10-30 21:56:16 -0400 | [diff] [blame] | 350 | if (p->tombTs > 0) { |
| 351 | // remove tombed process |
| 352 | if (this->scanTs >= p->tombTs) { |
| Christian Göttsche | 4fb82e3 | 2020-11-20 17:50:30 +0100 | [diff] [blame] | 353 | ProcessList_remove(this, p); |
| Adam Saponara | dde71c6 | 2020-10-30 21:56:16 -0400 | [diff] [blame] | 354 | } |
| 355 | } else if (p->updated == false) { |
| 356 | // process no longer exists |
| Adam Saponara | a83f515 | 2020-10-31 20:36:53 -0400 | [diff] [blame] | 357 | if (this->settings->highlightChanges && p->wasShown) { |
| Adam Saponara | dde71c6 | 2020-10-30 21:56:16 -0400 | [diff] [blame] | 358 | // mark tombed |
| 359 | p->tombTs = this->scanTs + this->settings->highlightDelaySecs; |
| 360 | } else { |
| 361 | // immediately remove |
| 362 | ProcessList_remove(this, p); |
| 363 | } |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 364 | } else { |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 365 | p->updated = false; |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 366 | } |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 367 | } |
| 368 | } |