2015-01-22 09:44:24 -08:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2015-01-22 09:44:24 -08:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* 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
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2015-01-22 09:44:24 -08:00
|
|
|
*/
|
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
Include <sched.h> where necessary for musl libc
All unit tests define the UNIT_TESTING macro, which causes <cmocka.h> to
replace malloc(), calloc(), realloc(), and free() with its own functions
tracking memory allocations. In order for this not to break
compilation, the system header declaring the prototypes for these
standard functions must be included before <cmocka.h>.
Normally, these prototypes are only present in <stdlib.h>, so we make
sure it is included before <cmocka.h>. However, musl libc also defines
the prototypes for calloc() and free() in <sched.h>, which is included
by <pthread.h>, which is included e.g. by <isc/mutex.h>. Thus, unit
tests including "dnstest.h" (which includes <isc/mem.h>, which includes
<isc/mutex.h>) after <cmocka.h> will not compile with musl libc as for
these programs, <sched.h> will be included after <cmocka.h>.
Always including <cmocka.h> after all other header files is not a
feasible solution as that causes the mock assertion macros defined in
<isc/util.h> to mangle the contents of <cmocka.h>, thus breaking
compilation. We cannot really use the __noreturn__ or analyzer_noreturn
attributes with cmocka assertion functions because they do return if the
tested condition is true. The problem is that what BIND unit tests do
is incompatible with Clang Static Analyzer's assumptions: since we use
cmocka, our custom assertion handlers are present in a shared library
(i.e. it is the cmocka library that checks the assertion condition, not
a macro in unit test code). Redefining cmocka's assertion macros in
<isc/util.h> is an ugly hack to overcome that problem - unfortunately,
this is the only way we can think of to make Clang Static Analyzer
properly process unit test code. Giving up on Clang Static Analyzer
being able to properly process unit test code is not a satisfactory
solution.
Undefining _GNU_SOURCE for unit test code could work around the problem
(musl libc's <sched.h> only defines the prototypes for calloc() and
free() when _GNU_SOURCE is defined), but doing that could introduce
discrepancies for unit tests including entire *.c files, so it is also
not a good solution.
All in all, including <sched.h> before <cmocka.h> for all affected unit
tests seems to be the most benign way of working around this musl libc
quirk. While quite an ugly solution, it achieves our goals here, which
are to keep the benefit of proper static analysis of unit test code and
to fix compilation against musl libc.
2019-07-30 21:08:40 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2015-01-22 09:44:24 -08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
2015-01-22 09:44:24 -08:00
|
|
|
|
2021-02-05 10:25:07 +01:00
|
|
|
#include <isc/atomic.h>
|
2017-08-24 10:58:20 +05:30
|
|
|
#include <isc/file.h>
|
2015-01-22 09:44:24 -08:00
|
|
|
#include <isc/mem.h>
|
2019-05-11 02:09:50 +07:00
|
|
|
#include <isc/mutex.h>
|
|
|
|
#include <isc/os.h>
|
2015-05-23 14:21:51 +02:00
|
|
|
#include <isc/print.h>
|
2015-01-22 09:44:24 -08:00
|
|
|
#include <isc/result.h>
|
2017-08-24 10:58:20 +05:30
|
|
|
#include <isc/stdio.h>
|
2019-05-11 02:09:50 +07:00
|
|
|
#include <isc/thread.h>
|
|
|
|
#include <isc/time.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <isc/util.h>
|
2015-01-22 09:44:24 -08:00
|
|
|
|
2018-10-03 19:04:46 -07:00
|
|
|
#include "../mem_p.h"
|
|
|
|
#include "isctest.h"
|
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_setup(void **state) {
|
2018-10-25 15:40:27 +08:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = isc_test_begin(NULL, true, 0);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_teardown(void **state) {
|
2018-10-25 15:40:27 +08:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
isc_test_end();
|
|
|
|
|
|
|
|
return (0);
|
2018-02-27 10:20:28 -08:00
|
|
|
}
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define MP1_FREEMAX 10
|
|
|
|
#define MP1_FILLCNT 10
|
2020-02-12 13:59:18 +01:00
|
|
|
#define MP1_MAXALLOC 30
|
2018-02-27 10:20:28 -08:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define MP2_FREEMAX 25
|
|
|
|
#define MP2_FILLCNT 25
|
2018-02-27 10:20:28 -08:00
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
/* general memory system tests */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_test(void **state) {
|
|
|
|
void *items1[50];
|
|
|
|
void *items2[50];
|
|
|
|
void *tmp;
|
2018-02-27 10:20:28 -08:00
|
|
|
isc_mempool_t *mp1 = NULL, *mp2 = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int i, j;
|
|
|
|
int rval;
|
2018-02-27 10:20:28 -08:00
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
UNUSED(state);
|
2018-02-27 10:20:28 -08:00
|
|
|
|
2020-02-02 08:50:41 +01:00
|
|
|
isc_mempool_create(test_mctx, 24, &mp1);
|
|
|
|
isc_mempool_create(test_mctx, 31, &mp2);
|
2018-02-27 10:20:28 -08:00
|
|
|
|
|
|
|
isc_mempool_setfreemax(mp1, MP1_FREEMAX);
|
|
|
|
isc_mempool_setfillcount(mp1, MP1_FILLCNT);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate MP1_MAXALLOC items from the pool. This is our max.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < MP1_MAXALLOC; i++) {
|
|
|
|
items1[i] = isc_mempool_get(mp1);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(items1[i]);
|
2018-02-27 10:20:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2021-02-25 11:08:34 +01:00
|
|
|
#if !__SANITIZE_ADDRESS__
|
2018-02-27 10:20:28 -08:00
|
|
|
rval = isc_mempool_getfreecount(mp1);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_int_equal(rval, 10);
|
2021-02-25 11:08:34 +01:00
|
|
|
#endif /* !__SANITIZE_ADDRESS__ */
|
2018-02-27 10:20:28 -08:00
|
|
|
|
|
|
|
rval = isc_mempool_getallocated(mp1);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_int_equal(rval, 19);
|
2018-02-27 10:20:28 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(items2[i]);
|
2018-02-27 10:20:28 -08:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
2020-02-02 08:50:41 +01:00
|
|
|
isc_mempool_create(test_mctx, 2, &mp1);
|
2018-08-14 17:13:20 +10:00
|
|
|
|
|
|
|
tmp = isc_mempool_get(mp1);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(tmp);
|
2018-08-14 17:13:20 +10:00
|
|
|
|
|
|
|
isc_mempool_put(mp1, tmp);
|
|
|
|
|
|
|
|
isc_mempool_destroy(&mp1);
|
2015-01-22 09:44:24 -08:00
|
|
|
}
|
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
/* test TotalUse calculation */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_total_test(void **state) {
|
2015-01-22 09:44:24 -08:00
|
|
|
isc_mem_t *mctx2 = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t before, after;
|
|
|
|
ssize_t diff;
|
|
|
|
int i;
|
2015-01-22 09:44:24 -08:00
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
UNUSED(state);
|
2015-01-22 09:44:24 -08:00
|
|
|
|
|
|
|
/* Local alloc, free */
|
|
|
|
mctx2 = NULL;
|
2019-09-06 11:31:15 +02:00
|
|
|
isc_mem_create(&mctx2);
|
2015-01-22 09:44:24 -08:00
|
|
|
|
|
|
|
before = isc_mem_total(mctx2);
|
|
|
|
|
|
|
|
for (i = 0; i < 100000; i++) {
|
|
|
|
void *ptr;
|
|
|
|
|
2021-07-09 11:44:44 +02:00
|
|
|
ptr = isc_mem_get(mctx2, 2048);
|
|
|
|
isc_mem_put(mctx2, ptr, 2048);
|
2015-01-22 09:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
after = isc_mem_total(mctx2);
|
|
|
|
diff = after - before;
|
|
|
|
|
2021-05-11 14:00:12 +02:00
|
|
|
assert_int_equal(diff, (2048) * 100000);
|
2015-01-22 09:44:24 -08:00
|
|
|
|
|
|
|
/* ISC_MEMFLAG_INTERNAL */
|
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
before = isc_mem_total(test_mctx);
|
2015-01-22 09:44:24 -08:00
|
|
|
|
|
|
|
for (i = 0; i < 100000; i++) {
|
|
|
|
void *ptr;
|
|
|
|
|
2021-07-09 11:44:44 +02:00
|
|
|
ptr = isc_mem_get(test_mctx, 2048);
|
|
|
|
isc_mem_put(test_mctx, ptr, 2048);
|
2015-01-22 09:44:24 -08:00
|
|
|
}
|
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
after = isc_mem_total(test_mctx);
|
2015-01-22 09:44:24 -08:00
|
|
|
diff = after - before;
|
|
|
|
|
2021-05-11 14:00:12 +02:00
|
|
|
assert_int_equal(diff, (2048) * 100000);
|
2015-01-22 09:44:24 -08:00
|
|
|
|
2019-09-05 18:40:57 +02:00
|
|
|
isc_mem_destroy(&mctx2);
|
2015-01-22 09:44:24 -08:00
|
|
|
}
|
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
/* test InUse calculation */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_inuse_test(void **state) {
|
2015-01-22 09:44:24 -08:00
|
|
|
isc_mem_t *mctx2 = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t before, after;
|
|
|
|
ssize_t diff;
|
|
|
|
void *ptr;
|
2015-01-22 09:44:24 -08:00
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
UNUSED(state);
|
2015-01-22 09:44:24 -08:00
|
|
|
|
|
|
|
mctx2 = NULL;
|
2019-09-06 11:31:15 +02:00
|
|
|
isc_mem_create(&mctx2);
|
2015-01-22 09:44:24 -08:00
|
|
|
|
|
|
|
before = isc_mem_inuse(mctx2);
|
|
|
|
ptr = isc_mem_allocate(mctx2, 1024000);
|
|
|
|
isc_mem_free(mctx2, ptr);
|
|
|
|
after = isc_mem_inuse(mctx2);
|
|
|
|
|
|
|
|
diff = after - before;
|
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_int_equal(diff, 0);
|
2015-01-22 09:44:24 -08:00
|
|
|
|
2019-09-05 18:40:57 +02:00
|
|
|
isc_mem_destroy(&mctx2);
|
2015-01-22 09:44:24 -08:00
|
|
|
}
|
|
|
|
|
2021-09-22 18:48:03 +02:00
|
|
|
#define REGET_INIT_SIZE 1024
|
|
|
|
#define REGET_GROW_SIZE 2048
|
|
|
|
#define REGET_SHRINK_SIZE 512
|
|
|
|
|
|
|
|
static void
|
|
|
|
isc_mem_reget_test(void **state) {
|
|
|
|
uint8_t *data = isc_mem_get(test_mctx, REGET_INIT_SIZE);
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < REGET_INIT_SIZE; i++) {
|
|
|
|
data[i] = i % UINT8_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = isc_mem_reget(test_mctx, data, REGET_INIT_SIZE, REGET_GROW_SIZE);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < REGET_INIT_SIZE; i++) {
|
|
|
|
assert_int_equal(data[i], i % UINT8_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = REGET_GROW_SIZE; i > 0; i--) {
|
|
|
|
data[i - 1] = i % UINT8_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = isc_mem_reget(test_mctx, data, REGET_GROW_SIZE,
|
|
|
|
REGET_SHRINK_SIZE);
|
|
|
|
|
|
|
|
for (size_t i = REGET_SHRINK_SIZE; i > 0; i--) {
|
|
|
|
assert_int_equal(data[i - 1], i % UINT8_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_mem_put(test_mctx, data, REGET_SHRINK_SIZE);
|
|
|
|
}
|
|
|
|
|
2017-08-24 10:58:20 +05:30
|
|
|
#if ISC_MEM_TRACKLINES
|
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
/* test mem with no flags */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_noflags_test(void **state) {
|
2017-08-24 10:58:20 +05:30
|
|
|
isc_result_t result;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_t *mctx2 = NULL;
|
|
|
|
char buf[4096], *p, *q;
|
|
|
|
FILE *f;
|
|
|
|
void *ptr;
|
2017-08-24 10:58:20 +05:30
|
|
|
|
|
|
|
result = isc_stdio_open("mem.output", "w", &f);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-08-24 10:58:20 +05:30
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
UNUSED(state);
|
2017-08-24 10:58:20 +05:30
|
|
|
|
2019-09-06 11:31:15 +02:00
|
|
|
isc_mem_create(&mctx2);
|
2017-08-24 10:58:20 +05:30
|
|
|
isc_mem_debugging = 0;
|
|
|
|
ptr = isc_mem_get(mctx2, 2048);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(ptr);
|
2018-10-03 19:04:46 -07:00
|
|
|
isc__mem_printactive(mctx2, f);
|
2017-08-24 10:58:20 +05:30
|
|
|
isc_mem_put(mctx2, ptr, 2048);
|
|
|
|
isc_mem_destroy(&mctx2);
|
|
|
|
isc_stdio_close(f);
|
|
|
|
|
2017-10-19 13:08:31 +11:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2017-08-24 10:58:20 +05:30
|
|
|
result = isc_stdio_open("mem.output", "r", &f);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-10-19 13:08:31 +11:00
|
|
|
result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_int_equal(result, ISC_R_EOF);
|
2017-08-24 10:58:20 +05:30
|
|
|
isc_stdio_close(f);
|
|
|
|
isc_file_remove("mem.output");
|
|
|
|
|
2017-10-19 13:08:31 +11:00
|
|
|
buf[sizeof(buf) - 1] = 0;
|
|
|
|
|
2017-08-24 10:58:20 +05:30
|
|
|
p = strchr(buf, '\n');
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(p);
|
|
|
|
assert_in_range(p, 0, buf + sizeof(buf) - 3);
|
2017-08-24 10:58:20 +05:30
|
|
|
p += 2;
|
|
|
|
q = strchr(p, '\n');
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(q);
|
2017-08-24 10:58:20 +05:30
|
|
|
*q = '\0';
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_string_equal(p, "None.");
|
2017-08-24 10:58:20 +05:30
|
|
|
|
|
|
|
isc_mem_debugging = ISC_MEM_DEBUGRECORD;
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
/* test mem with record flag */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_recordflag_test(void **state) {
|
2017-08-24 10:58:20 +05:30
|
|
|
isc_result_t result;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_t *mctx2 = NULL;
|
|
|
|
char buf[4096], *p;
|
|
|
|
FILE *f;
|
|
|
|
void *ptr;
|
2017-08-24 10:58:20 +05:30
|
|
|
|
|
|
|
result = isc_stdio_open("mem.output", "w", &f);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-08-24 10:58:20 +05:30
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
UNUSED(state);
|
2017-08-24 10:58:20 +05:30
|
|
|
|
2019-09-06 11:31:15 +02:00
|
|
|
isc_mem_create(&mctx2);
|
2017-08-24 10:58:20 +05:30
|
|
|
ptr = isc_mem_get(mctx2, 2048);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(ptr);
|
2018-10-03 19:04:46 -07:00
|
|
|
isc__mem_printactive(mctx2, f);
|
2017-08-24 10:58:20 +05:30
|
|
|
isc_mem_put(mctx2, ptr, 2048);
|
|
|
|
isc_mem_destroy(&mctx2);
|
|
|
|
isc_stdio_close(f);
|
|
|
|
|
2017-08-30 19:02:52 -07:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2017-08-24 10:58:20 +05:30
|
|
|
result = isc_stdio_open("mem.output", "r", &f);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-10-19 13:08:31 +11:00
|
|
|
result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_int_equal(result, ISC_R_EOF);
|
2017-08-24 10:58:20 +05:30
|
|
|
isc_stdio_close(f);
|
|
|
|
isc_file_remove("mem.output");
|
|
|
|
|
2017-10-19 13:08:31 +11:00
|
|
|
buf[sizeof(buf) - 1] = 0;
|
|
|
|
|
2017-08-24 10:58:20 +05:30
|
|
|
p = strchr(buf, '\n');
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(p);
|
|
|
|
assert_in_range(p, 0, buf + sizeof(buf) - 3);
|
|
|
|
assert_memory_equal(p + 2, "ptr ", 4);
|
2017-08-24 10:58:20 +05:30
|
|
|
p = strchr(p + 1, '\n');
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(p);
|
|
|
|
assert_int_equal(strlen(p), 1);
|
2017-08-24 10:58:20 +05:30
|
|
|
}
|
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
/* test mem with trace flag */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_traceflag_test(void **state) {
|
2017-08-24 10:58:20 +05:30
|
|
|
isc_result_t result;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_t *mctx2 = NULL;
|
|
|
|
char buf[4096], *p;
|
|
|
|
FILE *f;
|
|
|
|
void *ptr;
|
2017-08-24 10:58:20 +05:30
|
|
|
|
|
|
|
/* redirect stderr so we can check trace output */
|
|
|
|
f = freopen("mem.output", "w", stderr);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(f);
|
2017-08-24 10:58:20 +05:30
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
UNUSED(state);
|
2017-08-24 10:58:20 +05:30
|
|
|
|
2019-09-06 11:31:15 +02:00
|
|
|
isc_mem_create(&mctx2);
|
2017-08-24 10:58:20 +05:30
|
|
|
isc_mem_debugging = ISC_MEM_DEBUGTRACE;
|
|
|
|
ptr = isc_mem_get(mctx2, 2048);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(ptr);
|
2018-10-03 19:04:46 -07:00
|
|
|
isc__mem_printactive(mctx2, f);
|
2017-08-24 10:58:20 +05:30
|
|
|
isc_mem_put(mctx2, ptr, 2048);
|
|
|
|
isc_mem_destroy(&mctx2);
|
|
|
|
isc_stdio_close(f);
|
|
|
|
|
2017-10-19 13:08:31 +11:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2017-08-24 10:58:20 +05:30
|
|
|
result = isc_stdio_open("mem.output", "r", &f);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-10-19 13:08:31 +11:00
|
|
|
result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_int_equal(result, ISC_R_EOF);
|
2017-08-24 10:58:20 +05:30
|
|
|
isc_stdio_close(f);
|
|
|
|
isc_file_remove("mem.output");
|
|
|
|
|
|
|
|
/* return stderr to TTY so we can see errors */
|
|
|
|
f = freopen("/dev/tty", "w", stderr);
|
|
|
|
|
2017-10-19 13:08:31 +11:00
|
|
|
buf[sizeof(buf) - 1] = 0;
|
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_memory_equal(buf, "add ", 4);
|
2017-08-24 10:58:20 +05:30
|
|
|
p = strchr(buf, '\n');
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(p);
|
2017-08-24 10:58:20 +05:30
|
|
|
p = strchr(p + 1, '\n');
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(p);
|
|
|
|
assert_in_range(p, 0, buf + sizeof(buf) - 3);
|
|
|
|
assert_memory_equal(p + 2, "ptr ", 4);
|
2017-08-24 10:58:20 +05:30
|
|
|
p = strchr(p + 1, '\n');
|
2018-10-25 15:40:27 +08:00
|
|
|
assert_non_null(p);
|
|
|
|
assert_memory_equal(p + 1, "del ", 4);
|
2017-08-24 10:58:20 +05:30
|
|
|
|
|
|
|
isc_mem_debugging = ISC_MEM_DEBUGRECORD;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2017-08-24 10:58:20 +05:30
|
|
|
|
2019-07-04 14:25:59 +02:00
|
|
|
#if !defined(__SANITIZE_THREAD__)
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define ITERS 512
|
2020-02-13 21:48:23 +01:00
|
|
|
#define NUM_ITEMS 1024 /* 768 */
|
2019-05-11 02:09:50 +07:00
|
|
|
#define ITEM_SIZE 65534
|
|
|
|
|
2021-02-05 10:25:07 +01:00
|
|
|
static atomic_size_t mem_size;
|
|
|
|
|
2019-12-04 10:41:40 +01:00
|
|
|
static isc_threadresult_t
|
2020-02-13 14:44:37 -08:00
|
|
|
mem_thread(isc_threadarg_t arg) {
|
2021-02-05 10:25:07 +01:00
|
|
|
isc_mem_t *mctx = (isc_mem_t *)arg;
|
2020-02-13 14:44:37 -08:00
|
|
|
void *items[NUM_ITEMS];
|
2021-02-05 10:25:07 +01:00
|
|
|
size_t size = atomic_load(&mem_size);
|
|
|
|
while (!atomic_compare_exchange_weak(&mem_size, &size, size / 2))
|
|
|
|
;
|
2019-05-11 02:09:50 +07:00
|
|
|
|
|
|
|
for (int i = 0; i < ITERS; i++) {
|
|
|
|
for (int j = 0; j < NUM_ITEMS; j++) {
|
2021-02-05 10:25:07 +01:00
|
|
|
items[j] = isc_mem_get(mctx, size);
|
2019-05-11 02:09:50 +07:00
|
|
|
}
|
|
|
|
for (int j = 0; j < NUM_ITEMS; j++) {
|
2021-02-05 10:25:07 +01:00
|
|
|
isc_mem_put(mctx, items[j], size);
|
2019-05-11 02:09:50 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 10:41:40 +01:00
|
|
|
return ((isc_threadresult_t)0);
|
2019-05-11 02:09:50 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_benchmark(void **state) {
|
|
|
|
int nthreads = ISC_MAX(ISC_MIN(isc_os_ncpus(), 32), 1);
|
2019-05-11 02:09:50 +07:00
|
|
|
isc_thread_t threads[32];
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_t ts1, ts2;
|
|
|
|
double t;
|
2019-05-11 02:09:50 +07:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
2021-02-05 10:25:07 +01:00
|
|
|
atomic_init(&mem_size, ITEM_SIZE);
|
|
|
|
|
2019-05-11 02:09:50 +07:00
|
|
|
result = isc_time_now(&ts1);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
for (int i = 0; i < nthreads; i++) {
|
2021-02-05 10:25:07 +01:00
|
|
|
isc_thread_create(mem_thread, test_mctx, &threads[i]);
|
2019-05-11 02:09:50 +07:00
|
|
|
}
|
|
|
|
for (int i = 0; i < nthreads; i++) {
|
2019-07-18 17:47:40 +02:00
|
|
|
isc_thread_join(threads[i], NULL);
|
2019-05-11 02:09:50 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2019-07-04 14:25:59 +02:00
|
|
|
#endif /* __SANITIZE_THREAD */
|
|
|
|
|
2015-01-22 09:44:24 -08:00
|
|
|
/*
|
|
|
|
* Main
|
|
|
|
*/
|
2018-10-25 15:40:27 +08:00
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-25 15:40:27 +08:00
|
|
|
const struct CMUnitTest tests[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
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),
|
2021-09-22 18:48:03 +02:00
|
|
|
cmocka_unit_test_setup_teardown(isc_mem_reget_test, _setup,
|
|
|
|
_teardown),
|
2018-10-25 15:40:27 +08:00
|
|
|
|
2019-11-15 13:30:04 +01:00
|
|
|
#if !defined(__SANITIZE_THREAD__)
|
2020-02-12 13:59:18 +01:00
|
|
|
cmocka_unit_test_setup_teardown(isc_mem_benchmark, _setup,
|
|
|
|
_teardown),
|
2019-11-15 13:30:04 +01:00
|
|
|
#endif /* __SANITIZE_THREAD__ */
|
2017-08-24 10:58:20 +05:30
|
|
|
#if ISC_MEM_TRACKLINES
|
2020-02-12 13:59:18 +01:00
|
|
|
cmocka_unit_test_setup_teardown(isc_mem_noflags_test, _setup,
|
|
|
|
_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(isc_mem_recordflag_test, _setup,
|
|
|
|
_teardown),
|
2019-11-15 13:30:04 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
cmocka_unit_test_setup_teardown(isc_mem_traceflag_test, _setup,
|
|
|
|
_teardown),
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_MEM_TRACKLINES */
|
2018-10-25 15:40:27 +08:00
|
|
|
};
|
2015-01-22 09:44:24 -08:00
|
|
|
|
2018-10-25 15:40:27 +08:00
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
2015-01-22 09:44:24 -08:00
|
|
|
}
|
2018-10-25 15:40:27 +08:00
|
|
|
|
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2020-02-12 13:59:18 +01:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
2021-01-18 19:15:44 +01:00
|
|
|
return (SKIPPED_TEST_EXIT_CODE);
|
2018-10-25 15:40:27 +08:00
|
|
|
}
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_CMOCKA */
|