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

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