diff options
author | Sohaib Mohamed <sohaib.amhmd@gmail.com> | 2021-07-11 03:11:29 +0200 |
---|---|---|
committer | Nathan Scott <nathans@redhat.com> | 2021-08-13 07:32:57 +0200 |
commit | 6f2021f3d95e02fc54e59fdeeb006e34c209b9c3 (patch) | |
tree | a31ea1264b871e5a7e7785398a382a7813eb1749 /Settings.c | |
parent | ed82ce6456f0f904a0ab2b346b85d7cf46df109c (diff) |
PCP: support for 'dynamic columns' added at runtime
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.
Diffstat (limited to 'Settings.c')
-rw-r--r-- | Settings.c | 58 |
1 files changed, 43 insertions, 15 deletions
@@ -8,12 +8,14 @@ in the source distribution for its full text. #include "Settings.h" #include <errno.h> +#include <limits.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include "CRT.h" +#include "DynamicColumn.h" #include "Macros.h" #include "Meter.h" #include "Platform.h" @@ -106,22 +108,42 @@ static void Settings_defaultMeters(Settings* this, unsigned int initialCpuCount) this->columns[1].modes[r++] = TEXT_METERMODE; } -static void readFields(ProcessField* fields, uint32_t* flags, const char* line) { +static void Settings_readFields(Settings* settings, const char* line) { char* trim = String_trim(line); char** ids = String_split(trim, ' ', NULL); free(trim); - int i, j; - *flags = 0; - for (j = 0, i = 0; i < LAST_PROCESSFIELD && ids[i]; i++) { + + settings->flags = 0; + + unsigned int i, j; + for (j = 0, i = 0; ids[i]; i++) { + if (j >= UINT_MAX / sizeof(ProcessField)) + continue; + if (j >= LAST_PROCESSFIELD) { + settings->fields = xRealloc(settings->fields, j * sizeof(ProcessField)); + memset(&settings->fields[j], 0, sizeof(ProcessField)); + } + + // Dynamically-defined columns are always stored by-name. + char* end, dynamic[32] = {0}; + if (sscanf(ids[i], "Dynamic(%30s)", dynamic)) { + if ((end = strrchr(dynamic, ')')) == NULL) + continue; + *end = '\0'; + unsigned int key; + if (!DynamicColumn_search(settings->dynamicColumns, dynamic, &key)) + continue; + settings->fields[j++] = key; + continue; + } // This "+1" is for compatibility with the older enum format. int id = atoi(ids[i]) + 1; if (id > 0 && id < LAST_PROCESSFIELD && Process_fields[id].name) { - fields[j] = id; - *flags |= Process_fields[id].flags; - j++; + settings->flags |= Process_fields[id].flags; + settings->fields[j++] = id; } } - fields[j] = NULL_PROCESSFIELD; + settings->fields[j] = NULL_PROCESSFIELD; String_freeArray(ids); } @@ -145,7 +167,7 @@ static bool Settings_read(Settings* this, const char* fileName, unsigned int ini continue; } if (String_eq(option[0], "fields")) { - readFields(this->fields, &(this->flags), option[1]); + Settings_readFields(this, option[1]); didReadFields = true; } else if (String_eq(option[0], "sort_key")) { // This "+1" is for compatibility with the older enum format. @@ -256,12 +278,17 @@ static bool Settings_read(Settings* this, const char* fileName, unsigned int ini return didReadFields; } -static void writeFields(FILE* fd, const ProcessField* fields, const char* name) { +static void writeFields(FILE* fd, const ProcessField* fields, Hashtable* columns, const char* name) { fprintf(fd, "%s=", name); const char* sep = ""; - for (int i = 0; fields[i]; i++) { - // This "-1" is for compatibility with the older enum format. - fprintf(fd, "%s%d", sep, (int) fields[i] - 1); + for (unsigned int i = 0; fields[i]; i++) { + if (fields[i] >= LAST_PROCESSFIELD) { + const DynamicColumn* column = DynamicColumn_lookup(columns, fields[i]); + fprintf(fd, "%sDynamic(%s)", sep, column->name); + } else { + // This "-1" is for compatibility with the older enum format. + fprintf(fd, "%s%d", sep, (int) fields[i] - 1); + } sep = " "; } fprintf(fd, "\n"); @@ -299,7 +326,7 @@ int Settings_write(const Settings* this, bool onCrash) { fprintf(fd, "# Beware! This file is rewritten by htop when settings are changed in the interface.\n"); fprintf(fd, "# The parser is also very primitive, and not human-friendly.\n"); } - writeFields(fd, this->fields, "fields"); + writeFields(fd, this->fields, this->dynamicColumns, "fields"); // This "-1" is for compatibility with the older enum format. fprintf(fd, "sort_key=%d\n", (int) this->sortKey - 1); fprintf(fd, "sort_direction=%d\n", (int) this->direction); @@ -361,9 +388,10 @@ int Settings_write(const Settings* this, bool onCrash) { return r; } -Settings* Settings_new(unsigned int initialCpuCount) { +Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicColumns) { Settings* this = xCalloc(1, sizeof(Settings)); + this->dynamicColumns = dynamicColumns; this->sortKey = PERCENT_CPU; this->treeSortKey = PID; this->direction = -1; |