1 /* cache.c - routines to maintain an in-core cache of entries */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
18 #include "back-ldbm.h"
20 /* LDBM backend specific entry info -- visible only to the cache */
21 typedef struct ldbm_entry_info {
23 * These items are specific to the LDBM backend and should
24 * be hidden. Backend cache lock required to access.
26 int lei_state; /* for the cache */
27 #define CACHE_ENTRY_UNDEFINED 0
28 #define CACHE_ENTRY_CREATING 1
29 #define CACHE_ENTRY_READY 2
30 #define CACHE_ENTRY_DELETED 3
31 #define CACHE_ENTRY_COMMITTED 4
33 int lei_refcnt; /* # threads ref'ing this entry */
34 Entry *lei_lrunext; /* for cache lru list */
38 #define LEI(e) ((EntryInfo *) ((e)->e_private))
40 static int cache_delete_entry_internal(Cache *cache, Entry *e);
42 static void lru_print(Cache *cache);
46 cache_entry_private_init( Entry*e )
48 assert( e->e_private == NULL );
50 if( e->e_private != NULL ) {
51 /* this should never happen */
55 e->e_private = ch_calloc(1, sizeof(struct ldbm_entry_info));
61 * marks an entry in CREATING state as committed, so it is really returned
62 * to the cache. Otherwise an entry in CREATING state is removed.
63 * Makes e_private be destroyed at the following cache_return_entry_w,
64 * but lets the entry untouched (owned by someone else)
67 cache_entry_commit( Entry *e )
70 assert( e->e_private );
71 assert( LEI(e)->lei_state == CACHE_ENTRY_CREATING );
72 /* assert( LEI(e)->lei_refcnt == 1 ); */
74 LEI(e)->lei_state = CACHE_ENTRY_COMMITTED;
78 cache_entry_private_destroy( Entry*e )
80 assert( e->e_private );
88 cache_return_entry_rw( Cache *cache, Entry *e, int rw )
91 int refcnt, freeit = 1;
94 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
96 assert( e->e_private );
99 refcnt = --LEI(e)->lei_refcnt;
102 * if the entry is returned when in CREATING state, it is deleted
103 * but not freed because it may belong to someone else (do_add,
106 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
107 cache_delete_entry_internal( cache, e );
109 /* now the entry is in DELETED state */
112 if ( LEI(e)->lei_state == CACHE_ENTRY_COMMITTED ) {
113 LEI(e)->lei_state = CACHE_ENTRY_READY;
115 /* free cache mutex */
116 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
119 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
120 "cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
121 id, rw ? "w" : "r", refcnt ));
123 Debug( LDAP_DEBUG_TRACE,
124 "====> cache_return_entry_%s( %ld ): created (%d)\n",
125 rw ? "w" : "r", id, refcnt );
128 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
130 /* free cache mutex */
131 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
134 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
135 "cache_return_entry_rw: %ld, delete pending (%d).\n",
138 Debug( LDAP_DEBUG_TRACE,
139 "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
140 rw ? "w" : "r", id, refcnt );
144 cache_entry_private_destroy( e );
149 /* free cache mutex */
150 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
153 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
154 "cache_return_entry_rw: (%ld): deleted (%d)\n",
157 Debug( LDAP_DEBUG_TRACE,
158 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
159 rw ? "w" : "r", id, refcnt );
164 /* free cache mutex */
165 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
168 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
169 "cache_return_entry_rw: ID %ld:%s returned (%d)\n",
170 id, rw ? "w": "r", refcnt ));
172 Debug( LDAP_DEBUG_TRACE,
173 "====> cache_return_entry_%s( %ld ): returned (%d)\n",
174 rw ? "w" : "r", id, refcnt);
179 #define LRU_DELETE( cache, e ) do { \
180 if ( LEI(e)->lei_lruprev != NULL ) { \
181 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
183 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
185 if ( LEI(e)->lei_lrunext != NULL ) { \
186 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
188 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
192 #define LRU_ADD( cache, e ) do { \
193 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
194 if ( LEI(e)->lei_lrunext != NULL ) { \
195 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
197 (cache)->c_lruhead = (e); \
198 LEI(e)->lei_lruprev = NULL; \
199 if ( (cache)->c_lrutail == NULL ) { \
200 (cache)->c_lrutail = (e); \
205 * cache_add_entry_rw - create and lock an entry in the cache
206 * returns: 0 entry has been created and locked
207 * 1 entry already existed
208 * -1 something bad happened
221 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
222 "cache_add_entry_rw: add (%s):%s to cache\n",
223 e->e_dn, rw ? "w" : "r" ));
225 /* set cache mutex */
226 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
228 assert( e->e_private == NULL );
230 if( cache_entry_private_init(e) != 0 ) {
231 /* free cache mutex */
232 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
235 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
236 "cache_add_entry_rw: add (%s):%ld private init failed!\n",
239 Debug( LDAP_DEBUG_ANY,
240 "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
241 e->e_id, e->e_dn, 0 );
247 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
248 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
250 /* free cache mutex */
251 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
254 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
255 "cache_add_entry: (%s):%ld already in cache.\n",
258 Debug( LDAP_DEBUG_TRACE,
259 "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
260 e->e_id, e->e_dn, 0 );
263 cache_entry_private_destroy(e);
269 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
270 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
273 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
274 "cache_add_entry: (%s):%ls already in cache.\n",
277 Debug( LDAP_DEBUG_ANY,
278 "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
279 e->e_id, e->e_dn, 0 );
282 /* delete from dn tree inserted above */
283 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
284 (AVL_CMP) entry_dn_cmp ) == NULL )
287 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
288 "cache_add_entry: can't delete (%s) from cache.\n",
291 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
296 cache_entry_private_destroy(e);
298 /* free cache mutex */
299 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
303 /* put the entry into 'CREATING' state */
304 /* will be marked after when entry is returned */
305 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
306 LEI(e)->lei_refcnt = 1;
310 if ( ++cache->c_cursize > cache->c_maxsize ) {
312 * find the lru entry not currently in use and delete it.
313 * in case a lot of entries are in use, only look at the
314 * first 10 on the tail of the list.
317 while ( cache->c_lrutail != NULL &&
318 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
321 /* move this in-use entry to the front of the q */
322 ee = cache->c_lrutail;
323 LRU_DELETE( cache, ee );
324 LRU_ADD( cache, ee );
329 * found at least one to delete - try to get back under
330 * the max cache size.
332 while ( cache->c_lrutail != NULL &&
333 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
334 cache->c_cursize > cache->c_maxsize )
336 e = cache->c_lrutail;
338 /* delete from cache and lru q */
339 /* XXX do we need rc ? */
340 rc = cache_delete_entry_internal( cache, e );
341 cache_entry_private_destroy( e );
346 /* free cache mutex */
347 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
352 * cache_update_entry - update a LOCKED entry which has been deleted.
353 * returns: 0 entry has been created and locked
354 * 1 entry already existed
355 * -1 something bad happened
366 /* set cache mutex */
367 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
369 assert( e->e_private );
371 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
372 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
375 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
376 "cache_update_entry: (%s):%ld already in dn cache\n",
379 Debug( LDAP_DEBUG_TRACE,
380 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
381 e->e_id, e->e_dn, 0 );
384 /* free cache mutex */
385 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
390 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
391 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
394 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
395 "cache_update_entry: (%s)%ld already in id cache\n",
398 Debug( LDAP_DEBUG_ANY,
399 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
400 e->e_id, e->e_dn, 0 );
403 /* delete from dn tree inserted above */
404 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
405 (AVL_CMP) entry_dn_cmp ) == NULL )
408 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
409 "cache_update_entry: can't delete (%s)%ld from dn cache.\n",
412 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
417 /* free cache mutex */
418 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
422 /* put the entry into 'CREATING' state */
423 /* will be marked after when entry is returned */
424 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
428 if ( ++cache->c_cursize > cache->c_maxsize ) {
430 * find the lru entry not currently in use and delete it.
431 * in case a lot of entries are in use, only look at the
432 * first 10 on the tail of the list.
435 while ( cache->c_lrutail != NULL &&
436 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
439 /* move this in-use entry to the front of the q */
440 ee = cache->c_lrutail;
441 LRU_DELETE( cache, ee );
442 LRU_ADD( cache, ee );
447 * found at least one to delete - try to get back under
448 * the max cache size.
450 while ( cache->c_lrutail != NULL &&
451 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
452 cache->c_cursize > cache->c_maxsize )
454 e = cache->c_lrutail;
456 /* delete from cache and lru q */
457 /* XXX do we need rc ? */
458 rc = cache_delete_entry_internal( cache, e );
459 cache_entry_private_destroy( e );
464 /* free cache mutex */
465 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
470 cache_find_entry_ndn2id(
480 /* this function is always called with normalized DN */
484 /* set cache mutex */
485 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
487 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
488 (AVL_CMP) entry_dn_cmp )) != NULL )
494 * ep now points to an unlocked entry
495 * we do not need to lock the entry if we only
496 * check the state, refcnt, LRU, and id.
498 assert( ep->e_private );
502 state = LEI(ep)->lei_state;
505 * entry is deleted or not fully created yet
507 if ( state != CACHE_ENTRY_READY ) {
508 assert(state != CACHE_ENTRY_UNDEFINED);
510 /* free cache mutex */
511 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
514 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
515 "cache_find_entry_dn2id: (%s) %ld not ready: %d\n",
516 ndn->bv_val, id, state ));
518 Debug(LDAP_DEBUG_TRACE,
519 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
520 ndn->bv_val, id, state);
523 ldap_pvt_thread_yield();
528 LRU_DELETE( cache, ep );
529 LRU_ADD( cache, ep );
531 /* free cache mutex */
532 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
535 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
536 "cache_find_entry_dn2id: (%s): %ld %d tries\n",
537 ndn->bv_val, id, count ));
539 Debug(LDAP_DEBUG_TRACE,
540 "====> cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
541 ndn->bv_val, id, count);
545 /* free cache mutex */
546 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
554 * cache_find_entry_id - find an entry in the cache, given id
571 /* set cache mutex */
572 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
574 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
575 (AVL_CMP) entry_id_cmp )) != NULL )
582 assert( ep->e_private );
585 state = LEI(ep)->lei_state;
588 * entry is deleted or not fully created yet
590 if ( state != CACHE_ENTRY_READY ) {
591 assert(state != CACHE_ENTRY_UNDEFINED);
593 /* free cache mutex */
594 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
597 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
598 "cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
602 Debug(LDAP_DEBUG_TRACE,
603 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
607 ldap_pvt_thread_yield();
612 LRU_DELETE( cache, ep );
613 LRU_ADD( cache, ep );
615 LEI(ep)->lei_refcnt++;
617 /* free cache mutex */
618 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
621 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
622 "cache_find_entry_id: %ld -> %s found %d tries.\n",
623 ep_id, ep->e_dn, count ));
625 Debug(LDAP_DEBUG_TRACE,
626 "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
627 ep_id, ep->e_dn, count);
633 /* free cache mutex */
634 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
640 * cache_delete_entry - delete the entry e from the cache. the caller
641 * should have obtained e (increasing its ref count) via a call to one
642 * of the cache_find_* routines. the caller should *not* call the
643 * cache_return_entry() routine prior to calling cache_delete_entry().
644 * it performs this function.
646 * returns: 0 e was deleted ok
647 * 1 e was not in the cache
648 * -1 something bad happened
658 /* set cache mutex */
659 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
661 assert( e->e_private );
664 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
665 "cache_delete_entry: delete %ld.\n", e->e_id ));
667 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
671 rc = cache_delete_entry_internal( cache, e );
673 /* free cache mutex */
674 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
679 cache_delete_entry_internal(
684 int rc = 0; /* return code */
687 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
694 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
705 LRU_DELETE( cache, e );
709 * flag entry to be freed later by a call to cache_return_entry()
711 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
717 cache_release_all( Cache *cache )
722 /* set cache mutex */
723 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
726 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
727 "cache_release_all: enter\n" ));
729 Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
733 while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
734 /* delete from cache and lru q */
735 /* XXX do we need rc ? */
736 rc = cache_delete_entry_internal( cache, e );
737 cache_entry_private_destroy( e );
741 if ( cache->c_cursize ) {
743 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
744 "cache_release_all: Entry cache could not be emptied.\n" ));
746 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
750 /* free cache mutex */
751 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
756 lru_print( Cache *cache )
760 fprintf( stderr, "LRU queue (head to tail):\n" );
761 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
762 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
763 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
765 fprintf( stderr, "LRU queue (tail to head):\n" );
766 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
767 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
768 e->e_dn, e->e_id, LEI(e)->lei_refcnt );