1 /* cache.c - routines to maintain an in-core cache of entries */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
20 /* BDB backend specific entry info -- visible only to the cache */
21 typedef struct bdb_entry_info {
22 ldap_pvt_thread_rdwr_t bei_rdwr; /* reader/writer lock */
25 * remaining fields require backend cache lock to access
26 * These items are specific to the BDB backend and should
29 int bei_state; /* for the cache */
30 #define CACHE_ENTRY_UNDEFINED 0
31 #define CACHE_ENTRY_CREATING 1
32 #define CACHE_ENTRY_READY 2
33 #define CACHE_ENTRY_DELETED 3
34 #define CACHE_ENTRY_COMMITTED 4
36 int bei_refcnt; /* # threads ref'ing this entry */
37 Entry *bei_lrunext; /* for cache lru list */
41 #define BEI(e) ((EntryInfo *) ((e)->e_private))
43 static int bdb_cache_delete_entry_internal(Cache *cache, Entry *e);
45 static void bdb_lru_print(Cache *cache);
49 bdb_cache_entry_rdwr_lock(Entry *e, int rw)
52 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
53 "bdb_cache_entry_rdwr_lock: %s lock on ID %ld\n",
54 rw ? "w" : "r", e->e_id ));
56 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
57 rw ? "w" : "r", e->e_id, 0);
61 return ldap_pvt_thread_rdwr_wlock(&BEI(e)->bei_rdwr);
63 return ldap_pvt_thread_rdwr_rlock(&BEI(e)->bei_rdwr);
67 bdb_cache_entry_rdwr_trylock(Entry *e, int rw)
70 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
71 "bdb_cache_entry_rdwr_trylock: try %s lock on ID: %ld.\n",
72 rw ? "w" : "r", e->e_id ));
74 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%strylock: ID: %ld\n",
75 rw ? "w" : "r", e->e_id, 0);
79 return ldap_pvt_thread_rdwr_wtrylock(&BEI(e)->bei_rdwr);
81 return ldap_pvt_thread_rdwr_rtrylock(&BEI(e)->bei_rdwr);
85 bdb_cache_entry_rdwr_unlock(Entry *e, int rw)
88 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
89 "bdb_cache_entry_rdwr_unlock: remove %s lock on ID %ld.\n",
90 rw ? "w" : "r", e->e_id ));
92 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
93 rw ? "w" : "r", e->e_id, 0);
97 return ldap_pvt_thread_rdwr_wunlock(&BEI(e)->bei_rdwr);
99 return ldap_pvt_thread_rdwr_runlock(&BEI(e)->bei_rdwr);
103 bdb_cache_entry_rdwr_init(Entry *e)
105 return ldap_pvt_thread_rdwr_init( &BEI(e)->bei_rdwr );
109 bdb_cache_entry_rdwr_destroy(Entry *e)
111 return ldap_pvt_thread_rdwr_destroy( &BEI(e)->bei_rdwr );
115 bdb_cache_entry_private_init( Entry *e )
117 assert( e->e_private == NULL );
119 if( e->e_private != NULL ) {
120 /* this should never happen */
124 e->e_private = ch_calloc(1, sizeof(struct bdb_entry_info));
126 if( bdb_cache_entry_rdwr_init( e ) != 0 ) {
136 * marks an entry in CREATING state as committed, so it is really returned
137 * to the cache. Otherwise an entry in CREATING state is removed.
138 * Makes e_private be destroyed at the following cache_return_entry_w,
139 * but lets the entry untouched (owned by someone else)
142 bdb_cache_entry_commit( Entry *e )
145 assert( e->e_private );
146 assert( BEI(e)->bei_state == CACHE_ENTRY_CREATING );
147 /* assert( BEI(e)->bei_refcnt == 1 ); */
149 BEI(e)->bei_state = CACHE_ENTRY_COMMITTED;
153 bdb_cache_entry_private_destroy( Entry *e )
155 assert( e->e_private );
157 bdb_cache_entry_rdwr_destroy( e );
159 free( e->e_private );
165 bdb_cache_return_entry_rw( Cache *cache, Entry *e, int rw )
168 int refcnt, freeit = 1;
170 /* set cache mutex */
171 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
173 assert( e->e_private );
175 bdb_cache_entry_rdwr_unlock(e, rw);
178 refcnt = --BEI(e)->bei_refcnt;
181 * if the entry is returned when in CREATING state, it is deleted
182 * but not freed because it may belong to someone else (do_add,
185 if ( BEI(e)->bei_state == CACHE_ENTRY_CREATING ) {
186 bdb_cache_delete_entry_internal( cache, e );
188 /* now the entry is in DELETED state */
191 if ( BEI(e)->bei_state == CACHE_ENTRY_COMMITTED ) {
192 BEI(e)->bei_state = CACHE_ENTRY_READY;
194 /* free cache mutex */
195 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
198 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
199 "bdb_cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
200 id, rw ? "w" : "r", refcnt ));
202 Debug( LDAP_DEBUG_TRACE,
203 "====> bdb_cache_return_entry_%s( %ld ): created (%d)\n",
204 rw ? "w" : "r", id, refcnt );
208 } else if ( BEI(e)->bei_state == CACHE_ENTRY_DELETED ) {
210 /* free cache mutex */
211 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
214 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
215 "bdb_cache_return_entry_rw: %ld, delete pending (%d).\n",
218 Debug( LDAP_DEBUG_TRACE,
219 "====> bdb_cache_return_entry_%s( %ld ): delete pending (%d)\n",
220 rw ? "w" : "r", id, refcnt );
224 bdb_cache_entry_private_destroy( e );
226 bdb_entry_return( e );
229 /* free cache mutex */
230 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
233 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
234 "bdb_cache_return_entry_rw: (%ld): deleted (%d)\n",
237 Debug( LDAP_DEBUG_TRACE,
238 "====> bdb_cache_return_entry_%s( %ld ): deleted (%d)\n",
239 rw ? "w" : "r", id, refcnt );
244 /* free cache mutex */
245 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
248 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
249 "bdb_cache_return_entry_rw: ID %ld:%s returned (%d)\n",
250 id, rw ? "w": "r", refcnt ));
252 Debug( LDAP_DEBUG_TRACE,
253 "====> bdb_cache_return_entry_%s( %ld ): returned (%d)\n",
254 rw ? "w" : "r", id, refcnt);
259 #define LRU_DELETE( cache, e ) do { \
260 if ( BEI(e)->bei_lruprev != NULL ) { \
261 BEI(BEI(e)->bei_lruprev)->bei_lrunext = BEI(e)->bei_lrunext; \
263 (cache)->c_lruhead = BEI(e)->bei_lrunext; \
265 if ( BEI(e)->bei_lrunext != NULL ) { \
266 BEI(BEI(e)->bei_lrunext)->bei_lruprev = BEI(e)->bei_lruprev; \
268 (cache)->c_lrutail = BEI(e)->bei_lruprev; \
272 #define LRU_ADD( cache, e ) do { \
273 BEI(e)->bei_lrunext = (cache)->c_lruhead; \
274 if ( BEI(e)->bei_lrunext != NULL ) { \
275 BEI(BEI(e)->bei_lrunext)->bei_lruprev = (e); \
277 (cache)->c_lruhead = (e); \
278 BEI(e)->bei_lruprev = NULL; \
279 if ( (cache)->c_lrutail == NULL ) { \
280 (cache)->c_lrutail = (e); \
285 * cache_add_entry_rw - create and lock an entry in the cache
286 * returns: 0 entry has been created and locked
287 * 1 entry already existed
288 * -1 something bad happened
291 bdb_cache_add_entry_rw(
301 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
302 "bdb_cache_add_entry_rw: add (%s):%s to cache\n",
303 e->e_dn, rw ? "w" : "r" ));
305 /* set cache mutex */
306 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
308 assert( e->e_private == NULL );
310 if( bdb_cache_entry_private_init(e) != 0 ) {
311 /* free cache mutex */
312 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
315 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
316 "bdb_cache_add_entry_rw: add (%s):%ld private init failed!\n",
319 Debug( LDAP_DEBUG_ANY,
320 "====> bdb_cache_add_entry( %ld ): \"%s\": private init failed!\n",
321 e->e_id, e->e_dn, 0 );
328 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
329 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
331 /* free cache mutex */
332 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
335 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
336 "bdb_cache_add_entry: (%s):%ld already in cache.\n",
339 Debug( LDAP_DEBUG_TRACE,
340 "====> bdb_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
341 e->e_id, e->e_dn, 0 );
344 bdb_cache_entry_private_destroy(e);
350 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
351 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
354 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
355 "bdb_cache_add_entry: (%s):%ls already in cache.\n",
358 Debug( LDAP_DEBUG_ANY,
359 "====> bdb_cache_add_entry( %ld ): \"%s\": already in id cache\n",
360 e->e_id, e->e_dn, 0 );
363 /* delete from dn tree inserted above */
364 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
365 (AVL_CMP) entry_dn_cmp ) == NULL )
368 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
369 "bdb_cache_add_entry: can't delete (%s) from cache.\n",
372 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
377 bdb_cache_entry_private_destroy(e);
379 /* free cache mutex */
380 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
384 bdb_cache_entry_rdwr_lock( e, rw );
386 /* put the entry into 'CREATING' state */
387 /* will be marked after when entry is returned */
388 BEI(e)->bei_state = CACHE_ENTRY_CREATING;
389 BEI(e)->bei_refcnt = 1;
393 if ( ++cache->c_cursize > cache->c_maxsize ) {
395 * find the lru entry not currently in use and delete it.
396 * in case a lot of entries are in use, only look at the
397 * first 10 on the tail of the list.
400 while ( cache->c_lrutail != NULL &&
401 BEI(cache->c_lrutail)->bei_refcnt != 0 &&
404 /* move this in-use entry to the front of the q */
405 ee = cache->c_lrutail;
406 LRU_DELETE( cache, ee );
407 LRU_ADD( cache, ee );
412 * found at least one to delete - try to get back under
413 * the max cache size.
415 while ( cache->c_lrutail != NULL &&
416 BEI(cache->c_lrutail)->bei_refcnt == 0 &&
417 cache->c_cursize > cache->c_maxsize )
419 e = cache->c_lrutail;
421 /* delete from cache and lru q */
422 /* XXX do we need rc ? */
423 rc = bdb_cache_delete_entry_internal( cache, e );
424 bdb_cache_entry_private_destroy( e );
425 bdb_entry_return( e );
429 /* free cache mutex */
430 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
435 * cache_update_entry - update a LOCKED entry which has been deleted.
436 * returns: 0 entry has been created and locked
437 * 1 entry already existed
438 * -1 something bad happened
441 bdb_cache_update_entry(
449 /* set cache mutex */
450 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
452 assert( e->e_private );
454 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
455 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
458 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
459 "bdb_cache_update_entry: (%s):%ld already in dn cache\n",
462 Debug( LDAP_DEBUG_TRACE,
463 "====> bdb_cache_update_entry( %ld ): \"%s\": already in dn cache\n",
464 e->e_id, e->e_dn, 0 );
467 /* free cache mutex */
468 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
473 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
474 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
477 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
478 "bdb_cache_update_entry: (%s)%ld already in id cache\n",
481 Debug( LDAP_DEBUG_ANY,
482 "====> bdb_cache_update_entry( %ld ): \"%s\": already in id cache\n",
483 e->e_id, e->e_dn, 0 );
486 /* delete from dn tree inserted above */
487 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
488 (AVL_CMP) entry_dn_cmp ) == NULL )
491 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
492 "bdb_cache_update_entry: can't delete (%s)%ld from dn cache.\n",
495 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
500 /* free cache mutex */
501 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
506 /* put the entry into 'CREATING' state */
507 /* will be marked after when entry is returned */
508 BEI(e)->bei_state = CACHE_ENTRY_CREATING;
512 if ( ++cache->c_cursize > cache->c_maxsize ) {
514 * find the lru entry not currently in use and delete it.
515 * in case a lot of entries are in use, only look at the
516 * first 10 on the tail of the list.
519 while ( cache->c_lrutail != NULL &&
520 BEI(cache->c_lrutail)->bei_refcnt != 0 &&
523 /* move this in-use entry to the front of the q */
524 ee = cache->c_lrutail;
525 LRU_DELETE( cache, ee );
526 LRU_ADD( cache, ee );
531 * found at least one to delete - try to get back under
532 * the max cache size.
534 while ( cache->c_lrutail != NULL &&
535 BEI(cache->c_lrutail)->bei_refcnt == 0 &&
536 cache->c_cursize > cache->c_maxsize )
538 e = cache->c_lrutail;
540 /* delete from cache and lru q */
541 /* XXX do we need rc ? */
542 rc = bdb_cache_delete_entry_internal( cache, e );
543 bdb_cache_entry_private_destroy( e );
544 bdb_entry_return( e );
548 /* free cache mutex */
549 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
554 bdb_cache_find_entry_ndn2id(
564 /* this function is always called with normalized DN */
568 /* set cache mutex */
569 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
571 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
572 (AVL_CMP) entry_dn_cmp )) != NULL )
578 * ep now points to an unlocked entry
579 * we do not need to lock the entry if we only
580 * check the state, refcnt, LRU, and id.
583 assert( ep->e_private );
587 state = BEI(ep)->bei_state;
590 * entry is deleted or not fully created yet
592 if ( state != CACHE_ENTRY_READY ) {
593 assert(state != CACHE_ENTRY_UNDEFINED);
595 /* free cache mutex */
596 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
599 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
600 "bdb_cache_find_entry_dn2id: (%s) %ld not ready: %d\n",
601 ndn->bv_val, id, state ));
603 Debug(LDAP_DEBUG_TRACE,
604 "====> bdb_cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
605 ndn->bv_val, id, state);
609 ldap_pvt_thread_yield();
614 LRU_DELETE( cache, ep );
615 LRU_ADD( cache, ep );
617 /* free cache mutex */
618 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
621 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
622 "bdb_cache_find_entry_dn2id: (%s): %ld %d tries\n",
623 ndn->bv_val, id, count ));
625 Debug(LDAP_DEBUG_TRACE,
626 "====> bdb_cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
627 ndn->bv_val, id, count);
631 /* free cache mutex */
632 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
641 * cache_find_entry_id - find an entry in the cache, given id
645 bdb_cache_find_entry_id(
658 /* set cache mutex */
659 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
661 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
662 (AVL_CMP) entry_id_cmp )) != NULL )
669 assert( ep->e_private );
672 state = BEI(ep)->bei_state;
675 * entry is deleted or not fully created yet
677 if ( state != CACHE_ENTRY_READY ) {
679 assert(state != CACHE_ENTRY_UNDEFINED);
681 /* free cache mutex */
682 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
685 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
686 "bdb_cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
690 Debug(LDAP_DEBUG_TRACE,
691 "====> bdb_cache_find_entry_id( %ld ): %ld (not ready) %d\n",
695 ldap_pvt_thread_yield();
699 /* acquire reader lock */
700 if ( bdb_cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
701 /* could not acquire entry lock...
702 * owner cannot free as we have the cache locked.
703 * so, unlock the cache, yield, and try again.
706 /* free cache mutex */
707 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
710 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
711 "bdb_cache_find_entry_id: %ld -> %ld (busy) %d.\n",
714 Debug(LDAP_DEBUG_TRACE,
715 "====> bdb_cache_find_entry_id( %ld ): %ld (busy) %d\n",
719 ldap_pvt_thread_yield();
724 LRU_DELETE( cache, ep );
725 LRU_ADD( cache, ep );
727 BEI(ep)->bei_refcnt++;
729 /* free cache mutex */
730 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
733 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
734 "bdb_cache_find_entry_id: %ld -> %s found %d tries.\n",
735 ep_id, ep->e_dn, count ));
737 Debug(LDAP_DEBUG_TRACE,
738 "====> bdb_cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
739 ep_id, ep->e_dn, count);
746 /* free cache mutex */
747 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
753 * cache_delete_entry - delete the entry e from the cache. the caller
754 * should have obtained e (increasing its ref count) via a call to one
755 * of the cache_find_* routines. the caller should *not* call the
756 * cache_return_entry() routine prior to calling cache_delete_entry().
757 * it performs this function.
759 * returns: 0 e was deleted ok
760 * 1 e was not in the cache
761 * -1 something bad happened
764 bdb_cache_delete_entry(
771 /* set cache mutex */
772 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
774 assert( e->e_private );
777 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
778 "bdb_cache_delete_entry: delete %ld.\n", e->e_id ));
780 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete_entry( %ld )\n",
784 rc = bdb_cache_delete_entry_internal( cache, e );
786 /* free cache mutex */
787 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
792 bdb_cache_delete_entry_internal(
797 int rc = 0; /* return code */
800 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
807 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
818 LRU_DELETE( cache, e );
822 * flag entry to be freed later by a call to cache_return_entry()
824 BEI(e)->bei_state = CACHE_ENTRY_DELETED;
830 bdb_cache_release_all( Cache *cache )
835 /* set cache mutex */
836 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
839 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
840 "bdb_cache_release_all: enter\n" ));
842 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
845 while ( (e = cache->c_lrutail) != NULL && BEI(e)->bei_refcnt == 0 ) {
846 #ifdef LDAP_RDWR_DEBUG
847 assert(!ldap_pvt_thread_rdwr_active(&BEI(e)->bei_rdwr));
850 /* delete from cache and lru q */
851 /* XXX do we need rc ? */
852 rc = bdb_cache_delete_entry_internal( cache, e );
853 bdb_cache_entry_private_destroy( e );
854 bdb_entry_return( e );
857 if ( cache->c_cursize ) {
859 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
860 "bdb_cache_release_all: Entry cache could not be emptied.\n" ));
862 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
867 /* free cache mutex */
868 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
873 bdb_lru_print( Cache *cache )
877 fprintf( stderr, "LRU queue (head to tail):\n" );
878 for ( e = cache->c_lruhead; e != NULL; e = BEI(e)->bei_lrunext ) {
879 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
880 e->e_dn, e->e_id, BEI(e)->bei_refcnt );
882 fprintf( stderr, "LRU queue (tail to head):\n" );
883 for ( e = cache->c_lrutail; e != NULL; e = BEI(e)->bei_lruprev ) {
884 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
885 e->e_dn, e->e_id, BEI(e)->bei_refcnt );