Debian Codegraph @

tests/isc/mem_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <fcntl.h>
15 #include <inttypes.h>
16 #include <sched.h> /* IWYU pragma: keep */
17 #include <setjmp.h>
18 #include <stdarg.h>
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22
23 #define UNIT_TESTING
24 #include <cmocka.h>
25
26 #include <isc/atomic.h>
27 #include <isc/file.h>
28 #include <isc/mem.h>
29 #include <isc/mutex.h>
30 #include <isc/os.h>
31 #include <isc/random.h>
32 #include <isc/result.h>
33 #include <isc/stdio.h>
34 #include <isc/thread.h>
35 #include <isc/time.h>
36 #include <isc/util.h>
37
38 #include "mem_p.h"
39
40 #include <tests/isc.h>
41
42 #define MP1_FREEMAX 10
43 #define MP1_FILLCNT 10
44 #define MP1_MAXALLOC 30
45
46 #define MP2_FREEMAX 25
47 #define MP2_FILLCNT 25
48
49 /* general memory system tests */
50 ISC_RUN_TEST_IMPL(isc_mem_get) {
51 void *items1[50];
52 void *items2[50];
53 void *tmp;
54 isc_mempool_t *mp1 = NULL, *mp2 = NULL;
55 unsigned int i, j;
56 int rval;
57
58 isc_mempool_create(mctx, 24, &mp1);
59 isc_mempool_create(mctx, 31, &mp2);
60
61 isc_mempool_setfreemax(mp1, MP1_FREEMAX);
62 isc_mempool_setfillcount(mp1, MP1_FILLCNT);
63
64 /*
65 * Allocate MP1_MAXALLOC items from the pool. This is our max.
66 */
67 for (i = 0; i < MP1_MAXALLOC; i++) {
68 items1[i] = isc_mempool_get(mp1);
69 assert_non_null(items1[i]);
70 }
71
72 /*
73 * Free the first 11 items. Verify that there are 10 free items on
74 * the free list (which is our max).
75 */
76 for (i = 0; i < 11; i++) {
77 isc_mempool_put(mp1, items1[i]);
78 items1[i] = NULL;
79 }
80
81 #if !__SANITIZE_ADDRESS__
82 rval = isc_mempool_getfreecount(mp1);
83 assert_int_equal(rval, 10);
84 #endif /* !__SANITIZE_ADDRESS__ */
85
86 rval = isc_mempool_getallocated(mp1);
87 assert_int_equal(rval, 19);
88
89 /*
90 * Now, beat up on mp2 for a while. Allocate 50 items, then free
91 * them, then allocate 50 more, etc.
92 */
93
94 isc_mempool_setfreemax(mp2, 25);
95 isc_mempool_setfillcount(mp2, 25);
96
97 for (j = 0; j < 500000; j++) {
98 for (i = 0; i < 50; i++) {
99 items2[i] = isc_mempool_get(mp2);
100 assert_non_null(items2[i]);
101 }
102 for (i = 0; i < 50; i++) {
103 isc_mempool_put(mp2, items2[i]);
104 items2[i] = NULL;
105 }
106 }
107
108 /*
109 * Free all the other items and blow away this pool.
110 */
111 for (i = 11; i < MP1_MAXALLOC; i++) {
112 isc_mempool_put(mp1, items1[i]);
113 items1[i] = NULL;
114 }
115
116 isc_mempool_destroy(&mp1);
117 isc_mempool_destroy(&mp2);
118
119 isc_mempool_create(mctx, 2, &mp1);
120
121 tmp = isc_mempool_get(mp1);
122 assert_non_null(tmp);
123
124 isc_mempool_put(mp1, tmp);
125
126 isc_mempool_destroy(&mp1);
127 }
128
129 /* zeroed memory system tests */
130 ISC_RUN_TEST_IMPL(isc_mem_cget_zero) {
131 uint8_t *ptr;
132 bool zeroed;
133 uint8_t expected[4096] = { 0 };
134
135 /* Skip the test if the memory is zeroed even in normal case */
136 zeroed = true;
137 ptr = isc_mem_get(mctx, sizeof(expected));
138 for (size_t i = 0; i < sizeof(expected); i++) {
139 if (ptr[i] != expected[i]) {
140 zeroed = false;
141 break;
142 }
143 }
144 isc_mem_put(mctx, ptr, sizeof(expected));
145 if (zeroed) {
146 skip();
147 return;
148 }
149
150 ptr = isc_mem_cget(mctx, 1, sizeof(expected));
151 assert_memory_equal(ptr, expected, sizeof(expected));
152 isc_mem_put(mctx, ptr, sizeof(expected));
153 }
154
155 ISC_RUN_TEST_IMPL(isc_mem_callocate_zero) {
156 uint8_t *ptr;
157 bool zeroed;
158 uint8_t expected[4096] = { 0 };
159
160 /* Skip the test if the memory is zeroed even in normal case */
161 zeroed = true;
162 ptr = isc_mem_get(mctx, sizeof(expected));
163 for (size_t i = 0; i < sizeof(expected); i++) {
164 if (ptr[i] != expected[i]) {
165 zeroed = false;
166 break;
167 }
168 }
169 isc_mem_put(mctx, ptr, sizeof(expected));
170 if (zeroed) {
171 skip();
172 return;
173 }
174
175 ptr = isc_mem_callocate(mctx, 1, sizeof(expected));
176 assert_memory_equal(ptr, expected, sizeof(expected));
177 isc_mem_free(mctx, ptr);
178 }
179
180 /* test InUse calculation */
181 ISC_RUN_TEST_IMPL(isc_mem_inuse) {
182 isc_mem_t *mctx2 = NULL;
183 size_t before, after;
184 ssize_t diff;
185 void *ptr;
186
187 mctx2 = NULL;
188 isc_mem_create(&mctx2);
189
190 before = isc_mem_inuse(mctx2);
191 ptr = isc_mem_allocate(mctx2, 1024000);
192 isc_mem_free(mctx2, ptr);
193 after = isc_mem_inuse(mctx2);
194
195 diff = after - before;
196
197 assert_int_equal(diff, 0);
198
199 isc_mem_destroy(&mctx2);
200 }
201
202 ISC_RUN_TEST_IMPL(isc_mem_zeroget) {
203 uint8_t *data = NULL;
204
205 data = isc_mem_get(mctx, 0);
206 assert_non_null(data);
207 isc_mem_put(mctx, data, 0);
208 }
209
210 #define REGET_INIT_SIZE 1024
211 #define REGET_GROW_SIZE 2048
212 #define REGET_SHRINK_SIZE 512
213
214 ISC_RUN_TEST_IMPL(isc_mem_reget) {
215 uint8_t *data = NULL;
216
217 /* test that we can reget NULL */
218 data = isc_mem_reget(mctx, NULL, 0, REGET_INIT_SIZE);
219 assert_non_null(data);
220 isc_mem_put(mctx, data, REGET_INIT_SIZE);
221
222 /* test that we can re-get a zero-length allocation */
223 data = isc_mem_get(mctx, 0);
224 assert_non_null(data);
225
226 data = isc_mem_reget(mctx, data, 0, REGET_INIT_SIZE);
227 assert_non_null(data);
228
229 for (size_t i = 0; i < REGET_INIT_SIZE; i++) {
230 data[i] = i % UINT8_MAX;
231 }
232
233 data = isc_mem_reget(mctx, data, REGET_INIT_SIZE, REGET_GROW_SIZE);
234 assert_non_null(data);
235
236 for (size_t i = 0; i < REGET_INIT_SIZE; i++) {
237 assert_int_equal(data[i], i % UINT8_MAX);
238 }
239
240 for (size_t i = REGET_GROW_SIZE; i > 0; i--) {
241 data[i - 1] = i % UINT8_MAX;
242 }
243
244 data = isc_mem_reget(mctx, data, REGET_GROW_SIZE, REGET_SHRINK_SIZE);
245 assert_non_null(data);
246
247 for (size_t i = REGET_SHRINK_SIZE; i > 0; i--) {
248 assert_int_equal(data[i - 1], i % UINT8_MAX);
249 }
250
251 isc_mem_put(mctx, data, REGET_SHRINK_SIZE);
252 }
253
254 ISC_RUN_TEST_IMPL(isc_mem_reallocate) {
255 uint8_t *data = NULL;
256
257 /* test that we can reallocate NULL */
258 data = isc_mem_reallocate(mctx, NULL, REGET_INIT_SIZE);
259 assert_non_null(data);
260 isc_mem_free(mctx, data);
261
262 /* test that we can re-get a zero-length allocation */
263 data = isc_mem_allocate(mctx, 0);
264 assert_non_null(data);
265
266 data = isc_mem_reallocate(mctx, data, REGET_INIT_SIZE);
267 assert_non_null(data);
268
269 for (size_t i = 0; i < REGET_INIT_SIZE; i++) {
270 data[i] = i % UINT8_MAX;
271 }
272
273 data = isc_mem_reallocate(mctx, data, REGET_GROW_SIZE);
274 assert_non_null(data);
275
276 for (size_t i = 0; i < REGET_INIT_SIZE; i++) {
277 assert_int_equal(data[i], i % UINT8_MAX);
278 }
279
280 for (size_t i = REGET_GROW_SIZE; i > 0; i--) {
281 data[i - 1] = i % UINT8_MAX;
282 }
283
284 data = isc_mem_reallocate(mctx, data, REGET_SHRINK_SIZE);
285 assert_non_null(data);
286
287 for (size_t i = REGET_SHRINK_SIZE; i > 0; i--) {
288 assert_int_equal(data[i - 1], i % UINT8_MAX);
289 }
290
291 isc_mem_free(mctx, data);
292 }
293
294 ISC_RUN_TEST_IMPL(isc_mem_overmem) {
295 isc_mem_t *omctx = NULL;
296 isc_mem_create(&omctx);
297 assert_non_null(omctx);
298
299 isc_mem_setwater(omctx, 1024, 512);
300
301 /* inuse < lo_water */
302 void *data1 = isc_mem_allocate(omctx, 256);
303 assert_false(isc_mem_isovermem(omctx));
304
305 /* lo_water < inuse < hi_water */
306 void *data2 = isc_mem_allocate(omctx, 512);
307 assert_false(isc_mem_isovermem(omctx));
308
309 /* hi_water < inuse */
310 void *data3 = isc_mem_allocate(omctx, 512);
311 assert_true(isc_mem_isovermem(omctx));
312
313 /* lo_water < inuse < hi_water */
314 isc_mem_free(omctx, data2);
315 assert_true(isc_mem_isovermem(omctx));
316
317 /* inuse < lo_water */
318 isc_mem_free(omctx, data3);
319 assert_false(isc_mem_isovermem(omctx));
320
321 /* inuse == 0 */
322 isc_mem_free(omctx, data1);
323 assert_false(isc_mem_isovermem(omctx));
324
325 isc_mem_destroy(&omctx);
326 }
327
328 #if ISC_MEM_TRACKLINES
329
330 /* test mem with no flags */
331 ISC_RUN_TEST_IMPL(isc_mem_noflags) {
332 isc_result_t result;
333 isc_mem_t *mctx2 = NULL;
334 char buf[4096], *p;
335 FILE *f;
336 void *ptr;
337
338 result = isc_stdio_open("mem.output", "w", &f);
339 assert_int_equal(result, ISC_R_SUCCESS);
340
341 isc_mem_debugging = 0;
342 isc_mem_create(&mctx2);
343 ptr = isc_mem_get(mctx2, 2048);
344 assert_non_null(ptr);
345 isc__mem_printactive(mctx2, f);
346 isc_mem_put(mctx2, ptr, 2048);
347 isc_mem_destroy(&mctx2);
348 isc_mem_debugging = ISC_MEM_DEBUGRECORD;
349 isc_stdio_close(f);
350
351 memset(buf, 0, sizeof(buf));
352 result = isc_stdio_open("mem.output", "r", &f);
353 assert_int_equal(result, ISC_R_SUCCESS);
354 result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
355 assert_int_equal(result, ISC_R_EOF);
356 isc_stdio_close(f);
357 isc_file_remove("mem.output");
358
359 buf[sizeof(buf) - 1] = 0;
360
361 p = strchr(buf, '\n');
362 assert_null(p);
363 }
364
365 /* test mem with record flag */
366 ISC_RUN_TEST_IMPL(isc_mem_recordflag) {
367 isc_result_t result;
368 isc_mem_t *mctx2 = NULL;
369 char buf[4096], *p;
370 FILE *f;
371 void *ptr;
372
373 result = isc_stdio_open("mem.output", "w", &f);
374 assert_int_equal(result, ISC_R_SUCCESS);
375
376 isc_mem_create(&mctx2);
377 ptr = isc_mem_get(mctx2, 2048);
378 assert_non_null(ptr);
379 isc__mem_printactive(mctx2, f);
380 isc_mem_put(mctx2, ptr, 2048);
381 isc_mem_destroy(&mctx2);
382 isc_stdio_close(f);
383
384 memset(buf, 0, sizeof(buf));
385 result = isc_stdio_open("mem.output", "r", &f);
386 assert_int_equal(result, ISC_R_SUCCESS);
387 result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
388 assert_int_equal(result, ISC_R_EOF);
389 isc_stdio_close(f);
390 isc_file_remove("mem.output");
391
392 buf[sizeof(buf) - 1] = 0;
393
394 p = strchr(buf, '\n');
395 assert_non_null(p);
396 assert_in_range(p, 0, buf + sizeof(buf) - 3);
397 assert_memory_equal(p + 2, "ptr ", 4);
398 p = strchr(p + 1, '\n');
399 assert_non_null(p);
400 assert_int_equal(strlen(p), 1);
401 }
402
403 /* test mem with trace flag */
404 ISC_RUN_TEST_IMPL(isc_mem_traceflag) {
405 isc_result_t result;
406 isc_mem_t *mctx2 = NULL;
407 char buf[4096], *p;
408 FILE *f;
409 void *ptr;
410
411 /* redirect stderr so we can check trace output */
412 f = freopen("mem.output", "w", stderr);
413 assert_non_null(f);
414
415 isc_mem_debugging = ISC_MEM_DEBUGRECORD | ISC_MEM_DEBUGTRACE;
416 isc_mem_create(&mctx2);
417 ptr = isc_mem_get(mctx2, 2048);
418 assert_non_null(ptr);
419 isc__mem_printactive(mctx2, f);
420 isc_mem_put(mctx2, ptr, 2048);
421 isc_mem_destroy(&mctx2);
422 isc_mem_debugging = ISC_MEM_DEBUGRECORD;
423 isc_stdio_close(f);
424
425 memset(buf, 0, sizeof(buf));
426 result = isc_stdio_open("mem.output", "r", &f);
427 assert_int_equal(result, ISC_R_SUCCESS);
428 result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
429 assert_int_equal(result, ISC_R_EOF);
430 isc_stdio_close(f);
431 isc_file_remove("mem.output");
432
433 /* return stderr to TTY so we can see errors */
434 f = freopen("/dev/tty", "w", stderr);
435
436 buf[sizeof(buf) - 1] = 0;
437
438 assert_memory_equal(buf, "create ", 6);
439 p = strchr(buf, '\n');
440 assert_non_null(p);
441
442 assert_memory_equal(p + 1, "add ", 4);
443 p = strchr(p + 1, '\n');
444 assert_non_null(p);
445 p = strchr(p + 1, '\n');
446 assert_non_null(p);
447 assert_in_range(p, 0, buf + sizeof(buf) - 3);
448 assert_memory_equal(p + 2, "ptr ", 4);
449 p = strchr(p + 1, '\n');
450 assert_non_null(p);
451 assert_memory_equal(p + 1, "del ", 4);
452 }
453 #endif /* if ISC_MEM_TRACKLINES */
454
455 #if !defined(__SANITIZE_THREAD__)
456
457 #define ITERS 512
458 #define NUM_ITEMS 1024 /* 768 */
459 #define ITEM_SIZE 65534
460
461 static atomic_size_t mem_size;
462
463 static void *
464 mem_thread(void *arg) {
465 isc_mem_t *mctx2 = (isc_mem_t *)arg;
466 void *items[NUM_ITEMS];
467 size_t size = atomic_load(&mem_size);
468 while (!atomic_compare_exchange_weak(&mem_size, &size, size / 2)) {
469 ;
470 }
471
472 for (int i = 0; i < ITERS; i++) {
473 for (int j = 0; j < NUM_ITEMS; j++) {
474 items[j] = isc_mem_get(mctx2, size);
475 }
476 for (int j = 0; j < NUM_ITEMS; j++) {
477 isc_mem_put(mctx2, items[j], size);
478 }
479 }
480
481 return NULL;
482 }
483
484 ISC_RUN_TEST_IMPL(isc_mem_benchmark) {
485 int nthreads = ISC_MAX(ISC_MIN(isc_os_ncpus(), 32), 1);
486 isc_thread_t threads[32];
487 isc_time_t ts1, ts2;
488 double t;
489
490 atomic_init(&mem_size, ITEM_SIZE);
491
492 ts1 = isc_time_now();
493
494 for (int i = 0; i < nthreads; i++) {
495 isc_thread_create(mem_thread, mctx, &threads[i]);
496 }
497 for (int i = 0; i < nthreads; i++) {
498 isc_thread_join(threads[i], NULL);
499 }
500
501 ts2 = isc_time_now();
502
503 t = isc_time_microdiff(&ts2, &ts1);
504
505 printf("[ TIME ] isc_mem_benchmark: "
506 "%d isc_mem_{get,put} calls, %f seconds, %f "
507 "calls/second\n",
508 nthreads * ITERS * NUM_ITEMS, t / 1000000.0,
509 (nthreads * ITERS * NUM_ITEMS) / (t / 1000000.0));
510 }
511
512 #endif /* __SANITIZE_THREAD */
513
514 ISC_TEST_LIST_START
515
516 ISC_TEST_ENTRY(isc_mem_get)
517 ISC_TEST_ENTRY(isc_mem_cget_zero)
518 ISC_TEST_ENTRY(isc_mem_callocate_zero)
519 ISC_TEST_ENTRY(isc_mem_inuse)
520 ISC_TEST_ENTRY(isc_mem_zeroget)
521 ISC_TEST_ENTRY(isc_mem_reget)
522 ISC_TEST_ENTRY(isc_mem_reallocate)
523 ISC_TEST_ENTRY(isc_mem_overmem)
524
525 #if ISC_MEM_TRACKLINES
526 ISC_TEST_ENTRY(isc_mem_noflags)
527 ISC_TEST_ENTRY(isc_mem_recordflag)
528 /*
529 * traceflag_test closes stderr, which causes weird
530 * side effects for any next test trying to use libuv.
531 * This test has to be the last one to avoid problems.
532 */
533 ISC_TEST_ENTRY(isc_mem_traceflag)
534 #endif /* if ISC_MEM_TRACKLINES */
535 #if !defined(__SANITIZE_THREAD__)
536 ISC_TEST_ENTRY(isc_mem_benchmark)
537 #endif /* __SANITIZE_THREAD__ */
538
539 ISC_TEST_LIST_END
540
541 ISC_TEST_MAIN