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-2005 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 != NULL );
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 != NULL );
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 != NULL );
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 );
131 Debug( LDAP_DEBUG_TRACE,
132 "====> cache_return_entry_%s( %ld ): created (%d)\n",
133 rw ? "w" : "r", id, refcnt );
135 } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
137 /* free cache mutex */
138 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
140 Debug( LDAP_DEBUG_TRACE,
141 "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
142 rw ? "w" : "r", id, refcnt );
145 cache_entry_private_destroy( e );
150 /* free cache mutex */
151 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
153 Debug( LDAP_DEBUG_TRACE,
154 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
155 rw ? "w" : "r", id, refcnt );
159 /* free cache mutex */
160 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
162 Debug( LDAP_DEBUG_TRACE,
163 "====> cache_return_entry_%s( %ld ): returned (%d)\n",
164 rw ? "w" : "r", id, refcnt);
168 #define LRU_DELETE( cache, e ) do { \
169 if ( LEI(e)->lei_lruprev != NULL ) { \
170 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
172 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
174 if ( LEI(e)->lei_lrunext != NULL ) { \
175 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
177 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
181 #define LRU_ADD( cache, e ) do { \
182 LEI(e)->lei_lrunext = (cache)->c_lruhead; \
183 if ( LEI(e)->lei_lrunext != NULL ) { \
184 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
186 (cache)->c_lruhead = (e); \
187 LEI(e)->lei_lruprev = NULL; \
188 if ( (cache)->c_lrutail == NULL ) { \
189 (cache)->c_lrutail = (e); \
194 * cache_add_entry_rw - create and lock an entry in the cache
195 * returns: 0 entry has been created and locked
196 * 1 entry already existed
197 * -1 something bad happened
209 /* set cache mutex */
210 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
212 assert( e->e_private == NULL );
214 if( cache_entry_private_init(e) != 0 ) {
215 /* free cache mutex */
216 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
218 Debug( LDAP_DEBUG_ANY,
219 "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
220 e->e_id, e->e_dn, 0 );
225 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
226 entry_dn_cmp, avl_dup_error ) != 0 )
228 /* free cache mutex */
229 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
231 Debug( LDAP_DEBUG_TRACE,
232 "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
233 e->e_id, e->e_dn, 0 );
235 cache_entry_private_destroy(e);
241 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
242 entry_id_cmp, avl_dup_error ) != 0 )
244 Debug( LDAP_DEBUG_ANY,
245 "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
246 e->e_id, e->e_dn, 0 );
248 /* delete from dn tree inserted above */
249 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
250 entry_dn_cmp ) == NULL )
252 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
256 cache_entry_private_destroy(e);
258 /* free cache mutex */
259 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
263 /* put the entry into 'CREATING' state */
264 /* will be marked after when entry is returned */
265 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
266 LEI(e)->lei_refcnt = 1;
270 if ( ++cache->c_cursize > cache->c_maxsize ) {
272 * find the lru entry not currently in use and delete it.
273 * in case a lot of entries are in use, only look at the
274 * first 10 on the tail of the list.
277 while ( cache->c_lrutail != NULL &&
278 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
281 /* move this in-use entry to the front of the q */
282 ee = cache->c_lrutail;
283 LRU_DELETE( cache, ee );
284 LRU_ADD( cache, ee );
289 * found at least one to delete - try to get back under
290 * the max cache size.
292 while ( cache->c_lrutail != NULL &&
293 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
294 cache->c_cursize > cache->c_maxsize )
296 e = cache->c_lrutail;
298 /* delete from cache and lru q */
299 /* XXX do we need rc ? */
300 rc = cache_delete_entry_internal( cache, e );
301 cache_entry_private_destroy( e );
306 /* free cache mutex */
307 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
312 * cache_update_entry - update a LOCKED entry which has been deleted.
313 * returns: 0 entry has been created and locked
314 * 1 entry already existed
315 * -1 something bad happened
326 /* set cache mutex */
327 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
329 assert( e->e_private != NULL );
331 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
332 entry_dn_cmp, avl_dup_error ) != 0 )
334 Debug( LDAP_DEBUG_TRACE,
335 "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
336 e->e_id, e->e_dn, 0 );
338 /* free cache mutex */
339 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
344 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
345 entry_id_cmp, avl_dup_error ) != 0 )
347 Debug( LDAP_DEBUG_ANY,
348 "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
349 e->e_id, e->e_dn, 0 );
351 /* delete from dn tree inserted above */
352 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
353 entry_dn_cmp ) == NULL )
355 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
359 /* free cache mutex */
360 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
364 /* put the entry into 'CREATING' state */
365 /* will be marked after when entry is returned */
366 LEI(e)->lei_state = CACHE_ENTRY_CREATING;
370 if ( ++cache->c_cursize > cache->c_maxsize ) {
372 * find the lru entry not currently in use and delete it.
373 * in case a lot of entries are in use, only look at the
374 * first 10 on the tail of the list.
377 while ( cache->c_lrutail != NULL &&
378 LEI(cache->c_lrutail)->lei_refcnt != 0 &&
381 /* move this in-use entry to the front of the q */
382 ee = cache->c_lrutail;
383 LRU_DELETE( cache, ee );
384 LRU_ADD( cache, ee );
389 * found at least one to delete - try to get back under
390 * the max cache size.
392 while ( cache->c_lrutail != NULL &&
393 LEI(cache->c_lrutail)->lei_refcnt == 0 &&
394 cache->c_cursize > cache->c_maxsize )
396 e = cache->c_lrutail;
398 /* delete from cache and lru q */
399 /* XXX do we need rc ? */
400 rc = cache_delete_entry_internal( cache, e );
401 cache_entry_private_destroy( e );
406 /* free cache mutex */
407 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
412 cache_find_entry_ndn2id(
422 /* this function is always called with normalized DN */
426 /* set cache mutex */
427 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
429 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
430 entry_dn_cmp )) != NULL )
436 * ep now points to an unlocked entry
437 * we do not need to lock the entry if we only
438 * check the state, refcnt, LRU, and id.
440 assert( ep->e_private != NULL );
444 state = LEI(ep)->lei_state;
447 * entry is deleted or not fully created yet
449 if ( state != CACHE_ENTRY_READY ) {
450 assert(state != CACHE_ENTRY_UNDEFINED);
452 /* free cache mutex */
453 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
455 Debug(LDAP_DEBUG_TRACE,
456 "====> cache_find_entry_ndn2id(\"%s\"): %ld (not ready) %d\n",
457 ndn->bv_val, id, state);
459 ldap_pvt_thread_yield();
464 LRU_DELETE( cache, ep );
465 LRU_ADD( cache, ep );
467 /* free cache mutex */
468 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
470 Debug(LDAP_DEBUG_TRACE,
471 "====> cache_find_entry_ndn2id(\"%s\"): %ld (%d tries)\n",
472 ndn->bv_val, id, count);
475 /* free cache mutex */
476 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
484 * cache_find_entry_id - find an entry in the cache, given id
501 /* set cache mutex */
502 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
504 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
505 entry_id_cmp )) != NULL )
512 assert( ep->e_private != NULL );
515 state = LEI(ep)->lei_state;
518 * entry is deleted or not fully created yet
520 if ( state != CACHE_ENTRY_READY ) {
521 assert(state != CACHE_ENTRY_UNDEFINED);
523 /* free cache mutex */
524 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
526 Debug(LDAP_DEBUG_TRACE,
527 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
530 ldap_pvt_thread_yield();
535 LRU_DELETE( cache, ep );
536 LRU_ADD( cache, ep );
538 LEI(ep)->lei_refcnt++;
540 /* free cache mutex */
541 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
543 Debug(LDAP_DEBUG_TRACE,
544 "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
545 ep_id, ep->e_dn, count);
550 /* free cache mutex */
551 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
557 * cache_delete_entry - delete the entry e from the cache. the caller
558 * should have obtained e (increasing its ref count) via a call to one
559 * of the cache_find_* routines. the caller should *not* call the
560 * cache_return_entry() routine prior to calling cache_delete_entry().
561 * it performs this function.
563 * returns: 0 e was deleted ok
564 * 1 e was not in the cache
565 * -1 something bad happened
575 /* set cache mutex */
576 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
578 assert( e->e_private != NULL );
580 Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
583 rc = cache_delete_entry_internal( cache, e );
585 /* free cache mutex */
586 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
591 cache_delete_entry_internal(
596 int rc = 0; /* return code */
599 if ( avl_delete( &cache->c_dntree, (caddr_t) e, entry_dn_cmp ) == NULL )
605 if ( avl_delete( &cache->c_idtree, (caddr_t) e, entry_id_cmp ) == NULL )
615 LRU_DELETE( cache, e );
619 * flag entry to be freed later by a call to cache_return_entry()
621 LEI(e)->lei_state = CACHE_ENTRY_DELETED;
627 cache_release_all( Cache *cache )
632 /* set cache mutex */
633 ldap_pvt_thread_mutex_lock( &cache->c_mutex );
635 Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
638 while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
639 /* delete from cache and lru q */
640 /* XXX do we need rc ? */
641 rc = cache_delete_entry_internal( cache, e );
642 cache_entry_private_destroy( e );
646 if ( cache->c_cursize ) {
647 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
650 /* free cache mutex */
651 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
656 lru_print( Cache *cache )
660 fprintf( stderr, "LRU queue (head to tail):\n" );
661 for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
662 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
663 e->e_dn, e->e_id, LEI(e)->lei_refcnt );
665 fprintf( stderr, "LRU queue (tail to head):\n" );
666 for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
667 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
668 e->e_dn, e->e_id, LEI(e)->lei_refcnt );