From 8f7473d7e983583b3db7daae10810f1f890eef82 Mon Sep 17 00:00:00 2001 From: bert hubert Date: Wed, 25 Nov 2015 15:11:45 +0100 Subject: [PATCH] add ./configure --enable-malloc-trace which will cause powerdns recursor (for now) to trace malloc calls and report them as metrics. --- configure.ac | 1 + m4/pdns_enable_malloc_trace.m4 | 13 +++++++++++++ pdns/Makefile.am | 4 ++++ pdns/malloctrace.cc | 16 +++++++--------- pdns/malloctrace.hh | 4 +++- pdns/pdns_recursor.cc | 18 ++++++++++++++++++ pdns/rec_channel_rec.cc | 10 +++++++++- 7 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 m4/pdns_enable_malloc_trace.m4 diff --git a/configure.ac b/configure.ac index 8634b8a41..74746484e 100644 --- a/configure.ac +++ b/configure.ac @@ -318,6 +318,7 @@ AC_SUBST([PROGRAM_LDFLAGS]) PDNS_ENABLE_COVERAGE PDNS_ENABLE_SANITIZERS +PDNS_ENABLE_MALLOC_TRACE AC_SUBST(LIBS) diff --git a/m4/pdns_enable_malloc_trace.m4 b/m4/pdns_enable_malloc_trace.m4 new file mode 100644 index 000000000..4987a533b --- /dev/null +++ b/m4/pdns_enable_malloc_trace.m4 @@ -0,0 +1,13 @@ +AC_DEFUN([PDNS_ENABLE_MALLOC_TRACE], [ + AC_MSG_CHECKING([whether to enable code malloc-trace]) + AC_ARG_ENABLE([malloc-trace], + AS_HELP_STRING([--enable-malloc-trace], + [enable malloc-trace @<:@default=no@:>@]), + [enable_malloc_trace=$enableval], + [enable_malloc_trace=no] + ) + AC_MSG_RESULT([$enable_malloc_trace]) + AM_CONDITIONAL([MALLOC_TRACE], [test "x$enable_malloc_trace" = "xyes"]) + AS_IF([test "x$enable_malloc_trace" = "xyes"], + AC_DEFINE([MALLOC_TRACE], [1], [Define to 1 if you want to benefit from malloc trace]) ) +]) diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 583bad7c1..d90d7dca8 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -1143,6 +1143,10 @@ pdns_recursor_SOURCES += pkcs11signers.cc pkcs11signers.hh pdns_recursor_LDADD += $(P11KIT1_LIBS) endif +if MALLOC_TRACE +pdns_recursor_SOURCES += malloctrace.cc malloctrace.hh +pdns_recursor_LDFLAGS = $(AM_LDFLAGS) -rdynamic +endif if LUA pdns_recursor_LDADD += $(LUA_LIBS) diff --git a/pdns/malloctrace.cc b/pdns/malloctrace.cc index 268580d00..750782c78 100644 --- a/pdns/malloctrace.cc +++ b/pdns/malloctrace.cc @@ -118,13 +118,11 @@ std::string MallocTracer::topAllocatorsString(int num) return str; } -/* -char **strings; - size_t i; - strings = backtrace_symbols (array, size); //Need -rdynamic gcc (linker) flag for this to work - - for (i = 0; i < size; i++) //skip useless functions - ret+=strings[i]+string("\n"); - return ret; -*/ +void MallocTracer::clearAllocators() +{ + l_active=true; + std::lock_guard lock(d_mut); + d_stats.clear(); + l_active=false; +} diff --git a/pdns/malloctrace.hh b/pdns/malloctrace.hh index 83d23b206..b3091c156 100644 --- a/pdns/malloctrace.hh +++ b/pdns/malloctrace.hh @@ -19,7 +19,7 @@ public: uint64_t getAllocs(const std::string& = std::string()) const { return d_allocs; } uint64_t getAllocFlux(const std::string& = std::string()) const { return d_allocflux; } uint64_t getTotAllocated(const std::string& = std::string()) const { return d_totAllocated; } - + uint64_t getNumOut() { std::lock_guard lock(d_mut); return d_sizes.size(); } struct AllocStats { int count; @@ -29,6 +29,8 @@ public: std::vector > > allocators_t; allocators_t topAllocators(int num=-1); std::string topAllocatorsString(int num=-1); + void clearAllocators(); + private: static std::vector makeBacktrace(); std::atomic d_allocs{0}, d_allocflux{0}, d_totAllocated{0}; diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 6e48f3e1c..81ccf2415 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -56,6 +56,9 @@ #include #include #include +#ifdef MALLOC_TRACE +#include "malloctrace.hh" +#endif #include #include "dnsparser.hh" #include "dnswriter.hh" @@ -641,6 +644,7 @@ void startDoResolve(void *p) tracedQuery=true; } + if(!g_quiet || tracedQuery) L<getTid()<<"/"<numProcesses()<<"] " << (dc->d_tcp ? "TCP " : "") << "question for '"<d_mdp.d_qname<<"|" <d_mdp.d_qtype)<<"' from "<getRemote()<clearAllocators(); + cout<getAllocs()-last<<" "<getNumOut()<<" -- BEGIN TRACE"<getAllocs(); + cout<topAllocatorsString()<clearAllocators(); + */ +#endif + if(!SyncRes::s_nopacketcache && t_packetCache->getResponsePacket(question, g_now.tv_sec, &response, &age)) { if(!g_quiet) L<push_back("packetcached"); + + g_stats.packetCacheHits++; SyncRes::s_queries++; ageDNSPacket(response, age); diff --git a/pdns/rec_channel_rec.cc b/pdns/rec_channel_rec.cc index 1f744ee47..42e27fcec 100644 --- a/pdns/rec_channel_rec.cc +++ b/pdns/rec_channel_rec.cc @@ -6,7 +6,9 @@ #include #include #include - +#ifdef MALLOC_TRACE +#include "malloctrace.hh" +#endif #include "misc.hh" #include "recursor_cache.hh" #include "syncres.hh" @@ -615,6 +617,12 @@ RecursorControlParser::RecursorControlParser() // addGetStat("query-rate", getQueryRate); addGetStat("user-msec", getUserTimeMsec); addGetStat("sys-msec", getSysTimeMsec); + +#ifdef MALLOC_TRACE + addGetStat("memory-allocs", boost::bind(&MallocTracer::getAllocs, g_mtracer, string())); + addGetStat("memory-alloc-flux", boost::bind(&MallocTracer::getAllocFlux, g_mtracer, string())); + addGetStat("memory-allocated", boost::bind(&MallocTracer::getTotAllocated, g_mtracer, string())); +#endif } static void doExitGeneric(bool nicely) -- 2.40.0