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 LDBM 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 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);
80 return ldap_pvt_thread_rdwr_wtrylock(&BEI(e)->bei_rdwr);
82 return ldap_pvt_thread_rdwr_rtrylock(&BEI(e)->bei_rdwr);
86 bdb_cache_entry_rdwr_unlock(Entry *e, int rw)
89 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
90 "bdb_cache_entry_rdwr_unlock: remove %s lock on ID %ld.\n",
91 rw ? "w" : "r", e->e_id ));
93 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
94 rw ? "w" : "r", e->e_id, 0);
99 return ldap_pvt_thread_rdwr_wunlock(&BEI(e)->bei_rdwr);
101 return ldap_pvt_thread_rdwr_runlock(&BEI(e)->bei_rdwr);
105 bdb_cache_entry_rdwr_init(Entry *e)
107 return ldap_pvt_thread_rdwr_init( &BEI(e)->bei_rdwr );
111 bdb_cache_entry_rdwr_destroy(Entry *e)
113 return ldap_pvt_thread_rdwr_destroy( &BEI(e)->bei_rdwr );
117 bdb_cache_entry_private_init( Entry *e )
119 assert( e->e_private == NULL );
121 if( e->e_private != NULL ) {
122 /* this should never happen */
126 e->e_private = ch_calloc(1, sizeof(struct bdb_entry_info));
128 if( bdb_cache_entry_rdwr_init( e ) != 0 ) {
138 * marks an entry in CREATING state as committed, so it is really returned
139 * to the cache. Otherwise an entry in CREATING state is removed.
140 * Makes e_private be destroyed at the following cache_return_entry_w,
141 * but lets the entry untouched (owned by someone else)
144 bdb_cache_entry_commit( Entry *e )
147 assert( e->e_private );
148 assert( BEI(e)->bei_state == CACHE_ENTRY_CREATING );
149 /* assert( BEI(e)->bei_refcnt == 1 ); */
151 BEI(e)->bei_state = CACHE_ENTRY_COMMITTED;
155 bdb_cache_entry_private_destroy( Entry *e )
157 assert( e->e_private );
159 bdb_cache_entry_rdwr_destroy( e );
161 free( e->e_private );
167 bdb_cache_return_entry_rw( Cache *cache, Entry *e, int rw )
170 int refcnt, freeit = 1;
172 /* set cache mutex */
173 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
175 assert( e->e_private );
177 bdb_cache_entry_rdwr_unlock(e, rw);
180 refcnt = --BEI(e)->bei_refcnt;
183 * if the entry is returned when in CREATING state, it is deleted
184 * but not freed because it may belong to someone else (do_add,
187 if ( BEI(e)->bei_state == CACHE_ENTRY_CREATING ) {
188 bdb_cache_delete_entry_internal( cache, e );
190 /* now the entry is in DELETED state */
193 if ( BEI(e)->bei_state == CACHE_ENTRY_COMMITTED ) {
194 BEI(e)->bei_state = CACHE_ENTRY_READY;
196 /* free cache mutex */
197 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
200 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
201 "bdb_cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
202 id, rw ? "w" : "r", refcnt ));
204 Debug( LDAP_DEBUG_TRACE,
205 "====> bdb_cache_return_entry_%s( %ld ): created (%d)\n",
206 rw ? "w" : "r", id, refcnt );
210 } else if ( BEI(e)->bei_state == CACHE_ENTRY_DELETED ) {
212 /* free cache mutex */
213 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
216 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
217 "bdb_cache_return_entry_rw: %ld, delete pending (%d).\n",
220 Debug( LDAP_DEBUG_TRACE,
221 "====> bdb_cache_return_entry_%s( %ld ): delete pending (%d)\n",
222 rw ? "w" : "r", id, refcnt );
227 bdb_cache_entry_private_destroy( e );
229 bdb_entry_return( e );
232 /* free cache mutex */
233 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
236 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
237 "bdb_cache_return_entry_rw: (%ld): deleted (%d)\n",
240 Debug( LDAP_DEBUG_TRACE,
241 "====> bdb_cache_return_entry_%s( %ld ): deleted (%d)\n",
242 rw ? "w" : "r", id, refcnt );
248 /* free cache mutex */
249 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
252 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
253 "bdb_cache_return_entry_rw: ID %ld:%s returned (%d)\n",
254 id, rw ? "w": "r", refcnt ));
256 Debug( LDAP_DEBUG_TRACE,
257 "====> bdb_cache_return_entry_%s( %ld ): returned (%d)\n",
258 rw ? "w" : "r", id, refcnt);
264 #define LRU_DELETE( cache, e ) do { \
265 if ( BEI(e)->bei_lruprev != NULL ) { \
266 BEI(BEI(e)->bei_lruprev)->bei_lrunext = BEI(e)->bei_lrunext; \
268 (cache)->c_lruhead = BEI(e)->bei_lrunext; \
270 if ( BEI(e)->bei_lrunext != NULL ) { \
271 BEI(BEI(e)->bei_lrunext)->bei_lruprev = BEI(e)->bei_lruprev; \
273 (cache)->c_lrutail = BEI(e)->bei_lruprev; \
277 #define LRU_ADD( cache, e ) do { \
278 BEI(e)->bei_lrunext = (cache)->c_lruhead; \
279 if ( BEI(e)->bei_lrunext != NULL ) { \
280 BEI(BEI(e)->bei_lrunext)->bei_lruprev = (e); \
282 (cache)->c_lruhead = (e); \
283 BEI(e)->bei_lruprev = NULL; \
284 if ( (cache)->c_lrutail == NULL ) { \
285 (cache)->c_lrutail = (e); \
290 * cache_add_entry_rw - create and lock an entry in the cache
291 * returns: 0 entry has been created and locked
292 * 1 entry already existed
293 * -1 something bad happened
296 bdb_cache_add_entry_rw(
306 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
307 "bdb_cache_add_entry_rw: add (%s):%s to cache\n",
308 e->e_dn, rw ? "w" : "r" ));
310 /* set cache mutex */
311 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
313 assert( e->e_private == NULL );
315 if( bdb_cache_entry_private_init(e) != 0 ) {
316 /* free cache mutex */
317 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
320 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
321 "bdb_cache_add_entry_rw: add (%s):%ld private init failed!\n",
324 Debug( LDAP_DEBUG_ANY,
325 "====> bdb_cache_add_entry( %ld ): \"%s\": private init failed!\n",
326 e->e_id, e->e_dn, 0 );
333 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
334 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
336 /* free cache mutex */
337 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
340 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
341 "bdb_cache_add_entry: (%s):%ld already in cache.\n",
344 Debug( LDAP_DEBUG_TRACE,
345 "====> bdb_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
346 e->e_id, e->e_dn, 0 );
350 bdb_cache_entry_private_destroy(e);
356 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
357 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
360 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
361 "bdb_cache_add_entry: (%s):%ls already in cache.\n",
364 Debug( LDAP_DEBUG_ANY,
365 "====> bdb_cache_add_entry( %ld ): \"%s\": already in id cache\n",
366 e->e_id, e->e_dn, 0 );
371 /* delete from dn tree inserted above */
372 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
373 (AVL_CMP) entry_dn_cmp ) == NULL )
376 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
377 "bdb_cache_add_entry: can't delete (%s) from cache.\n",
380 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
386 bdb_cache_entry_private_destroy(e);
388 /* free cache mutex */
389 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
393 bdb_cache_entry_rdwr_lock( e, rw );
395 /* put the entry into 'CREATING' state */
396 /* will be marked after when entry is returned */
397 BEI(e)->bei_state = CACHE_ENTRY_CREATING;
398 BEI(e)->bei_refcnt = 1;
402 if ( ++cache->c_cursize > cache->c_maxsize ) {
404 * find the lru entry not currently in use and delete it.
405 * in case a lot of entries are in use, only look at the
406 * first 10 on the tail of the list.
409 while ( cache->c_lrutail != NULL &&
410 BEI(cache->c_lrutail)->bei_refcnt != 0 &&
413 /* move this in-use entry to the front of the q */
414 ee = cache->c_lrutail;
415 LRU_DELETE( cache, ee );
416 LRU_ADD( cache, ee );
421 * found at least one to delete - try to get back under
422 * the max cache size.
424 while ( cache->c_lrutail != NULL &&
425 BEI(cache->c_lrutail)->bei_refcnt == 0 &&
426 cache->c_cursize > cache->c_maxsize )
428 e = cache->c_lrutail;
430 /* delete from cache and lru q */
431 /* XXX do we need rc ? */
432 rc = bdb_cache_delete_entry_internal( cache, e );
433 bdb_cache_entry_private_destroy( e );
434 bdb_entry_return( e );
438 /* free cache mutex */
439 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
444 * cache_update_entry - update a LOCKED entry which has been deleted.
445 * returns: 0 entry has been created and locked
446 * 1 entry already existed
447 * -1 something bad happened
450 bdb_cache_update_entry(
458 /* set cache mutex */
459 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
461 assert( e->e_private );
463 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
464 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
467 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
468 "bdb_cache_update_entry: (%s):%ld already in dn cache\n",
471 Debug( LDAP_DEBUG_TRACE,
472 "====> bdb_cache_update_entry( %ld ): \"%s\": already in dn cache\n",
473 e->e_id, e->e_dn, 0 );
477 /* free cache mutex */
478 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
483 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
484 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
487 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
488 "bdb_cache_update_entry: (%s)%ld already in id cache\n",
491 Debug( LDAP_DEBUG_ANY,
492 "====> bdb_cache_update_entry( %ld ): \"%s\": already in id cache\n",
493 e->e_id, e->e_dn, 0 );
497 /* delete from dn tree inserted above */
498 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
499 (AVL_CMP) entry_dn_cmp ) == NULL )
502 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
503 "bdb_cache_update_entry: can't delete (%s)%ld from dn cache.\n",
506 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
512 /* free cache mutex */
513 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
518 /* put the entry into 'CREATING' state */
519 /* will be marked after when entry is returned */
520 BEI(e)->bei_state = CACHE_ENTRY_CREATING;
524 if ( ++cache->c_cursize > cache->c_maxsize ) {
526 * find the lru entry not currently in use and delete it.
527 * in case a lot of entries are in use, only look at the
528 * first 10 on the tail of the list.
531 while ( cache->c_lrutail != NULL &&
532 BEI(cache->c_lrutail)->bei_refcnt != 0 &&
535 /* move this in-use entry to the front of the q */
536 ee = cache->c_lrutail;
537 LRU_DELETE( cache, ee );
538 LRU_ADD( cache, ee );
543 * found at least one to delete - try to get back under
544 * the max cache size.
546 while ( cache->c_lrutail != NULL &&
547 BEI(cache->c_lrutail)->bei_refcnt == 0 &&
548 cache->c_cursize > cache->c_maxsize )
550 e = cache->c_lrutail;
552 /* delete from cache and lru q */
553 /* XXX do we need rc ? */
554 rc = bdb_cache_delete_entry_internal( cache, e );
555 bdb_cache_entry_private_destroy( e );
556 bdb_entry_return( e );
560 /* free cache mutex */
561 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
566 bdb_cache_find_entry_ndn2id(
576 /* this function is always called with normalized DN */
580 /* set cache mutex */
581 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
583 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
584 (AVL_CMP) entry_dn_cmp )) != NULL )
590 * ep now points to an unlocked entry
591 * we do not need to lock the entry if we only
592 * check the state, refcnt, LRU, and id.
595 assert( ep->e_private );
599 state = BEI(ep)->bei_state;
602 * entry is deleted or not fully created yet
604 if ( state != CACHE_ENTRY_READY ) {
605 assert(state != CACHE_ENTRY_UNDEFINED);
607 /* free cache mutex */
608 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
611 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
612 "bdb_cache_find_entry_dn2id: (%s) %ld not ready: %d\n",
613 ndn->bv_val, id, state ));
615 Debug(LDAP_DEBUG_TRACE,
616 "====> bdb_cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
617 ndn->bv_val, id, state);
621 ldap_pvt_thread_yield();
626 LRU_DELETE( cache, ep );
627 LRU_ADD( cache, ep );
629 /* free cache mutex */
630 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
633 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
634 "bdb_cache_find_entry_dn2id: (%s): %ld %d tries\n",
635 ndn->bv_val, id, count ));
637 Debug(LDAP_DEBUG_TRACE,
638 "====> bdb_cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
639 ndn->bv_val, id, count);
644 /* free cache mutex */
645 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
654 * cache_find_entry_id - find an entry in the cache, given id
658 bdb_cache_find_entry_id(
671 /* set cache mutex */
672 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
674 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
675 (AVL_CMP) entry_id_cmp )) != NULL )
682 assert( ep->e_private );
685 state = BEI(ep)->bei_state;
688 * entry is deleted or not fully created yet
690 if ( state != CACHE_ENTRY_READY ) {
692 assert(state != CACHE_ENTRY_UNDEFINED);
694 /* free cache mutex */
695 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
698 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
699 "bdb_cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
703 Debug(LDAP_DEBUG_TRACE,
704 "====> bdb_cache_find_entry_id( %ld ): %ld (not ready) %d\n",
709 ldap_pvt_thread_yield();
713 /* acquire reader lock */
714 if ( bdb_cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
715 /* could not acquire entry lock...
716 * owner cannot free as we have the cache locked.
717 * so, unlock the cache, yield, and try again.
720 /* free cache mutex */
721 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
724 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
725 "bdb_cache_find_entry_id: %ld -> %ld (busy) %d.\n",
728 Debug(LDAP_DEBUG_TRACE,
729 "====> bdb_cache_find_entry_id( %ld ): %ld (busy) %d\n",
734 ldap_pvt_thread_yield();
739 LRU_DELETE( cache, ep );
740 LRU_ADD( cache, ep );
742 BEI(ep)->bei_refcnt++;
744 /* free cache mutex */
745 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
748 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
749 "bdb_cache_find_entry_id: %ld -> %s found %d tries.\n",
750 ep_id, ep->e_dn, count ));
752 Debug(LDAP_DEBUG_TRACE,
753 "====> bdb_cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
754 ep_id, ep->e_dn, count);
761 /* free cache mutex */
762 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
768 * cache_delete_entry - delete the entry e from the cache. the caller
769 * should have obtained e (increasing its ref count) via a call to one
770 * of the cache_find_* routines. the caller should *not* call the
771 * cache_return_entry() routine prior to calling cache_delete_entry().
772 * it performs this function.
774 * returns: 0 e was deleted ok
775 * 1 e was not in the cache
776 * -1 something bad happened
779 bdb_cache_delete_entry(
786 /* set cache mutex */
787 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
789 assert( e->e_private );
792 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
793 "bdb_cache_delete_entry: delete %ld.\n", e->e_id ));
795 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete_entry( %ld )\n",
800 rc = bdb_cache_delete_entry_internal( cache, e );
802 /* free cache mutex */
803 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
808 bdb_cache_delete_entry_internal(
813 int rc = 0; /* return code */
816 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
823 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
834 LRU_DELETE( cache, e );
838 * flag entry to be freed later by a call to cache_return_entry()
840 BEI(e)->bei_state = CACHE_ENTRY_DELETED;
846 bdb_cache_release_all( Cache *cache )
851 /* set cache mutex */
852 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
855 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
856 "bdb_cache_release_all: enter\n" ));
858 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
862 while ( (e = cache->c_lrutail) != NULL && BEI(e)->bei_refcnt == 0 ) {
863 #ifdef LDAP_RDWR_DEBUG
864 assert(!ldap_pvt_thread_rdwr_active(&BEI(e)->bei_rdwr));
867 /* delete from cache and lru q */
868 /* XXX do we need rc ? */
869 rc = bdb_cache_delete_entry_internal( cache, e );
870 bdb_cache_entry_private_destroy( e );
871 bdb_entry_return( e );
874 if ( cache->c_cursize ) {
876 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
877 "bdb_cache_release_all: Entry cache could not be emptied.\n" ));
879 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
884 /* free cache mutex */
885 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
891 bdb_lru_print( Cache *cache )
895 fprintf( stderr, "LRU queue (head to tail):\n" );
896 for ( e = cache->c_lruhead; e != NULL; e = BEI(e)->bei_lrunext ) {
897 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
898 e->e_dn, e->e_id, BEI(e)->bei_refcnt );
900 fprintf( stderr, "LRU queue (tail to head):\n" );
901 for ( e = cache->c_lrutail; e != NULL; e = BEI(e)->bei_lruprev ) {
902 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
903 e->e_dn, e->e_id, BEI(e)->bei_refcnt );