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: %lu\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: %lu\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: %lu\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 if( cache_entry_rdwr_init( e ) != 0 ) {
115 cache_entry_private_destroy( Entry*e )
117 struct ldbm_entry_info *lei;
120 assert( e->e_private );
123 cache_entry_rdwr_destroy( e );
125 free( e->e_private );
131 cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
133 /* set cache mutex */
134 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
137 assert( e->e_private );
140 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 "====> cache_return_entry_%s( %lu ): 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 "====> cache_return_entry_%s( %lu ): delete pending (%d)\n",
155 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
158 Debug( LDAP_DEBUG_TRACE,
159 "====> cache_return_entry_%s( %lu ): 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 "====> cache_return_entry_%s( %lu ): returned (%d)\n",
169 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt);
173 /* free cache mutex */
174 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
177 #define LRU_DELETE( cache, e ) { \
178 if ( LEI(e)->lei_lruprev != NULL ) { \
179 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
181 cache->c_lruhead = LEI(e)->lei_lrunext; \
183 if ( LEI(e)->lei_lrunext != NULL ) { \
184 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
186 cache->c_lrutail = LEI(e)->lei_lruprev; \
190 #define LRU_ADD( cache, e ) { \
191 LEI(e)->lei_lrunext = cache->c_lruhead; \
192 if ( LEI(e)->lei_lrunext != NULL ) { \
193 LEI(LEI(e)->lei_lrunext)->lei_lruprev = e; \
195 cache->c_lruhead = e; \
196 LEI(e)->lei_lruprev = NULL; \
197 if ( cache->c_lrutail == NULL ) { \
198 cache->c_lrutail = e; \
203 * cache_add_entry_rw - create and lock an entry in the cache
204 * returns: 0 entry has been created and locked
205 * 1 entry already existed
206 * -1 something bad happened
218 /* set cache mutex */
219 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
222 assert( e->e_private == NULL );
225 if( cache_entry_private_init(e) != 0 ) {
226 Debug( LDAP_DEBUG_ANY,
227 "====> cache_add_entry( %lu ): \"%s\": private init failed!\n",
228 e->e_id, e->e_dn, 0 );
232 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
233 entry_dn_cmp, avl_dup_error ) != 0 )
235 Debug( LDAP_DEBUG_TRACE,
236 "====> cache_add_entry( %lu ): \"%s\": already in dn cache\n",
237 e->e_id, e->e_dn, 0 );
239 cache_entry_private_destroy(e);
241 /* free cache mutex */
242 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
247 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
248 entry_id_cmp, avl_dup_error ) != 0 )
250 Debug( LDAP_DEBUG_ANY,
251 "====> cache_add_entry( %lu ): \"%s\": already in id cache\n",
252 e->e_id, e->e_dn, 0 );
255 /* delete from dn tree inserted above */
256 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
257 entry_dn_cmp ) == NULL )
259 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
263 cache_entry_private_destroy(e);
265 /* free cache mutex */
266 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
270 cache_entry_rdwr_lock( e, rw );
272 /* put the entry into 'CREATING' state */
273 /* will be marked after when entry is returned */
274 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
275 LEI(e)->lei_refcnt = 1;
279 if ( ++cache->c_cursize > cache->c_maxsize ) {
281 * find the lru entry not currently in use and delete it.
282 * in case a lot of entries are in use, only look at the
283 * first 10 on the tail of the list.
286 while ( cache->c_lrutail != NULL &&
287 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
290 /* move this in-use entry to the front of the q */
291 ee = cache->c_lrutail;
292 LRU_DELETE( cache, ee );
293 LRU_ADD( cache, ee );
298 * found at least one to delete - try to get back under
299 * the max cache size.
301 while ( cache->c_lrutail != NULL &&
302 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
303 cache->c_cursize > cache->c_maxsize )
305 e = cache->c_lrutail;
307 /* delete from cache and lru q */
308 rc = cache_delete_entry_internal( cache, e );
309 cache_entry_private_destroy( e );
314 /* free cache mutex */
315 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
320 * cache_update_entry - update a LOCKED entry which has been deleted.
321 * returns: 0 entry has been created and locked
322 * 1 entry already existed
323 * -1 something bad happened
334 /* set cache mutex */
335 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
338 assert( e->e_private );
341 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
342 entry_dn_cmp, avl_dup_error ) != 0 )
344 Debug( LDAP_DEBUG_TRACE,
345 "====> cache_update_entry( %lu ): \"%s\": already in dn cache\n",
346 e->e_id, e->e_dn, 0 );
348 /* free cache mutex */
349 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
354 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
355 entry_id_cmp, avl_dup_error ) != 0 )
357 Debug( LDAP_DEBUG_ANY,
358 "====> cache_update_entry( %lu ): \"%s\": already in id cache\n",
359 e->e_id, e->e_dn, 0 );
361 /* delete from dn tree inserted above */
362 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
363 entry_dn_cmp ) == NULL )
365 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
369 /* free cache mutex */
370 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
375 /* put the entry into 'CREATING' state */
376 /* will be marked after when entry is returned */
377 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
381 if ( ++cache->c_cursize > cache->c_maxsize ) {
383 * find the lru entry not currently in use and delete it.
384 * in case a lot of entries are in use, only look at the
385 * first 10 on the tail of the list.
388 while ( cache->c_lrutail != NULL &&
389 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
392 /* move this in-use entry to the front of the q */
393 ee = cache->c_lrutail;
394 LRU_DELETE( cache, ee );
395 LRU_ADD( cache, ee );
400 * found at least one to delete - try to get back under
401 * the max cache size.
403 while ( cache->c_lrutail != NULL &&
404 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
405 cache->c_cursize > cache->c_maxsize )
407 e = cache->c_lrutail;
409 /* delete from cache and lru q */
410 rc = cache_delete_entry_internal( cache, e );
411 cache_entry_private_destroy( e );
416 /* free cache mutex */
417 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
422 * cache_find_entry_dn2id - find an entry in the cache, given dn
426 cache_find_entry_dn2id(
432 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
436 /* set cache mutex */
437 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
440 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
442 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
443 entry_dn_cmp )) != NULL )
446 * ep now points to an unlocked entry
447 * we do not need to lock the entry if we only
448 * check the state, refcnt, LRU, and id.
453 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 "====> cache_find_entry_dn2id(\"%s\"): %lu (not ready) %d\n",
464 dn, ep->e_id, LEI(ep)->lei_state);
466 /* free cache mutex */
467 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
471 Debug(LDAP_DEBUG_TRACE,
472 "====> cache_find_entry_dn2id(\"%s\"): %lu\n",
476 LRU_DELETE( cache, ep );
477 LRU_ADD( cache, ep );
482 /* free cache mutex */
483 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
490 /* free cache mutex */
491 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
497 * cache_find_entry_id - find an entry in the cache, given id
513 /* set cache mutex */
514 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 );
523 * entry is deleted or not fully created yet
525 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
527 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
529 Debug(LDAP_DEBUG_TRACE,
530 "====> cache_find_entry_id( %lu ): %lu (not ready) %d\n",
531 id, ep->e_id, LEI(ep)->lei_state);
533 /* free cache mutex */
534 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
538 Debug(LDAP_DEBUG_TRACE,
539 "====> cache_find_entry_id( %lu, %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( %lu )\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, entry_dn_cmp )
625 if ( avl_delete( &cache->c_idtree, (caddr_t) e, 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 %lu 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 %lu refcnt %d\n",
662 e->e_dn, e->e_id, LEI(e)->lei_refcnt );