1 /* zn_malloc.c - zone-based malloc routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-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>.
16 /* Copyright 2004 IBM Corporation
17 * All rights reserved.
18 * Redisribution and use in source and binary forms, with or without
19 * modification, are permitted only as authorizd by the OpenLADP
23 * This work originally developed by Jong-Hyuk Choi
24 * 2004/12/09 jongchoi@OpenLDAP.org
30 #include <ac/string.h>
31 #include <sys/types.h>
37 #ifdef SLAP_ZONE_ALLOC
39 static int slap_zone_cmp(const void *v1, const void *v2);
40 void * slap_replenish_zopool(void *ctx);
43 slap_zo_release(void *data)
45 struct zone_object *zo = (struct zone_object *)data;
54 struct zone_heap *zh = ctx;
55 int pad = 2*sizeof(int)-1, pad_shift;
56 int order_start = -1, i, j;
57 struct zone_object *zo;
62 } while (pad_shift >>= 1);
64 ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
65 for (i = 0; i < zh->zh_zoneorder - order_start + 1; i++) {
66 zo = LDAP_LIST_FIRST(&zh->zh_free[i]);
68 struct zone_object *zo_tmp = zo;
69 zo = LDAP_LIST_NEXT(zo, zo_link);
70 LDAP_LIST_REMOVE(zo_tmp, zo_link);
71 LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_tmp, zo_link);
76 for (i = 0; i < zh->zh_numzones; i++) {
77 for (j = 0; j < zh->zh_zoneorder - order_start + 1; j++) {
78 ch_free(zh->zh_maps[i][j]);
80 ch_free(zh->zh_maps[i]);
81 munmap(zh->zh_zones[i], zh->zh_zonesize);
82 ldap_pvt_thread_rdwr_destroy(&zh->zh_znlock[i]);
85 ch_free(zh->zh_zones);
86 ch_free(zh->zh_seqno);
87 ch_free(zh->zh_znlock);
89 avl_free(zh->zh_zonetree, slap_zo_release);
91 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
93 struct zone_object *zo_tmp = zo;
94 zo = LDAP_LIST_NEXT(zo, zo_link);
95 if (!zo_tmp->zo_blockhead) {
96 LDAP_LIST_REMOVE(zo_tmp, zo_link);
99 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
101 struct zone_object *zo_tmp = zo;
102 zo = LDAP_LIST_NEXT(zo, zo_link);
105 ldap_pvt_thread_mutex_unlock(&zh->zh_mutex);
106 ldap_pvt_thread_rdwr_destroy(&zh->zh_lock);
107 ldap_pvt_thread_mutex_destroy(&zh->zh_mutex);
119 struct zone_heap *zh = NULL;
121 int pad = 2*sizeof(int)-1, pad_shift;
123 int order = -1, order_start = -1, order_end = -1;
125 struct zone_object *zo;
127 Debug(LDAP_DEBUG_NONE,
128 "--> slap_zn_mem_create: initsize=%d, maxsize=%d\n",
129 initsize, maxsize, 0);
130 Debug(LDAP_DEBUG_NONE,
131 "++> slap_zn_mem_create: deltasize=%d, zonesize=%d\n",
132 deltasize, zonesize, 0);
134 zh = (struct zone_heap *)ch_calloc(1, sizeof(struct zone_heap));
136 zh->zh_fd = open("/dev/zero", O_RDWR);
139 zh->zh_zonesize = zonesize;
141 zh->zh_zonesize = SLAP_ZONE_SIZE;
144 zpad = zh->zh_zonesize - 1;
145 zh->zh_numzones = ((initsize + zpad) & ~zpad) / zh->zh_zonesize;
147 if ( maxsize && maxsize >= initsize ) {
148 zh->zh_maxzones = ((maxsize + zpad) & ~zpad) / zh->zh_zonesize;
150 zh->zh_maxzones = ((initsize + zpad) & ~zpad) / zh->zh_zonesize;
154 zh->zh_deltazones = ((deltasize + zpad) & ~zpad) / zh->zh_zonesize;
156 zh->zh_deltazones = ((SLAP_ZONE_DELTA+zpad) & ~zpad) / zh->zh_zonesize;
159 size_shift = zh->zh_zonesize - 1;
162 } while (size_shift >>= 1);
167 } while (pad_shift >>= 1);
169 order = order_end - order_start + 1;
171 zh->zh_zones = (void **)ch_malloc(zh->zh_maxzones * sizeof(void*));
172 zh->zh_znlock = (ldap_pvt_thread_rdwr_t *)ch_malloc(
173 zh->zh_maxzones * sizeof(ldap_pvt_thread_rdwr_t *));
174 zh->zh_maps = (unsigned char ***)ch_malloc(
175 zh->zh_maxzones * sizeof(unsigned char**));
177 zh->zh_zoneorder = order_end;
178 zh->zh_free = (struct zh_freelist *)
179 ch_malloc(order * sizeof(struct zh_freelist));
180 zh->zh_seqno = (unsigned long *)ch_calloc(zh->zh_maxzones,
181 sizeof(unsigned long));
182 for (i = 0; i < order; i++) {
183 LDAP_LIST_INIT(&zh->zh_free[i]);
185 LDAP_LIST_INIT(&zh->zh_zopool);
187 for (i = 0; i < zh->zh_numzones; i++) {
188 zh->zh_zones[i] = mmap(0, zh->zh_zonesize, PROT_READ | PROT_WRITE,
189 MAP_PRIVATE, zh->zh_fd, 0);
190 zh->zh_maps[i] = (unsigned char **)
191 ch_malloc(order * sizeof(unsigned char *));
192 for (j = 0; j < order; j++) {
193 int shiftamt = order_start + 1 + j;
194 int nummaps = zh->zh_zonesize >> shiftamt;
197 if (!nummaps) nummaps = 1;
198 zh->zh_maps[i][j] = (unsigned char *)ch_malloc(nummaps);
199 memset(zh->zh_maps[i][j], 0, nummaps);
202 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
203 slap_replenish_zopool(zh);
205 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
206 LDAP_LIST_REMOVE(zo, zo_link);
207 zo->zo_ptr = zh->zh_zones[i];
209 LDAP_LIST_INSERT_HEAD(&zh->zh_free[order-1], zo, zo_link);
211 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
212 slap_replenish_zopool(zh);
214 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
215 LDAP_LIST_REMOVE(zo, zo_link);
216 zo->zo_ptr = zh->zh_zones[i];
217 zo->zo_siz = zh->zh_zonesize;
219 avl_insert(&zh->zh_zonetree, zo, slap_zone_cmp, avl_dup_error);
220 ldap_pvt_thread_rdwr_init(&zh->zh_znlock[i]);
223 LDAP_STAILQ_INIT(&zh->zh_latency_history_queue);
224 ldap_pvt_thread_mutex_init(&zh->zh_mutex);
225 ldap_pvt_thread_rdwr_init(&zh->zh_lock);
236 struct zone_heap *zh = ctx;
237 ber_len_t size_shift;
238 int pad = 2*sizeof(int)-1, pad_shift;
239 int order = -1, order_start = -1;
240 struct zone_object *zo, *zo_new, *zo_left, *zo_right;
241 ber_len_t *ptr, *new;
246 Debug(LDAP_DEBUG_NONE,
247 "--> slap_zn_malloc: size=%d\n", size, 0, 0);
249 if (!zh) return ber_memalloc_x(size, NULL);
251 /* round up to doubleword boundary */
252 size += 2*sizeof(ber_len_t) + pad;
255 size_shift = size - 1;
258 } while (size_shift >>= 1);
263 } while (pad_shift >>= 1);
267 ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
268 for (i = order; i <= zh->zh_zoneorder &&
269 LDAP_LIST_EMPTY(&zh->zh_free[i-order_start]); i++);
272 zo_new = LDAP_LIST_FIRST(&zh->zh_free[i-order_start]);
273 LDAP_LIST_REMOVE(zo_new, zo_link);
274 ptr = zo_new->zo_ptr;
275 idx = zo_new->zo_idx;
276 diff = (unsigned long)((char*)ptr -
277 (char*)zh->zh_zones[idx]) >> (order + 1);
278 zh->zh_maps[idx][order-order_start][diff>>3] |= (1 << (diff & 0x7));
279 *ptr++ = zh->zh_seqno[idx];
280 *ptr++ = size - 2*sizeof(ber_len_t);
281 zo_new->zo_ptr = NULL;
283 LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_new, zo_link);
284 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
285 Debug(LDAP_DEBUG_NONE, "slap_zn_malloc: returning 0x%x, 0x%x\n",
286 ptr, (int)ptr>>(zh->zh_zoneorder+1), 0);
288 } else if (i <= zh->zh_zoneorder) {
289 for (j = i; j > order; j--) {
290 zo_left = LDAP_LIST_FIRST(&zh->zh_free[j-order_start]);
291 LDAP_LIST_REMOVE(zo_left, zo_link);
292 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
293 slap_replenish_zopool(zh);
295 zo_right = LDAP_LIST_FIRST(&zh->zh_zopool);
296 LDAP_LIST_REMOVE(zo_right, zo_link);
297 zo_right->zo_ptr = zo_left->zo_ptr + (1 << j);
298 zo_right->zo_idx = zo_left->zo_idx;
299 Debug(LDAP_DEBUG_NONE,
300 "slap_zn_malloc: split (left=0x%x, right=0x%x)\n",
301 zo_left->zo_ptr, zo_right->zo_ptr, 0);
302 if (j == order + 1) {
303 ptr = zo_left->zo_ptr;
304 diff = (unsigned long)((char*)ptr -
305 (char*)zh->zh_zones[zo_left->zo_idx]) >> (order+1);
306 zh->zh_maps[zo_left->zo_idx][order-order_start][diff>>3] |=
308 *ptr++ = zh->zh_seqno[zo_left->zo_idx];
309 *ptr++ = size - 2*sizeof(ber_len_t);
310 LDAP_LIST_INSERT_HEAD(
311 &zh->zh_free[j-1-order_start], zo_right, zo_link);
312 LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_left, zo_link);
313 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
314 Debug(LDAP_DEBUG_NONE,
315 "slap_zn_malloc: returning 0x%x, 0x%x\n",
316 ptr, (int)ptr>>(zh->zh_zoneorder+1), 0);
319 LDAP_LIST_INSERT_HEAD(
320 &zh->zh_free[j-1-order_start], zo_right, zo_link);
321 LDAP_LIST_INSERT_HEAD(
322 &zh->zh_free[j-1-order_start], zo_left, zo_link);
328 if ( zh->zh_maxzones < zh->zh_numzones + zh->zh_deltazones ) {
329 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
330 Debug( LDAP_DEBUG_TRACE,
331 "slap_zn_malloc of %lu bytes failed, using ch_malloc\n",
333 Debug(LDAP_DEBUG_NONE,
334 "slap_zn_malloc: returning 0x%x, 0x%x\n",
335 ptr, (int)ptr>>(zh->zh_zoneorder+1), 0);
336 return (void*)ch_malloc(size);
339 for (i = zh->zh_numzones; i < zh->zh_numzones+zh->zh_deltazones; i++) {
340 zh->zh_zones[i] = mmap(0, zh->zh_zonesize, PROT_READ | PROT_WRITE,
341 MAP_PRIVATE, zh->zh_fd, 0);
342 zh->zh_maps[i] = (unsigned char **)
343 ch_malloc((zh->zh_zoneorder - order_start + 1) *
344 sizeof(unsigned char *));
345 for (j = 0; j < zh->zh_zoneorder-order_start+1; j++) {
346 int shiftamt = order_start + 1 + j;
347 int nummaps = zh->zh_zonesize >> shiftamt;
350 if (!nummaps) nummaps = 1;
351 zh->zh_maps[i][j] = (unsigned char *)ch_malloc(nummaps);
352 memset(zh->zh_maps[i][j], 0, nummaps);
355 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
356 slap_replenish_zopool(zh);
358 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
359 LDAP_LIST_REMOVE(zo, zo_link);
360 zo->zo_ptr = zh->zh_zones[i];
362 LDAP_LIST_INSERT_HEAD(&zh->
363 zh_free[zh->zh_zoneorder-order_start],zo,zo_link);
365 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
366 slap_replenish_zopool(zh);
368 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
369 LDAP_LIST_REMOVE(zo, zo_link);
370 zo->zo_ptr = zh->zh_zones[i];
371 zo->zo_siz = zh->zh_zonesize;
373 avl_insert(&zh->zh_zonetree, zo, slap_zone_cmp, avl_dup_error);
374 ldap_pvt_thread_rdwr_init(&zh->zh_znlock[i]);
376 zh->zh_numzones += zh->zh_deltazones;
377 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
383 slap_zn_calloc( ber_len_t n, ber_len_t size, void *ctx )
387 new = slap_zn_malloc( n*size, ctx );
389 memset( new, 0, n*size );
395 slap_zn_realloc(void *ptr, ber_len_t size, void *ctx)
397 struct zone_heap *zh = ctx;
398 int pad = 2*sizeof(int)-1, pad_shift;
399 int order_start = -1, order = -1;
400 struct zone_object zoi, *zoo;
401 ber_len_t *p = (ber_len_t *)ptr, *new;
405 struct zone_heap *zone = NULL;
407 Debug(LDAP_DEBUG_NONE,
408 "--> slap_zn_realloc: ptr=0x%x, size=%d\n", ptr, size, 0);
411 return slap_zn_malloc(size, zh);
417 ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
418 zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
419 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
422 /* Not our memory? */
424 /* duplicate of realloc behavior, oh well */
425 new = ber_memrealloc_x(ptr, size, NULL);
429 Debug(LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
432 exit( EXIT_FAILURE );
435 assert(zoo->zo_idx != -1);
437 zone = zh->zh_zones[zoo->zo_idx];
440 slap_zn_free(ptr, zh);
444 newptr = slap_zn_malloc(size, zh);
446 AC_MEMCPY(newptr, ptr, size);
448 AC_MEMCPY(newptr, ptr, p[-1]);
450 slap_zn_free(ptr, zh);
455 slap_zn_free(void *ptr, void *ctx)
457 struct zone_heap *zh = ctx;
458 int size, size_shift, order_size;
459 int pad = 2*sizeof(int)-1, pad_shift;
460 ber_len_t *p = (ber_len_t *)ptr, *tmpp;
461 int order_start = -1, order = -1;
462 struct zone_object zoi, *zoo, *zo;
464 int i, k, inserted = 0, idx;
465 struct zone_heap *zone = NULL;
470 Debug(LDAP_DEBUG_NONE, "--> slap_zn_free: ptr=0x%x\n", ptr, 0, 0);
473 ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
474 zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
475 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
479 ber_memfree_x(ptr, NULL);
483 zone = zh->zh_zones[idx];
486 size_shift = size + 2*sizeof(ber_len_t) - 1;
489 } while (size_shift >>= 1);
494 } while (pad_shift >>= 1);
496 ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
497 for (i = order, tmpp = p; i <= zh->zh_zoneorder; i++) {
498 order_size = 1 << (i+1);
499 diff = (unsigned long)((char*)tmpp - (char*)zone) >> (i+1);
500 zh->zh_maps[idx][i-order_start][diff>>3] &= (~(1 << (diff & 0x7)));
501 if (diff == ((diff>>1)<<1)) {
502 if (!(zh->zh_maps[idx][i-order_start][(diff+1)>>3] &
503 (1<<((diff+1)&0x7)))) {
504 zo = LDAP_LIST_FIRST(&zh->zh_free[i-order_start]);
506 if ((char*)zo->zo_ptr == (char*)tmpp) {
507 LDAP_LIST_REMOVE( zo, zo_link );
508 } else if ((char*)zo->zo_ptr ==
509 (char*)tmpp + order_size) {
510 LDAP_LIST_REMOVE(zo, zo_link);
513 zo = LDAP_LIST_NEXT(zo, zo_link);
516 if (i < zh->zh_zoneorder) {
519 Debug(LDAP_DEBUG_NONE,
520 "slap_zn_free: merging 0x%x\n",
522 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start+1],
527 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
528 slap_replenish_zopool(zh);
530 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
531 LDAP_LIST_REMOVE(zo, zo_link);
534 Debug(LDAP_DEBUG_NONE,
535 "slap_zn_free: merging 0x%x\n",
537 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
541 Debug(LDAP_DEBUG_ANY, "slap_zn_free: "
542 "free object not found while bit is clear.\n",
549 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
550 slap_replenish_zopool(zh);
552 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
553 LDAP_LIST_REMOVE(zo, zo_link);
556 Debug(LDAP_DEBUG_NONE,
557 "slap_zn_free: merging 0x%x\n",
559 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
565 if (!(zh->zh_maps[idx][i-order_start][(diff-1)>>3] &
566 (1<<((diff-1)&0x7)))) {
567 zo = LDAP_LIST_FIRST(&zh->zh_free[i-order_start]);
569 if ((char*)zo->zo_ptr == (char*)tmpp) {
570 LDAP_LIST_REMOVE(zo, zo_link);
571 } else if ((char*)tmpp == zo->zo_ptr + order_size) {
572 LDAP_LIST_REMOVE(zo, zo_link);
576 zo = LDAP_LIST_NEXT(zo, zo_link);
579 if (i < zh->zh_zoneorder) {
581 Debug(LDAP_DEBUG_NONE,
582 "slap_zn_free: merging 0x%x\n",
584 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start+1],
589 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
590 slap_replenish_zopool(zh);
592 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
593 LDAP_LIST_REMOVE(zo, zo_link);
596 Debug(LDAP_DEBUG_NONE,
597 "slap_zn_free: merging 0x%x\n",
599 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
603 Debug(LDAP_DEBUG_ANY, "slap_zn_free: "
604 "free object not found while bit is clear.\n",
611 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
612 slap_replenish_zopool(zh);
614 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
615 LDAP_LIST_REMOVE(zo, zo_link);
618 Debug(LDAP_DEBUG_NONE,
619 "slap_zn_free: merging 0x%x\n",
621 LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
628 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
633 slap_zone_cmp(const void *v1, const void *v2)
635 const struct zone_object *zo1 = v1;
636 const struct zone_object *zo2 = v2;
641 zpad = zo2->zo_siz - 1;
642 ptr1 = (char*)(((unsigned long)zo1->zo_ptr + zpad) & ~zpad);
643 ptr2 = (char*)zo2->zo_ptr + ((char*)ptr1 - (char*)zo1->zo_ptr);
644 ptr2 = (char*)(((unsigned long)ptr2 + zpad) & ~zpad);
645 return (int)((char*)ptr1 - (char*)ptr2);
649 slap_replenish_zopool(
653 struct zone_heap* zh = ctx;
654 struct zone_object *zo_block;
657 zo_block = (struct zone_object *)ch_malloc(
658 SLAP_ZONE_ZOBLOCK * sizeof(struct zone_object));
660 if ( zo_block == NULL ) {
664 zo_block[0].zo_blockhead = 1;
665 LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, &zo_block[0], zo_link);
666 for (i = 1; i < SLAP_ZONE_ZOBLOCK; i++) {
667 zo_block[i].zo_blockhead = 0;
668 LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, &zo_block[i], zo_link );
680 struct zone_heap* zh = ctx;
681 struct zone_object zoi, *zoo;
682 struct zone_heap *zone = NULL;
683 int seqno = *((ber_len_t*)ptr - 2);
684 int idx = -1, rc = 0;
685 int pad = 2*sizeof(int)-1, pad_shift;
686 int order_start = -1, i;
687 struct zone_object *zo;
692 } while (pad_shift >>= 1);
697 ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
698 zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
703 madvise(zh->zh_zones[idx], zh->zh_zonesize, MADV_DONTNEED);
704 for (i = 0; i < zh->zh_zoneorder - order_start + 1; i++) {
705 int shiftamt = order_start + 1 + i;
706 int nummaps = zh->zh_zonesize >> shiftamt;
709 if (!nummaps) nummaps = 1;
710 memset(zh->zh_maps[idx][i], 0, nummaps);
711 zo = LDAP_LIST_FIRST(&zh->zh_free[i]);
713 struct zone_object *zo_tmp = zo;
714 zo = LDAP_LIST_NEXT(zo, zo_link);
715 if (zo_tmp && zo_tmp->zo_idx == idx) {
716 LDAP_LIST_REMOVE(zo_tmp, zo_link);
717 LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_tmp, zo_link);
721 if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
722 slap_replenish_zopool(zh);
724 zo = LDAP_LIST_FIRST(&zh->zh_zopool);
725 LDAP_LIST_REMOVE(zo, zo_link);
726 zo->zo_ptr = zh->zh_zones[idx];
728 LDAP_LIST_INSERT_HEAD(&zh->zh_free[zh->zh_zoneorder-order_start],
732 Debug(LDAP_DEBUG_NONE, "zone not found for (ctx=0x%x, ptr=0x%x) !\n",
736 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
737 Debug(LDAP_DEBUG_NONE, "zone %d invalidate\n", idx, 0, 0);
748 struct zone_heap* zh = ctx;
749 struct zone_object zoi, *zoo;
750 struct zone_heap *zone = NULL;
756 zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
761 assert(seqno <= zh->zh_seqno[idx]);
762 rc = (seqno == zh->zh_seqno[idx]);
772 struct zone_heap* zh = ctx;
773 ldap_pvt_thread_rdwr_rlock(&zh->zh_lock);
780 struct zone_heap* zh = ctx;
781 ldap_pvt_thread_rdwr_runlock(&zh->zh_lock);
788 struct zone_heap* zh = ctx;
789 ldap_pvt_thread_rdwr_wlock(&zh->zh_lock);
796 struct zone_heap* zh = ctx;
797 ldap_pvt_thread_rdwr_wunlock(&zh->zh_lock);
805 struct zone_heap* zh = ctx;
806 struct zone_object zoi, *zoo;
807 struct zone_heap *zone = NULL;
813 ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
814 zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
815 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
820 ldap_pvt_thread_rdwr_rlock(&zh->zh_znlock[idx]);
829 struct zone_heap* zh = ctx;
830 struct zone_object zoi, *zoo;
831 struct zone_heap *zone = NULL;
837 ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
838 zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
839 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
844 ldap_pvt_thread_rdwr_runlock(&zh->zh_znlock[idx]);
853 struct zone_heap* zh = ctx;
854 struct zone_object zoi, *zoo;
855 struct zone_heap *zone = NULL;
861 ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
862 zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
863 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
868 ldap_pvt_thread_rdwr_wlock(&zh->zh_znlock[idx]);
877 struct zone_heap* zh = ctx;
878 struct zone_object zoi, *zoo;
879 struct zone_heap *zone = NULL;
885 ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
886 zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
887 ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
892 ldap_pvt_thread_rdwr_wunlock(&zh->zh_znlock[idx]);
896 #define T_SEC_IN_USEC 1000000
899 slap_timediff(struct timeval *tv_begin, struct timeval *tv_end)
901 uint64_t t_begin, t_end, t_diff;
903 t_begin = T_SEC_IN_USEC * tv_begin->tv_sec + tv_begin->tv_usec;
904 t_end = T_SEC_IN_USEC * tv_end->tv_sec + tv_end->tv_usec;
905 t_diff = t_end - t_begin;
914 slap_set_timing(struct timeval *tv_set)
916 gettimeofday(tv_set, (struct timezone *)NULL);
920 slap_measure_timing(struct timeval *tv_set, struct timeval *tv_measure)
922 gettimeofday(tv_measure, (struct timezone *)NULL);
923 return(slap_timediff(tv_set, tv_measure));
926 #define EMA_WEIGHT 0.999000
927 #define SLAP_ZN_LATENCY_HISTORY_QLEN 500
929 slap_zn_latency_history(void* ctx, int ea_latency)
931 /* TODO: monitor /proc/swap as well */
932 struct zone_heap* zh = ctx;
935 zh->zh_ema_latency = (double)ea_latency * (1.0 - EMA_WEIGHT)
936 + zh->zh_ema_latency * EMA_WEIGHT;
937 if (!zh->zh_swapping && zh->zh_ema_samples++ % 100 == 99) {
938 struct zone_latency_history *zlh_entry;
939 zlh_entry = ch_calloc(1, sizeof(struct zone_latency_history));
940 zlh_entry->zlh_latency = zh->zh_ema_latency;
941 LDAP_STAILQ_INSERT_TAIL(
942 &zh->zh_latency_history_queue, zlh_entry, zlh_next);
943 zh->zh_latency_history_qlen++;
944 while (zh->zh_latency_history_qlen > SLAP_ZN_LATENCY_HISTORY_QLEN) {
945 struct zone_latency_history *zlh;
946 zlh = LDAP_STAILQ_FIRST(&zh->zh_latency_history_queue);
947 LDAP_STAILQ_REMOVE_HEAD(
948 &zh->zh_latency_history_queue, zlh_next);
949 zh->zh_latency_history_qlen--;
952 if (zh->zh_latency_history_qlen == SLAP_ZN_LATENCY_HISTORY_QLEN) {
953 struct zone_latency_history *zlh_first, *zlh_last;
954 zlh_first = LDAP_STAILQ_FIRST(&zh->zh_latency_history_queue);
955 zlh_last = LDAP_STAILQ_LAST(&zh->zh_latency_history_queue,
956 zone_latency_history, zlh_next);
957 t_diff = zlh_last->zlh_latency - zlh_first->zlh_latency;
959 if (t_diff >= 2000) {
960 zh->zh_latency_jump++;
962 zh->zh_latency_jump = 0;
964 if (zh->zh_latency_jump > 3) {
965 zh->zh_latency_jump = 0;
969 return zh->zh_swapping;
971 #endif /* SLAP_ZONE_ALLOC */