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 )
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( %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 "====> 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 "====> 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 "====> cache_return_entry_%s( %ld ): 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( %ld ): \"%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( %ld ): \"%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( %ld ): \"%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 /* XXX do we need rc ? */
309 rc = cache_delete_entry_internal( cache, e );
310 cache_entry_private_destroy( e );
315 /* free cache mutex */
316 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
321 * cache_update_entry - update a LOCKED entry which has been deleted.
322 * returns: 0 entry has been created and locked
323 * 1 entry already existed
324 * -1 something bad happened
335 /* set cache mutex */
336 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
339 assert( e->e_private );
342 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
343 entry_dn_cmp, avl_dup_error ) != 0 )
345 Debug( LDAP_DEBUG_TRACE,
346 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
347 e->e_id, e->e_dn, 0 );
349 /* free cache mutex */
350 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
355 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
356 entry_id_cmp, avl_dup_error ) != 0 )
358 Debug( LDAP_DEBUG_ANY,
359 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
360 e->e_id, e->e_dn, 0 );
362 /* delete from dn tree inserted above */
363 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
364 entry_dn_cmp ) == NULL )
366 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
370 /* free cache mutex */
371 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
376 /* put the entry into 'CREATING' state */
377 /* will be marked after when entry is returned */
378 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
382 if ( ++cache->c_cursize > cache->c_maxsize ) {
384 * find the lru entry not currently in use and delete it.
385 * in case a lot of entries are in use, only look at the
386 * first 10 on the tail of the list.
389 while ( cache->c_lrutail != NULL &&
390 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
393 /* move this in-use entry to the front of the q */
394 ee = cache->c_lrutail;
395 LRU_DELETE( cache, ee );
396 LRU_ADD( cache, ee );
401 * found at least one to delete - try to get back under
402 * the max cache size.
404 while ( cache->c_lrutail != NULL &&
405 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
406 cache->c_cursize > cache->c_maxsize )
408 e = cache->c_lrutail;
410 /* delete from cache and lru q */
411 /* XXX do we need rc ? */
412 rc = cache_delete_entry_internal( cache, e );
413 cache_entry_private_destroy( e );
418 /* free cache mutex */
419 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
424 * cache_find_entry_dn2id - find an entry in the cache, given dn
428 cache_find_entry_dn2id(
434 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
438 /* set cache mutex */
439 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
442 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
444 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
445 entry_dn_cmp )) != NULL )
448 * ep now points to an unlocked entry
449 * we do not need to lock the entry if we only
450 * check the state, refcnt, LRU, and id.
455 assert( ep->e_private );
458 * entry is deleted or not fully created yet
460 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
462 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
464 Debug(LDAP_DEBUG_TRACE,
465 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
466 dn, ep->e_id, LEI(ep)->lei_state);
468 /* free cache mutex */
469 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
473 Debug(LDAP_DEBUG_TRACE,
474 "====> cache_find_entry_dn2id(\"%s\"): %ld\n",
478 LRU_DELETE( cache, ep );
479 LRU_ADD( cache, ep );
484 /* free cache mutex */
485 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
492 /* free cache mutex */
493 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
499 * cache_find_entry_id - find an entry in the cache, given id
515 /* set cache mutex */
516 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
518 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
519 entry_id_cmp )) != NULL )
522 assert( ep->e_private );
525 * entry is deleted or not fully created yet
527 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
529 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
531 Debug(LDAP_DEBUG_TRACE,
532 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
533 id, ep->e_id, LEI(ep)->lei_state);
535 /* free cache mutex */
536 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
540 Debug(LDAP_DEBUG_TRACE,
541 "====> cache_find_entry_id( %ld, %s ) \"%s\" (found)\n",
542 id, rw ? "w" : "r", ep->e_dn);
544 /* acquire reader lock */
545 if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
546 /* could not acquire entry lock...
547 * owner cannot free as we have the cache locked.
548 * so, unlock the cache, yield, and try again.
551 /* free cache mutex */
552 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
553 ldap_pvt_thread_yield();
558 LRU_DELETE( cache, ep );
559 LRU_ADD( cache, ep );
561 LEI(ep)->lei_refcnt++;
563 /* free cache mutex */
564 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
569 /* free cache mutex */
570 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
576 * cache_delete_entry - delete the entry e from the cache. the caller
577 * should have obtained e (increasing its ref count) via a call to one
578 * of the cache_find_* routines. the caller should *not* call the
579 * cache_return_entry() routine prior to calling cache_delete_entry().
580 * it performs this function.
582 * returns: 0 e was deleted ok
583 * 1 e was not in the cache
584 * -1 something bad happened
594 /* set cache mutex */
595 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
598 assert( e->e_private );
601 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
604 rc = cache_delete_entry_internal( cache, e );
606 /* free cache mutex */
607 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
612 cache_delete_entry_internal(
617 int rc = 0; /* return code */
620 if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp )
627 if ( avl_delete( &cache->c_idtree, (caddr_t) e, entry_id_cmp )
638 LRU_DELETE( cache, e );
642 * flag entry to be freed later by a call to cache_return_entry()
644 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
652 lru_print( struct cache *cache )
656 fprintf( stderr, "LRU queue (head to tail):\n" );
657 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
658 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
659 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
661 fprintf( stderr, "LRU queue (tail to head):\n" );
662 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
663 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
664 e->e_dn, e->e_id, LEI(e)->lei_refcnt );