1 /* cache.c - routines to maintain an in-core cache of entries */
13 #include "back-bdb2.h"
15 /* LDBM backend specific entry info -- visible only to the cache */
16 struct ldbm_entry_info {
18 * These items are specific to the LDBM backend and should
21 int lei_state; /* for the cache */
22 #define CACHE_ENTRY_UNDEFINED 0
23 #define CACHE_ENTRY_CREATING 1
24 #define CACHE_ENTRY_READY 2
25 #define CACHE_ENTRY_DELETED 3
27 int lei_refcnt; /* # threads ref'ing this entry */
28 struct entry *lei_lrunext; /* for cache lru list */
29 struct entry *lei_lruprev;
31 #define LEI(e) ((struct ldbm_entry_info *) ((e)->e_private))
33 static int cache_delete_entry_internal(struct cache *cache, Entry *e);
35 static void lru_print(struct cache *cache);
39 cache_entry_private_init( Entry*e )
41 assert( e->e_private == NULL );
43 if( e->e_private != NULL ) {
44 /* this should never happen */
48 e->e_private = ch_calloc(1, sizeof(struct ldbm_entry_info));
54 cache_entry_private_destroy( Entry*e )
56 assert( e->e_private );
64 bdb2i_cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
67 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
69 assert( e->e_private );
73 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
74 Debug( LDAP_DEBUG_TRACE,
75 "====> bdb2i_cache_return_entry_%s( %ld ): created (%d)\n",
76 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
78 LEI(e)->lei_state = CACHE_ENTRY_READY;
80 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
81 if( LEI(e)->lei_refcnt > 0 ) {
82 Debug( LDAP_DEBUG_TRACE,
83 "====> bdb2i_cache_return_entry_%s( %ld ): delete pending (%d)\n",
84 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
87 Debug( LDAP_DEBUG_TRACE,
88 "====> bdb2i_cache_return_entry_%s( %ld ): deleted (%d)\n",
89 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
91 cache_entry_private_destroy( e );
96 Debug( LDAP_DEBUG_TRACE,
97 "====> bdb2i_cache_return_entry_%s( %ld ): returned (%d)\n",
98 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt);
101 /* free cache mutex */
102 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
105 #define LRU_DELETE( cache, e ) { \
106 if ( LEI(e)->lei_lruprev != NULL ) { \
107 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
109 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
111 if ( LEI(e)->lei_lrunext != NULL ) { \
112 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
114 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
118 #define LRU_ADD( cache, e ) { \
119 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
120 if ( LEI(e)->lei_lrunext != NULL ) { \
121 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
123 (cache)->c_lruhead = (e); \
124 LEI(e)->lei_lruprev = NULL; \
125 if ( (cache)->c_lrutail == NULL ) { \
126 (cache)->c_lrutail = (e); \
131 * bdb2i_cache_add_entry_rw - create and lock an entry in the cache
132 * returns: 0 entry has been created and locked
133 * 1 entry already existed
134 * -1 something bad happened
137 bdb2i_cache_add_entry_rw(
146 /* set cache mutex */
147 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
149 assert( e->e_private == NULL );
151 if( cache_entry_private_init(e) != 0 ) {
152 /* free cache mutex */
153 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
155 Debug( LDAP_DEBUG_ANY,
156 "====> bdb2i_cache_add_entry( %ld ): \"%s\": private init failed!\n",
157 e->e_id, e->e_dn, 0 );
162 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
163 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
165 Debug( LDAP_DEBUG_TRACE,
166 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
167 e->e_id, e->e_dn, 0 );
169 cache_entry_private_destroy(e);
171 /* free cache mutex */
172 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
177 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
178 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
180 Debug( LDAP_DEBUG_ANY,
181 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in id cache\n",
182 e->e_id, e->e_dn, 0 );
184 /* delete from dn tree inserted above */
185 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
186 (AVL_CMP) entry_dn_cmp ) == NULL )
188 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
192 cache_entry_private_destroy(e);
194 /* free cache mutex */
195 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
199 /* put the entry into 'CREATING' state */
200 /* will be marked after when entry is returned */
201 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
202 LEI(e)->lei_refcnt = 1;
206 if ( ++cache->c_cursize > cache->c_maxsize ) {
208 * find the lru entry not currently in use and delete it.
209 * in case a lot of entries are in use, only look at the
210 * first 10 on the tail of the list.
213 while ( cache->c_lrutail != NULL &&
214 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
217 /* move this in-use entry to the front of the q */
218 ee = cache->c_lrutail;
219 LRU_DELETE( cache, ee );
220 LRU_ADD( cache, ee );
225 * found at least one to delete - try to get back under
226 * the max cache size.
228 while ( cache->c_lrutail != NULL &&
229 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
230 cache->c_cursize > cache->c_maxsize )
232 e = cache->c_lrutail;
234 /* delete from cache and lru q */
235 cache_delete_entry_internal( cache, e );
236 cache_entry_private_destroy( e );
241 /* free cache mutex */
242 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
247 * cache_update_entry - update a LOCKED entry which has been deleted.
248 * returns: 0 entry has been created and locked
249 * 1 entry already existed
250 * -1 something bad happened
253 bdb2i_cache_update_entry(
261 /* set cache mutex */
262 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
264 assert( e->e_private );
266 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
267 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
269 Debug( LDAP_DEBUG_TRACE,
270 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
271 e->e_id, e->e_dn, 0 );
273 /* free cache mutex */
274 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
279 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
280 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
282 Debug( LDAP_DEBUG_ANY,
283 "====> bdb2i_cache_update_entry( %ld ): \"%s\": already in id cache\n",
284 e->e_id, e->e_dn, 0 );
286 /* delete from dn tree inserted above */
287 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
288 (AVL_CMP) entry_dn_cmp ) == NULL )
290 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
294 /* free cache mutex */
295 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
299 /* put the entry into 'CREATING' state */
300 /* will be marked after when entry is returned */
301 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
305 if ( ++cache->c_cursize > cache->c_maxsize ) {
307 * find the lru entry not currently in use and delete it.
308 * in case a lot of entries are in use, only look at the
309 * first 10 on the tail of the list.
312 while ( cache->c_lrutail != NULL &&
313 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
316 /* move this in-use entry to the front of the q */
317 ee = cache->c_lrutail;
318 LRU_DELETE( cache, ee );
319 LRU_ADD( cache, ee );
324 * found at least one to delete - try to get back under
325 * the max cache size.
327 while ( cache->c_lrutail != NULL &&
328 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
329 cache->c_cursize > cache->c_maxsize )
331 e = cache->c_lrutail;
333 /* delete from cache and lru q */
334 cache_delete_entry_internal( cache, e );
335 cache_entry_private_destroy( e );
340 /* free cache mutex */
341 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
346 * bdb2i_cache_find_entry_dn2id - find an entry in the cache, given dn
350 bdb2i_cache_find_entry_dn2id(
361 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
364 /* set cache mutex */
365 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
367 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
368 (AVL_CMP) entry_dn_cmp )) != NULL )
374 * ep now points to an unlocked entry
375 * we do not need to lock the entry if we only
376 * check the state, refcnt, LRU, and id.
379 assert( ep->e_private );
383 state = LEI(ep)->lei_state;
386 * entry is deleted or not fully created yet
388 if ( state != CACHE_ENTRY_READY ) {
389 assert(state != CACHE_ENTRY_UNDEFINED);
391 /* free cache mutex */
392 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
394 Debug(LDAP_DEBUG_TRACE,
395 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
398 ldap_pvt_thread_yield();
403 LRU_DELETE( cache, ep );
404 LRU_ADD( cache, ep );
406 /* free cache mutex */
407 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
409 Debug(LDAP_DEBUG_TRACE,
410 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
414 /* free cache mutex */
415 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
426 * cache_find_entry_id - find an entry in the cache, given id
430 bdb2i_cache_find_entry_id(
443 /* set cache mutex */
444 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
446 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
447 (AVL_CMP) entry_id_cmp )) != NULL )
451 assert( ep->e_private );
453 state = LEI(ep)->lei_state;
456 * entry is deleted or not fully created yet
458 if ( state != CACHE_ENTRY_READY ) {
461 assert(state != CACHE_ENTRY_UNDEFINED);
463 /* free cache mutex */
464 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
466 Debug(LDAP_DEBUG_TRACE,
467 "====> bdb2i_cache_find_entry_id( %ld ): %ld (not ready) %d\n",
470 ldap_pvt_thread_yield();
475 LRU_DELETE( cache, ep );
476 LRU_ADD( cache, ep );
478 LEI(ep)->lei_refcnt++;
480 /* free cache mutex */
481 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
483 Debug(LDAP_DEBUG_TRACE,
484 "====> bdb2i_cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
485 id, ep->e_dn, count);
490 /* free cache mutex */
491 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
497 * cache_delete_entry - delete the entry e from the cache. the caller
498 * should have obtained e (increasing its ref count) via a call to one
499 * of the cache_find_* routines. the caller should *not* call the
500 * cache_return_entry() routine prior to calling cache_delete_entry().
501 * it performs this function.
503 * returns: 0 e was deleted ok
504 * 1 e was not in the cache
505 * -1 something bad happened
508 bdb2i_cache_delete_entry(
515 /* set cache mutex */
516 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
518 assert( e->e_private );
520 Debug( LDAP_DEBUG_TRACE, "====> bdb2i_cache_delete_entry( %ld )\n",
523 rc = cache_delete_entry_internal( cache, e );
525 /* free cache mutex */
526 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
531 cache_delete_entry_internal(
536 int rc = 0; /* return code */
539 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
546 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
557 LRU_DELETE( cache, e );
561 * flag entry to be freed later by a call to cache_return_entry()
563 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
571 lru_print( struct cache *cache )
575 fprintf( stderr, "LRU queue (head to tail):\n" );
576 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
577 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
578 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
580 fprintf( stderr, "LRU queue (tail to head):\n" );
581 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
582 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
583 e->e_dn, e->e_id, LEI(e)->lei_refcnt );