Implement command line and environment handling for pcp htop.

This commit is contained in:
Nathan Scott 2021-03-25 17:24:14 +11:00
parent b424a5b137
commit 5dfb524237
6 changed files with 116 additions and 11 deletions

3
.gitignore vendored
View File

@ -1,5 +1,6 @@
# the binary: # the binaries:
htop htop
pcp-htop
# all object files # all object files
*.o *.o

View File

@ -343,8 +343,7 @@ pcp_platform_headers = \
zfs/ZfsArcStats.h \ zfs/ZfsArcStats.h \
zfs/ZfsCompressedArcMeter.h zfs/ZfsCompressedArcMeter.h
if HTOP_PCP pcp_platform_sources = \
myhtopplatsources = \
pcp/PCPProcess.c \ pcp/PCPProcess.c \
pcp/PCPProcessList.c \ pcp/PCPProcessList.c \
pcp/Platform.c \ pcp/Platform.c \
@ -353,7 +352,11 @@ myhtopplatsources = \
zfs/ZfsArcMeter.c \ zfs/ZfsArcMeter.c \
zfs/ZfsCompressedArcMeter.c zfs/ZfsCompressedArcMeter.c
if HTOP_PCP
myhtopplatprogram = pcp-htop
myhtopplatheaders = $(pcp_platform_headers) myhtopplatheaders = $(pcp_platform_headers)
myhtopplatsources = $(pcp_platform_sources)
pcp_htop_SOURCES = $(htop_SOURCES)
endif endif
# Unsupported # Unsupported

26
pcp-htop.c Normal file
View File

@ -0,0 +1,26 @@
/*
htop - pcp-htop.c
(C) 2004-2011 Hisham H. Muhammad
(C) 2020-2021 htop dev team
Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/
#include "config.h" // IWYU pragma: keep
#include <pcp/pmapi.h>
#include "CommandLine.h"
#include "Platform.h"
int main(int argc, char** argv) {
const char* name = "pcp-htop";
pmSetProgname(name);
/* extract environment variables */
opts.flags |= PM_OPTFLAG_ENV_ONLY;
pmGetOptions(argc, argv, &opts);
return CommandLine_run(name, argc, argv);
}

View File

@ -31,7 +31,7 @@ static int PCPProcessList_computeCPUcount(void) {
static void PCPProcessList_updateCPUcount(PCPProcessList* this) { static void PCPProcessList_updateCPUcount(PCPProcessList* this) {
ProcessList* pl = &(this->super); ProcessList* pl = &(this->super);
int cpus = PCPProcessList_computeCPUcount(); unsigned int cpus = PCPProcessList_computeCPUcount();
if (cpus == pl->cpuCount) if (cpus == pl->cpuCount)
return; return;
@ -40,7 +40,7 @@ static void PCPProcessList_updateCPUcount(PCPProcessList* this) {
free(this->values); free(this->values);
this->percpu = xCalloc(cpus, sizeof(pmAtomValue *)); this->percpu = xCalloc(cpus, sizeof(pmAtomValue *));
for (int i = 0; i < cpus; i++) for (unsigned int i = 0; i < cpus; i++)
this->percpu[i] = xCalloc(CPU_METRIC_COUNT, sizeof(pmAtomValue)); this->percpu[i] = xCalloc(CPU_METRIC_COUNT, sizeof(pmAtomValue));
this->values = xCalloc(cpus, sizeof(pmAtomValue)); this->values = xCalloc(cpus, sizeof(pmAtomValue));
} }
@ -78,7 +78,7 @@ void ProcessList_delete(ProcessList* pl) {
PCPProcessList* this = (PCPProcessList*) pl; PCPProcessList* this = (PCPProcessList*) pl;
ProcessList_done(pl); ProcessList_done(pl);
free(this->values); free(this->values);
for (int i = 0; i < pl->cpuCount; i++) for (unsigned int i = 0; i < pl->cpuCount; i++)
free(this->percpu[i]); free(this->percpu[i]);
free(this->percpu); free(this->percpu);
free(this->cpu); free(this->cpu);
@ -716,7 +716,7 @@ static void PCPProcessList_updateHeader(ProcessList* super, const Settings* sett
PCPProcessList_updateAllCPUTime(this, PCP_CPU_GUEST, CPU_GUEST_TIME); PCPProcessList_updateAllCPUTime(this, PCP_CPU_GUEST, CPU_GUEST_TIME);
PCPProcessList_deriveCPUTime(this->cpu); PCPProcessList_deriveCPUTime(this->cpu);
for (int i = 0; i < super->cpuCount; i++) for (unsigned int i = 0; i < super->cpuCount; i++)
PCPProcessList_backupCPUTime(this->percpu[i]); PCPProcessList_backupCPUTime(this->percpu[i]);
PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_USER, CPU_USER_TIME); PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_USER, CPU_USER_TIME);
PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_NICE, CPU_NICE_TIME); PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_NICE, CPU_NICE_TIME);
@ -727,7 +727,7 @@ static void PCPProcessList_updateHeader(ProcessList* super, const Settings* sett
PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_SOFTIRQ, CPU_SOFTIRQ_TIME); PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_SOFTIRQ, CPU_SOFTIRQ_TIME);
PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_STEAL, CPU_STEAL_TIME); PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_STEAL, CPU_STEAL_TIME);
PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_GUEST, CPU_GUEST_TIME); PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_GUEST, CPU_GUEST_TIME);
for (int i = 0; i < super->cpuCount; i++) for (unsigned int i = 0; i < super->cpuCount; i++)
PCPProcessList_deriveCPUTime(this->percpu[i]); PCPProcessList_deriveCPUTime(this->percpu[i]);
if (settings->showCPUFrequency) if (settings->showCPUFrequency)

View File

@ -57,7 +57,7 @@ typedef struct Platform_ {
pmResult* result; /* sample values result indexed by Metric */ pmResult* result; /* sample values result indexed by Metric */
long long btime; /* boottime in seconds since the epoch */ long long btime; /* boottime in seconds since the epoch */
char *release; /* uname and distro from this context */ char* release; /* uname and distro from this context */
int pidmax; /* maximum platform process identifier */ int pidmax; /* maximum platform process identifier */
int ncpu; /* maximum processor count configured */ int ncpu; /* maximum processor count configured */
} Platform; } Platform;
@ -403,14 +403,28 @@ static int Platform_addMetric(Metric id, const char *name) {
return ++pcp->total; return ++pcp->total;
} }
/* global state from the environment and command line arguments */
pmOptions opts;
void Platform_init(void) { void Platform_init(void) {
int sts = pmNewContext(PM_CONTEXT_HOST, "local:"); const char* host = opts.nhosts > 0 ? opts.hosts[0] : "local:";
if (sts < 0)
int sts;
sts = pmNewContext(PM_CONTEXT_HOST, host);
/* with no host requested, fallback to PM_CONTEXT_LOCAL shared libraries */
if (sts < 0 && opts.nhosts == 0)
sts = pmNewContext(PM_CONTEXT_LOCAL, NULL); sts = pmNewContext(PM_CONTEXT_LOCAL, NULL);
if (sts < 0) { if (sts < 0) {
fprintf(stderr, "Cannot setup PCP metric source: %s\n", pmErrStr(sts)); fprintf(stderr, "Cannot setup PCP metric source: %s\n", pmErrStr(sts));
exit(1); exit(1);
} }
/* setup timezones and other general startup preparation completion */
pmGetContextOptions(sts, &opts);
if (opts.errors) {
pmflush();
exit(1);
}
pcp = xCalloc(1, sizeof(Platform)); pcp = xCalloc(1, sizeof(Platform));
pcp->context = sts; pcp->context = sts;
pcp->fetch = xCalloc(PCP_METRIC_COUNT, sizeof(pmID)); pcp->fetch = xCalloc(PCP_METRIC_COUNT, sizeof(pmID));
@ -795,3 +809,47 @@ void Platform_getBattery(double* level, ACPresence* isOnAC) {
*level = NAN; *level = NAN;
*isOnAC = AC_ERROR; *isOnAC = AC_ERROR;
} }
void Platform_longOptionsUsage(ATTR_UNUSED const char* name) {
printf(
" --host=HOSTSPEC metrics source is PMCD at HOSTSPEC [see PCPIntro(1)]\n"
" --hostzone set reporting timezone to local time of metrics source\n"
" --timezone=TZ set reporting timezone\n");
}
bool Platform_getLongOption(int opt, ATTR_UNUSED int argc, char** argv) {
/* libpcp export without a header definition */
extern void __pmAddOptHost(pmOptions *, char *);
switch (opt) {
case PLATFORM_LONGOPT_HOST: /* --host=HOSTSPEC */
if (argv[optind][0] == '\0')
return false;
__pmAddOptHost(&opts, optarg);
return true;
case PLATFORM_LONGOPT_HOSTZONE: /* --hostzone */
if (opts.timezone) {
pmprintf("%s: at most one of -Z and -z allowed\n", pmGetProgname());
opts.errors++;
} else {
opts.tzflag = 1;
}
return true;
case PLATFORM_LONGOPT_TIMEZONE: /* --timezone=TZ */
if (argv[optind][0] == '\0')
return false;
if (opts.tzflag) {
pmprintf("%s: at most one of -Z and -z allowed\n", pmGetProgname());
opts.errors++;
} else {
opts.timezone = optarg;
}
return true;
default:
break;
}
return false;
}

View File

@ -87,6 +87,23 @@ void Platform_getHostname(char* buffer, size_t size);
void Platform_getRelease(char** string); void Platform_getRelease(char** string);
enum {
PLATFORM_LONGOPT_HOST = 128,
PLATFORM_LONGOPT_TIMEZONE,
PLATFORM_LONGOPT_HOSTZONE,
};
#define PLATFORM_LONG_OPTIONS \
{PMLONGOPT_HOST, optional_argument, 0, PLATFORM_LONGOPT_HOST}, \
{PMLONGOPT_TIMEZONE, optional_argument, 0, PLATFORM_LONGOPT_TIMEZONE}, \
{PMLONGOPT_HOSTZONE, optional_argument, 0, PLATFORM_LONGOPT_HOSTZONE}, \
void Platform_longOptionsUsage(const char* name);
bool Platform_getLongOption(int opt, int argc, char** argv);
extern pmOptions opts;
typedef enum Metric_ { typedef enum Metric_ {
PCP_CONTROL_THREADS, /* proc.control.perclient.threads */ PCP_CONTROL_THREADS, /* proc.control.perclient.threads */