1 /* cache.c - routines to maintain an in-core cache of entries */
10 #include <ac/socket.h>
14 #include "back-bdb2.h"
16 /* LDBM backend specific entry info -- visible only to the cache */
17 struct ldbm_entry_info {
19 * These items are specific to the LDBM backend and should
22 int lei_state; /* for the cache */
23 #define CACHE_ENTRY_UNDEFINED 0
24 #define CACHE_ENTRY_CREATING 1
25 #define CACHE_ENTRY_READY 2
26 #define CACHE_ENTRY_DELETED 3
28 int lei_refcnt; /* # threads ref'ing this entry */
29 Entry *lei_lrunext; /* for cache lru list */
32 #define LEI(e) ((struct ldbm_entry_info *) ((e)->e_private))
34 static int cache_delete_entry_internal(struct cache *cache, Entry *e);
36 static void lru_print(struct cache *cache);
40 cache_entry_private_init( Entry*e )
42 assert( e->e_private == NULL );
44 if( e->e_private != NULL ) {
45 /* this should never happen */
49 e->e_private = ch_calloc(1, sizeof(struct ldbm_entry_info));
55 cache_entry_private_destroy( Entry*e )
57 assert( e->e_private );
65 bdb2i_cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
68 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
70 assert( e->e_private );
74 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
75 Debug( LDAP_DEBUG_TRACE,
76 "====> bdb2i_cache_return_entry_%s( %ld ): created (%d)\n",
77 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
79 LEI(e)->lei_state = CACHE_ENTRY_READY;
81 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
82 if( LEI(e)->lei_refcnt > 0 ) {
83 Debug( LDAP_DEBUG_TRACE,
84 "====> bdb2i_cache_return_entry_%s( %ld ): delete pending (%d)\n",
85 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
88 Debug( LDAP_DEBUG_TRACE,
89 "====> bdb2i_cache_return_entry_%s( %ld ): deleted (%d)\n",
90 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt );
92 cache_entry_private_destroy( e );
97 Debug( LDAP_DEBUG_TRACE,
98 "====> bdb2i_cache_return_entry_%s( %ld ): returned (%d)\n",
99 rw ? "w" : "r", e->e_id, LEI(e)->lei_refcnt);
102 /* free cache mutex */
103 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
106 #define LRU_DELETE( cache, e ) { \
107 if ( LEI(e)->lei_lruprev != NULL ) { \
108 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
110 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
112 if ( LEI(e)->lei_lrunext != NULL ) { \
113 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
115 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
119 #define LRU_ADD( cache, e ) { \
120 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
121 if ( LEI(e)->lei_lrunext != NULL ) { \
122 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
124 (cache)->c_lruhead = (e); \
125 LEI(e)->lei_lruprev = NULL; \
126 if ( (cache)->c_lrutail == NULL ) { \
127 (cache)->c_lrutail = (e); \
132 * bdb2i_cache_add_entry_rw - create and lock an entry in the cache
133 * returns: 0 entry has been created and locked
134 * 1 entry already existed
135 * -1 something bad happened
138 bdb2i_cache_add_entry_rw(
147 /* set cache mutex */
148 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
150 assert( e->e_private == NULL );
152 if( cache_entry_private_init(e) != 0 ) {
153 /* free cache mutex */
154 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
156 Debug( LDAP_DEBUG_ANY,
157 "====> bdb2i_cache_add_entry( %ld ): \"%s\": private init failed!\n",
158 e->e_id, e->e_dn, 0 );
163 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
164 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
166 Debug( LDAP_DEBUG_TRACE,
167 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
168 e->e_id, e->e_dn, 0 );
170 cache_entry_private_destroy(e);
172 /* free cache mutex */
173 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
178 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
179 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
181 Debug( LDAP_DEBUG_ANY,
182 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in id cache\n",
183 e->e_id, e->e_dn, 0 );
185 /* delete from dn tree inserted above */
186 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
187 (AVL_CMP) entry_dn_cmp ) == NULL )
189 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
193 cache_entry_private_destroy(e);
195 /* free cache mutex */
196 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
200 /* put the entry into 'CREATING' state */
201 /* will be marked after when entry is returned */
202 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
203 LEI(e)->lei_refcnt = 1;
207 if ( ++cache->c_cursize > cache->c_maxsize ) {
209 * find the lru entry not currently in use and delete it.
210 * in case a lot of entries are in use, only look at the
211 * first 10 on the tail of the list.
214 while ( cache->c_lrutail != NULL &&
215 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
218 /* move this in-use entry to the front of the q */
219 ee = cache->c_lrutail;
220 LRU_DELETE( cache, ee );
221 LRU_ADD( cache, ee );
226 * found at least one to delete - try to get back under
227 * the max cache size.
229 while ( cache->c_lrutail != NULL &&
230 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
231 cache->c_cursize > cache->c_maxsize )
233 e = cache->c_lrutail;
235 /* delete from cache and lru q */
236 cache_delete_entry_internal( cache, e );
237 cache_entry_private_destroy( e );
242 /* free cache mutex */
243 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
248 * cache_update_entry - update a LOCKED entry which has been deleted.
249 * returns: 0 entry has been created and locked
250 * 1 entry already existed
251 * -1 something bad happened
254 bdb2i_cache_update_entry(
262 /* set cache mutex */
263 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
265 assert( e->e_private );
267 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
268 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
270 Debug( LDAP_DEBUG_TRACE,
271 "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
272 e->e_id, e->e_dn, 0 );
274 /* free cache mutex */
275 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
280 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
281 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
283 Debug( LDAP_DEBUG_ANY,
284 "====> bdb2i_cache_update_entry( %ld ): \"%s\": already in id cache\n",
285 e->e_id, e->e_dn, 0 );
287 /* delete from dn tree inserted above */
288 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
289 (AVL_CMP) entry_dn_cmp ) == NULL )
291 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
295 /* free cache mutex */
296 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
300 /* put the entry into 'CREATING' state */
301 /* will be marked after when entry is returned */
302 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
306 if ( ++cache->c_cursize > cache->c_maxsize ) {
308 * find the lru entry not currently in use and delete it.
309 * in case a lot of entries are in use, only look at the
310 * first 10 on the tail of the list.
313 while ( cache->c_lrutail != NULL &&
314 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
317 /* move this in-use entry to the front of the q */
318 ee = cache->c_lrutail;
319 LRU_DELETE( cache, ee );
320 LRU_ADD( cache, ee );
325 * found at least one to delete - try to get back under
326 * the max cache size.
328 while ( cache->c_lrutail != NULL &&
329 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
330 cache->c_cursize > cache->c_maxsize )
332 e = cache->c_lrutail;
334 /* delete from cache and lru q */
335 cache_delete_entry_internal( cache, e );
336 cache_entry_private_destroy( e );
341 /* free cache mutex */
342 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
347 * bdb2i_cache_find_entry_dn2id - find an entry in the cache, given dn
351 bdb2i_cache_find_entry_dn2id(
361 e.e_dn = (char *) dn;
362 e.e_ndn = ch_strdup( dn );
363 (void) dn_normalize( e.e_ndn );
366 /* set cache mutex */
367 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
369 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
370 (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.
381 assert( ep->e_private );
385 state = LEI(ep)->lei_state;
388 * entry is deleted or not fully created yet
390 if ( state != CACHE_ENTRY_READY ) {
391 assert(state != CACHE_ENTRY_UNDEFINED);
393 /* free cache mutex */
394 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
396 Debug(LDAP_DEBUG_TRACE,
397 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
400 ldap_pvt_thread_yield();
405 LRU_DELETE( cache, ep );
406 LRU_ADD( cache, ep );
408 /* free cache mutex */
409 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
411 Debug(LDAP_DEBUG_TRACE,
412 "====> bdb2i_cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
416 /* free cache mutex */
417 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(
445 /* set cache mutex */
446 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
448 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
449 (AVL_CMP) entry_id_cmp )) != NULL )
453 assert( ep->e_private );
455 state = LEI(ep)->lei_state;
458 * entry is deleted or not fully created yet
460 if ( state != CACHE_ENTRY_READY ) {
463 assert(state != CACHE_ENTRY_UNDEFINED);
465 /* free cache mutex */
466 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
468 Debug(LDAP_DEBUG_TRACE,
469 "====> bdb2i_cache_find_entry_id( %ld ): %ld (not ready) %d\n",
472 ldap_pvt_thread_yield();
477 LRU_DELETE( cache, ep );
478 LRU_ADD( cache, ep );
480 LEI(ep)->lei_refcnt++;
482 /* free cache mutex */
483 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
485 Debug(LDAP_DEBUG_TRACE,
486 "====> bdb2i_cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
487 id, ep->e_dn, count);
492 /* free cache mutex */
493 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
499 * cache_delete_entry - delete the entry e from the cache. the caller
500 * should have obtained e (increasing its ref count) via a call to one
501 * of the cache_find_* routines. the caller should *not* call the
502 * cache_return_entry() routine prior to calling cache_delete_entry().
503 * it performs this function.
505 * returns: 0 e was deleted ok
506 * 1 e was not in the cache
507 * -1 something bad happened
510 bdb2i_cache_delete_entry(
517 /* set cache mutex */
518 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
520 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, (AVL_CMP) entry_dn_cmp )
548 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) 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 );