1 /* cache.c - routines to maintain an in-core cache of entries */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
17 #include "back-ldbm.h"
19 /* LDBM backend specific entry info -- visible only to the cache */
20 typedef struct ldbm_entry_info {
21 ldap_pvt_thread_rdwr_t lei_rdwr; /* reader/writer lock */
24 * remaining fields require backend cache lock to access
25 * These items are specific to the LDBM backend and should
28 int lei_state; /* for the cache */
29 #define CACHE_ENTRY_UNDEFINED 0
30 #define CACHE_ENTRY_CREATING 1
31 #define CACHE_ENTRY_READY 2
32 #define CACHE_ENTRY_DELETED 3
34 int lei_refcnt; /* # threads ref'ing this entry */
35 Entry *lei_lrunext; /* for cache lru list */
38 #define LEI(e) ((EntryInfo *) ((e)->e_private))
40 static int cache_delete_entry_internal(Cache *cache, Entry *e);
42 static void lru_print(Cache *cache);
46 cache_entry_rdwr_lock(Entry *e, int rw)
48 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
49 rw ? "w" : "r", e->e_id, 0);
52 return ldap_pvt_thread_rdwr_wlock(&LEI(e)->lei_rdwr);
54 return ldap_pvt_thread_rdwr_rlock(&LEI(e)->lei_rdwr);
58 cache_entry_rdwr_trylock(Entry *e, int rw)
60 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%strylock: ID: %ld\n",
61 rw ? "w" : "r", e->e_id, 0);
64 return ldap_pvt_thread_rdwr_wtrylock(&LEI(e)->lei_rdwr);
66 return ldap_pvt_thread_rdwr_rtrylock(&LEI(e)->lei_rdwr);
70 cache_entry_rdwr_unlock(Entry *e, int rw)
72 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
73 rw ? "w" : "r", e->e_id, 0);
76 return ldap_pvt_thread_rdwr_wunlock(&LEI(e)->lei_rdwr);
78 return ldap_pvt_thread_rdwr_runlock(&LEI(e)->lei_rdwr);
82 cache_entry_rdwr_init(Entry *e)
84 return ldap_pvt_thread_rdwr_init( &LEI(e)->lei_rdwr );
88 cache_entry_rdwr_destroy(Entry *e)
90 return ldap_pvt_thread_rdwr_destroy( &LEI(e)->lei_rdwr );
94 cache_entry_private_init( Entry*e )
96 assert( e->e_private == NULL );
98 if( e->e_private != NULL ) {
99 /* this should never happen */
103 e->e_private = ch_calloc(1, sizeof(struct ldbm_entry_info));
105 if( cache_entry_rdwr_init( e ) != 0 ) {
115 cache_entry_private_destroy( Entry*e )
117 assert( e->e_private );
119 cache_entry_rdwr_destroy( e );
121 free( e->e_private );
127 cache_return_entry_rw( Cache *cache, Entry *e, int rw )
132 /* set cache mutex */
133 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
135 assert( e->e_private );
137 cache_entry_rdwr_unlock(e, rw);
140 refcnt = --LEI(e)->lei_refcnt;
142 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
143 LEI(e)->lei_state = CACHE_ENTRY_READY;
145 /* free cache mutex */
146 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
148 Debug( LDAP_DEBUG_TRACE,
149 "====> cache_return_entry_%s( %ld ): created (%d)\n",
150 rw ? "w" : "r", id, refcnt );
152 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
154 /* free cache mutex */
155 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
157 Debug( LDAP_DEBUG_TRACE,
158 "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
159 rw ? "w" : "r", id, refcnt );
162 cache_entry_private_destroy( e );
165 /* free cache mutex */
166 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
168 Debug( LDAP_DEBUG_TRACE,
169 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
170 rw ? "w" : "r", id, refcnt );
174 /* free cache mutex */
175 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
177 Debug( LDAP_DEBUG_TRACE,
178 "====> cache_return_entry_%s( %ld ): returned (%d)\n",
179 rw ? "w" : "r", id, refcnt);
183 #define LRU_DELETE( cache, e ) do { \
184 if ( LEI(e)->lei_lruprev != NULL ) { \
185 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
187 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
189 if ( LEI(e)->lei_lrunext != NULL ) { \
190 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
192 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
196 #define LRU_ADD( cache, e ) do { \
197 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
198 if ( LEI(e)->lei_lrunext != NULL ) { \
199 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
201 (cache)->c_lruhead = (e); \
202 LEI(e)->lei_lruprev = NULL; \
203 if ( (cache)->c_lrutail == NULL ) { \
204 (cache)->c_lrutail = (e); \
209 * cache_add_entry_rw - create and lock an entry in the cache
210 * returns: 0 entry has been created and locked
211 * 1 entry already existed
212 * -1 something bad happened
224 /* set cache mutex */
225 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
227 assert( e->e_private == NULL );
229 if( cache_entry_private_init(e) != 0 ) {
230 /* free cache mutex */
231 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
233 Debug( LDAP_DEBUG_ANY,
234 "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
235 e->e_id, e->e_dn, 0 );
240 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
241 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
243 /* free cache mutex */
244 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
246 Debug( LDAP_DEBUG_TRACE,
247 "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
248 e->e_id, e->e_dn, 0 );
250 cache_entry_private_destroy(e);
256 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
257 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
259 Debug( LDAP_DEBUG_ANY,
260 "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
261 e->e_id, e->e_dn, 0 );
264 /* delete from dn tree inserted above */
265 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
266 (AVL_CMP) entry_dn_cmp ) == NULL )
268 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
272 cache_entry_private_destroy(e);
274 /* free cache mutex */
275 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
279 cache_entry_rdwr_lock( e, rw );
281 /* put the entry into 'CREATING' state */
282 /* will be marked after when entry is returned */
283 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
284 LEI(e)->lei_refcnt = 1;
288 if ( ++cache->c_cursize > cache->c_maxsize ) {
290 * find the lru entry not currently in use and delete it.
291 * in case a lot of entries are in use, only look at the
292 * first 10 on the tail of the list.
295 while ( cache->c_lrutail != NULL &&
296 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
299 /* move this in-use entry to the front of the q */
300 ee = cache->c_lrutail;
301 LRU_DELETE( cache, ee );
302 LRU_ADD( cache, ee );
307 * found at least one to delete - try to get back under
308 * the max cache size.
310 while ( cache->c_lrutail != NULL &&
311 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
312 cache->c_cursize > cache->c_maxsize )
314 e = cache->c_lrutail;
316 /* delete from cache and lru q */
317 /* XXX do we need rc ? */
318 rc = cache_delete_entry_internal( cache, e );
319 cache_entry_private_destroy( e );
324 /* free cache mutex */
325 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
330 * cache_update_entry - update a LOCKED entry which has been deleted.
331 * returns: 0 entry has been created and locked
332 * 1 entry already existed
333 * -1 something bad happened
344 /* set cache mutex */
345 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
347 assert( e->e_private );
349 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
350 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
352 Debug( LDAP_DEBUG_TRACE,
353 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
354 e->e_id, e->e_dn, 0 );
356 /* free cache mutex */
357 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
362 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
363 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
365 Debug( LDAP_DEBUG_ANY,
366 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
367 e->e_id, e->e_dn, 0 );
369 /* delete from dn tree inserted above */
370 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
371 (AVL_CMP) entry_dn_cmp ) == NULL )
373 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
377 /* free cache mutex */
378 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
383 /* put the entry into 'CREATING' state */
384 /* will be marked after when entry is returned */
385 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
389 if ( ++cache->c_cursize > cache->c_maxsize ) {
391 * find the lru entry not currently in use and delete it.
392 * in case a lot of entries are in use, only look at the
393 * first 10 on the tail of the list.
396 while ( cache->c_lrutail != NULL &&
397 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
400 /* move this in-use entry to the front of the q */
401 ee = cache->c_lrutail;
402 LRU_DELETE( cache, ee );
403 LRU_ADD( cache, ee );
408 * found at least one to delete - try to get back under
409 * the max cache size.
411 while ( cache->c_lrutail != NULL &&
412 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
413 cache->c_cursize > cache->c_maxsize )
415 e = cache->c_lrutail;
417 /* delete from cache and lru q */
418 /* XXX do we need rc ? */
419 rc = cache_delete_entry_internal( cache, e );
420 cache_entry_private_destroy( e );
425 /* free cache mutex */
426 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
431 * cache_find_entry_dn2id - find an entry in the cache, given dn
435 cache_find_entry_dn2id(
446 e.e_ndn = ch_strdup( dn );
447 (void) dn_normalize_case( e.e_ndn );
450 /* set cache mutex */
451 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
453 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
454 (AVL_CMP) entry_dn_cmp )) != NULL )
460 * ep now points to an unlocked entry
461 * we do not need to lock the entry if we only
462 * check the state, refcnt, LRU, and id.
465 assert( ep->e_private );
469 state = LEI(ep)->lei_state;
472 * entry is deleted or not fully created yet
474 if ( state != CACHE_ENTRY_READY ) {
475 assert(state != CACHE_ENTRY_UNDEFINED);
477 /* free cache mutex */
478 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
480 Debug(LDAP_DEBUG_TRACE,
481 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
484 ldap_pvt_thread_yield();
489 LRU_DELETE( cache, ep );
490 LRU_ADD( cache, ep );
492 /* free cache mutex */
493 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
495 Debug(LDAP_DEBUG_TRACE,
496 "====> cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
500 /* free cache mutex */
501 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
512 * cache_find_entry_id - find an entry in the cache, given id
529 /* set cache mutex */
530 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
532 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
533 (AVL_CMP) entry_id_cmp )) != NULL )
540 assert( ep->e_private );
543 state = LEI(ep)->lei_state;
546 * entry is deleted or not fully created yet
548 if ( state != CACHE_ENTRY_READY ) {
550 assert(state != CACHE_ENTRY_UNDEFINED);
552 /* free cache mutex */
553 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
555 Debug(LDAP_DEBUG_TRACE,
556 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
559 ldap_pvt_thread_yield();
563 /* acquire reader lock */
564 if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
565 /* could not acquire entry lock...
566 * owner cannot free as we have the cache locked.
567 * so, unlock the cache, yield, and try again.
570 /* free cache mutex */
571 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
573 Debug(LDAP_DEBUG_TRACE,
574 "====> cache_find_entry_id( %ld ): %ld (busy) %d\n",
577 ldap_pvt_thread_yield();
582 LRU_DELETE( cache, ep );
583 LRU_ADD( cache, ep );
585 LEI(ep)->lei_refcnt++;
587 /* free cache mutex */
588 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
590 Debug(LDAP_DEBUG_TRACE,
591 "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
592 ep_id, ep->e_dn, count);
597 /* free cache mutex */
598 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
604 * cache_delete_entry - delete the entry e from the cache. the caller
605 * should have obtained e (increasing its ref count) via a call to one
606 * of the cache_find_* routines. the caller should *not* call the
607 * cache_return_entry() routine prior to calling cache_delete_entry().
608 * it performs this function.
610 * returns: 0 e was deleted ok
611 * 1 e was not in the cache
612 * -1 something bad happened
622 /* set cache mutex */
623 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
625 assert( e->e_private );
627 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
630 rc = cache_delete_entry_internal( cache, e );
632 /* free cache mutex */
633 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
638 cache_delete_entry_internal(
643 int rc = 0; /* return code */
646 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
653 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
664 LRU_DELETE( cache, e );
668 * flag entry to be freed later by a call to cache_return_entry()
670 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
678 cache_release_all( Cache *cache )
683 /* set cache mutex */
684 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
686 Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
688 while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
689 assert(!ldap_pvt_thread_rdwr_active(&LEI(e)->lei_rdwr));
691 /* delete from cache and lru q */
692 /* XXX do we need rc ? */
693 rc = cache_delete_entry_internal( cache, e );
694 cache_entry_private_destroy( e );
698 if ( cache->c_cursize ) {
699 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
702 /* free cache mutex */
703 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
706 #endif /* SLAP_CLEANUP */
711 lru_print( Cache *cache )
715 fprintf( stderr, "LRU queue (head to tail):\n" );
716 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
717 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
718 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
720 fprintf( stderr, "LRU queue (tail to head):\n" );
721 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
722 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
723 e->e_dn, e->e_id, LEI(e)->lei_refcnt );