diff options
author | Nathan Scott <nathans@redhat.com> | 2021-03-25 17:24:14 +1100 |
---|---|---|
committer | Nathan Scott <nathans@redhat.com> | 2021-06-09 17:09:29 +1000 |
commit | 5dfb5242370610dfad09c855925224c587edbfbf (patch) | |
tree | c39bdb0e1e8e5e443b96e3e6be7685ad90ff9746 /pcp | |
parent | b424a5b137797ef1b921b7ed1e6ee6521b7b65b0 (diff) |
Implement command line and environment handling for pcp htop.
Diffstat (limited to 'pcp')
-rw-r--r-- | pcp/PCPProcessList.c | 10 | ||||
-rw-r--r-- | pcp/Platform.c | 64 | ||||
-rw-r--r-- | pcp/Platform.h | 17 |
3 files changed, 83 insertions, 8 deletions
diff --git a/pcp/PCPProcessList.c b/pcp/PCPProcessList.c index 780862b3..847765a2 100644 --- a/pcp/PCPProcessList.c +++ b/pcp/PCPProcessList.c @@ -31,7 +31,7 @@ static int PCPProcessList_computeCPUcount(void) { static void PCPProcessList_updateCPUcount(PCPProcessList* this) { ProcessList* pl = &(this->super); - int cpus = PCPProcessList_computeCPUcount(); + unsigned int cpus = PCPProcessList_computeCPUcount(); if (cpus == pl->cpuCount) return; @@ -40,7 +40,7 @@ static void PCPProcessList_updateCPUcount(PCPProcessList* this) { free(this->values); 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->values = xCalloc(cpus, sizeof(pmAtomValue)); } @@ -78,7 +78,7 @@ void ProcessList_delete(ProcessList* pl) { PCPProcessList* this = (PCPProcessList*) pl; ProcessList_done(pl); 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); 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_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_updatePerCPUTime(this, PCP_PERCPU_USER, CPU_USER_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_STEAL, CPU_STEAL_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]); if (settings->showCPUFrequency) diff --git a/pcp/Platform.c b/pcp/Platform.c index 70af0a85..2a591dee 100644 --- a/pcp/Platform.c +++ b/pcp/Platform.c @@ -57,7 +57,7 @@ typedef struct Platform_ { pmResult* result; /* sample values result indexed by Metric */ 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 ncpu; /* maximum processor count configured */ } Platform; @@ -403,14 +403,28 @@ static int Platform_addMetric(Metric id, const char *name) { return ++pcp->total; } +/* global state from the environment and command line arguments */ +pmOptions opts; + void Platform_init(void) { - int sts = pmNewContext(PM_CONTEXT_HOST, "local:"); - if (sts < 0) + const char* host = opts.nhosts > 0 ? opts.hosts[0] : "local:"; + + 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); if (sts < 0) { fprintf(stderr, "Cannot setup PCP metric source: %s\n", pmErrStr(sts)); 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->context = sts; pcp->fetch = xCalloc(PCP_METRIC_COUNT, sizeof(pmID)); @@ -795,3 +809,47 @@ void Platform_getBattery(double* level, ACPresence* isOnAC) { *level = NAN; *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; +} diff --git a/pcp/Platform.h b/pcp/Platform.h index 1661be77..33ea0db7 100644 --- a/pcp/Platform.h +++ b/pcp/Platform.h @@ -87,6 +87,23 @@ void Platform_getHostname(char* buffer, size_t size); 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_ { PCP_CONTROL_THREADS, /* proc.control.perclient.threads */ |