Add time handling interfaces for the pcp platform

Related to https://github.com/htop-dev/htop/pull/574
This commit is contained in:
Nathan Scott 2021-04-08 10:13:43 +10:00
parent 5dfb524237
commit d4a2587568
3 changed files with 48 additions and 13 deletions

View File

@ -44,7 +44,6 @@ myhtopsources = \
Hashtable.c \ Hashtable.c \
Header.c \ Header.c \
HostnameMeter.c \ HostnameMeter.c \
htop.c \
IncSet.c \ IncSet.c \
InfoScreen.c \ InfoScreen.c \
ListItem.c \ ListItem.c \
@ -356,7 +355,7 @@ if HTOP_PCP
myhtopplatprogram = pcp-htop myhtopplatprogram = pcp-htop
myhtopplatheaders = $(pcp_platform_headers) myhtopplatheaders = $(pcp_platform_headers)
myhtopplatsources = $(pcp_platform_sources) myhtopplatsources = $(pcp_platform_sources)
pcp_htop_SOURCES = $(htop_SOURCES) pcp_htop_SOURCES = $(myhtopplatprogram).c $(myhtopheaders) $(myhtopplatheaders) $(myhtopsources) $(myhtopplatsources)
endif endif
# Unsupported # Unsupported
@ -384,7 +383,7 @@ endif
# ---- # ----
bin_PROGRAMS = $(myhtopplatprogram) bin_PROGRAMS = $(myhtopplatprogram)
htop_SOURCES = $(myhtopheaders) $(myhtopplatheaders) $(myhtopsources) $(myhtopplatsources) htop_SOURCES = $(myhtopplatprogram).c $(myhtopheaders) $(myhtopplatheaders) $(myhtopsources) $(myhtopplatsources)
nodist_htop_SOURCES = config.h nodist_htop_SOURCES = config.h
target: target:

View File

@ -9,8 +9,6 @@ in the source distribution for its full text.
#include "config.h" #include "config.h"
#include "Platform.h"
#include <math.h> #include <math.h>
#include "BatteryMeter.h" #include "BatteryMeter.h"
@ -31,6 +29,7 @@ in the source distribution for its full text.
#include "Panel.h" #include "Panel.h"
#include "PCPProcess.h" #include "PCPProcess.h"
#include "PCPProcessList.h" #include "PCPProcessList.h"
#include "Platform.h"
#include "ProcessList.h" #include "ProcessList.h"
#include "ProvideCurses.h" #include "ProvideCurses.h"
#include "Settings.h" #include "Settings.h"
@ -55,6 +54,7 @@ typedef struct Platform_ {
pmID* fetch; /* enabled identifiers for sampling */ pmID* fetch; /* enabled identifiers for sampling */
pmDesc* descs; /* metric desc array indexed by Metric */ pmDesc* descs; /* metric desc array indexed by Metric */
pmResult* result; /* sample values result indexed by Metric */ pmResult* result; /* sample values result indexed by Metric */
struct timeval offset; /* time offset used in archive mode only */
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 */
@ -381,7 +381,7 @@ bool Metric_fetch(struct timeval *timestamp) {
return false; return false;
} }
if (timestamp) if (timestamp)
*timestamp = pcp->result->timestamp; *timestamp = pcp->result->timestamp;
return true; return true;
} }
@ -407,13 +407,23 @@ static int Platform_addMetric(Metric id, const char *name) {
pmOptions opts; pmOptions opts;
void Platform_init(void) { void Platform_init(void) {
const char* host = opts.nhosts > 0 ? opts.hosts[0] : "local:"; const char* source;
if (opts.context == PM_CONTEXT_ARCHIVE) {
source = opts.archives[0];
} else if (opts.context == PM_CONTEXT_HOST) {
source = opts.nhosts > 0 ? opts.hosts[0] : "local:";
} else {
opts.context = PM_CONTEXT_HOST;
source = "local:";
}
int sts; int sts;
sts = pmNewContext(PM_CONTEXT_HOST, host); sts = pmNewContext(opts.context, source);
/* with no host requested, fallback to PM_CONTEXT_LOCAL shared libraries */ /* with no host requested, fallback to PM_CONTEXT_LOCAL shared libraries */
if (sts < 0 && opts.nhosts == 0) if (sts < 0 && opts.context == PM_CONTEXT_HOST && opts.nhosts == 0) {
sts = pmNewContext(PM_CONTEXT_LOCAL, NULL); opts.context = PM_CONTEXT_LOCAL;
sts = pmNewContext(opts.context, 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);
@ -432,6 +442,11 @@ void Platform_init(void) {
pcp->names = xCalloc(PCP_METRIC_COUNT, sizeof(char*)); pcp->names = xCalloc(PCP_METRIC_COUNT, sizeof(char*));
pcp->descs = xCalloc(PCP_METRIC_COUNT, sizeof(pmDesc)); pcp->descs = xCalloc(PCP_METRIC_COUNT, sizeof(pmDesc));
if (opts.context == PM_CONTEXT_ARCHIVE) {
gettimeofday(&pcp->offset, NULL);
pmtimevalDec(&pcp->offset, &opts.start);
}
for (unsigned int i = 0; i < PCP_METRIC_COUNT; i++) for (unsigned int i = 0; i < PCP_METRIC_COUNT; i++)
Platform_addMetric(i, Platform_metricNames[i]); Platform_addMetric(i, Platform_metricNames[i]);
@ -825,7 +840,7 @@ bool Platform_getLongOption(int opt, ATTR_UNUSED int argc, char** argv) {
case PLATFORM_LONGOPT_HOST: /* --host=HOSTSPEC */ case PLATFORM_LONGOPT_HOST: /* --host=HOSTSPEC */
if (argv[optind][0] == '\0') if (argv[optind][0] == '\0')
return false; return false;
__pmAddOptHost(&opts, optarg); __pmAddOptHost(&opts, optarg);
return true; return true;
case PLATFORM_LONGOPT_HOSTZONE: /* --hostzone */ case PLATFORM_LONGOPT_HOSTZONE: /* --hostzone */
@ -843,9 +858,9 @@ bool Platform_getLongOption(int opt, ATTR_UNUSED int argc, char** argv) {
if (opts.tzflag) { if (opts.tzflag) {
pmprintf("%s: at most one of -Z and -z allowed\n", pmGetProgname()); pmprintf("%s: at most one of -Z and -z allowed\n", pmGetProgname());
opts.errors++; opts.errors++;
} else { } else {
opts.timezone = optarg; opts.timezone = optarg;
} }
return true; return true;
default: default:
@ -853,3 +868,20 @@ bool Platform_getLongOption(int opt, ATTR_UNUSED int argc, char** argv) {
} }
return false; return false;
} }
void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec) {
if (gettimeofday(tv, NULL) == 0) {
/* shift by start offset to stay in lock-step with realtime (archives) */
if (pcp->offset.tv_sec || pcp->offset.tv_usec)
pmtimevalDec(tv, &pcp->offset);
*msec = ((uint64_t)tv->tv_sec * 1000) + ((uint64_t)tv->tv_usec / 1000);
} else {
memset(tv, 0, sizeof(struct timeval));
*msec = 0;
}
}
void Platform_gettime_monotonic(uint64_t* msec) {
struct timeval* tv = &pcp->result->timestamp;
*msec = ((uint64_t)tv->tv_sec * 1000) + ((uint64_t)tv->tv_usec / 1000);
}

View File

@ -250,4 +250,8 @@ int Metric_instanceOffset(Metric metric, int inst);
pmAtomValue *Metric_instance(Metric metric, int inst, int offset, pmAtomValue *atom, int type); pmAtomValue *Metric_instance(Metric metric, int inst, int offset, pmAtomValue *atom, int type);
void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec);
void Platform_gettime_monotonic(uint64_t* msec);
#endif #endif