Blame SOURCES/redhat-bugzilla-1973833.patch

e94686
commit 6b907e90c74fce82d6b712493d8b362bdd1a1ec1
e94686
Author: Nathan Scott <nathans@redhat.com>
e94686
Date:   Tue Dec 14 08:54:14 2021 +1100
e94686
e94686
    pmlogconf: switch to the bulk pmLookupDescs(3) interface
e94686
    
e94686
    No functional change, all existing regression tests pass.
e94686
    
e94686
    Related to Red Hat BZ #1973833.
e94686
e94686
diff --git a/src/pmlogconf/pmlogconf.c b/src/pmlogconf/pmlogconf.c
e94686
index fa1156859d..ef4fc08bbd 100644
e94686
--- a/src/pmlogconf/pmlogconf.c
e94686
+++ b/src/pmlogconf/pmlogconf.c
e94686
@@ -473,13 +473,19 @@ fetch_groups(void)
e94686
 {
e94686
     static pmResult	*result;
e94686
     const char		**names;
e94686
+    pmDesc		*descs;
e94686
     pmID		*pmids;
e94686
-    int			i, n, sts;
e94686
+    int			i, n, sts, count;
e94686
 
e94686
-    /* prepare arrays of names and identifiers for PMAPI metric lookup */
e94686
+    /* prepare arrays of names, descriptors and IDs for PMAPI metric lookup */
e94686
     if ((names = calloc(ngroups, sizeof(char *))) == NULL)
e94686
 	return -ENOMEM;
e94686
+    if ((descs = calloc(ngroups, sizeof(pmDesc))) == NULL) {
e94686
+	free(names);
e94686
+	return -ENOMEM;
e94686
+    }
e94686
     if ((pmids = calloc(ngroups, sizeof(pmID))) == NULL) {
e94686
+	free(descs);
e94686
 	free(names);
e94686
 	return -ENOMEM;
e94686
     }
e94686
@@ -490,15 +496,16 @@ fetch_groups(void)
e94686
 	    continue;
e94686
 	names[n++] = (const char *)groups[i].metric;
e94686
     }
e94686
+    count = n;
e94686
 
e94686
-    if ((sts = pmLookupName(n, names, pmids)) < 0) {
e94686
-	if (n == 1)
e94686
+    if ((sts = pmLookupName(count, names, pmids)) < 0) {
e94686
+	if (count == 1)
e94686
 	    groups[0].pmid = PM_ID_NULL;
e94686
 	else
e94686
 	    fprintf(stderr, "%s: cannot lookup metric names: %s\n",
e94686
 			    pmGetProgname(), pmErrStr(sts));
e94686
     }
e94686
-    else if ((sts = pmFetch(n, pmids, &result)) < 0) {
e94686
+    else if ((sts = pmFetch(count, pmids, &result)) < 0) {
e94686
 	fprintf(stderr, "%s: cannot fetch metric values: %s\n",
e94686
 			pmGetProgname(), pmErrStr(sts));
e94686
     }
e94686
@@ -510,6 +517,13 @@ fetch_groups(void)
e94686
 	    else
e94686
 		groups[i].pmid = pmids[n++];
e94686
 	}
e94686
+	/* descriptor lookup, descs_hash handles failure here */
e94686
+	(void) pmLookupDescs(count, pmids, descs);
e94686
+
e94686
+	/* create a hash over the descs for quick PMID lookup */
e94686
+	if ((sts = descs_hash(count, descs)) < 0)
e94686
+	    fprintf(stderr, "%s: cannot hash metric descs: %s\n",
e94686
+			    pmGetProgname(), pmErrStr(sts));
e94686
 	/* create a hash over the result for quick PMID lookup */
e94686
 	if ((sts = values_hash(result)) < 0)
e94686
 	    fprintf(stderr, "%s: cannot hash metric values: %s\n",
e94686
@@ -806,14 +820,16 @@ evaluate_string_regexp(group_t *group, regex_cmp_t compare)
e94686
     int			i, found;
e94686
     pmValueSet		*vsp;
e94686
     pmValue		*vp;
e94686
+    pmDesc		*dp;
e94686
     pmAtomValue		atom;
e94686
     regex_t		regex;
e94686
     int			sts, type;
e94686
 
e94686
-    if ((vsp = metric_values(group->pmid)) == NULL)
e94686
+    if ((vsp = metric_values(group->pmid)) == NULL ||
e94686
+        (dp = metric_desc(group->pmid)) == NULL)
e94686
 	return 0;
e94686
 
e94686
-    type = metric_type(group->pmid);
e94686
+    type = dp->type;
e94686
     if (type < 0 || type > PM_TYPE_STRING) {
e94686
 	fprintf(stderr, "%s: %s uses regular expression on non-scalar metric\n",
e94686
 		pmGetProgname(), group->tag);
e94686
@@ -849,11 +865,14 @@ evaluate_string_regexp(group_t *group, regex_cmp_t compare)
e94686
 static int
e94686
 evaluate_values(group_t *group, numeric_cmp_t ncmp, string_cmp_t scmp)
e94686
 {
e94686
-    int			type = metric_type(group->pmid);
e94686
+    pmDesc		*dp;
e94686
+
e94686
+    if ((dp = metric_desc(group->pmid)) == NULL)
e94686
+	return 0;
e94686
 
e94686
-    if (type == PM_TYPE_STRING)
e94686
+    if (dp->type == PM_TYPE_STRING)
e94686
 	return evaluate_string_values(group, scmp);
e94686
-    return evaluate_number_values(group, type, ncmp);
e94686
+    return evaluate_number_values(group, dp->type, ncmp);
e94686
 }
e94686
 
e94686
 int
e94686
diff --git a/src/pmlogconf/util.c b/src/pmlogconf/util.c
e94686
index d44c2e529a..293eb2eca3 100644
e94686
--- a/src/pmlogconf/util.c
e94686
+++ b/src/pmlogconf/util.c
e94686
@@ -1,5 +1,5 @@
e94686
 /*
e94686
- * Copyright (c) 2020 Red Hat.  All Rights Reserved.
e94686
+ * Copyright (c) 2020-2021 Red Hat.  All Rights Reserved.
e94686
  * 
e94686
  * This program is free software; you can redistribute it and/or modify it
e94686
  * under the terms of the GNU General Public License as published by the
e94686
@@ -14,7 +14,7 @@
e94686
 #include "util.h"
e94686
 
e94686
 static __pmHashCtl	valuesctl;	/* pointers to values in pmResult */
e94686
-static __pmHashCtl	typesctl;	/* metric types from pmLookupDesc */
e94686
+static __pmHashCtl	descsctl;	/* metric descs from pmLookupDesc */
e94686
 
e94686
 int
e94686
 values_hash(pmResult *result)
e94686
@@ -47,27 +47,33 @@ metric_values(pmID pmid)
e94686
 }
e94686
 
e94686
 int
e94686
-metric_type(pmID pmid)
e94686
+descs_hash(int numpmid, pmDesc *descs)
e94686
 {
e94686
-    __pmHashNode	*node;
e94686
-    pmDesc		desc;
e94686
-    int			sts, *data;
e94686
+    unsigned int	i;
e94686
+    pmDesc		*dp;
e94686
+    int			sts;
e94686
 
e94686
-    if (pmid == PM_IN_NULL)
e94686
-	return PM_TYPE_UNKNOWN;
e94686
-    if ((node = __pmHashSearch(pmid, &typesctl)) == NULL) {
e94686
-	if ((sts = pmLookupDesc(pmid, &desc)) < 0)
e94686
-	    return sts;
e94686
-	if ((data = malloc(sizeof(int))) == NULL)
e94686
-	    return sts;
e94686
-	*data = desc.type;
e94686
-	if ((sts = __pmHashAdd(pmid, data, &typesctl)) < 0) {
e94686
-	    free(data);
e94686
+    if ((sts = __pmHashPreAlloc(numpmid, &descsctl)) < 0)
e94686
+	return sts;
e94686
+
e94686
+    for (i = 0; i < numpmid; i++) {
e94686
+	dp = &descs[i];
e94686
+	if ((sts = __pmHashAdd(dp->pmid, dp, &descsctl)) < 0)
e94686
 	    return sts;
e94686
-	}
e94686
-	return *data;
e94686
     }
e94686
-    return *(int *)node->data;
e94686
+    return numpmid;
e94686
+}
e94686
+
e94686
+pmDesc *
e94686
+metric_desc(pmID pmid)
e94686
+{
e94686
+    __pmHashNode	*node;
e94686
+
e94686
+    if (pmid == PM_IN_NULL)
e94686
+	return NULL;
e94686
+    if ((node = __pmHashSearch(pmid, &descsctl)) == NULL)
e94686
+	return NULL;
e94686
+    return (pmDesc *)node->data;
e94686
 }
e94686
 
e94686
 int
e94686
diff --git a/src/pmlogconf/util.h b/src/pmlogconf/util.h
e94686
index 17d856a0d7..a11350d899 100644
e94686
--- a/src/pmlogconf/util.h
e94686
+++ b/src/pmlogconf/util.h
e94686
@@ -34,7 +34,9 @@ extern void fmt(const char *, char *, size_t, int, int, fmt_t, void *);
e94686
 
e94686
 extern int values_hash(pmResult *);
e94686
 extern pmValueSet *metric_values(pmID);
e94686
-extern int metric_type(pmID);
e94686
+
e94686
+extern int descs_hash(int, pmDesc *);
e94686
+extern pmDesc *metric_desc(pmID);
e94686
 
e94686
 typedef int (*numeric_cmp_t)(double, double);
e94686
 extern int number_equal(double, double);