1 /* cache.c - routines to maintain an in-core cache of entries */
13 #include "back-bdb2.h"
15 /* LDBM backend specific entry info -- visible only to the cache */
16 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 struct entry *lei_lrunext; /* for cache lru list */
32 struct entry *lei_lruprev;
34 #define LEI(e) ((struct ldbm_entry_info *) ((e)->e_private))
36 static int cache_delete_entry_internal(struct cache *cache, Entry *e);
38 static void lru_print(struct 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 struct ldbm_entry_info *lei;
95 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 /* DDD if( cache_entry_rdwr_init( e ) != 0 ) { */
106 /* DDD free( LEI(e) ); */
107 /* DDD e->e_private = NULL; */
115 cache_entry_private_destroy( Entry*e )
117 struct ldbm_entry_info *lei;
120 assert( e->e_private );
123 /* DDD cache_entry_rdwr_destroy( e ); */
125 free( e->e_private );
131 bdb2i_cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
133 /* set cache mutex */
134 /* DDD ldap_pvt_thread_mutex_lock( &cache->c_mutex ); */
137 assert( e->e_private );
140 /* DDD cache_entry_rdwr_unlock(e, rw); */
142 LEI(e)->lei_refcnt--;
144 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
145 Debug( LDAP_DEBUG_TRACE,
146 "====> bdb2i_cache_return_entry_%s( %ld ): created (%d)\n",
147 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
149 LEI(e)->lei_state = CACHE_ENTRY_READY;
151 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
152 if( LEI(e)->lei_refcnt > 0 ) {
153 Debug( LDAP_DEBUG_TRACE,
154 "====> bdb2i_cache_return_entry_%s( %ld ): delete pending (%d)\n",
155 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
158 Debug( LDAP_DEBUG_TRACE,
159 "====> bdb2i_cache_return_entry_%s( %ld ): deleted (%d)\n",
160 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
162 cache_entry_private_destroy( e );
167 Debug( LDAP_DEBUG_TRACE,
168 "====> bdb2i_cache_return_entry_%s( %ld ): returned (%d)\n",
169 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt);
172 /* free cache mutex */
173 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
176 #define LRU_DELETE( cache, e ) { \
177 if ( LEI(e)->lei_lruprev != NULL ) { \
178 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
180 cache->c_lruhead = LEI(e)->lei_lrunext; \
182 if ( LEI(e)->lei_lrunext != NULL ) { \
183 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
185 cache->c_lrutail = LEI(e)->lei_lruprev; \
189 #define LRU_ADD( cache, e ) { \
190 LEI(e)->lei_lrunext = cache->c_lruhead; \
191 if ( LEI(e)->lei_lrunext != NULL ) { \
192 LEI(LEI(e)->lei_lrunext)->lei_lruprev = e; \
194 cache->c_lruhead = e; \
195 LEI(e)->lei_lruprev = NULL; \
196 if ( cache->c_lrutail == NULL ) { \
197 cache->c_lrutail = e; \
202 * bdb2i_cache_add_entry_rw - create and lock an entry in the cache
203 * returns: 0 entry has been created and locked
204 * 1 entry already existed
205 * -1 something bad happened
208 bdb2i_cache_add_entry_rw(
217 /* set cache mutex */
218 /* DDD ldap_pvt_thread_mutex_lock( &cache->c_mutex ); */
221 assert( e->e_private == NULL );
224 if( cache_entry_private_init(e) != 0 ) {
225 Debug( LDAP_DEBUG_ANY,
226 "====> bdb2i_cache_add_entry( %ld ): \"%s\": private init failed!\n",
227 e->e_id, e->e_dn, 0 );
231 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
232 entry_dn_cmp, avl_dup_error ) != 0 )
234 Debug( LDAP_DEBUG_TRACE,
235 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
236 e->e_id, e->e_dn, 0 );
238 cache_entry_private_destroy(e);
240 /* free cache mutex */
241 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
246 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
247 entry_id_cmp, avl_dup_error ) != 0 )
249 Debug( LDAP_DEBUG_ANY,
250 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in id cache\n",
251 e->e_id, e->e_dn, 0 );
253 /* delete from dn tree inserted above */
254 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
255 entry_dn_cmp ) == NULL )
257 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
261 cache_entry_private_destroy(e);
263 /* free cache mutex */
264 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
268 /* DDD cache_entry_rdwr_lock( e, rw ); */
270 /* put the entry into 'CREATING' state */
271 /* will be marked after when entry is returned */
272 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
273 LEI(e)->lei_refcnt = 1;
277 if ( ++cache->c_cursize > cache->c_maxsize ) {
279 * find the lru entry not currently in use and delete it.
280 * in case a lot of entries are in use, only look at the
281 * first 10 on the tail of the list.
284 while ( cache->c_lrutail != NULL &&
285 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
288 /* move this in-use entry to the front of the q */
289 ee = cache->c_lrutail;
290 LRU_DELETE( cache, ee );
291 LRU_ADD( cache, ee );
296 * found at least one to delete - try to get back under
297 * the max cache size.
299 while ( cache->c_lrutail != NULL &&
300 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
301 cache->c_cursize > cache->c_maxsize )
303 e = cache->c_lrutail;
305 /* delete from cache and lru q */
306 /* XXX do we need rc ? */
307 rc = cache_delete_entry_internal( cache, e );
308 cache_entry_private_destroy( e );
313 /* free cache mutex */
314 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
319 * cache_update_entry - update a LOCKED entry which has been deleted.
320 * returns: 0 entry has been created and locked
321 * 1 entry already existed
322 * -1 something bad happened
325 bdb2i_cache_update_entry(
333 /* set cache mutex */
334 /* DDD ldap_pvt_thread_mutex_lock( &cache->c_mutex ); */
337 assert( e->e_private );
340 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
341 entry_dn_cmp, avl_dup_error ) != 0 )
343 Debug( LDAP_DEBUG_TRACE,
344 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
345 e->e_id, e->e_dn, 0 );
347 /* free cache mutex */
348 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
353 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
354 entry_id_cmp, avl_dup_error ) != 0 )
356 Debug( LDAP_DEBUG_ANY,
357 "====> bdb2i_cache_update_entry( %ld ): \"%s\": already in id cache\n",
358 e->e_id, e->e_dn, 0 );
360 /* delete from dn tree inserted above */
361 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
362 entry_dn_cmp ) == NULL )
364 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
368 /* free cache mutex */
369 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
373 /* put the entry into 'CREATING' state */
374 /* will be marked after when entry is returned */
375 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
379 if ( ++cache->c_cursize > cache->c_maxsize ) {
381 * find the lru entry not currently in use and delete it.
382 * in case a lot of entries are in use, only look at the
383 * first 10 on the tail of the list.
386 while ( cache->c_lrutail != NULL &&
387 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
390 /* move this in-use entry to the front of the q */
391 ee = cache->c_lrutail;
392 LRU_DELETE( cache, ee );
393 LRU_ADD( cache, ee );
398 * found at least one to delete - try to get back under
399 * the max cache size.
401 while ( cache->c_lrutail != NULL &&
402 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
403 cache->c_cursize > cache->c_maxsize )
405 e = cache->c_lrutail;
407 /* delete from cache and lru q */
408 /* XXX do we need rc ? */
409 rc = cache_delete_entry_internal( cache, e );
410 cache_entry_private_destroy( e );
415 /* free cache mutex */
416 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
421 * cache_find_entry_dn2id - find an entry in the cache, given dn
425 bdb2i_cache_find_entry_dn2id(
431 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
435 /* set cache mutex */
436 /* DDD ldap_pvt_thread_mutex_lock( &cache->c_mutex ); */
439 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
441 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
442 entry_dn_cmp )) != NULL )
445 * ep now points to an unlocked entry
446 * we do not need to lock the entry if we only
447 * check the state, refcnt, LRU, and id.
452 assert( ep->e_private );
456 * entry is deleted or not fully created yet
458 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
460 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
462 Debug(LDAP_DEBUG_TRACE,
463 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
464 dn, ep->e_id, LEI(ep)->lei_state);
466 /* free cache mutex */
467 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
471 Debug(LDAP_DEBUG_TRACE,
472 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld\n",
476 LRU_DELETE( cache, ep );
477 LRU_ADD( cache, ep );
482 /* free cache mutex */
483 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
490 /* free cache mutex */
491 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
497 * cache_find_entry_id - find an entry in the cache, given id
501 bdb2i_cache_find_entry_id(
513 /* set cache mutex */
514 /* DDD ldap_pvt_thread_mutex_lock( &cache->c_mutex ); */
516 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
517 entry_id_cmp )) != NULL )
520 assert( ep->e_private );
524 * entry is deleted or not fully created yet
526 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
528 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
530 Debug(LDAP_DEBUG_TRACE,
531 "====> bdb2i_cache_find_entry_id( %ld ): %ld (not ready) %d\n",
532 id, ep->e_id, LEI(ep)->lei_state);
534 /* free cache mutex */
535 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
539 Debug(LDAP_DEBUG_TRACE,
540 "====> bdb2i_cache_find_entry_id( %ld, %s ) \"%s\" (found)\n",
541 id, rw ? "w" : "r", ep->e_dn);
543 /* acquire reader lock */
544 /* DDD if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) { */
545 /* could not acquire entry lock...
546 * owner cannot free as we have the cache locked.
547 * so, unlock the cache, yield, and try again.
550 /* free cache mutex */
551 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
552 /* DDD ldap_pvt_thread_yield(); */
553 /* DDD goto try_again; */
557 LRU_DELETE( cache, ep );
558 LRU_ADD( cache, ep );
560 LEI(ep)->lei_refcnt++;
562 /* free cache mutex */
563 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
568 /* free cache mutex */
569 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
575 * cache_delete_entry - delete the entry e from the cache. the caller
576 * should have obtained e (increasing its ref count) via a call to one
577 * of the cache_find_* routines. the caller should *not* call the
578 * cache_return_entry() routine prior to calling cache_delete_entry().
579 * it performs this function.
581 * returns: 0 e was deleted ok
582 * 1 e was not in the cache
583 * -1 something bad happened
586 bdb2i_cache_delete_entry(
593 /* set cache mutex */
594 /* DDD ldap_pvt_thread_mutex_lock( &cache->c_mutex ); */
597 assert( e->e_private );
600 Debug( LDAP_DEBUG_TRACE, "====> bdb2i_cache_delete_entry( %ld )\n",
603 rc = cache_delete_entry_internal( cache, e );
605 /* free cache mutex */
606 /* DDD ldap_pvt_thread_mutex_unlock( &cache->c_mutex ); */
611 cache_delete_entry_internal(
616 int rc = 0; /* return code */
619 if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp )
626 if ( avl_delete( &cache->c_idtree, (caddr_t) e, entry_id_cmp )
637 LRU_DELETE( cache, e );
641 * flag entry to be freed later by a call to cache_return_entry()
643 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
651 lru_print( struct cache *cache )
655 fprintf( stderr, "LRU queue (head to tail):\n" );
656 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
657 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
658 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
660 fprintf( stderr, "LRU queue (tail to head):\n" );
661 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
662 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
663 e->e_dn, e->e_id, LEI(e)->lei_refcnt );