Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Row and Table classes for tabular screens beyond top-processes #1243

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 105 additions & 60 deletions Action.c

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bool Action_setUserOnly(const char* userName, uid_t* userId);

Htop_Reaction Action_setSortKey(Settings* settings, ProcessField sortKey);

Htop_Reaction Action_setScreenTab(Settings* settings, int x);
Htop_Reaction Action_setScreenTab(State* st, int x);

Htop_Reaction Action_follow(State* st);

Expand Down
34 changes: 25 additions & 9 deletions Affinity.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ in the source distribution for its full text.

#include <stdlib.h>

#include "Process.h"
#include "XUtils.h"

#if defined(HAVE_LIBHWLOC)
Expand Down Expand Up @@ -49,12 +50,11 @@ void Affinity_add(Affinity* this, unsigned int id) {
this->used++;
}


#if defined(HAVE_LIBHWLOC)

Affinity* Affinity_get(const Process* proc, Machine* host) {
static Affinity* Affinity_get(const Process* p, Machine* host) {
hwloc_cpuset_t cpuset = hwloc_bitmap_alloc();
bool ok = (hwloc_get_proc_cpubind(host->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0);
bool ok = (hwloc_get_proc_cpubind(host->topology, Process_getPid(p), cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0);
Affinity* affinity = NULL;
if (ok) {
affinity = Affinity_new(host);
Expand All @@ -73,22 +73,22 @@ Affinity* Affinity_get(const Process* proc, Machine* host) {
return affinity;
}

bool Affinity_set(Process* proc, Arg arg) {
static bool Affinity_set(Process* p, Arg arg) {
Affinity* this = arg.v;
hwloc_cpuset_t cpuset = hwloc_bitmap_alloc();
for (unsigned int i = 0; i < this->used; i++) {
hwloc_bitmap_set(cpuset, this->cpus[i]);
}
bool ok = (hwloc_set_proc_cpubind(this->host->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0);
bool ok = (hwloc_set_proc_cpubind(this->host->topology, Process_getPid(p), cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0);
hwloc_bitmap_free(cpuset);
return ok;
}

#elif defined(HAVE_AFFINITY)

Affinity* Affinity_get(const Process* proc, Machine* host) {
static Affinity* Affinity_get(const Process* p, Machine* host) {
cpu_set_t cpuset;
bool ok = (sched_getaffinity(proc->pid, sizeof(cpu_set_t), &cpuset) == 0);
bool ok = (sched_getaffinity(Process_getPid(p), sizeof(cpu_set_t), &cpuset) == 0);
if (!ok)
return NULL;

Expand All @@ -101,15 +101,31 @@ Affinity* Affinity_get(const Process* proc, Machine* host) {
return affinity;
}

bool Affinity_set(Process* proc, Arg arg) {
static bool Affinity_set(Process* p, Arg arg) {
Affinity* this = arg.v;
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
for (unsigned int i = 0; i < this->used; i++) {
CPU_SET(this->cpus[i], &cpuset);
}
bool ok = (sched_setaffinity(proc->pid, sizeof(unsigned long), &cpuset) == 0);
bool ok = (sched_setaffinity(Process_getPid(p), sizeof(unsigned long), &cpuset) == 0);
return ok;
}

#endif

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

bool Affinity_rowSet(Row* row, Arg arg) {
Process* p = (Process*) row;
assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
return Affinity_set(p, arg);
}

Affinity* Affinity_rowGet(const Row* row, Machine* host) {
const Process* p = (const Process*) row;
assert(Object_isA((const Object*) p, (const ObjectClass*) &Process_class));
return Affinity_get(p, host);
}

#endif /* HAVE_LIBHWLOC || HAVE_AFFINITY */
6 changes: 3 additions & 3 deletions Affinity.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ in the source distribution for its full text.
#include <stdbool.h>

#include "Object.h"
#include "Process.h"
#include "Row.h"
natoscott marked this conversation as resolved.
Show resolved Hide resolved
#endif


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

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

Affinity* Affinity_get(const Process* proc, Machine* host);
Affinity* Affinity_rowGet(const Row* row, Machine* host);

bool Affinity_set(Process* proc, Arg arg);
bool Affinity_rowSet(Row* row, Arg arg);

#endif /* HAVE_LIBHWLOC || HAVE_AFFINITY */

Expand Down
50 changes: 35 additions & 15 deletions AvailableColumnsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ in the source distribution for its full text.
#include "Hashtable.h"
#include "ListItem.h"
#include "Object.h"
#include "Platform.h"
#include "Process.h"
#include "ProvideCurses.h"
#include "XUtils.h"
Expand All @@ -34,8 +35,8 @@ static void AvailableColumnsPanel_delete(Object* object) {

static void AvailableColumnsPanel_insert(AvailableColumnsPanel* this, int at, int key) {
const char* name;
if (key >= LAST_PROCESSFIELD)
name = DynamicColumn_init(key);
if (key >= ROW_DYNAMIC_FIELDS)
name = DynamicColumn_name(key);
else
name = Process_fields[key].name;
Panel_insert(this->columns, at, (Object*) ListItem_new(name, key));
Expand Down Expand Up @@ -81,42 +82,61 @@ const PanelClass AvailableColumnsPanel_class = {

static void AvailableColumnsPanel_addDynamicColumn(ht_key_t key, void* value, void* data) {
const DynamicColumn* column = (const DynamicColumn*) value;
Panel* super = (Panel*) data;
const char* title = column->caption ? column->caption : column->heading;
if (!title)
title = column->name; // fallback to the only mandatory field
if (column->table) /* DynamicScreen, handled differently */
return;
AvailableColumnsPanel* this = (AvailableColumnsPanel*) data;
const char* title = column->heading ? column->heading : column->name;
const char* text = column->description ? column->description : column->caption;
char description[256];
xSnprintf(description, sizeof(description), "%s - %s", title, column->description);
Panel_add(super, (Object*) ListItem_new(description, key));
if (text)
xSnprintf(description, sizeof(description), "%s - %s", title, text);
else
xSnprintf(description, sizeof(description), "%s", title);
Panel_add(&this->super, (Object*) ListItem_new(description, key));
}

// Handle DynamicColumns entries in the AvailableColumnsPanel
static void AvailableColumnsPanel_addDynamicColumns(Panel* super, Hashtable* dynamicColumns) {
static void AvailableColumnsPanel_addDynamicColumns(AvailableColumnsPanel* this, Hashtable* dynamicColumns) {
assert(dynamicColumns);
Hashtable_foreach(dynamicColumns, AvailableColumnsPanel_addDynamicColumn, super);
Hashtable_foreach(dynamicColumns, AvailableColumnsPanel_addDynamicColumn, this);
}

// Handle remaining Platform Meter entries in the AvailableColumnsPanel
static void AvailableColumnsPanel_addPlatformColumn(Panel* super) {
static void AvailableColumnsPanel_addPlatformColumns(AvailableColumnsPanel* this) {
for (int i = 1; i < LAST_PROCESSFIELD; i++) {
if (i != COMM && Process_fields[i].description) {
char description[256];
xSnprintf(description, sizeof(description), "%s - %s", Process_fields[i].name, Process_fields[i].description);
Panel_add(super, (Object*) ListItem_new(description, i));
Panel_add(&this->super, (Object*) ListItem_new(description, i));
}
}
}

// Handle DynamicColumns entries associated with DynamicScreens
static void AvailableColumnsPanel_addDynamicScreens(AvailableColumnsPanel* this, const char* screen) {
Platform_addDynamicScreenAvailableColumns(&this->super, screen);
}

void AvailableColumnsPanel_fill(AvailableColumnsPanel* this, const char* dynamicScreen, Hashtable* dynamicColumns) {
Panel* super = (Panel*) this;
Panel_prune(super);
if (dynamicScreen) {
AvailableColumnsPanel_addDynamicScreens(this, dynamicScreen);
} else {
AvailableColumnsPanel_addPlatformColumns(this);
AvailableColumnsPanel_addDynamicColumns(this, dynamicColumns);
}
}

AvailableColumnsPanel* AvailableColumnsPanel_new(Panel* columns, Hashtable* dynamicColumns) {
AvailableColumnsPanel* this = AllocThis(AvailableColumnsPanel);
Panel* super = (Panel*) this;
FunctionBar* fuBar = FunctionBar_new(AvailableColumnsFunctions, NULL, NULL);
Panel_init(super, 1, 1, 1, 1, Class(ListItem), true, fuBar);

Panel_setHeader(super, "Available Columns");
AvailableColumnsPanel_addPlatformColumn(super);
AvailableColumnsPanel_addDynamicColumns(super, dynamicColumns);

this->columns = columns;
AvailableColumnsPanel_fill(this, NULL, dynamicColumns);

return this;
}
3 changes: 3 additions & 0 deletions AvailableColumnsPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ in the source distribution for its full text.

#include "Hashtable.h"
#include "Panel.h"
#include "Settings.h"


typedef struct AvailableColumnsPanel_ {
Expand All @@ -20,4 +21,6 @@ extern const PanelClass AvailableColumnsPanel_class;

AvailableColumnsPanel* AvailableColumnsPanel_new(Panel* columns, Hashtable* dynamicColumns);

void AvailableColumnsPanel_fill(AvailableColumnsPanel* this, const char* dynamicScreen, Hashtable* dynamicColumns);

#endif
18 changes: 16 additions & 2 deletions CategoriesPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ in the source distribution for its full text.
#include "Object.h"
#include "ProvideCurses.h"
#include "ScreensPanel.h"
#include "ScreenTabsPanel.h"
#include "Vector.h"
#include "XUtils.h"

Expand Down Expand Up @@ -72,11 +73,21 @@ static void CategoriesPanel_makeColorsPage(CategoriesPanel* this) {
ScreenManager_add(this->scr, colors, -1);
}

#if defined(HTOP_PCP) /* all platforms supporting dynamic screens */
static void CategoriesPanel_makeScreenTabsPage(CategoriesPanel* this) {
Settings* settings = this->host->settings;
Panel* screenTabs = (Panel*) ScreenTabsPanel_new(settings);
Panel* screenNames = (Panel*) ((ScreenTabsPanel*)screenTabs)->names;
ScreenManager_add(this->scr, screenTabs, 20);
ScreenManager_add(this->scr, screenNames, -1);
}
#endif

static void CategoriesPanel_makeScreensPage(CategoriesPanel* this) {
Settings* settings = this->host->settings;
Panel* screens = (Panel*) ScreensPanel_new(settings);
Panel* columns = (Panel*) ((ScreensPanel*)screens)->columns;
Panel* availableColumns = (Panel*) AvailableColumnsPanel_new(columns, settings->dynamicColumns);
Panel* availableColumns = (Panel*) ((ScreensPanel*)screens)->availableColumns;
ScreenManager_add(this->scr, screens, 20);
ScreenManager_add(this->scr, columns, 20);
ScreenManager_add(this->scr, availableColumns, -1);
Expand All @@ -94,10 +105,13 @@ typedef struct CategoriesPanelPage_ {
CategoriesPanel_makePageFunc ctor;
} CategoriesPanelPage;

static const CategoriesPanelPage categoriesPanelPages[] = {
static CategoriesPanelPage categoriesPanelPages[] = {
{ .name = "Display options", .ctor = CategoriesPanel_makeDisplayOptionsPage },
{ .name = "Header layout", .ctor = CategoriesPanel_makeHeaderOptionsPage },
{ .name = "Meters", .ctor = CategoriesPanel_makeMetersPage },
#if defined(HTOP_PCP) /* all platforms supporting dynamic screens */
{ .name = "Screen tabs", .ctor = CategoriesPanel_makeScreenTabsPage },
#endif
{ .name = "Screens", .ctor = CategoriesPanel_makeScreensPage },
{ .name = "Colors", .ctor = CategoriesPanel_makeColorsPage },
};
Expand Down
7 changes: 3 additions & 4 deletions ColumnsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ static void ColumnsPanel_add(Panel* super, unsigned int key, Hashtable* columns)
if (!column) {
name = NULL;
} else {
name = column->caption ? column->caption : column->heading;
if (!name)
name = column->name; /* name is a mandatory field */
/* heading preferred here but name is always available */
name = column->heading ? column->heading : column->name;
}
}
if (name == NULL)
Expand All @@ -141,7 +140,7 @@ static void ColumnsPanel_add(Panel* super, unsigned int key, Hashtable* columns)
void ColumnsPanel_fill(ColumnsPanel* this, ScreenSettings* ss, Hashtable* columns) {
Panel* super = (Panel*) this;
Panel_prune(super);
for (const ProcessField* fields = ss->fields; *fields; fields++)
for (const RowField* fields = ss->fields; *fields; fields++)
ColumnsPanel_add(super, *fields, columns);
this->ss = ss;
}
Expand Down
26 changes: 11 additions & 15 deletions CommandLine.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ in the source distribution for its full text.
#include "CRT.h"
#include "DynamicColumn.h"
#include "DynamicMeter.h"
#include "DynamicScreen.h"
#include "Hashtable.h"
#include "Header.h"
#include "IncSet.h"
Expand Down Expand Up @@ -303,11 +304,11 @@ static void CommandLine_delay(Machine* host, unsigned long millisec) {
}

static void setCommFilter(State* state, char** commFilter) {
ProcessList* pl = state->host->pl;
Table* table = state->host->activeTable;
IncSet* inc = state->mainPanel->inc;

IncSet_setFilter(inc, *commFilter);
pl->incFilter = IncSet_filter(inc);
table->incFilter = IncSet_filter(inc);

free(*commFilter);
*commFilter = NULL;
Expand All @@ -334,20 +335,15 @@ int CommandLine_run(int argc, char** argv) {
if (!Platform_init())
return 1;

Process_setupColumnWidths();

UsersTable* ut = UsersTable_new();
Hashtable* dm = DynamicMeters_new();
Hashtable* dc = DynamicColumns_new();
if (!dc)
dc = Hashtable_new(0, true);
Hashtable* ds = DynamicScreens_new();

Machine* host = Machine_new(ut, flags.userId);
ProcessList* pl = ProcessList_new(host, flags.pidMatchList);
Settings* settings = Settings_new(host->activeCPUs, dm, dc);

host->settings = settings;
Machine_addList(host, pl);
Settings* settings = Settings_new(host->activeCPUs, dm, dc, ds);
Machine_populateTablesFromSettings(host, settings, &pl->super);

Header* header = Header_new(host, 2);
Header_populateFromSettings(header);
Expand Down Expand Up @@ -379,7 +375,7 @@ int CommandLine_run(int argc, char** argv) {
CRT_init(settings, flags.allowUnicode, flags.iterationsRemaining != -1);

MainPanel* panel = MainPanel_new();
ProcessList_setPanel(pl, (Panel*) panel);
Machine_setTablesPanel(host, (Panel*) panel);

MainPanel_updateLabels(panel, settings->ss->treeView, flags.commFilter);

Expand All @@ -400,13 +396,13 @@ int CommandLine_run(int argc, char** argv) {
ScreenManager_add(scr, (Panel*) panel, -1);

Machine_scan(host);
ProcessList_scan(pl);
Machine_scanTables(host);
CommandLine_delay(host, 75);
Machine_scan(host);
ProcessList_scan(pl);
Machine_scanTables(host);

if (settings->ss->allBranchesCollapsed)
ProcessList_collapseAllBranches(pl);
Table_collapseAllBranches(&pl->super);

ScreenManager_run(scr, NULL, NULL, NULL);

Expand All @@ -421,7 +417,6 @@ int CommandLine_run(int argc, char** argv) {
}

Header_delete(header);
ProcessList_delete(pl);
Machine_delete(host);

ScreenManager_delete(scr);
Expand All @@ -438,6 +433,7 @@ int CommandLine_run(int argc, char** argv) {
Settings_delete(settings);
DynamicColumns_delete(dc);
DynamicMeters_delete(dm);
DynamicScreens_delete(ds);

return 0;
}
2 changes: 1 addition & 1 deletion CommandScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static void CommandScreen_scan(InfoScreen* this) {
}

static void CommandScreen_draw(InfoScreen* this) {
InfoScreen_drawTitled(this, "Command of process %d - %s", this->process->pid, Process_getCommand(this->process));
InfoScreen_drawTitled(this, "Command of process %d - %s", Process_getPid(this->process), Process_getCommand(this->process));
}

const InfoScreenClass CommandScreen_class = {
Expand Down
2 changes: 1 addition & 1 deletion DisplayOptionsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager*

#define TABMSG "For current screen tab: \0"
char tabheader[sizeof(TABMSG) + SCREEN_NAME_LEN + 1] = TABMSG;
strncat(tabheader, settings->ss->name, SCREEN_NAME_LEN);
strncat(tabheader, settings->ss->heading, SCREEN_NAME_LEN);
Panel_add(super, (Object*) TextItem_new(tabheader));
#undef TABMSG

Expand Down
Loading