zn_malloc.c 23.9 KB
Newer Older
1
2
3
4
/* zn_malloc.c - zone-based malloc routines */
/* $OpenLDAP$*/
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
5
 * Copyright 2003-2019 The OpenLDAP Foundation.
6
7
8
9
10
11
12
13
14
15
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
16
/* Portions Copyright 2004 IBM Corporation
17
 * All rights reserved.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
18
19
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
20
21
22
 * Public License.
 */
/* ACKNOWLEDGEMENTS
Kurt Zeilenga's avatar
Kurt Zeilenga committed
23
24
 * This work originally developed by Jong-Hyuk Choi for inclusion in
 * OpenLDAP Software.
25
26
27
28
29
30
31
32
33
34
35
36
37
 */

#include "portable.h"

#include <stdio.h>
#include <ac/string.h>
#include <sys/types.h>
#include <fcntl.h>

#include "slap.h"

#ifdef SLAP_ZONE_ALLOC

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
38
39
#include <sys/mman.h>

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
static int slap_zone_cmp(const void *v1, const void *v2);
void * slap_replenish_zopool(void *ctx);

static void
slap_zo_release(void *data)
{
	struct zone_object *zo = (struct zone_object *)data;
	ch_free( zo );
}

void
slap_zn_mem_destroy(
	void *ctx
)
{
	struct zone_heap *zh = ctx;
	int pad = 2*sizeof(int)-1, pad_shift;
	int order_start = -1, i, j;
	struct zone_object *zo;

	pad_shift = pad - 1;
	do {
		order_start++;
	} while (pad_shift >>= 1);

	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
66
	for (i = 0; i < zh->zh_zoneorder - order_start + 1; i++) {
67
68
69
70
		zo = LDAP_LIST_FIRST(&zh->zh_free[i]);
		while (zo) {
			struct zone_object *zo_tmp = zo;
			zo = LDAP_LIST_NEXT(zo, zo_link);
71
			LDAP_LIST_REMOVE(zo_tmp, zo_link);
72
73
74
75
76
77
			LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_tmp, zo_link);
		}
	}
	ch_free(zh->zh_free);

	for (i = 0; i < zh->zh_numzones; i++) {
78
		for (j = 0; j < zh->zh_zoneorder - order_start + 1; j++) {
79
80
81
82
			ch_free(zh->zh_maps[i][j]);
		}
		ch_free(zh->zh_maps[i]);
		munmap(zh->zh_zones[i], zh->zh_zonesize);
83
		ldap_pvt_thread_rdwr_destroy(&zh->zh_znlock[i]);
84
85
86
87
	}
	ch_free(zh->zh_maps);
	ch_free(zh->zh_zones);
	ch_free(zh->zh_seqno);
88
	ch_free(zh->zh_znlock);
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

	avl_free(zh->zh_zonetree, slap_zo_release);

	zo = LDAP_LIST_FIRST(&zh->zh_zopool);
	while (zo) {
		struct zone_object *zo_tmp = zo;
		zo = LDAP_LIST_NEXT(zo, zo_link);
		if (!zo_tmp->zo_blockhead) {
			LDAP_LIST_REMOVE(zo_tmp, zo_link);
		}
	}
	zo = LDAP_LIST_FIRST(&zh->zh_zopool);
	while (zo) {
		struct zone_object *zo_tmp = zo;
		zo = LDAP_LIST_NEXT(zo, zo_link);
		ch_free(zo_tmp);
	}
106
107
108
	ldap_pvt_thread_mutex_unlock(&zh->zh_mutex);
	ldap_pvt_thread_rdwr_destroy(&zh->zh_lock);
	ldap_pvt_thread_mutex_destroy(&zh->zh_mutex);
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	ch_free(zh);
}

void *
slap_zn_mem_create(
	ber_len_t initsize,
	ber_len_t maxsize,
	ber_len_t deltasize,
	ber_len_t zonesize
)
{
	struct zone_heap *zh = NULL;
	ber_len_t zpad;
	int pad = 2*sizeof(int)-1, pad_shift;
	int size_shift;
	int order = -1, order_start = -1, order_end = -1;
	int i, j;
	struct zone_object *zo;

128
129
130
131
132
133
134
	Debug(LDAP_DEBUG_NONE,
		"--> slap_zn_mem_create: initsize=%d, maxsize=%d\n",
		initsize, maxsize, 0);
	Debug(LDAP_DEBUG_NONE,
		"++> slap_zn_mem_create: deltasize=%d, zonesize=%d\n",
		deltasize, zonesize, 0);

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
	zh = (struct zone_heap *)ch_calloc(1, sizeof(struct zone_heap));

	zh->zh_fd = open("/dev/zero", O_RDWR);

	if ( zonesize ) {
		zh->zh_zonesize = zonesize;
	} else {
		zh->zh_zonesize = SLAP_ZONE_SIZE;
	}

	zpad = zh->zh_zonesize - 1;
	zh->zh_numzones = ((initsize + zpad) & ~zpad) / zh->zh_zonesize;

	if ( maxsize && maxsize >= initsize ) {
		zh->zh_maxzones = ((maxsize + zpad) & ~zpad) / zh->zh_zonesize;
	} else {
		zh->zh_maxzones = ((initsize + zpad) & ~zpad) / zh->zh_zonesize;
	}

	if ( deltasize ) {
		zh->zh_deltazones = ((deltasize + zpad) & ~zpad) / zh->zh_zonesize;
	} else {
		zh->zh_deltazones = ((SLAP_ZONE_DELTA+zpad) & ~zpad) / zh->zh_zonesize;
	}

	size_shift = zh->zh_zonesize - 1;
	do {
		order_end++;
	} while (size_shift >>= 1);

	pad_shift = pad - 1;
	do {
		order_start++;
	} while (pad_shift >>= 1);

	order = order_end - order_start + 1;

	zh->zh_zones = (void **)ch_malloc(zh->zh_maxzones * sizeof(void*));
173
174
	zh->zh_znlock = (ldap_pvt_thread_rdwr_t *)ch_malloc(
						zh->zh_maxzones * sizeof(ldap_pvt_thread_rdwr_t *));
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
	zh->zh_maps = (unsigned char ***)ch_malloc(
					zh->zh_maxzones * sizeof(unsigned char**));

	zh->zh_zoneorder = order_end;
	zh->zh_free = (struct zh_freelist *)
					ch_malloc(order * sizeof(struct zh_freelist));
	zh->zh_seqno = (unsigned long *)ch_calloc(zh->zh_maxzones,
											sizeof(unsigned long));
	for (i = 0; i < order; i++) {
		LDAP_LIST_INIT(&zh->zh_free[i]);
	}
	LDAP_LIST_INIT(&zh->zh_zopool);

	for (i = 0; i < zh->zh_numzones; i++) {
		zh->zh_zones[i] = mmap(0, zh->zh_zonesize, PROT_READ | PROT_WRITE,
							MAP_PRIVATE, zh->zh_fd, 0);
		zh->zh_maps[i] = (unsigned char **)
					ch_malloc(order * sizeof(unsigned char *));
		for (j = 0; j < order; j++) {
			int shiftamt = order_start + 1 + j;
			int nummaps = zh->zh_zonesize >> shiftamt;
			assert(nummaps);
197
			nummaps >>= 3;
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
			if (!nummaps) nummaps = 1;
			zh->zh_maps[i][j] = (unsigned char *)ch_malloc(nummaps);
			memset(zh->zh_maps[i][j], 0, nummaps);
		}

		if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
			slap_replenish_zopool(zh);
		}
		zo = LDAP_LIST_FIRST(&zh->zh_zopool);
		LDAP_LIST_REMOVE(zo, zo_link);
		zo->zo_ptr = zh->zh_zones[i];
		zo->zo_idx = i;
		LDAP_LIST_INSERT_HEAD(&zh->zh_free[order-1], zo, zo_link);

		if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
			slap_replenish_zopool(zh);
		}
		zo = LDAP_LIST_FIRST(&zh->zh_zopool);
		LDAP_LIST_REMOVE(zo, zo_link);
217
218
		zo->zo_ptr = zh->zh_zones[i];
		zo->zo_siz = zh->zh_zonesize;
219
220
		zo->zo_idx = i;
		avl_insert(&zh->zh_zonetree, zo, slap_zone_cmp, avl_dup_error);
221
		ldap_pvt_thread_rdwr_init(&zh->zh_znlock[i]);
222
223
	}

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
224
	LDAP_STAILQ_INIT(&zh->zh_latency_history_queue);
225
	ldap_pvt_thread_mutex_init(&zh->zh_mutex);
226
	ldap_pvt_thread_rdwr_init(&zh->zh_lock);
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

	return zh;
}

void *
slap_zn_malloc(
    ber_len_t	size,
	void *ctx
)
{
	struct zone_heap *zh = ctx;
	ber_len_t size_shift;
	int pad = 2*sizeof(int)-1, pad_shift;
	int order = -1, order_start = -1;
	struct zone_object *zo, *zo_new, *zo_left, *zo_right;
	ber_len_t *ptr, *new;
	int idx;
	unsigned long diff;
	int i, j, k;

247
248
249
	Debug(LDAP_DEBUG_NONE,
		"--> slap_zn_malloc: size=%d\n", size, 0, 0);

250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
	if (!zh) return ber_memalloc_x(size, NULL);

	/* round up to doubleword boundary */
	size += 2*sizeof(ber_len_t) + pad;
	size &= ~pad;

	size_shift = size - 1;
	do {
		order++;
	} while (size_shift >>= 1);

	pad_shift = pad - 1;
	do {
		order_start++;
	} while (pad_shift >>= 1);

retry:

	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
	for (i = order; i <= zh->zh_zoneorder &&
			LDAP_LIST_EMPTY(&zh->zh_free[i-order_start]); i++);

	if (i == order) {
		zo_new = LDAP_LIST_FIRST(&zh->zh_free[i-order_start]);
		LDAP_LIST_REMOVE(zo_new, zo_link);
		ptr = zo_new->zo_ptr;
		idx = zo_new->zo_idx;
		diff = (unsigned long)((char*)ptr -
				(char*)zh->zh_zones[idx]) >> (order + 1);
		zh->zh_maps[idx][order-order_start][diff>>3] |= (1 << (diff & 0x7));
		*ptr++ = zh->zh_seqno[idx];
		*ptr++ = size - 2*sizeof(ber_len_t);
		zo_new->zo_ptr = NULL;
		zo_new->zo_idx = -1;
		LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_new, zo_link);
		ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
286
287
		Debug(LDAP_DEBUG_NONE, "slap_zn_malloc: returning 0x%x, 0x%x\n",
				ptr, (int)ptr>>(zh->zh_zoneorder+1), 0);
288
289
290
291
292
293
294
295
296
297
298
299
		return((void*)ptr);
	} else if (i <= zh->zh_zoneorder) {
		for (j = i; j > order; j--) {
			zo_left = LDAP_LIST_FIRST(&zh->zh_free[j-order_start]);
			LDAP_LIST_REMOVE(zo_left, zo_link);
			if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
				slap_replenish_zopool(zh);
			}
			zo_right = LDAP_LIST_FIRST(&zh->zh_zopool);
			LDAP_LIST_REMOVE(zo_right, zo_link);
			zo_right->zo_ptr = zo_left->zo_ptr + (1 << j);
			zo_right->zo_idx = zo_left->zo_idx;
300
301
302
			Debug(LDAP_DEBUG_NONE,
				"slap_zn_malloc: split (left=0x%x, right=0x%x)\n",
				zo_left->zo_ptr, zo_right->zo_ptr, 0);
303
304
305
306
307
308
309
310
311
312
313
314
			if (j == order + 1) {
				ptr = zo_left->zo_ptr;
				diff = (unsigned long)((char*)ptr -
						(char*)zh->zh_zones[zo_left->zo_idx]) >> (order+1);
				zh->zh_maps[zo_left->zo_idx][order-order_start][diff>>3] |=
						(1 << (diff & 0x7));
				*ptr++ = zh->zh_seqno[zo_left->zo_idx];
				*ptr++ = size - 2*sizeof(ber_len_t);
				LDAP_LIST_INSERT_HEAD(
						&zh->zh_free[j-1-order_start], zo_right, zo_link);
				LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_left, zo_link);
				ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
315
316
317
				Debug(LDAP_DEBUG_NONE,
					"slap_zn_malloc: returning 0x%x, 0x%x\n",
					ptr, (int)ptr>>(zh->zh_zoneorder+1), 0);
318
319
320
321
322
323
324
325
326
327
328
329
330
331
				return((void*)ptr);
			} else {
				LDAP_LIST_INSERT_HEAD(
						&zh->zh_free[j-1-order_start], zo_right, zo_link);
				LDAP_LIST_INSERT_HEAD(
						&zh->zh_free[j-1-order_start], zo_left, zo_link);
			}
		}
		assert(0);
	} else {

		if ( zh->zh_maxzones < zh->zh_numzones + zh->zh_deltazones ) {
			ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
			Debug( LDAP_DEBUG_TRACE,
332
				"zn_malloc %lu: ch_malloc\n",
333
				(long)size, 0, 0);
334
335
336
			Debug(LDAP_DEBUG_NONE,
				"slap_zn_malloc: returning 0x%x, 0x%x\n",
				ptr, (int)ptr>>(zh->zh_zoneorder+1), 0);
337
338
339
340
341
342
343
			return (void*)ch_malloc(size);
		}

		for (i = zh->zh_numzones; i < zh->zh_numzones+zh->zh_deltazones; i++) {
			zh->zh_zones[i] = mmap(0, zh->zh_zonesize, PROT_READ | PROT_WRITE,
								MAP_PRIVATE, zh->zh_fd, 0);
			zh->zh_maps[i] = (unsigned char **)
344
						ch_malloc((zh->zh_zoneorder - order_start + 1) *
345
						sizeof(unsigned char *));
346
			for (j = 0; j < zh->zh_zoneorder-order_start+1; j++) {
347
348
349
				int shiftamt = order_start + 1 + j;
				int nummaps = zh->zh_zonesize >> shiftamt;
				assert(nummaps);
350
				nummaps >>= 3;
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
				if (!nummaps) nummaps = 1;
				zh->zh_maps[i][j] = (unsigned char *)ch_malloc(nummaps);
				memset(zh->zh_maps[i][j], 0, nummaps);
			}
	
			if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
				slap_replenish_zopool(zh);
			}
			zo = LDAP_LIST_FIRST(&zh->zh_zopool);
			LDAP_LIST_REMOVE(zo, zo_link);
			zo->zo_ptr = zh->zh_zones[i];
			zo->zo_idx = i;
			LDAP_LIST_INSERT_HEAD(&zh->
						zh_free[zh->zh_zoneorder-order_start],zo,zo_link);
	
			if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
				slap_replenish_zopool(zh);
			}
			zo = LDAP_LIST_FIRST(&zh->zh_zopool);
			LDAP_LIST_REMOVE(zo, zo_link);
371
372
			zo->zo_ptr = zh->zh_zones[i];
			zo->zo_siz = zh->zh_zonesize;
373
374
			zo->zo_idx = i;
			avl_insert(&zh->zh_zonetree, zo, slap_zone_cmp, avl_dup_error);
375
			ldap_pvt_thread_rdwr_init(&zh->zh_znlock[i]);
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
		}
		zh->zh_numzones += zh->zh_deltazones;
		ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
		goto retry;
	}
}

void *
slap_zn_calloc( ber_len_t n, ber_len_t size, void *ctx )
{
	void *new;

	new = slap_zn_malloc( n*size, ctx );
	if ( new ) {
		memset( new, 0, n*size );
	}
	return new;
}

void *
slap_zn_realloc(void *ptr, ber_len_t size, void *ctx)
{
	struct zone_heap *zh = ctx;
	int pad = 2*sizeof(int)-1, pad_shift;
	int order_start = -1, order = -1;
	struct zone_object zoi, *zoo;
	ber_len_t *p = (ber_len_t *)ptr, *new;
	unsigned long diff;
	int i;
	void *newptr = NULL;
	struct zone_heap *zone = NULL;

408
409
410
	Debug(LDAP_DEBUG_NONE,
		"--> slap_zn_realloc: ptr=0x%x, size=%d\n", ptr, size, 0);

411
412
413
	if (ptr == NULL)
		return slap_zn_malloc(size, zh);

414
	zoi.zo_ptr = p;
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
	zoi.zo_idx = -1;

	if (zh) {
		ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
		zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
		ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
	}

	/* Not our memory? */
	if (!zoo) {
		/* duplicate of realloc behavior, oh well */
		new = ber_memrealloc_x(ptr, size, NULL);
		if (new) {
			return new;
		}
		Debug(LDAP_DEBUG_ANY, "ch_realloc of %lu bytes failed\n",
				(long) size, 0, 0);
		assert(0);
		exit( EXIT_FAILURE );
	}

	assert(zoo->zo_idx != -1);	

	zone = zh->zh_zones[zoo->zo_idx];

	if (size == 0) {
		slap_zn_free(ptr, zh);
		return NULL;
	}

	newptr = slap_zn_malloc(size, zh);
	if (size < p[-1]) {
		AC_MEMCPY(newptr, ptr, size);
	} else {
		AC_MEMCPY(newptr, ptr, p[-1]);
	}
	slap_zn_free(ptr, zh);
	return newptr;
}

void
slap_zn_free(void *ptr, void *ctx)
{
	struct zone_heap *zh = ctx;
	int size, size_shift, order_size;
	int pad = 2*sizeof(int)-1, pad_shift;
	ber_len_t *p = (ber_len_t *)ptr, *tmpp;
	int order_start = -1, order = -1;
	struct zone_object zoi, *zoo, *zo;
	unsigned long diff;
	int i, k, inserted = 0, idx;
	struct zone_heap *zone = NULL;

468
	zoi.zo_ptr = p;
469
470
	zoi.zo_idx = -1;

471
472
	Debug(LDAP_DEBUG_NONE, "--> slap_zn_free: ptr=0x%x\n", ptr, 0, 0);

473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
	if (zh) {
		ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
		zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
		ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
	}

	if (!zoo) {
		ber_memfree_x(ptr, NULL);
	} else {
		idx = zoo->zo_idx;
		assert(idx != -1);
		zone = zh->zh_zones[idx];

		size = *(--p);
		size_shift = size + 2*sizeof(ber_len_t) - 1;
		do {
			order++;
		} while (size_shift >>= 1);

		pad_shift = pad - 1;
		do {
			order_start++;
		} while (pad_shift >>= 1);

		ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
		for (i = order, tmpp = p; i <= zh->zh_zoneorder; i++) {
			order_size = 1 << (i+1);
			diff = (unsigned long)((char*)tmpp - (char*)zone) >> (i+1);
			zh->zh_maps[idx][i-order_start][diff>>3] &= (~(1 << (diff & 0x7)));
			if (diff == ((diff>>1)<<1)) {
				if (!(zh->zh_maps[idx][i-order_start][(diff+1)>>3] &
						(1<<((diff+1)&0x7)))) {
					zo = LDAP_LIST_FIRST(&zh->zh_free[i-order_start]);
					while (zo) {
						if ((char*)zo->zo_ptr == (char*)tmpp) {
							LDAP_LIST_REMOVE( zo, zo_link );
						} else if ((char*)zo->zo_ptr ==
								(char*)tmpp + order_size) {
							LDAP_LIST_REMOVE(zo, zo_link);
							break;
						}
						zo = LDAP_LIST_NEXT(zo, zo_link);
					}
					if (zo) {
						if (i < zh->zh_zoneorder) {
							inserted = 1;
							zo->zo_ptr = tmpp;
520
521
522
							Debug(LDAP_DEBUG_NONE,
								"slap_zn_free: merging 0x%x\n",
								zo->zo_ptr, 0, 0);
523
524
525
526
527
528
529
530
531
532
533
							LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start+1],
									zo, zo_link);
						}
						continue;
					} else {
						if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
							slap_replenish_zopool(zh);
						}
						zo = LDAP_LIST_FIRST(&zh->zh_zopool);
						LDAP_LIST_REMOVE(zo, zo_link);
						zo->zo_ptr = tmpp;
534
						zo->zo_idx = idx;
535
536
537
						Debug(LDAP_DEBUG_NONE,
							"slap_zn_free: merging 0x%x\n",
							zo->zo_ptr, 0, 0);
538
539
540
541
542
543
544
						LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
								zo, zo_link);
						break;

						Debug(LDAP_DEBUG_ANY, "slap_zn_free: "
							"free object not found while bit is clear.\n",
							0, 0, 0);
545
						assert(zo != NULL);
546
547
548
549
550
551
552
553
554
555

					}
				} else {
					if (!inserted) {
						if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
							slap_replenish_zopool(zh);
						}
						zo = LDAP_LIST_FIRST(&zh->zh_zopool);
						LDAP_LIST_REMOVE(zo, zo_link);
						zo->zo_ptr = tmpp;
556
						zo->zo_idx = idx;
557
558
559
						Debug(LDAP_DEBUG_NONE,
							"slap_zn_free: merging 0x%x\n",
							zo->zo_ptr, 0, 0);
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
						LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
								zo, zo_link);
					}
					break;
				}
			} else {
				if (!(zh->zh_maps[idx][i-order_start][(diff-1)>>3] &
						(1<<((diff-1)&0x7)))) {
					zo = LDAP_LIST_FIRST(&zh->zh_free[i-order_start]);
					while (zo) {
						if ((char*)zo->zo_ptr == (char*)tmpp) {
							LDAP_LIST_REMOVE(zo, zo_link);
						} else if ((char*)tmpp == zo->zo_ptr + order_size) {
							LDAP_LIST_REMOVE(zo, zo_link);
							tmpp = zo->zo_ptr;
							break;
						}
						zo = LDAP_LIST_NEXT(zo, zo_link);
					}
					if (zo) {
						if (i < zh->zh_zoneorder) {
							inserted = 1;
582
583
584
							Debug(LDAP_DEBUG_NONE,
								"slap_zn_free: merging 0x%x\n",
								zo->zo_ptr, 0, 0);
585
586
587
588
589
590
591
592
593
594
595
							LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start+1],
									zo, zo_link);
							continue;
						}
					} else {
						if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
							slap_replenish_zopool(zh);
						}
						zo = LDAP_LIST_FIRST(&zh->zh_zopool);
						LDAP_LIST_REMOVE(zo, zo_link);
						zo->zo_ptr = tmpp;
596
						zo->zo_idx = idx;
597
598
599
						Debug(LDAP_DEBUG_NONE,
							"slap_zn_free: merging 0x%x\n",
							zo->zo_ptr, 0, 0);
600
601
602
603
604
605
606
						LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
								zo, zo_link);
						break;

						Debug(LDAP_DEBUG_ANY, "slap_zn_free: "
							"free object not found while bit is clear.\n",
							0, 0, 0 );
607
						assert(zo != NULL);
608
609
610
611
612
613
614
615
616
617

					}
				} else {
					if ( !inserted ) {
						if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
							slap_replenish_zopool(zh);
						}
						zo = LDAP_LIST_FIRST(&zh->zh_zopool);
						LDAP_LIST_REMOVE(zo, zo_link);
						zo->zo_ptr = tmpp;
618
						zo->zo_idx = idx;
619
620
621
						Debug(LDAP_DEBUG_NONE,
							"slap_zn_free: merging 0x%x\n",
							zo->zo_ptr, 0, 0);
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
						LDAP_LIST_INSERT_HEAD(&zh->zh_free[i-order_start],
								zo, zo_link);
					}
					break;
				}
			}
		}
		ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
	}
}

static int
slap_zone_cmp(const void *v1, const void *v2)
{
	const struct zone_object *zo1 = v1;
	const struct zone_object *zo2 = v2;
638
639
640
	char *ptr1;
	char *ptr2;
	ber_len_t zpad;
641

642
643
644
645
646
	zpad = zo2->zo_siz - 1;
	ptr1 = (char*)(((unsigned long)zo1->zo_ptr + zpad) & ~zpad);
	ptr2 = (char*)zo2->zo_ptr + ((char*)ptr1 - (char*)zo1->zo_ptr);
	ptr2 = (char*)(((unsigned long)ptr2 + zpad) & ~zpad);
	return (int)((char*)ptr1 - (char*)ptr2);
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
}

void *
slap_replenish_zopool(
	void *ctx
)
{
	struct zone_heap* zh = ctx;
	struct zone_object *zo_block;
	int i;

	zo_block = (struct zone_object *)ch_malloc(
					SLAP_ZONE_ZOBLOCK * sizeof(struct zone_object));

	if ( zo_block == NULL ) {
		return NULL;
	}

	zo_block[0].zo_blockhead = 1;
	LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, &zo_block[0], zo_link);
	for (i = 1; i < SLAP_ZONE_ZOBLOCK; i++) {
		zo_block[i].zo_blockhead = 0;
		LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, &zo_block[i], zo_link );
	}

	return zo_block;
}
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896

int
slap_zn_invalidate(
	void *ctx,
	void *ptr
)
{
	struct zone_heap* zh = ctx;
	struct zone_object zoi, *zoo;
	struct zone_heap *zone = NULL;
	int seqno = *((ber_len_t*)ptr - 2);
	int idx = -1, rc = 0;
	int pad = 2*sizeof(int)-1, pad_shift;
	int order_start = -1, i;
	struct zone_object *zo;

	pad_shift = pad - 1;
	do {
		order_start++;
	} while (pad_shift >>= 1);

	zoi.zo_ptr = ptr;
	zoi.zo_idx = -1;

	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
	zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);

	if (zoo) {
		idx = zoo->zo_idx;
		assert(idx != -1);
		madvise(zh->zh_zones[idx], zh->zh_zonesize, MADV_DONTNEED);
		for (i = 0; i < zh->zh_zoneorder - order_start + 1; i++) {
			int shiftamt = order_start + 1 + i;
			int nummaps = zh->zh_zonesize >> shiftamt;
			assert(nummaps);
			nummaps >>= 3;
			if (!nummaps) nummaps = 1;
			memset(zh->zh_maps[idx][i], 0, nummaps);
			zo = LDAP_LIST_FIRST(&zh->zh_free[i]);
			while (zo) {
				struct zone_object *zo_tmp = zo;
				zo = LDAP_LIST_NEXT(zo, zo_link);
				if (zo_tmp && zo_tmp->zo_idx == idx) {
					LDAP_LIST_REMOVE(zo_tmp, zo_link);
					LDAP_LIST_INSERT_HEAD(&zh->zh_zopool, zo_tmp, zo_link);
				}
			}
		}
		if (LDAP_LIST_EMPTY(&zh->zh_zopool)) {
			slap_replenish_zopool(zh);
		}
		zo = LDAP_LIST_FIRST(&zh->zh_zopool);
		LDAP_LIST_REMOVE(zo, zo_link);
		zo->zo_ptr = zh->zh_zones[idx];
		zo->zo_idx = idx;
		LDAP_LIST_INSERT_HEAD(&zh->zh_free[zh->zh_zoneorder-order_start],
								zo, zo_link);
		zh->zh_seqno[idx]++;
	} else {
		Debug(LDAP_DEBUG_NONE, "zone not found for (ctx=0x%x, ptr=0x%x) !\n",
				ctx, ptr, 0);
	}

	ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );
	Debug(LDAP_DEBUG_NONE, "zone %d invalidate\n", idx, 0, 0);
	return rc;
}

int
slap_zn_validate(
	void *ctx,
	void *ptr,
	int seqno
)
{
	struct zone_heap* zh = ctx;
	struct zone_object zoi, *zoo;
	struct zone_heap *zone = NULL;
	int idx, rc = 0;

	zoi.zo_ptr = ptr;
	zoi.zo_idx = -1;

	zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);

	if (zoo) {
		idx = zoo->zo_idx;
		assert(idx != -1);
		assert(seqno <= zh->zh_seqno[idx]);
		rc = (seqno == zh->zh_seqno[idx]);
	}

	return rc;
}

int slap_zh_rlock(
	void *ctx
)
{
	struct zone_heap* zh = ctx;
	ldap_pvt_thread_rdwr_rlock(&zh->zh_lock);
}

int slap_zh_runlock(
	void *ctx
)
{
	struct zone_heap* zh = ctx;
	ldap_pvt_thread_rdwr_runlock(&zh->zh_lock);
}

int slap_zh_wlock(
	void *ctx
)
{
	struct zone_heap* zh = ctx;
	ldap_pvt_thread_rdwr_wlock(&zh->zh_lock);
}

int slap_zh_wunlock(
	void *ctx
)
{
	struct zone_heap* zh = ctx;
	ldap_pvt_thread_rdwr_wunlock(&zh->zh_lock);
}

int slap_zn_rlock(
	void *ctx,
	void *ptr
)
{
	struct zone_heap* zh = ctx;
	struct zone_object zoi, *zoo;
	struct zone_heap *zone = NULL;
	int idx;

	zoi.zo_ptr = ptr;
	zoi.zo_idx = -1;

	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
	zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
	ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );

	if (zoo) {
		idx = zoo->zo_idx;
		assert(idx != -1);
		ldap_pvt_thread_rdwr_rlock(&zh->zh_znlock[idx]);
	}
}

int slap_zn_runlock(
	void *ctx,
	void *ptr
)
{
	struct zone_heap* zh = ctx;
	struct zone_object zoi, *zoo;
	struct zone_heap *zone = NULL;
	int idx;

	zoi.zo_ptr = ptr;
	zoi.zo_idx = -1;

	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
	zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
	ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );

	if (zoo) {
		idx = zoo->zo_idx;
		assert(idx != -1);
		ldap_pvt_thread_rdwr_runlock(&zh->zh_znlock[idx]);
	}
}

int slap_zn_wlock(
	void *ctx,
	void *ptr
)
{
	struct zone_heap* zh = ctx;
	struct zone_object zoi, *zoo;
	struct zone_heap *zone = NULL;
	int idx;

	zoi.zo_ptr = ptr;
	zoi.zo_idx = -1;

	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
	zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
	ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );

	if (zoo) {
		idx = zoo->zo_idx;
		assert(idx != -1);
		ldap_pvt_thread_rdwr_wlock(&zh->zh_znlock[idx]);
	}
}

int slap_zn_wunlock(
	void *ctx,
	void *ptr
)
{
	struct zone_heap* zh = ctx;
	struct zone_object zoi, *zoo;
	struct zone_heap *zone = NULL;
	int idx;

	zoi.zo_ptr = ptr;
	zoi.zo_idx = -1;

	ldap_pvt_thread_mutex_lock( &zh->zh_mutex );
	zoo = avl_find(zh->zh_zonetree, &zoi, slap_zone_cmp);
	ldap_pvt_thread_mutex_unlock( &zh->zh_mutex );

	if (zoo) {
		idx = zoo->zo_idx;
		assert(idx != -1);
		ldap_pvt_thread_rdwr_wunlock(&zh->zh_znlock[idx]);
	}
}

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
#define T_SEC_IN_USEC 1000000

static int
slap_timediff(struct timeval *tv_begin, struct timeval *tv_end)
{
	uint64_t t_begin, t_end, t_diff;

	t_begin = T_SEC_IN_USEC * tv_begin->tv_sec + tv_begin->tv_usec;
	t_end  = T_SEC_IN_USEC * tv_end->tv_sec  + tv_end->tv_usec;
	t_diff  = t_end - t_begin;
	
	if ( t_diff < 0 )
		t_diff = 0;

	return (int)t_diff;
}

void
slap_set_timing(struct timeval *tv_set)
{
	gettimeofday(tv_set, (struct timezone *)NULL);
}

int
slap_measure_timing(struct timeval *tv_set, struct timeval *tv_measure)
{
	gettimeofday(tv_measure, (struct timezone *)NULL);
	return(slap_timediff(tv_set, tv_measure));
}

#define EMA_WEIGHT 0.999000
#define SLAP_ZN_LATENCY_HISTORY_QLEN 500
int
slap_zn_latency_history(void* ctx, int ea_latency)
{
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
932
/* TODO: monitor /proc/stat (swap) as well */
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
	struct zone_heap* zh = ctx;
	double t_diff = 0.0;

	zh->zh_ema_latency = (double)ea_latency * (1.0 - EMA_WEIGHT)
					+ zh->zh_ema_latency * EMA_WEIGHT;
	if (!zh->zh_swapping && zh->zh_ema_samples++ % 100 == 99) {
		struct zone_latency_history *zlh_entry;
		zlh_entry = ch_calloc(1, sizeof(struct zone_latency_history));
		zlh_entry->zlh_latency = zh->zh_ema_latency;
		LDAP_STAILQ_INSERT_TAIL(
				&zh->zh_latency_history_queue, zlh_entry, zlh_next);
		zh->zh_latency_history_qlen++;
		while (zh->zh_latency_history_qlen > SLAP_ZN_LATENCY_HISTORY_QLEN) {
			struct zone_latency_history *zlh;
			zlh = LDAP_STAILQ_FIRST(&zh->zh_latency_history_queue);
			LDAP_STAILQ_REMOVE_HEAD(
					&zh->zh_latency_history_queue, zlh_next);
			zh->zh_latency_history_qlen--;
			ch_free(zlh);
		}
		if (zh->zh_latency_history_qlen == SLAP_ZN_LATENCY_HISTORY_QLEN) {
			struct zone_latency_history *zlh_first, *zlh_last;
			zlh_first = LDAP_STAILQ_FIRST(&zh->zh_latency_history_queue);
			zlh_last = LDAP_STAILQ_LAST(&zh->zh_latency_history_queue,
						zone_latency_history, zlh_next);
			t_diff = zlh_last->zlh_latency - zlh_first->zlh_latency;
		}
		if (t_diff >= 2000) {
			zh->zh_latency_jump++;
		} else {
			zh->zh_latency_jump = 0;
		}
		if (zh->zh_latency_jump > 3) {
			zh->zh_latency_jump = 0;
			zh->zh_swapping = 1;
		}
	}
	return zh->zh_swapping;
}
972
#endif /* SLAP_ZONE_ALLOC */