1 /* cache.c - routines to maintain an in-core cache of entries */
13 #include "back-ldbm.h"
15 /* LDBM backend specific entry info -- visible only to the cache */
16 struct ldbm_entry_info {
17 ldap_pvt_thread_rdwr_t lei_rdwr; /* reader/writer lock */
20 * remaining fields require backend cache lock to access
21 * These items are specific to the LDBM backend and should
24 int lei_state; /* for the cache */
25 #define CACHE_ENTRY_UNDEFINED 0
26 #define CACHE_ENTRY_CREATING 1
27 #define CACHE_ENTRY_READY 2
28 #define CACHE_ENTRY_DELETED 3
30 int lei_refcnt; /* # threads ref'ing this entry */
31 struct entry *lei_lrunext; /* for cache lru list */
32 struct entry *lei_lruprev;
34 #define LEI(e) ((struct ldbm_entry_info *) ((e)->e_private))
36 static int cache_delete_entry_internal(struct cache *cache, Entry *e);
38 static void lru_print(struct cache *cache);
42 cache_entry_rdwr_lock(Entry *e, int rw)
44 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
45 rw ? "w" : "r", e->e_id, 0);
48 return ldap_pvt_thread_rdwr_wlock(&LEI(e)->lei_rdwr);
50 return ldap_pvt_thread_rdwr_rlock(&LEI(e)->lei_rdwr);
54 cache_entry_rdwr_trylock(Entry *e, int rw)
56 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%strylock: ID: %ld\n",
57 rw ? "w" : "r", e->e_id, 0);
60 return ldap_pvt_thread_rdwr_wtrylock(&LEI(e)->lei_rdwr);
62 return ldap_pvt_thread_rdwr_rtrylock(&LEI(e)->lei_rdwr);
66 cache_entry_rdwr_unlock(Entry *e, int rw)
68 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
69 rw ? "w" : "r", e->e_id, 0);
72 return ldap_pvt_thread_rdwr_wunlock(&LEI(e)->lei_rdwr);
74 return ldap_pvt_thread_rdwr_runlock(&LEI(e)->lei_rdwr);
78 cache_entry_rdwr_init(Entry *e)
80 return ldap_pvt_thread_rdwr_init( &LEI(e)->lei_rdwr );
84 cache_entry_rdwr_destroy(Entry *e)
86 return ldap_pvt_thread_rdwr_destroy( &LEI(e)->lei_rdwr );
90 cache_entry_private_init( Entry*e )
92 assert( e->e_private == NULL );
94 if( e->e_private != NULL ) {
95 /* this should never happen */
99 e->e_private = ch_calloc(1, sizeof(struct ldbm_entry_info));
101 if( cache_entry_rdwr_init( e ) != 0 ) {
111 cache_entry_private_destroy( Entry*e )
113 assert( e->e_private );
115 cache_entry_rdwr_destroy( e );
117 free( e->e_private );
123 cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
128 /* set cache mutex */
129 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
131 assert( e->e_private );
133 cache_entry_rdwr_unlock(e, rw);
136 refcnt = --LEI(e)->lei_refcnt;
138 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
139 LEI(e)->lei_state = CACHE_ENTRY_READY;
141 /* free cache mutex */
142 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
144 Debug( LDAP_DEBUG_TRACE,
145 "====> cache_return_entry_%s( %ld ): created (%d)\n",
146 rw ? "w" : "r", id, refcnt );
148 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
150 /* free cache mutex */
151 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
153 Debug( LDAP_DEBUG_TRACE,
154 "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
155 rw ? "w" : "r", id, refcnt );
158 cache_entry_private_destroy( e );
161 /* free cache mutex */
162 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
164 Debug( LDAP_DEBUG_TRACE,
165 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
166 rw ? "w" : "r", id, refcnt );
170 /* free cache mutex */
171 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
173 Debug( LDAP_DEBUG_TRACE,
174 "====> cache_return_entry_%s( %ld ): returned (%d)\n",
175 rw ? "w" : "r", id, refcnt);
179 #define LRU_DELETE( cache, e ) do { \
180 if ( LEI(e)->lei_lruprev != NULL ) { \
181 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
183 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
185 if ( LEI(e)->lei_lrunext != NULL ) { \
186 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
188 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
192 #define LRU_ADD( cache, e ) do { \
193 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
194 if ( LEI(e)->lei_lrunext != NULL ) { \
195 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
197 (cache)->c_lruhead = (e); \
198 LEI(e)->lei_lruprev = NULL; \
199 if ( (cache)->c_lrutail == NULL ) { \
200 (cache)->c_lrutail = (e); \
205 * cache_add_entry_rw - create and lock an entry in the cache
206 * returns: 0 entry has been created and locked
207 * 1 entry already existed
208 * -1 something bad happened
220 /* set cache mutex */
221 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
223 assert( e->e_private == NULL );
225 if( cache_entry_private_init(e) != 0 ) {
226 /* free cache mutex */
227 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
229 Debug( LDAP_DEBUG_ANY,
230 "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
231 e->e_id, e->e_dn, 0 );
236 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
237 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
239 /* free cache mutex */
240 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
242 Debug( LDAP_DEBUG_TRACE,
243 "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
244 e->e_id, e->e_dn, 0 );
246 cache_entry_private_destroy(e);
252 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
253 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
255 Debug( LDAP_DEBUG_ANY,
256 "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
257 e->e_id, e->e_dn, 0 );
260 /* delete from dn tree inserted above */
261 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
262 (AVL_CMP) entry_dn_cmp ) == NULL )
264 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
268 cache_entry_private_destroy(e);
270 /* free cache mutex */
271 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
275 cache_entry_rdwr_lock( e, rw );
277 /* put the entry into 'CREATING' state */
278 /* will be marked after when entry is returned */
279 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
280 LEI(e)->lei_refcnt = 1;
284 if ( ++cache->c_cursize > cache->c_maxsize ) {
286 * find the lru entry not currently in use and delete it.
287 * in case a lot of entries are in use, only look at the
288 * first 10 on the tail of the list.
291 while ( cache->c_lrutail != NULL &&
292 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
295 /* move this in-use entry to the front of the q */
296 ee = cache->c_lrutail;
297 LRU_DELETE( cache, ee );
298 LRU_ADD( cache, ee );
303 * found at least one to delete - try to get back under
304 * the max cache size.
306 while ( cache->c_lrutail != NULL &&
307 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
308 cache->c_cursize > cache->c_maxsize )
310 e = cache->c_lrutail;
312 /* delete from cache and lru q */
313 /* XXX do we need rc ? */
314 rc = cache_delete_entry_internal( cache, e );
315 cache_entry_private_destroy( e );
320 /* free cache mutex */
321 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
326 * cache_update_entry - update a LOCKED entry which has been deleted.
327 * returns: 0 entry has been created and locked
328 * 1 entry already existed
329 * -1 something bad happened
340 /* set cache mutex */
341 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
343 assert( e->e_private );
345 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
346 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
348 Debug( LDAP_DEBUG_TRACE,
349 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
350 e->e_id, e->e_dn, 0 );
352 /* free cache mutex */
353 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
358 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
359 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
361 Debug( LDAP_DEBUG_ANY,
362 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
363 e->e_id, e->e_dn, 0 );
365 /* delete from dn tree inserted above */
366 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
367 (AVL_CMP) entry_dn_cmp ) == NULL )
369 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
373 /* free cache mutex */
374 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
379 /* put the entry into 'CREATING' state */
380 /* will be marked after when entry is returned */
381 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
385 if ( ++cache->c_cursize > cache->c_maxsize ) {
387 * find the lru entry not currently in use and delete it.
388 * in case a lot of entries are in use, only look at the
389 * first 10 on the tail of the list.
392 while ( cache->c_lrutail != NULL &&
393 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
396 /* move this in-use entry to the front of the q */
397 ee = cache->c_lrutail;
398 LRU_DELETE( cache, ee );
399 LRU_ADD( cache, ee );
404 * found at least one to delete - try to get back under
405 * the max cache size.
407 while ( cache->c_lrutail != NULL &&
408 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
409 cache->c_cursize > cache->c_maxsize )
411 e = cache->c_lrutail;
413 /* delete from cache and lru q */
414 /* XXX do we need rc ? */
415 rc = cache_delete_entry_internal( cache, e );
416 cache_entry_private_destroy( e );
421 /* free cache mutex */
422 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
427 * cache_find_entry_dn2id - find an entry in the cache, given dn
431 cache_find_entry_dn2id(
442 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
445 /* set cache mutex */
446 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
448 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
449 (AVL_CMP) entry_dn_cmp )) != NULL )
455 * ep now points to an unlocked entry
456 * we do not need to lock the entry if we only
457 * check the state, refcnt, LRU, and id.
460 assert( ep->e_private );
464 state = LEI(ep)->lei_state;
467 * entry is deleted or not fully created yet
469 if ( state != CACHE_ENTRY_READY ) {
470 assert(state != CACHE_ENTRY_UNDEFINED);
472 /* free cache mutex */
473 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
475 Debug(LDAP_DEBUG_TRACE,
476 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
479 ldap_pvt_thread_yield();
484 LRU_DELETE( cache, ep );
485 LRU_ADD( cache, ep );
487 /* free cache mutex */
488 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
490 Debug(LDAP_DEBUG_TRACE,
491 "====> cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
495 /* free cache mutex */
496 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
507 * cache_find_entry_id - find an entry in the cache, given id
524 /* set cache mutex */
525 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
527 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
528 (AVL_CMP) entry_id_cmp )) != NULL )
535 assert( ep->e_private );
538 state = LEI(ep)->lei_state;
541 * entry is deleted or not fully created yet
543 if ( state != CACHE_ENTRY_READY ) {
545 assert(state != CACHE_ENTRY_UNDEFINED);
547 /* free cache mutex */
548 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
550 Debug(LDAP_DEBUG_TRACE,
551 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
554 ldap_pvt_thread_yield();
558 /* acquire reader lock */
559 if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
560 /* could not acquire entry lock...
561 * owner cannot free as we have the cache locked.
562 * so, unlock the cache, yield, and try again.
565 /* free cache mutex */
566 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
568 Debug(LDAP_DEBUG_TRACE,
569 "====> cache_find_entry_id( %ld ): %ld (busy) %d\n",
572 ldap_pvt_thread_yield();
577 LRU_DELETE( cache, ep );
578 LRU_ADD( cache, ep );
580 LEI(ep)->lei_refcnt++;
582 /* free cache mutex */
583 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
585 Debug(LDAP_DEBUG_TRACE,
586 "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
587 ep_id, ep->e_dn, count);
592 /* free cache mutex */
593 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
599 * cache_delete_entry - delete the entry e from the cache. the caller
600 * should have obtained e (increasing its ref count) via a call to one
601 * of the cache_find_* routines. the caller should *not* call the
602 * cache_return_entry() routine prior to calling cache_delete_entry().
603 * it performs this function.
605 * returns: 0 e was deleted ok
606 * 1 e was not in the cache
607 * -1 something bad happened
617 /* set cache mutex */
618 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
620 assert( e->e_private );
622 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
625 rc = cache_delete_entry_internal( cache, e );
627 /* free cache mutex */
628 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
633 cache_delete_entry_internal(
638 int rc = 0; /* return code */
641 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
648 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
659 LRU_DELETE( cache, e );
663 * flag entry to be freed later by a call to cache_return_entry()
665 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
673 cache_release_all( struct cache *cache )
678 /* set cache mutex */
679 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
681 Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
683 while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
684 assert(!ldap_pvt_thread_rdwr_active(&LEI(e)->lei_rdwr));
686 /* delete from cache and lru q */
687 /* XXX do we need rc ? */
688 rc = cache_delete_entry_internal( cache, e );
689 cache_entry_private_destroy( e );
693 if ( cache->c_cursize ) {
694 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
697 /* free cache mutex */
698 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
701 #endif /* SLAP_CLEANUP */
706 lru_print( struct cache *cache )
710 fprintf( stderr, "LRU queue (head to tail):\n" );
711 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
712 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
713 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
715 fprintf( stderr, "LRU queue (tail to head):\n" );
716 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
717 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
718 e->e_dn, e->e_id, LEI(e)->lei_refcnt );