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 assert( e->e_private );
80 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
81 Debug( LDAP_DEBUG_TRACE,
82 "====> bdb2i_cache_return_entry_%s( %ld ): created (%d)\n",
83 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
85 LEI(e)->lei_state = CACHE_ENTRY_READY;
87 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
88 if( LEI(e)->lei_refcnt > 0 ) {
89 Debug( LDAP_DEBUG_TRACE,
90 "====> bdb2i_cache_return_entry_%s( %ld ): delete pending (%d)\n",
91 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
94 Debug( LDAP_DEBUG_TRACE,
95 "====> bdb2i_cache_return_entry_%s( %ld ): deleted (%d)\n",
96 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
98 cache_entry_private_destroy( e );
103 Debug( LDAP_DEBUG_TRACE,
104 "====> bdb2i_cache_return_entry_%s( %ld ): returned (%d)\n",
105 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt);
109 #define LRU_DELETE( cache, e ) { \
110 if ( LEI(e)->lei_lruprev != NULL ) { \
111 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
113 cache->c_lruhead = LEI(e)->lei_lrunext; \
115 if ( LEI(e)->lei_lrunext != NULL ) { \
116 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
118 cache->c_lrutail = LEI(e)->lei_lruprev; \
122 #define LRU_ADD( cache, e ) { \
123 LEI(e)->lei_lrunext = cache->c_lruhead; \
124 if ( LEI(e)->lei_lrunext != NULL ) { \
125 LEI(LEI(e)->lei_lrunext)->lei_lruprev = e; \
127 cache->c_lruhead = e; \
128 LEI(e)->lei_lruprev = NULL; \
129 if ( cache->c_lrutail == NULL ) { \
130 cache->c_lrutail = e; \
135 * bdb2i_cache_add_entry_rw - create and lock an entry in the cache
136 * returns: 0 entry has been created and locked
137 * 1 entry already existed
138 * -1 something bad happened
141 bdb2i_cache_add_entry_rw(
151 assert( e->e_private == NULL );
154 if( cache_entry_private_init(e) != 0 ) {
155 Debug( LDAP_DEBUG_ANY,
156 "====> bdb2i_cache_add_entry( %ld ): \"%s\": private init failed!\n",
157 e->e_id, e->e_dn, 0 );
161 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
162 entry_dn_cmp, avl_dup_error ) != 0 )
164 Debug( LDAP_DEBUG_TRACE,
165 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
166 e->e_id, e->e_dn, 0 );
168 cache_entry_private_destroy(e);
174 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
175 entry_id_cmp, avl_dup_error ) != 0 )
177 Debug( LDAP_DEBUG_ANY,
178 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in id cache\n",
179 e->e_id, e->e_dn, 0 );
181 /* delete from dn tree inserted above */
182 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
183 entry_dn_cmp ) == NULL )
185 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
189 cache_entry_private_destroy(e);
194 /* put the entry into 'CREATING' state */
195 /* will be marked after when entry is returned */
196 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
197 LEI(e)->lei_refcnt = 1;
201 if ( ++cache->c_cursize > cache->c_maxsize ) {
203 * find the lru entry not currently in use and delete it.
204 * in case a lot of entries are in use, only look at the
205 * first 10 on the tail of the list.
208 while ( cache->c_lrutail != NULL &&
209 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
212 /* move this in-use entry to the front of the q */
213 ee = cache->c_lrutail;
214 LRU_DELETE( cache, ee );
215 LRU_ADD( cache, ee );
220 * found at least one to delete - try to get back under
221 * the max cache size.
223 while ( cache->c_lrutail != NULL &&
224 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
225 cache->c_cursize > cache->c_maxsize )
227 e = cache->c_lrutail;
229 /* delete from cache and lru q */
230 cache_delete_entry_internal( cache, e );
231 cache_entry_private_destroy( e );
240 * cache_update_entry - update a LOCKED entry which has been deleted.
241 * returns: 0 entry has been created and locked
242 * 1 entry already existed
243 * -1 something bad happened
246 bdb2i_cache_update_entry(
255 assert( e->e_private );
258 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
259 entry_dn_cmp, avl_dup_error ) != 0 )
261 Debug( LDAP_DEBUG_TRACE,
262 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
263 e->e_id, e->e_dn, 0 );
269 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
270 entry_id_cmp, avl_dup_error ) != 0 )
272 Debug( LDAP_DEBUG_ANY,
273 "====> bdb2i_cache_update_entry( %ld ): \"%s\": already in id cache\n",
274 e->e_id, e->e_dn, 0 );
276 /* delete from dn tree inserted above */
277 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
278 entry_dn_cmp ) == NULL )
280 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
287 /* put the entry into 'CREATING' state */
288 /* will be marked after when entry is returned */
289 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
293 if ( ++cache->c_cursize > cache->c_maxsize ) {
295 * find the lru entry not currently in use and delete it.
296 * in case a lot of entries are in use, only look at the
297 * first 10 on the tail of the list.
300 while ( cache->c_lrutail != NULL &&
301 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
304 /* move this in-use entry to the front of the q */
305 ee = cache->c_lrutail;
306 LRU_DELETE( cache, ee );
307 LRU_ADD( cache, ee );
312 * found at least one to delete - try to get back under
313 * the max cache size.
315 while ( cache->c_lrutail != NULL &&
316 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
317 cache->c_cursize > cache->c_maxsize )
319 e = cache->c_lrutail;
321 /* delete from cache and lru q */
322 cache_delete_entry_internal( cache, e );
323 cache_entry_private_destroy( e );
332 * cache_find_entry_dn2id - find an entry in the cache, given dn
336 bdb2i_cache_find_entry_dn2id(
342 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
347 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
349 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
350 entry_dn_cmp )) != NULL )
353 * ep now points to an unlocked entry
354 * we do not need to lock the entry if we only
355 * check the state, refcnt, LRU, and id.
360 assert( ep->e_private );
364 * entry is deleted or not fully created yet
366 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
368 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
370 Debug(LDAP_DEBUG_TRACE,
371 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
372 dn, ep->e_id, LEI(ep)->lei_state);
377 Debug(LDAP_DEBUG_TRACE,
378 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld\n",
382 LRU_DELETE( cache, ep );
383 LRU_ADD( cache, ep );
397 * cache_find_entry_id - find an entry in the cache, given id
401 bdb2i_cache_find_entry_id(
413 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
414 entry_id_cmp )) != NULL )
417 assert( ep->e_private );
421 * entry is deleted or not fully created yet
423 if ( LEI(ep)->lei_state != CACHE_ENTRY_READY ) {
425 assert(LEI(ep)->lei_state != CACHE_ENTRY_UNDEFINED);
427 Debug(LDAP_DEBUG_TRACE,
428 "====> bdb2i_cache_find_entry_id( %ld ): %ld (not ready) %d\n",
429 id, ep->e_id, LEI(ep)->lei_state);
434 Debug(LDAP_DEBUG_TRACE,
435 "====> bdb2i_cache_find_entry_id( %ld, %s ) \"%s\" (found)\n",
436 id, rw ? "w" : "r", ep->e_dn);
439 LRU_DELETE( cache, ep );
440 LRU_ADD( cache, ep );
442 LEI(ep)->lei_refcnt++;
451 * cache_delete_entry - delete the entry e from the cache. the caller
452 * should have obtained e (increasing its ref count) via a call to one
453 * of the cache_find_* routines. the caller should *not* call the
454 * cache_return_entry() routine prior to calling cache_delete_entry().
455 * it performs this function.
457 * returns: 0 e was deleted ok
458 * 1 e was not in the cache
459 * -1 something bad happened
462 bdb2i_cache_delete_entry(
470 assert( e->e_private );
473 Debug( LDAP_DEBUG_TRACE, "====> bdb2i_cache_delete_entry( %ld )\n",
476 rc = cache_delete_entry_internal( cache, e );
482 cache_delete_entry_internal(
487 int rc = 0; /* return code */
490 if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp )
497 if ( avl_delete( &cache->c_idtree, (caddr_t) e, entry_id_cmp )
508 LRU_DELETE( cache, e );
512 * flag entry to be freed later by a call to cache_return_entry()
514 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
522 lru_print( struct cache *cache )
526 fprintf( stderr, "LRU queue (head to tail):\n" );
527 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
528 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
529 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
531 fprintf( stderr, "LRU queue (tail to head):\n" );
532 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
533 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
534 e->e_dn, e->e_id, LEI(e)->lei_refcnt );