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(
362 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
366 /* set cache mutex */
367 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
370 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
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.
383 assert( ep->e_private );
387 * entry is deleted or not fully created yet
389 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
391 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
393 Debug(LDAP_DEBUG_TRACE,
394 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
395 dn, ep->e_id, LEI(ep)->lei_state);
397 /* free cache mutex */
398 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
402 Debug(LDAP_DEBUG_TRACE,
403 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld\n",
407 LRU_DELETE( cache, ep );
408 LRU_ADD( cache, ep );
413 /* free cache mutex */
414 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
421 /* free cache mutex */
422 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
428 * cache_find_entry_id - find an entry in the cache, given id
432 bdb2i_cache_find_entry_id(
443 /* set cache mutex */
444 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
446 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
447 (AVL_CMP) entry_id_cmp )) != NULL )
450 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 "====> bdb2i_cache_find_entry_id( %ld ): %ld (not ready) %d\n",
462 id, ep->e_id, LEI(ep)->lei_state);
464 /* free cache mutex */
465 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
469 Debug(LDAP_DEBUG_TRACE,
470 "====> bdb2i_cache_find_entry_id( %ld, %s ) \"%s\" (found)\n",
471 id, rw ? "w" : "r", ep->e_dn);
474 LRU_DELETE( cache, ep );
475 LRU_ADD( cache, ep );
477 LEI(ep)->lei_refcnt++;
479 /* free cache mutex */
480 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
485 /* free cache mutex */
486 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
492 * cache_delete_entry - delete the entry e from the cache. the caller
493 * should have obtained e (increasing its ref count) via a call to one
494 * of the cache_find_* routines. the caller should *not* call the
495 * cache_return_entry() routine prior to calling cache_delete_entry().
496 * it performs this function.
498 * returns: 0 e was deleted ok
499 * 1 e was not in the cache
500 * -1 something bad happened
503 bdb2i_cache_delete_entry(
510 /* set cache mutex */
511 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
514 assert( e->e_private );
517 Debug( LDAP_DEBUG_TRACE, "====> bdb2i_cache_delete_entry( %ld )\n",
520 rc = cache_delete_entry_internal( cache, e );
522 /* free cache mutex */
523 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
528 cache_delete_entry_internal(
533 int rc = 0; /* return code */
536 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
543 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
554 LRU_DELETE( cache, e );
558 * flag entry to be freed later by a call to cache_return_entry()
560 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
568 lru_print( struct cache *cache )
572 fprintf( stderr, "LRU queue (head to tail):\n" );
573 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
574 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
575 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
577 fprintf( stderr, "LRU queue (tail to head):\n" );
578 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
579 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
580 e->e_dn, e->e_id, LEI(e)->lei_refcnt );