1 /* cache.c - routines to maintain an in-core cache of entries */
4 * Copyright 1998-2003 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, 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, 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, DETAIL1,
154 "cache_return_entry_rw: (%ld): deleted (%d)\n", id, refcnt, 0 );
156 Debug( LDAP_DEBUG_TRACE,
157 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
158 rw ? "w" : "r", id, refcnt );
163 /* free cache mutex */
164 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
167 LDAP_LOG( CACHE, DETAIL1,
168 "cache_return_entry_rw: ID %ld:%s returned (%d)\n",
169 id, rw ? "w": "r", refcnt );
171 Debug( LDAP_DEBUG_TRACE,
172 "====> cache_return_entry_%s( %ld ): returned (%d)\n",
173 rw ? "w" : "r", id, refcnt);
178 #define LRU_DELETE( cache, e ) do { \
179 if ( LEI(e)->lei_lruprev != NULL ) { \
180 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
182 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
184 if ( LEI(e)->lei_lrunext != NULL ) { \
185 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
187 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
191 #define LRU_ADD( cache, e ) do { \
192 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
193 if ( LEI(e)->lei_lrunext != NULL ) { \
194 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
196 (cache)->c_lruhead = (e); \
197 LEI(e)->lei_lruprev = NULL; \
198 if ( (cache)->c_lrutail == NULL ) { \
199 (cache)->c_lrutail = (e); \
204 * cache_add_entry_rw - create and lock an entry in the cache
205 * returns: 0 entry has been created and locked
206 * 1 entry already existed
207 * -1 something bad happened
220 LDAP_LOG( CACHE, ENTRY,
221 "cache_add_entry_rw: add (%s):%s to cache\n",
222 e->e_dn, rw ? "w" : "r", 0 );
224 /* set cache mutex */
225 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
227 assert( e->e_private == NULL );
229 if( cache_entry_private_init(e) != 0 ) {
230 /* free cache mutex */
231 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
234 LDAP_LOG( CACHE, ERR,
235 "cache_add_entry_rw: add (%s):%ld private init failed!\n",
236 e->e_dn, e->e_id, 0 );
238 Debug( LDAP_DEBUG_ANY,
239 "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
240 e->e_id, e->e_dn, 0 );
246 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
247 entry_dn_cmp, avl_dup_error ) != 0 )
249 /* free cache mutex */
250 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
253 LDAP_LOG( CACHE, DETAIL1,
254 "cache_add_entry: (%s):%ld already in cache.\n",
255 e->e_dn, e->e_id, 0 );
257 Debug( LDAP_DEBUG_TRACE,
258 "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
259 e->e_id, e->e_dn, 0 );
262 cache_entry_private_destroy(e);
268 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
269 entry_id_cmp, avl_dup_error ) != 0 )
272 LDAP_LOG( CACHE, DETAIL1,
273 "cache_add_entry: (%s):%ls already in cache.\n",
274 e->e_dn, e->e_id, 0 );
276 Debug( LDAP_DEBUG_ANY,
277 "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
278 e->e_id, e->e_dn, 0 );
281 /* delete from dn tree inserted above */
282 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
283 entry_dn_cmp ) == NULL )
286 LDAP_LOG( CACHE, INFO,
287 "cache_add_entry: can't delete (%s) from cache.\n",
290 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
295 cache_entry_private_destroy(e);
297 /* free cache mutex */
298 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
302 /* put the entry into 'CREATING' state */
303 /* will be marked after when entry is returned */
304 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
305 LEI(e)->lei_refcnt = 1;
309 if ( ++cache->c_cursize > cache->c_maxsize ) {
311 * find the lru entry not currently in use and delete it.
312 * in case a lot of entries are in use, only look at the
313 * first 10 on the tail of the list.
316 while ( cache->c_lrutail != NULL &&
317 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
320 /* move this in-use entry to the front of the q */
321 ee = cache->c_lrutail;
322 LRU_DELETE( cache, ee );
323 LRU_ADD( cache, ee );
328 * found at least one to delete - try to get back under
329 * the max cache size.
331 while ( cache->c_lrutail != NULL &&
332 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
333 cache->c_cursize > cache->c_maxsize )
335 e = cache->c_lrutail;
337 /* delete from cache and lru q */
338 /* XXX do we need rc ? */
339 rc = cache_delete_entry_internal( cache, e );
340 cache_entry_private_destroy( e );
345 /* free cache mutex */
346 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
351 * cache_update_entry - update a LOCKED entry which has been deleted.
352 * returns: 0 entry has been created and locked
353 * 1 entry already existed
354 * -1 something bad happened
365 /* set cache mutex */
366 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
368 assert( e->e_private );
370 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
371 entry_dn_cmp, avl_dup_error ) != 0 )
374 LDAP_LOG( CACHE, DETAIL1,
375 "cache_update_entry: (%s):%ld already in dn cache\n",
376 e->e_dn, e->e_id, 0 );
378 Debug( LDAP_DEBUG_TRACE,
379 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
380 e->e_id, e->e_dn, 0 );
383 /* free cache mutex */
384 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
389 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
390 entry_id_cmp, avl_dup_error ) != 0 )
393 LDAP_LOG( CACHE, DETAIL1,
394 "cache_update_entry: (%s)%ld already in id cache\n",
395 e->e_dn, e->e_id, 0 );
397 Debug( LDAP_DEBUG_ANY,
398 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
399 e->e_id, e->e_dn, 0 );
402 /* delete from dn tree inserted above */
403 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
404 entry_dn_cmp ) == NULL )
407 LDAP_LOG( CACHE, INFO,
408 "cache_update_entry: can't delete (%s)%ld from dn cache.\n",
409 e->e_dn, e->e_id, 0 );
411 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
416 /* free cache mutex */
417 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
421 /* put the entry into 'CREATING' state */
422 /* will be marked after when entry is returned */
423 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
427 if ( ++cache->c_cursize > cache->c_maxsize ) {
429 * find the lru entry not currently in use and delete it.
430 * in case a lot of entries are in use, only look at the
431 * first 10 on the tail of the list.
434 while ( cache->c_lrutail != NULL &&
435 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
438 /* move this in-use entry to the front of the q */
439 ee = cache->c_lrutail;
440 LRU_DELETE( cache, ee );
441 LRU_ADD( cache, ee );
446 * found at least one to delete - try to get back under
447 * the max cache size.
449 while ( cache->c_lrutail != NULL &&
450 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
451 cache->c_cursize > cache->c_maxsize )
453 e = cache->c_lrutail;
455 /* delete from cache and lru q */
456 /* XXX do we need rc ? */
457 rc = cache_delete_entry_internal( cache, e );
458 cache_entry_private_destroy( e );
463 /* free cache mutex */
464 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
469 cache_find_entry_ndn2id(
479 /* this function is always called with normalized DN */
483 /* set cache mutex */
484 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
486 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
487 entry_dn_cmp )) != NULL )
493 * ep now points to an unlocked entry
494 * we do not need to lock the entry if we only
495 * check the state, refcnt, LRU, and id.
497 assert( ep->e_private );
501 state = LEI(ep)->lei_state;
504 * entry is deleted or not fully created yet
506 if ( state != CACHE_ENTRY_READY ) {
507 assert(state != CACHE_ENTRY_UNDEFINED);
509 /* free cache mutex */
510 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
513 LDAP_LOG( CACHE, INFO,
514 "cache_find_entry_dn2id: (%s) %ld not ready: %d\n",
515 ndn->bv_val, id, state );
517 Debug(LDAP_DEBUG_TRACE,
518 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
519 ndn->bv_val, id, state);
522 ldap_pvt_thread_yield();
527 LRU_DELETE( cache, ep );
528 LRU_ADD( cache, ep );
530 /* free cache mutex */
531 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
534 LDAP_LOG( CACHE, DETAIL1,
535 "cache_find_entry_dn2id: (%s): %ld %d tries\n",
536 ndn->bv_val, id, count );
538 Debug(LDAP_DEBUG_TRACE,
539 "====> cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
540 ndn->bv_val, id, count);
544 /* free cache mutex */
545 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
553 * cache_find_entry_id - find an entry in the cache, given id
570 /* set cache mutex */
571 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
573 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
574 entry_id_cmp )) != NULL )
581 assert( ep->e_private );
584 state = LEI(ep)->lei_state;
587 * entry is deleted or not fully created yet
589 if ( state != CACHE_ENTRY_READY ) {
590 assert(state != CACHE_ENTRY_UNDEFINED);
592 /* free cache mutex */
593 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
596 LDAP_LOG( CACHE, INFO,
597 "cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
601 Debug(LDAP_DEBUG_TRACE,
602 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
606 ldap_pvt_thread_yield();
611 LRU_DELETE( cache, ep );
612 LRU_ADD( cache, ep );
614 LEI(ep)->lei_refcnt++;
616 /* free cache mutex */
617 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
620 LDAP_LOG( CACHE, DETAIL1,
621 "cache_find_entry_id: %ld -> %s found %d tries.\n",
622 ep_id, ep->e_dn, count );
624 Debug(LDAP_DEBUG_TRACE,
625 "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
626 ep_id, ep->e_dn, count);
632 /* free cache mutex */
633 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
639 * cache_delete_entry - delete the entry e from the cache. the caller
640 * should have obtained e (increasing its ref count) via a call to one
641 * of the cache_find_* routines. the caller should *not* call the
642 * cache_return_entry() routine prior to calling cache_delete_entry().
643 * it performs this function.
645 * returns: 0 e was deleted ok
646 * 1 e was not in the cache
647 * -1 something bad happened
657 /* set cache mutex */
658 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
660 assert( e->e_private );
663 LDAP_LOG( CACHE, ENTRY,
664 "cache_delete_entry: delete %ld.\n", e->e_id, 0, 0 );
666 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
670 rc = cache_delete_entry_internal( cache, e );
672 /* free cache mutex */
673 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
678 cache_delete_entry_internal(
683 int rc = 0; /* return code */
686 if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp ) == NULL )
692 if ( avl_delete( &cache->c_idtree, (caddr_t) e, entry_id_cmp ) == NULL )
702 LRU_DELETE( cache, e );
706 * flag entry to be freed later by a call to cache_return_entry()
708 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
714 cache_release_all( Cache *cache )
719 /* set cache mutex */
720 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
723 LDAP_LOG( CACHE, ENTRY, "cache_release_all: enter\n" , 0, 0, 0);
725 Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
729 while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
730 /* delete from cache and lru q */
731 /* XXX do we need rc ? */
732 rc = cache_delete_entry_internal( cache, e );
733 cache_entry_private_destroy( e );
737 if ( cache->c_cursize ) {
739 LDAP_LOG( CACHE, INFO,
740 "cache_release_all: Entry cache could not be emptied.\n", 0, 0, 0);
742 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
746 /* free cache mutex */
747 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
752 lru_print( Cache *cache )
756 fprintf( stderr, "LRU queue (head to tail):\n" );
757 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
758 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
759 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
761 fprintf( stderr, "LRU queue (tail to head):\n" );
762 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
763 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
764 e->e_dn, e->e_id, LEI(e)->lei_refcnt );