Skip to content

Commit fe92ef2

Browse files
committed
Introduce Row and Table classes for screens beyond top-processes
This commit refactors the Process and ProcessList structures such they each have a new parent - Row and Table, respectively. These new classes handle screen updates relating to anything that could be represented in tabular format, e.g. cgroups, filesystems, etc, without us having to reimplement the display logic repeatedly for each new entity.
1 parent 1de7a2b commit fe92ef2

53 files changed

Lines changed: 1954 additions & 1460 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Action.c

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,22 @@ Object* Action_pickFromVector(State* st, Panel* list, int x, bool follow) {
5656
Panel* panelFocus;
5757
int ch;
5858
bool unfollow = false;
59-
int pid = follow ? MainPanel_selectedPid(mainPanel) : -1;
60-
if (follow && host->pl->following == -1) {
61-
host->pl->following = pid;
59+
int row = follow ? MainPanel_selectedRow(mainPanel) : -1;
60+
if (follow && host->activeTable->following == -1) {
61+
host->activeTable->following = row;
6262
unfollow = true;
6363
}
6464
ScreenManager_run(scr, &panelFocus, &ch, NULL);
6565
if (unfollow) {
66-
host->pl->following = -1;
66+
host->activeTable->following = -1;
6767
}
6868
ScreenManager_delete(scr);
6969
Panel_move((Panel*)mainPanel, 0, y);
7070
Panel_resize((Panel*)mainPanel, COLS, LINES - y - 1);
7171
if (panelFocus == list && ch == 13) {
7272
if (follow) {
73-
const Process* selected = (const Process*)Panel_getSelected((Panel*)mainPanel);
74-
if (selected && selected->pid == pid)
73+
const Row* selected = (const Row*)Panel_getSelected((Panel*)mainPanel);
74+
if (selected && selected->id == row)
7575
return Panel_getSelected(list);
7676

7777
beep();
@@ -99,7 +99,7 @@ static void Action_runSetup(State* st) {
9999

100100
static bool changePriority(MainPanel* panel, int delta) {
101101
bool anyTagged;
102-
bool ok = MainPanel_foreachProcess(panel, Process_changePriorityBy, (Arg) { .i = delta }, &anyTagged);
102+
bool ok = MainPanel_foreachRow(panel, Process_changePriorityBy, (Arg) { .i = delta }, &anyTagged);
103103
if (!ok)
104104
beep();
105105
return anyTagged;
@@ -121,36 +121,36 @@ bool Action_setUserOnly(const char* userName, uid_t* userId) {
121121
return false;
122122
}
123123

124-
static void tagAllChildren(Panel* panel, Process* parent) {
124+
static void tagAllChildren(Panel* panel, Row* parent) {
125125
parent->tag = true;
126-
pid_t ppid = parent->pid;
126+
int parent_id = parent->id;
127127
for (int i = 0; i < Panel_size(panel); i++) {
128-
Process* p = (Process*) Panel_get(panel, i);
129-
if (!p->tag && Process_isChildOf(p, ppid)) {
130-
tagAllChildren(panel, p);
128+
Row* row = (Row*) Panel_get(panel, i);
129+
if (!row->tag && Row_isChildOf(row, parent_id)) {
130+
tagAllChildren(panel, row);
131131
}
132132
}
133133
}
134134

135135
static bool expandCollapse(Panel* panel) {
136-
Process* p = (Process*) Panel_getSelected(panel);
137-
if (!p)
136+
Row* row = (Row*) Panel_getSelected(panel);
137+
if (!row)
138138
return false;
139139

140-
p->showChildren = !p->showChildren;
140+
row->showChildren = !row->showChildren;
141141
return true;
142142
}
143143

144144
static bool collapseIntoParent(Panel* panel) {
145-
const Process* p = (Process*) Panel_getSelected(panel);
146-
if (!p)
145+
const Row* r = (Row*) Panel_getSelected(panel);
146+
if (!r)
147147
return false;
148148

149-
pid_t ppid = Process_getParentPid(p);
149+
int parent_id = Row_getGroupOrParent(r);
150150
for (int i = 0; i < Panel_size(panel); i++) {
151-
Process* q = (Process*) Panel_get(panel, i);
152-
if (q->pid == ppid) {
153-
q->showChildren = false;
151+
Row* row = (Row*) Panel_get(panel, i);
152+
if (row->id == parent_id) {
153+
row->showChildren = false;
154154
Panel_setSelected(panel, i);
155155
return true;
156156
}
@@ -159,7 +159,7 @@ static bool collapseIntoParent(Panel* panel) {
159159
}
160160

161161
Htop_Reaction Action_setSortKey(Settings* settings, ProcessField sortKey) {
162-
ScreenSettings_setSortKey(settings->ss, sortKey);
162+
ScreenSettings_setSortKey(settings->ss, (RowField) sortKey);
163163
return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_UPDATE_PANELHDR | HTOP_KEEP_FOLLOWING;
164164
}
165165

@@ -171,11 +171,11 @@ static Htop_Reaction actionSetSortColumn(State* st) {
171171
Panel_setHeader(sortPanel, "Sort by");
172172
Machine* host = st->host;
173173
Settings* settings = host->settings;
174-
const ProcessField* fields = settings->ss->fields;
174+
const RowField* fields = settings->ss->fields;
175175
Hashtable* dynamicColumns = settings->dynamicColumns;
176176
for (int i = 0; fields[i]; i++) {
177177
char* name = NULL;
178-
if (fields[i] >= LAST_PROCESSFIELD) {
178+
if (fields[i] >= ROW_DYNAMIC_FIELDS) {
179179
DynamicColumn* column = Hashtable_get(dynamicColumns, fields[i]);
180180
if (!column)
181181
continue;
@@ -195,7 +195,7 @@ static Htop_Reaction actionSetSortColumn(State* st) {
195195
}
196196
Object_delete(sortPanel);
197197

198-
host->pl->needsSort = true;
198+
host->activeTable->needsSort = true;
199199

200200
return reaction | HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
201201
}
@@ -262,9 +262,9 @@ static Htop_Reaction actionToggleTreeView(State* st) {
262262
ss->treeView = !ss->treeView;
263263

264264
if (!ss->allBranchesCollapsed)
265-
ProcessList_expandTree(host->pl);
265+
Table_expandTree(host->activeTable);
266266

267-
host->pl->needsSort = true;
267+
host->activeTable->needsSort = true;
268268

269269
return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR;
270270
}
@@ -282,17 +282,17 @@ static Htop_Reaction actionExpandOrCollapseAllBranches(State* st) {
282282
}
283283
ss->allBranchesCollapsed = !ss->allBranchesCollapsed;
284284
if (ss->allBranchesCollapsed)
285-
ProcessList_collapseAllBranches(host->pl);
285+
Table_collapseAllBranches(host->activeTable);
286286
else
287-
ProcessList_expandTree(host->pl);
287+
Table_expandTree(host->activeTable);
288288
return HTOP_REFRESH | HTOP_SAVE_SETTINGS;
289289
}
290290

291291
static Htop_Reaction actionIncFilter(State* st) {
292292
Machine* host = st->host;
293293
IncSet* inc = (st->mainPanel)->inc;
294294
IncSet_activate(inc, INC_FILTER, (Panel*)st->mainPanel);
295-
host->pl->incFilter = IncSet_filter(inc);
295+
host->activeTable->incFilter = IncSet_filter(inc);
296296
return HTOP_REFRESH | HTOP_KEEP_FOLLOWING;
297297
}
298298

@@ -321,7 +321,7 @@ static Htop_Reaction actionLowerPriority(State* st) {
321321
static Htop_Reaction actionInvertSortOrder(State* st) {
322322
Machine* host = st->host;
323323
ScreenSettings_invertSortOrder(host->settings->ss);
324-
host->pl->needsSort = true;
324+
host->activeTable->needsSort = true;
325325
return HTOP_REFRESH | HTOP_SAVE_SETTINGS | HTOP_KEEP_FOLLOWING | HTOP_UPDATE_PANELHDR;
326326
}
327327

@@ -397,11 +397,11 @@ static Htop_Reaction actionSetAffinity(State* st) {
397397
return HTOP_OK;
398398

399399
#if (defined(HAVE_LIBHWLOC) || defined(HAVE_AFFINITY))
400-
const Process* p = (const Process*) Panel_getSelected((Panel*)st->mainPanel);
401-
if (!p)
400+
const Row* row = (const Row*) Panel_getSelected((Panel*)st->mainPanel);
401+
if (!row)
402402
return HTOP_OK;
403403

404-
Affinity* affinity1 = Affinity_get(p, host);
404+
Affinity* affinity1 = Affinity_get(row, host);
405405
if (!affinity1)
406406
return HTOP_OK;
407407

@@ -412,7 +412,7 @@ static Htop_Reaction actionSetAffinity(State* st) {
412412
const void* set = Action_pickFromVector(st, affinityPanel, width, true);
413413
if (set) {
414414
Affinity* affinity2 = AffinityPanel_getAffinity(affinityPanel, host);
415-
bool ok = MainPanel_foreachProcess(st->mainPanel, Affinity_set, (Arg) { .v = affinity2 }, NULL);
415+
bool ok = MainPanel_foreachRow(st->mainPanel, Affinity_set, (Arg) { .v = affinity2 }, NULL);
416416
if (!ok)
417417
beep();
418418
Affinity_delete(affinity2);
@@ -459,7 +459,7 @@ static Htop_Reaction actionSetSchedPolicy(State* st) {
459459

460460
SchedulingArg v = { .policy = preSelectedPolicy, .priority = preSelectedPriority };
461461

462-
bool ok = MainPanel_foreachProcess(st->mainPanel, Scheduling_setPolicy, (Arg) { .v = &v }, NULL);
462+
bool ok = MainPanel_foreachRow(st->mainPanel, Scheduling_setPolicy, (Arg) { .v = &v }, NULL);
463463
if (!ok)
464464
beep();
465465
}
@@ -483,7 +483,7 @@ static Htop_Reaction actionKill(State* st) {
483483
Panel_setHeader((Panel*)st->mainPanel, "Sending...");
484484
Panel_draw((Panel*)st->mainPanel, false, true, true, State_hideFunctionBar(st));
485485
refresh();
486-
MainPanel_foreachProcess(st->mainPanel, Process_sendSignal, (Arg) { .i = sgn->key }, NULL);
486+
MainPanel_foreachRow(st->mainPanel, Process_sendSignal, (Arg) { .i = sgn->key }, NULL);
487487
napms(500);
488488
}
489489
Panel_delete((Object*)signalsPanel);
@@ -511,7 +511,7 @@ static Htop_Reaction actionFilterByUser(State* st) {
511511
}
512512

513513
Htop_Reaction Action_follow(State* st) {
514-
st->host->pl->following = MainPanel_selectedPid(st->mainPanel);
514+
st->host->activeTable->following = MainPanel_selectedRow(st->mainPanel);
515515
Panel_setSelectionColor((Panel*)st->mainPanel, PANEL_SELECTION_FOLLOW);
516516
return HTOP_KEEP_FOLLOWING;
517517
}
@@ -529,6 +529,8 @@ static Htop_Reaction actionLsof(State* st) {
529529
if (!p)
530530
return HTOP_OK;
531531

532+
assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
533+
532534
OpenFilesScreen* ofs = OpenFilesScreen_new(p);
533535
InfoScreen_run((InfoScreen*)ofs);
534536
OpenFilesScreen_delete((Object*)ofs);
@@ -541,6 +543,9 @@ static Htop_Reaction actionShowLocks(State* st) {
541543
const Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
542544
if (!p)
543545
return HTOP_OK;
546+
547+
assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
548+
544549
ProcessLocksScreen* pls = ProcessLocksScreen_new(p);
545550
InfoScreen_run((InfoScreen*)pls);
546551
ProcessLocksScreen_delete((Object*)pls);
@@ -556,6 +561,7 @@ static Htop_Reaction actionStrace(State* st) {
556561
const Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
557562
if (!p)
558563
return HTOP_OK;
564+
assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
559565

560566
TraceScreen* ts = TraceScreen_new(p);
561567
bool ok = TraceScreen_forkTracer(ts);
@@ -569,11 +575,11 @@ static Htop_Reaction actionStrace(State* st) {
569575
}
570576

571577
static Htop_Reaction actionTag(State* st) {
572-
Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
573-
if (!p)
578+
Row* r = (Row*) Panel_getSelected((Panel*)st->mainPanel);
579+
if (!r)
574580
return HTOP_OK;
575581

576-
Process_toggleTag(p);
582+
Row_toggleTag(r);
577583
Panel_onKey((Panel*)st->mainPanel, KEY_DOWN);
578584
return HTOP_OK;
579585
}
@@ -782,18 +788,18 @@ static Htop_Reaction actionHelp(State* st) {
782788

783789
static Htop_Reaction actionUntagAll(State* st) {
784790
for (int i = 0; i < Panel_size((Panel*)st->mainPanel); i++) {
785-
Process* p = (Process*) Panel_get((Panel*)st->mainPanel, i);
786-
p->tag = false;
791+
Row* row = (Row*) Panel_get((Panel*)st->mainPanel, i);
792+
row->tag = false;
787793
}
788794
return HTOP_REFRESH;
789795
}
790796

791797
static Htop_Reaction actionTagAllChildren(State* st) {
792-
Process* p = (Process*) Panel_getSelected((Panel*)st->mainPanel);
793-
if (!p)
798+
Row* row = (Row*) Panel_getSelected((Panel*)st->mainPanel);
799+
if (!row)
794800
return HTOP_OK;
795801

796-
tagAllChildren((Panel*)st->mainPanel, p);
802+
tagAllChildren((Panel*)st->mainPanel, row);
797803
return HTOP_OK;
798804
}
799805

@@ -802,6 +808,8 @@ static Htop_Reaction actionShowEnvScreen(State* st) {
802808
if (!p)
803809
return HTOP_OK;
804810

811+
assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
812+
805813
EnvScreen* es = EnvScreen_new(p);
806814
InfoScreen_run((InfoScreen*)es);
807815
EnvScreen_delete((Object*)es);
@@ -815,6 +823,8 @@ static Htop_Reaction actionShowCommandScreen(State* st) {
815823
if (!p)
816824
return HTOP_OK;
817825

826+
assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
827+
818828
CommandScreen* cmdScr = CommandScreen_new(p);
819829
InfoScreen_run((InfoScreen*)cmdScr);
820830
CommandScreen_delete((Object*)cmdScr);

Affinity.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ void Affinity_add(Affinity* this, unsigned int id) {
5252

5353
#if defined(HAVE_LIBHWLOC)
5454

55-
Affinity* Affinity_get(const Process* proc, Machine* host) {
55+
Affinity* Affinity_get(const Row* row, Machine* host) {
5656
hwloc_cpuset_t cpuset = hwloc_bitmap_alloc();
57-
bool ok = (hwloc_get_proc_cpubind(host->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0);
57+
bool ok = (hwloc_get_proc_cpubind(host->topology, row->id, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0);
5858
Affinity* affinity = NULL;
5959
if (ok) {
6060
affinity = Affinity_new(host);
@@ -73,22 +73,22 @@ Affinity* Affinity_get(const Process* proc, Machine* host) {
7373
return affinity;
7474
}
7575

76-
bool Affinity_set(Process* proc, Arg arg) {
76+
bool Affinity_set(Row* row, Arg arg) {
7777
Affinity* this = arg.v;
7878
hwloc_cpuset_t cpuset = hwloc_bitmap_alloc();
7979
for (unsigned int i = 0; i < this->used; i++) {
8080
hwloc_bitmap_set(cpuset, this->cpus[i]);
8181
}
82-
bool ok = (hwloc_set_proc_cpubind(this->host->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0);
82+
bool ok = (hwloc_set_proc_cpubind(this->host->topology, row->id, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0);
8383
hwloc_bitmap_free(cpuset);
8484
return ok;
8585
}
8686

8787
#elif defined(HAVE_AFFINITY)
8888

89-
Affinity* Affinity_get(const Process* proc, Machine* host) {
89+
Affinity* Affinity_get(const Row* row, Machine* host) {
9090
cpu_set_t cpuset;
91-
bool ok = (sched_getaffinity(proc->pid, sizeof(cpu_set_t), &cpuset) == 0);
91+
bool ok = (sched_getaffinity(row->id, sizeof(cpu_set_t), &cpuset) == 0);
9292
if (!ok)
9393
return NULL;
9494

@@ -101,14 +101,14 @@ Affinity* Affinity_get(const Process* proc, Machine* host) {
101101
return affinity;
102102
}
103103

104-
bool Affinity_set(Process* proc, Arg arg) {
104+
bool Affinity_set(Row* row, Arg arg) {
105105
Affinity* this = arg.v;
106106
cpu_set_t cpuset;
107107
CPU_ZERO(&cpuset);
108108
for (unsigned int i = 0; i < this->used; i++) {
109109
CPU_SET(this->cpus[i], &cpuset);
110110
}
111-
bool ok = (sched_setaffinity(proc->pid, sizeof(unsigned long), &cpuset) == 0);
111+
bool ok = (sched_setaffinity(row->id, sizeof(unsigned long), &cpuset) == 0);
112112
return ok;
113113
}
114114

Affinity.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ in the source distribution for its full text.
1616
#include <stdbool.h>
1717

1818
#include "Object.h"
19-
#include "Process.h"
19+
#include "Row.h"
2020
#endif
2121

2222

@@ -40,9 +40,9 @@ void Affinity_add(Affinity* this, unsigned int id);
4040

4141
#if defined(HAVE_LIBHWLOC) || defined(HAVE_AFFINITY)
4242

43-
Affinity* Affinity_get(const Process* proc, Machine* host);
43+
Affinity* Affinity_get(const Row* row, Machine* host);
4444

45-
bool Affinity_set(Process* proc, Arg arg);
45+
bool Affinity_set(Row* row, Arg arg);
4646

4747
#endif /* HAVE_LIBHWLOC || HAVE_AFFINITY */
4848

AvailableColumnsPanel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void AvailableColumnsPanel_delete(Object* object) {
3434

3535
static void AvailableColumnsPanel_insert(AvailableColumnsPanel* this, int at, int key) {
3636
const char* name;
37-
if (key >= LAST_PROCESSFIELD)
37+
if (key >= ROW_DYNAMIC_FIELDS)
3838
name = DynamicColumn_init(key);
3939
else
4040
name = Process_fields[key].name;

ColumnsPanel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static void ColumnsPanel_add(Panel* super, unsigned int key, Hashtable* columns)
141141
void ColumnsPanel_fill(ColumnsPanel* this, ScreenSettings* ss, Hashtable* columns) {
142142
Panel* super = (Panel*) this;
143143
Panel_prune(super);
144-
for (const ProcessField* fields = ss->fields; *fields; fields++)
144+
for (const RowField* fields = ss->fields; *fields; fields++)
145145
ColumnsPanel_add(super, *fields, columns);
146146
this->ss = ss;
147147
}

0 commit comments

Comments
 (0)