blob: 8ac235d1d1bcc07cf20b450568ee5e432691b961 [file] [log] [blame]
Nathan Scott0bdade12023-05-02 09:02:22 +10001/*
2htop - Machine.c
3(C) 2023 Red Hat, Inc.
4(C) 2004,2005 Hisham H. Muhammad
5Released under the GNU GPLv2+, see the COPYING file
6in the source distribution for its full text.
7*/
8
Daniel Lange179aeb02023-11-29 17:44:20 +01009#include "config.h" // IWYU pragma: keep
10
Nathan Scott0bdade12023-05-02 09:02:22 +100011#include "Machine.h"
12
13#include <stdlib.h>
Benny Baumanne56089e2023-11-28 15:15:03 +010014#include <unistd.h>
Nathan Scott0bdade12023-05-02 09:02:22 +100015
Benny Baumanne56089e2023-11-28 15:15:03 +010016#include "Object.h"
Nathan Scott0bdade12023-05-02 09:02:22 +100017#include "Platform.h"
Nathan Scott0f751e92023-08-22 16:11:05 +100018#include "Row.h"
Nathan Scott0bdade12023-05-02 09:02:22 +100019#include "XUtils.h"
20
21
22void Machine_init(Machine* this, UsersTable* usersTable, uid_t userId) {
23 this->usersTable = usersTable;
24 this->userId = userId;
25
Nathan Scott0f751e92023-08-22 16:11:05 +100026 this->htopUserId = getuid();
27
28 // discover fixed column width limits
29 Row_setPidColumnWidth(Platform_getMaxPid());
30
Nathan Scott0bdade12023-05-02 09:02:22 +100031 // always maintain valid realtime timestamps
32 Platform_gettime_realtime(&this->realtime, &this->realtimeMs);
33
34#ifdef HAVE_LIBHWLOC
35 this->topologyOk = false;
36 if (hwloc_topology_init(&this->topology) == 0) {
37 this->topologyOk =
38 #if HWLOC_API_VERSION < 0x00020000
39 /* try to ignore the top-level machine object type */
40 0 == hwloc_topology_ignore_type_keep_structure(this->topology, HWLOC_OBJ_MACHINE) &&
41 /* ignore caches, which don't add structure */
42 0 == hwloc_topology_ignore_type_keep_structure(this->topology, HWLOC_OBJ_CORE) &&
43 0 == hwloc_topology_ignore_type_keep_structure(this->topology, HWLOC_OBJ_CACHE) &&
44 0 == hwloc_topology_set_flags(this->topology, HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM) &&
45 #else
46 0 == hwloc_topology_set_all_types_filter(this->topology, HWLOC_TYPE_FILTER_KEEP_STRUCTURE) &&
47 #endif
48 0 == hwloc_topology_load(this->topology);
49 }
50#endif
51}
52
53void Machine_done(Machine* this) {
54#ifdef HAVE_LIBHWLOC
55 if (this->topologyOk) {
56 hwloc_topology_destroy(this->topology);
57 }
Nathan Scott0bdade12023-05-02 09:02:22 +100058#endif
Sohaib Mohamed53bdcab2023-08-22 16:46:59 +100059 Object_delete(this->processTable);
60 free(this->tables);
Nathan Scott0bdade12023-05-02 09:02:22 +100061}
62
Sohaib Mohamed53bdcab2023-08-22 16:46:59 +100063static void Machine_addTable(Machine* this, Table* table) {
64 /* check that this table has not been seen previously */
65 for (size_t i = 0; i < this->tableCount; i++)
66 if (this->tables[i] == table)
67 return;
Nathan Scott0f751e92023-08-22 16:11:05 +100068
69 size_t nmemb = this->tableCount + 1;
70 Table** tables = xReallocArray(this->tables, nmemb, sizeof(Table*));
71 tables[nmemb - 1] = table;
72 this->tables = tables;
73 this->tableCount++;
74}
75
Sohaib Mohamed53bdcab2023-08-22 16:46:59 +100076void Machine_populateTablesFromSettings(Machine* this, Settings* settings, Table* processTable) {
77 this->settings = settings;
78 this->processTable = processTable;
79
80 for (size_t i = 0; i < settings->nScreens; i++) {
81 ScreenSettings* ss = settings->screens[i];
82 Table* table = ss->table;
83 if (!table)
84 table = ss->table = processTable;
85 if (i == 0)
86 this->activeTable = table;
87
88 Machine_addTable(this, table);
89 }
90}
91
92void Machine_setTablesPanel(Machine* this, Panel* panel) {
93 for (size_t i = 0; i < this->tableCount; i++) {
94 Table_setPanel(this->tables[i], panel);
95 }
96}
97
Nathan Scott0f751e92023-08-22 16:11:05 +100098void Machine_scanTables(Machine* this) {
99 // set scan timestamp
100 static bool firstScanDone = false;
101
Christian Göttscheb6168572023-08-29 13:02:50 +0200102 if (firstScanDone) {
103 this->prevMonotonicMs = this->monotonicMs;
Benny Baumann55186522024-04-05 13:07:21 +0200104 Platform_gettime_monotonic(&this->monotonicMs);
Christian Göttscheb6168572023-08-29 13:02:50 +0200105 } else {
106 this->prevMonotonicMs = 0;
Benny Baumann55186522024-04-05 13:07:21 +0200107 this->monotonicMs = 1;
Nathan Scott0f751e92023-08-22 16:11:05 +1000108 firstScanDone = true;
Christian Göttscheb6168572023-08-29 13:02:50 +0200109 }
Explorer0990ef4082025-03-10 04:50:28 +0800110 if (this->monotonicMs <= this->prevMonotonicMs) {
111 return;
112 }
Nathan Scott0f751e92023-08-22 16:11:05 +1000113
114 this->maxUserId = 0;
115 Row_resetFieldWidths();
116
117 for (size_t i = 0; i < this->tableCount; i++) {
118 Table* table = this->tables[i];
119
120 // pre-processing of each row
121 Table_scanPrepare(table);
122
123 // scan values for this table
124 Table_scanIterate(table);
125
126 // post-process after scanning
127 Table_scanCleanup(table);
128 }
129
130 Row_setUidColumnWidth(this->maxUserId);
Nathan Scott0bdade12023-05-02 09:02:22 +1000131}