1 /* cache.c - routines to maintain an in-core cache of entries */
13 #include "back-ldbm.h"
15 /* LDBM backend specific entry info -- visible only to the cache */
16 typedef struct ldbm_entry_info {
17 ldap_pvt_thread_rdwr_t lei_rdwr; /* reader/writer lock */
20 * remaining fields require backend cache lock to access
21 * These items are specific to the LDBM backend and should
24 int lei_state; /* for the cache */
25 #define CACHE_ENTRY_UNDEFINED 0
26 #define CACHE_ENTRY_CREATING 1
27 #define CACHE_ENTRY_READY 2
28 #define CACHE_ENTRY_DELETED 3
30 int lei_refcnt; /* # threads ref'ing this entry */
31 Entry *lei_lrunext; /* for cache lru list */
34 #define LEI(e) ((EntryInfo *) ((e)->e_private))
36 static int cache_delete_entry_internal(Cache *cache, Entry *e);
38 static void lru_print(Cache *cache);
42 cache_entry_rdwr_lock(Entry *e, int rw)
44 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
45 rw ? "w" : "r", e->e_id, 0);
48 return ldap_pvt_thread_rdwr_wlock(&LEI(e)->lei_rdwr);
50 return ldap_pvt_thread_rdwr_rlock(&LEI(e)->lei_rdwr);
54 cache_entry_rdwr_trylock(Entry *e, int rw)
56 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%strylock: ID: %ld\n",
57 rw ? "w" : "r", e->e_id, 0);
60 return ldap_pvt_thread_rdwr_wtrylock(&LEI(e)->lei_rdwr);
62 return ldap_pvt_thread_rdwr_rtrylock(&LEI(e)->lei_rdwr);
66 cache_entry_rdwr_unlock(Entry *e, int rw)
68 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
69 rw ? "w" : "r", e->e_id, 0);
72 return ldap_pvt_thread_rdwr_wunlock(&LEI(e)->lei_rdwr);
74 return ldap_pvt_thread_rdwr_runlock(&LEI(e)->lei_rdwr);
78 cache_entry_rdwr_init(Entry *e)
80 return ldap_pvt_thread_rdwr_init( &LEI(e)->lei_rdwr );
84 cache_entry_rdwr_destroy(Entry *e)
86 return ldap_pvt_thread_rdwr_destroy( &LEI(e)->lei_rdwr );
90 cache_entry_private_init( Entry*e )
92 assert( e->e_private == NULL );
94 if( e->e_private != NULL ) {
95 /* this should never happen */
99 e->e_private = ch_calloc(1, sizeof(struct ldbm_entry_info));
101 if( cache_entry_rdwr_init( e ) != 0 ) {
111 cache_entry_private_destroy( Entry*e )
113 assert( e->e_private );
115 cache_entry_rdwr_destroy( e );
117 free( e->e_private );
123 cache_return_entry_rw( Cache *cache, Entry *e, int rw )
128 /* set cache mutex */
129 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
131 assert( e->e_private );
133 cache_entry_rdwr_unlock(e, rw);
136 refcnt = --LEI(e)->lei_refcnt;
138 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
139 LEI(e)->lei_state = CACHE_ENTRY_READY;
141 /* free cache mutex */
142 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
144 Debug( LDAP_DEBUG_TRACE,
145 "====> cache_return_entry_%s( %ld ): created (%d)\n",
146 rw ? "w" : "r", id, refcnt );
148 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
150 /* free cache mutex */
151 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
153 Debug( LDAP_DEBUG_TRACE,
154 "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
155 rw ? "w" : "r", id, refcnt );
158 cache_entry_private_destroy( e );
161 /* free cache mutex */
162 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
164 Debug( LDAP_DEBUG_TRACE,
165 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
166 rw ? "w" : "r", id, refcnt );
170 /* free cache mutex */
171 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
173 Debug( LDAP_DEBUG_TRACE,
174 "====> cache_return_entry_%s( %ld ): returned (%d)\n",
175 rw ? "w" : "r", id, refcnt);
179 #define LRU_DELETE( cache, e ) do { \
180 if ( LEI(e)->lei_lruprev != NULL ) { \
181 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
183 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
185 if ( LEI(e)->lei_lrunext != NULL ) { \
186 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
188 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
192 #define LRU_ADD( cache, e ) do { \
193 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
194 if ( LEI(e)->lei_lrunext != NULL ) { \
195 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
197 (cache)->c_lruhead = (e); \
198 LEI(e)->lei_lruprev = NULL; \
199 if ( (cache)->c_lrutail == NULL ) { \
200 (cache)->c_lrutail = (e); \
205 * cache_add_entry_rw - create and lock an entry in the cache
206 * returns: 0 entry has been created and locked
207 * 1 entry already existed
208 * -1 something bad happened
220 /* set cache mutex */
221 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
223 assert( e->e_private == NULL );
225 if( cache_entry_private_init(e) != 0 ) {
226 /* free cache mutex */
227 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
229 Debug( LDAP_DEBUG_ANY,
230 "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
231 e->e_id, e->e_dn, 0 );
236 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
237 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
239 /* free cache mutex */
240 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
242 Debug( LDAP_DEBUG_TRACE,
243 "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
244 e->e_id, e->e_dn, 0 );
246 cache_entry_private_destroy(e);
252 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
253 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
255 Debug( LDAP_DEBUG_ANY,
256 "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
257 e->e_id, e->e_dn, 0 );
260 /* delete from dn tree inserted above */
261 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
262 (AVL_CMP) entry_dn_cmp ) == NULL )
264 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
268 cache_entry_private_destroy(e);
270 /* free cache mutex */
271 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
275 cache_entry_rdwr_lock( e, rw );
277 /* put the entry into 'CREATING' state */
278 /* will be marked after when entry is returned */
279 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
280 LEI(e)->lei_refcnt = 1;
284 if ( ++cache->c_cursize > cache->c_maxsize ) {
286 * find the lru entry not currently in use and delete it.
287 * in case a lot of entries are in use, only look at the
288 * first 10 on the tail of the list.
291 while ( cache->c_lrutail != NULL &&
292 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
295 /* move this in-use entry to the front of the q */
296 ee = cache->c_lrutail;
297 LRU_DELETE( cache, ee );
298 LRU_ADD( cache, ee );
303 * found at least one to delete - try to get back under
304 * the max cache size.
306 while ( cache->c_lrutail != NULL &&
307 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
308 cache->c_cursize > cache->c_maxsize )
310 e = cache->c_lrutail;
312 /* delete from cache and lru q */
313 /* XXX do we need rc ? */
314 rc = cache_delete_entry_internal( cache, e );
315 cache_entry_private_destroy( e );
320 /* free cache mutex */
321 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
326 * cache_update_entry - update a LOCKED entry which has been deleted.
327 * returns: 0 entry has been created and locked
328 * 1 entry already existed
329 * -1 something bad happened
340 /* set cache mutex */
341 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
343 assert( e->e_private );
345 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
346 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
348 Debug( LDAP_DEBUG_TRACE,
349 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
350 e->e_id, e->e_dn, 0 );
352 /* free cache mutex */
353 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
358 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
359 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
361 Debug( LDAP_DEBUG_ANY,
362 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
363 e->e_id, e->e_dn, 0 );
365 /* delete from dn tree inserted above */
366 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
367 (AVL_CMP) entry_dn_cmp ) == NULL )
369 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
373 /* free cache mutex */
374 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
379 /* put the entry into 'CREATING' state */
380 /* will be marked after when entry is returned */
381 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
385 if ( ++cache->c_cursize > cache->c_maxsize ) {
387 * find the lru entry not currently in use and delete it.
388 * in case a lot of entries are in use, only look at the
389 * first 10 on the tail of the list.
392 while ( cache->c_lrutail != NULL &&
393 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
396 /* move this in-use entry to the front of the q */
397 ee = cache->c_lrutail;
398 LRU_DELETE( cache, ee );
399 LRU_ADD( cache, ee );
404 * found at least one to delete - try to get back under
405 * the max cache size.
407 while ( cache->c_lrutail != NULL &&
408 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
409 cache->c_cursize > cache->c_maxsize )
411 e = cache->c_lrutail;
413 /* delete from cache and lru q */
414 /* XXX do we need rc ? */
415 rc = cache_delete_entry_internal( cache, e );
416 cache_entry_private_destroy( e );
421 /* free cache mutex */
422 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
427 * cache_find_entry_dn2id - find an entry in the cache, given dn
431 cache_find_entry_dn2id(
442 e.e_ndn = ch_strdup( dn );
443 (void) dn_normalize_case( e.e_ndn );
446 /* set cache mutex */
447 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
449 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
450 (AVL_CMP) entry_dn_cmp )) != NULL )
456 * ep now points to an unlocked entry
457 * we do not need to lock the entry if we only
458 * check the state, refcnt, LRU, and id.
461 assert( ep->e_private );
465 state = LEI(ep)->lei_state;
468 * entry is deleted or not fully created yet
470 if ( state != CACHE_ENTRY_READY ) {
471 assert(state != CACHE_ENTRY_UNDEFINED);
473 /* free cache mutex */
474 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
476 Debug(LDAP_DEBUG_TRACE,
477 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
480 ldap_pvt_thread_yield();
485 LRU_DELETE( cache, ep );
486 LRU_ADD( cache, ep );
488 /* free cache mutex */
489 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
491 Debug(LDAP_DEBUG_TRACE,
492 "====> cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
496 /* free cache mutex */
497 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
508 * cache_find_entry_id - find an entry in the cache, given id
525 /* set cache mutex */
526 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
528 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
529 (AVL_CMP) entry_id_cmp )) != NULL )
536 assert( ep->e_private );
539 state = LEI(ep)->lei_state;
542 * entry is deleted or not fully created yet
544 if ( state != CACHE_ENTRY_READY ) {
546 assert(state != CACHE_ENTRY_UNDEFINED);
548 /* free cache mutex */
549 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
551 Debug(LDAP_DEBUG_TRACE,
552 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
555 ldap_pvt_thread_yield();
559 /* acquire reader lock */
560 if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
561 /* could not acquire entry lock...
562 * owner cannot free as we have the cache locked.
563 * so, unlock the cache, yield, and try again.
566 /* free cache mutex */
567 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
569 Debug(LDAP_DEBUG_TRACE,
570 "====> cache_find_entry_id( %ld ): %ld (busy) %d\n",
573 ldap_pvt_thread_yield();
578 LRU_DELETE( cache, ep );
579 LRU_ADD( cache, ep );
581 LEI(ep)->lei_refcnt++;
583 /* free cache mutex */
584 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
586 Debug(LDAP_DEBUG_TRACE,
587 "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
588 ep_id, ep->e_dn, count);
593 /* free cache mutex */
594 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
600 * cache_delete_entry - delete the entry e from the cache. the caller
601 * should have obtained e (increasing its ref count) via a call to one
602 * of the cache_find_* routines. the caller should *not* call the
603 * cache_return_entry() routine prior to calling cache_delete_entry().
604 * it performs this function.
606 * returns: 0 e was deleted ok
607 * 1 e was not in the cache
608 * -1 something bad happened
618 /* set cache mutex */
619 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
621 assert( e->e_private );
623 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
626 rc = cache_delete_entry_internal( cache, e );
628 /* free cache mutex */
629 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
634 cache_delete_entry_internal(
639 int rc = 0; /* return code */
642 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
649 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
660 LRU_DELETE( cache, e );
664 * flag entry to be freed later by a call to cache_return_entry()
666 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
674 cache_release_all( Cache *cache )
679 /* set cache mutex */
680 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
682 Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
684 while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
685 assert(!ldap_pvt_thread_rdwr_active(&LEI(e)->lei_rdwr));
687 /* delete from cache and lru q */
688 /* XXX do we need rc ? */
689 rc = cache_delete_entry_internal( cache, e );
690 cache_entry_private_destroy( e );
694 if ( cache->c_cursize ) {
695 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
698 /* free cache mutex */
699 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
702 #endif /* SLAP_CLEANUP */
707 lru_print( Cache *cache )
711 fprintf( stderr, "LRU queue (head to tail):\n" );
712 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
713 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
714 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
716 fprintf( stderr, "LRU queue (tail to head):\n" );
717 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
718 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
719 e->e_dn, e->e_id, LEI(e)->lei_refcnt );