Skip to content

Commit

Permalink
Introduce Machine class for host-specific info (split from ProcessList)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
natoscott committed May 8, 2023
1 parent e4ebe18 commit 0bdade1
Show file tree
Hide file tree
Showing 81 changed files with 989 additions and 718 deletions.
111 changes: 63 additions & 48 deletions Action.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -45,23 +46,24 @@ 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);
Panel* panelFocus;
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);
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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++) {
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -307,33 +319,34 @@ 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);
return changed ? HTOP_RECALCULATE : HTOP_OK;
}

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);
return changed ? HTOP_RECALCULATE : HTOP_OK;
}

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;
Expand All @@ -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 {
Expand Down Expand Up @@ -379,25 +392,26 @@ 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))
const Process* p = (const Process*) Panel_getSelected((Panel*)st->mainPanel);
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();
Expand Down Expand Up @@ -479,24 +493,25 @@ 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);
return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
}

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;
}
Expand Down Expand Up @@ -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");
Expand Down
11 changes: 5 additions & 6 deletions Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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;
Expand All @@ -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);

Expand Down
Loading

0 comments on commit 0bdade1

Please sign in to comment.