Blame SOURCES/0062-Ticket-49330-Improve-ndn-cache-performance-1.3.6.patch

61f723
From 2975f68e139169ee2d2259cfbbb2a15b54dc3724 Mon Sep 17 00:00:00 2001
61f723
From: William Brown <firstyear@redhat.com>
61f723
Date: Wed, 26 Jul 2017 11:01:49 +1000
61f723
Subject: [PATCH] Ticket 49330 - Improve ndn cache performance 1.3.6
61f723
61f723
Backport from 1.3.7 master.
61f723
61f723
Bug Description:  Normalised DN's are a costly process to update
61f723
and maintain. As a result, a normalised DN cache was created. Yet
61f723
it was never able to perform well. In some datasets with large sets
61f723
of dn attr types, the NDN cache actively hurt performance.
61f723
61f723
The issue stemmed from 3 major issues in the design of the NDN
61f723
cache.
61f723
61f723
First, it is a global cache which means it exists behind
61f723
a rwlock. This causes delay as threads wait behind the lock
61f723
to access or update the cache (especially on a miss).
61f723
61f723
Second, the cache was limited to 4073 buckets. Despite the fact
61f723
that a prime number on a hash causes a skew in distribution,
61f723
this was in an NSPR hash - which does not grow dynamically,
61f723
rather devolving a bucket to a linked list. AS a result, once you
61f723
passed ~3000 your lookup performance would degrade rapidly to O(1)
61f723
61f723
Finally, the cache's lru policy did not evict least used - it
61f723
evicted the 10,000 least used. So if you tuned your cache
61f723
to match the NSPR map, every inclusion that would trigger a
61f723
delete of old values would effectively empty your cache. ON bigger
61f723
set sizes, this has to walk the map (at O(1)) to clean 10,000
61f723
elements.
61f723
61f723
Premature optimisation strikes again ....
61f723
61f723
Fix Description:  Throw it out. Rewrite. We now use a hash
61f723
algo that has proper distribution across a set. The hash
61f723
sizes slots to a power of two. Finally, each thread has
61f723
a private cache rather than shared which completely eliminates
61f723
a lock contention and even NUMA performance issues.
61f723
61f723
Interestingly this fix should have improvements for DB
61f723
imports, memberof and refint performance and more.
61f723
61f723
Some testing has shown in simple search workloads a 10%
61f723
improvement in throughput, and on complex searches a 47x
61f723
improvement.
61f723
61f723
https://pagure.io/389-ds-base/issue/49330
61f723
61f723
Author: wibrown
61f723
61f723
Review by: lkrispen, tbordaz
61f723
---
61f723
 ldap/servers/slapd/back-ldbm/monitor.c |  11 +-
61f723
 ldap/servers/slapd/dn.c                | 809 +++++++++++++++++++++------------
61f723
 ldap/servers/slapd/slapi-private.h     |   2 +-
61f723
 3 files changed, 527 insertions(+), 295 deletions(-)
61f723
61f723
diff --git a/ldap/servers/slapd/back-ldbm/monitor.c b/ldap/servers/slapd/back-ldbm/monitor.c
61f723
index c58b069..aa7d709 100644
61f723
--- a/ldap/servers/slapd/back-ldbm/monitor.c
61f723
+++ b/ldap/servers/slapd/back-ldbm/monitor.c
61f723
@@ -43,6 +43,9 @@ int ldbm_back_monitor_instance_search(Slapi_PBlock *pb, Slapi_Entry *e,
61f723
     PRUint64 hits, tries;
61f723
     long nentries, maxentries, count;
61f723
     size_t size, maxsize;
61f723
+    size_t thread_size;
61f723
+    size_t evicts;
61f723
+    size_t slots;
61f723
 /* NPCTE fix for bugid 544365, esc 0. <P.R> <04-Jul-2001> */
61f723
     struct stat astat;
61f723
 /* end of NPCTE fix for bugid 544365 */
61f723
@@ -118,7 +121,7 @@ int ldbm_back_monitor_instance_search(Slapi_PBlock *pb, Slapi_Entry *e,
61f723
     }
61f723
     /* normalized dn cache stats */
61f723
     if(ndn_cache_started()){
61f723
-        ndn_cache_get_stats(&hits, &tries, &size, &maxsize, &count);
61f723
+        ndn_cache_get_stats(&hits, &tries, &size, &maxsize, &thread_size, &evicts, &slots, &count);
61f723
         sprintf(buf, "%" PRIu64, tries);
61f723
         MSET("normalizedDnCacheTries");
61f723
         sprintf(buf, "%" PRIu64, hits);
61f723
@@ -127,6 +130,8 @@ int ldbm_back_monitor_instance_search(Slapi_PBlock *pb, Slapi_Entry *e,
61f723
         MSET("normalizedDnCacheMisses");
61f723
         sprintf(buf, "%lu", (unsigned long)(100.0*(double)hits / (double)(tries > 0 ? tries : 1)));
61f723
         MSET("normalizedDnCacheHitRatio");
61f723
+        sprintf(buf, "%"PRIu64, evicts);
61f723
+        MSET("NormalizedDnCacheEvictions");
61f723
         sprintf(buf, "%lu", (long unsigned int)size);
61f723
         MSET("currentNormalizedDnCacheSize");
61f723
         if(maxsize == 0){
61f723
@@ -135,6 +140,10 @@ int ldbm_back_monitor_instance_search(Slapi_PBlock *pb, Slapi_Entry *e,
61f723
         	sprintf(buf, "%lu", (long unsigned int)maxsize);
61f723
         }
61f723
         MSET("maxNormalizedDnCacheSize");
61f723
+        sprintf(buf, "%"PRIu64, thread_size);
61f723
+        MSET("NormalizedDnCacheThreadSize");
61f723
+        sprintf(buf, "%"PRIu64, slots);
61f723
+        MSET("NormalizedDnCacheThreadSlots");
61f723
         sprintf(buf, "%ld", count);
61f723
         MSET("currentNormalizedDnCacheCount");
61f723
     }
61f723
diff --git a/ldap/servers/slapd/dn.c b/ldap/servers/slapd/dn.c
61f723
index fa3909f..9cb3e7b 100644
61f723
--- a/ldap/servers/slapd/dn.c
61f723
+++ b/ldap/servers/slapd/dn.c
61f723
@@ -22,6 +22,24 @@
61f723
 #include "slap.h"
61f723
 #include <plhash.h>
61f723
 
61f723
+#include <inttypes.h>
61f723
+#include <stddef.h> /* for size_t */
61f723
+
61f723
+#if defined(HAVE_SYS_ENDIAN_H)
61f723
+#include <sys/endian.h>
61f723
+#elif defined(HAVE_ENDIAN_H)
61f723
+#include <endian.h>
61f723
+#else
61f723
+#error platform header for endian detection not found.
61f723
+#endif
61f723
+
61f723
+/* See: http://sourceforge.net/p/predef/wiki/Endianness/ */
61f723
+#if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN
61f723
+#define _le64toh(x) ((uint64_t)(x))
61f723
+#else
61f723
+#define _le64toh(x) le64toh(x)
61f723
+#endif
61f723
+
61f723
 #undef SDN_DEBUG
61f723
 
61f723
 static void add_rdn_av( char *avstart, char *avend, int *rdn_av_countp,
61f723
@@ -33,52 +51,89 @@ static void rdn_av_swap( struct berval *av1, struct berval *av2, int escape );
61f723
 static int does_cn_uses_dn_syntax_in_dns(char *type, char *dn);
61f723
 
61f723
 /* normalized dn cache related definitions*/
61f723
-struct
61f723
-ndn_cache_lru
61f723
-{
61f723
-    struct ndn_cache_lru *prev;
61f723
-    struct ndn_cache_lru *next;
61f723
-    char *key;
61f723
-};
61f723
-
61f723
-struct
61f723
-ndn_cache_ctx
61f723
-{
61f723
-    struct ndn_cache_lru *head;
61f723
-    struct ndn_cache_lru *tail;
61f723
+struct ndn_cache_stats {
61f723
     Slapi_Counter *cache_hits;
61f723
     Slapi_Counter *cache_tries;
61f723
-    Slapi_Counter *cache_misses;
61f723
-    size_t cache_size;
61f723
-    size_t cache_max_size;
61f723
-    long cache_count;
61f723
+    Slapi_Counter *cache_count;
61f723
+    Slapi_Counter *cache_size;
61f723
+    Slapi_Counter *cache_evicts;
61f723
+    size_t max_size;
61f723
+    size_t thread_max_size;
61f723
+    size_t slots;
61f723
 };
61f723
 
61f723
-struct
61f723
-ndn_hash_val
61f723
-{
61f723
+struct ndn_cache_value {
61f723
+    size_t size;
61f723
+    size_t slot;
61f723
+    char *dn;
61f723
     char *ndn;
61f723
-    size_t len;
61f723
-    int size;
61f723
-    struct ndn_cache_lru *lru_node; /* used to speed up lru shuffling */
61f723
+    struct ndn_cache_value *next;
61f723
+    struct ndn_cache_value *prev;
61f723
+    struct ndn_cache_value *child;
61f723
+};
61f723
+
61f723
+/*
61f723
+ * This uses a similar alloc trick to IDList to keep
61f723
+ * The amount of derefs small.
61f723
+ */
61f723
+struct ndn_cache {
61f723
+    /*
61f723
+     * We keep per thread stats and flush them occasionally
61f723
+     */
61f723
+    size_t max_size;
61f723
+    /* Need to track this because we need to provide diffs to counter */
61f723
+    size_t last_count;
61f723
+    size_t count;
61f723
+    /* Number of ops */
61f723
+    size_t tries;
61f723
+    /* hit vs miss. in theroy miss == tries - hits.*/
61f723
+    size_t hits;
61f723
+    /* How many values we kicked out */
61f723
+    size_t evicts;
61f723
+    /* Need to track this because we need to provide diffs to counter */
61f723
+    size_t last_size;
61f723
+    size_t size;
61f723
+
61f723
+    size_t slots;
61f723
+    /*
61f723
+     * This is used by siphash to prevent hash bugket attacks
61f723
+     */
61f723
+    char key[16];
61f723
+
61f723
+    struct ndn_cache_value *head;
61f723
+    struct ndn_cache_value *tail;
61f723
+    struct ndn_cache_value *table[1];
61f723
 };
61f723
 
61f723
-#define NDN_FLUSH_COUNT 10000 /* number of DN's to remove when cache fills up */
61f723
-#define NDN_MIN_COUNT 1000 /* the minimum number of DN's to keep in the cache */
61f723
-#define NDN_CACHE_BUCKETS 2053 /* prime number */
61f723
+/*
61f723
+ * This means we need 1 MB minimum per thread
61f723
+ * 
61f723
+ */
61f723
+#define NDN_CACHE_MINIMUM_CAPACITY 1048576
61f723
+/*
61f723
+ * This helps us define the number of hashtable slots
61f723
+ * to create. We assume an average DN is 64 chars long
61f723
+ * This way we end up we a ht entry of:
61f723
+ * 8 bytes: from the table pointing to us.
61f723
+ * 8 bytes: next ptr
61f723
+ * 8 bytes: prev ptr
61f723
+ * 8 bytes + 64: dn
61f723
+ * 8 bytes + 64: ndn itself.
61f723
+ * This gives us 168 bytes. In theory this means
61f723
+ * 6241 entries, but we have to clamp this to a power of
61f723
+ * two, so we have 8192 slots. In reality, dns may be
61f723
+ * shorter *and* the dn may be the same as the ndn
61f723
+ * so we *may* store more ndns that this. Again, a good reason
61f723
+ * to round the ht size up!
61f723
+ */
61f723
+#define NDN_ENTRY_AVG_SIZE 168
61f723
+/*
61f723
+ * After how many operations do we sync our per-thread stats.
61f723
+ */
61f723
+#define NDN_STAT_COMMIT_FREQUENCY 256
61f723
 
61f723
-static PLHashNumber ndn_hash_string(const void *key);
61f723
 static int ndn_cache_lookup(char *dn, size_t dn_len, char **result, char **udn, int *rc);
61f723
-static void ndn_cache_update_lru(struct ndn_cache_lru **node);
61f723
 static void ndn_cache_add(char *dn, size_t dn_len, char *ndn, size_t ndn_len);
61f723
-static void ndn_cache_delete(char *dn);
61f723
-static void ndn_cache_flush(void);
61f723
-static void ndn_cache_free(void);
61f723
-static int ndn_started = 0;
61f723
-static PRLock *lru_lock = NULL;
61f723
-static Slapi_RWLock *ndn_cache_lock = NULL;
61f723
-static struct ndn_cache_ctx *ndn_cache = NULL;
61f723
-static PLHashTable *ndn_cache_hashtable = NULL;
61f723
 
61f723
 #define ISBLANK(c)	((c) == ' ')
61f723
 #define ISBLANKSTR(s)	(((*(s)) == '2') && (*((s)+1) == '0'))
61f723
@@ -2768,166 +2823,408 @@ slapi_sdn_get_size(const Slapi_DN *sdn)
61f723
  *
61f723
  */
61f723
 
61f723
+/* <MIT License>
61f723
+ Copyright (c) 2013  Marek Majkowski <marek@popcount.org>
61f723
+
61f723
+ Permission is hereby granted, free of charge, to any person obtaining a copy
61f723
+ of this software and associated documentation files (the "Software"), to deal
61f723
+ in the Software without restriction, including without limitation the rights
61f723
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
61f723
+ copies of the Software, and to permit persons to whom the Software is
61f723
+ furnished to do so, subject to the following conditions:
61f723
+
61f723
+ The above copyright notice and this permission notice shall be included in
61f723
+ all copies or substantial portions of the Software.
61f723
+
61f723
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
61f723
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
61f723
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
61f723
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
61f723
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
61f723
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
61f723
+ THE SOFTWARE.
61f723
+ </MIT License>
61f723
+
61f723
+ Original location:
61f723
+    https://github.com/majek/csiphash/
61f723
+
61f723
+ Solution inspired by code from:
61f723
+    Samuel Neves (supercop/crypto_auth/siphash24/little)
61f723
+    djb (supercop/crypto_auth/siphash24/little2)
61f723
+    Jean-Philippe Aumasson (https://131002.net/siphash/siphash24.c)
61f723
+*/
61f723
+
61f723
+#define ROTATE(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
61f723
+
61f723
+#define HALF_ROUND(a, b, c, d, s, t) \
61f723
+    a += b;                          \
61f723
+    c += d;                          \
61f723
+    b = ROTATE(b, s) ^ a;            \
61f723
+    d = ROTATE(d, t) ^ c;            \
61f723
+    a = ROTATE(a, 32);
61f723
+
61f723
+#define ROUND(v0, v1, v2, v3)           \
61f723
+    HALF_ROUND(v0, v1, v2, v3, 13, 16); \
61f723
+    HALF_ROUND(v2, v1, v0, v3, 17, 21)
61f723
+
61f723
+#define cROUND(v0, v1, v2, v3) \
61f723
+    ROUND(v0, v1, v2, v3)
61f723
+
61f723
+#define dROUND(v0, v1, v2, v3) \
61f723
+    ROUND(v0, v1, v2, v3);     \
61f723
+    ROUND(v0, v1, v2, v3);     \
61f723
+    ROUND(v0, v1, v2, v3)
61f723
+
61f723
+
61f723
+static uint64_t
61f723
+sds_siphash13(const void *src, size_t src_sz, const char key[16])
61f723
+{
61f723
+    const uint64_t *_key = (uint64_t *)key;
61f723
+    uint64_t k0 = _le64toh(_key[0]);
61f723
+    uint64_t k1 = _le64toh(_key[1]);
61f723
+    uint64_t b = (uint64_t)src_sz << 56;
61f723
+    const uint64_t *in = (uint64_t *)src;
61f723
+
61f723
+    uint64_t v0 = k0 ^ 0x736f6d6570736575ULL;
61f723
+    uint64_t v1 = k1 ^ 0x646f72616e646f6dULL;
61f723
+    uint64_t v2 = k0 ^ 0x6c7967656e657261ULL;
61f723
+    uint64_t v3 = k1 ^ 0x7465646279746573ULL;
61f723
+
61f723
+    while (src_sz >= 8) {
61f723
+        uint64_t mi = _le64toh(*in);
61f723
+        in += 1;
61f723
+        src_sz -= 8;
61f723
+        v3 ^= mi;
61f723
+        // cround
61f723
+        cROUND(v0, v1, v2, v3);
61f723
+        v0 ^= mi;
61f723
+    }
61f723
+
61f723
+    uint64_t t = 0;
61f723
+    uint8_t *pt = (uint8_t *)&t;
61f723
+    uint8_t *m = (uint8_t *)in;
61f723
+
61f723
+    switch (src_sz) {
61f723
+    case 7:
61f723
+        pt[6] = m[6]; /* FALLTHRU */
61f723
+    case 6:
61f723
+        pt[5] = m[5]; /* FALLTHRU */
61f723
+    case 5:
61f723
+        pt[4] = m[4]; /* FALLTHRU */
61f723
+    case 4:
61f723
+        *((uint32_t *)&pt[0]) = *((uint32_t *)&m[0]);
61f723
+        break;
61f723
+    case 3:
61f723
+        pt[2] = m[2]; /* FALLTHRU */
61f723
+    case 2:
61f723
+        pt[1] = m[1]; /* FALLTHRU */
61f723
+    case 1:
61f723
+        pt[0] = m[0]; /* FALLTHRU */
61f723
+    }
61f723
+    b |= _le64toh(t);
61f723
+
61f723
+    v3 ^= b;
61f723
+    // cround
61f723
+    cROUND(v0, v1, v2, v3);
61f723
+    v0 ^= b;
61f723
+    v2 ^= 0xff;
61f723
+    // dround
61f723
+    dROUND(v0, v1, v2, v3);
61f723
+    return (v0 ^ v1) ^ (v2 ^ v3);
61f723
+}
61f723
+
61f723
+static pthread_key_t ndn_cache_key;
61f723
+static pthread_once_t ndn_cache_key_once = PTHREAD_ONCE_INIT;
61f723
+static struct ndn_cache_stats t_cache_stats = {0};
61f723
 /*
61f723
- *  Hashing function using Bernstein's method
61f723
+ * WARNING: For some reason we try to use the NDN cache *before*
61f723
+ * we have a chance to configure it. As a result, we need to rely
61f723
+ * on a trick in the way we start, that we start in one thread
61f723
+ * so we can manipulate ints as though they were atomics, then
61f723
+ * we start in *one* thread, so it's set, then when threads
61f723
+ * fork the get barriers, so we can go from there. However we *CANNOT*
61f723
+ * change this at runtime without expensive atomics per op, so lets
61f723
+ * not bother until we improve libglobs to be COW.
61f723
  */
61f723
-static PLHashNumber
61f723
-ndn_hash_string(const void *key)
61f723
-{
61f723
-    PLHashNumber hash = 5381;
61f723
-    unsigned char *x = (unsigned char *)key;
61f723
-    int c;
61f723
+static int32_t ndn_enabled = 0;
61f723
+
61f723
+static struct ndn_cache *
61f723
+ndn_thread_cache_create(size_t thread_max_size, size_t slots) {
61f723
+    size_t t_cache_size = sizeof(struct ndn_cache) + (slots * sizeof(struct ndn_cache_value *));
61f723
+    struct ndn_cache *t_cache = (struct ndn_cache *)slapi_ch_calloc(1, t_cache_size);
61f723
+
61f723
+    t_cache->max_size = thread_max_size;
61f723
+    t_cache->slots = slots;
61f723
 
61f723
-    while ((c = *x++)){
61f723
-        hash = ((hash << 5) + hash) ^ c;
61f723
+    return t_cache;
61f723
+}
61f723
+
61f723
+static void
61f723
+ndn_thread_cache_commit_status(struct ndn_cache *t_cache) {
61f723
+    /*
61f723
+     * Every so often we commit these atomically. We do this infrequently
61f723
+     * to avoid the costly atomics.
61f723
+     */
61f723
+    if (t_cache->tries % NDN_STAT_COMMIT_FREQUENCY == 0) {
61f723
+        /* We can just add tries and hits. */
61f723
+        slapi_counter_add(t_cache_stats.cache_evicts, t_cache->evicts);
61f723
+        slapi_counter_add(t_cache_stats.cache_tries, t_cache->tries);
61f723
+        slapi_counter_add(t_cache_stats.cache_hits, t_cache->hits);
61f723
+        t_cache->hits = 0;
61f723
+        t_cache->tries = 0;
61f723
+        t_cache->evicts = 0;
61f723
+        /* Count and size need diff */
61f723
+        int64_t diff = (t_cache->size - t_cache->last_size);
61f723
+        if (diff > 0) {
61f723
+            // We have more ....
61f723
+            slapi_counter_add(t_cache_stats.cache_size, (uint64_t)diff);
61f723
+        } else if (diff < 0) {
61f723
+            slapi_counter_subtract(t_cache_stats.cache_size, (uint64_t)llabs(diff));
61f723
+        }
61f723
+        t_cache->last_size = t_cache->size;
61f723
+
61f723
+        diff = (t_cache->count - t_cache->last_count);
61f723
+        if (diff > 0) {
61f723
+            // We have more ....
61f723
+            slapi_counter_add(t_cache_stats.cache_count, (uint64_t)diff);
61f723
+        } else if (diff < 0) {
61f723
+            slapi_counter_subtract(t_cache_stats.cache_count, (uint64_t)llabs(diff));
61f723
+        }
61f723
+        t_cache->last_count = t_cache->count;
61f723
+
61f723
+    }
61f723
+}
61f723
+
61f723
+static void
61f723
+ndn_thread_cache_value_destroy(struct ndn_cache *t_cache, struct ndn_cache_value *v) {
61f723
+    /* Update stats */
61f723
+    t_cache->size = t_cache->size - v->size;
61f723
+    t_cache->count--;
61f723
+    t_cache->evicts++;
61f723
+
61f723
+    if (v == t_cache->head) {
61f723
+        t_cache->head = v->prev;
61f723
+    }
61f723
+    if (v == t_cache->tail) {
61f723
+        t_cache->tail = v->next;
61f723
+    }
61f723
+
61f723
+    /* Cut the node out. */
61f723
+    if (v->next != NULL) {
61f723
+        v->next->prev = v->prev;
61f723
+    }
61f723
+    if (v->prev != NULL) {
61f723
+        v->prev->next = v->next;
61f723
+    }
61f723
+    /* Set the pointer in the table to NULL */
61f723
+    /* Now see if we were in a list */
61f723
+    struct ndn_cache_value *slot_node = t_cache->table[v->slot];
61f723
+    if (slot_node == v) {
61f723
+        t_cache->table[v->slot] = v->child;
61f723
+    } else {
61f723
+        struct ndn_cache_value *former_slot_node = NULL;
61f723
+        do {
61f723
+            former_slot_node = slot_node;
61f723
+            slot_node = slot_node->child;
61f723
+        } while(slot_node != v);
61f723
+        /* Okay, now slot_node is us, and former is our parent */
61f723
+        former_slot_node->child = v->child;
61f723
+    }
61f723
+
61f723
+    slapi_ch_free((void **)&(v->dn));
61f723
+    slapi_ch_free((void **)&(v->ndn));
61f723
+    slapi_ch_free((void **)&v);
61f723
+}
61f723
+
61f723
+static void
61f723
+ndn_thread_cache_destroy(void *v_cache) {
61f723
+    struct ndn_cache *t_cache = (struct ndn_cache *)v_cache;
61f723
+    /*
61f723
+     * FREE ALL THE NODES!!!
61f723
+     */
61f723
+    struct ndn_cache_value *node = t_cache->tail;
61f723
+    struct ndn_cache_value *next_node = NULL;
61f723
+    while (node) {
61f723
+        next_node = node->next;
61f723
+        ndn_thread_cache_value_destroy(t_cache, node);
61f723
+        node = next_node;
61f723
+    }
61f723
+    slapi_ch_free((void **)&t_cache);
61f723
+}
61f723
+
61f723
+static void
61f723
+ndn_cache_key_init() {
61f723
+    if (pthread_key_create(&ndn_cache_key, ndn_thread_cache_destroy) != 0) {
61f723
+        /* Log a scary warning? */
61f723
+        slapi_log_err(SLAPI_LOG_ERR, "ndn_cache_init", "Failed to create pthread key, aborting.\n");
61f723
     }
61f723
-    return hash;
61f723
 }
61f723
 
61f723
 void
61f723
 ndn_cache_init()
61f723
 {
61f723
-    if(!config_get_ndn_cache_enabled() || ndn_started){
61f723
+    ndn_enabled = config_get_ndn_cache_enabled();
61f723
+    if (ndn_enabled == 0) {
61f723
+        /*
61f723
+         * Don't configure the keys or anything, need a restart
61f723
+         * to enable. We'll just never use ndn cache in this
61f723
+         * run.
61f723
+         */
61f723
         return;
61f723
     }
61f723
-    ndn_cache_hashtable = PL_NewHashTable( NDN_CACHE_BUCKETS, ndn_hash_string, PL_CompareStrings, PL_CompareValues, 0, 0);
61f723
-    ndn_cache = (struct ndn_cache_ctx *)slapi_ch_malloc(sizeof(struct ndn_cache_ctx));
61f723
-    ndn_cache->cache_max_size = config_get_ndn_cache_size();
61f723
-    ndn_cache->cache_hits = slapi_counter_new();
61f723
-    ndn_cache->cache_tries = slapi_counter_new();
61f723
-    ndn_cache->cache_misses = slapi_counter_new();
61f723
-    ndn_cache->cache_count = 0;
61f723
-    ndn_cache->cache_size = sizeof(struct ndn_cache_ctx) + sizeof(PLHashTable) + sizeof(PLHashTable);
61f723
-    ndn_cache->head = NULL;
61f723
-    ndn_cache->tail = NULL;
61f723
-    ndn_started = 1;
61f723
-    if ( NULL == ( lru_lock = PR_NewLock()) ||  NULL == ( ndn_cache_lock = slapi_new_rwlock())) {
61f723
-        ndn_cache_destroy();
61f723
-        slapi_log_err(SLAPI_LOG_ERR, "ndn_cache_init", "Failed to create locks.  Disabling cache.\n" );
61f723
+
61f723
+    /* Create the pthread key */
61f723
+    (void)pthread_once(&ndn_cache_key_once, ndn_cache_key_init);
61f723
+
61f723
+    /* Create the global stats. */
61f723
+    t_cache_stats.max_size = config_get_ndn_cache_size();
61f723
+    t_cache_stats.cache_evicts = slapi_counter_new();
61f723
+    t_cache_stats.cache_tries = slapi_counter_new();
61f723
+    t_cache_stats.cache_hits = slapi_counter_new();
61f723
+    t_cache_stats.cache_count = slapi_counter_new();
61f723
+    t_cache_stats.cache_size = slapi_counter_new();
61f723
+    /* Get thread numbers and calc the per thread size */
61f723
+    int32_t maxthreads = (int32_t)config_get_threadnumber();
61f723
+    size_t tentative_size = t_cache_stats.max_size / maxthreads;
61f723
+    if (tentative_size < NDN_CACHE_MINIMUM_CAPACITY) {
61f723
+        tentative_size = NDN_CACHE_MINIMUM_CAPACITY;
61f723
+        t_cache_stats.max_size = NDN_CACHE_MINIMUM_CAPACITY * maxthreads;
61f723
+    }
61f723
+    t_cache_stats.thread_max_size = tentative_size;
61f723
+
61f723
+    /*
61f723
+     * Slots *must* be a power of two, even if the number of entries
61f723
+     * we store will be *less* than this.
61f723
+     */
61f723
+    size_t possible_elements = tentative_size / NDN_ENTRY_AVG_SIZE;
61f723
+    /*
61f723
+     * So this is like 1048576 / 168, so we get 6241. Now we need to
61f723
+     * shift this to get the number of bits.
61f723
+     */
61f723
+    size_t shifts = 0;
61f723
+    while (possible_elements > 0) {
61f723
+        shifts++;
61f723
+        possible_elements = possible_elements >> 1;
61f723
     }
61f723
+    /*
61f723
+     * So now we can use this to make the slot count.
61f723
+     */
61f723
+    t_cache_stats.slots = 1 << shifts;
61f723
+    /* Done? */
61f723
+    return;
61f723
 }
61f723
 
61f723
 void
61f723
 ndn_cache_destroy()
61f723
 {
61f723
-    if(!ndn_started){
61f723
+    if (ndn_enabled == 0) {
61f723
         return;
61f723
     }
61f723
-    if(lru_lock){
61f723
-        PR_DestroyLock(lru_lock);
61f723
-        lru_lock = NULL;
61f723
-    }
61f723
-    if(ndn_cache_lock){
61f723
-        slapi_destroy_rwlock(ndn_cache_lock);
61f723
-        ndn_cache_lock = NULL;
61f723
-    }
61f723
-    if(ndn_cache_hashtable){
61f723
-        ndn_cache_free();
61f723
-        PL_HashTableDestroy(ndn_cache_hashtable);
61f723
-        ndn_cache_hashtable = NULL;
61f723
-    }
61f723
-    config_set_ndn_cache_enabled(CONFIG_NDN_CACHE, "off", NULL, 1 );
61f723
-    slapi_counter_destroy(&ndn_cache->cache_hits);
61f723
-    slapi_counter_destroy(&ndn_cache->cache_tries);
61f723
-    slapi_counter_destroy(&ndn_cache->cache_misses);
61f723
-    slapi_ch_free((void **)&ndn_cache);
61f723
-
61f723
-    ndn_started = 0;
61f723
+    slapi_counter_destroy(&(t_cache_stats.cache_tries));
61f723
+    slapi_counter_destroy(&(t_cache_stats.cache_hits));
61f723
+    slapi_counter_destroy(&(t_cache_stats.cache_count));
61f723
+    slapi_counter_destroy(&(t_cache_stats.cache_size));
61f723
+    slapi_counter_destroy(&(t_cache_stats.cache_evicts));
61f723
 }
61f723
 
61f723
 int
61f723
 ndn_cache_started()
61f723
 {
61f723
-    return ndn_started;
61f723
+    return ndn_enabled;
61f723
 }
61f723
 
61f723
 /*
61f723
  *  Look up this dn in the ndn cache
61f723
  */
61f723
 static int
61f723
-ndn_cache_lookup(char *dn, size_t dn_len, char **result, char **udn, int *rc)
61f723
+ndn_cache_lookup(char *dn, size_t dn_len, char **ndn, char **udn, int *rc)
61f723
 {
61f723
-    struct ndn_hash_val *ndn_ht_val = NULL;
61f723
-    char *ndn, *key;
61f723
-    int rv = 0;
61f723
-
61f723
-    if(NULL == udn){
61f723
-        return rv;
61f723
+    if (ndn_enabled == 0 || NULL == udn) {
61f723
+        return 0;
61f723
     }
61f723
     *udn = NULL;
61f723
-    if(ndn_started == 0){
61f723
-        return rv;
61f723
-    }
61f723
-    if(dn_len == 0){
61f723
-        *result = dn;
61f723
+
61f723
+    if (dn_len == 0) {
61f723
+        *ndn = dn;
61f723
         *rc = 0;
61f723
         return 1;
61f723
     }
61f723
-    slapi_counter_increment(ndn_cache->cache_tries);
61f723
-    slapi_rwlock_rdlock(ndn_cache_lock);
61f723
-    ndn_ht_val = (struct ndn_hash_val *)PL_HashTableLookupConst(ndn_cache_hashtable, dn);
61f723
-    if(ndn_ht_val){
61f723
-        ndn_cache_update_lru(&ndn_ht_val->lru_node);
61f723
-        slapi_counter_increment(ndn_cache->cache_hits);
61f723
-        if ((ndn_ht_val->len != dn_len) || 
61f723
-            /* even if the lengths match, dn may not be normalized yet.
61f723
-             * (e.g., 'cn="o=ABC",o=XYZ' vs. 'cn=o\3DABC,o=XYZ') */
61f723
-            (memcmp(dn, ndn_ht_val->ndn, dn_len))){
61f723
-            *rc = 1; /* free result */
61f723
-            ndn = slapi_ch_malloc(ndn_ht_val->len + 1);
61f723
-            memcpy(ndn, ndn_ht_val->ndn, ndn_ht_val->len);
61f723
-            ndn[ndn_ht_val->len] = '\0';
61f723
-            *result = ndn;
61f723
-        } else {
61f723
-            /* the dn was already normalized, just return the dn as the result */
61f723
-            *result = dn;
61f723
-            *rc = 0;
61f723
-        }
61f723
-        rv = 1;
61f723
-    } else {
61f723
-        /* copy/preserve the udn, so we can use it as the key when we add dn's to the hashtable */
61f723
-        key = slapi_ch_malloc(dn_len + 1);
61f723
-        memcpy(key, dn, dn_len);
61f723
-        key[dn_len] = '\0';
61f723
-        *udn = key;
61f723
+
61f723
+    struct ndn_cache *t_cache = pthread_getspecific(ndn_cache_key);
61f723
+    if (t_cache == NULL) {
61f723
+        t_cache = ndn_thread_cache_create(t_cache_stats.thread_max_size, t_cache_stats.slots);
61f723
+        pthread_setspecific(ndn_cache_key, t_cache);
61f723
+        /* If we have no cache, we can't look up ... */
61f723
+        return 0;
61f723
     }
61f723
-    slapi_rwlock_unlock(ndn_cache_lock);
61f723
 
61f723
-    return rv;
61f723
-}
61f723
+    t_cache->tries++;
61f723
 
61f723
-/*
61f723
- *  Move this lru node to the top of the list
61f723
- */
61f723
-static void
61f723
-ndn_cache_update_lru(struct ndn_cache_lru **node)
61f723
-{
61f723
-    struct ndn_cache_lru *prev, *next, *curr_node = *node;
61f723
+    /*
61f723
+     * Hash our DN ...
61f723
+     */
61f723
+    uint64_t dn_hash = sds_siphash13(dn, dn_len, t_cache->key);
61f723
+    /* Where should it be? */
61f723
+    size_t expect_slot = dn_hash % t_cache->slots;
61f723
 
61f723
-    if(curr_node == NULL){
61f723
-        return;
61f723
-    }
61f723
-    PR_Lock(lru_lock);
61f723
-    if(curr_node->prev == NULL){
61f723
-        /* already the top node */
61f723
-        PR_Unlock(lru_lock);
61f723
-        return;
61f723
-    }
61f723
-    prev = curr_node->prev;
61f723
-    next = curr_node->next;
61f723
-    if(next){
61f723
-        next->prev = prev;
61f723
-        prev->next = next;
61f723
-    } else {
61f723
-        /* this was the tail, so reset the tail */
61f723
-        ndn_cache->tail = prev;
61f723
-        prev->next = NULL;
61f723
+    /*
61f723
+     * Is it there?
61f723
+     */
61f723
+    if (t_cache->table[expect_slot] != NULL) {
61f723
+        /*
61f723
+         * Check it really matches, could be collision.
61f723
+         */
61f723
+        struct ndn_cache_value *node = t_cache->table[expect_slot];
61f723
+        while (node != NULL) {
61f723
+            if (strcmp(dn, node->dn) == 0) {
61f723
+                /*
61f723
+                 * Update LRU
61f723
+                 * Are we already the tail? If so, we can just skip.
61f723
+                 * remember, this means in a set of 1, we will always be tail
61f723
+                 */
61f723
+                if (t_cache->tail != node) {
61f723
+                    /*
61f723
+                     * Okay, we are *not* the tail. We could be anywhere between
61f723
+                     * tail -> ... -> x -> head
61f723
+                     * or even, we are the head ourself.
61f723
+                     */
61f723
+                    if (t_cache->head == node) {
61f723
+                        /* We are the head, update head to our predecessor */
61f723
+                        t_cache->head = node->prev;
61f723
+                        /* Remember, the head has no next. */
61f723
+                        t_cache->head->next = NULL;
61f723
+                    } else {
61f723
+                        /* Right, we aren't the head, so we have a next node. */
61f723
+                        node->next->prev = node->prev;
61f723
+                    }
61f723
+                    /* Because we must be in the middle somewhere, we can assume next and prev exist. */
61f723
+                    node->prev->next = node->next;
61f723
+                    /*
61f723
+                     * Tail can't be NULL if we have a value in the cache, so we can
61f723
+                     * just deref this.
61f723
+                     */
61f723
+                    node->next = t_cache->tail;
61f723
+                    t_cache->tail->prev = node;
61f723
+                    t_cache->tail = node;
61f723
+                    node->prev = NULL;
61f723
+                }
61f723
+                /* Update that we have a hit.*/
61f723
+                t_cache->hits++;
61f723
+                /* Cope the NDN to the caller. */
61f723
+                *ndn = slapi_ch_strdup(node->ndn);
61f723
+                /* Indicate to the caller to free this. */
61f723
+                *rc = 1;
61f723
+                ndn_thread_cache_commit_status(t_cache);
61f723
+                return 1;
61f723
+            }
61f723
+            node = node->child;
61f723
+        }
61f723
     }
61f723
-    curr_node->prev = NULL;
61f723
-    curr_node->next = ndn_cache->head;
61f723
-    ndn_cache->head->prev = curr_node;
61f723
-    ndn_cache->head = curr_node;
61f723
-    PR_Unlock(lru_lock);
61f723
+    /* If we miss, we need to duplicate dn to udn here. */
61f723
+    *udn = slapi_ch_strdup(dn);
61f723
+    *rc = 0;
61f723
+    ndn_thread_cache_commit_status(t_cache);
61f723
+    return 0;
61f723
 }
61f723
 
61f723
 /*
61f723
@@ -2936,176 +3233,102 @@ ndn_cache_update_lru(struct ndn_cache_lru **node)
61f723
 static void
61f723
 ndn_cache_add(char *dn, size_t dn_len, char *ndn, size_t ndn_len)
61f723
 {
61f723
-    struct ndn_hash_val *ht_entry;
61f723
-    struct ndn_cache_lru *new_node = NULL;
61f723
-    PLHashEntry *he;
61f723
-    int size;
61f723
-
61f723
-    if(ndn_started == 0 || dn_len == 0){
61f723
+    if (ndn_enabled == 0) {
61f723
         return;
61f723
     }
61f723
-    if(strlen(ndn) > ndn_len){
61f723
+    if (dn_len == 0) {
61f723
+        return;
61f723
+    }
61f723
+    if (strlen(ndn) > ndn_len) {
61f723
         /* we need to null terminate the ndn */
61f723
         *(ndn + ndn_len) = '\0';
61f723
     }
61f723
     /*
61f723
      *  Calculate the approximate memory footprint of the hash entry, key, and lru entry.
61f723
      */
61f723
-    size = (dn_len * 2) + ndn_len + sizeof(PLHashEntry) + sizeof(struct ndn_hash_val) + sizeof(struct ndn_cache_lru);
61f723
+    struct ndn_cache_value *new_value = (struct ndn_cache_value *)slapi_ch_calloc(1, sizeof(struct ndn_cache_value));
61f723
+    new_value->size = sizeof(struct ndn_cache_value) + dn_len + ndn_len;
61f723
+    /* DN is alloc for us */
61f723
+    new_value->dn = dn;
61f723
+    /* But we need to copy ndn */
61f723
+    new_value->ndn = slapi_ch_strdup(ndn);
61f723
+
61f723
     /*
61f723
-     *  Create our LRU node
61f723
+     * Get our local cache out.
61f723
      */
61f723
-    new_node = (struct ndn_cache_lru *)slapi_ch_malloc(sizeof(struct ndn_cache_lru));
61f723
-    if(new_node == NULL){
61f723
-        slapi_log_err(SLAPI_LOG_ERR, "ndn_cache_add", "Failed to allocate new lru node.\n");
61f723
-        return;
61f723
+    struct ndn_cache *t_cache = pthread_getspecific(ndn_cache_key);
61f723
+    if (t_cache == NULL) {
61f723
+        t_cache = ndn_thread_cache_create(t_cache_stats.thread_max_size, t_cache_stats.slots);
61f723
+        pthread_setspecific(ndn_cache_key, t_cache);
61f723
     }
61f723
-    new_node->prev = NULL;
61f723
-    new_node->key = dn; /* dn has already been allocated */
61f723
     /*
61f723
-     *  Its possible this dn was added to the hash by another thread.
61f723
+     * Hash the DN
61f723
      */
61f723
-    slapi_rwlock_wrlock(ndn_cache_lock);
61f723
-    ht_entry = (struct ndn_hash_val *)PL_HashTableLookupConst(ndn_cache_hashtable, dn);
61f723
-    if(ht_entry){
61f723
-        /* already exists, free the node and return */
61f723
-        slapi_rwlock_unlock(ndn_cache_lock);
61f723
-        slapi_ch_free_string(&new_node->key);
61f723
-        slapi_ch_free((void **)&new_node);
61f723
-        return;
61f723
-    }
61f723
+    uint64_t dn_hash = sds_siphash13(new_value->dn, dn_len, t_cache->key);
61f723
     /*
61f723
-     *  Create the hash entry
61f723
+     * Get the insert slot: This works because the number spaces of dn_hash is
61f723
+     * a 64bit int, and slots is a power of two. As a result, we end up with
61f723
+     * even distribution of the values.
61f723
      */
61f723
-    ht_entry = (struct ndn_hash_val *)slapi_ch_malloc(sizeof(struct ndn_hash_val));
61f723
-    if(ht_entry == NULL){
61f723
-        slapi_rwlock_unlock(ndn_cache_lock);
61f723
-        slapi_log_err(SLAPI_LOG_ERR, "ndn_cache_add", "Failed to allocate new hash entry.\n");
61f723
-        slapi_ch_free_string(&new_node->key);
61f723
-        slapi_ch_free((void **)&new_node);
61f723
-        return;
61f723
-    }
61f723
-    ht_entry->ndn = slapi_ch_malloc(ndn_len + 1);
61f723
-    memcpy(ht_entry->ndn, ndn, ndn_len);
61f723
-    ht_entry->ndn[ndn_len] = '\0';
61f723
-    ht_entry->len = ndn_len;
61f723
-    ht_entry->size = size;
61f723
-    ht_entry->lru_node = new_node;
61f723
+    size_t insert_slot = dn_hash % t_cache->slots;
61f723
+    /* Track this for free */
61f723
+    new_value->slot = insert_slot;
61f723
+
61f723
     /*
61f723
-     *  Check if our cache is full
61f723
+     * Okay, check if we have space, else we need to trim nodes from
61f723
+     * the LRU
61f723
      */
61f723
-    PR_Lock(lru_lock); /* grab the lru lock now, as ndn_cache_flush needs it */
61f723
-    if(ndn_cache->cache_max_size != 0 && ((ndn_cache->cache_size + size) > ndn_cache->cache_max_size)){
61f723
-        ndn_cache_flush();
61f723
+    while (t_cache->head && (t_cache->size + new_value->size) > t_cache->max_size) {
61f723
+        struct ndn_cache_value *trim_node = t_cache->head;
61f723
+        ndn_thread_cache_value_destroy(t_cache, trim_node);
61f723
     }
61f723
+
61f723
     /*
61f723
-     * Set the ndn cache lru nodes
61f723
+     * Add it!
61f723
      */
61f723
-    if(ndn_cache->head == NULL && ndn_cache->tail == NULL){
61f723
-        /* this is the first node */
61f723
-        ndn_cache->head = new_node;
61f723
-        ndn_cache->tail = new_node;
61f723
-        new_node->next = NULL;
61f723
+    if (t_cache->table[insert_slot] == NULL) {
61f723
+        t_cache->table[insert_slot] = new_value;
61f723
     } else {
61f723
-        new_node->next = ndn_cache->head;
61f723
-        if(ndn_cache->head)
61f723
-            ndn_cache->head->prev = new_node;
61f723
+        /*
61f723
+         * Hash collision! We need to replace the bucket then ....
61f723
+         * insert at the head of the slot to make this simpler.
61f723
+         */
61f723
+        new_value->child = t_cache->table[insert_slot];
61f723
+        t_cache->table[insert_slot] = new_value;
61f723
     }
61f723
-    ndn_cache->head = new_node;
61f723
-    PR_Unlock(lru_lock);
61f723
+
61f723
     /*
61f723
-     *  Add the new object to the hashtable, and update our stats
61f723
+     * Finally, stick this onto the tail because it's the newest.
61f723
      */
61f723
-    he = PL_HashTableAdd(ndn_cache_hashtable, new_node->key, (void *)ht_entry);
61f723
-    if(he == NULL){
61f723
-        slapi_log_err(SLAPI_LOG_ERR, "ndn_cache_add", "Failed to add new entry to hash(%s)\n",dn);
61f723
-    } else {
61f723
-        ndn_cache->cache_count++;
61f723
-        ndn_cache->cache_size += size;
61f723
+    if (t_cache->head == NULL) {
61f723
+        t_cache->head = new_value;
61f723
     }
61f723
-    slapi_rwlock_unlock(ndn_cache_lock);
61f723
-}
61f723
-
61f723
-/*
61f723
- *  cache is full, remove the least used dn's.  lru_lock/ndn_cache write lock are already taken
61f723
- */
61f723
-static void
61f723
-ndn_cache_flush(void)
61f723
-{
61f723
-    struct ndn_cache_lru *node, *next, *flush_node;
61f723
-    int i;
61f723
-
61f723
-    node = ndn_cache->tail;
61f723
-    for(i = 0; node && i < NDN_FLUSH_COUNT && ndn_cache->cache_count > NDN_MIN_COUNT; i++){
61f723
-        flush_node = node;
61f723
-        /* update the lru */
61f723
-        next = node->prev;
61f723
-        next->next = NULL;
61f723
-        ndn_cache->tail = next;
61f723
-        node = next;
61f723
-        /* now update the hash */
61f723
-        ndn_cache->cache_count--;
61f723
-        ndn_cache_delete(flush_node->key);
61f723
-        slapi_ch_free_string(&flush_node->key);
61f723
-        slapi_ch_free((void **)&flush_node);
61f723
+    if (t_cache->tail != NULL) {
61f723
+        new_value->next = t_cache->tail;
61f723
+        t_cache->tail->prev = new_value;
61f723
     }
61f723
+    t_cache->tail = new_value;
61f723
 
61f723
-    slapi_log_err(SLAPI_LOG_CACHE, "ndn_cache_flush","Flushed cache.\n");
61f723
-}
61f723
-
61f723
-static void
61f723
-ndn_cache_free(void)
61f723
-{
61f723
-    struct ndn_cache_lru *node, *next, *flush_node;
61f723
-
61f723
-    if(!ndn_cache){
61f723
-        return;
61f723
-    }
61f723
-
61f723
-    node = ndn_cache->tail;
61f723
-    while(node && ndn_cache->cache_count){
61f723
-        flush_node = node;
61f723
-        /* update the lru */
61f723
-        next = node->prev;
61f723
-        if(next){
61f723
-            next->next = NULL;
61f723
-        }
61f723
-        ndn_cache->tail = next;
61f723
-        node = next;
61f723
-        /* now update the hash */
61f723
-        ndn_cache->cache_count--;
61f723
-        ndn_cache_delete(flush_node->key);
61f723
-        slapi_ch_free_string(&flush_node->key);
61f723
-        slapi_ch_free((void **)&flush_node);
61f723
-    }
61f723
-}
61f723
-
61f723
-/* this is already "write" locked from ndn_cache_add */
61f723
-static void
61f723
-ndn_cache_delete(char *dn)
61f723
-{
61f723
-    struct ndn_hash_val *ht_entry;
61f723
+    /*
61f723
+     * And update the stats.
61f723
+     */
61f723
+    t_cache->size = t_cache->size + new_value->size;
61f723
+    t_cache->count++;
61f723
 
61f723
-    ht_entry = (struct ndn_hash_val *)PL_HashTableLookupConst(ndn_cache_hashtable, dn);
61f723
-    if(ht_entry){
61f723
-        ndn_cache->cache_size -= ht_entry->size;
61f723
-        slapi_ch_free_string(&ht_entry->ndn);
61f723
-        slapi_ch_free((void **)&ht_entry);
61f723
-        PL_HashTableRemove(ndn_cache_hashtable, dn);
61f723
-    }
61f723
 }
61f723
 
61f723
 /* stats for monitor */
61f723
 void
61f723
-ndn_cache_get_stats(PRUint64 *hits, PRUint64 *tries, size_t *size, size_t *max_size, long *count)
61f723
-{
61f723
-    slapi_rwlock_rdlock(ndn_cache_lock);
61f723
-    *hits = slapi_counter_get_value(ndn_cache->cache_hits);
61f723
-    *tries = slapi_counter_get_value(ndn_cache->cache_tries);
61f723
-    *size = ndn_cache->cache_size;
61f723
-    *max_size = ndn_cache->cache_max_size;
61f723
-    *count = ndn_cache->cache_count;
61f723
-    slapi_rwlock_unlock(ndn_cache_lock);
61f723
+ndn_cache_get_stats(PRUint64 *hits, PRUint64 *tries, size_t *size, size_t *max_size, size_t *thread_size, size_t *evicts, size_t *slots, long *count)
61f723
+{
61f723
+    *max_size = t_cache_stats.max_size;
61f723
+    *thread_size = t_cache_stats.thread_max_size;
61f723
+    *slots = t_cache_stats.slots;
61f723
+    *evicts = slapi_counter_get_value(t_cache_stats.cache_evicts);
61f723
+    *hits = slapi_counter_get_value(t_cache_stats.cache_hits);
61f723
+    *tries = slapi_counter_get_value(t_cache_stats.cache_tries);
61f723
+    *size = slapi_counter_get_value(t_cache_stats.cache_size);
61f723
+    *count = slapi_counter_get_value(t_cache_stats.cache_count);
61f723
 }
61f723
 
61f723
 /* Common ancestor sdn is allocated.
61f723
diff --git a/ldap/servers/slapd/slapi-private.h b/ldap/servers/slapd/slapi-private.h
61f723
index 3910dbe..68b59f3 100644
61f723
--- a/ldap/servers/slapd/slapi-private.h
61f723
+++ b/ldap/servers/slapd/slapi-private.h
61f723
@@ -380,7 +380,7 @@ char *slapi_dn_normalize_case_original( char *dn );
61f723
 void ndn_cache_init(void);
61f723
 void ndn_cache_destroy(void);
61f723
 int ndn_cache_started(void);
61f723
-void ndn_cache_get_stats(PRUint64 *hits, PRUint64 *tries, size_t *size, size_t *max_size, long *count);
61f723
+void ndn_cache_get_stats(PRUint64 *hits, PRUint64 *tries, size_t *size, size_t *max_size, size_t *thread_size, size_t *evicts, size_t *slots, long *count);
61f723
 #define NDN_DEFAULT_SIZE 20971520 /* 20mb - size of normalized dn cache */
61f723
 
61f723
 /* filter.c */
61f723
-- 
61f723
2.9.4
61f723