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 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 )
93 assert( e->e_private == NULL );
96 if( e->e_private != NULL ) {
97 /* this should never happen */
101 e->e_private = ch_calloc(1, sizeof(struct ldbm_entry_info));
103 if( cache_entry_rdwr_init( e ) != 0 ) {
113 cache_entry_private_destroy( Entry*e )
116 assert( e->e_private );
119 cache_entry_rdwr_destroy( e );
121 free( e->e_private );
127 cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
129 /* set cache mutex */
130 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
133 assert( e->e_private );
136 cache_entry_rdwr_unlock(e, rw);
138 LEI(e)->lei_refcnt--;
140 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
141 Debug( LDAP_DEBUG_TRACE,
142 "====> cache_return_entry_%s( %ld ): created (%d)\n",
143 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
145 LEI(e)->lei_state = CACHE_ENTRY_READY;
147 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
148 if( LEI(e)->lei_refcnt > 0 ) {
149 Debug( LDAP_DEBUG_TRACE,
150 "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
151 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
154 Debug( LDAP_DEBUG_TRACE,
155 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
156 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
158 cache_entry_private_destroy( e );
163 Debug( LDAP_DEBUG_TRACE,
164 "====> cache_return_entry_%s( %ld ): returned (%d)\n",
165 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt);
169 /* free cache mutex */
170 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
173 #define LRU_DELETE( cache, e ) { \
174 if ( LEI(e)->lei_lruprev != NULL ) { \
175 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
177 cache->c_lruhead = LEI(e)->lei_lrunext; \
179 if ( LEI(e)->lei_lrunext != NULL ) { \
180 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
182 cache->c_lrutail = LEI(e)->lei_lruprev; \
186 #define LRU_ADD( cache, e ) { \
187 LEI(e)->lei_lrunext = cache->c_lruhead; \
188 if ( LEI(e)->lei_lrunext != NULL ) { \
189 LEI(LEI(e)->lei_lrunext)->lei_lruprev = e; \
191 cache->c_lruhead = e; \
192 LEI(e)->lei_lruprev = NULL; \
193 if ( cache->c_lrutail == NULL ) { \
194 cache->c_lrutail = e; \
199 * cache_add_entry_rw - create and lock an entry in the cache
200 * returns: 0 entry has been created and locked
201 * 1 entry already existed
202 * -1 something bad happened
214 /* set cache mutex */
215 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
218 assert( e->e_private == NULL );
221 if( cache_entry_private_init(e) != 0 ) {
222 Debug( LDAP_DEBUG_ANY,
223 "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
224 e->e_id, e->e_dn, 0 );
228 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
229 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
231 Debug( LDAP_DEBUG_TRACE,
232 "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
233 e->e_id, e->e_dn, 0 );
235 cache_entry_private_destroy(e);
237 /* free cache mutex */
238 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
243 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
244 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
246 Debug( LDAP_DEBUG_ANY,
247 "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
248 e->e_id, e->e_dn, 0 );
251 /* delete from dn tree inserted above */
252 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
253 (AVL_CMP) entry_dn_cmp ) == NULL )
255 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
259 cache_entry_private_destroy(e);
261 /* free cache mutex */
262 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
266 cache_entry_rdwr_lock( e, rw );
268 /* put the entry into 'CREATING' state */
269 /* will be marked after when entry is returned */
270 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
271 LEI(e)->lei_refcnt = 1;
275 if ( ++cache->c_cursize > cache->c_maxsize ) {
277 * find the lru entry not currently in use and delete it.
278 * in case a lot of entries are in use, only look at the
279 * first 10 on the tail of the list.
282 while ( cache->c_lrutail != NULL &&
283 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
286 /* move this in-use entry to the front of the q */
287 ee = cache->c_lrutail;
288 LRU_DELETE( cache, ee );
289 LRU_ADD( cache, ee );
294 * found at least one to delete - try to get back under
295 * the max cache size.
297 while ( cache->c_lrutail != NULL &&
298 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
299 cache->c_cursize > cache->c_maxsize )
301 e = cache->c_lrutail;
303 /* delete from cache and lru q */
304 /* XXX do we need rc ? */
305 rc = cache_delete_entry_internal( cache, e );
306 cache_entry_private_destroy( e );
311 /* free cache mutex */
312 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
317 * cache_update_entry - update a LOCKED entry which has been deleted.
318 * returns: 0 entry has been created and locked
319 * 1 entry already existed
320 * -1 something bad happened
331 /* set cache mutex */
332 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
335 assert( e->e_private );
338 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
339 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
341 Debug( LDAP_DEBUG_TRACE,
342 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
343 e->e_id, e->e_dn, 0 );
345 /* free cache mutex */
346 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
351 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
352 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
354 Debug( LDAP_DEBUG_ANY,
355 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
356 e->e_id, e->e_dn, 0 );
358 /* delete from dn tree inserted above */
359 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
360 (AVL_CMP) entry_dn_cmp ) == NULL )
362 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
366 /* free cache mutex */
367 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
372 /* put the entry into 'CREATING' state */
373 /* will be marked after when entry is returned */
374 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
378 if ( ++cache->c_cursize > cache->c_maxsize ) {
380 * find the lru entry not currently in use and delete it.
381 * in case a lot of entries are in use, only look at the
382 * first 10 on the tail of the list.
385 while ( cache->c_lrutail != NULL &&
386 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
389 /* move this in-use entry to the front of the q */
390 ee = cache->c_lrutail;
391 LRU_DELETE( cache, ee );
392 LRU_ADD( cache, ee );
397 * found at least one to delete - try to get back under
398 * the max cache size.
400 while ( cache->c_lrutail != NULL &&
401 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
402 cache->c_cursize > cache->c_maxsize )
404 e = cache->c_lrutail;
406 /* delete from cache and lru q */
407 /* XXX do we need rc ? */
408 rc = cache_delete_entry_internal( cache, e );
409 cache_entry_private_destroy( e );
414 /* free cache mutex */
415 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
420 * cache_find_entry_dn2id - find an entry in the cache, given dn
424 cache_find_entry_dn2id(
433 /* set cache mutex */
434 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
437 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
439 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
440 (AVL_CMP) entry_dn_cmp )) != NULL )
443 * ep now points to an unlocked entry
444 * we do not need to lock the entry if we only
445 * check the state, refcnt, LRU, and id.
450 assert( ep->e_private );
453 * entry is deleted or not fully created yet
455 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
457 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
459 Debug(LDAP_DEBUG_TRACE,
460 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
461 dn, ep->e_id, LEI(ep)->lei_state);
463 /* free cache mutex */
464 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
468 Debug(LDAP_DEBUG_TRACE,
469 "====> cache_find_entry_dn2id(\"%s\"): %ld\n",
473 LRU_DELETE( cache, ep );
474 LRU_ADD( cache, ep );
479 /* free cache mutex */
480 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
487 /* free cache mutex */
488 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
494 * cache_find_entry_id - find an entry in the cache, given id
510 /* set cache mutex */
511 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
513 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
514 (AVL_CMP) entry_id_cmp )) != NULL )
517 assert( ep->e_private );
520 * entry is deleted or not fully created yet
522 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
524 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
526 Debug(LDAP_DEBUG_TRACE,
527 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
528 id, ep->e_id, LEI(ep)->lei_state);
530 /* free cache mutex */
531 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
535 Debug(LDAP_DEBUG_TRACE,
536 "====> cache_find_entry_id( %ld, %s ) \"%s\" (found)\n",
537 id, rw ? "w" : "r", ep->e_dn);
539 /* acquire reader lock */
540 if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
541 /* could not acquire entry lock...
542 * owner cannot free as we have the cache locked.
543 * so, unlock the cache, yield, and try again.
546 /* free cache mutex */
547 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
548 ldap_pvt_thread_yield();
553 LRU_DELETE( cache, ep );
554 LRU_ADD( cache, ep );
556 LEI(ep)->lei_refcnt++;
558 /* free cache mutex */
559 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
564 /* free cache mutex */
565 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
571 * cache_delete_entry - delete the entry e from the cache. the caller
572 * should have obtained e (increasing its ref count) via a call to one
573 * of the cache_find_* routines. the caller should *not* call the
574 * cache_return_entry() routine prior to calling cache_delete_entry().
575 * it performs this function.
577 * returns: 0 e was deleted ok
578 * 1 e was not in the cache
579 * -1 something bad happened
589 /* set cache mutex */
590 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
593 assert( e->e_private );
596 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
599 rc = cache_delete_entry_internal( cache, e );
601 /* free cache mutex */
602 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
607 cache_delete_entry_internal(
612 int rc = 0; /* return code */
615 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
622 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
633 LRU_DELETE( cache, e );
637 * flag entry to be freed later by a call to cache_return_entry()
639 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
647 lru_print( struct cache *cache )
651 fprintf( stderr, "LRU queue (head to tail):\n" );
652 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
653 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
654 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
656 fprintf( stderr, "LRU queue (tail to head):\n" );
657 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
658 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
659 e->e_dn, e->e_id, LEI(e)->lei_refcnt );