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 return( strcasecmp( e1->e_dn, e2->e_dn ) );
42 cache_entryid_cmp( Entry *e1, Entry *e2 )
44 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
48 cache_set_state( struct cache *cache, Entry *e, int state )
51 pthread_mutex_lock( &cache->c_mutex );
55 /* free cache mutex */
56 pthread_mutex_unlock( &cache->c_mutex );
60 cache_return_entry( struct cache *cache, Entry *e )
63 pthread_mutex_lock( &cache->c_mutex );
65 if ( --e->e_refcnt == 0 && e->e_state == ENTRY_STATE_DELETED ) {
69 /* free cache mutex */
70 pthread_mutex_unlock( &cache->c_mutex );
74 cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
76 Debug( LDAP_DEBUG_TRACE, "====> cache_return_entry_%s\n",
77 rw ? "w" : "r", 0, 0);
78 entry_rdwr_unlock(e, rw);;
79 cache_return_entry(cache, e);
83 cache_return_entry_r( struct cache *cache, Entry *e )
85 cache_return_entry_rw(cache, e, 0);
89 cache_return_entry_w( struct cache *cache, Entry *e )
91 cache_return_entry_rw(cache, e, 1);
95 #define LRU_DELETE( cache, e ) { \
96 if ( e->e_lruprev != NULL ) { \
97 e->e_lruprev->e_lrunext = e->e_lrunext; \
99 cache->c_lruhead = e->e_lrunext; \
101 if ( e->e_lrunext != NULL ) { \
102 e->e_lrunext->e_lruprev = e->e_lruprev; \
104 cache->c_lrutail = e->e_lruprev; \
108 #define LRU_ADD( cache, e ) { \
109 e->e_lrunext = cache->c_lruhead; \
110 if ( e->e_lrunext != NULL ) { \
111 e->e_lrunext->e_lruprev = e; \
113 cache->c_lruhead = e; \
114 e->e_lruprev = NULL; \
115 if ( cache->c_lrutail == NULL ) { \
116 cache->c_lrutail = e; \
121 * cache_create_entry_lock - create an entry in the cache, and lock it.
122 * returns: 0 entry has been created and locked
123 * 1 entry already existed
124 * -1 something bad happened
127 cache_add_entry_lock(
136 /* set cache mutex */
137 pthread_mutex_lock( &cache->c_mutex );
139 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
140 cache_entrydn_cmp, avl_dup_error ) != 0 )
142 Debug( LDAP_DEBUG_TRACE,
143 "====> cache_add_entry lock: entry %20s id %lu already in dn cache\n",
144 e->e_dn, e->e_id, 0 );
146 /* free cache mutex */
147 pthread_mutex_unlock( &cache->c_mutex );
152 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
153 cache_entryid_cmp, avl_dup_error ) != 0 )
155 Debug( LDAP_DEBUG_ANY,
156 "====> entry %20s id %lu already in id cache\n",
157 e->e_dn, e->e_id, 0 );
159 /* delete from dn tree inserted above */
160 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
161 cache_entrydn_cmp ) == NULL )
163 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
167 /* free cache mutex */
168 pthread_mutex_unlock( &cache->c_mutex );
177 if ( ++cache->c_cursize > cache->c_maxsize ) {
179 * find the lru entry not currently in use and delete it.
180 * in case a lot of entries are in use, only look at the
181 * first 10 on the tail of the list.
184 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
186 /* move this in-use entry to the front of the q */
187 ee = cache->c_lrutail;
188 LRU_DELETE( cache, ee );
189 LRU_ADD( cache, ee );
194 * found at least one to delete - try to get back under
195 * the max cache size.
197 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
198 == 0 && cache->c_cursize > cache->c_maxsize ) {
199 e = cache->c_lrutail;
201 /* XXX check for writer lock - should also check no readers pending */
203 assert(!pthread_rdwr_rwchk_np(&e->e_rdwr));
206 /* delete from cache and lru q */
207 rc = cache_delete_entry_internal( cache, e );
213 /* free cache mutex */
214 pthread_mutex_unlock( &cache->c_mutex );
219 * cache_find_entry_dn2id - find an entry in the cache, given dn
223 cache_find_entry_dn2id(
229 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
233 /* set cache mutex */
234 pthread_mutex_lock( &cache->c_mutex );
238 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
239 cache_entrydn_cmp )) != NULL )
241 Debug(LDAP_DEBUG_TRACE, "====> cache_find_entry_dn2id: found dn: %s\n",
245 * entry is deleted or not fully created yet
247 if ( ep->e_state == ENTRY_STATE_DELETED ||
248 ep->e_state == ENTRY_STATE_CREATING )
250 /* free cache mutex */
251 pthread_mutex_unlock( &cache->c_mutex );
255 /* XXX is this safe without writer lock? */
259 LRU_DELETE( cache, ep );
260 LRU_ADD( cache, ep );
262 /* acquire reader lock */
263 entry_rdwr_lock(ep, 0);
266 if ( ep->e_state == ENTRY_STATE_DELETED ||
267 ep->e_state == ENTRY_STATE_CREATING )
269 /* XXX check that is is required */
272 /* free reader lock */
273 entry_rdwr_unlock(ep, 0);
274 /* free cache mutex */
275 pthread_mutex_unlock( &cache->c_mutex );
283 /* free reader lock */
284 entry_rdwr_unlock(ep, 0);
286 /* free cache mutex */
287 pthread_mutex_unlock( &cache->c_mutex );
289 cache_return_entry( &li->li_cache, ep );
294 /* free cache mutex */
295 pthread_mutex_unlock( &cache->c_mutex );
301 * cache_find_entry_id - find an entry in the cache, given id
314 /* set cache mutex */
315 pthread_mutex_lock( &cache->c_mutex );
319 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
320 cache_entryid_cmp )) != NULL )
322 Debug(LDAP_DEBUG_TRACE,
323 "====> cache_find_entry_dn2id: found id: %ld rw: %d\n",
327 * entry is deleted or not fully created yet
329 if ( ep->e_state == ENTRY_STATE_DELETED ||
330 ep->e_state == ENTRY_STATE_CREATING )
332 /* free cache mutex */
333 pthread_mutex_unlock( &cache->c_mutex );
336 /* XXX is this safe without writer lock? */
340 LRU_DELETE( cache, ep );
341 LRU_ADD( cache, ep );
343 /* acquire reader lock */
344 entry_rdwr_lock(ep, 0);
347 if ( ep->e_state == ENTRY_STATE_DELETED ||
348 ep->e_state == ENTRY_STATE_CREATING ) {
350 /* XXX check that is is required */
353 /* free reader lock */
354 entry_rdwr_unlock(ep, 0);
356 /* free cache mutex */
357 pthread_mutex_unlock( &cache->c_mutex );
362 entry_rdwr_unlock(ep, 0);
363 entry_rdwr_lock(ep, 1);
366 /* free cache mutex */
367 pthread_mutex_unlock( &cache->c_mutex );
372 /* free cache mutex */
373 pthread_mutex_unlock( &cache->c_mutex );
379 * cache_delete_entry - delete the entry e from the cache. the caller
380 * should have obtained e (increasing its ref count) via a call to one
381 * of the cache_find_* routines. the caller should *not* call the
382 * cache_return_entry() routine prior to calling cache_delete_entry().
383 * it performs this function.
385 * returns: 0 e was deleted ok
386 * 1 e was not in the cache
387 * -1 something bad happened
397 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry:\n", 0, 0, 0 );
399 /* XXX check for writer lock - should also check no readers pending */
401 assert(pthread_rdwr_wchk_np(&e->e_rdwr));
404 /* set cache mutex */
405 pthread_mutex_lock( &cache->c_mutex );
407 rc = cache_delete_entry_internal( cache, e );
409 /* free cache mutex */
410 pthread_mutex_unlock( &cache->c_mutex );
415 cache_delete_entry_internal(
421 if ( avl_delete( &cache->c_dntree, (caddr_t) e, cache_entrydn_cmp )
428 if ( avl_delete( &cache->c_idtree, (caddr_t) e, cache_entryid_cmp )
435 LRU_DELETE( cache, e );
439 * flag entry to be freed later by a call to cache_return_entry()
441 e->e_state = ENTRY_STATE_DELETED;
449 lru_print( struct cache *cache )
453 fprintf( stderr, "LRU queue (head to tail):\n" );
454 for ( e = cache->c_lruhead; e != NULL; e = e->e_lrunext ) {
455 fprintf( stderr, "\tdn %20s id %lu refcnt %d\n", e->e_dn,
456 e->e_id, e->e_refcnt );
458 fprintf( stderr, "LRU queue (tail to head):\n" );
459 for ( e = cache->c_lrutail; e != NULL; e = e->e_lruprev ) {
460 fprintf( stderr, "\tdn %20s id %lu refcnt %d\n", e->e_dn,
461 e->e_id, e->e_refcnt );