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
34 #define CACHE_ENTRY_DESTROY_PRIVATE 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 * assumes that the entry is write-locked;marks it i a manner that
140 * makes e_private be destroyed at the following cache_return_entry_w,
141 * but lets the entry untouched (owned by someone else)
144 cache_entry_private_destroy_mark( Entry *e )
147 assert( e->e_private );
149 LEI(e)->lei_state = CACHE_ENTRY_DESTROY_PRIVATE;
153 cache_entry_private_destroy( Entry*e )
155 assert( e->e_private );
157 cache_entry_rdwr_destroy( e );
159 free( e->e_private );
165 cache_return_entry_rw( Cache *cache, Entry *e, int rw )
170 /* set cache mutex */
171 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
173 assert( e->e_private );
175 cache_entry_rdwr_unlock(e, rw);
178 refcnt = --LEI(e)->lei_refcnt;
180 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
181 LEI(e)->lei_state = CACHE_ENTRY_READY;
183 /* free cache mutex */
184 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
187 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
188 "cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
189 id, rw ? "w" : "r", refcnt ));
191 Debug( LDAP_DEBUG_TRACE,
192 "====> cache_return_entry_%s( %ld ): created (%d)\n",
193 rw ? "w" : "r", id, refcnt );
197 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED
198 || LEI(e)->lei_state == CACHE_ENTRY_DESTROY_PRIVATE ) {
200 /* free cache mutex */
201 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
204 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
205 "cache_return_entry_rw: %ld, delete pending (%d).\n",
208 Debug( LDAP_DEBUG_TRACE,
209 "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
210 rw ? "w" : "r", id, refcnt );
215 int state = LEI(e)->lei_state;
217 cache_entry_private_destroy( e );
218 if ( state == CACHE_ENTRY_DELETED ) {
222 /* free cache mutex */
223 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
226 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
227 "cache_return_entry_rw: (%ld): deleted (%d)\n",
230 Debug( LDAP_DEBUG_TRACE,
231 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
232 rw ? "w" : "r", id, refcnt );
238 /* free cache mutex */
239 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
242 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
243 "cache_return_entry_rw: ID %ld:%s returned (%d)\n",
244 id, rw ? "w": "r", refcnt ));
246 Debug( LDAP_DEBUG_TRACE,
247 "====> cache_return_entry_%s( %ld ): returned (%d)\n",
248 rw ? "w" : "r", id, refcnt);
254 #define LRU_DELETE( cache, e ) do { \
255 if ( LEI(e)->lei_lruprev != NULL ) { \
256 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
258 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
260 if ( LEI(e)->lei_lrunext != NULL ) { \
261 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
263 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
267 #define LRU_ADD( cache, e ) do { \
268 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
269 if ( LEI(e)->lei_lrunext != NULL ) { \
270 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
272 (cache)->c_lruhead = (e); \
273 LEI(e)->lei_lruprev = NULL; \
274 if ( (cache)->c_lrutail == NULL ) { \
275 (cache)->c_lrutail = (e); \
280 * cache_add_entry_rw - create and lock an entry in the cache
281 * returns: 0 entry has been created and locked
282 * 1 entry already existed
283 * -1 something bad happened
296 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
297 "cache_add_entry_rw: add (%s):%s to cache\n",
298 e->e_dn, rw ? "w" : "r" ));
300 /* set cache mutex */
301 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
303 assert( e->e_private == NULL );
305 if( cache_entry_private_init(e) != 0 ) {
306 /* free cache mutex */
307 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
310 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
311 "cache_add_entry_rw: add (%s):%ld private init failed!\n",
314 Debug( LDAP_DEBUG_ANY,
315 "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
316 e->e_id, e->e_dn, 0 );
323 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
324 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
326 /* free cache mutex */
327 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
330 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
331 "cache_add_entry: (%s):%ld already in cache.\n",
334 Debug( LDAP_DEBUG_TRACE,
335 "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
336 e->e_id, e->e_dn, 0 );
340 cache_entry_private_destroy(e);
346 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
347 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
350 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
351 "cache_add_entry: (%s):%ls already in cache.\n",
354 Debug( LDAP_DEBUG_ANY,
355 "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
356 e->e_id, e->e_dn, 0 );
361 /* delete from dn tree inserted above */
362 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
363 (AVL_CMP) entry_dn_cmp ) == NULL )
366 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
367 "cache_add_entry: can't delete (%s) from cache.\n",
370 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
376 cache_entry_private_destroy(e);
378 /* free cache mutex */
379 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
383 cache_entry_rdwr_lock( e, rw );
385 /* put the entry into 'CREATING' state */
386 /* will be marked after when entry is returned */
387 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
388 LEI(e)->lei_refcnt = 1;
392 if ( ++cache->c_cursize > cache->c_maxsize ) {
394 * find the lru entry not currently in use and delete it.
395 * in case a lot of entries are in use, only look at the
396 * first 10 on the tail of the list.
399 while ( cache->c_lrutail != NULL &&
400 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
403 /* move this in-use entry to the front of the q */
404 ee = cache->c_lrutail;
405 LRU_DELETE( cache, ee );
406 LRU_ADD( cache, ee );
411 * found at least one to delete - try to get back under
412 * the max cache size.
414 while ( cache->c_lrutail != NULL &&
415 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
416 cache->c_cursize > cache->c_maxsize )
418 e = cache->c_lrutail;
420 /* delete from cache and lru q */
421 /* XXX do we need rc ? */
422 rc = cache_delete_entry_internal( cache, e );
423 cache_entry_private_destroy( e );
428 /* free cache mutex */
429 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
434 * cache_update_entry - update a LOCKED entry which has been deleted.
435 * returns: 0 entry has been created and locked
436 * 1 entry already existed
437 * -1 something bad happened
448 /* set cache mutex */
449 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
451 assert( e->e_private );
453 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
454 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
457 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
458 "cache_update_entry: (%s):%ld already in dn cache\n",
461 Debug( LDAP_DEBUG_TRACE,
462 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
463 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 "cache_update_entry: (%s)%ld already in id cache\n",
481 Debug( LDAP_DEBUG_ANY,
482 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
483 e->e_id, e->e_dn, 0 );
487 /* delete from dn tree inserted above */
488 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
489 (AVL_CMP) entry_dn_cmp ) == NULL )
492 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
493 "cache_update_entry: can't delete (%s)%ld from dn cache.\n",
496 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
502 /* free cache mutex */
503 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
508 /* put the entry into 'CREATING' state */
509 /* will be marked after when entry is returned */
510 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
514 if ( ++cache->c_cursize > cache->c_maxsize ) {
516 * find the lru entry not currently in use and delete it.
517 * in case a lot of entries are in use, only look at the
518 * first 10 on the tail of the list.
521 while ( cache->c_lrutail != NULL &&
522 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
525 /* move this in-use entry to the front of the q */
526 ee = cache->c_lrutail;
527 LRU_DELETE( cache, ee );
528 LRU_ADD( cache, ee );
533 * found at least one to delete - try to get back under
534 * the max cache size.
536 while ( cache->c_lrutail != NULL &&
537 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
538 cache->c_cursize > cache->c_maxsize )
540 e = cache->c_lrutail;
542 /* delete from cache and lru q */
543 /* XXX do we need rc ? */
544 rc = cache_delete_entry_internal( cache, e );
545 cache_entry_private_destroy( e );
550 /* free cache mutex */
551 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
556 * cache_find_entry_dn2id - find an entry in the cache, given dn
560 cache_find_entry_dn2id(
569 ndn = ch_strdup( dn );
570 (void) dn_normalize( ndn );
572 id = cache_find_entry_ndn2id( be, cache, ndn );
580 cache_find_entry_ndn2id(
590 /* this function is always called with normalized DN */
591 e.e_ndn = (char *)ndn;
594 /* set cache mutex */
595 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
597 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
598 (AVL_CMP) entry_dn_cmp )) != NULL )
604 * ep now points to an unlocked entry
605 * we do not need to lock the entry if we only
606 * check the state, refcnt, LRU, and id.
609 assert( ep->e_private );
613 state = LEI(ep)->lei_state;
616 * entry is deleted or not fully created yet
618 if ( state != CACHE_ENTRY_READY ) {
619 assert(state != CACHE_ENTRY_UNDEFINED);
621 /* free cache mutex */
622 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
625 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
626 "cache_find_entry_dn2id: (%s)%ld not ready: %d\n",
629 Debug(LDAP_DEBUG_TRACE,
630 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
635 ldap_pvt_thread_yield();
640 LRU_DELETE( cache, ep );
641 LRU_ADD( cache, ep );
643 /* free cache mutex */
644 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
647 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
648 "cache_find_entry_dn2id: (%s)%ld %d tries\n",
651 Debug(LDAP_DEBUG_TRACE,
652 "====> cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
658 /* free cache mutex */
659 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
668 * cache_find_entry_id - find an entry in the cache, given id
685 /* set cache mutex */
686 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
688 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
689 (AVL_CMP) entry_id_cmp )) != NULL )
696 assert( ep->e_private );
699 state = LEI(ep)->lei_state;
702 * entry is deleted or not fully created yet
704 if ( state != CACHE_ENTRY_READY ) {
706 assert(state != CACHE_ENTRY_UNDEFINED);
708 /* free cache mutex */
709 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
712 LDAP_LOG(( "caceh", LDAP_LEVEL_INFO,
713 "cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
717 Debug(LDAP_DEBUG_TRACE,
718 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
723 ldap_pvt_thread_yield();
727 /* acquire reader lock */
728 if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
729 /* could not acquire entry lock...
730 * owner cannot free as we have the cache locked.
731 * so, unlock the cache, yield, and try again.
734 /* free cache mutex */
735 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
738 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
739 "cache_find_entry_id: %ld -> %ld (busy) %d.\n",
742 Debug(LDAP_DEBUG_TRACE,
743 "====> cache_find_entry_id( %ld ): %ld (busy) %d\n",
748 ldap_pvt_thread_yield();
753 LRU_DELETE( cache, ep );
754 LRU_ADD( cache, ep );
756 LEI(ep)->lei_refcnt++;
758 /* free cache mutex */
759 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
762 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
763 "cache_find_entry_id: %ld -> %s found %d tries.\n",
764 ep_id, ep->e_dn, count ));
766 Debug(LDAP_DEBUG_TRACE,
767 "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
768 ep_id, ep->e_dn, count);
775 /* free cache mutex */
776 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
782 * cache_delete_entry - delete the entry e from the cache. the caller
783 * should have obtained e (increasing its ref count) via a call to one
784 * of the cache_find_* routines. the caller should *not* call the
785 * cache_return_entry() routine prior to calling cache_delete_entry().
786 * it performs this function.
788 * returns: 0 e was deleted ok
789 * 1 e was not in the cache
790 * -1 something bad happened
800 /* set cache mutex */
801 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
803 assert( e->e_private );
806 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
807 "cache_delete_entry: delete %ld.\n", e->e_id ));
809 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
814 rc = cache_delete_entry_internal( cache, e );
816 /* free cache mutex */
817 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
822 cache_delete_entry_internal(
827 int rc = 0; /* return code */
830 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
837 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
848 LRU_DELETE( cache, e );
852 * flag entry to be freed later by a call to cache_return_entry()
854 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
860 cache_release_all( Cache *cache )
865 /* set cache mutex */
866 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
869 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
870 "cache_release_all: enter\n" ));
872 Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
876 while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
877 #ifdef LDAP_RDWR_DEBUG
878 assert(!ldap_pvt_thread_rdwr_active(&LEI(e)->lei_rdwr));
881 /* delete from cache and lru q */
882 /* XXX do we need rc ? */
883 rc = cache_delete_entry_internal( cache, e );
884 cache_entry_private_destroy( e );
888 if ( cache->c_cursize ) {
890 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
891 "cache_release_all: Entry cache could not be emptied.\n" ));
893 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
898 /* free cache mutex */
899 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
905 lru_print( Cache *cache )
909 fprintf( stderr, "LRU queue (head to tail):\n" );
910 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
911 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
912 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
914 fprintf( stderr, "LRU queue (tail to head):\n" );
915 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
916 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
917 e->e_dn, e->e_id, LEI(e)->lei_refcnt );