Blame SOURCES/redhat-bugzilla-2030121.patch

5469b2
commit df7b7bf64eb354114e6c519e3e03ffc446afa8ba
5469b2
Author: Nathan Scott <nathans@redhat.com>
5469b2
Date:   Fri Nov 26 09:17:23 2021 +1100
5469b2
5469b2
    libpcp_pmda: add indom cache fast-paths for inst lookup beyond max
5469b2
    
5469b2
    We encountered a situation where indom cache loading consumed vast
5469b2
    CPU resources for an indom of size ~150k instances.  Profiling was
5469b2
    used to identify the insert loop that ensures the inst linked list
5469b2
    within the cache hash tables is sorted - this loop is O(N*2) as we
5469b2
    potentially walk this list from the start on every insert during a
5469b2
    cache load.  Because cache loading happens from a sorted file, the
5469b2
    worst-case scenario happened every time - each new instance insert
5469b2
    occurs beyond the current maximum.  Fortunately we maintain a last
5469b2
    entry pointer, so the new fast path uses that first and falls back
5469b2
    to the original behaviour for an out-of-order insertion.
5469b2
    
5469b2
    A second opportunity for the same optimization was identified when
5469b2
    auditing the rest of cache.c - in the find_inst() routine for inst
5469b2
    identifier lookups beyond the current maximum observed instance.
5469b2
    
5469b2
    Resolves Red Hat BZ #2024648
5469b2
5469b2
diff --git a/src/libpcp_pmda/src/cache.c b/src/libpcp_pmda/src/cache.c
5469b2
index 0e66506d74..196ffc1da9 100644
5469b2
--- a/src/libpcp_pmda/src/cache.c
5469b2
+++ b/src/libpcp_pmda/src/cache.c
5469b2
@@ -328,6 +328,9 @@ find_inst(hdr_t *h, int inst)
5469b2
 {
5469b2
     entry_t	*e;
5469b2
 
5469b2
+    if ((e = h->last) != NULL && e->inst < inst)
5469b2
+	return NULL;
5469b2
+
5469b2
     for (e = h->first; e != NULL; e = e->next) {
5469b2
 	if (e->inst == inst && e->state != PMDA_CACHE_EMPTY)
5469b2
 	    break;
5469b2
@@ -621,7 +624,11 @@ insert_cache(hdr_t *h, const char *name, int inst, int *sts)
5469b2
 	    *sts = PM_ERR_INST;
5469b2
 	    return e;
5469b2
 	}
5469b2
-	for (e = h->first; e != NULL; e = e->next) {
5469b2
+	/* if this entry is beyond the (sorted) list end, avoid linear scan */
5469b2
+	if ((e = h->last) == NULL || e->inst > inst)
5469b2
+	    e = h->first;
5469b2
+	/* linear search over linked list, starting at either first or last */
5469b2
+	for (; e != NULL; e = e->next) {
5469b2
 	    if (e->inst < inst)
5469b2
 		last_e = e;
5469b2
 	    else if (e->inst > inst)