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 /* set cache mutex */
103 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
105 assert( e->e_private );
108 refcnt = --LEI(e)->lei_refcnt;
111 * if the entry is returned when in CREATING state, it is deleted
112 * but not freed because it may belong to someone else (do_add,
115 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
116 cache_delete_entry_internal( cache, e );
118 /* now the entry is in DELETED state */
121 if ( LEI(e)->lei_state == CACHE_ENTRY_COMMITTED ) {
122 LEI(e)->lei_state = CACHE_ENTRY_READY;
124 /* free cache mutex */
125 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
128 LDAP_LOG( CACHE, DETAIL1,
129 "cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
130 id, rw ? "w" : "r", refcnt );
132 Debug( LDAP_DEBUG_TRACE,
133 "====> cache_return_entry_%s( %ld ): created (%d)\n",
134 rw ? "w" : "r", id, refcnt );
137 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
139 /* free cache mutex */
140 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
143 LDAP_LOG( CACHE, DETAIL1,
144 "cache_return_entry_rw: %ld, delete pending (%d).\n",
147 Debug( LDAP_DEBUG_TRACE,
148 "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
149 rw ? "w" : "r", id, refcnt );
153 cache_entry_private_destroy( e );
158 /* free cache mutex */
159 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
162 LDAP_LOG( CACHE, DETAIL1,
163 "cache_return_entry_rw: (%ld): deleted (%d)\n", id, refcnt, 0 );
165 Debug( LDAP_DEBUG_TRACE,
166 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
167 rw ? "w" : "r", id, refcnt );
172 /* free cache mutex */
173 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
176 LDAP_LOG( CACHE, DETAIL1,
177 "cache_return_entry_rw: ID %ld:%s returned (%d)\n",
178 id, rw ? "w": "r", refcnt );
180 Debug( LDAP_DEBUG_TRACE,
181 "====> cache_return_entry_%s( %ld ): returned (%d)\n",
182 rw ? "w" : "r", id, refcnt);
187 #define LRU_DELETE( cache, e ) do { \
188 if ( LEI(e)->lei_lruprev != NULL ) { \
189 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
191 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
193 if ( LEI(e)->lei_lrunext != NULL ) { \
194 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
196 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
200 #define LRU_ADD( cache, e ) do { \
201 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
202 if ( LEI(e)->lei_lrunext != NULL ) { \
203 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
205 (cache)->c_lruhead = (e); \
206 LEI(e)->lei_lruprev = NULL; \
207 if ( (cache)->c_lrutail == NULL ) { \
208 (cache)->c_lrutail = (e); \
213 * cache_add_entry_rw - create and lock an entry in the cache
214 * returns: 0 entry has been created and locked
215 * 1 entry already existed
216 * -1 something bad happened
229 LDAP_LOG( CACHE, ENTRY,
230 "cache_add_entry_rw: add (%s):%s to cache\n",
231 e->e_dn, rw ? "w" : "r", 0 );
233 /* set cache mutex */
234 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
236 assert( e->e_private == NULL );
238 if( cache_entry_private_init(e) != 0 ) {
239 /* free cache mutex */
240 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
243 LDAP_LOG( CACHE, ERR,
244 "cache_add_entry_rw: add (%s):%ld private init failed!\n",
245 e->e_dn, e->e_id, 0 );
247 Debug( LDAP_DEBUG_ANY,
248 "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
249 e->e_id, e->e_dn, 0 );
255 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
256 entry_dn_cmp, avl_dup_error ) != 0 )
258 /* free cache mutex */
259 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
262 LDAP_LOG( CACHE, DETAIL1,
263 "cache_add_entry: (%s):%ld already in cache.\n",
264 e->e_dn, e->e_id, 0 );
266 Debug( LDAP_DEBUG_TRACE,
267 "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
268 e->e_id, e->e_dn, 0 );
271 cache_entry_private_destroy(e);
277 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
278 entry_id_cmp, avl_dup_error ) != 0 )
281 LDAP_LOG( CACHE, DETAIL1,
282 "cache_add_entry: (%s):%ls already in cache.\n",
283 e->e_dn, e->e_id, 0 );
285 Debug( LDAP_DEBUG_ANY,
286 "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
287 e->e_id, e->e_dn, 0 );
290 /* delete from dn tree inserted above */
291 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
292 entry_dn_cmp ) == NULL )
295 LDAP_LOG( CACHE, INFO,
296 "cache_add_entry: can't delete (%s) from cache.\n",
299 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
304 cache_entry_private_destroy(e);
306 /* free cache mutex */
307 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
311 /* put the entry into 'CREATING' state */
312 /* will be marked after when entry is returned */
313 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
314 LEI(e)->lei_refcnt = 1;
318 if ( ++cache->c_cursize > cache->c_maxsize ) {
320 * find the lru entry not currently in use and delete it.
321 * in case a lot of entries are in use, only look at the
322 * first 10 on the tail of the list.
325 while ( cache->c_lrutail != NULL &&
326 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
329 /* move this in-use entry to the front of the q */
330 ee = cache->c_lrutail;
331 LRU_DELETE( cache, ee );
332 LRU_ADD( cache, ee );
337 * found at least one to delete - try to get back under
338 * the max cache size.
340 while ( cache->c_lrutail != NULL &&
341 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
342 cache->c_cursize > cache->c_maxsize )
344 e = cache->c_lrutail;
346 /* delete from cache and lru q */
347 /* XXX do we need rc ? */
348 rc = cache_delete_entry_internal( cache, e );
349 cache_entry_private_destroy( e );
354 /* free cache mutex */
355 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
360 * cache_update_entry - update a LOCKED entry which has been deleted.
361 * returns: 0 entry has been created and locked
362 * 1 entry already existed
363 * -1 something bad happened
374 /* set cache mutex */
375 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
377 assert( e->e_private );
379 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
380 entry_dn_cmp, avl_dup_error ) != 0 )
383 LDAP_LOG( CACHE, DETAIL1,
384 "cache_update_entry: (%s):%ld already in dn cache\n",
385 e->e_dn, e->e_id, 0 );
387 Debug( LDAP_DEBUG_TRACE,
388 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
389 e->e_id, e->e_dn, 0 );
392 /* free cache mutex */
393 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
398 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
399 entry_id_cmp, avl_dup_error ) != 0 )
402 LDAP_LOG( CACHE, DETAIL1,
403 "cache_update_entry: (%s)%ld already in id cache\n",
404 e->e_dn, e->e_id, 0 );
406 Debug( LDAP_DEBUG_ANY,
407 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
408 e->e_id, e->e_dn, 0 );
411 /* delete from dn tree inserted above */
412 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
413 entry_dn_cmp ) == NULL )
416 LDAP_LOG( CACHE, INFO,
417 "cache_update_entry: can't delete (%s)%ld from dn cache.\n",
418 e->e_dn, e->e_id, 0 );
420 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
425 /* free cache mutex */
426 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
430 /* put the entry into 'CREATING' state */
431 /* will be marked after when entry is returned */
432 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
436 if ( ++cache->c_cursize > cache->c_maxsize ) {
438 * find the lru entry not currently in use and delete it.
439 * in case a lot of entries are in use, only look at the
440 * first 10 on the tail of the list.
443 while ( cache->c_lrutail != NULL &&
444 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
447 /* move this in-use entry to the front of the q */
448 ee = cache->c_lrutail;
449 LRU_DELETE( cache, ee );
450 LRU_ADD( cache, ee );
455 * found at least one to delete - try to get back under
456 * the max cache size.
458 while ( cache->c_lrutail != NULL &&
459 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
460 cache->c_cursize > cache->c_maxsize )
462 e = cache->c_lrutail;
464 /* delete from cache and lru q */
465 /* XXX do we need rc ? */
466 rc = cache_delete_entry_internal( cache, e );
467 cache_entry_private_destroy( e );
472 /* free cache mutex */
473 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
478 cache_find_entry_ndn2id(
488 /* this function is always called with normalized DN */
492 /* set cache mutex */
493 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
495 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
496 entry_dn_cmp )) != NULL )
502 * ep now points to an unlocked entry
503 * we do not need to lock the entry if we only
504 * check the state, refcnt, LRU, and id.
506 assert( ep->e_private );
510 state = LEI(ep)->lei_state;
513 * entry is deleted or not fully created yet
515 if ( state != CACHE_ENTRY_READY ) {
516 assert(state != CACHE_ENTRY_UNDEFINED);
518 /* free cache mutex */
519 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
522 LDAP_LOG( CACHE, INFO,
523 "cache_find_entry_ndn2id: (%s) %ld not ready: %d\n",
524 ndn->bv_val, id, state );
526 Debug(LDAP_DEBUG_TRACE,
527 "====> cache_find_entry_ndn2id(\"%s\"): %ld (not ready) %d\n",
528 ndn->bv_val, id, state);
531 ldap_pvt_thread_yield();
536 LRU_DELETE( cache, ep );
537 LRU_ADD( cache, ep );
539 /* free cache mutex */
540 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
543 LDAP_LOG( CACHE, DETAIL1,
544 "cache_find_entry_ndn2id: (%s): %ld %d tries\n",
545 ndn->bv_val, id, count );
547 Debug(LDAP_DEBUG_TRACE,
548 "====> cache_find_entry_ndn2id(\"%s\"): %ld (%d tries)\n",
549 ndn->bv_val, id, count);
553 /* free cache mutex */
554 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
562 * cache_find_entry_id - find an entry in the cache, given id
579 /* set cache mutex */
580 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
582 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
583 entry_id_cmp )) != NULL )
590 assert( ep->e_private );
593 state = LEI(ep)->lei_state;
596 * entry is deleted or not fully created yet
598 if ( state != CACHE_ENTRY_READY ) {
599 assert(state != CACHE_ENTRY_UNDEFINED);
601 /* free cache mutex */
602 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
605 LDAP_LOG( CACHE, INFO,
606 "cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
610 Debug(LDAP_DEBUG_TRACE,
611 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
615 ldap_pvt_thread_yield();
620 LRU_DELETE( cache, ep );
621 LRU_ADD( cache, ep );
623 LEI(ep)->lei_refcnt++;
625 /* free cache mutex */
626 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
629 LDAP_LOG( CACHE, DETAIL1,
630 "cache_find_entry_id: %ld -> %s found %d tries.\n",
631 ep_id, ep->e_dn, count );
633 Debug(LDAP_DEBUG_TRACE,
634 "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
635 ep_id, ep->e_dn, count);
641 /* free cache mutex */
642 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
648 * cache_delete_entry - delete the entry e from the cache. the caller
649 * should have obtained e (increasing its ref count) via a call to one
650 * of the cache_find_* routines. the caller should *not* call the
651 * cache_return_entry() routine prior to calling cache_delete_entry().
652 * it performs this function.
654 * returns: 0 e was deleted ok
655 * 1 e was not in the cache
656 * -1 something bad happened
666 /* set cache mutex */
667 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
669 assert( e->e_private );
672 LDAP_LOG( CACHE, ENTRY,
673 "cache_delete_entry: delete %ld.\n", e->e_id, 0, 0 );
675 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
679 rc = cache_delete_entry_internal( cache, e );
681 /* free cache mutex */
682 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
687 cache_delete_entry_internal(
692 int rc = 0; /* return code */
695 if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp ) == NULL )
701 if ( avl_delete( &cache->c_idtree, (caddr_t) e, entry_id_cmp ) == NULL )
711 LRU_DELETE( cache, e );
715 * flag entry to be freed later by a call to cache_return_entry()
717 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
723 cache_release_all( Cache *cache )
728 /* set cache mutex */
729 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
732 LDAP_LOG( CACHE, ENTRY, "cache_release_all: enter\n" , 0, 0, 0);
734 Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
738 while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
739 /* delete from cache and lru q */
740 /* XXX do we need rc ? */
741 rc = cache_delete_entry_internal( cache, e );
742 cache_entry_private_destroy( e );
746 if ( cache->c_cursize ) {
748 LDAP_LOG( CACHE, INFO,
749 "cache_release_all: Entry cache could not be emptied.\n", 0, 0, 0);
751 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
755 /* free cache mutex */
756 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
761 lru_print( Cache *cache )
765 fprintf( stderr, "LRU queue (head to tail):\n" );
766 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
767 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
768 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
770 fprintf( stderr, "LRU queue (tail to head):\n" );
771 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
772 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
773 e->e_dn, e->e_id, LEI(e)->lei_refcnt );