Repository: trafficserver Updated Branches: refs/heads/master 15c27d277 -> 0bae26a94
TS-3877: Add a tracking ClassAllocator to keep track of where the allocation happened Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/0bae26a9 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/0bae26a9 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/0bae26a9 Branch: refs/heads/master Commit: 0bae26a9402203c55e36292abe7278971a910034 Parents: 15c27d2 Author: Bryan Call <[email protected]> Authored: Mon Aug 31 18:24:49 2015 -0700 Committer: Bryan Call <[email protected]> Committed: Mon Aug 31 18:24:49 2015 -0700 ---------------------------------------------------------------------- lib/ts/Allocator.h | 41 +++++++++++++++++++++++++++++++++++++++++ lib/ts/ink_queue.cc | 5 +++++ lib/ts/ink_resource.cc | 32 ++++++++++++++++++++++++-------- lib/ts/ink_resource.h | 3 ++- 4 files changed, 72 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/0bae26a9/lib/ts/Allocator.h ---------------------------------------------------------------------- diff --git a/lib/ts/Allocator.h b/lib/ts/Allocator.h index ca9d938..63695a9 100644 --- a/lib/ts/Allocator.h +++ b/lib/ts/Allocator.h @@ -44,6 +44,7 @@ #include "ts/ink_queue.h" #include "ts/ink_defs.h" #include "ts/ink_resource.h" +#include <execinfo.h> #define RND16(_x) (((_x) + 15) & ~15) @@ -201,5 +202,45 @@ public: } proto; }; +template <class C> class TrackerClassAllocator : public ClassAllocator<C> +{ +public: + TrackerClassAllocator(const char *name, unsigned int chunk_size = 128, unsigned int alignment = 16) + : ClassAllocator<C>(name, chunk_size, alignment) + { + } + + C * + alloc() + { + void *callstack[128]; + int frames = backtrace(callstack, 128); + char **strs = backtrace_symbols(callstack, frames); + char name[128]; + snprintf(name, sizeof(name), "%s/%s", this->fl->name, strs[1]); + tracker.increment((char *)strs[1], (int64_t)sizeof(C)); + C *ptr = ClassAllocator<C>::alloc(); + reverse_lookup[ptr] = strs[1]; + ++allocations; + ::free(strs); + return ptr; + } + + void + free(C *ptr) + { + std::map<void *, std::string>::iterator it = reverse_lookup.find(ptr); + if (it != reverse_lookup.end()) { + tracker.increment(it->second.c_str(), (int64_t)sizeof(C) * -1); + reverse_lookup.erase(it); + } + ClassAllocator<C>::free(ptr); + } + +private: + ResourceTracker tracker; + std::map<void *, std::string> reverse_lookup; + uint64_t allocations; +}; #endif // _Allocator_h_ http://git-wip-us.apache.org/repos/asf/trafficserver/blob/0bae26a9/lib/ts/ink_queue.cc ---------------------------------------------------------------------- diff --git a/lib/ts/ink_queue.cc b/lib/ts/ink_queue.cc index 8fa2902..fa6508a 100644 --- a/lib/ts/ink_queue.cc +++ b/lib/ts/ink_queue.cc @@ -416,13 +416,18 @@ ink_freelists_dump(FILE *f) fprintf(f, " allocated | in-use | type size | free list name\n"); fprintf(f, "--------------------|--------------------|------------|----------------------------------\n"); + uint64_t total_allocated = 0; + uint64_t total_used = 0; fll = freelists; while (fll) { fprintf(f, " %18" PRIu64 " | %18" PRIu64 " | %10u | memory/%s\n", (uint64_t)fll->fl->allocated * (uint64_t)fll->fl->type_size, (uint64_t)fll->fl->used * (uint64_t)fll->fl->type_size, fll->fl->type_size, fll->fl->name ? fll->fl->name : "<unknown>"); + total_allocated += fll->fl->allocated * fll->fl->type_size; + total_used += fll->fl->used * fll->fl->type_size; fll = fll->next; } + fprintf(f, " %18" PRIu64 " | %18" PRIu64 " | | TOTAL\n", total_allocated, total_used); #else // ! TS_USE_FREELIST (void)f; #endif http://git-wip-us.apache.org/repos/asf/trafficserver/blob/0bae26a9/lib/ts/ink_resource.cc ---------------------------------------------------------------------- diff --git a/lib/ts/ink_resource.cc b/lib/ts/ink_resource.cc index c887c6e..ac272fe 100644 --- a/lib/ts/ink_resource.cc +++ b/lib/ts/ink_resource.cc @@ -24,10 +24,11 @@ #include "ts/ink_assert.h" #include "ts/ink_atomic.h" #include "ts/ink_resource.h" +#include <execinfo.h> volatile int res_track_memory = 0; // Disabled by default -std::map<const char *, Resource *> ResourceTracker::_resourceMap; +std::map<std::string, Resource *> ResourceTracker::_resourceMap; ink_mutex ResourceTracker::resourceLock = PTHREAD_MUTEX_INITIALIZER; /** @@ -43,6 +44,16 @@ public: { return _value; } + int64_t + getIncrement() const + { + return _incrementCount; + } + int64_t + getDecrement() const + { + return _decrementCount; + } private: int64_t _incrementCount; @@ -73,7 +84,7 @@ ResourceTracker::lookup(const char *name) { Resource *resource = NULL; ink_mutex_acquire(&resourceLock); - std::map<const char *, Resource *>::iterator it = _resourceMap.find(name); + std::map<std::string, Resource *>::iterator it = _resourceMap.find(name); if (it != _resourceMap.end()) { resource = it->second; } else { @@ -96,14 +107,19 @@ ResourceTracker::dump(FILE *fd) ink_mutex_acquire(&resourceLock); if (!_resourceMap.empty()) { - fprintf(fd, "%50s | %20s\n", "Location", "Size In-use"); - fprintf(fd, "---------------------------------------------------+------------------------\n"); - for (std::map<const char *, Resource *>::const_iterator it = _resourceMap.begin(); it != _resourceMap.end(); ++it) { + fprintf(fd, "\n%-10s | %-10s | %-20s | %-10s | %-50s\n", "Allocs", "Frees", "Size In-use", "Avg Size", "Location"); + fprintf(fd, "-----------|------------|----------------------|------------|" + "--------------------------------------------------------------------\n"); + for (std::map<std::string, Resource *>::const_iterator it = _resourceMap.begin(); it != _resourceMap.end(); ++it) { const Resource &resource = *it->second; - fprintf(fd, "%50s | %20" PRId64 "\n", it->first, resource.getValue()); - total += resource.getValue(); + if (resource.getIncrement() - resource.getDecrement()) { + fprintf(fd, "%10" PRId64 " | %10" PRId64 " | %20" PRId64 " | %10" PRId64 " | %-50s\n", resource.getIncrement(), + resource.getDecrement(), resource.getValue(), + resource.getValue() / (resource.getIncrement() - resource.getDecrement()), it->first.c_str()); + total += resource.getValue(); + } } + fprintf(fd, " %20" PRId64 " | | %-50s\n", total, "TOTAL"); } ink_mutex_release(&resourceLock); - fprintf(fd, "%50s | %20" PRId64 "\n", "TOTAL", total); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/0bae26a9/lib/ts/ink_resource.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_resource.h b/lib/ts/ink_resource.h index 7726b0c..4e579cd 100644 --- a/lib/ts/ink_resource.h +++ b/lib/ts/ink_resource.h @@ -26,6 +26,7 @@ #include "ts/ink_mutex.h" #include <map> +#include <string> extern volatile int res_track_memory; /* set this to zero to disable resource tracking */ @@ -48,7 +49,7 @@ public: private: static Resource &lookup(const char *name); - static std::map<const char *, Resource *> _resourceMap; + static std::map<std::string, Resource *> _resourceMap; static ink_mutex resourceLock; };
