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(
434 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
437 /* set cache mutex */
438 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
440 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
441 (AVL_CMP) entry_dn_cmp )) != NULL )
444 * ep now points to an unlocked entry
445 * we do not need to lock the entry if we only
446 * check the state, refcnt, LRU, and id.
451 assert( ep->e_private );
454 * entry is deleted or not fully created yet
456 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
458 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
460 Debug(LDAP_DEBUG_TRACE,
461 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
462 dn, ep->e_id, LEI(ep)->lei_state);
464 /* free cache mutex */
465 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
466 ldap_pvt_thread_yield();
470 Debug(LDAP_DEBUG_TRACE,
471 "====> cache_find_entry_dn2id(\"%s\"): %ld\n",
475 LRU_DELETE( cache, ep );
476 LRU_ADD( cache, ep );
481 /* free cache mutex */
482 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
489 /* free cache mutex */
490 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
496 * cache_find_entry_id - find an entry in the cache, given id
512 /* set cache mutex */
513 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
515 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
516 (AVL_CMP) entry_id_cmp )) != NULL )
519 assert( ep->e_private );
522 * entry is deleted or not fully created yet
524 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
526 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
528 Debug(LDAP_DEBUG_TRACE,
529 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
530 id, ep->e_id, LEI(ep)->lei_state);
532 /* free cache mutex */
533 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
534 ldap_pvt_thread_yield();
538 Debug(LDAP_DEBUG_TRACE,
539 "====> cache_find_entry_id( %ld, %s ) \"%s\" (found)\n",
540 id, rw ? "w" : "r", ep->e_dn);
542 /* acquire reader lock */
543 if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
544 /* could not acquire entry lock...
545 * owner cannot free as we have the cache locked.
546 * so, unlock the cache, yield, and try again.
549 /* free cache mutex */
550 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
551 ldap_pvt_thread_yield();
556 LRU_DELETE( cache, ep );
557 LRU_ADD( cache, ep );
559 LEI(ep)->lei_refcnt++;
561 /* free cache mutex */
562 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
567 /* free cache mutex */
568 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
574 * cache_delete_entry - delete the entry e from the cache. the caller
575 * should have obtained e (increasing its ref count) via a call to one
576 * of the cache_find_* routines. the caller should *not* call the
577 * cache_return_entry() routine prior to calling cache_delete_entry().
578 * it performs this function.
580 * returns: 0 e was deleted ok
581 * 1 e was not in the cache
582 * -1 something bad happened
592 /* set cache mutex */
593 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
596 assert( e->e_private );
599 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
602 rc = cache_delete_entry_internal( cache, e );
604 /* free cache mutex */
605 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
610 cache_delete_entry_internal(
615 int rc = 0; /* return code */
618 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
625 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
636 LRU_DELETE( cache, e );
640 * flag entry to be freed later by a call to cache_return_entry()
642 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
650 lru_print( struct cache *cache )
654 fprintf( stderr, "LRU queue (head to tail):\n" );
655 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
656 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
657 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
659 fprintf( stderr, "LRU queue (tail to head):\n" );
660 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
661 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
662 e->e_dn, e->e_id, LEI(e)->lei_refcnt );