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(
550 e.e_dn = (char *) dn;
551 e.e_ndn = ch_strdup( dn );
552 (void) dn_normalize( e.e_ndn );
555 /* set cache mutex */
556 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
558 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
559 (AVL_CMP) entry_dn_cmp )) != NULL )
565 * ep now points to an unlocked entry
566 * we do not need to lock the entry if we only
567 * check the state, refcnt, LRU, and id.
570 assert( ep->e_private );
574 state = LEI(ep)->lei_state;
577 * entry is deleted or not fully created yet
579 if ( state != CACHE_ENTRY_READY ) {
580 assert(state != CACHE_ENTRY_UNDEFINED);
582 /* free cache mutex */
583 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
586 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
587 "cache_find_entry_dn2id: (%s)%ld not ready: %d\n",
590 Debug(LDAP_DEBUG_TRACE,
591 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
596 ldap_pvt_thread_yield();
601 LRU_DELETE( cache, ep );
602 LRU_ADD( cache, ep );
604 /* free cache mutex */
605 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
608 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
609 "cache_find_entry_dn2id: (%s)%ld %d tries\n",
612 Debug(LDAP_DEBUG_TRACE,
613 "====> cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
619 /* free cache mutex */
620 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
631 * cache_find_entry_id - find an entry in the cache, given id
648 /* set cache mutex */
649 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
651 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
652 (AVL_CMP) entry_id_cmp )) != NULL )
659 assert( ep->e_private );
662 state = LEI(ep)->lei_state;
665 * entry is deleted or not fully created yet
667 if ( state != CACHE_ENTRY_READY ) {
669 assert(state != CACHE_ENTRY_UNDEFINED);
671 /* free cache mutex */
672 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
675 LDAP_LOG(( "caceh", LDAP_LEVEL_INFO,
676 "cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
680 Debug(LDAP_DEBUG_TRACE,
681 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
686 ldap_pvt_thread_yield();
690 /* acquire reader lock */
691 if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
692 /* could not acquire entry lock...
693 * owner cannot free as we have the cache locked.
694 * so, unlock the cache, yield, and try again.
697 /* free cache mutex */
698 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
701 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
702 "cache_find_entry_id: %ld -> %ld (busy) %d.\n",
705 Debug(LDAP_DEBUG_TRACE,
706 "====> cache_find_entry_id( %ld ): %ld (busy) %d\n",
711 ldap_pvt_thread_yield();
716 LRU_DELETE( cache, ep );
717 LRU_ADD( cache, ep );
719 LEI(ep)->lei_refcnt++;
721 /* free cache mutex */
722 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
725 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
726 "cache_find_entry_id: %ld -> %s found %d tries.\n",
727 ep_id, ep->e_dn, count ));
729 Debug(LDAP_DEBUG_TRACE,
730 "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
731 ep_id, ep->e_dn, count);
738 /* free cache mutex */
739 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
745 * cache_delete_entry - delete the entry e from the cache. the caller
746 * should have obtained e (increasing its ref count) via a call to one
747 * of the cache_find_* routines. the caller should *not* call the
748 * cache_return_entry() routine prior to calling cache_delete_entry().
749 * it performs this function.
751 * returns: 0 e was deleted ok
752 * 1 e was not in the cache
753 * -1 something bad happened
763 /* set cache mutex */
764 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
766 assert( e->e_private );
769 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
770 "cache_delete_entry: delete %ld.\n", e->e_id ));
772 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
777 rc = cache_delete_entry_internal( cache, e );
779 /* free cache mutex */
780 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
785 cache_delete_entry_internal(
790 int rc = 0; /* return code */
793 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
800 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
811 LRU_DELETE( cache, e );
815 * flag entry to be freed later by a call to cache_return_entry()
817 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
823 cache_release_all( Cache *cache )
828 /* set cache mutex */
829 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
832 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
833 "cache_release_all: enter\n" ));
835 Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
839 while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
840 #ifdef LDAP_RDWR_DEBUG
841 assert(!ldap_pvt_thread_rdwr_active(&LEI(e)->lei_rdwr));
844 /* delete from cache and lru q */
845 /* XXX do we need rc ? */
846 rc = cache_delete_entry_internal( cache, e );
847 cache_entry_private_destroy( e );
851 if ( cache->c_cursize ) {
853 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
854 "cache_release_all: Entry cache could not be emptied.\n" ));
856 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
861 /* free cache mutex */
862 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
868 lru_print( Cache *cache )
872 fprintf( stderr, "LRU queue (head to tail):\n" );
873 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
874 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
875 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
877 fprintf( stderr, "LRU queue (tail to head):\n" );
878 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
879 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
880 e->e_dn, e->e_id, LEI(e)->lei_refcnt );