blob: ac6fc705f711396d41cbd87a08e4a67d418d2b01 [file] [log] [blame]
Hisham Muhammadd6231ba2006-03-04 18:16:49 +00001/*
Nathan Scottb74673f2023-08-31 11:56:43 +10002htop - ProcessTable.c
Hisham Muhammadd6231ba2006-03-04 18:16:49 +00003(C) 2004,2005 Hisham H. Muhammad
Daniel Lange94ad1112021-09-22 11:33:00 +02004Released under the GNU GPLv2+, see the COPYING file
Hisham Muhammadd6231ba2006-03-04 18:16:49 +00005in the source distribution for its full text.
6*/
7
Benny Baumann64905902023-12-11 10:04:39 +01008#include "config.h" // IWYU pragma: keep
9
Nathan Scottb74673f2023-08-31 11:56:43 +100010#include "ProcessTable.h"
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000011
Benny Baumann0f526292020-09-19 13:55:23 +020012#include <assert.h>
Maxim Zhiburtcf306ff2020-11-18 14:19:42 +030013#include <stdlib.h>
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000014
Maxim Zhiburtcf306ff2020-11-18 14:19:42 +030015#include "Hashtable.h"
Benny Baumanne56089e2023-11-28 15:15:03 +010016#include "Row.h"
17#include "Settings.h"
Maxim Zhiburtcf306ff2020-11-18 14:19:42 +030018#include "Vector.h"
Benny Baumann0f526292020-09-19 13:55:23 +020019
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000020
Nathan Scottb74673f2023-08-31 11:56:43 +100021void ProcessTable_init(ProcessTable* this, const ObjectClass* klass, Machine* host, Hashtable* pidMatchList) {
Nathan Scott0f751e92023-08-22 16:11:05 +100022 Table_init(&this->super, klass, host);
23
Benny Baumannd0e71cb2020-12-01 23:27:04 +010024 this->pidMatchList = pidMatchList;
Hisham Muhammadd6231ba2006-03-04 18:16:49 +000025}
26
Nathan Scottb74673f2023-08-31 11:56:43 +100027void ProcessTable_done(ProcessTable* this) {
Nathan Scott0f751e92023-08-22 16:11:05 +100028 Table_done(&this->super);
Hisham Muhammadbfd86a62011-12-01 12:31:57 +000029}
Hisham Muhammadeb229d92014-11-24 18:55:03 -020030
Nathan Scottb74673f2023-08-31 11:56:43 +100031Process* ProcessTable_getProcess(ProcessTable* this, pid_t pid, bool* preExisting, Process_New constructor) {
Nathan Scott0f751e92023-08-22 16:11:05 +100032 const Table* table = &this->super;
33 Process* proc = (Process*) Hashtable_get(table->table, pid);
Christian Göttschebd689ab2021-04-18 15:51:46 +020034 *preExisting = proc != NULL;
Hisham Muhammad272e2d92015-03-16 23:01:48 -030035 if (proc) {
Nathan Scott0f751e92023-08-22 16:11:05 +100036 assert(Vector_indexOf(table->rows, proc, Row_idEqualCompare) != -1);
37 assert(Process_getPid(proc) == pid);
Hisham Muhammad272e2d92015-03-16 23:01:48 -030038 } else {
Nathan Scott0f751e92023-08-22 16:11:05 +100039 proc = constructor(table->host);
Benny Baumann02431c42020-12-19 16:21:08 +010040 assert(proc->cmdline == NULL);
Nathan Scott0f751e92023-08-22 16:11:05 +100041 Process_setPid(proc, pid);
Hisham Muhammad272e2d92015-03-16 23:01:48 -030042 }
43 return proc;
44}
45
Nathan Scottb74673f2023-08-31 11:56:43 +100046static void ProcessTable_prepareEntries(Table* super) {
47 ProcessTable* this = (ProcessTable*) super;
Hisham Muhammad272e2d92015-03-16 23:01:48 -030048 this->totalTasks = 0;
49 this->userlandThreads = 0;
50 this->kernelThreads = 0;
51 this->runningTasks = 0;
52
Nathan Scott0f751e92023-08-22 16:11:05 +100053 Table_prepareEntries(super);
54}
Adam Saponaradde71c62020-10-30 21:56:16 -040055
Nathan Scottb74673f2023-08-31 11:56:43 +100056static void ProcessTable_iterateEntries(Table* super) {
57 ProcessTable* this = (ProcessTable*) super;
Nathan Scott0f751e92023-08-22 16:11:05 +100058 // calling into platform-specific code
Nathan Scottb74673f2023-08-31 11:56:43 +100059 ProcessTable_goThroughEntries(this);
Nathan Scott0f751e92023-08-22 16:11:05 +100060}
Daniel Flanagandd334442019-10-31 11:39:12 -050061
Nathan Scottb74673f2023-08-31 11:56:43 +100062static void ProcessTable_cleanupEntries(Table* super) {
Nathan Scott0f751e92023-08-22 16:11:05 +100063 Machine* host = super->host;
Nathan Scott0bdade12023-05-02 09:02:22 +100064 const Settings* settings = host->settings;
Nathan Scott0f751e92023-08-22 16:11:05 +100065
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 Scottb74673f2023-08-31 11:56:43 +100070 // tidy up Process state after refreshing the ProcessTable table
Nathan Scott0f751e92023-08-22 16:11:05 +100071 Process_makeCommandStr(p, settings);
Benny Baumanna61a2e62021-04-18 18:10:04 +020072
Silke Hofstra696f79f2021-08-16 22:50:36 +020073 // keep track of the highest UID for column scaling
Nathan Scott0f751e92023-08-22 16:11:05 +100074 if (p->st_uid > host->maxUserId)
75 host->maxUserId = p->st_uid;
Silke Hofstra696f79f2021-08-16 22:50:36 +020076
Nathan Scott0f751e92023-08-22 16:11:05 +100077 Table_cleanupRow(super, (Row*) p, i);
Hisham Muhammad272e2d92015-03-16 23:01:48 -030078 }
Maxim Zhiburtcf306ff2020-11-18 14:19:42 +030079
Nathan Scott0f751e92023-08-22 16:11:05 +100080 // compact the table in case of deletions
81 Table_compact(super);
Hisham Muhammad272e2d92015-03-16 23:01:48 -030082}
Nathan Scott0f751e92023-08-22 16:11:05 +100083
Nathan Scottb74673f2023-08-31 11:56:43 +100084const TableClass ProcessTable_class = {
Nathan Scott0f751e92023-08-22 16:11:05 +100085 .super = {
86 .extends = Class(Table),
Nathan Scottb74673f2023-08-31 11:56:43 +100087 .delete = ProcessTable_delete,
Nathan Scott0f751e92023-08-22 16:11:05 +100088 },
Nathan Scottb74673f2023-08-31 11:56:43 +100089 .prepare = ProcessTable_prepareEntries,
90 .iterate = ProcessTable_iterateEntries,
91 .cleanup = ProcessTable_cleanupEntries,
Nathan Scott0f751e92023-08-22 16:11:05 +100092};