1 /* cache.c - routines to maintain an in-core cache of entries */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
27 #include "back-ldbm.h"
29 /* LDBM backend specific entry info -- visible only to the cache */
30 typedef struct ldbm_entry_info {
32 * These items are specific to the LDBM backend and should
33 * be hidden. Backend cache lock required to access.
35 int lei_state; /* for the cache */
36 #define CACHE_ENTRY_UNDEFINED 0
37 #define CACHE_ENTRY_CREATING 1
38 #define CACHE_ENTRY_READY 2
39 #define CACHE_ENTRY_DELETED 3
40 #define CACHE_ENTRY_COMMITTED 4
42 int lei_refcnt; /* # threads ref'ing this entry */
43 Entry *lei_lrunext; /* for cache lru list */
47 #define LEI(e) ((EntryInfo *) ((e)->e_private))
49 static int cache_delete_entry_internal(Cache *cache, Entry *e);
51 static void lru_print(Cache *cache);
55 cache_entry_private_init( Entry*e )
57 assert( e->e_private == NULL );
59 if( e->e_private != NULL ) {
60 /* this should never happen */
64 e->e_private = ch_calloc(1, sizeof(struct ldbm_entry_info));
70 * marks an entry in CREATING state as committed, so it is really returned
71 * to the cache. Otherwise an entry in CREATING state is removed.
72 * Makes e_private be destroyed at the following cache_return_entry_w,
73 * but lets the entry untouched (owned by someone else)
76 cache_entry_commit( Entry *e )
79 assert( e->e_private );
80 assert( LEI(e)->lei_state == CACHE_ENTRY_CREATING );
81 /* assert( LEI(e)->lei_refcnt == 1 ); */
83 LEI(e)->lei_state = CACHE_ENTRY_COMMITTED;
87 cache_entry_private_destroy( Entry*e )
89 assert( e->e_private );
97 cache_return_entry_rw( Cache *cache, Entry *e, int rw )
100 int refcnt, freeit = 1;
102 if ( slapMode != SLAP_SERVER_MODE ) {
106 /* set cache mutex */
107 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
109 assert( e->e_private );
112 refcnt = --LEI(e)->lei_refcnt;
115 * if the entry is returned when in CREATING state, it is deleted
116 * but not freed because it may belong to someone else (do_add,
119 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
120 cache_delete_entry_internal( cache, e );
122 /* now the entry is in DELETED state */
125 if ( LEI(e)->lei_state == CACHE_ENTRY_COMMITTED ) {
126 LEI(e)->lei_state = CACHE_ENTRY_READY;
128 /* free cache mutex */
129 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
132 LDAP_LOG( CACHE, DETAIL1,
133 "cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
134 id, rw ? "w" : "r", refcnt );
136 Debug( LDAP_DEBUG_TRACE,
137 "====> cache_return_entry_%s( %ld ): created (%d)\n",
138 rw ? "w" : "r", id, refcnt );
141 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
143 /* free cache mutex */
144 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
147 LDAP_LOG( CACHE, DETAIL1,
148 "cache_return_entry_rw: %ld, delete pending (%d).\n",
151 Debug( LDAP_DEBUG_TRACE,
152 "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
153 rw ? "w" : "r", id, refcnt );
157 cache_entry_private_destroy( e );
162 /* free cache mutex */
163 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
166 LDAP_LOG( CACHE, DETAIL1,
167 "cache_return_entry_rw: (%ld): deleted (%d)\n", id, refcnt, 0 );
169 Debug( LDAP_DEBUG_TRACE,
170 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
171 rw ? "w" : "r", id, refcnt );
176 /* free cache mutex */
177 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
180 LDAP_LOG( CACHE, DETAIL1,
181 "cache_return_entry_rw: ID %ld:%s returned (%d)\n",
182 id, rw ? "w": "r", refcnt );
184 Debug( LDAP_DEBUG_TRACE,
185 "====> cache_return_entry_%s( %ld ): returned (%d)\n",
186 rw ? "w" : "r", id, refcnt);
191 #define LRU_DELETE( cache, e ) do { \
192 if ( LEI(e)->lei_lruprev != NULL ) { \
193 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
195 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
197 if ( LEI(e)->lei_lrunext != NULL ) { \
198 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
200 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
204 #define LRU_ADD( cache, e ) do { \
205 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
206 if ( LEI(e)->lei_lrunext != NULL ) { \
207 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
209 (cache)->c_lruhead = (e); \
210 LEI(e)->lei_lruprev = NULL; \
211 if ( (cache)->c_lrutail == NULL ) { \
212 (cache)->c_lrutail = (e); \
217 * cache_add_entry_rw - create and lock an entry in the cache
218 * returns: 0 entry has been created and locked
219 * 1 entry already existed
220 * -1 something bad happened
233 LDAP_LOG( CACHE, ENTRY,
234 "cache_add_entry_rw: add (%s):%s to cache\n",
235 e->e_dn, rw ? "w" : "r", 0 );
237 /* set cache mutex */
238 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
240 assert( e->e_private == NULL );
242 if( cache_entry_private_init(e) != 0 ) {
243 /* free cache mutex */
244 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
247 LDAP_LOG( CACHE, ERR,
248 "cache_add_entry_rw: add (%s):%ld private init failed!\n",
249 e->e_dn, e->e_id, 0 );
251 Debug( LDAP_DEBUG_ANY,
252 "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
253 e->e_id, e->e_dn, 0 );
259 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
260 entry_dn_cmp, avl_dup_error ) != 0 )
262 /* free cache mutex */
263 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
266 LDAP_LOG( CACHE, DETAIL1,
267 "cache_add_entry: (%s):%ld already in cache.\n",
268 e->e_dn, e->e_id, 0 );
270 Debug( LDAP_DEBUG_TRACE,
271 "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
272 e->e_id, e->e_dn, 0 );
275 cache_entry_private_destroy(e);
281 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
282 entry_id_cmp, avl_dup_error ) != 0 )
285 LDAP_LOG( CACHE, DETAIL1,
286 "cache_add_entry: (%s):%ls already in cache.\n",
287 e->e_dn, e->e_id, 0 );
289 Debug( LDAP_DEBUG_ANY,
290 "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
291 e->e_id, e->e_dn, 0 );
294 /* delete from dn tree inserted above */
295 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
296 entry_dn_cmp ) == NULL )
299 LDAP_LOG( CACHE, INFO,
300 "cache_add_entry: can't delete (%s) from cache.\n",
303 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
308 cache_entry_private_destroy(e);
310 /* free cache mutex */
311 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
315 /* put the entry into 'CREATING' state */
316 /* will be marked after when entry is returned */
317 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
318 LEI(e)->lei_refcnt = 1;
322 if ( ++cache->c_cursize > cache->c_maxsize ) {
324 * find the lru entry not currently in use and delete it.
325 * in case a lot of entries are in use, only look at the
326 * first 10 on the tail of the list.
329 while ( cache->c_lrutail != NULL &&
330 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
333 /* move this in-use entry to the front of the q */
334 ee = cache->c_lrutail;
335 LRU_DELETE( cache, ee );
336 LRU_ADD( cache, ee );
341 * found at least one to delete - try to get back under
342 * the max cache size.
344 while ( cache->c_lrutail != NULL &&
345 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
346 cache->c_cursize > cache->c_maxsize )
348 e = cache->c_lrutail;
350 /* delete from cache and lru q */
351 /* XXX do we need rc ? */
352 rc = cache_delete_entry_internal( cache, e );
353 cache_entry_private_destroy( e );
358 /* free cache mutex */
359 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
364 * cache_update_entry - update a LOCKED entry which has been deleted.
365 * returns: 0 entry has been created and locked
366 * 1 entry already existed
367 * -1 something bad happened
378 /* set cache mutex */
379 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
381 assert( e->e_private );
383 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
384 entry_dn_cmp, avl_dup_error ) != 0 )
387 LDAP_LOG( CACHE, DETAIL1,
388 "cache_update_entry: (%s):%ld already in dn cache\n",
389 e->e_dn, e->e_id, 0 );
391 Debug( LDAP_DEBUG_TRACE,
392 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
393 e->e_id, e->e_dn, 0 );
396 /* free cache mutex */
397 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
402 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
403 entry_id_cmp, avl_dup_error ) != 0 )
406 LDAP_LOG( CACHE, DETAIL1,
407 "cache_update_entry: (%s)%ld already in id cache\n",
408 e->e_dn, e->e_id, 0 );
410 Debug( LDAP_DEBUG_ANY,
411 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
412 e->e_id, e->e_dn, 0 );
415 /* delete from dn tree inserted above */
416 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
417 entry_dn_cmp ) == NULL )
420 LDAP_LOG( CACHE, INFO,
421 "cache_update_entry: can't delete (%s)%ld from dn cache.\n",
422 e->e_dn, e->e_id, 0 );
424 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
429 /* free cache mutex */
430 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
434 /* put the entry into 'CREATING' state */
435 /* will be marked after when entry is returned */
436 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
440 if ( ++cache->c_cursize > cache->c_maxsize ) {
442 * find the lru entry not currently in use and delete it.
443 * in case a lot of entries are in use, only look at the
444 * first 10 on the tail of the list.
447 while ( cache->c_lrutail != NULL &&
448 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
451 /* move this in-use entry to the front of the q */
452 ee = cache->c_lrutail;
453 LRU_DELETE( cache, ee );
454 LRU_ADD( cache, ee );
459 * found at least one to delete - try to get back under
460 * the max cache size.
462 while ( cache->c_lrutail != NULL &&
463 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
464 cache->c_cursize > cache->c_maxsize )
466 e = cache->c_lrutail;
468 /* delete from cache and lru q */
469 /* XXX do we need rc ? */
470 rc = cache_delete_entry_internal( cache, e );
471 cache_entry_private_destroy( e );
476 /* free cache mutex */
477 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
482 cache_find_entry_ndn2id(
492 /* this function is always called with normalized DN */
496 /* set cache mutex */
497 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
499 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
500 entry_dn_cmp )) != NULL )
506 * ep now points to an unlocked entry
507 * we do not need to lock the entry if we only
508 * check the state, refcnt, LRU, and id.
510 assert( ep->e_private );
514 state = LEI(ep)->lei_state;
517 * entry is deleted or not fully created yet
519 if ( state != CACHE_ENTRY_READY ) {
520 assert(state != CACHE_ENTRY_UNDEFINED);
522 /* free cache mutex */
523 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
526 LDAP_LOG( CACHE, INFO,
527 "cache_find_entry_ndn2id: (%s) %ld not ready: %d\n",
528 ndn->bv_val, id, state );
530 Debug(LDAP_DEBUG_TRACE,
531 "====> cache_find_entry_ndn2id(\"%s\"): %ld (not ready) %d\n",
532 ndn->bv_val, id, state);
535 ldap_pvt_thread_yield();
540 LRU_DELETE( cache, ep );
541 LRU_ADD( cache, ep );
543 /* free cache mutex */
544 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
547 LDAP_LOG( CACHE, DETAIL1,
548 "cache_find_entry_ndn2id: (%s): %ld %d tries\n",
549 ndn->bv_val, id, count );
551 Debug(LDAP_DEBUG_TRACE,
552 "====> cache_find_entry_ndn2id(\"%s\"): %ld (%d tries)\n",
553 ndn->bv_val, id, count);
557 /* free cache mutex */
558 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
566 * cache_find_entry_id - find an entry in the cache, given id
583 /* set cache mutex */
584 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
586 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
587 entry_id_cmp )) != NULL )
594 assert( ep->e_private );
597 state = LEI(ep)->lei_state;
600 * entry is deleted or not fully created yet
602 if ( state != CACHE_ENTRY_READY ) {
603 assert(state != CACHE_ENTRY_UNDEFINED);
605 /* free cache mutex */
606 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
609 LDAP_LOG( CACHE, INFO,
610 "cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
614 Debug(LDAP_DEBUG_TRACE,
615 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
619 ldap_pvt_thread_yield();
624 LRU_DELETE( cache, ep );
625 LRU_ADD( cache, ep );
627 LEI(ep)->lei_refcnt++;
629 /* free cache mutex */
630 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
633 LDAP_LOG( CACHE, DETAIL1,
634 "cache_find_entry_id: %ld -> %s found %d tries.\n",
635 ep_id, ep->e_dn, count );
637 Debug(LDAP_DEBUG_TRACE,
638 "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
639 ep_id, ep->e_dn, count);
645 /* free cache mutex */
646 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
652 * cache_delete_entry - delete the entry e from the cache. the caller
653 * should have obtained e (increasing its ref count) via a call to one
654 * of the cache_find_* routines. the caller should *not* call the
655 * cache_return_entry() routine prior to calling cache_delete_entry().
656 * it performs this function.
658 * returns: 0 e was deleted ok
659 * 1 e was not in the cache
660 * -1 something bad happened
670 /* set cache mutex */
671 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
673 assert( e->e_private );
676 LDAP_LOG( CACHE, ENTRY,
677 "cache_delete_entry: delete %ld.\n", e->e_id, 0, 0 );
679 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
683 rc = cache_delete_entry_internal( cache, e );
685 /* free cache mutex */
686 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
691 cache_delete_entry_internal(
696 int rc = 0; /* return code */
699 if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp ) == NULL )
705 if ( avl_delete( &cache->c_idtree, (caddr_t) e, entry_id_cmp ) == NULL )
715 LRU_DELETE( cache, e );
719 * flag entry to be freed later by a call to cache_return_entry()
721 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
727 cache_release_all( Cache *cache )
732 /* set cache mutex */
733 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
736 LDAP_LOG( CACHE, ENTRY, "cache_release_all: enter\n" , 0, 0, 0);
738 Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
742 while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
743 /* delete from cache and lru q */
744 /* XXX do we need rc ? */
745 rc = cache_delete_entry_internal( cache, e );
746 cache_entry_private_destroy( e );
750 if ( cache->c_cursize ) {
752 LDAP_LOG( CACHE, INFO,
753 "cache_release_all: Entry cache could not be emptied.\n", 0, 0, 0);
755 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
759 /* free cache mutex */
760 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
765 lru_print( Cache *cache )
769 fprintf( stderr, "LRU queue (head to tail):\n" );
770 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
771 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
772 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
774 fprintf( stderr, "LRU queue (tail to head):\n" );
775 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
776 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
777 e->e_dn, e->e_id, LEI(e)->lei_refcnt );