From 865621b742bb41554a1a3d4291ca4e8ea7ae3b28 Mon Sep 17 00:00:00 2001 From: Evan Hunt Date: Thu, 22 Jan 2015 09:46:28 -0800 Subject: [PATCH] [v9_10] fix 'total use' accounting 4046. [bug] Accounting of "total use" in memory context statistics was not correct. [RT #38370] --- CHANGES | 3 + bin/named/main.c | 9 ++- lib/isc/mem.c | 3 +- lib/isc/tests/Makefile.in | 10 ++- lib/isc/tests/mem_test.c | 156 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 173 insertions(+), 8 deletions(-) create mode 100644 lib/isc/tests/mem_test.c diff --git a/CHANGES b/CHANGES index c8040da0a2..8150a265d6 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,6 @@ +4046. [bug] Accounting of "total use" in memory context + statistics was not correct. [RT #38370] + 4045. [bug] Skip to next master on dns_request_createvia4 failure. [RT #25185] diff --git a/bin/named/main.c b/bin/named/main.c index dee41be4d3..9187cbf89f 100644 --- a/bin/named/main.c +++ b/bin/named/main.c @@ -81,6 +81,7 @@ #ifdef OPENSSL #include +#include #endif #ifdef HAVE_LIBXML2 #include @@ -602,12 +603,16 @@ parse_command_line(int argc, char *argv[]) { printf("compiled by Solaris Studio %x\n", __SUNPRO_C); #endif #ifdef OPENSSL - printf("using OpenSSL version: %s\n", + printf("compiled with OpenSSL version: %s\n", OPENSSL_VERSION_TEXT); + printf("linked to OpenSSL version: %s\n", + SSLeay_version(SSLEAY_VERSION)); #endif #ifdef HAVE_LIBXML2 - printf("using libxml2 version: %s\n", + printf("compiled with libxml2 version: %s\n", LIBXML_DOTTED_VERSION); + printf("linked to libxml2 version: %s\n", + xmlParserVersion); #endif exit(0); case 'F': diff --git a/lib/isc/mem.c b/lib/isc/mem.c index 07975ee43c..67f36f411f 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -741,9 +741,8 @@ mem_putunlocked(isc__mem_t *ctx, void *mem, size_t size) { (ctx->memfree)(ctx->arg, mem); INSIST(ctx->stats[ctx->max_size].gets != 0U); ctx->stats[ctx->max_size].gets--; - INSIST(size <= ctx->total); + INSIST(size <= ctx->inuse); ctx->inuse -= size; - ctx->total -= size; return; } diff --git a/lib/isc/tests/Makefile.in b/lib/isc/tests/Makefile.in index 22cc7a1ede..452250832c 100644 --- a/lib/isc/tests/Makefile.in +++ b/lib/isc/tests/Makefile.in @@ -12,8 +12,6 @@ # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR # PERFORMANCE OF THIS SOFTWARE. -# $Id$ - srcdir = @srcdir@ VPATH = @srcdir@ top_srcdir = @top_srcdir@ @@ -40,7 +38,7 @@ SRCS = isctest.c taskpool_test.c socket_test.c hash_test.c \ sockaddr_test.c symtab_test.c task_test.c queue_test.c \ parse_test.c pool_test.c print_test.c regex_test.c \ socket_test.c safe_test.c time_test.c aes_test.c \ - counter_test.c + counter_test.c mem_test.c SUBDIRS = TARGETS = taskpool_test@EXEEXT@ socket_test@EXEEXT@ hash_test@EXEEXT@ \ @@ -49,7 +47,7 @@ TARGETS = taskpool_test@EXEEXT@ socket_test@EXEEXT@ hash_test@EXEEXT@ \ queue_test@EXEEXT@ parse_test@EXEEXT@ pool_test@EXEEXT@ \ print_test@EXEEXT@ regex_test@EXEEXT@ socket_test@EXEEXT@ \ safe_test@EXEEXT@ time_test@EXEEXT@ aes_test@EXEEXT@ \ - counter_test@EXEEXT@ + counter_test@EXEEXT@ mem_test@EXEEXT@ @BIND9_MAKE_RULES@ @@ -121,6 +119,10 @@ counter_test@EXEEXT@: counter_test.@O@ ${ISCDEPLIBS} ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \ counter_test.@O@ isctest.@O@ ${ISCLIBS} ${LIBS} +mem_test@EXEEXT@: mem_test.@O@ ${ISCDEPLIBS} + ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \ + mem_test.@O@ isctest.@O@ ${ISCLIBS} ${LIBS} + unit:: sh ${top_srcdir}/unit/unittest.sh diff --git a/lib/isc/tests/mem_test.c b/lib/isc/tests/mem_test.c new file mode 100644 index 0000000000..49190cff91 --- /dev/null +++ b/lib/isc/tests/mem_test.c @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC") + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, + * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM + * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE + * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +#include + +#include "isctest.h" + +#include +#include + +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); +} + +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", + before, after, diff); + /* 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", + before, after, diff); + /* 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", + before, during, after); + ATF_REQUIRE_EQ(diff, 0); + + out: + if (mctx2 != NULL) + isc_mem_destroy(&mctx2); + + isc_test_end(); +} + +/* + * Main + */ +ATF_TP_ADD_TCS(tp) { + ATF_TP_ADD_TC(tp, isc_mem_total); + ATF_TP_ADD_TC(tp, isc_mem_inuse); + + return (atf_no_error()); +}