1 /* cache.c - routines to maintain an in-core cache of entries */
4 * Copyright 1998-2000 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
35 int lei_refcnt; /* # threads ref'ing this entry */
36 Entry *lei_lrunext; /* for cache lru list */
40 #define LEI(e) ((EntryInfo *) ((e)->e_private))
42 static int cache_delete_entry_internal(Cache *cache, Entry *e);
44 static void lru_print(Cache *cache);
48 cache_entry_rdwr_lock(Entry *e, int rw)
51 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
52 "cache_entry_rdwr_lock: %s lock on ID %ld\n",
53 rw ? "w" : "r", e->e_id ));
55 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
56 rw ? "w" : "r", e->e_id, 0);
61 return ldap_pvt_thread_rdwr_wlock(&LEI(e)->lei_rdwr);
63 return ldap_pvt_thread_rdwr_rlock(&LEI(e)->lei_rdwr);
67 cache_entry_rdwr_trylock(Entry *e, int rw)
70 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
71 "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(&LEI(e)->lei_rdwr);
82 return ldap_pvt_thread_rdwr_rtrylock(&LEI(e)->lei_rdwr);
86 cache_entry_rdwr_unlock(Entry *e, int rw)
89 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
90 "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(&LEI(e)->lei_rdwr);
101 return ldap_pvt_thread_rdwr_runlock(&LEI(e)->lei_rdwr);
105 cache_entry_rdwr_init(Entry *e)
107 return ldap_pvt_thread_rdwr_init( &LEI(e)->lei_rdwr );
111 cache_entry_rdwr_destroy(Entry *e)
113 return ldap_pvt_thread_rdwr_destroy( &LEI(e)->lei_rdwr );
117 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 ldbm_entry_info));
128 if( cache_entry_rdwr_init( e ) != 0 ) {
138 cache_entry_private_destroy( Entry*e )
140 assert( e->e_private );
142 cache_entry_rdwr_destroy( e );
144 free( e->e_private );
150 cache_return_entry_rw( Cache *cache, Entry *e, int rw )
155 /* set cache mutex */
156 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
158 assert( e->e_private );
160 cache_entry_rdwr_unlock(e, rw);
163 refcnt = --LEI(e)->lei_refcnt;
165 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
166 LEI(e)->lei_state = CACHE_ENTRY_READY;
168 /* free cache mutex */
169 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
172 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
173 "cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
174 id, rw ? "w" : "r", refcnt ));
176 Debug( LDAP_DEBUG_TRACE,
177 "====> cache_return_entry_%s( %ld ): created (%d)\n",
178 rw ? "w" : "r", id, refcnt );
182 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
184 /* free cache mutex */
185 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
188 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
189 "cache_return_entry_rw: %ld, delete pending (%d).\n",
192 Debug( LDAP_DEBUG_TRACE,
193 "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
194 rw ? "w" : "r", id, refcnt );
199 cache_entry_private_destroy( e );
202 /* free cache mutex */
203 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
206 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
207 "cache_return_entry_rw: (%ld): deleted (%d)\n",
210 Debug( LDAP_DEBUG_TRACE,
211 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
212 rw ? "w" : "r", id, refcnt );
218 /* free cache mutex */
219 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
222 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
223 "cache_return_entry_rw: ID %ld:%s returned (%d)\n",
224 id, rw ? "w": "r", refcnt ));
226 Debug( LDAP_DEBUG_TRACE,
227 "====> cache_return_entry_%s( %ld ): returned (%d)\n",
228 rw ? "w" : "r", id, refcnt);
234 #define LRU_DELETE( cache, e ) do { \
235 if ( LEI(e)->lei_lruprev != NULL ) { \
236 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
238 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
240 if ( LEI(e)->lei_lrunext != NULL ) { \
241 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
243 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
247 #define LRU_ADD( cache, e ) do { \
248 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
249 if ( LEI(e)->lei_lrunext != NULL ) { \
250 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
252 (cache)->c_lruhead = (e); \
253 LEI(e)->lei_lruprev = NULL; \
254 if ( (cache)->c_lrutail == NULL ) { \
255 (cache)->c_lrutail = (e); \
260 * cache_add_entry_rw - create and lock an entry in the cache
261 * returns: 0 entry has been created and locked
262 * 1 entry already existed
263 * -1 something bad happened
276 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
277 "cache_add_entry_rw: add (%s):%s to cache\n",
278 e->e_dn, rw ? "w" : "r" ));
280 /* set cache mutex */
281 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
283 assert( e->e_private == NULL );
285 if( cache_entry_private_init(e) != 0 ) {
286 /* free cache mutex */
287 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
290 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
291 "cache_add_entry_rw: add (%s):%ld private init failed!\n",
294 Debug( LDAP_DEBUG_ANY,
295 "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
296 e->e_id, e->e_dn, 0 );
303 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
304 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
306 /* free cache mutex */
307 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
310 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
311 "cache_add_entry: (%s):%ld already in cache.\n",
314 Debug( LDAP_DEBUG_TRACE,
315 "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
316 e->e_id, e->e_dn, 0 );
320 cache_entry_private_destroy(e);
326 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
327 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
330 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
331 "cache_add_entry: (%s):%ls already in cache.\n",
334 Debug( LDAP_DEBUG_ANY,
335 "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
336 e->e_id, e->e_dn, 0 );
341 /* delete from dn tree inserted above */
342 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
343 (AVL_CMP) entry_dn_cmp ) == NULL )
346 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
347 "cache_add_entry: can't delete (%s) from cache.\n",
350 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
356 cache_entry_private_destroy(e);
358 /* free cache mutex */
359 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
363 cache_entry_rdwr_lock( e, rw );
365 /* put the entry into 'CREATING' state */
366 /* will be marked after when entry is returned */
367 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
368 LEI(e)->lei_refcnt = 1;
372 if ( ++cache->c_cursize > cache->c_maxsize ) {
374 * find the lru entry not currently in use and delete it.
375 * in case a lot of entries are in use, only look at the
376 * first 10 on the tail of the list.
379 while ( cache->c_lrutail != NULL &&
380 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
383 /* move this in-use entry to the front of the q */
384 ee = cache->c_lrutail;
385 LRU_DELETE( cache, ee );
386 LRU_ADD( cache, ee );
391 * found at least one to delete - try to get back under
392 * the max cache size.
394 while ( cache->c_lrutail != NULL &&
395 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
396 cache->c_cursize > cache->c_maxsize )
398 e = cache->c_lrutail;
400 /* delete from cache and lru q */
401 /* XXX do we need rc ? */
402 rc = cache_delete_entry_internal( cache, e );
403 cache_entry_private_destroy( e );
408 /* free cache mutex */
409 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
414 * cache_update_entry - update a LOCKED entry which has been deleted.
415 * returns: 0 entry has been created and locked
416 * 1 entry already existed
417 * -1 something bad happened
428 /* set cache mutex */
429 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
431 assert( e->e_private );
433 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
434 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
437 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
438 "cache_update_entry: (%s):%ld already in dn cache\n",
441 Debug( LDAP_DEBUG_TRACE,
442 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
443 e->e_id, e->e_dn, 0 );
447 /* free cache mutex */
448 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
453 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
454 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
457 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
458 "cache_update_entry: (%s)%ld already in id cache\n",
461 Debug( LDAP_DEBUG_ANY,
462 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
463 e->e_id, e->e_dn, 0 );
467 /* delete from dn tree inserted above */
468 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
469 (AVL_CMP) entry_dn_cmp ) == NULL )
472 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
473 "cache_update_entry: can't delete (%s)%ld from dn cache.\n",
476 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
482 /* free cache mutex */
483 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
488 /* put the entry into 'CREATING' state */
489 /* will be marked after when entry is returned */
490 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
494 if ( ++cache->c_cursize > cache->c_maxsize ) {
496 * find the lru entry not currently in use and delete it.
497 * in case a lot of entries are in use, only look at the
498 * first 10 on the tail of the list.
501 while ( cache->c_lrutail != NULL &&
502 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
505 /* move this in-use entry to the front of the q */
506 ee = cache->c_lrutail;
507 LRU_DELETE( cache, ee );
508 LRU_ADD( cache, ee );
513 * found at least one to delete - try to get back under
514 * the max cache size.
516 while ( cache->c_lrutail != NULL &&
517 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
518 cache->c_cursize > cache->c_maxsize )
520 e = cache->c_lrutail;
522 /* delete from cache and lru q */
523 /* XXX do we need rc ? */
524 rc = cache_delete_entry_internal( cache, e );
525 cache_entry_private_destroy( e );
530 /* free cache mutex */
531 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
536 * cache_find_entry_dn2id - find an entry in the cache, given dn
540 cache_find_entry_dn2id(
549 ndn = ch_strdup( dn );
550 (void) dn_normalize( ndn );
552 id = cache_find_entry_ndn2id( be, cache, ndn );
560 cache_find_entry_ndn2id(
570 /* this function is always called with normalized DN */
571 e.e_ndn = (char *)ndn;
574 /* set cache mutex */
575 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
577 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
578 (AVL_CMP) entry_dn_cmp )) != NULL )
584 * ep now points to an unlocked entry
585 * we do not need to lock the entry if we only
586 * check the state, refcnt, LRU, and id.
589 assert( ep->e_private );
593 state = LEI(ep)->lei_state;
596 * entry is deleted or not fully created yet
598 if ( state != CACHE_ENTRY_READY ) {
599 assert(state != CACHE_ENTRY_UNDEFINED);
601 /* free cache mutex */
602 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
605 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
606 "cache_find_entry_dn2id: (%s)%ld not ready: %d\n",
609 Debug(LDAP_DEBUG_TRACE,
610 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
615 ldap_pvt_thread_yield();
620 LRU_DELETE( cache, ep );
621 LRU_ADD( cache, ep );
623 /* free cache mutex */
624 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
627 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
628 "cache_find_entry_dn2id: (%s)%ld %d tries\n",
631 Debug(LDAP_DEBUG_TRACE,
632 "====> cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
638 /* free cache mutex */
639 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
648 * cache_find_entry_id - find an entry in the cache, given id
665 /* set cache mutex */
666 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
668 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
669 (AVL_CMP) entry_id_cmp )) != NULL )
676 assert( ep->e_private );
679 state = LEI(ep)->lei_state;
682 * entry is deleted or not fully created yet
684 if ( state != CACHE_ENTRY_READY ) {
686 assert(state != CACHE_ENTRY_UNDEFINED);
688 /* free cache mutex */
689 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
692 LDAP_LOG(( "caceh", LDAP_LEVEL_INFO,
693 "cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
697 Debug(LDAP_DEBUG_TRACE,
698 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
703 ldap_pvt_thread_yield();
707 /* acquire reader lock */
708 if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
709 /* could not acquire entry lock...
710 * owner cannot free as we have the cache locked.
711 * so, unlock the cache, yield, and try again.
714 /* free cache mutex */
715 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
718 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
719 "cache_find_entry_id: %ld -> %ld (busy) %d.\n",
722 Debug(LDAP_DEBUG_TRACE,
723 "====> cache_find_entry_id( %ld ): %ld (busy) %d\n",
728 ldap_pvt_thread_yield();
733 LRU_DELETE( cache, ep );
734 LRU_ADD( cache, ep );
736 LEI(ep)->lei_refcnt++;
738 /* free cache mutex */
739 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
742 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
743 "cache_find_entry_id: %ld -> %s found %d tries.\n",
744 ep_id, ep->e_dn, count ));
746 Debug(LDAP_DEBUG_TRACE,
747 "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
748 ep_id, ep->e_dn, count);
755 /* free cache mutex */
756 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
762 * cache_delete_entry - delete the entry e from the cache. the caller
763 * should have obtained e (increasing its ref count) via a call to one
764 * of the cache_find_* routines. the caller should *not* call the
765 * cache_return_entry() routine prior to calling cache_delete_entry().
766 * it performs this function.
768 * returns: 0 e was deleted ok
769 * 1 e was not in the cache
770 * -1 something bad happened
780 /* set cache mutex */
781 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
783 assert( e->e_private );
786 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
787 "cache_delete_entry: delete %ld.\n", e->e_id ));
789 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
794 rc = cache_delete_entry_internal( cache, e );
796 /* free cache mutex */
797 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
802 cache_delete_entry_internal(
807 int rc = 0; /* return code */
810 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
817 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
828 LRU_DELETE( cache, e );
832 * flag entry to be freed later by a call to cache_return_entry()
834 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
840 cache_release_all( Cache *cache )
845 /* set cache mutex */
846 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
849 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
850 "cache_release_all: enter\n" ));
852 Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
856 while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
857 #ifdef LDAP_RDWR_DEBUG
858 assert(!ldap_pvt_thread_rdwr_active(&LEI(e)->lei_rdwr));
861 /* delete from cache and lru q */
862 /* XXX do we need rc ? */
863 rc = cache_delete_entry_internal( cache, e );
864 cache_entry_private_destroy( e );
868 if ( cache->c_cursize ) {
870 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
871 "cache_release_all: Entry cache could not be emptied.\n" ));
873 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
878 /* free cache mutex */
879 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
885 lru_print( Cache *cache )
889 fprintf( stderr, "LRU queue (head to tail):\n" );
890 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
891 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
892 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
894 fprintf( stderr, "LRU queue (tail to head):\n" );
895 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
896 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
897 e->e_dn, e->e_id, LEI(e)->lei_refcnt );