1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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
408
409
410
411
412
413
414
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
468
469
470
471
472
473
474
475
476
477
|
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#if HAVE_CMOCKA
#include <fcntl.h>
#include <sched.h> /* IWYU pragma: keep */
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/file.h>
#include <isc/mem.h>
#include <isc/mutex.h>
#include <isc/os.h>
#include <isc/print.h>
#include <isc/result.h>
#include <isc/stdio.h>
#include <isc/thread.h>
#include <isc/time.h>
#include <isc/util.h>
#include "../mem_p.h"
#include "isctest.h"
static int
_setup(void **state) {
isc_result_t result;
UNUSED(state);
result = isc_test_begin(NULL, true, 0);
assert_int_equal(result, ISC_R_SUCCESS);
return (0);
}
static int
_teardown(void **state) {
UNUSED(state);
isc_test_end();
return (0);
}
#define MP1_FREEMAX 10
#define MP1_FILLCNT 10
#define MP1_MAXALLOC 30
#define MP2_FREEMAX 25
#define MP2_FILLCNT 25
/* general memory system tests */
static void
isc_mem_test(void **state) {
void *items1[50];
void *items2[50];
void *tmp;
isc_mempool_t *mp1 = NULL, *mp2 = NULL;
unsigned int i, j;
int rval;
UNUSED(state);
isc_mempool_create(test_mctx, 24, &mp1);
isc_mempool_create(test_mctx, 31, &mp2);
isc_mempool_setfreemax(mp1, MP1_FREEMAX);
isc_mempool_setfillcount(mp1, MP1_FILLCNT);
isc_mempool_setmaxalloc(mp1, MP1_MAXALLOC);
/*
* Allocate MP1_MAXALLOC items from the pool. This is our max.
*/
for (i = 0; i < MP1_MAXALLOC; i++) {
items1[i] = isc_mempool_get(mp1);
assert_non_null(items1[i]);
}
/*
* Try to allocate one more. This should fail.
*/
tmp = isc_mempool_get(mp1);
assert_null(tmp);
/*
* Free the first 11 items. Verify that there are 10 free items on
* the free list (which is our max).
*/
for (i = 0; i < 11; i++) {
isc_mempool_put(mp1, items1[i]);
items1[i] = NULL;
}
#if !__SANITIZE_ADDRESS__
rval = isc_mempool_getfreecount(mp1);
assert_int_equal(rval, 10);
#endif /* !__SANITIZE_ADDRESS__ */
rval = isc_mempool_getallocated(mp1);
assert_int_equal(rval, 19);
/*
* Now, beat up on mp2 for a while. Allocate 50 items, then free
* them, then allocate 50 more, etc.
*/
isc_mempool_setfreemax(mp2, 25);
isc_mempool_setfillcount(mp2, 25);
for (j = 0; j < 500000; j++) {
for (i = 0; i < 50; i++) {
items2[i] = isc_mempool_get(mp2);
assert_non_null(items2[i]);
}
for (i = 0; i < 50; i++) {
isc_mempool_put(mp2, items2[i]);
items2[i] = NULL;
}
}
/*
* Free all the other items and blow away this pool.
*/
for (i = 11; i < MP1_MAXALLOC; i++) {
isc_mempool_put(mp1, items1[i]);
items1[i] = NULL;
}
isc_mempool_destroy(&mp1);
isc_mempool_destroy(&mp2);
isc_mempool_create(test_mctx, 2, &mp1);
tmp = isc_mempool_get(mp1);
assert_non_null(tmp);
isc_mempool_put(mp1, tmp);
isc_mempool_destroy(&mp1);
}
/* test TotalUse calculation */
static void
isc_mem_total_test(void **state) {
isc_mem_t *mctx2 = NULL;
size_t before, after;
ssize_t diff;
int i;
UNUSED(state);
/* Local alloc, free */
mctx2 = NULL;
isc_mem_create(&mctx2);
before = isc_mem_total(mctx2);
for (i = 0; i < 100000; i++) {
void *ptr;
ptr = isc_mem_allocate(mctx2, 2048);
isc_mem_free(mctx2, ptr);
}
after = isc_mem_total(mctx2);
diff = after - before;
/* 2048 +8 bytes extra for size_info */
assert_int_equal(diff, (2048 + 8) * 100000);
/* ISC_MEMFLAG_INTERNAL */
before = isc_mem_total(test_mctx);
for (i = 0; i < 100000; i++) {
void *ptr;
ptr = isc_mem_allocate(test_mctx, 2048);
isc_mem_free(test_mctx, ptr);
}
after = isc_mem_total(test_mctx);
diff = after - before;
/* 2048 +8 bytes extra for size_info */
assert_int_equal(diff, (2048 + 8) * 100000);
isc_mem_destroy(&mctx2);
}
/* test InUse calculation */
static void
isc_mem_inuse_test(void **state) {
isc_mem_t *mctx2 = NULL;
size_t before, after;
ssize_t diff;
void *ptr;
UNUSED(state);
mctx2 = NULL;
isc_mem_create(&mctx2);
before = isc_mem_inuse(mctx2);
ptr = isc_mem_allocate(mctx2, 1024000);
isc_mem_free(mctx2, ptr);
after = isc_mem_inuse(mctx2);
diff = after - before;
assert_int_equal(diff, 0);
isc_mem_destroy(&mctx2);
}
#if ISC_MEM_TRACKLINES
/* test mem with no flags */
static void
isc_mem_noflags_test(void **state) {
isc_result_t result;
isc_mem_t *mctx2 = NULL;
char buf[4096], *p, *q;
FILE *f;
void *ptr;
result = isc_stdio_open("mem.output", "w", &f);
assert_int_equal(result, ISC_R_SUCCESS);
UNUSED(state);
isc_mem_create(&mctx2);
isc_mem_debugging = 0;
ptr = isc_mem_get(mctx2, 2048);
assert_non_null(ptr);
isc__mem_printactive(mctx2, f);
isc_mem_put(mctx2, ptr, 2048);
isc_mem_destroy(&mctx2);
isc_stdio_close(f);
memset(buf, 0, sizeof(buf));
result = isc_stdio_open("mem.output", "r", &f);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
assert_int_equal(result, ISC_R_EOF);
isc_stdio_close(f);
isc_file_remove("mem.output");
buf[sizeof(buf) - 1] = 0;
p = strchr(buf, '\n');
assert_non_null(p);
assert_in_range(p, 0, buf + sizeof(buf) - 3);
p += 2;
q = strchr(p, '\n');
assert_non_null(q);
*q = '\0';
assert_string_equal(p, "None.");
isc_mem_debugging = ISC_MEM_DEBUGRECORD;
}
/* test mem with record flag */
static void
isc_mem_recordflag_test(void **state) {
isc_result_t result;
isc_mem_t *mctx2 = NULL;
char buf[4096], *p;
FILE *f;
void *ptr;
result = isc_stdio_open("mem.output", "w", &f);
assert_int_equal(result, ISC_R_SUCCESS);
UNUSED(state);
isc_mem_create(&mctx2);
ptr = isc_mem_get(mctx2, 2048);
assert_non_null(ptr);
isc__mem_printactive(mctx2, f);
isc_mem_put(mctx2, ptr, 2048);
isc_mem_destroy(&mctx2);
isc_stdio_close(f);
memset(buf, 0, sizeof(buf));
result = isc_stdio_open("mem.output", "r", &f);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
assert_int_equal(result, ISC_R_EOF);
isc_stdio_close(f);
isc_file_remove("mem.output");
buf[sizeof(buf) - 1] = 0;
p = strchr(buf, '\n');
assert_non_null(p);
assert_in_range(p, 0, buf + sizeof(buf) - 3);
assert_memory_equal(p + 2, "ptr ", 4);
p = strchr(p + 1, '\n');
assert_non_null(p);
assert_int_equal(strlen(p), 1);
}
/* test mem with trace flag */
static void
isc_mem_traceflag_test(void **state) {
isc_result_t result;
isc_mem_t *mctx2 = NULL;
char buf[4096], *p;
FILE *f;
void *ptr;
/* redirect stderr so we can check trace output */
f = freopen("mem.output", "w", stderr);
assert_non_null(f);
UNUSED(state);
isc_mem_create(&mctx2);
isc_mem_debugging = ISC_MEM_DEBUGTRACE;
ptr = isc_mem_get(mctx2, 2048);
assert_non_null(ptr);
isc__mem_printactive(mctx2, f);
isc_mem_put(mctx2, ptr, 2048);
isc_mem_destroy(&mctx2);
isc_stdio_close(f);
memset(buf, 0, sizeof(buf));
result = isc_stdio_open("mem.output", "r", &f);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
assert_int_equal(result, ISC_R_EOF);
isc_stdio_close(f);
isc_file_remove("mem.output");
/* return stderr to TTY so we can see errors */
f = freopen("/dev/tty", "w", stderr);
buf[sizeof(buf) - 1] = 0;
assert_memory_equal(buf, "add ", 4);
p = strchr(buf, '\n');
assert_non_null(p);
p = strchr(p + 1, '\n');
assert_non_null(p);
assert_in_range(p, 0, buf + sizeof(buf) - 3);
assert_memory_equal(p + 2, "ptr ", 4);
p = strchr(p + 1, '\n');
assert_non_null(p);
assert_memory_equal(p + 1, "del ", 4);
isc_mem_debugging = ISC_MEM_DEBUGRECORD;
}
#endif /* if ISC_MEM_TRACKLINES */
#if !defined(__SANITIZE_THREAD__)
#define ITERS 512
#define NUM_ITEMS 1024 /* 768 */
#define ITEM_SIZE 65534
static isc_threadresult_t
mem_thread(isc_threadarg_t arg) {
void *items[NUM_ITEMS];
size_t size = *((size_t *)arg);
for (int i = 0; i < ITERS; i++) {
for (int j = 0; j < NUM_ITEMS; j++) {
items[j] = isc_mem_get(test_mctx, size);
}
for (int j = 0; j < NUM_ITEMS; j++) {
isc_mem_put(test_mctx, items[j], size);
}
}
return ((isc_threadresult_t)0);
}
static void
isc_mem_benchmark(void **state) {
int nthreads = ISC_MAX(ISC_MIN(isc_os_ncpus(), 32), 1);
isc_thread_t threads[32];
isc_time_t ts1, ts2;
double t;
isc_result_t result;
size_t size = ITEM_SIZE;
UNUSED(state);
result = isc_time_now(&ts1);
assert_int_equal(result, ISC_R_SUCCESS);
for (int i = 0; i < nthreads; i++) {
isc_thread_create(mem_thread, &size, &threads[i]);
size = size / 2;
}
for (int i = 0; i < nthreads; i++) {
isc_thread_join(threads[i], NULL);
}
result = isc_time_now(&ts2);
assert_int_equal(result, ISC_R_SUCCESS);
t = isc_time_microdiff(&ts2, &ts1);
printf("[ TIME ] isc_mem_benchmark: "
"%d isc_mem_{get,put} calls, %f seconds, %f calls/second\n",
nthreads * ITERS * NUM_ITEMS, t / 1000000.0,
(nthreads * ITERS * NUM_ITEMS) / (t / 1000000.0));
}
#endif /* __SANITIZE_THREAD */
/*
* Main
*/
int
main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(isc_mem_test, _setup,
_teardown),
cmocka_unit_test_setup_teardown(isc_mem_total_test, _setup,
_teardown),
cmocka_unit_test_setup_teardown(isc_mem_inuse_test, _setup,
_teardown),
#if !defined(__SANITIZE_THREAD__)
cmocka_unit_test_setup_teardown(isc_mem_benchmark, _setup,
_teardown),
#endif /* __SANITIZE_THREAD__ */
#if ISC_MEM_TRACKLINES
cmocka_unit_test_setup_teardown(isc_mem_noflags_test, _setup,
_teardown),
cmocka_unit_test_setup_teardown(isc_mem_recordflag_test, _setup,
_teardown),
/*
* traceflag_test closes stderr, which causes weird
* side effects for any next test trying to use libuv.
* This test has to be the last one to avoid problems.
*/
cmocka_unit_test_setup_teardown(isc_mem_traceflag_test, _setup,
_teardown),
#endif /* if ISC_MEM_TRACKLINES */
};
return (cmocka_run_group_tests(tests, NULL, NULL));
}
#else /* HAVE_CMOCKA */
#include <stdio.h>
int
main(void) {
printf("1..0 # Skipped: cmocka not available\n");
return (SKIPPED_TEST_EXIT_CODE);
}
#endif /* if HAVE_CMOCKA */
|