1 /* cache.c - routines to maintain an in-core cache of entries */
13 #include "back-bdb2.h"
15 static int cache_delete_entry_internal(struct cache *cache, Entry *e);
17 static void lru_print(struct cache *cache);
21 * the cache has three entry points (ways to find things):
23 * by entry e.g., if you already have an entry from the cache
24 * and want to delete it. (really by entry ptr)
25 * by dn e.g., when looking for the base object of a search
26 * by id e.g., for search candidates
28 * these correspond to three different avl trees that are maintained.
30 #define cache_entry_cmp entry_cmp
31 #define cache_entrydn_cmp entry_dn_cmp
32 #define cache_entryid_cmp entry_id_cmp
35 bdb2i_cache_set_state( struct cache *cache, Entry *e, int state )
38 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
42 /* free cache mutex */
43 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
48 cache_return_entry( struct cache *cache, Entry *e )
51 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
53 if ( --e->e_refcnt == 0 && e->e_state == ENTRY_STATE_DELETED ) {
57 /* free cache mutex */
58 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
63 cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
65 Debug( LDAP_DEBUG_TRACE, "====> cache_return_entry_%s\n",
66 rw ? "w" : "r", 0, 0);
69 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
71 entry_rdwr_unlock(e, rw);
73 if ( --e->e_refcnt == 0 && e->e_state == ENTRY_STATE_DELETED ) {
77 /* free cache mutex */
78 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
82 bdb2i_cache_return_entry_r( struct cache *cache, Entry *e )
84 cache_return_entry_rw(cache, e, 0);
88 bdb2i_cache_return_entry_w( struct cache *cache, Entry *e )
90 cache_return_entry_rw(cache, e, 1);
94 #define LRU_DELETE( cache, e ) { \
95 if ( e->e_lruprev != NULL ) { \
96 e->e_lruprev->e_lrunext = e->e_lrunext; \
98 cache->c_lruhead = e->e_lrunext; \
100 if ( e->e_lrunext != NULL ) { \
101 e->e_lrunext->e_lruprev = e->e_lruprev; \
103 cache->c_lrutail = e->e_lruprev; \
107 #define LRU_ADD( cache, e ) { \
108 e->e_lrunext = cache->c_lruhead; \
109 if ( e->e_lrunext != NULL ) { \
110 e->e_lrunext->e_lruprev = e; \
112 cache->c_lruhead = e; \
113 e->e_lruprev = NULL; \
114 if ( cache->c_lrutail == NULL ) { \
115 cache->c_lrutail = e; \
120 * cache_create_entry_lock - create an entry in the cache, and lock it.
121 * returns: 0 entry has been created and locked
122 * 1 entry already existed
123 * -1 something bad happened
126 bdb2i_cache_add_entry_lock(
135 /* set cache mutex */
136 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
138 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
139 cache_entrydn_cmp, avl_dup_error ) != 0 )
141 Debug( LDAP_DEBUG_TRACE,
142 "====> cache_add_entry lock: entry %20s id %lu already in dn cache\n",
143 e->e_dn, e->e_id, 0 );
145 /* free cache mutex */
146 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
151 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
152 cache_entryid_cmp, avl_dup_error ) != 0 )
154 Debug( LDAP_DEBUG_ANY,
155 "====> entry %20s id %lu already in id cache\n",
156 e->e_dn, e->e_id, 0 );
158 /* delete from dn tree inserted above */
159 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
160 cache_entrydn_cmp ) == NULL )
162 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
166 /* free cache mutex */
167 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
176 if ( ++cache->c_cursize > cache->c_maxsize ) {
178 * find the lru entry not currently in use and delete it.
179 * in case a lot of entries are in use, only look at the
180 * first 10 on the tail of the list.
183 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
185 /* move this in-use entry to the front of the q */
186 ee = cache->c_lrutail;
187 LRU_DELETE( cache, ee );
188 LRU_ADD( cache, ee );
193 * found at least one to delete - try to get back under
194 * the max cache size.
196 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
197 == 0 && cache->c_cursize > cache->c_maxsize ) {
198 e = cache->c_lrutail;
200 /* check for active readers/writer lock */
202 assert(!ldap_pvt_thread_rdwr_active( &e->e_rdwr ));
205 /* delete from cache and lru q */
206 rc = cache_delete_entry_internal( cache, e );
212 /* free cache mutex */
213 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
218 * cache_find_entry_dn2id - find an entry in the cache, given dn
222 bdb2i_cache_find_entry_dn2id(
228 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
232 /* set cache mutex */
233 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
236 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
238 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
239 cache_entrydn_cmp )) != NULL )
242 * ep now points to an unlocked entry
243 * we do not need to lock the entry if we only
244 * check the state, refcnt, LRU, and id.
248 Debug(LDAP_DEBUG_TRACE, "====> cache_find_entry_dn2id: found dn: %s\n",
252 * entry is deleted or not fully created yet
254 if ( ep->e_state == ENTRY_STATE_DELETED ||
255 ep->e_state == ENTRY_STATE_CREATING )
257 /* free cache mutex */
258 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
263 LRU_DELETE( cache, ep );
264 LRU_ADD( cache, ep );
269 /* free cache mutex */
270 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
277 /* free cache mutex */
278 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
284 * cache_find_entry_id - find an entry in the cache, given id
288 bdb2i_cache_find_entry_id(
300 /* set cache mutex */
301 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
303 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
304 cache_entryid_cmp )) != NULL )
306 Debug(LDAP_DEBUG_TRACE,
307 "====> cache_find_entry_dn2id: found id: %ld rw: %d\n",
311 * entry is deleted or not fully created yet
313 if ( ep->e_state == ENTRY_STATE_DELETED ||
314 ep->e_state == ENTRY_STATE_CREATING )
316 /* free cache mutex */
317 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
321 /* acquire reader lock */
322 if ( entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
323 /* could not acquire entry lock...
324 * owner cannot free as we have the cache locked.
325 * so, unlock the cache, yield, and try again.
328 /* free cache mutex */
329 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
330 ldap_pvt_thread_yield();
335 LRU_DELETE( cache, ep );
336 LRU_ADD( cache, ep );
340 /* free cache mutex */
341 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
346 /* free cache mutex */
347 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
353 * cache_delete_entry - delete the entry e from the cache. the caller
354 * should have obtained e (increasing its ref count) via a call to one
355 * of the cache_find_* routines. the caller should *not* call the
356 * cache_return_entry() routine prior to calling cache_delete_entry().
357 * it performs this function.
359 * returns: 0 e was deleted ok
360 * 1 e was not in the cache
361 * -1 something bad happened
364 bdb2i_cache_delete_entry(
371 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry:\n", 0, 0, 0 );
373 /* set cache mutex */
374 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
376 /* XXX check for writer lock - should also check no readers pending */
378 assert(ldap_pvt_thread_rdwr_writers( &e->e_rdwr ) == 1);
381 rc = cache_delete_entry_internal( cache, e );
383 /* free cache mutex */
384 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
389 cache_delete_entry_internal(
394 int rc = 0; /* return code */
397 if ( avl_delete( &cache->c_dntree, (caddr_t) e, cache_entrydn_cmp )
404 if ( avl_delete( &cache->c_idtree, (caddr_t) e, cache_entryid_cmp )
415 LRU_DELETE( cache, e );
419 * flag entry to be freed later by a call to cache_return_entry()
421 e->e_state = ENTRY_STATE_DELETED;
429 lru_print( struct cache *cache )
433 fprintf( stderr, "LRU queue (head to tail):\n" );
434 for ( e = cache->c_lruhead; e != NULL; e = e->e_lrunext ) {
435 fprintf( stderr, "\tdn %20s id %lu refcnt %d\n", e->e_dn,
436 e->e_id, e->e_refcnt );
438 fprintf( stderr, "LRU queue (tail to head):\n" );
439 for ( e = cache->c_lrutail; e != NULL; e = e->e_lruprev ) {
440 fprintf( stderr, "\tdn %20s id %lu refcnt %d\n", e->e_dn,
441 e->e_id, e->e_refcnt );