| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 1 | /* |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 2 | htop - ProcessTable.c |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 3 | (C) 2004,2005 Hisham H. Muhammad |
| Daniel Lange | 94ad111 | 2021-09-22 11:33:00 +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 | |
| Benny Baumann | 6490590 | 2023-12-11 10:04:39 +0100 | [diff] [blame] | 8 | #include "config.h" // IWYU pragma: keep |
| 9 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 10 | #include "ProcessTable.h" |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 11 | |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 12 | #include <assert.h> |
| Maxim Zhiburt | cf306ff | 2020-11-18 14:19:42 +0300 | [diff] [blame] | 13 | #include <stdlib.h> |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 14 | |
| Maxim Zhiburt | cf306ff | 2020-11-18 14:19:42 +0300 | [diff] [blame] | 15 | #include "Hashtable.h" |
| Benny Baumann | e56089e | 2023-11-28 15:15:03 +0100 | [diff] [blame] | 16 | #include "Row.h" |
| 17 | #include "Settings.h" |
| Maxim Zhiburt | cf306ff | 2020-11-18 14:19:42 +0300 | [diff] [blame] | 18 | #include "Vector.h" |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 19 | |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 20 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 21 | void ProcessTable_init(ProcessTable* this, const ObjectClass* klass, Machine* host, Hashtable* pidMatchList) { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 22 | Table_init(&this->super, klass, host); |
| 23 | |
| Benny Baumann | d0e71cb | 2020-12-01 23:27:04 +0100 | [diff] [blame] | 24 | this->pidMatchList = pidMatchList; |
| Hisham Muhammad | d6231ba | 2006-03-04 18:16:49 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 27 | void ProcessTable_done(ProcessTable* this) { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 28 | Table_done(&this->super); |
| Hisham Muhammad | bfd86a6 | 2011-12-01 12:31:57 +0000 | [diff] [blame] | 29 | } |
| Hisham Muhammad | eb229d9 | 2014-11-24 18:55:03 -0200 | [diff] [blame] | 30 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 31 | Process* ProcessTable_getProcess(ProcessTable* this, pid_t pid, bool* preExisting, Process_New constructor) { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 32 | const Table* table = &this->super; |
| 33 | Process* proc = (Process*) Hashtable_get(table->table, pid); |
| Christian Göttsche | bd689ab | 2021-04-18 15:51:46 +0200 | [diff] [blame] | 34 | *preExisting = proc != NULL; |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 35 | if (proc) { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 36 | assert(Vector_indexOf(table->rows, proc, Row_idEqualCompare) != -1); |
| 37 | assert(Process_getPid(proc) == pid); |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 38 | } else { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 39 | proc = constructor(table->host); |
| Benny Baumann | 02431c4 | 2020-12-19 16:21:08 +0100 | [diff] [blame] | 40 | assert(proc->cmdline == NULL); |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 41 | Process_setPid(proc, pid); |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 42 | } |
| 43 | return proc; |
| 44 | } |
| 45 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 46 | static void ProcessTable_prepareEntries(Table* super) { |
| 47 | ProcessTable* this = (ProcessTable*) super; |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 48 | this->totalTasks = 0; |
| 49 | this->userlandThreads = 0; |
| 50 | this->kernelThreads = 0; |
| 51 | this->runningTasks = 0; |
| 52 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 53 | Table_prepareEntries(super); |
| 54 | } |
| Adam Saponara | dde71c6 | 2020-10-30 21:56:16 -0400 | [diff] [blame] | 55 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 56 | static void ProcessTable_iterateEntries(Table* super) { |
| 57 | ProcessTable* this = (ProcessTable*) super; |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 58 | // calling into platform-specific code |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 59 | ProcessTable_goThroughEntries(this); |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 60 | } |
| Daniel Flanagan | dd33444 | 2019-10-31 11:39:12 -0500 | [diff] [blame] | 61 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 62 | static void ProcessTable_cleanupEntries(Table* super) { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 63 | Machine* host = super->host; |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 64 | const Settings* settings = host->settings; |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 65 | |
| 66 | // Finish process table update, culling any exit'd processes |
| 67 | for (int i = Vector_size(super->rows) - 1; i >= 0; i--) { |
| 68 | Process* p = (Process*) Vector_get(super->rows, i); |
| 69 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 70 | // tidy up Process state after refreshing the ProcessTable table |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 71 | Process_makeCommandStr(p, settings); |
| Benny Baumann | a61a2e6 | 2021-04-18 18:10:04 +0200 | [diff] [blame] | 72 | |
| Silke Hofstra | 696f79f | 2021-08-16 22:50:36 +0200 | [diff] [blame] | 73 | // keep track of the highest UID for column scaling |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 74 | if (p->st_uid > host->maxUserId) |
| 75 | host->maxUserId = p->st_uid; |
| Silke Hofstra | 696f79f | 2021-08-16 22:50:36 +0200 | [diff] [blame] | 76 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 77 | Table_cleanupRow(super, (Row*) p, i); |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 78 | } |
| Maxim Zhiburt | cf306ff | 2020-11-18 14:19:42 +0300 | [diff] [blame] | 79 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 80 | // compact the table in case of deletions |
| 81 | Table_compact(super); |
| Hisham Muhammad | 272e2d9 | 2015-03-16 23:01:48 -0300 | [diff] [blame] | 82 | } |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 83 | |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 84 | const TableClass ProcessTable_class = { |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 85 | .super = { |
| 86 | .extends = Class(Table), |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 87 | .delete = ProcessTable_delete, |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 88 | }, |
| Nathan Scott | b74673f | 2023-08-31 11:56:43 +1000 | [diff] [blame] | 89 | .prepare = ProcessTable_prepareEntries, |
| 90 | .iterate = ProcessTable_iterateEntries, |
| 91 | .cleanup = ProcessTable_cleanupEntries, |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 92 | }; |