| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 1 | /* |
| 2 | htop - Machine.c |
| 3 | (C) 2023 Red Hat, Inc. |
| 4 | (C) 2004,2005 Hisham H. Muhammad |
| 5 | Released under the GNU GPLv2+, see the COPYING file |
| 6 | in the source distribution for its full text. |
| 7 | */ |
| 8 | |
| Daniel Lange | 179aeb0 | 2023-11-29 17:44:20 +0100 | [diff] [blame] | 9 | #include "config.h" // IWYU pragma: keep |
| 10 | |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 11 | #include "Machine.h" |
| 12 | |
| 13 | #include <stdlib.h> |
| Benny Baumann | e56089e | 2023-11-28 15:15:03 +0100 | [diff] [blame] | 14 | #include <unistd.h> |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 15 | |
| Benny Baumann | e56089e | 2023-11-28 15:15:03 +0100 | [diff] [blame] | 16 | #include "Object.h" |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 17 | #include "Platform.h" |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 18 | #include "Row.h" |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 19 | #include "XUtils.h" |
| 20 | |
| 21 | |
| 22 | void Machine_init(Machine* this, UsersTable* usersTable, uid_t userId) { |
| 23 | this->usersTable = usersTable; |
| 24 | this->userId = userId; |
| 25 | |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 26 | this->htopUserId = getuid(); |
| 27 | |
| 28 | // discover fixed column width limits |
| 29 | Row_setPidColumnWidth(Platform_getMaxPid()); |
| 30 | |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 31 | // 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 | |
| 53 | void Machine_done(Machine* this) { |
| 54 | #ifdef HAVE_LIBHWLOC |
| 55 | if (this->topologyOk) { |
| 56 | hwloc_topology_destroy(this->topology); |
| 57 | } |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 58 | #endif |
| Sohaib Mohamed | 53bdcab | 2023-08-22 16:46:59 +1000 | [diff] [blame] | 59 | Object_delete(this->processTable); |
| 60 | free(this->tables); |
| Nathan Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 61 | } |
| 62 | |
| Sohaib Mohamed | 53bdcab | 2023-08-22 16:46:59 +1000 | [diff] [blame] | 63 | static 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 Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 68 | |
| 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 Mohamed | 53bdcab | 2023-08-22 16:46:59 +1000 | [diff] [blame] | 76 | void 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 | |
| 92 | void 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 Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 98 | void Machine_scanTables(Machine* this) { |
| 99 | // set scan timestamp |
| 100 | static bool firstScanDone = false; |
| 101 | |
| Christian Göttsche | b616857 | 2023-08-29 13:02:50 +0200 | [diff] [blame] | 102 | if (firstScanDone) { |
| 103 | this->prevMonotonicMs = this->monotonicMs; |
| Benny Baumann | 5518652 | 2024-04-05 13:07:21 +0200 | [diff] [blame] | 104 | Platform_gettime_monotonic(&this->monotonicMs); |
| Christian Göttsche | b616857 | 2023-08-29 13:02:50 +0200 | [diff] [blame] | 105 | } else { |
| 106 | this->prevMonotonicMs = 0; |
| Benny Baumann | 5518652 | 2024-04-05 13:07:21 +0200 | [diff] [blame] | 107 | this->monotonicMs = 1; |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 108 | firstScanDone = true; |
| Christian Göttsche | b616857 | 2023-08-29 13:02:50 +0200 | [diff] [blame] | 109 | } |
| Explorer09 | 90ef408 | 2025-03-10 04:50:28 +0800 | [diff] [blame] | 110 | if (this->monotonicMs <= this->prevMonotonicMs) { |
| 111 | return; |
| 112 | } |
| Nathan Scott | 0f751e9 | 2023-08-22 16:11:05 +1000 | [diff] [blame] | 113 | |
| 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 Scott | 0bdade1 | 2023-05-02 09:02:22 +1000 | [diff] [blame] | 131 | } |