1 /* cache.c - routines to maintain an in-core cache of entries */
5 #include <sys/socket.h>
9 static int cache_delete_entry_internal();
11 static void lru_print();
15 * the cache has three entry points (ways to find things):
17 * by entry e.g., if you already have an entry from the cache
18 * and want to delete it. (really by entry ptr)
19 * by dn e.g., when looking for the base object of a search
20 * by id e.g., for search candidates
22 * these correspond to three different avl trees that are maintained.
26 cache_entry_cmp( Entry *e1, Entry *e2 )
28 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
32 cache_entrydn_cmp( Entry *e1, Entry *e2 )
34 return( strcasecmp( e1->e_dn, e2->e_dn ) );
38 cache_entryid_cmp( Entry *e1, Entry *e2 )
40 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
44 cache_set_state( struct cache *cache, Entry *e, int state )
47 pthread_mutex_lock( &cache->c_mutex );
51 /* free cache mutex */
52 pthread_mutex_unlock( &cache->c_mutex );
56 cache_return_entry( struct cache *cache, Entry *e )
59 pthread_mutex_lock( &cache->c_mutex );
61 if ( --e->e_refcnt == 0 && e->e_state == ENTRY_STATE_DELETED ) {
65 /* free cache mutex */
66 pthread_mutex_unlock( &cache->c_mutex );
70 cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
72 Debug( LDAP_DEBUG_TRACE, "====> cache_return_entry_%s\n",
73 rw ? "w" : "r", 0, 0);
74 entry_rdwr_unlock(e, rw);;
75 cache_return_entry(cache, e);
79 cache_return_entry_r( struct cache *cache, Entry *e )
81 cache_return_entry_rw(cache, e, 0);
85 cache_return_entry_w( struct cache *cache, Entry *e )
87 cache_return_entry_rw(cache, e, 1);
91 #define LRU_DELETE( cache, e ) { \
92 if ( e->e_lruprev != NULL ) { \
93 e->e_lruprev->e_lrunext = e->e_lrunext; \
95 cache->c_lruhead = e->e_lrunext; \
97 if ( e->e_lrunext != NULL ) { \
98 e->e_lrunext->e_lruprev = e->e_lruprev; \
100 cache->c_lrutail = e->e_lruprev; \
104 #define LRU_ADD( cache, e ) { \
105 e->e_lrunext = cache->c_lruhead; \
106 if ( e->e_lrunext != NULL ) { \
107 e->e_lrunext->e_lruprev = e; \
109 cache->c_lruhead = e; \
110 e->e_lruprev = NULL; \
111 if ( cache->c_lrutail == NULL ) { \
112 cache->c_lrutail = e; \
117 * cache_create_entry_lock - create an entry in the cache, and lock it.
118 * returns: 0 entry has been created and locked
119 * 1 entry already existed
120 * -1 something bad happened
123 cache_add_entry_lock(
132 /* set cache mutex */
133 pthread_mutex_lock( &cache->c_mutex );
135 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
136 cache_entrydn_cmp, avl_dup_error ) != 0 )
138 Debug( LDAP_DEBUG_TRACE,
139 "====> cache_add_entry lock: entry %20s id %d already in dn cache\n",
140 e->e_dn, e->e_id, 0 );
142 /* free cache mutex */
143 pthread_mutex_unlock( &cache->c_mutex );
148 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
149 cache_entryid_cmp, avl_dup_error ) != 0 )
151 Debug( LDAP_DEBUG_ANY,
152 "====> entry %20s id %d already in id cache\n",
153 e->e_dn, e->e_id, 0 );
155 /* delete from dn tree inserted above */
156 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
157 cache_entrydn_cmp ) == NULL )
159 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
163 /* free cache mutex */
164 pthread_mutex_unlock( &cache->c_mutex );
173 if ( ++cache->c_cursize > cache->c_maxsize ) {
175 * find the lru entry not currently in use and delete it.
176 * in case a lot of entries are in use, only look at the
177 * first 10 on the tail of the list.
180 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
182 /* move this in-use entry to the front of the q */
183 ee = cache->c_lrutail;
184 LRU_DELETE( cache, ee );
185 LRU_ADD( cache, ee );
190 * found at least one to delete - try to get back under
191 * the max cache size.
193 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
194 == 0 && cache->c_cursize > cache->c_maxsize ) {
195 e = cache->c_lrutail;
197 /* XXX check for writer lock - should also check no readers pending */
198 assert(pthread_rdwr_wchk_np(&e->e_rdwr));
200 /* delete from cache and lru q */
201 rc = cache_delete_entry_internal( cache, e );
207 /* free cache mutex */
208 pthread_mutex_unlock( &cache->c_mutex );
213 * cache_find_entry_dn2id - find an entry in the cache, given dn
217 cache_find_entry_dn2id(
223 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
227 /* set cache mutex */
228 pthread_mutex_lock( &cache->c_mutex );
232 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
233 cache_entrydn_cmp )) != NULL )
235 Debug(LDAP_DEBUG_TRACE, "====> cache_find_entry_dn2id: found dn: %s\n",
239 * entry is deleted or not fully created yet
241 if ( ep->e_state == ENTRY_STATE_DELETED ||
242 ep->e_state == ENTRY_STATE_CREATING )
244 /* free cache mutex */
245 pthread_mutex_unlock( &cache->c_mutex );
249 /* XXX is this safe without writer lock? */
253 LRU_DELETE( cache, ep );
254 LRU_ADD( cache, ep );
256 /* acquire reader lock */
257 entry_rdwr_lock(ep, 0);
260 if ( ep->e_state == ENTRY_STATE_DELETED ||
261 ep->e_state == ENTRY_STATE_CREATING )
263 /* XXX check that is is required */
266 /* free reader lock */
267 entry_rdwr_unlock(ep, 0);
268 /* free cache mutex */
269 pthread_mutex_unlock( &cache->c_mutex );
277 /* free reader lock */
278 entry_rdwr_unlock(ep, 0);
280 /* free cache mutex */
281 pthread_mutex_unlock( &cache->c_mutex );
283 cache_return_entry( &li->li_cache, ep );
288 /* free cache mutex */
289 pthread_mutex_unlock( &cache->c_mutex );
295 * cache_find_entry_id - find an entry in the cache, given id
308 /* set cache mutex */
309 pthread_mutex_lock( &cache->c_mutex );
313 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
314 cache_entryid_cmp )) != NULL )
316 Debug(LDAP_DEBUG_TRACE,
317 "====> cache_find_entry_dn2id: found id: %ld rw: %d\n",
321 * entry is deleted or not fully created yet
323 if ( ep->e_state == ENTRY_STATE_DELETED ||
324 ep->e_state == ENTRY_STATE_CREATING )
326 /* free cache mutex */
327 pthread_mutex_unlock( &cache->c_mutex );
330 /* XXX is this safe without writer lock? */
334 LRU_DELETE( cache, ep );
335 LRU_ADD( cache, ep );
337 /* acquire reader lock */
338 entry_rdwr_lock(ep, 0);
341 if ( ep->e_state == ENTRY_STATE_DELETED ||
342 ep->e_state == ENTRY_STATE_CREATING ) {
344 /* XXX check that is is required */
347 /* free reader lock */
348 entry_rdwr_unlock(ep, 0);
350 /* free cache mutex */
351 pthread_mutex_unlock( &cache->c_mutex );
356 entry_rdwr_unlock(ep, 0);
357 entry_rdwr_lock(ep, 1);
360 /* free cache mutex */
361 pthread_mutex_unlock( &cache->c_mutex );
366 /* free cache mutex */
367 pthread_mutex_unlock( &cache->c_mutex );
373 * cache_delete_entry - delete the entry e from the cache. the caller
374 * should have obtained e (increasing its ref count) via a call to one
375 * of the cache_find_* routines. the caller should *not* call the
376 * cache_return_entry() routine prior to calling cache_delete_entry().
377 * it performs this function.
379 * returns: 0 e was deleted ok
380 * 1 e was not in the cache
381 * -1 something bad happened
391 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry:\n", 0, 0, 0 );
393 /* XXX check for writer lock - should also check no readers pending */
394 assert(pthread_rdwr_wchk_np(&e->e_rdwr));
396 /* set cache mutex */
397 pthread_mutex_lock( &cache->c_mutex );
399 rc = cache_delete_entry_internal( cache, e );
401 /* free cache mutex */
402 pthread_mutex_unlock( &cache->c_mutex );
407 cache_delete_entry_internal(
413 if ( avl_delete( &cache->c_dntree, (caddr_t) e, cache_entrydn_cmp )
420 if ( avl_delete( &cache->c_idtree, (caddr_t) e, cache_entryid_cmp )
427 LRU_DELETE( cache, e );
431 * flag entry to be freed later by a call to cache_return_entry()
433 e->e_state = ENTRY_STATE_DELETED;
441 lru_print( struct cache *cache )
445 fprintf( stderr, "LRU queue (head to tail):\n" );
446 for ( e = cache->c_lruhead; e != NULL; e = e->e_lrunext ) {
447 fprintf( stderr, "\tdn %20s id %d refcnt %d\n", e->e_dn,
448 e->e_id, e->e_refcnt );
450 fprintf( stderr, "LRU queue (tail to head):\n" );
451 for ( e = cache->c_lrutail; e != NULL; e = e->e_lruprev ) {
452 fprintf( stderr, "\tdn %20s id %d refcnt %d\n", e->e_dn,
453 e->e_id, e->e_refcnt );