diff options
author | Nathan Scott <nathans@redhat.com> | 2023-05-02 09:02:22 +1000 |
---|---|---|
committer | Nathan Scott <nathans@redhat.com> | 2023-05-08 13:06:07 +1000 |
commit | 0bdade1b6cb40c5bd374a93ac0489058a7421bb5 (patch) | |
tree | 0e0225f7dbf6867402c5ed3481a705d01941f42e | |
parent | e4ebe18b67c366d367231a1123b057c82018cf5b (diff) |
Introduce Machine class for host-specific info (split from ProcessList)
First stage in sanitizing the process list structure so that htop
can support other types of lists too (cgroups, filesystems, ...),
in the not-too-distant future.
This introduces struct Machine for system-wide information while
keeping process-list information in ProcessList (now much less).
Next step is to propogate this separation into each platform, to
match these core changes.
81 files changed, 989 insertions, 718 deletions
@@ -27,6 +27,7 @@ in the source distribution for its full text. #include "MainPanel.h" #include "OpenFilesScreen.h" #include "Process.h" +#include "ProcessList.h" #include "ProcessLocksScreen.h" #include "ProvideCurses.h" #include "Scheduling.h" @@ -45,9 +46,10 @@ in the source distribution for its full text. Object* Action_pickFromVector(State* st, Panel* list, int x, bool followProcess) { MainPanel* mainPanel = st->mainPanel; Header* header = st->header; + Machine* host = st->host; int y = ((Panel*)mainPanel)->y; - ScreenManager* scr = ScreenManager_new(header, st->settings, st, false); + ScreenManager* scr = ScreenManager_new(header, host, st, false); scr->allowFocusChange = false; ScreenManager_add(scr, list, x); ScreenManager_add(scr, (Panel*)mainPanel, -1); @@ -55,13 +57,13 @@ Object* Action_pickFromVector(State* st, Panel* list, int x, bool followProcess) int ch; bool unfollow = false; int pid = followProcess ? MainPanel_selectedPid(mainPanel) : -1; - if (followProcess && header->pl->following == -1) { - header->pl->following = pid; + if (followProcess && host->pl->following == -1) { + host->pl->following = pid; unfollow = true; } ScreenManager_run(scr, &panelFocus, &ch, NULL); if (unfollow) { - header->pl->following = -1; + host->pl->following = -1; } ScreenManager_delete(scr); Panel_move((Panel*)mainPanel, 0, y); @@ -84,12 +86,13 @@ Object* Action_pickFromVector(State* st, Panel* list, int x, bool followProcess) // ---------------------------------------- static void Action_runSetup(State* st) { - ScreenManager* scr = ScreenManager_new(st->header, st->settings, st, true); - CategoriesPanel_new(scr, st->settings, st->header, st->pl); + const Settings* settings = st->host->settings; + ScreenManager* scr = ScreenManager_new(st->header, st->host, st, true); + CategoriesPanel_new(scr, st->header, st->host); ScreenManager_run(scr, NULL, NULL, "Setup"); ScreenManager_delete(scr); - if (st->settings->changed) { - CRT_setMouse(st->settings->enableMouse); + if (settings->changed) { + CRT_setMouse(settings->enableMouse); Header_writeBackToSettings(st->header); } } @@ -166,7 +169,8 @@ static Htop_Reaction actionSetSortColumn(State* st) { Htop_Reaction reaction = HTOP_OK; Panel* sortPanel = Panel_new(0, 0, 0, 0, Class(ListItem), true, FunctionBar_newEnterEsc("Sort ", "Cancel ")); Panel_setHeader(sortPanel, "Sort by"); - const Settings* settings = st->settings; + Machine* host = st->host; + Settings* settings = host->settings; const ProcessField* fields = settings->ss->fields; Hashtable* dynamicColumns = settings->dynamicColumns; for (int i = 0; fields[i]; i++) { @@ -187,74 +191,80 @@ static Htop_Reaction actionSetSortColumn(State* st) { } const ListItem* field = (const ListItem*) Action_pickFromVector(st, sortPanel, 14, false); if (field) { - reaction |= Action_setSortKey(st->settings, field->key); + reaction |= Action_setSortKey(settings, field->key); } Object_delete(sortPanel); - st->pl->needsSort = true; + host->pl->needsSort = true; return reaction | HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR; } static Htop_Reaction actionSortByPID(State* st) { - return Action_setSortKey(st->settings, PID); + return Action_setSortKey(st->host->settings, PID); } static Htop_Reaction actionSortByMemory(State* st) { - return Action_setSortKey(st->settings, PERCENT_MEM); + return Action_setSortKey(st->host->settings, PERCENT_MEM); } static Htop_Reaction actionSortByCPU(State* st) { - return Action_setSortKey(st->settings, PERCENT_CPU); + return Action_setSortKey(st->host->settings, PERCENT_CPU); } static Htop_Reaction actionSortByTime(State* st) { - return Action_setSortKey(st->settings, TIME); + return Action_setSortKey(st->host->settings, TIME); } static Htop_Reaction actionToggleKernelThreads(State* st) { - st->settings->hideKernelThreads = !st->settings->hideKernelThreads; - st->settings->lastUpdate++; + Settings* settings = st->host->settings; + settings->hideKernelThreads = !settings->hideKernelThreads; + settings->lastUpdate++; return HTOP_RECALCULATE | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING; } static Htop_Reaction actionToggleUserlandThreads(State* st) { - st->settings->hideUserlandThreads = !st->settings->hideUserlandThreads; - st->settings->lastUpdate++; + Settings* settings = st->host->settings; + settings->hideUserlandThreads = !settings->hideUserlandThreads; + settings->lastUpdate++; return HTOP_RECALCULATE | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING; } static Htop_Reaction actionToggleRunningInContainer(State* st) { - st->settings->hideRunningInContainer = !st->settings->hideRunningInContainer; - st->settings->lastUpdate++; + Settings* settings = st->host->settings; + settings->hideRunningInContainer = !settings->hideRunningInContainer; + settings->lastUpdate++; return HTOP_RECALCULATE | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING; } static Htop_Reaction actionToggleProgramPath(State* st) { - st->settings->showProgramPath = !st->settings->showProgramPath; - st->settings->lastUpdate++; + Settings* settings = st->host->settings; + settings->showProgramPath = !settings->showProgramPath; + settings->lastUpdate++; return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING; } static Htop_Reaction actionToggleMergedCommand(State* st) { - st->settings->showMergedCommand = !st->settings->showMergedCommand; - st->settings->lastUpdate++; + Settings* settings = st->host->settings; + settings->showMergedCommand = !settings->showMergedCommand; + settings->lastUpdate++; return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING | HTOP_UPDATE_PANELHDR; } static Htop_Reaction actionToggleTreeView(State* st) { - ScreenSettings* ss = st->settings->ss; + Machine* host = st->host; + ScreenSettings* ss = host->settings->ss; ss->treeView = !ss->treeView; if (!ss->allBranchesCollapsed) - ProcessList_expandTree(st->pl); + ProcessList_expandTree(host->pl); - st->pl->needsSort = true; + host->pl->needsSort = true; return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR; } @@ -265,22 +275,24 @@ static Htop_Reaction actionToggleHideMeters(State* st) { } static Htop_Reaction actionExpandOrCollapseAllBranches(State* st) { - ScreenSettings* ss = st->settings->ss; + Machine* host = st->host; + ScreenSettings* ss = host->settings->ss; if (!ss->treeView) { return HTOP_OK; } ss->allBranchesCollapsed = !ss->allBranchesCollapsed; if (ss->allBranchesCollapsed) - ProcessList_collapseAllBranches(st->pl); + ProcessList_collapseAllBranches(host->pl); else - ProcessList_expandTree(st->pl); + ProcessList_expandTree(host->pl); return HTOP_REFRESH | HTOP_SAVE_SETTINGS; } static Htop_Reaction actionIncFilter(State* st) { + Machine* host = st->host; IncSet* inc = (st->mainPanel)->inc; IncSet_activate(inc, INC_FILTER, (Panel*)st->mainPanel); - st->pl->incFilter = IncSet_filter(inc); + host->pl->incFilter = IncSet_filter(inc); return HTOP_REFRESH | HTOP_KEEP_FOLLOWING; } @@ -307,13 +319,14 @@ static Htop_Reaction actionLowerPriority(State* st) { } static Htop_Reaction actionInvertSortOrder(State* st) { - ScreenSettings_invertSortOrder(st->settings->ss); - st->pl->needsSort = true; + Machine* host = st->host; + ScreenSettings_invertSortOrder(host->settings->ss); + host->pl->needsSort = true; return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING | HTOP_UPDATE_PANELHDR; } static Htop_Reaction actionExpandOrCollapse(State* st) { - if (!st->settings->ss->treeView) + if (!st->host->settings->ss->treeView) return HTOP_OK; bool changed = expandCollapse((Panel*)st->mainPanel); @@ -321,7 +334,7 @@ static Htop_Reaction actionExpandOrCollapse(State* st) { } static Htop_Reaction actionCollapseIntoParent(State* st) { - if (!st->settings->ss->treeView) { + if (!st->host->settings->ss->treeView) { return HTOP_OK; } bool changed = collapseIntoParent((Panel*)st->mainPanel); @@ -329,11 +342,11 @@ static Htop_Reaction actionCollapseIntoParent(State* st) { } static Htop_Reaction actionExpandCollapseOrSortColumn(State* st) { - return st->settings->ss->treeView ? actionExpandOrCollapse(st) : actionSetSortColumn(st); + return st->host->settings->ss->treeView ? actionExpandOrCollapse(st) : actionSetSortColumn(st); } static Htop_Reaction actionNextScreen(State* st) { - Settings* settings = st->settings; + Settings* settings = st->host->settings; settings->ssIndex++; if (settings->ssIndex == settings->nScreens) { settings->ssIndex = 0; @@ -343,7 +356,7 @@ static Htop_Reaction actionNextScreen(State* st) { } static Htop_Reaction actionPrevScreen(State* st) { - Settings* settings = st->settings; + Settings* settings = st->host->settings; if (settings->ssIndex == 0) { settings->ssIndex = settings->nScreens - 1; } else { @@ -379,7 +392,8 @@ static Htop_Reaction actionSetAffinity(State* st) { if (Settings_isReadonly()) return HTOP_OK; - if (st->pl->activeCPUs == 1) + Machine* host = st->host; + if (host->activeCPUs == 1) return HTOP_OK; #if (defined(HAVE_LIBHWLOC) || defined(HAVE_AFFINITY)) @@ -387,17 +401,17 @@ static Htop_Reaction actionSetAffinity(State* st) { if (!p) return HTOP_OK; - Affinity* affinity1 = Affinity_get(p, st->pl); + Affinity* affinity1 = Affinity_get(p, host); if (!affinity1) return HTOP_OK; int width; - Panel* affinityPanel = AffinityPanel_new(st->pl, affinity1, &width); + Panel* affinityPanel = AffinityPanel_new(host, affinity1, &width); Affinity_delete(affinity1); const void* set = Action_pickFromVector(st, affinityPanel, width, true); if (set) { - Affinity* affinity2 = AffinityPanel_getAffinity(affinityPanel, st->pl); + Affinity* affinity2 = AffinityPanel_getAffinity(affinityPanel, host); bool ok = MainPanel_foreachProcess(st->mainPanel, Affinity_set, (Arg) { .v = affinity2 }, NULL); if (!ok) beep(); @@ -479,16 +493,17 @@ static Htop_Reaction actionKill(State* st) { static Htop_Reaction actionFilterByUser(State* st) { Panel* usersPanel = Panel_new(0, 0, 0, 0, Class(ListItem), true, FunctionBar_newEnterEsc("Show ", "Cancel ")); Panel_setHeader(usersPanel, "Show processes of:"); - UsersTable_foreach(st->ut, addUserToVector, usersPanel); + Machine* host = st->host; + UsersTable_foreach(host->usersTable, addUserToVector, usersPanel); Vector_insertionSort(usersPanel->items); ListItem* allUsers = ListItem_new("All users", -1); Panel_insert(usersPanel, 0, (Object*) allUsers); const ListItem* picked = (ListItem*) Action_pickFromVector(st, usersPanel, 19, false); if (picked) { if (picked == allUsers) { - st->pl->userId = (uid_t)-1; + host->userId = (uid_t)-1; } else { - Action_setUserOnly(ListItem_getRef(picked), &(st->pl->userId)); + Action_setUserOnly(ListItem_getRef(picked), &host->userId); } } Panel_delete((Object*)usersPanel); @@ -496,7 +511,7 @@ static Htop_Reaction actionFilterByUser(State* st) { } Htop_Reaction Action_follow(State* st) { - st->pl->following = MainPanel_selectedPid(st->mainPanel); + st->host->pl->following = MainPanel_selectedPid(st->mainPanel); Panel_setSelectionColor((Panel*)st->mainPanel, PANEL_SELECTION_FOLLOW); return HTOP_KEEP_FOLLOWING; } @@ -660,7 +675,7 @@ static Htop_Reaction actionHelp(State* st) { addbartext(CRT_colors[CPU_NICE_TEXT], "", "low"); addbartext(CRT_colors[CPU_NORMAL], "/", "normal"); addbartext(CRT_colors[CPU_SYSTEM], "/", "kernel"); - if (st->settings->detailedCPUTime) { + if (st->host->settings->detailedCPUTime) { addbartext(CRT_colors[CPU_IRQ], "/", "irq"); addbartext(CRT_colors[CPU_SOFTIRQ], "/", "soft-irq"); addbartext(CRT_colors[CPU_STEAL], "/", "steal"); @@ -13,10 +13,10 @@ in the source distribution for its full text. #include <sys/types.h> #include "Header.h" +#include "Machine.h" #include "Object.h" #include "Panel.h" #include "Process.h" -#include "ProcessList.h" #include "Settings.h" #include "UsersTable.h" @@ -36,9 +36,7 @@ typedef enum { struct MainPanel_; // IWYU pragma: keep typedef struct State_ { - Settings* settings; - UsersTable* ut; - ProcessList* pl; + Machine* host; struct MainPanel_* mainPanel; Header* header; bool pauseUpdate; @@ -47,12 +45,13 @@ typedef struct State_ { } State; static inline bool State_hideFunctionBar(const State* st) { - return st->settings->hideFunctionBar == 2 || (st->settings->hideFunctionBar == 1 && st->hideSelection); + const Settings* settings = st->host->settings; + return settings->hideFunctionBar == 2 || (settings->hideFunctionBar == 1 && st->hideSelection); } typedef Htop_Reaction (*Htop_Action)(State* st); -Object* Action_pickFromVector(State* st, Panel* list, int x, bool followProcess); +Object* Action_pickFromVector(State* st, Panel* list, int x, bool follow); bool Action_setUserOnly(const char* userName, uid_t* userId); @@ -27,11 +27,11 @@ in the source distribution for its full text. #endif -Affinity* Affinity_new(ProcessList* pl) { +Affinity* Affinity_new(Machine* host) { Affinity* this = xCalloc(1, sizeof(Affinity)); this->size = 8; this->cpus = xCalloc(this->size, sizeof(unsigned int)); - this->pl = pl; + this->host = host; return this; } @@ -52,14 +52,14 @@ void Affinity_add(Affinity* this, unsigned int id) { #if defined(HAVE_LIBHWLOC) -Affinity* Affinity_get(const Process* proc, ProcessList* pl) { +Affinity* Affinity_get(const Process* proc, Machine* host) { hwloc_cpuset_t cpuset = hwloc_bitmap_alloc(); - bool ok = (hwloc_get_proc_cpubind(pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0); + bool ok = (hwloc_get_proc_cpubind(host->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0); Affinity* affinity = NULL; if (ok) { - affinity = Affinity_new(pl); + affinity = Affinity_new(host); if (hwloc_bitmap_last(cpuset) == -1) { - for (unsigned int i = 0; i < pl->existingCPUs; i++) { + for (unsigned int i = 0; i < host->existingCPUs; i++) { Affinity_add(affinity, i); } } else { @@ -79,21 +79,21 @@ bool Affinity_set(Process* proc, Arg arg) { for (unsigned int i = 0; i < this->used; i++) { hwloc_bitmap_set(cpuset, this->cpus[i]); } - bool ok = (hwloc_set_proc_cpubind(this->pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0); + bool ok = (hwloc_set_proc_cpubind(this->host->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0); hwloc_bitmap_free(cpuset); return ok; } #elif defined(HAVE_AFFINITY) -Affinity* Affinity_get(const Process* proc, ProcessList* pl) { +Affinity* Affinity_get(const Process* proc, Machine* host) { cpu_set_t cpuset; bool ok = (sched_getaffinity(proc->pid, sizeof(cpu_set_t), &cpuset) == 0); if (!ok) return NULL; - Affinity* affinity = Affinity_new(pl); - for (unsigned int i = 0; i < pl->existingCPUs; i++) { + Affinity* affinity = Affinity_new(host); + for (unsigned int i = 0; i < host->existingCPUs; i++) { if (CPU_ISSET(i, &cpuset)) { |