84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2015, 2016 Internet Systems Consortium, Inc. ("ISC")
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt */
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt#include <config.h>
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt#include <stdlib.h>
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt#include <unistd.h>
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt#include <fcntl.h>
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt#include <atf-c.h>
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt#include "isctest.h"
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt#include <isc/mem.h>
3759f10fc543747668b1ca4b4671f35b0dea8445Francis Dupont#include <isc/print.h>
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt#include <isc/result.h>
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Huntstatic void *
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Huntdefault_memalloc(void *arg, size_t size) {
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt UNUSED(arg);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt if (size == 0U)
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt size = 1;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt return (malloc(size));
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt}
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Huntstatic void
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Huntdefault_memfree(void *arg, void *ptr) {
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt UNUSED(arg);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt free(ptr);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt}
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan HuntATF_TC(isc_mem_total);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan HuntATF_TC_HEAD(isc_mem_total, tc) {
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt atf_tc_set_md_var(tc, "descr", "test TotalUse calculation");
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt}
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan HuntATF_TC_BODY(isc_mem_total, tc) {
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt isc_result_t result;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt isc_mem_t *mctx2 = NULL;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt size_t before, after;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt ssize_t diff;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt int i;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt result = isc_test_begin(NULL, ISC_TRUE);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt /* Local alloc, free */
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt mctx2 = NULL;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt NULL, &mctx2, 0);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt if (result != ISC_R_SUCCESS)
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt goto out;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt before = isc_mem_total(mctx2);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt for (i = 0; i < 100000; i++) {
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt void *ptr;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt ptr = isc_mem_allocate(mctx2, 2048);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt isc_mem_free(mctx2, ptr);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt }
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt after = isc_mem_total(mctx2);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt diff = after - before;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt printf("total_before=%lu, total_after=%lu, total_diff=%lu\n",
d2a50c9ba87a6c57cb310308dfe95fff2f2836e2Mark Andrews (unsigned long)before, (unsigned long)after,
d2a50c9ba87a6c57cb310308dfe95fff2f2836e2Mark Andrews (unsigned long)diff);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt /* 2048 +8 bytes extra for size_info */
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt ATF_CHECK_EQ(diff, (2048 + 8) * 100000);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt /* ISC_MEMFLAG_INTERNAL */
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt before = isc_mem_total(mctx);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt for (i = 0; i < 100000; i++) {
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt void *ptr;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt ptr = isc_mem_allocate(mctx, 2048);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt isc_mem_free(mctx, ptr);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt }
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt after = isc_mem_total(mctx);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt diff = after - before;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt printf("total_before=%lu, total_after=%lu, total_diff=%lu\n",
d2a50c9ba87a6c57cb310308dfe95fff2f2836e2Mark Andrews (unsigned long)before, (unsigned long)after,
d2a50c9ba87a6c57cb310308dfe95fff2f2836e2Mark Andrews (unsigned long)diff);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt /* 2048 +8 bytes extra for size_info */
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt ATF_CHECK_EQ(diff, (2048 + 8) * 100000);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt out:
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt if (mctx2 != NULL)
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt isc_mem_destroy(&mctx2);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt isc_test_end();
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt}
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan HuntATF_TC(isc_mem_inuse);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan HuntATF_TC_HEAD(isc_mem_inuse, tc) {
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt atf_tc_set_md_var(tc, "descr", "test InUse calculation");
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt}
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan HuntATF_TC_BODY(isc_mem_inuse, tc) {
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt isc_result_t result;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt isc_mem_t *mctx2 = NULL;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt size_t before, during, after;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt ssize_t diff;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt void *ptr;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt result = isc_test_begin(NULL, ISC_TRUE);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt mctx2 = NULL;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt NULL, &mctx2, 0);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt if (result != ISC_R_SUCCESS)
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt goto out;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt before = isc_mem_inuse(mctx2);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt ptr = isc_mem_allocate(mctx2, 1024000);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt during = isc_mem_inuse(mctx2);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt isc_mem_free(mctx2, ptr);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt after = isc_mem_inuse(mctx2);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt diff = after - before;
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt printf("inuse_before=%lu, inuse_during=%lu, inuse_after=%lu\n",
d2a50c9ba87a6c57cb310308dfe95fff2f2836e2Mark Andrews (unsigned long)before, (unsigned long)during,
d2a50c9ba87a6c57cb310308dfe95fff2f2836e2Mark Andrews (unsigned long)after);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt ATF_REQUIRE_EQ(diff, 0);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt out:
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt if (mctx2 != NULL)
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt isc_mem_destroy(&mctx2);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt isc_test_end();
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt}
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt/*
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt * Main
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt */
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan HuntATF_TP_ADD_TCS(tp) {
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt ATF_TP_ADD_TC(tp, isc_mem_total);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt ATF_TP_ADD_TC(tp, isc_mem_inuse);
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt return (atf_no_error());
84ee90b52d4fb443c796f4e1481f98d5a95b5614Evan Hunt}