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 bdb2i_cache_set_state( struct cache *cache, Entry *e, int state )
24 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
28 /* free cache mutex */
29 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
34 cache_return_entry( struct cache *cache, Entry *e )
37 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
39 if ( --e->e_refcnt == 0 && e->e_state == ENTRY_STATE_DELETED ) {
43 /* free cache mutex */
44 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
49 cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
51 Debug( LDAP_DEBUG_TRACE, "====> cache_return_entry_%s\n",
52 rw ? "w" : "r", 0, 0);
55 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
57 entry_rdwr_unlock(e, rw);
59 if ( --e->e_refcnt == 0 && e->e_state == ENTRY_STATE_DELETED ) {
63 /* free cache mutex */
64 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
68 bdb2i_cache_return_entry_r( struct cache *cache, Entry *e )
70 cache_return_entry_rw(cache, e, 0);
74 bdb2i_cache_return_entry_w( struct cache *cache, Entry *e )
76 cache_return_entry_rw(cache, e, 1);
80 #define LRU_DELETE( cache, e ) { \
81 if ( e->e_lruprev != NULL ) { \
82 e->e_lruprev->e_lrunext = e->e_lrunext; \
84 cache->c_lruhead = e->e_lrunext; \
86 if ( e->e_lrunext != NULL ) { \
87 e->e_lrunext->e_lruprev = e->e_lruprev; \
89 cache->c_lrutail = e->e_lruprev; \
93 #define LRU_ADD( cache, e ) { \
94 e->e_lrunext = cache->c_lruhead; \
95 if ( e->e_lrunext != NULL ) { \
96 e->e_lrunext->e_lruprev = e; \
98 cache->c_lruhead = e; \
99 e->e_lruprev = NULL; \
100 if ( cache->c_lrutail == NULL ) { \
101 cache->c_lrutail = e; \
106 * cache_create_entry_lock - create an entry in the cache, and lock it.
107 * returns: 0 entry has been created and locked
108 * 1 entry already existed
109 * -1 something bad happened
112 bdb2i_cache_add_entry_lock(
121 /* set cache mutex */
122 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
124 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
125 entry_dn_cmp, avl_dup_error ) != 0 )
127 Debug( LDAP_DEBUG_TRACE,
128 "====> cache_add_entry lock: entry %20s id %lu already in dn cache\n",
129 e->e_dn, e->e_id, 0 );
131 /* free cache mutex */
132 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
137 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
138 entry_id_cmp, avl_dup_error ) != 0 )
140 Debug( LDAP_DEBUG_ANY,
141 "====> entry %20s id %lu already in id cache\n",
142 e->e_dn, e->e_id, 0 );
144 /* delete from dn tree inserted above */
145 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
146 entry_dn_cmp ) == NULL )
148 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
152 /* free cache mutex */
153 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
162 if ( ++cache->c_cursize > cache->c_maxsize ) {
164 * find the lru entry not currently in use and delete it.
165 * in case a lot of entries are in use, only look at the
166 * first 10 on the tail of the list.
169 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
171 /* move this in-use entry to the front of the q */
172 ee = cache->c_lrutail;
173 LRU_DELETE( cache, ee );
174 LRU_ADD( cache, ee );
179 * found at least one to delete - try to get back under
180 * the max cache size.
182 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
183 == 0 && cache->c_cursize > cache->c_maxsize ) {
184 e = cache->c_lrutail;
186 /* check for active readers/writer lock */
188 assert(!ldap_pvt_thread_rdwr_active( &e->e_rdwr ));
191 /* delete from cache and lru q */
192 rc = cache_delete_entry_internal( cache, e );
198 /* free cache mutex */
199 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
204 * cache_find_entry_dn2id - find an entry in the cache, given dn
208 bdb2i_cache_find_entry_dn2id(
214 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
218 /* set cache mutex */
219 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
222 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
224 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
225 entry_dn_cmp )) != NULL )
228 * ep now points to an unlocked entry
229 * we do not need to lock the entry if we only
230 * check the state, refcnt, LRU, and id.
234 Debug(LDAP_DEBUG_TRACE, "====> cache_find_entry_dn2id: found dn: %s\n",
238 * entry is deleted or not fully created yet
240 if ( ep->e_state == ENTRY_STATE_DELETED ||
241 ep->e_state == ENTRY_STATE_CREATING )
243 /* free cache mutex */
244 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
249 LRU_DELETE( cache, ep );
250 LRU_ADD( cache, ep );
255 /* free cache mutex */
256 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
263 /* free cache mutex */
264 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
270 * cache_find_entry_id - find an entry in the cache, given id
274 bdb2i_cache_find_entry_id(
286 /* set cache mutex */
287 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
289 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
290 entry_id_cmp )) != NULL )
292 Debug(LDAP_DEBUG_TRACE,
293 "====> cache_find_entry_dn2id: found id: %ld rw: %d\n",
297 * entry is deleted or not fully created yet
299 if ( ep->e_state == ENTRY_STATE_DELETED ||
300 ep->e_state == ENTRY_STATE_CREATING )
302 /* free cache mutex */
303 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
307 /* acquire reader lock */
308 if ( entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
309 /* could not acquire entry lock...
310 * owner cannot free as we have the cache locked.
311 * so, unlock the cache, yield, and try again.
314 /* free cache mutex */
315 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
316 ldap_pvt_thread_yield();
321 LRU_DELETE( cache, ep );
322 LRU_ADD( cache, ep );
326 /* free cache mutex */
327 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
332 /* free cache mutex */
333 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
339 * cache_delete_entry - delete the entry e from the cache. the caller
340 * should have obtained e (increasing its ref count) via a call to one
341 * of the cache_find_* routines. the caller should *not* call the
342 * cache_return_entry() routine prior to calling cache_delete_entry().
343 * it performs this function.
345 * returns: 0 e was deleted ok
346 * 1 e was not in the cache
347 * -1 something bad happened
350 bdb2i_cache_delete_entry(
357 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry:\n", 0, 0, 0 );
359 /* set cache mutex */
360 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
362 /* XXX check for writer lock - should also check no readers pending */
364 assert(ldap_pvt_thread_rdwr_writers( &e->e_rdwr ) == 1);
367 rc = cache_delete_entry_internal( cache, e );
369 /* free cache mutex */
370 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
375 cache_delete_entry_internal(
380 int rc = 0; /* return code */
383 if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp )
390 if ( avl_delete( &cache->c_idtree, (caddr_t) e, entry_id_cmp )
401 LRU_DELETE( cache, e );
405 * flag entry to be freed later by a call to cache_return_entry()
407 e->e_state = ENTRY_STATE_DELETED;
415 lru_print( struct cache *cache )
419 fprintf( stderr, "LRU queue (head to tail):\n" );
420 for ( e = cache->c_lruhead; e != NULL; e = e->e_lrunext ) {
421 fprintf( stderr, "\tdn %20s id %lu refcnt %d\n", e->e_dn,
422 e->e_id, e->e_refcnt );
424 fprintf( stderr, "LRU queue (tail to head):\n" );
425 for ( e = cache->c_lrutail; e != NULL; e = e->e_lruprev ) {
426 fprintf( stderr, "\tdn %20s id %lu refcnt %d\n", e->e_dn,
427 e->e_id, e->e_refcnt );