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>
18 #include "back-ldbm.h"
20 /* LDBM backend specific entry info -- visible only to the cache */
21 typedef struct ldbm_entry_info {
22 ldap_pvt_thread_rdwr_t lei_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 lei_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 lei_refcnt; /* # threads ref'ing this entry */
37 Entry *lei_lrunext; /* for cache lru list */
41 #define LEI(e) ((EntryInfo *) ((e)->e_private))
43 static int cache_delete_entry_internal(Cache *cache, Entry *e);
45 static void lru_print(Cache *cache);
49 cache_entry_rdwr_lock(Entry *e, int rw)
52 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
53 "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);
62 return ldap_pvt_thread_rdwr_wlock(&LEI(e)->lei_rdwr);
64 return ldap_pvt_thread_rdwr_rlock(&LEI(e)->lei_rdwr);
68 cache_entry_rdwr_trylock(Entry *e, int rw)
71 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
72 "cache_entry_rdwr_trylock: try %s lock on ID: %ld.\n",
73 rw ? "w" : "r", e->e_id ));
75 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%strylock: ID: %ld\n",
76 rw ? "w" : "r", e->e_id, 0);
81 return ldap_pvt_thread_rdwr_wtrylock(&LEI(e)->lei_rdwr);
83 return ldap_pvt_thread_rdwr_rtrylock(&LEI(e)->lei_rdwr);
87 cache_entry_rdwr_unlock(Entry *e, int rw)
90 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
91 "cache_entry_rdwr_unlock: remove %s lock on ID %ld.\n",
92 rw ? "w" : "r", e->e_id ));
94 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
95 rw ? "w" : "r", e->e_id, 0);
100 return ldap_pvt_thread_rdwr_wunlock(&LEI(e)->lei_rdwr);
102 return ldap_pvt_thread_rdwr_runlock(&LEI(e)->lei_rdwr);
106 cache_entry_rdwr_init(Entry *e)
108 return ldap_pvt_thread_rdwr_init( &LEI(e)->lei_rdwr );
112 cache_entry_rdwr_destroy(Entry *e)
114 return ldap_pvt_thread_rdwr_destroy( &LEI(e)->lei_rdwr );
118 cache_entry_private_init( Entry*e )
120 assert( e->e_private == NULL );
122 if( e->e_private != NULL ) {
123 /* this should never happen */
127 e->e_private = ch_calloc(1, sizeof(struct ldbm_entry_info));
129 if( cache_entry_rdwr_init( e ) != 0 ) {
139 * marks an entry in CREATING state as committed, so it is really returned
140 * to the cache. Otherwise an entry in CREATING state is removed.
141 * Makes e_private be destroyed at the following cache_return_entry_w,
142 * but lets the entry untouched (owned by someone else)
145 cache_entry_commit( Entry *e )
148 assert( e->e_private );
149 assert( LEI(e)->lei_state == CACHE_ENTRY_CREATING );
150 /* assert( LEI(e)->lei_refcnt == 1 ); */
152 LEI(e)->lei_state = CACHE_ENTRY_COMMITTED;
156 cache_entry_private_destroy( Entry*e )
158 assert( e->e_private );
160 cache_entry_rdwr_destroy( e );
162 free( e->e_private );
168 cache_return_entry_rw( Cache *cache, Entry *e, int rw )
171 int refcnt, freeit = 1;
173 /* set cache mutex */
174 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
176 assert( e->e_private );
178 cache_entry_rdwr_unlock(e, rw);
181 refcnt = --LEI(e)->lei_refcnt;
184 * if the entry is returned when in CREATING state, it is deleted
185 * but not freed because it may belong to someone else (do_add,
188 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
189 cache_delete_entry_internal( cache, e );
191 /* now the entry is in DELETED state */
194 if ( LEI(e)->lei_state == CACHE_ENTRY_COMMITTED ) {
195 LEI(e)->lei_state = CACHE_ENTRY_READY;
197 /* free cache mutex */
198 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
201 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
202 "cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
203 id, rw ? "w" : "r", refcnt ));
205 Debug( LDAP_DEBUG_TRACE,
206 "====> cache_return_entry_%s( %ld ): created (%d)\n",
207 rw ? "w" : "r", id, refcnt );
211 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
213 /* free cache mutex */
214 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
217 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
218 "cache_return_entry_rw: %ld, delete pending (%d).\n",
221 Debug( LDAP_DEBUG_TRACE,
222 "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
223 rw ? "w" : "r", id, refcnt );
228 cache_entry_private_destroy( e );
233 /* free cache mutex */
234 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
237 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
238 "cache_return_entry_rw: (%ld): deleted (%d)\n",
241 Debug( LDAP_DEBUG_TRACE,
242 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
243 rw ? "w" : "r", id, refcnt );
249 /* free cache mutex */
250 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
253 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
254 "cache_return_entry_rw: ID %ld:%s returned (%d)\n",
255 id, rw ? "w": "r", refcnt ));
257 Debug( LDAP_DEBUG_TRACE,
258 "====> cache_return_entry_%s( %ld ): returned (%d)\n",
259 rw ? "w" : "r", id, refcnt);
265 #define LRU_DELETE( cache, e ) do { \
266 if ( LEI(e)->lei_lruprev != NULL ) { \
267 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
269 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
271 if ( LEI(e)->lei_lrunext != NULL ) { \
272 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
274 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
278 #define LRU_ADD( cache, e ) do { \
279 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
280 if ( LEI(e)->lei_lrunext != NULL ) { \
281 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
283 (cache)->c_lruhead = (e); \
284 LEI(e)->lei_lruprev = NULL; \
285 if ( (cache)->c_lrutail == NULL ) { \
286 (cache)->c_lrutail = (e); \
291 * cache_add_entry_rw - create and lock an entry in the cache
292 * returns: 0 entry has been created and locked
293 * 1 entry already existed
294 * -1 something bad happened
307 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
308 "cache_add_entry_rw: add (%s):%s to cache\n",
309 e->e_dn, rw ? "w" : "r" ));
311 /* set cache mutex */
312 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
314 assert( e->e_private == NULL );
316 if( cache_entry_private_init(e) != 0 ) {
317 /* free cache mutex */
318 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
321 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
322 "cache_add_entry_rw: add (%s):%ld private init failed!\n",
325 Debug( LDAP_DEBUG_ANY,
326 "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
327 e->e_id, e->e_dn, 0 );
334 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
335 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
337 /* free cache mutex */
338 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
341 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
342 "cache_add_entry: (%s):%ld already in cache.\n",
345 Debug( LDAP_DEBUG_TRACE,
346 "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
347 e->e_id, e->e_dn, 0 );
351 cache_entry_private_destroy(e);
357 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
358 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
361 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
362 "cache_add_entry: (%s):%ls already in cache.\n",
365 Debug( LDAP_DEBUG_ANY,
366 "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
367 e->e_id, e->e_dn, 0 );
372 /* delete from dn tree inserted above */
373 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
374 (AVL_CMP) entry_dn_cmp ) == NULL )
377 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
378 "cache_add_entry: can't delete (%s) from cache.\n",
381 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
387 cache_entry_private_destroy(e);
389 /* free cache mutex */
390 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
394 cache_entry_rdwr_lock( e, rw );
396 /* put the entry into 'CREATING' state */
397 /* will be marked after when entry is returned */
398 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
399 LEI(e)->lei_refcnt = 1;
403 if ( ++cache->c_cursize > cache->c_maxsize ) {
405 * find the lru entry not currently in use and delete it.
406 * in case a lot of entries are in use, only look at the
407 * first 10 on the tail of the list.
410 while ( cache->c_lrutail != NULL &&
411 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
414 /* move this in-use entry to the front of the q */
415 ee = cache->c_lrutail;
416 LRU_DELETE( cache, ee );
417 LRU_ADD( cache, ee );
422 * found at least one to delete - try to get back under
423 * the max cache size.
425 while ( cache->c_lrutail != NULL &&
426 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
427 cache->c_cursize > cache->c_maxsize )
429 e = cache->c_lrutail;
431 /* delete from cache and lru q */
432 /* XXX do we need rc ? */
433 rc = cache_delete_entry_internal( cache, e );
434 cache_entry_private_destroy( e );
439 /* free cache mutex */
440 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
445 * cache_update_entry - update a LOCKED entry which has been deleted.
446 * returns: 0 entry has been created and locked
447 * 1 entry already existed
448 * -1 something bad happened
459 /* set cache mutex */
460 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
462 assert( e->e_private );
464 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
465 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
468 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
469 "cache_update_entry: (%s):%ld already in dn cache\n",
472 Debug( LDAP_DEBUG_TRACE,
473 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
474 e->e_id, e->e_dn, 0 );
478 /* free cache mutex */
479 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
484 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
485 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
488 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
489 "cache_update_entry: (%s)%ld already in id cache\n",
492 Debug( LDAP_DEBUG_ANY,
493 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
494 e->e_id, e->e_dn, 0 );
498 /* delete from dn tree inserted above */
499 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
500 (AVL_CMP) entry_dn_cmp ) == NULL )
503 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
504 "cache_update_entry: can't delete (%s)%ld from dn cache.\n",
507 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
513 /* free cache mutex */
514 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
519 /* put the entry into 'CREATING' state */
520 /* will be marked after when entry is returned */
521 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
525 if ( ++cache->c_cursize > cache->c_maxsize ) {
527 * find the lru entry not currently in use and delete it.
528 * in case a lot of entries are in use, only look at the
529 * first 10 on the tail of the list.
532 while ( cache->c_lrutail != NULL &&
533 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
536 /* move this in-use entry to the front of the q */
537 ee = cache->c_lrutail;
538 LRU_DELETE( cache, ee );
539 LRU_ADD( cache, ee );
544 * found at least one to delete - try to get back under
545 * the max cache size.
547 while ( cache->c_lrutail != NULL &&
548 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
549 cache->c_cursize > cache->c_maxsize )
551 e = cache->c_lrutail;
553 /* delete from cache and lru q */
554 /* XXX do we need rc ? */
555 rc = cache_delete_entry_internal( cache, e );
556 cache_entry_private_destroy( e );
561 /* free cache mutex */
562 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
567 cache_find_entry_ndn2id(
577 /* this function is always called with normalized DN */
581 /* set cache mutex */
582 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
584 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
585 (AVL_CMP) entry_dn_cmp )) != NULL )
591 * ep now points to an unlocked entry
592 * we do not need to lock the entry if we only
593 * check the state, refcnt, LRU, and id.
596 assert( ep->e_private );
600 state = LEI(ep)->lei_state;
603 * entry is deleted or not fully created yet
605 if ( state != CACHE_ENTRY_READY ) {
606 assert(state != CACHE_ENTRY_UNDEFINED);
608 /* free cache mutex */
609 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
612 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
613 "cache_find_entry_dn2id: (%s) %ld not ready: %d\n",
614 ndn->bv_val, id, state ));
616 Debug(LDAP_DEBUG_TRACE,
617 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
618 ndn->bv_val, id, state);
622 ldap_pvt_thread_yield();
627 LRU_DELETE( cache, ep );
628 LRU_ADD( cache, ep );
630 /* free cache mutex */
631 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
634 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
635 "cache_find_entry_dn2id: (%s): %ld %d tries\n",
636 ndn->bv_val, id, count ));
638 Debug(LDAP_DEBUG_TRACE,
639 "====> cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
640 ndn->bv_val, id, count);
645 /* free cache mutex */
646 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
655 * cache_find_entry_id - find an entry in the cache, given id
672 /* set cache mutex */
673 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
675 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
676 (AVL_CMP) entry_id_cmp )) != NULL )
683 assert( ep->e_private );
686 state = LEI(ep)->lei_state;
689 * entry is deleted or not fully created yet
691 if ( state != CACHE_ENTRY_READY ) {
693 assert(state != CACHE_ENTRY_UNDEFINED);
695 /* free cache mutex */
696 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
699 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
700 "cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
704 Debug(LDAP_DEBUG_TRACE,
705 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
710 ldap_pvt_thread_yield();
714 /* acquire reader lock */
715 if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
716 /* could not acquire entry lock...
717 * owner cannot free as we have the cache locked.
718 * so, unlock the cache, yield, and try again.
721 /* free cache mutex */
722 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
725 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
726 "cache_find_entry_id: %ld -> %ld (busy) %d.\n",
729 Debug(LDAP_DEBUG_TRACE,
730 "====> cache_find_entry_id( %ld ): %ld (busy) %d\n",
735 ldap_pvt_thread_yield();
740 LRU_DELETE( cache, ep );
741 LRU_ADD( cache, ep );
743 LEI(ep)->lei_refcnt++;
745 /* free cache mutex */
746 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
749 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
750 "cache_find_entry_id: %ld -> %s found %d tries.\n",
751 ep_id, ep->e_dn, count ));
753 Debug(LDAP_DEBUG_TRACE,
754 "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
755 ep_id, ep->e_dn, count);
762 /* free cache mutex */
763 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
769 * cache_delete_entry - delete the entry e from the cache. the caller
770 * should have obtained e (increasing its ref count) via a call to one
771 * of the cache_find_* routines. the caller should *not* call the
772 * cache_return_entry() routine prior to calling cache_delete_entry().
773 * it performs this function.
775 * returns: 0 e was deleted ok
776 * 1 e was not in the cache
777 * -1 something bad happened
787 /* set cache mutex */
788 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
790 assert( e->e_private );
793 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
794 "cache_delete_entry: delete %ld.\n", e->e_id ));
796 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
801 rc = cache_delete_entry_internal( cache, e );
803 /* free cache mutex */
804 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
809 cache_delete_entry_internal(
814 int rc = 0; /* return code */
817 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
824 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
835 LRU_DELETE( cache, e );
839 * flag entry to be freed later by a call to cache_return_entry()
841 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
847 cache_release_all( Cache *cache )
852 /* set cache mutex */
853 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
856 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
857 "cache_release_all: enter\n" ));
859 Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
863 while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
864 #ifdef LDAP_RDWR_DEBUG
865 assert(!ldap_pvt_thread_rdwr_active(&LEI(e)->lei_rdwr));
868 /* delete from cache and lru q */
869 /* XXX do we need rc ? */
870 rc = cache_delete_entry_internal( cache, e );
871 cache_entry_private_destroy( e );
875 if ( cache->c_cursize ) {
877 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
878 "cache_release_all: Entry cache could not be emptied.\n" ));
880 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
885 /* free cache mutex */
886 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
892 lru_print( Cache *cache )
896 fprintf( stderr, "LRU queue (head to tail):\n" );
897 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
898 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
899 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
901 fprintf( stderr, "LRU queue (tail to head):\n" );
902 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
903 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
904 e->e_dn, e->e_id, LEI(e)->lei_refcnt );