mem_test.c 9.71 KB
Newer Older
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * 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 http://mozilla.org/MPL/2.0/.
7 8 9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
10 11 12 13 14 15 16 17 18 19 20
 */

#include <config.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include <atf-c.h>

#include "isctest.h"

21
#include <isc/file.h>
22
#include <isc/mem.h>
23
#include <isc/print.h>
24
#include <isc/result.h>
25
#include <isc/stdio.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

static void *
default_memalloc(void *arg, size_t size) {
	UNUSED(arg);
	if (size == 0U)
		size = 1;
	return (malloc(size));
}

static void
default_memfree(void *arg, void *ptr) {
	UNUSED(arg);
	free(ptr);
}

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
ATF_TC(isc_mem);
ATF_TC_HEAD(isc_mem, tc) {
	atf_tc_set_md_var(tc, "descr", "general memory system tests");
}

#define	MP1_FREEMAX	10
#define	MP1_FILLCNT	10
#define	MP1_MAXALLOC	30

#define	MP2_FREEMAX	25
#define	MP2_FILLCNT	25

ATF_TC_BODY(isc_mem, tc) {
	isc_result_t result;
	void *items1[50];
	void *items2[50];
	void *tmp;
	isc_mem_t *localmctx = NULL;
	isc_mempool_t *mp1 = NULL, *mp2 = NULL;
	unsigned int i, j;
	int rval;

	result = isc_test_begin(NULL, ISC_TRUE);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_mem_create(0, 0, &localmctx);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_mempool_create(localmctx, 24, &mp1);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_mempool_create(localmctx, 31, &mp2);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	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);
		ATF_CHECK(items1[i] != NULL);
	}

	/*
	 * Try to allocate one more.  This should fail.
	 */
	tmp = isc_mempool_get(mp1);
	ATF_CHECK_EQ(tmp, NULL);

	/*
	 * 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;
	}

	rval = isc_mempool_getfreecount(mp1);
	ATF_CHECK_EQ(rval, 10);

	rval = isc_mempool_getallocated(mp1);
	ATF_CHECK_EQ(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);
			ATF_CHECK(items2[i] != NULL);
		}
		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_mem_destroy(&localmctx);

	isc_test_end();
}

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
ATF_TC(isc_mem_total);
ATF_TC_HEAD(isc_mem_total, tc) {
	atf_tc_set_md_var(tc, "descr", "test TotalUse calculation");
}

ATF_TC_BODY(isc_mem_total, tc) {
	isc_result_t result;
	isc_mem_t *mctx2 = NULL;
	size_t before, after;
	ssize_t diff;
	int i;

	result = isc_test_begin(NULL, ISC_TRUE);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	/* Local alloc, free */
	mctx2 = NULL;
	result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
				  NULL, &mctx2, 0);
	if (result != ISC_R_SUCCESS)
		goto out;

	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;

	printf("total_before=%lu, total_after=%lu, total_diff=%lu\n",
178 179
	       (unsigned long)before, (unsigned long)after,
	       (unsigned long)diff);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	/* 2048 +8 bytes extra for size_info */
	ATF_CHECK_EQ(diff, (2048 + 8) * 100000);

	/* ISC_MEMFLAG_INTERNAL */

	before = isc_mem_total(mctx);

	for (i = 0; i < 100000; i++) {
		void *ptr;

		ptr = isc_mem_allocate(mctx, 2048);
		isc_mem_free(mctx, ptr);
	}

	after = isc_mem_total(mctx);
	diff = after - before;

	printf("total_before=%lu, total_after=%lu, total_diff=%lu\n",
198 199
	       (unsigned long)before, (unsigned long)after,
	       (unsigned long)diff);
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
	/* 2048 +8 bytes extra for size_info */
	ATF_CHECK_EQ(diff, (2048 + 8) * 100000);

 out:
	if (mctx2 != NULL)
		isc_mem_destroy(&mctx2);

	isc_test_end();
}

ATF_TC(isc_mem_inuse);
ATF_TC_HEAD(isc_mem_inuse, tc) {
	atf_tc_set_md_var(tc, "descr", "test InUse calculation");
}

ATF_TC_BODY(isc_mem_inuse, tc) {
	isc_result_t result;
	isc_mem_t *mctx2 = NULL;
	size_t before, during, after;
	ssize_t diff;
	void *ptr;

	result = isc_test_begin(NULL, ISC_TRUE);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	mctx2 = NULL;
	result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
				  NULL, &mctx2, 0);
	if (result != ISC_R_SUCCESS)
		goto out;

	before = isc_mem_inuse(mctx2);
	ptr = isc_mem_allocate(mctx2, 1024000);
	during = isc_mem_inuse(mctx2);
	isc_mem_free(mctx2, ptr);
	after = isc_mem_inuse(mctx2);

	diff = after - before;

	printf("inuse_before=%lu, inuse_during=%lu, inuse_after=%lu\n",
240 241
	       (unsigned long)before, (unsigned long)during,
	       (unsigned long)after);
242 243 244 245 246 247 248 249 250
	ATF_REQUIRE_EQ(diff, 0);

 out:
	if (mctx2 != NULL)
		isc_mem_destroy(&mctx2);

	isc_test_end();
}

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
#if ISC_MEM_TRACKLINES
ATF_TC(isc_mem_noflags);
ATF_TC_HEAD(isc_mem_noflags, tc) {
	atf_tc_set_md_var(tc, "descr", "test mem with no flags");
}

ATF_TC_BODY(isc_mem_noflags, tc) {
	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);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_test_begin(NULL, ISC_TRUE);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
				  NULL, &mctx2, 0);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	isc_mem_debugging = 0;
	ptr = isc_mem_get(mctx2, 2048);
	ATF_CHECK(ptr != NULL);
	isc__mem_printactive(mctx2, f);
	isc_mem_put(mctx2, ptr, 2048);
	isc_mem_destroy(&mctx2);
	isc_stdio_close(f);

281
	memset(buf, 0, sizeof(buf));
282 283
	result = isc_stdio_open("mem.output", "r", &f);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
284
	result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
Mark Andrews's avatar
Mark Andrews committed
285
	ATF_REQUIRE_EQ(result, ISC_R_EOF);
286 287 288
	isc_stdio_close(f);
	isc_file_remove("mem.output");

289 290
	buf[sizeof(buf) - 1] = 0;

291
	p = strchr(buf, '\n');
292 293
	ATF_REQUIRE(p != NULL);
	ATF_REQUIRE(p < buf + sizeof(buf) - 2);
294 295
	p += 2;
	q = strchr(p, '\n');
296
	ATF_REQUIRE(q != NULL);
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
	*q = '\0';
	ATF_CHECK_STREQ(p, "None.");

	isc_mem_debugging = ISC_MEM_DEBUGRECORD;
	isc_test_end();
}

ATF_TC(isc_mem_recordflag);
ATF_TC_HEAD(isc_mem_recordflag, tc) {
	atf_tc_set_md_var(tc, "descr", "test mem with record flag");
}

ATF_TC_BODY(isc_mem_recordflag, tc) {
	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);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_test_begin(NULL, ISC_FALSE);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
				  NULL, &mctx2, 0);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	ptr = isc_mem_get(mctx2, 2048);
	ATF_CHECK(ptr != NULL);
	isc__mem_printactive(mctx2, f);
	isc_mem_put(mctx2, ptr, 2048);
	isc_mem_destroy(&mctx2);
	isc_stdio_close(f);

332
	memset(buf, 0, sizeof(buf));
333 334
	result = isc_stdio_open("mem.output", "r", &f);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
335
	result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
Mark Andrews's avatar
Mark Andrews committed
336
	ATF_REQUIRE_EQ(result, ISC_R_EOF);
337 338 339
	isc_stdio_close(f);
	isc_file_remove("mem.output");

340 341
	buf[sizeof(buf) - 1] = 0;

342
	p = strchr(buf, '\n');
343 344
	ATF_REQUIRE(p != NULL);
	ATF_REQUIRE(p < buf + sizeof(buf) - 2);
345 346
	ATF_CHECK(strncmp(p + 2, "ptr ", 4) == 0);
	p = strchr(p + 1, '\n');
347
	ATF_REQUIRE(p != NULL);
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
	ATF_CHECK(strlen(p) == 1);

	isc_test_end();
}

ATF_TC(isc_mem_traceflag);
ATF_TC_HEAD(isc_mem_traceflag, tc) {
	atf_tc_set_md_var(tc, "descr", "test mem with trace flag");
}

ATF_TC_BODY(isc_mem_traceflag, tc) {
	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);
	ATF_REQUIRE(f != NULL);

	result = isc_test_begin(NULL, ISC_TRUE);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
				  NULL, &mctx2, 0);
	isc_mem_debugging = ISC_MEM_DEBUGTRACE;
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	ptr = isc_mem_get(mctx2, 2048);
	ATF_CHECK(ptr != NULL);
	isc__mem_printactive(mctx2, f);
	isc_mem_put(mctx2, ptr, 2048);
	isc_mem_destroy(&mctx2);
	isc_stdio_close(f);

383
	memset(buf, 0, sizeof(buf));
384 385
	result = isc_stdio_open("mem.output", "r", &f);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
386
	result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
Mark Andrews's avatar
Mark Andrews committed
387
	ATF_REQUIRE_EQ(result, ISC_R_EOF);
388 389 390 391 392 393
	isc_stdio_close(f);
	isc_file_remove("mem.output");

	/* return stderr to TTY so we can see errors */
	f = freopen("/dev/tty", "w", stderr);

394 395
	buf[sizeof(buf) - 1] = 0;

396 397
	ATF_CHECK(strncmp(buf, "add ", 4) == 0);
	p = strchr(buf, '\n');
398
	ATF_REQUIRE(p != NULL);
399
	p = strchr(p + 1, '\n');
400 401
	ATF_REQUIRE(p != NULL);
	ATF_REQUIRE(p < buf + sizeof(buf) - 2);
402 403
	ATF_CHECK(strncmp(p + 2, "ptr ", 4) == 0);
	p = strchr(p + 1, '\n');
404
	ATF_REQUIRE(p != NULL);
405 406 407 408 409 410 411
	ATF_CHECK(strncmp(p + 1, "del ", 4) == 0);

	isc_mem_debugging = ISC_MEM_DEBUGRECORD;
	isc_test_end();
}
#endif

412 413 414 415
/*
 * Main
 */
ATF_TP_ADD_TCS(tp) {
416
	ATF_TP_ADD_TC(tp, isc_mem);
417 418
	ATF_TP_ADD_TC(tp, isc_mem_total);
	ATF_TP_ADD_TC(tp, isc_mem_inuse);
419 420 421 422 423
#if ISC_MEM_TRACKLINES
	ATF_TP_ADD_TC(tp, isc_mem_noflags);
	ATF_TP_ADD_TC(tp, isc_mem_recordflag);
	ATF_TP_ADD_TC(tp, isc_mem_traceflag);
#endif
424 425 426

	return (atf_no_error());
}