Skip to content

Commit

Permalink
PCP: support for 'dynamic columns' added at runtime
Browse files Browse the repository at this point in the history
Implements support for arbitrary Performance Co-Pilot
metrics with per-process instance domains to form new
htop columns.  The column-to-metric mappings are setup
using configuration files which will be documented via
man pages as part of a follow-up commit.

We provide an initial set of column configurations so
as to provide new capabilities to pcp-htop: including
configs for containers, open fd counts, scheduler run
queue time, tcp/udp bytes/calls sent/recv, delay acct,
virtual machine guests, detailed virtual memory, swap.

Note there is a change to the configuration file path
resolution algorithm introduced for 'dynamic meters'.
First, look in any custom PCP_HTOP_DIR location.  Then
iterate, in priority order, users home directory, then
local sysadmins files in /etc/pcp/htop, then readonly
configuration files below /usr/share/pcp/htop.  This
final location becomes the preferred place for our own
shipped meter and column files.

The Settings file (htoprc) writing code is updated to
not using the numeric identifier for dynamic columns.
The same strategy used for dynamic meters is used here
where we write Dynamic(name) so the name can be setup
once more at start.  Regular (static) columns writing
to htoprc - i.e. numerically indexed - is unchanged.
  • Loading branch information
smalinux authored and natoscott committed Aug 13, 2021
1 parent ed82ce6 commit 6f2021f
Show file tree
Hide file tree
Showing 59 changed files with 1,063 additions and 174 deletions.
13 changes: 11 additions & 2 deletions Action.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ in the source distribution for its full text.
#include <stdbool.h>
#include <stdlib.h>

#include "CRT.h"
#include "CategoriesPanel.h"
#include "CommandScreen.h"
#include "CRT.h"
#include "DynamicColumn.h"
#include "EnvScreen.h"
#include "FunctionBar.h"
#include "Hashtable.h"
Expand Down Expand Up @@ -168,8 +169,16 @@ static Htop_Reaction actionSetSortColumn(State* st) {
Panel* sortPanel = Panel_new(0, 0, 0, 0, Class(ListItem), true, FunctionBar_newEnterEsc("Sort ", "Cancel "));
Panel_setHeader(sortPanel, "Sort by");
const ProcessField* fields = st->settings->fields;
Hashtable* dynamicColumns = st->settings->dynamicColumns;
for (int i = 0; fields[i]; i++) {
char* name = String_trim(Process_fields[fields[i]].name);
char* name = NULL;
if (fields[i] >= LAST_PROCESSFIELD) {
DynamicColumn* column = Hashtable_get(dynamicColumns, fields[i]);
if (column)
name = xStrdup(column->caption ? column->caption : column->name);
} else {
name = String_trim(Process_fields[fields[i]].name);
}
Panel_add(sortPanel, (Object*) ListItem_new(name, fields[i]));
if (fields[i] == Settings_getActiveSortKey(st->settings))
Panel_setSelected(sortPanel, i);
Expand Down
54 changes: 45 additions & 9 deletions AvailableColumnsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ in the source distribution for its full text.

#include "AvailableColumnsPanel.h"

#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>

#include "ColumnsPanel.h"
#include "DynamicColumn.h"
#include "FunctionBar.h"
#include "Hashtable.h"
#include "ListItem.h"
#include "Macros.h"
#include "Object.h"
#include "Process.h"
#include "ProcessList.h"
#include "ProvideCurses.h"
#include "XUtils.h"

Expand All @@ -29,6 +34,15 @@ static void AvailableColumnsPanel_delete(Object* object) {
free(this);
}

static void AvailableColumnsPanel_insert(AvailableColumnsPanel* this, int at, int key) {
const char* name;
if (key >= LAST_PROCESSFIELD)
name = DynamicColumn_init(key);
else
name = Process_fields[key].name;
Panel_insert(this->columns, at, (Object*) ListItem_new(name, key));
}

static HandlerResult AvailableColumnsPanel_eventHandler(Panel* super, int ch) {
AvailableColumnsPanel* this = (AvailableColumnsPanel*) super;
HandlerResult result = IGNORED;
Expand All @@ -42,10 +56,9 @@ static HandlerResult AvailableColumnsPanel_eventHandler(Panel* super, int ch) {
if (!selected)
break;

int key = selected->key;
int at = Panel_getSelectedIndex(this->columns);
Panel_insert(this->columns, at, (Object*) ListItem_new(Process_fields[key].name, key));
Panel_setSelected(this->columns, at+1);
AvailableColumnsPanel_insert(this, at, selected->key);
Panel_setSelected(this->columns, at + 1);
ColumnsPanel_update(this->columns);
result = HANDLED;
break;
Expand All @@ -68,21 +81,44 @@ const PanelClass AvailableColumnsPanel_class = {
.eventHandler = AvailableColumnsPanel_eventHandler
};

AvailableColumnsPanel* AvailableColumnsPanel_new(Panel* columns) {
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);
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
char description[256];
xSnprintf(description, sizeof(description), "%s - %s", title, column->description);
Panel_add(super, (Object*) ListItem_new(description, key));
}

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

// Handle remaining Platform Meter entries in the AvailableColumnsPanel
static void AvailableColumnsPanel_addPlatformColumn(Panel* super) {
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));
}
}
}

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;
return this;
}
4 changes: 3 additions & 1 deletion AvailableColumnsPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/

#include "Hashtable.h"
#include "Panel.h"
#include "ProcessList.h"


typedef struct AvailableColumnsPanel_ {
Expand All @@ -17,6 +19,6 @@ typedef struct AvailableColumnsPanel_ {

extern const PanelClass AvailableColumnsPanel_class;

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

#endif
2 changes: 2 additions & 0 deletions AvailableMetersPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ in the source distribution for its full text.
#include "CPUMeter.h"
#include "DynamicMeter.h"
#include "FunctionBar.h"
#include "Hashtable.h"
#include "Header.h"
#include "ListItem.h"
#include "Macros.h"
#include "Meter.h"
#include "MetersPanel.h"
#include "Object.h"
Expand Down
2 changes: 1 addition & 1 deletion CategoriesPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static void CategoriesPanel_makeColorsPage(CategoriesPanel* this) {

static void CategoriesPanel_makeColumnsPage(CategoriesPanel* this) {
Panel* columns = (Panel*) ColumnsPanel_new(this->settings);
Panel* availableColumns = (Panel*) AvailableColumnsPanel_new(columns);
Panel* availableColumns = (Panel*) AvailableColumnsPanel_new(columns, this->settings->dynamicColumns);
ScreenManager_add(this->scr, columns, 20);
ScreenManager_add(this->scr, availableColumns, -1);
}
Expand Down
42 changes: 35 additions & 7 deletions ColumnsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ in the source distribution for its full text.
#include <stdlib.h>

#include "CRT.h"
#include "DynamicColumn.h"
#include "FunctionBar.h"
#include "Hashtable.h"
#include "ListItem.h"
#include "Object.h"
#include "Process.h"
Expand Down Expand Up @@ -115,6 +117,32 @@ const PanelClass ColumnsPanel_class = {
.eventHandler = ColumnsPanel_eventHandler
};

typedef struct {
Panel* super;
unsigned int id;
unsigned int offset;
} DynamicIterator;

static void ColumnsPanel_add(Panel* super, unsigned int key, Hashtable* columns) {
const char* name;
if (key < LAST_PROCESSFIELD) {
name = Process_fields[key].name;
} else {
const DynamicColumn* column = Hashtable_get(columns, key);
assert(column);
if (!column) {
name = NULL;
} else {
name = column->caption ? column->caption : column->heading;
if (!name)
name = column->name; /* name is a mandatory field */
}
}
if (name == NULL)
name = "- ";
Panel_add(super, (Object*) ListItem_new(name, key));
}

ColumnsPanel* ColumnsPanel_new(Settings* settings) {
ColumnsPanel* this = AllocThis(ColumnsPanel);
Panel* super = (Panel*) this;
Expand All @@ -125,12 +153,11 @@ ColumnsPanel* ColumnsPanel_new(Settings* settings) {
this->moving = false;
Panel_setHeader(super, "Active Columns");

const ProcessField* fields = this->settings->fields;
for (; *fields; fields++) {
if (Process_fields[*fields].name) {
Panel_add(super, (Object*) ListItem_new(Process_fields[*fields].name, *fields));
}
}
Hashtable* dynamicColumns = settings->dynamicColumns;
const ProcessField* fields = settings->fields;
for (; *fields; fields++)
ColumnsPanel_add(super, *fields, dynamicColumns);

return this;
}

Expand All @@ -143,7 +170,8 @@ void ColumnsPanel_update(Panel* super) {
for (int i = 0; i < size; i++) {
int key = ((ListItem*) Panel_get(super, i))->key;
this->settings->fields[i] = key;
this->settings->flags |= Process_fields[key].flags;
if (key < LAST_PROCESSFIELD)
this->settings->flags |= Process_fields[key].flags;
}
this->settings->fields[size] = 0;
}
1 change: 1 addition & 0 deletions ColumnsPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ in the source distribution for its full text.
#include <stdbool.h>

#include "Panel.h"
#include "ProcessList.h"
#include "Settings.h"


Expand Down
15 changes: 12 additions & 3 deletions CommandLine.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ in the source distribution for its full text.

#include "Action.h"
#include "CRT.h"
#include "DynamicColumn.h"
#include "DynamicMeter.h"
#include "Hashtable.h"
#include "Header.h"
Expand Down Expand Up @@ -292,10 +293,14 @@ int CommandLine_run(const char* name, int argc, char** argv) {
Process_setupColumnWidths();

UsersTable* ut = UsersTable_new();
Hashtable* dc = DynamicColumns_new();
Hashtable* dm = DynamicMeters_new();
ProcessList* pl = ProcessList_new(ut, dm, flags.pidMatchList, flags.userId);
if (!dc)
dc = Hashtable_new(0, true);

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

Settings* settings = Settings_new(pl->activeCPUs, dc);
pl->settings = settings;

Header* header = Header_new(pl, settings, 2);
Expand Down Expand Up @@ -384,8 +389,12 @@ int CommandLine_run(const char* name, int argc, char** argv) {
if (flags.pidMatchList)
Hashtable_delete(flags.pidMatchList);

/* Delete Settings last, since it can get accessed in the crash handler */
/* Delete these last, since they can get accessed in the crash handler */
Settings_delete(settings);
if (dc)
Hashtable_delete(dc);
if (dm)
Hashtable_delete(dm);

return 0;
}
60 changes: 60 additions & 0 deletions DynamicColumn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
htop - DynamicColumn.c
(C) 2021 Sohaib Mohammed
(C) 2021 htop dev team
(C) 2021 Red Hat, Inc. All Rights Reserved.
Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/

#include "config.h" // IWYU pragma: keep

#include "DynamicColumn.h"

#include <stddef.h>

#include "Platform.h"
#include "ProcessList.h"
#include "RichString.h"
#include "XUtils.h"


Hashtable* DynamicColumns_new(void) {
return Platform_dynamicColumns();
}

const char* DynamicColumn_init(unsigned int key) {
return Platform_dynamicColumnInit(key);
}

typedef struct {
const char* name;
const DynamicColumn* data;
unsigned int key;
} DynamicIterator;

static void DynamicColumn_compare(ht_key_t key, void* value, void* data) {
const DynamicColumn* column = (const DynamicColumn*)value;
DynamicIterator* iter = (DynamicIterator*)data;
if (String_eq(iter->name, column->name)) {
iter->data = column;
iter->key = key;
}
}

const DynamicColumn* DynamicColumn_search(Hashtable* dynamics, const char* name, unsigned int* key) {
DynamicIterator iter = { .key = 0, .data = NULL, .name = name };
if (dynamics)
Hashtable_foreach(dynamics, DynamicColumn_compare, &iter);
if (key)
*key = iter.key;
return iter.data;
}

const DynamicColumn* DynamicColumn_lookup(Hashtable* dynamics, unsigned int key) {
return (const DynamicColumn*) Hashtable_get(dynamics, key);
}

bool DynamicColumn_writeField(const Process* proc, RichString* str, unsigned int key) {
return Platform_dynamicColumnWriteField(proc, str, key);
}
31 changes: 31 additions & 0 deletions DynamicColumn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef HEADER_DynamicColumn
#define HEADER_DynamicColumn

#include "Hashtable.h"
#include "Process.h"
#include "ProcessList.h"
#include "RichString.h"


#define DYNAMIC_MAX_COLUMN_WIDTH 28
#define DYNAMIC_DEFAULT_COLUMN_WIDTH -5

typedef struct DynamicColumn_ {
char name[32]; /* unique, internal-only name */
char* heading; /* displayed in main screen */
char* caption; /* displayed in setup menu (short name) */
char* description; /* displayed in setup menu (detail) */
int width; /* display width +/- for value alignment */
} DynamicColumn;

Hashtable* DynamicColumns_new(void);

const char* DynamicColumn_init(unsigned int key);

const DynamicColumn* DynamicColumn_lookup(Hashtable* dynamics, unsigned int key);

const DynamicColumn* DynamicColumn_search(Hashtable* dynamics, const char* name, unsigned int* field);

bool DynamicColumn_writeField(const Process* proc, RichString* str, unsigned int key);

#endif
4 changes: 2 additions & 2 deletions DynamicMeter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef HEADER_DynamicMeter
#define HEADER_DynamicMeter

#include <stdbool.h>

#include "Hashtable.h"
#include "Meter.h"

Expand All @@ -11,8 +13,6 @@ typedef struct DynamicMeter_ {
char* description;
unsigned int type;
double maximum;

void* dynamicData; /* platform-specific meter data */
} DynamicMeter;

Hashtable* DynamicMeters_new(void);
Expand Down
Loading

0 comments on commit 6f2021f

Please sign in to comment.