]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/cache.c
Change slapd/delete stats message for consistency.
[openldap] / servers / slapd / back-ldbm / cache.c
1 /* cache.c - routines to maintain an in-core cache of entries */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/errno.h>
8 #include <ac/string.h>
9 #include <ac/socket.h>
10
11 #include "slap.h"
12
13 #include "back-ldbm.h"
14
15 static int      cache_delete_entry_internal(struct cache *cache, Entry *e);
16 #ifdef LDAP_DEBUG
17 static void     lru_print(struct cache *cache);
18 #endif
19
20 /*
21  * the cache has three entry points (ways to find things):
22  *
23  *      by entry        e.g., if you already have an entry from the cache
24  *                      and want to delete it. (really by entry ptr)
25  *      by dn           e.g., when looking for the base object of a search
26  *      by id           e.g., for search candidates
27  *
28  * these correspond to three different avl trees that are maintained.
29  */
30
31 static int
32 cache_entry_cmp( Entry *e1, Entry *e2 )
33 {
34         return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
35 }
36
37 static int
38 cache_entrydn_cmp( Entry *e1, Entry *e2 )
39 {
40         /* compare their normalized UPPERCASED dn's */
41         return( strcmp( e1->e_ndn, e2->e_ndn ) );
42 }
43
44 static int
45 cache_entryid_cmp( Entry *e1, Entry *e2 )
46 {
47         return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
48 }
49
50 void
51 cache_set_state( struct cache *cache, Entry *e, int state )
52 {
53         /* set cache mutex */
54         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
55
56         e->e_state = state;
57
58         /* free cache mutex */
59         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
60 }
61
62 static void
63 cache_return_entry_rw( struct cache *cache, Entry *e, int rw )
64 {
65         Debug( LDAP_DEBUG_TRACE, "====> cache_return_entry_%s\n",
66                 rw ? "w" : "r", 0, 0);
67
68         /* set cache mutex */
69         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
70
71         entry_rdwr_unlock(e, rw);
72
73         if ( --e->e_refcnt == 0 && e->e_state == ENTRY_STATE_DELETED ) {
74                 entry_free( e );
75         }
76
77         /* free cache mutex */
78         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
79 }
80
81 void
82 cache_return_entry_r( struct cache *cache, Entry *e )
83 {
84         cache_return_entry_rw(cache, e, 0);
85 }
86
87 void
88 cache_return_entry_w( struct cache *cache, Entry *e )
89 {
90         cache_return_entry_rw(cache, e, 1);
91 }
92
93
94 #define LRU_DELETE( cache, e ) { \
95         if ( e->e_lruprev != NULL ) { \
96                 e->e_lruprev->e_lrunext = e->e_lrunext; \
97         } else { \
98                 cache->c_lruhead = e->e_lrunext; \
99         } \
100         if ( e->e_lrunext != NULL ) { \
101                 e->e_lrunext->e_lruprev = e->e_lruprev; \
102         } else { \
103                 cache->c_lrutail = e->e_lruprev; \
104         } \
105 }
106
107 #define LRU_ADD( cache, e ) { \
108         e->e_lrunext = cache->c_lruhead; \
109         if ( e->e_lrunext != NULL ) { \
110                 e->e_lrunext->e_lruprev = e; \
111         } \
112         cache->c_lruhead = e; \
113         e->e_lruprev = NULL; \
114         if ( cache->c_lrutail == NULL ) { \
115                 cache->c_lrutail = e; \
116         } \
117 }
118
119 /*
120  * cache_create_entry - create an entry in the cache, and lock it.
121  * returns:     0       entry has been created and locked
122  *              1       entry already existed
123  *              -1      something bad happened
124  */
125 int
126 cache_add_entry(
127     struct cache        *cache,
128     Entry               *e,
129     int                 state
130 )
131 {
132         int     i, rc;
133         Entry   *ee;
134
135         /* set cache mutex */
136         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
137
138         if ( avl_insert( &cache->c_dntree, (caddr_t) e,
139                 cache_entrydn_cmp, avl_dup_error ) != 0 )
140         {
141                 Debug( LDAP_DEBUG_TRACE,
142                         "====> cache_add_entry lock: entry %20s id %lu already in dn cache\n",
143                     e->e_dn, e->e_id, 0 );
144
145                 /* free cache mutex */
146                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
147                 return( 1 );
148         }
149
150         /* id tree */
151         if ( avl_insert( &cache->c_idtree, (caddr_t) e,
152                 cache_entryid_cmp, avl_dup_error ) != 0 )
153         {
154                 Debug( LDAP_DEBUG_ANY,
155                         "====> entry %20s id %lu already in id cache\n",
156                     e->e_dn, e->e_id, 0 );
157
158                 /* delete from dn tree inserted above */
159                 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
160                         cache_entrydn_cmp ) == NULL )
161                 {
162                         Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
163                             0, 0, 0 );
164                 }
165
166                 /* free cache mutex */
167                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
168                 return( -1 );
169         }
170
171         e->e_state = state;
172         e->e_refcnt = 1;
173
174         /* lru */
175         LRU_ADD( cache, e );
176         if ( ++cache->c_cursize > cache->c_maxsize ) {
177                 /*
178                  * find the lru entry not currently in use and delete it.
179                  * in case a lot of entries are in use, only look at the
180                  * first 10 on the tail of the list.
181                  */
182                 i = 0;
183                 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
184                     != 0 && i < 10 ) {
185                         /* move this in-use entry to the front of the q */
186                         ee = cache->c_lrutail;
187                         LRU_DELETE( cache, ee );
188                         LRU_ADD( cache, ee );
189                         i++;
190                 }
191
192                 /*
193                  * found at least one to delete - try to get back under
194                  * the max cache size.
195                  */
196                 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
197                     == 0 && cache->c_cursize > cache->c_maxsize ) {
198                         e = cache->c_lrutail;
199
200                         /* check for active readers/writer lock */
201 #ifdef LDAP_DEBUG
202                         assert(!ldap_pvt_thread_rdwr_active( &e->e_rdwr ));
203 #endif
204
205                         /* delete from cache and lru q */
206                         rc = cache_delete_entry_internal( cache, e );
207
208                         entry_free( e );
209                 }
210         }
211
212         /* free cache mutex */
213         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
214         return( 0 );
215 }
216
217 /*
218  * cache_update_entry - update an entry in the cache
219  * returns:     0       entry has been created and locked
220  *              1       entry already existed
221  *              -1      something bad happened
222  */
223 int
224 cache_update_entry(
225     struct cache        *cache,
226     Entry               *e
227 )
228 {
229         int     i, rc;
230         Entry   *ee;
231
232         /* set cache mutex */
233         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
234
235         if ( avl_insert( &cache->c_dntree, (caddr_t) e,
236                 cache_entrydn_cmp, avl_dup_error ) != 0 )
237         {
238                 Debug( LDAP_DEBUG_TRACE,
239                         "====> cache_add_entry lock: entry %20s id %lu already in dn cache\n",
240                     e->e_dn, e->e_id, 0 );
241
242                 /* free cache mutex */
243                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
244                 return( 1 );
245         }
246
247         /* id tree */
248         if ( avl_insert( &cache->c_idtree, (caddr_t) e,
249                 cache_entryid_cmp, avl_dup_error ) != 0 )
250         {
251                 Debug( LDAP_DEBUG_ANY,
252                         "====> entry %20s id %lu already in id cache\n",
253                     e->e_dn, e->e_id, 0 );
254
255                 /* delete from dn tree inserted above */
256                 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
257                         cache_entrydn_cmp ) == NULL )
258                 {
259                         Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
260                             0, 0, 0 );
261                 }
262
263                 /* free cache mutex */
264                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
265                 return( -1 );
266         }
267
268         e->e_state = 0;
269
270         /* lru */
271         LRU_ADD( cache, e );
272         if ( ++cache->c_cursize > cache->c_maxsize ) {
273                 /*
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.
277                  */
278                 i = 0;
279                 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
280                     != 0 && i < 10 ) {
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 );
285                         i++;
286                 }
287
288                 /*
289                  * found at least one to delete - try to get back under
290                  * the max cache size.
291                  */
292                 while ( cache->c_lrutail != NULL && cache->c_lrutail->e_refcnt
293                     == 0 && cache->c_cursize > cache->c_maxsize ) {
294                         e = cache->c_lrutail;
295
296                         /* check for active readers/writer lock */
297 #ifdef LDAP_DEBUG
298                         assert(!ldap_pvt_thread_rdwr_active( &e->e_rdwr ));
299 #endif
300
301                         /* delete from cache and lru q */
302                         rc = cache_delete_entry_internal( cache, e );
303
304                         entry_free( e );
305                 }
306         }
307
308         /* free cache mutex */
309         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
310         return( 0 );
311 }
312
313 /*
314  * cache_find_entry_dn2id - find an entry in the cache, given dn
315  */
316
317 ID
318 cache_find_entry_dn2id(
319         Backend         *be,
320     struct cache        *cache,
321     char                *dn
322 )
323 {
324         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
325         Entry           e, *ep;
326         ID                      id;
327
328         /* set cache mutex */
329         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
330
331         e.e_dn = dn;
332         e.e_ndn = dn_normalize_case( ch_strdup( dn ) );
333
334         if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
335                 cache_entrydn_cmp )) != NULL )
336         {
337                 /*
338                  * ep now points to an unlocked entry
339                  * we do not need to lock the entry if we only
340                  * check the state, refcnt, LRU, and id.
341                  */
342                 free(e.e_ndn);
343
344                 Debug(LDAP_DEBUG_TRACE, "====> cache_find_entry_dn2id: found dn: %s\n",
345                         dn, 0, 0);
346
347                 /*
348                  * entry is deleted or not fully created yet
349                  */
350                 if ( ep->e_state == ENTRY_STATE_DELETED ||
351                         ep->e_state == ENTRY_STATE_CREATING )
352                 {
353                         /* free cache mutex */
354                         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
355                         return( NOID );
356                 }
357
358                 /* lru */
359                 LRU_DELETE( cache, ep );
360                 LRU_ADD( cache, ep );
361                 
362                 /* save id */
363                 id = ep->e_id;
364
365                 /* free cache mutex */
366                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
367
368                 return( id );
369         }
370
371         free(e.e_ndn);
372
373         /* free cache mutex */
374         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
375
376         return( NOID );
377 }
378
379 /*
380  * cache_find_entry_id - find an entry in the cache, given id
381  */
382
383 Entry *
384 cache_find_entry_id(
385         struct cache    *cache,
386         ID                              id,
387         int                             rw
388 )
389 {
390         Entry   e;
391         Entry   *ep;
392
393         e.e_id = id;
394
395 try_again:
396         /* set cache mutex */
397         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
398
399         if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
400                 cache_entryid_cmp )) != NULL )
401         {
402                 Debug(LDAP_DEBUG_TRACE,
403                         "====> cache_find_entry_dn2id: found id: %ld rw: %d\n",
404                         id, rw, 0);
405
406                 /*
407                  * entry is deleted or not fully created yet
408                  */
409                 if ( ep->e_state == ENTRY_STATE_DELETED ||
410                         ep->e_state == ENTRY_STATE_CREATING )
411                 {
412                         /* free cache mutex */
413                         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
414                         return( NULL );
415                 }
416
417                 /* acquire reader lock */
418                 if ( entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
419                         /* could not acquire entry lock...
420                          * owner cannot free as we have the cache locked.
421                          * so, unlock the cache, yield, and try again.
422                          */
423
424                         /* free cache mutex */
425                         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
426                         ldap_pvt_thread_yield();
427                         goto try_again;
428                 }
429
430                 /* lru */
431                 LRU_DELETE( cache, ep );
432                 LRU_ADD( cache, ep );
433                 
434                 ep->e_refcnt++;
435
436                 /* free cache mutex */
437                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
438
439                 return( ep );
440         }
441
442         /* free cache mutex */
443         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
444
445         return( NULL );
446 }
447
448 /*
449  * cache_delete_entry - delete the entry e from the cache.  the caller
450  * should have obtained e (increasing its ref count) via a call to one
451  * of the cache_find_* routines.  the caller should *not* call the
452  * cache_return_entry() routine prior to calling cache_delete_entry().
453  * it performs this function.
454  *
455  * returns:     0       e was deleted ok
456  *              1       e was not in the cache
457  *              -1      something bad happened
458  */
459 int
460 cache_delete_entry(
461     struct cache        *cache,
462     Entry               *e
463 )
464 {
465         int     rc;
466
467         Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry:\n", 0, 0, 0 );
468
469         /* set cache mutex */
470         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
471
472         /* XXX check for writer lock - should also check no readers pending */
473 #ifdef LDAP_DEBUG
474         assert(ldap_pvt_thread_rdwr_writers( &e->e_rdwr ) == 1);
475 #endif
476
477         rc = cache_delete_entry_internal( cache, e );
478
479         /* free cache mutex */
480         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
481         return( rc );
482 }
483
484 static int
485 cache_delete_entry_internal(
486     struct cache        *cache,
487     Entry               *e
488 )
489 {
490         int rc = 0;     /* return code */
491
492         /* dn tree */
493         if ( avl_delete( &cache->c_dntree, (caddr_t) e, cache_entrydn_cmp )
494                 == NULL )
495         {
496                 rc = -1;
497         }
498
499         /* id tree */
500         if ( avl_delete( &cache->c_idtree, (caddr_t) e, cache_entryid_cmp )
501                 == NULL )
502         {
503                 rc = -1;
504         }
505
506         if (rc != 0) {
507                 return rc;
508         }
509
510         /* lru */
511         LRU_DELETE( cache, e );
512         cache->c_cursize--;
513
514         /*
515          * flag entry to be freed later by a call to cache_return_entry()
516          */
517         e->e_state = ENTRY_STATE_DELETED;
518
519         return( 0 );
520 }
521
522 #ifdef LDAP_DEBUG
523
524 static void
525 lru_print( struct cache *cache )
526 {
527         Entry   *e;
528
529         fprintf( stderr, "LRU queue (head to tail):\n" );
530         for ( e = cache->c_lruhead; e != NULL; e = e->e_lrunext ) {
531                 fprintf( stderr, "\tdn %20s id %lu refcnt %d\n", e->e_dn,
532                     e->e_id, e->e_refcnt );
533         }
534         fprintf( stderr, "LRU queue (tail to head):\n" );
535         for ( e = cache->c_lrutail; e != NULL; e = e->e_lruprev ) {
536                 fprintf( stderr, "\tdn %20s id %lu refcnt %d\n", e->e_dn,
537                     e->e_id, e->e_refcnt );
538         }
539 }
540
541 #endif
542