1 /* cache.c - routines to maintain an in-core cache of entries */
12 #include "back-ldbm.h"
14 static int cache_delete_entry_internal(struct cache *cache, Entry *e);
16 static void lru_print(struct cache *cache);
20 * the cache has three entry points (ways to find things):
22 * by entry e.g., if you already have an entry from the cache
23 * and want to delete it. (really by entry ptr)
24 * by dn e.g., when looking for the base object of a search
25 * by id e.g., for search candidates
27 * these correspond to three different avl trees that are maintained.
31 cache_entry_cmp( Entry *e1, Entry *e2 )
33 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
37 cache_entrydn_cmp( Entry *e1, Entry *e2 )
39 /* compare their normalized UPPERCASED dn's */
40 return( strcmp( e1->e_ndn, e2->e_ndn ) );
44 cache_entryid_cmp( Entry *e1, Entry *e2 )
46 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
50 cache_set_state( struct cache *cache, Entry *e, int state )
53 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
57 /* free cache mutex */
58 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
62 cache_return_entry( struct cache *cache, Entry *e )
65 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
67 if ( --e->e_refcnt == 0 && e->e_state == ENTRY_STATE_DELETED ) {
71 /* free cache mutex */
72 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
76 cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
78 Debug( LDAP_DEBUG_TRACE, "====> cache_return_entry_%s\n",
79 rw ? "w" : "r", 0, 0);
80 entry_rdwr_unlock(e, rw);;
81 cache_return_entry(cache, e);
85 cache_return_entry_r( struct cache *cache, Entry *e )
87 cache_return_entry_rw(cache, e, 0);
91 cache_return_entry_w( struct cache *cache, Entry *e )
93 cache_return_entry_rw(cache, e, 1);
97 #define LRU_DELETE( cache, e ) { \
98 if ( e->e_lruprev != NULL ) { \
99 e->e_lruprev->e_lrunext = e->e_lrunext; \
101 cache->c_lruhead = e->e_lrunext; \
103 if ( e->e_lrunext != NULL ) { \
104 e->e_lrunext->e_lruprev = e->e_lruprev; \
106 cache->c_lrutail = e->e_lruprev; \
110 #define LRU_ADD( cache, e ) { \
111 e->e_lrunext = cache->c_lruhead; \
112 if ( e->e_lrunext != NULL ) { \
113 e->e_lrunext->e_lruprev = e; \
115 cache->c_lruhead = e; \
116 e->e_lruprev = NULL; \
117 if ( cache->c_lrutail == NULL ) { \
118 cache->c_lrutail = e; \
123 * cache_create_entry_lock - create an entry in the cache, and lock it.
124 * returns: 0 entry has been created and locked
125 * 1 entry already existed
126 * -1 something bad happened
129 cache_add_entry_lock(
138 /* set cache mutex */
139 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
141 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
142 cache_entrydn_cmp, avl_dup_error ) != 0 )
144 Debug( LDAP_DEBUG_TRACE,
145 "====> cache_add_entry lock: entry %20s id %lu already in dn cache\n",
146 e->e_dn, e->e_id, 0 );
148 /* free cache mutex */
149 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
154 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
155 cache_entryid_cmp, avl_dup_error ) != 0 )
157 Debug( LDAP_DEBUG_ANY,
158 "====> entry %20s id %lu already in id cache\n",
159 e->e_dn, e->e_id, 0 );
161 /* delete from dn tree inserted above */
162 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
163 cache_entrydn_cmp ) == NULL )
165 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
169 /* free cache mutex */
170 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
179 if ( ++cache->c_cursize > cache->c_maxsize ) {
181 * find the lru entry not currently in use and delete it.
182 * in case a lot of entries are in use, only look at the
183 * first 10 on the tail of the list.
186 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
188 /* move this in-use entry to the front of the q */
189 ee = cache->c_lrutail;
190 LRU_DELETE( cache, ee );
191 LRU_ADD( cache, ee );
196 * found at least one to delete - try to get back under
197 * the max cache size.
199 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
200 == 0 && cache->c_cursize > cache->c_maxsize ) {
201 e = cache->c_lrutail;
203 /* XXX check for writer lock - should also check no readers pending */
205 assert(!ldap_pvt_thread_rdwr_rwchk(&e->e_rdwr));
208 /* delete from cache and lru q */
209 rc = cache_delete_entry_internal( cache, e );
215 /* free cache mutex */
216 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
221 * cache_find_entry_dn2id - find an entry in the cache, given dn
225 cache_find_entry_dn2id(
231 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
235 /* set cache mutex */
236 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
239 e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
241 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
242 cache_entrydn_cmp )) != NULL )
246 Debug(LDAP_DEBUG_TRACE, "====> cache_find_entry_dn2id: found dn: %s\n",
250 * entry is deleted or not fully created yet
252 if ( ep->e_state == ENTRY_STATE_DELETED ||
253 ep->e_state == ENTRY_STATE_CREATING )
255 /* free cache mutex */
256 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
260 /* XXX is this safe without writer lock? */
264 LRU_DELETE( cache, ep );
265 LRU_ADD( cache, ep );
267 /* acquire reader lock */
268 entry_rdwr_lock(ep, 0);
271 if ( ep->e_state == ENTRY_STATE_DELETED ||
272 ep->e_state == ENTRY_STATE_CREATING )
274 /* XXX check that is is required */
277 /* free reader lock */
278 entry_rdwr_unlock(ep, 0);
279 /* free cache mutex */
280 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
288 /* free reader lock */
289 entry_rdwr_unlock(ep, 0);
291 /* free cache mutex */
292 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
294 cache_return_entry( &li->li_cache, ep );
301 /* free cache mutex */
302 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
308 * cache_find_entry_id - find an entry in the cache, given id
321 /* set cache mutex */
322 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
326 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
327 cache_entryid_cmp )) != NULL )
329 Debug(LDAP_DEBUG_TRACE,
330 "====> cache_find_entry_dn2id: found id: %ld rw: %d\n",
334 * entry is deleted or not fully created yet
336 if ( ep->e_state == ENTRY_STATE_DELETED ||
337 ep->e_state == ENTRY_STATE_CREATING )
339 /* free cache mutex */
340 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
343 /* XXX is this safe without writer lock? */
347 LRU_DELETE( cache, ep );
348 LRU_ADD( cache, ep );
350 /* acquire reader lock */
351 entry_rdwr_lock(ep, 0);
354 if ( ep->e_state == ENTRY_STATE_DELETED ||
355 ep->e_state == ENTRY_STATE_CREATING ) {
357 /* XXX check that is is required */
360 /* free reader lock */
361 entry_rdwr_unlock(ep, 0);
363 /* free cache mutex */
364 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
369 entry_rdwr_unlock(ep, 0);
370 entry_rdwr_lock(ep, 1);
373 /* free cache mutex */
374 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
379 /* free cache mutex */
380 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
386 * cache_delete_entry - delete the entry e from the cache. the caller
387 * should have obtained e (increasing its ref count) via a call to one
388 * of the cache_find_* routines. the caller should *not* call the
389 * cache_return_entry() routine prior to calling cache_delete_entry().
390 * it performs this function.
392 * returns: 0 e was deleted ok
393 * 1 e was not in the cache
394 * -1 something bad happened
404 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry:\n", 0, 0, 0 );
406 /* XXX check for writer lock - should also check no readers pending */
408 assert(ldap_pvt_thread_rdwr_wchk(&e->e_rdwr));
411 /* set cache mutex */
412 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
414 rc = cache_delete_entry_internal( cache, e );
416 /* free cache mutex */
417 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
422 cache_delete_entry_internal(
427 int rc = 0; /* return code */
430 if ( avl_delete( &cache->c_dntree, (caddr_t) e, cache_entrydn_cmp )
437 if ( avl_delete( &cache->c_idtree, (caddr_t) e, cache_entryid_cmp )
448 LRU_DELETE( cache, e );
452 * flag entry to be freed later by a call to cache_return_entry()
454 e->e_state = ENTRY_STATE_DELETED;
462 lru_print( struct cache *cache )
466 fprintf( stderr, "LRU queue (head to tail):\n" );
467 for ( e = cache->c_lruhead; e != NULL; e = e->e_lrunext ) {
468 fprintf( stderr, "\tdn %20s id %lu refcnt %d\n", e->e_dn,
469 e->e_id, e->e_refcnt );
471 fprintf( stderr, "LRU queue (tail to head):\n" );
472 for ( e = cache->c_lrutail; e != NULL; e = e->e_lruprev ) {
473 fprintf( stderr, "\tdn %20s id %lu refcnt %d\n", e->e_dn,
474 e->e_id, e->e_refcnt );