Repository: trafficserver Updated Branches: refs/heads/master 4784d8327 -> 35e93fb0a
TS-4212: Add option to track memory allocation with OpenSSL This closes #489 Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/35e93fb0 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/35e93fb0 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/35e93fb0 Branch: refs/heads/master Commit: 35e93fb0a1112e2e9f66f406abd90b6b55fec8c6 Parents: 4784d83 Author: Bryan Call <[email protected]> Authored: Tue Feb 23 15:06:58 2016 -0800 Committer: Bryan Call <[email protected]> Committed: Tue Feb 23 15:08:21 2016 -0800 ---------------------------------------------------------------------- configure.ac | 5 +++++ iocore/net/SSLUtils.cc | 24 +++++++++++++++++++++++- lib/ts/ink_memory.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/ts/ink_memory.h | 5 +++++ lib/ts/ink_queue.cc | 4 +++- lib/ts/ink_resource.cc | 14 +++++++++++++- lib/ts/ink_resource.h | 2 ++ mgmt/RecordsConfig.cc | 2 +- 8 files changed, 94 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/35e93fb0/configure.ac ---------------------------------------------------------------------- diff --git a/configure.ac b/configure.ac index be00b84..e49ae14 100644 --- a/configure.ac +++ b/configure.ac @@ -1183,6 +1183,11 @@ AC_CHECK_HEADERS(mcheck.h) AC_CHECK_FUNCS(mcheck_pedantic) # +# Check for malloc_usable_size() +# +AC_CHECK_FUNCS(malloc_usable_size) + +# # Check for pcre library # TS_CHECK_PCRE http://git-wip-us.apache.org/repos/asf/trafficserver/blob/35e93fb0/iocore/net/SSLUtils.cc ---------------------------------------------------------------------- diff --git a/iocore/net/SSLUtils.cc b/iocore/net/SSLUtils.cc index ff3fb46..da7f176 100644 --- a/iocore/net/SSLUtils.cc +++ b/iocore/net/SSLUtils.cc @@ -790,13 +790,35 @@ SSLRecRawStatSyncCount(const char *name, RecDataT data_type, RecData *data, RecR return RecRawStatSyncCount(name, data_type, data, rsb, id); } +void * +ssl_malloc(size_t size) +{ + return ats_track_malloc(size, &ssl_memory_allocated); +} + +void * +ssl_realloc(void *ptr, size_t size) +{ + return ats_track_realloc(ptr, size, &ssl_memory_allocated, &ssl_memory_freed); +} + +void +ssl_free(void *ptr) +{ + ats_track_free(ptr, &ssl_memory_freed); +} + void SSLInitializeLibrary() { if (!open_ssl_initialized) { // BoringSSL does not have the memory functions #ifndef OPENSSL_IS_BORINGSSL - CRYPTO_set_mem_functions(ats_malloc, ats_realloc, ats_free); + if (res_track_memory >= 2) { + CRYPTO_set_mem_functions(ssl_malloc, ssl_realloc, ssl_free); + } else { + CRYPTO_set_mem_functions(ats_malloc, ats_realloc, ats_free); + } #endif SSL_load_error_strings(); http://git-wip-us.apache.org/repos/asf/trafficserver/blob/35e93fb0/lib/ts/ink_memory.cc ---------------------------------------------------------------------- diff --git a/lib/ts/ink_memory.cc b/lib/ts/ink_memory.cc index 698ab78..00d7eb6 100644 --- a/lib/ts/ink_memory.cc +++ b/lib/ts/ink_memory.cc @@ -25,6 +25,7 @@ #include "ts/ink_defs.h" #include "ts/ink_stack_trace.h" #include "ts/Diags.h" +#include "ts/ink_atomic.h" #include <assert.h> #if defined(linux) @@ -209,6 +210,47 @@ ats_mlock(caddr_t addr, size_t len) return res; } +void * +ats_track_malloc(size_t size, uint64_t *stat) +{ + void *ptr = ats_malloc(size); +#ifdef HAVE_MALLOC_USABLE_SIZE + ink_atomic_increment(stat, malloc_usable_size(ptr)); +#endif + return ptr; +} + +void * +ats_track_realloc(void *ptr, size_t size, uint64_t *alloc_stat, uint64_t *free_stat) +{ +#ifdef HAVE_MALLOC_USABLE_SIZE + const size_t old_size = malloc_usable_size(ptr); + ptr = ats_realloc(ptr, size); + const size_t new_size = malloc_usable_size(ptr); + if (old_size < new_size) { + // allocating something bigger + ink_atomic_increment(alloc_stat, new_size - old_size); + } else if (old_size > new_size) { + ink_atomic_increment(free_stat, old_size - new_size); + } + return ptr; +#else + return ats_realloc(ptr, size); +#endif +} + +void +ats_track_free(void *ptr, uint64_t *stat) +{ + if (ptr == NULL) { + return; + } + +#ifdef HAVE_MALLOC_USABLE_SIZE + ink_atomic_increment(stat, malloc_usable_size(ptr)); +#endif + ats_free(ptr); +} /*------------------------------------------------------------------------- Moved from old ink_resource.h http://git-wip-us.apache.org/repos/asf/trafficserver/blob/35e93fb0/lib/ts/ink_memory.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_memory.h b/lib/ts/ink_memory.h index 67d900c..8a1ca8b 100644 --- a/lib/ts/ink_memory.h +++ b/lib/ts/ink_memory.h @@ -26,6 +26,7 @@ #include <ctype.h> #include <string.h> #include <strings.h> +#include <inttypes.h> #include "ts/ink_config.h" @@ -92,6 +93,10 @@ int ats_msync(caddr_t addr, size_t len, caddr_t end, int flags); int ats_madvise(caddr_t addr, size_t len, int flags); int ats_mlock(caddr_t addr, size_t len); +void *ats_track_malloc(size_t size, uint64_t *stat); +void *ats_track_realloc(void *ptr, size_t size, uint64_t *alloc_stat, uint64_t *free_stat); +void ats_track_free(void *ptr, uint64_t *stat); + static inline size_t __attribute__((const)) ats_pagesize(void) { static size_t page_size; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/35e93fb0/lib/ts/ink_queue.cc ---------------------------------------------------------------------- diff --git a/lib/ts/ink_queue.cc b/lib/ts/ink_queue.cc index e331a28..af3766e 100644 --- a/lib/ts/ink_queue.cc +++ b/lib/ts/ink_queue.cc @@ -426,6 +426,7 @@ ink_freelists_dump_baselinerel(FILE *f) } fll = fll->next; } + fprintf(f, "-----------------------------------------------------------------------------------------\n"); } void @@ -435,7 +436,7 @@ ink_freelists_dump(FILE *f) if (f == NULL) f = stderr; - fprintf(f, " allocated | in-use | type size | free list name\n"); + fprintf(f, " Allocated | In-Use | Type Size | Free List Name\n"); fprintf(f, "--------------------|--------------------|------------|----------------------------------\n"); uint64_t total_allocated = 0; @@ -450,6 +451,7 @@ ink_freelists_dump(FILE *f) fll = fll->next; } fprintf(f, " %18" PRIu64 " | %18" PRIu64 " | | TOTAL\n", total_allocated, total_used); + fprintf(f, "-----------------------------------------------------------------------------------------\n"); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/35e93fb0/lib/ts/ink_resource.cc ---------------------------------------------------------------------- diff --git a/lib/ts/ink_resource.cc b/lib/ts/ink_resource.cc index d4180b9..16cc6e7 100644 --- a/lib/ts/ink_resource.cc +++ b/lib/ts/ink_resource.cc @@ -27,6 +27,8 @@ #include <execinfo.h> volatile int res_track_memory = 0; // Disabled by default +uint64_t ssl_memory_allocated = 0; +uint64_t ssl_memory_freed = 0; std::map<const char *, Resource *> ResourceTracker::_resourceMap; ink_mutex ResourceTracker::resourceLock = PTHREAD_MUTEX_INITIALIZER; @@ -165,8 +167,18 @@ ResourceTracker::dump(FILE *fd) resource.getDecrement(), resource.getValue(), average_size, resource.getName()); total += resource.getValue(); } + fprintf(fd, " %20" PRId64 " | | %-50s\n", total, "TOTAL"); + fprintf(fd, "--------------------------------------------------------------" + "--------------------------------------------------------------------\n"); } - fprintf(fd, " %20" PRId64 " | | %-50s\n", total, "TOTAL"); ink_mutex_release(&resourceLock); + + if (res_track_memory >= 2) { + fprintf(fd, "\n%-20s | %-20s | %-20s | %-20s\n", "Total Allocated", "Total Freed", "Currently Allocated", "Type"); + fprintf(fd, "---------------------|----------------------|----------------------|----------------------\n"); + fprintf(fd, "%20" PRId64 " | %20" PRId64 " | %20" PRId64 " | %-50s\n", ssl_memory_allocated, ssl_memory_freed, + ssl_memory_allocated - ssl_memory_freed, "SSL Allocated Memory"); + fprintf(fd, "---------------------|----------------------|----------------------|----------------------\n"); + } } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/35e93fb0/lib/ts/ink_resource.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_resource.h b/lib/ts/ink_resource.h index 27bce50..135f21e 100644 --- a/lib/ts/ink_resource.h +++ b/lib/ts/ink_resource.h @@ -29,6 +29,8 @@ #include <string> extern volatile int res_track_memory; /* set this to zero to disable resource tracking */ +extern uint64_t ssl_memory_allocated; +extern uint64_t ssl_memory_freed; #define __RES_PATH(x) #x #define _RES_PATH(x) __RES_PATH(x) http://git-wip-us.apache.org/repos/asf/trafficserver/blob/35e93fb0/mgmt/RecordsConfig.cc ---------------------------------------------------------------------- diff --git a/mgmt/RecordsConfig.cc b/mgmt/RecordsConfig.cc index 553476e..541ec07 100644 --- a/mgmt/RecordsConfig.cc +++ b/mgmt/RecordsConfig.cc @@ -107,7 +107,7 @@ static const RecordElement RecordsConfig[] = {RECT_CONFIG, "proxy.config.snapshot_dir", RECD_STRING, "snapshots", RECU_NULL, RR_REQUIRED, RECC_NULL, NULL, RECA_NULL} , //# 0 = disable - {RECT_CONFIG, "proxy.config.res_track_memory", RECD_INT, "0", RECU_NULL, RR_NULL, RECC_NULL, NULL, RECA_NULL} + {RECT_CONFIG, "proxy.config.res_track_memory", RECD_INT, "0", RECU_NULL, RR_REQUIRED, RECC_NULL, "[0-2]", RECA_NULL} , //############################################################################## //# Traffic Server system settings
