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 {
18 * These items are specific to the LDBM backend and should
21 int lei_state; /* for the cache */
22 #define CACHE_ENTRY_UNDEFINED 0
23 #define CACHE_ENTRY_CREATING 1
24 #define CACHE_ENTRY_READY 2
25 #define CACHE_ENTRY_DELETED 3
27 int lei_refcnt; /* # threads ref'ing this entry */
28 struct entry *lei_lrunext; /* for cache lru list */
29 struct entry *lei_lruprev;
31 #define LEI(e) ((struct ldbm_entry_info *) ((e)->e_private))
33 static int cache_delete_entry_internal(struct cache *cache, Entry *e);
35 static void lru_print(struct cache *cache);
39 cache_entry_private_init( Entry*e )
42 assert( e->e_private == NULL );
45 if( e->e_private != NULL ) {
46 /* this should never happen */
50 e->e_private = ch_calloc(1, sizeof(struct ldbm_entry_info));
56 cache_entry_private_destroy( Entry*e )
59 assert( e->e_private );
68 bdb2i_cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
71 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
74 assert( e->e_private );
79 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
80 Debug( LDAP_DEBUG_TRACE,
81 "====> bdb2i_cache_return_entry_%s( %ld ): created (%d)\n",
82 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
84 LEI(e)->lei_state = CACHE_ENTRY_READY;
86 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
87 if( LEI(e)->lei_refcnt > 0 ) {
88 Debug( LDAP_DEBUG_TRACE,
89 "====> bdb2i_cache_return_entry_%s( %ld ): delete pending (%d)\n",
90 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
93 Debug( LDAP_DEBUG_TRACE,
94 "====> bdb2i_cache_return_entry_%s( %ld ): deleted (%d)\n",
95 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
97 cache_entry_private_destroy( e );
102 Debug( LDAP_DEBUG_TRACE,
103 "====> bdb2i_cache_return_entry_%s( %ld ): returned (%d)\n",
104 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt);
107 /* free cache mutex */
108 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
111 #define LRU_DELETE( cache, e ) { \
112 if ( LEI(e)->lei_lruprev != NULL ) { \
113 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
115 cache->c_lruhead = LEI(e)->lei_lrunext; \
117 if ( LEI(e)->lei_lrunext != NULL ) { \
118 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
120 cache->c_lrutail = LEI(e)->lei_lruprev; \
124 #define LRU_ADD( cache, e ) { \
125 LEI(e)->lei_lrunext = cache->c_lruhead; \
126 if ( LEI(e)->lei_lrunext != NULL ) { \
127 LEI(LEI(e)->lei_lrunext)->lei_lruprev = e; \
129 cache->c_lruhead = e; \
130 LEI(e)->lei_lruprev = NULL; \
131 if ( cache->c_lrutail == NULL ) { \
132 cache->c_lrutail = e; \
137 * bdb2i_cache_add_entry_rw - create and lock an entry in the cache
138 * returns: 0 entry has been created and locked
139 * 1 entry already existed
140 * -1 something bad happened
143 bdb2i_cache_add_entry_rw(
152 /* set cache mutex */
153 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
156 assert( e->e_private == NULL );
159 if( cache_entry_private_init(e) != 0 ) {
160 Debug( LDAP_DEBUG_ANY,
161 "====> bdb2i_cache_add_entry( %ld ): \"%s\": private init failed!\n",
162 e->e_id, e->e_dn, 0 );
166 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
167 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
169 Debug( LDAP_DEBUG_TRACE,
170 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
171 e->e_id, e->e_dn, 0 );
173 cache_entry_private_destroy(e);
175 /* free cache mutex */
176 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
181 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
182 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
184 Debug( LDAP_DEBUG_ANY,
185 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in id cache\n",
186 e->e_id, e->e_dn, 0 );
188 /* delete from dn tree inserted above */
189 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
190 (AVL_CMP) entry_dn_cmp ) == NULL )
192 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
196 cache_entry_private_destroy(e);
198 /* free cache mutex */
199 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
203 /* put the entry into 'CREATING' state */
204 /* will be marked after when entry is returned */
205 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
206 LEI(e)->lei_refcnt = 1;
210 if ( ++cache->c_cursize > cache->c_maxsize ) {
212 * find the lru entry not currently in use and delete it.
213 * in case a lot of entries are in use, only look at the
214 * first 10 on the tail of the list.
217 while ( cache->c_lrutail != NULL &&
218 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
221 /* move this in-use entry to the front of the q */
222 ee = cache->c_lrutail;
223 LRU_DELETE( cache, ee );
224 LRU_ADD( cache, ee );
229 * found at least one to delete - try to get back under
230 * the max cache size.
232 while ( cache->c_lrutail != NULL &&
233 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
234 cache->c_cursize > cache->c_maxsize )
236 e = cache->c_lrutail;
238 /* delete from cache and lru q */
239 cache_delete_entry_internal( cache, e );
240 cache_entry_private_destroy( e );
245 /* free cache mutex */
246 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
251 * cache_update_entry - update a LOCKED entry which has been deleted.
252 * returns: 0 entry has been created and locked
253 * 1 entry already existed
254 * -1 something bad happened
257 bdb2i_cache_update_entry(
265 /* set cache mutex */
266 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
269 assert( e->e_private );
272 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
273 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
275 Debug( LDAP_DEBUG_TRACE,
276 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
277 e->e_id, e->e_dn, 0 );
279 /* free cache mutex */
280 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
285 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
286 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
288 Debug( LDAP_DEBUG_ANY,
289 "====> bdb2i_cache_update_entry( %ld ): \"%s\": already in id cache\n",
290 e->e_id, e->e_dn, 0 );
292 /* delete from dn tree inserted above */
293 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
294 (AVL_CMP) entry_dn_cmp ) == NULL )
296 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
300 /* free cache mutex */
301 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
305 /* put the entry into 'CREATING' state */
306 /* will be marked after when entry is returned */
307 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
311 if ( ++cache->c_cursize > cache->c_maxsize ) {
313 * find the lru entry not currently in use and delete it.
314 * in case a lot of entries are in use, only look at the
315 * first 10 on the tail of the list.
318 while ( cache->c_lrutail != NULL &&
319 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
322 /* move this in-use entry to the front of the q */
323 ee = cache->c_lrutail;
324 LRU_DELETE( cache, ee );
325 LRU_ADD( cache, ee );
330 * found at least one to delete - try to get back under
331 * the max cache size.
333 while ( cache->c_lrutail != NULL &&
334 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
335 cache->c_cursize > cache->c_maxsize )
337 e = cache->c_lrutail;
339 /* delete from cache and lru q */
340 cache_delete_entry_internal( cache, e );
341 cache_entry_private_destroy( e );
346 /* free cache mutex */
347 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
352 * bdb2i_cache_find_entry_dn2id - find an entry in the cache, given dn
356 bdb2i_cache_find_entry_dn2id(
366 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
369 /* set cache mutex */
370 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
372 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
373 (AVL_CMP) entry_dn_cmp )) != NULL )
376 * ep now points to an unlocked entry
377 * we do not need to lock the entry if we only
378 * check the state, refcnt, LRU, and id.
382 assert( ep->e_private );
386 * entry is deleted or not fully created yet
388 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
390 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
392 Debug(LDAP_DEBUG_TRACE,
393 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
394 dn, ep->e_id, LEI(ep)->lei_state);
396 /* free cache mutex */
397 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
398 ldap_pvt_thread_yield();
402 Debug(LDAP_DEBUG_TRACE,
403 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld\n",
407 LRU_DELETE( cache, ep );
408 LRU_ADD( cache, ep );
418 /* free cache mutex */
419 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
425 * cache_find_entry_id - find an entry in the cache, given id
429 bdb2i_cache_find_entry_id(
441 /* set cache mutex */
442 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
444 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
445 (AVL_CMP) entry_id_cmp )) != NULL )
448 assert( ep->e_private );
452 * entry is deleted or not fully created yet
454 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
456 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
458 Debug(LDAP_DEBUG_TRACE,
459 "====> bdb2i_cache_find_entry_id( %ld ): %ld (not ready) %d\n",
460 id, ep->e_id, LEI(ep)->lei_state);
462 /* free cache mutex */
463 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
464 ldap_pvt_thread_yield();
468 Debug(LDAP_DEBUG_TRACE,
469 "====> bdb2i_cache_find_entry_id( %ld, %s ) \"%s\" (found)\n",
470 id, rw ? "w" : "r", ep->e_dn);
473 LRU_DELETE( cache, ep );
474 LRU_ADD( cache, ep );
476 LEI(ep)->lei_refcnt++;
478 /* free cache mutex */
479 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
484 /* free cache mutex */
485 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
491 * cache_delete_entry - delete the entry e from the cache. the caller
492 * should have obtained e (increasing its ref count) via a call to one
493 * of the cache_find_* routines. the caller should *not* call the
494 * cache_return_entry() routine prior to calling cache_delete_entry().
495 * it performs this function.
497 * returns: 0 e was deleted ok
498 * 1 e was not in the cache
499 * -1 something bad happened
502 bdb2i_cache_delete_entry(
509 /* set cache mutex */
510 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
513 assert( e->e_private );
516 Debug( LDAP_DEBUG_TRACE, "====> bdb2i_cache_delete_entry( %ld )\n",
519 rc = cache_delete_entry_internal( cache, e );
521 /* free cache mutex */
522 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
527 cache_delete_entry_internal(
532 int rc = 0; /* return code */
535 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
542 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
553 LRU_DELETE( cache, e );
557 * flag entry to be freed later by a call to cache_return_entry()
559 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
567 lru_print( struct cache *cache )
571 fprintf( stderr, "LRU queue (head to tail):\n" );
572 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
573 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
574 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
576 fprintf( stderr, "LRU queue (tail to head):\n" );
577 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
578 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
579 e->e_dn, e->e_id, LEI(e)->lei_refcnt );