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, e, cache_entrydn_cmp, avl_dup_error )
137 Debug( LDAP_DEBUG_TRACE,
138 "====> cache_add_entry lock: entry %20s id %d already in dn cache\n",
139 e->e_dn, e->e_id, 0 );
141 /* free cache mutex */
142 pthread_mutex_unlock( &cache->c_mutex );
147 if ( avl_insert( &cache->c_idtree, e, cache_entryid_cmp, avl_dup_error )
149 Debug( LDAP_DEBUG_ANY,
150 "====> entry %20s id %d already in id cache\n",
151 e->e_dn, e->e_id, 0 );
153 /* delete from dn tree inserted above */
154 if ( avl_delete( &cache->c_dntree, e, cache_entrydn_cmp )
156 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
160 /* free cache mutex */
161 pthread_mutex_unlock( &cache->c_mutex );
170 if ( ++cache->c_cursize > cache->c_maxsize ) {
172 * find the lru entry not currently in use and delete it.
173 * in case a lot of entries are in use, only look at the
174 * first 10 on the tail of the list.
177 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
179 /* move this in-use entry to the front of the q */
180 ee = cache->c_lrutail;
181 LRU_DELETE( cache, ee );
182 LRU_ADD( cache, ee );
187 * found at least one to delete - try to get back under
188 * the max cache size.
190 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
191 == 0 && cache->c_cursize > cache->c_maxsize ) {
192 e = cache->c_lrutail;
194 /* XXX check for writer lock - should also check no readers pending */
195 assert(pthread_rdwr_wchk_np(&e->e_rdwr));
197 /* delete from cache and lru q */
198 rc = cache_delete_entry_internal( cache, e );
204 /* free cache mutex */
205 pthread_mutex_unlock( &cache->c_mutex );
210 * cache_find_entry_dn2id - find an entry in the cache, given dn
214 cache_find_entry_dn2id(
220 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
224 /* set cache mutex */
225 pthread_mutex_lock( &cache->c_mutex );
229 if ( (ep = (Entry *) avl_find( cache->c_dntree, &e, cache_entrydn_cmp ))
232 Debug(LDAP_DEBUG_TRACE, "====> cache_find_entry_dn2id: found dn: %s\n",
236 * entry is deleted or not fully created yet
238 if ( ep->e_state == ENTRY_STATE_DELETED ||
239 ep->e_state == ENTRY_STATE_CREATING )
241 /* free cache mutex */
242 pthread_mutex_unlock( &cache->c_mutex );
246 /* XXX is this safe without writer lock? */
250 LRU_DELETE( cache, ep );
251 LRU_ADD( cache, ep );
253 /* acquire reader lock */
254 entry_rdwr_lock(ep, 0);
257 if ( ep->e_state == ENTRY_STATE_DELETED ||
258 ep->e_state == ENTRY_STATE_CREATING )
260 /* XXX check that is is required */
263 /* free reader lock */
264 entry_rdwr_unlock(ep, 0);
265 /* free cache mutex */
266 pthread_mutex_unlock( &cache->c_mutex );
274 /* free reader lock */
275 entry_rdwr_unlock(ep, 0);
277 /* free cache mutex */
278 pthread_mutex_unlock( &cache->c_mutex );
280 cache_return_entry( &li->li_cache, ep );
285 /* free cache mutex */
286 pthread_mutex_unlock( &cache->c_mutex );
292 * cache_find_entry_id - find an entry in the cache, given id
305 /* set cache mutex */
306 pthread_mutex_lock( &cache->c_mutex );
310 if ( (ep = (Entry *) avl_find( cache->c_idtree, &e, cache_entryid_cmp ))
313 Debug(LDAP_DEBUG_TRACE,
314 "====> cache_find_entry_dn2id: found id: %ld rw: %d\n",
318 * entry is deleted or not fully created yet
320 if ( ep->e_state == ENTRY_STATE_DELETED ||
321 ep->e_state == ENTRY_STATE_CREATING )
323 /* free cache mutex */
324 pthread_mutex_unlock( &cache->c_mutex );
327 /* XXX is this safe without writer lock? */
331 LRU_DELETE( cache, ep );
332 LRU_ADD( cache, ep );
334 /* acquire reader lock */
335 entry_rdwr_lock(ep, 0);
338 if ( ep->e_state == ENTRY_STATE_DELETED ||
339 ep->e_state == ENTRY_STATE_CREATING ) {
341 /* XXX check that is is required */
344 /* free reader lock */
345 entry_rdwr_unlock(ep, 0);
347 /* free cache mutex */
348 pthread_mutex_unlock( &cache->c_mutex );
353 entry_rdwr_unlock(ep, 0);
354 entry_rdwr_lock(ep, 1);
357 /* free cache mutex */
358 pthread_mutex_unlock( &cache->c_mutex );
363 /* free cache mutex */
364 pthread_mutex_unlock( &cache->c_mutex );
370 * cache_delete_entry - delete the entry e from the cache. the caller
371 * should have obtained e (increasing its ref count) via a call to one
372 * of the cache_find_* routines. the caller should *not* call the
373 * cache_return_entry() routine prior to calling cache_delete_entry().
374 * it performs this function.
376 * returns: 0 e was deleted ok
377 * 1 e was not in the cache
378 * -1 something bad happened
388 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry:\n", 0, 0, 0 );
390 /* XXX check for writer lock - should also check no readers pending */
391 assert(pthread_rdwr_wchk_np(&e->e_rdwr));
393 /* set cache mutex */
394 pthread_mutex_lock( &cache->c_mutex );
396 rc = cache_delete_entry_internal( cache, e );
398 /* free cache mutex */
399 pthread_mutex_unlock( &cache->c_mutex );
404 cache_delete_entry_internal(
410 if ( avl_delete( &cache->c_dntree, e, cache_entrydn_cmp ) == NULL ) {
415 if ( avl_delete( &cache->c_idtree, e, cache_entryid_cmp ) == NULL ) {
420 LRU_DELETE( cache, e );
424 * flag entry to be freed later by a call to cache_return_entry()
426 e->e_state = ENTRY_STATE_DELETED;
434 lru_print( struct cache *cache )
438 fprintf( stderr, "LRU queue (head to tail):\n" );
439 for ( e = cache->c_lruhead; e != NULL; e = e->e_lrunext ) {
440 fprintf( stderr, "\tdn %20s id %d refcnt %d\n", e->e_dn,
441 e->e_id, e->e_refcnt );
443 fprintf( stderr, "LRU queue (tail to head):\n" );
444 for ( e = cache->c_lrutail; e != NULL; e = e->e_lruprev ) {
445 fprintf( stderr, "\tdn %20s id %d refcnt %d\n", e->e_dn,
446 e->e_id, e->e_refcnt );