Skip to content

Commit

Permalink
Use pmLookupDescs(3) function if available from libpcp
Browse files Browse the repository at this point in the history
This is a relative new, single-round-trip variant of the
pmLookupDesc(3) function for metric descriptors lookup.
  • Loading branch information
natoscott authored and BenBE committed Sep 14, 2022
1 parent 5552dc9 commit d58180b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
4 changes: 4 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ if test "$my_htop_platform" = darwin; then
AC_CHECK_FUNCS([mach_timebase_info])
fi

if test "$my_htop_platform" = pcp; then
AC_CHECK_FUNCS([pmLookupDescs])
fi

if test "$my_htop_platform" = linux && test "x$enable_static" = xyes; then
AC_CHECK_LIB([systemd], [sd_bus_open_system])
fi
Expand Down
58 changes: 41 additions & 17 deletions pcp/Platform.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
htop - linux/Platform.c
(C) 2014 Hisham H. Muhammad
(C) 2020-2021 htop dev team
(C) 2020-2021 Red Hat, Inc.
(C) 2020-2022 htop dev team
(C) 2020-2022 Red Hat, Inc.
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/
Expand Down Expand Up @@ -248,6 +248,37 @@ static const char* Platform_metricNames[] = {
[PCP_METRIC_COUNT] = NULL
};

#ifndef HAVE_PMLOOKUPDESCS
/*
* pmLookupDescs(3) exists in latest versions of libpcp (5.3.6+),
* but for older versions we provide an implementation here. This
* involves multiple round trips to pmcd though, which the latest
* libpcp version avoids by using a protocol extension. In time,
* perhaps in a few years, we could remove this back-compat code.
*/
int pmLookupDescs(int numpmid, pmID *pmids, pmDesc *descs) {
int count = 0;

for (int i = 0; i < numpmid; i++) {
/* expect some metrics to be missing - e.g. PMDA not available */
if (pmids[i] == PM_ID_NULL)
continue;

int sts = pmLookupDesc(pmids[i], &descs[i]);
if (sts < 0) {
if (pmDebugOptions.appl0)
fprintf(stderr, "Error: cannot lookup metric %s(%s): %s\n",
pcp->names[i], pmIDStr(pcp->pmids[i]), pmErrStr(sts));
pmids[i] = PM_ID_NULL;
continue;
}

count++;
}
return count;
}
#endif

size_t Platform_addMetric(PCPMetric id, const char* name) {
unsigned int i = (unsigned int)id;

Expand Down Expand Up @@ -325,21 +356,14 @@ bool Platform_init(void) {
return false;
}

for (size_t i = 0; i < pcp->totalMetrics; i++) {
pcp->fetch[i] = PM_ID_NULL; /* default is to not sample */

/* expect some metrics to be missing - e.g. PMDA not available */
if (pcp->pmids[i] == PM_ID_NULL)
continue;

sts = pmLookupDesc(pcp->pmids[i], &pcp->descs[i]);
if (sts < 0) {
if (pmDebugOptions.appl0)
fprintf(stderr, "Error: cannot lookup metric %s(%s): %s\n",
pcp->names[i], pmIDStr(pcp->pmids[i]), pmErrStr(sts));
pcp->pmids[i] = PM_ID_NULL;
continue;
}
sts = pmLookupDescs(pcp->totalMetrics, pcp->pmids, pcp->descs);
if (sts < 1) {
if (sts < 0)
fprintf(stderr, "Error: cannot lookup descriptors: %s\n", pmErrStr(sts));
else /* ensure we have at least one valid metric to work with */
fprintf(stderr, "Error: cannot find a single valid metric, exiting\n");
Platform_done();
return false;
}

/* set proc.control.perclient.threads to 1 for live contexts */
Expand Down

0 comments on commit d58180b

Please sign in to comment.