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 )
41 struct ldbm_entry_info *lei;
44 assert( e->e_private == NULL );
47 if( e->e_private != NULL ) {
48 /* this should never happen */
52 e->e_private = ch_calloc(1, sizeof(struct ldbm_entry_info));
58 cache_entry_private_destroy( Entry*e )
60 struct ldbm_entry_info *lei;
63 assert( e->e_private );
72 bdb2i_cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
75 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
78 assert( e->e_private );
83 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
84 Debug( LDAP_DEBUG_TRACE,
85 "====> bdb2i_cache_return_entry_%s( %ld ): created (%d)\n",
86 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
88 LEI(e)->lei_state = CACHE_ENTRY_READY;
90 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
91 if( LEI(e)->lei_refcnt > 0 ) {
92 Debug( LDAP_DEBUG_TRACE,
93 "====> bdb2i_cache_return_entry_%s( %ld ): delete pending (%d)\n",
94 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
97 Debug( LDAP_DEBUG_TRACE,
98 "====> bdb2i_cache_return_entry_%s( %ld ): deleted (%d)\n",
99 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
101 cache_entry_private_destroy( e );
106 Debug( LDAP_DEBUG_TRACE,
107 "====> bdb2i_cache_return_entry_%s( %ld ): returned (%d)\n",
108 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt);
111 /* free cache mutex */
112 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
115 #define LRU_DELETE( cache, e ) { \
116 if ( LEI(e)->lei_lruprev != NULL ) { \
117 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
119 cache->c_lruhead = LEI(e)->lei_lrunext; \
121 if ( LEI(e)->lei_lrunext != NULL ) { \
122 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
124 cache->c_lrutail = LEI(e)->lei_lruprev; \
128 #define LRU_ADD( cache, e ) { \
129 LEI(e)->lei_lrunext = cache->c_lruhead; \
130 if ( LEI(e)->lei_lrunext != NULL ) { \
131 LEI(LEI(e)->lei_lrunext)->lei_lruprev = e; \
133 cache->c_lruhead = e; \
134 LEI(e)->lei_lruprev = NULL; \
135 if ( cache->c_lrutail == NULL ) { \
136 cache->c_lrutail = e; \
141 * bdb2i_cache_add_entry_rw - create and lock an entry in the cache
142 * returns: 0 entry has been created and locked
143 * 1 entry already existed
144 * -1 something bad happened
147 bdb2i_cache_add_entry_rw(
156 /* set cache mutex */
157 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
160 assert( e->e_private == NULL );
163 if( cache_entry_private_init(e) != 0 ) {
164 Debug( LDAP_DEBUG_ANY,
165 "====> bdb2i_cache_add_entry( %ld ): \"%s\": private init failed!\n",
166 e->e_id, e->e_dn, 0 );
170 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
171 entry_dn_cmp, avl_dup_error ) != 0 )
173 Debug( LDAP_DEBUG_TRACE,
174 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
175 e->e_id, e->e_dn, 0 );
177 cache_entry_private_destroy(e);
179 /* free cache mutex */
180 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
185 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
186 entry_id_cmp, avl_dup_error ) != 0 )
188 Debug( LDAP_DEBUG_ANY,
189 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in id cache\n",
190 e->e_id, e->e_dn, 0 );
192 /* delete from dn tree inserted above */
193 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
194 entry_dn_cmp ) == NULL )
196 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
200 cache_entry_private_destroy(e);
202 /* free cache mutex */
203 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
207 /* put the entry into 'CREATING' state */
208 /* will be marked after when entry is returned */
209 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
210 LEI(e)->lei_refcnt = 1;
214 if ( ++cache->c_cursize > cache->c_maxsize ) {
216 * find the lru entry not currently in use and delete it.
217 * in case a lot of entries are in use, only look at the
218 * first 10 on the tail of the list.
221 while ( cache->c_lrutail != NULL &&
222 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
225 /* move this in-use entry to the front of the q */
226 ee = cache->c_lrutail;
227 LRU_DELETE( cache, ee );
228 LRU_ADD( cache, ee );
233 * found at least one to delete - try to get back under
234 * the max cache size.
236 while ( cache->c_lrutail != NULL &&
237 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
238 cache->c_cursize > cache->c_maxsize )
240 e = cache->c_lrutail;
242 /* delete from cache and lru q */
243 cache_delete_entry_internal( cache, e );
244 cache_entry_private_destroy( e );
249 /* free cache mutex */
250 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
255 * cache_update_entry - update a LOCKED entry which has been deleted.
256 * returns: 0 entry has been created and locked
257 * 1 entry already existed
258 * -1 something bad happened
261 bdb2i_cache_update_entry(
269 /* set cache mutex */
270 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
273 assert( e->e_private );
276 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
277 entry_dn_cmp, avl_dup_error ) != 0 )
279 Debug( LDAP_DEBUG_TRACE,
280 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
281 e->e_id, e->e_dn, 0 );
283 /* free cache mutex */
284 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
289 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
290 entry_id_cmp, avl_dup_error ) != 0 )
292 Debug( LDAP_DEBUG_ANY,
293 "====> bdb2i_cache_update_entry( %ld ): \"%s\": already in id cache\n",
294 e->e_id, e->e_dn, 0 );
296 /* delete from dn tree inserted above */
297 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
298 entry_dn_cmp ) == NULL )
300 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
304 /* free cache mutex */
305 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
309 /* put the entry into 'CREATING' state */
310 /* will be marked after when entry is returned */
311 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
315 if ( ++cache->c_cursize > cache->c_maxsize ) {
317 * find the lru entry not currently in use and delete it.
318 * in case a lot of entries are in use, only look at the
319 * first 10 on the tail of the list.
322 while ( cache->c_lrutail != NULL &&
323 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
326 /* move this in-use entry to the front of the q */
327 ee = cache->c_lrutail;
328 LRU_DELETE( cache, ee );
329 LRU_ADD( cache, ee );
334 * found at least one to delete - try to get back under
335 * the max cache size.
337 while ( cache->c_lrutail != NULL &&
338 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
339 cache->c_cursize > cache->c_maxsize )
341 e = cache->c_lrutail;
343 /* delete from cache and lru q */
344 cache_delete_entry_internal( cache, e );
345 cache_entry_private_destroy( e );
350 /* free cache mutex */
351 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
356 * bdb2i_cache_find_entry_dn2id - find an entry in the cache, given dn
360 bdb2i_cache_find_entry_dn2id(
366 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
370 /* set cache mutex */
371 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
374 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
376 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
377 entry_dn_cmp )) != NULL )
380 * ep now points to an unlocked entry
381 * we do not need to lock the entry if we only
382 * check the state, refcnt, LRU, and id.
387 assert( ep->e_private );
391 * entry is deleted or not fully created yet
393 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
395 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
397 Debug(LDAP_DEBUG_TRACE,
398 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
399 dn, ep->e_id, LEI(ep)->lei_state);
401 /* free cache mutex */
402 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
406 Debug(LDAP_DEBUG_TRACE,
407 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld\n",
411 LRU_DELETE( cache, ep );
412 LRU_ADD( cache, ep );
417 /* free cache mutex */
418 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
425 /* free cache mutex */
426 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
432 * cache_find_entry_id - find an entry in the cache, given id
436 bdb2i_cache_find_entry_id(
448 /* set cache mutex */
449 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
451 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
452 entry_id_cmp )) != NULL )
455 assert( ep->e_private );
459 * entry is deleted or not fully created yet
461 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
463 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
465 Debug(LDAP_DEBUG_TRACE,
466 "====> bdb2i_cache_find_entry_id( %ld ): %ld (not ready) %d\n",
467 id, ep->e_id, LEI(ep)->lei_state);
469 /* free cache mutex */
470 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
474 Debug(LDAP_DEBUG_TRACE,
475 "====> bdb2i_cache_find_entry_id( %ld, %s ) \"%s\" (found)\n",
476 id, rw ? "w" : "r", ep->e_dn);
479 LRU_DELETE( cache, ep );
480 LRU_ADD( cache, ep );
482 LEI(ep)->lei_refcnt++;
484 /* free cache mutex */
485 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
490 /* free cache mutex */
491 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
497 * cache_delete_entry - delete the entry e from the cache. the caller
498 * should have obtained e (increasing its ref count) via a call to one
499 * of the cache_find_* routines. the caller should *not* call the
500 * cache_return_entry() routine prior to calling cache_delete_entry().
501 * it performs this function.
503 * returns: 0 e was deleted ok
504 * 1 e was not in the cache
505 * -1 something bad happened
508 bdb2i_cache_delete_entry(
515 /* set cache mutex */
516 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
519 assert( e->e_private );
522 Debug( LDAP_DEBUG_TRACE, "====> bdb2i_cache_delete_entry( %ld )\n",
525 rc = cache_delete_entry_internal( cache, e );
527 /* free cache mutex */
528 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
533 cache_delete_entry_internal(
538 int rc = 0; /* return code */
541 if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp )
548 if ( avl_delete( &cache->c_idtree, (caddr_t) e, entry_id_cmp )
559 LRU_DELETE( cache, e );
563 * flag entry to be freed later by a call to cache_return_entry()
565 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
573 lru_print( struct cache *cache )
577 fprintf( stderr, "LRU queue (head to tail):\n" );
578 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
579 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
580 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
582 fprintf( stderr, "LRU queue (tail to head):\n" );
583 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
584 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
585 e->e_dn, e->e_id, LEI(e)->lei_refcnt );