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-2006 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);
52 static void lru_print(Cache *cache);
57 cache_entry_private_init( Entry*e )
59 assert( e->e_private == NULL );
61 if( e->e_private != NULL ) {
62 /* this should never happen */
66 e->e_private = ch_calloc(1, sizeof(struct ldbm_entry_info));
72 * marks an entry in CREATING state as committed, so it is really returned
73 * to the cache. Otherwise an entry in CREATING state is removed.
74 * Makes e_private be destroyed at the following cache_return_entry_w,
75 * but lets the entry untouched (owned by someone else)
78 cache_entry_commit( Entry *e )
81 assert( e->e_private != NULL );
82 assert( LEI(e)->lei_state == CACHE_ENTRY_CREATING );
83 /* assert( LEI(e)->lei_refcnt == 1 ); */
85 LEI(e)->lei_state = CACHE_ENTRY_COMMITTED;
89 cache_entry_private_destroy( Entry*e )
91 assert( e->e_private != NULL );
99 cache_return_entry_rw( Cache *cache, Entry *e, int rw )
102 int refcnt, freeit = 1;
104 if ( slapMode != SLAP_SERVER_MODE ) {
108 /* set cache mutex */
109 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
111 assert( e->e_private != NULL );
114 refcnt = --LEI(e)->lei_refcnt;
117 * if the entry is returned when in CREATING state, it is deleted
118 * but not freed because it may belong to someone else (do_add,
121 if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
122 cache_delete_entry_internal( cache, e );
124 /* now the entry is in DELETED state */
127 if ( LEI(e)->lei_state == CACHE_ENTRY_COMMITTED ) {
128 LEI(e)->lei_state = CACHE_ENTRY_READY;
130 /* free cache mutex */
131 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
133 Debug( LDAP_DEBUG_TRACE,
134 "====> cache_return_entry_%s( %ld ): created (%d)\n",
135 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 );
142 Debug( LDAP_DEBUG_TRACE,
143 "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
144 rw ? "w" : "r", id, refcnt );
147 cache_entry_private_destroy( e );
152 /* free cache mutex */
153 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
155 Debug( LDAP_DEBUG_TRACE,
156 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
157 rw ? "w" : "r", id, refcnt );
161 /* free cache mutex */
162 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
164 Debug( LDAP_DEBUG_TRACE,
165 "====> cache_return_entry_%s( %ld ): returned (%d)\n",
166 rw ? "w" : "r", id, refcnt);
170 #define LRU_DELETE( cache, e ) do { \
171 if ( LEI(e)->lei_lruprev != NULL ) { \
172 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
174 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
176 if ( LEI(e)->lei_lrunext != NULL ) { \
177 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
179 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
183 #define LRU_ADD( cache, e ) do { \
184 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
185 if ( LEI(e)->lei_lrunext != NULL ) { \
186 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
188 (cache)->c_lruhead = (e); \
189 LEI(e)->lei_lruprev = NULL; \
190 if ( (cache)->c_lrutail == NULL ) { \
191 (cache)->c_lrutail = (e); \
196 * cache_add_entry_rw - create and lock an entry in the cache
197 * returns: 0 entry has been created and locked
198 * 1 entry already existed
199 * -1 something bad happened
211 /* set cache mutex */
212 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
214 assert( e->e_private == NULL );
216 if( cache_entry_private_init(e) != 0 ) {
217 /* free cache mutex */
218 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
220 Debug( LDAP_DEBUG_ANY,
221 "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
222 e->e_id, e->e_dn, 0 );
227 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
228 entry_dn_cmp, avl_dup_error ) != 0 )
230 /* free cache mutex */
231 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
233 Debug( LDAP_DEBUG_TRACE,
234 "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
235 e->e_id, e->e_dn, 0 );
237 cache_entry_private_destroy(e);
243 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
244 entry_id_cmp, avl_dup_error ) != 0 )
246 Debug( LDAP_DEBUG_ANY,
247 "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
248 e->e_id, e->e_dn, 0 );
250 /* delete from dn tree inserted above */
251 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
252 entry_dn_cmp ) == NULL )
254 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
258 cache_entry_private_destroy(e);
260 /* free cache mutex */
261 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
265 /* put the entry into 'CREATING' state */
266 /* will be marked after when entry is returned */
267 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
268 LEI(e)->lei_refcnt = 1;
272 if ( ++cache->c_cursize > cache->c_maxsize ) {
274 * find the lru entry not currently in use and delete it.
275 * in case a lot of entries are in use, only look at the
276 * first 10 on the tail of the list.
279 while ( cache->c_lrutail != NULL &&
280 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
283 /* move this in-use entry to the front of the q */
284 ee = cache->c_lrutail;
285 LRU_DELETE( cache, ee );
286 LRU_ADD( cache, ee );
291 * found at least one to delete - try to get back under
292 * the max cache size.
294 while ( cache->c_lrutail != NULL &&
295 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
296 cache->c_cursize > cache->c_maxsize )
298 e = cache->c_lrutail;
300 /* delete from cache and lru q */
301 /* XXX do we need rc ? */
302 rc = cache_delete_entry_internal( cache, e );
303 cache_entry_private_destroy( e );
308 /* free cache mutex */
309 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
314 * cache_update_entry - update a LOCKED entry which has been deleted.
315 * returns: 0 entry has been created and locked
316 * 1 entry already existed
317 * -1 something bad happened
328 /* set cache mutex */
329 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
331 assert( e->e_private != NULL );
333 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
334 entry_dn_cmp, avl_dup_error ) != 0 )
336 Debug( LDAP_DEBUG_TRACE,
337 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
338 e->e_id, e->e_dn, 0 );
340 /* free cache mutex */
341 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
346 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
347 entry_id_cmp, avl_dup_error ) != 0 )
349 Debug( LDAP_DEBUG_ANY,
350 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
351 e->e_id, e->e_dn, 0 );
353 /* delete from dn tree inserted above */
354 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
355 entry_dn_cmp ) == NULL )
357 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
361 /* free cache mutex */
362 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
366 /* put the entry into 'CREATING' state */
367 /* will be marked after when entry is returned */
368 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
372 if ( ++cache->c_cursize > cache->c_maxsize ) {
374 * find the lru entry not currently in use and delete it.
375 * in case a lot of entries are in use, only look at the
376 * first 10 on the tail of the list.
379 while ( cache->c_lrutail != NULL &&
380 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
383 /* move this in-use entry to the front of the q */
384 ee = cache->c_lrutail;
385 LRU_DELETE( cache, ee );
386 LRU_ADD( cache, ee );
391 * found at least one to delete - try to get back under
392 * the max cache size.
394 while ( cache->c_lrutail != NULL &&
395 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
396 cache->c_cursize > cache->c_maxsize )
398 e = cache->c_lrutail;
400 /* delete from cache and lru q */
401 /* XXX do we need rc ? */
402 rc = cache_delete_entry_internal( cache, e );
403 cache_entry_private_destroy( e );
408 /* free cache mutex */
409 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
414 cache_find_entry_ndn2id(
424 /* this function is always called with normalized DN */
428 /* set cache mutex */
429 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
431 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
432 entry_dn_cmp )) != NULL )
438 * ep now points to an unlocked entry
439 * we do not need to lock the entry if we only
440 * check the state, refcnt, LRU, and id.
442 assert( ep->e_private != NULL );
446 state = LEI(ep)->lei_state;
449 * entry is deleted or not fully created yet
451 if ( state != CACHE_ENTRY_READY ) {
452 assert(state != CACHE_ENTRY_UNDEFINED);
454 /* free cache mutex */
455 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
457 Debug(LDAP_DEBUG_TRACE,
458 "====> cache_find_entry_ndn2id(\"%s\"): %ld (not ready) %d\n",
459 ndn->bv_val, id, state);
461 ldap_pvt_thread_yield();
466 LRU_DELETE( cache, ep );
467 LRU_ADD( cache, ep );
469 /* free cache mutex */
470 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
472 Debug(LDAP_DEBUG_TRACE,
473 "====> cache_find_entry_ndn2id(\"%s\"): %ld (%d tries)\n",
474 ndn->bv_val, id, count);
477 /* free cache mutex */
478 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
486 * cache_find_entry_id - find an entry in the cache, given id
503 /* set cache mutex */
504 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
506 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
507 entry_id_cmp )) != NULL )
514 assert( ep->e_private != NULL );
517 state = LEI(ep)->lei_state;
520 * entry is deleted or not fully created yet
522 if ( state != CACHE_ENTRY_READY ) {
523 assert(state != CACHE_ENTRY_UNDEFINED);
525 /* free cache mutex */
526 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
528 Debug(LDAP_DEBUG_TRACE,
529 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
532 ldap_pvt_thread_yield();
537 LRU_DELETE( cache, ep );
538 LRU_ADD( cache, ep );
540 LEI(ep)->lei_refcnt++;
542 /* free cache mutex */
543 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
545 Debug(LDAP_DEBUG_TRACE,
546 "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
547 ep_id, ep->e_dn, count);
552 /* free cache mutex */
553 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
559 * cache_delete_entry - delete the entry e from the cache. the caller
560 * should have obtained e (increasing its ref count) via a call to one
561 * of the cache_find_* routines. the caller should *not* call the
562 * cache_return_entry() routine prior to calling cache_delete_entry().
563 * it performs this function.
565 * returns: 0 e was deleted ok
566 * 1 e was not in the cache
567 * -1 something bad happened
577 /* set cache mutex */
578 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
580 assert( e->e_private != NULL );
582 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
585 rc = cache_delete_entry_internal( cache, e );
587 /* free cache mutex */
588 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
593 cache_delete_entry_internal(
598 int rc = 0; /* return code */
601 if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp ) == NULL )
607 if ( avl_delete( &cache->c_idtree, (caddr_t) e, entry_id_cmp ) == NULL )
617 LRU_DELETE( cache, e );
621 * flag entry to be freed later by a call to cache_return_entry()
623 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
629 cache_release_all( Cache *cache )
634 /* set cache mutex */
635 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
637 Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
640 while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
641 /* delete from cache and lru q */
642 /* XXX do we need rc ? */
643 rc = cache_delete_entry_internal( cache, e );
644 cache_entry_private_destroy( e );
648 if ( cache->c_cursize ) {
649 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
652 /* free cache mutex */
653 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
659 lru_print( Cache *cache )
663 fprintf( stderr, "LRU queue (head to tail):\n" );
664 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
665 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
666 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
668 fprintf( stderr, "LRU queue (tail to head):\n" );
669 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
670 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
671 e->e_dn, e->e_id, LEI(e)->lei_refcnt );