Blame SOURCES/redhat-bugzilla-2030121.patch

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