1 /* cache.c - routines to maintain an in-core cache of entries */
6 int strcasecmp( const char *, const char *);
11 #include "back-ldbm.h"
13 static int cache_delete_entry_internal(struct cache *cache, Entry *e);
15 static void lru_print(struct cache *cache);
19 * the cache has three entry points (ways to find things):
21 * by entry e.g., if you already have an entry from the cache
22 * and want to delete it. (really by entry ptr)
23 * by dn e.g., when looking for the base object of a search
24 * by id e.g., for search candidates
26 * these correspond to three different avl trees that are maintained.
30 cache_entry_cmp( Entry *e1, Entry *e2 )
32 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
36 cache_entrydn_cmp( Entry *e1, Entry *e2 )
38 /* compare their normalized dn's */
39 return( strcasecmp( e1->e_ndn, e2->e_ndn ) );
43 cache_entryid_cmp( Entry *e1, Entry *e2 )
45 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
49 cache_set_state( struct cache *cache, Entry *e, int state )
52 pthread_mutex_lock( &cache->c_mutex );
56 /* free cache mutex */
57 pthread_mutex_unlock( &cache->c_mutex );
61 cache_return_entry( struct cache *cache, Entry *e )
64 pthread_mutex_lock( &cache->c_mutex );
66 if ( --e->e_refcnt == 0 && e->e_state == ENTRY_STATE_DELETED ) {
70 /* free cache mutex */
71 pthread_mutex_unlock( &cache->c_mutex );
75 cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
77 Debug( LDAP_DEBUG_TRACE, "====> cache_return_entry_%s\n",
78 rw ? "w" : "r", 0, 0);
79 entry_rdwr_unlock(e, rw);;
80 cache_return_entry(cache, e);
84 cache_return_entry_r( struct cache *cache, Entry *e )
86 cache_return_entry_rw(cache, e, 0);
90 cache_return_entry_w( struct cache *cache, Entry *e )
92 cache_return_entry_rw(cache, e, 1);
96 #define LRU_DELETE( cache, e ) { \
97 if ( e->e_lruprev != NULL ) { \
98 e->e_lruprev->e_lrunext = e->e_lrunext; \
100 cache->c_lruhead = e->e_lrunext; \
102 if ( e->e_lrunext != NULL ) { \
103 e->e_lrunext->e_lruprev = e->e_lruprev; \
105 cache->c_lrutail = e->e_lruprev; \
109 #define LRU_ADD( cache, e ) { \
110 e->e_lrunext = cache->c_lruhead; \
111 if ( e->e_lrunext != NULL ) { \
112 e->e_lrunext->e_lruprev = e; \
114 cache->c_lruhead = e; \
115 e->e_lruprev = NULL; \
116 if ( cache->c_lrutail == NULL ) { \
117 cache->c_lrutail = e; \
122 * cache_create_entry_lock - create an entry in the cache, and lock it.
123 * returns: 0 entry has been created and locked
124 * 1 entry already existed
125 * -1 something bad happened
128 cache_add_entry_lock(
137 /* set cache mutex */
138 pthread_mutex_lock( &cache->c_mutex );
140 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
141 cache_entrydn_cmp, avl_dup_error ) != 0 )
143 Debug( LDAP_DEBUG_TRACE,
144 "====> cache_add_entry lock: entry %20s id %lu already in dn cache\n",
145 e->e_dn, e->e_id, 0 );
147 /* free cache mutex */
148 pthread_mutex_unlock( &cache->c_mutex );
153 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
154 cache_entryid_cmp, avl_dup_error ) != 0 )
156 Debug( LDAP_DEBUG_ANY,
157 "====> entry %20s id %lu already in id cache\n",
158 e->e_dn, e->e_id, 0 );
160 /* delete from dn tree inserted above */
161 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
162 cache_entrydn_cmp ) == NULL )
164 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
168 /* free cache mutex */
169 pthread_mutex_unlock( &cache->c_mutex );
178 if ( ++cache->c_cursize > cache->c_maxsize ) {
180 * find the lru entry not currently in use and delete it.
181 * in case a lot of entries are in use, only look at the
182 * first 10 on the tail of the list.
185 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
187 /* move this in-use entry to the front of the q */
188 ee = cache->c_lrutail;
189 LRU_DELETE( cache, ee );
190 LRU_ADD( cache, ee );
195 * found at least one to delete - try to get back under
196 * the max cache size.
198 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
199 == 0 && cache->c_cursize > cache->c_maxsize ) {
200 e = cache->c_lrutail;
202 /* XXX check for writer lock - should also check no readers pending */
204 assert(!pthread_rdwr_rwchk_np(&e->e_rdwr));
207 /* delete from cache and lru q */
208 rc = cache_delete_entry_internal( cache, e );
214 /* free cache mutex */
215 pthread_mutex_unlock( &cache->c_mutex );
220 * cache_find_entry_dn2id - find an entry in the cache, given dn
224 cache_find_entry_dn2id(
230 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
234 /* set cache mutex */
235 pthread_mutex_lock( &cache->c_mutex );
238 e.e_ndn = dn_normalize( ch_strdup( dn ) );
240 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
241 cache_entrydn_cmp )) != NULL )
245 Debug(LDAP_DEBUG_TRACE, "====> cache_find_entry_dn2id: found dn: %s\n",
249 * entry is deleted or not fully created yet
251 if ( ep->e_state == ENTRY_STATE_DELETED ||
252 ep->e_state == ENTRY_STATE_CREATING )
254 /* free cache mutex */
255 pthread_mutex_unlock( &cache->c_mutex );
259 /* XXX is this safe without writer lock? */
263 LRU_DELETE( cache, ep );
264 LRU_ADD( cache, ep );
266 /* acquire reader lock */
267 entry_rdwr_lock(ep, 0);
270 if ( ep->e_state == ENTRY_STATE_DELETED ||
271 ep->e_state == ENTRY_STATE_CREATING )
273 /* XXX check that is is required */
276 /* free reader lock */
277 entry_rdwr_unlock(ep, 0);
278 /* free cache mutex */
279 pthread_mutex_unlock( &cache->c_mutex );
287 /* free reader lock */
288 entry_rdwr_unlock(ep, 0);
290 /* free cache mutex */
291 pthread_mutex_unlock( &cache->c_mutex );
293 cache_return_entry( &li->li_cache, ep );
300 /* free cache mutex */
301 pthread_mutex_unlock( &cache->c_mutex );
307 * cache_find_entry_id - find an entry in the cache, given id
320 /* set cache mutex */
321 pthread_mutex_lock( &cache->c_mutex );
325 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
326 cache_entryid_cmp )) != NULL )
328 Debug(LDAP_DEBUG_TRACE,
329 "====> cache_find_entry_dn2id: found id: %ld rw: %d\n",
333 * entry is deleted or not fully created yet
335 if ( ep->e_state == ENTRY_STATE_DELETED ||
336 ep->e_state == ENTRY_STATE_CREATING )
338 /* free cache mutex */
339 pthread_mutex_unlock( &cache->c_mutex );
342 /* XXX is this safe without writer lock? */
346 LRU_DELETE( cache, ep );
347 LRU_ADD( cache, ep );
349 /* acquire reader lock */
350 entry_rdwr_lock(ep, 0);
353 if ( ep->e_state == ENTRY_STATE_DELETED ||
354 ep->e_state == ENTRY_STATE_CREATING ) {
356 /* XXX check that is is required */
359 /* free reader lock */
360 entry_rdwr_unlock(ep, 0);
362 /* free cache mutex */
363 pthread_mutex_unlock( &cache->c_mutex );
368 entry_rdwr_unlock(ep, 0);
369 entry_rdwr_lock(ep, 1);
372 /* free cache mutex */
373 pthread_mutex_unlock( &cache->c_mutex );
378 /* free cache mutex */
379 pthread_mutex_unlock( &cache->c_mutex );
385 * cache_delete_entry - delete the entry e from the cache. the caller
386 * should have obtained e (increasing its ref count) via a call to one
387 * of the cache_find_* routines. the caller should *not* call the
388 * cache_return_entry() routine prior to calling cache_delete_entry().
389 * it performs this function.
391 * returns: 0 e was deleted ok
392 * 1 e was not in the cache
393 * -1 something bad happened
403 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry:\n", 0, 0, 0 );
405 /* XXX check for writer lock - should also check no readers pending */
407 assert(pthread_rdwr_wchk_np(&e->e_rdwr));
410 /* set cache mutex */
411 pthread_mutex_lock( &cache->c_mutex );
413 rc = cache_delete_entry_internal( cache, e );
415 /* free cache mutex */
416 pthread_mutex_unlock( &cache->c_mutex );
421 cache_delete_entry_internal(
426 int rc = 0; /* return code */
429 if ( avl_delete( &cache->c_dntree, (caddr_t) e, cache_entrydn_cmp )
436 if ( avl_delete( &cache->c_idtree, (caddr_t) e, cache_entryid_cmp )
447 LRU_DELETE( cache, e );
451 * flag entry to be freed later by a call to cache_return_entry()
453 e->e_state = ENTRY_STATE_DELETED;
461 lru_print( struct cache *cache )
465 fprintf( stderr, "LRU queue (head to tail):\n" );
466 for ( e = cache->c_lruhead; e != NULL; e = e->e_lrunext ) {
467 fprintf( stderr, "\tdn %20s id %lu refcnt %d\n", e->e_dn,
468 e->e_id, e->e_refcnt );
470 fprintf( stderr, "LRU queue (tail to head):\n" );
471 for ( e = cache->c_lrutail; e != NULL; e = e->e_lruprev ) {
472 fprintf( stderr, "\tdn %20s id %lu refcnt %d\n", e->e_dn,
473 e->e_id, e->e_refcnt );