Repository: trafficserver Updated Branches: refs/heads/master 355c165ad -> 3b40453a4
TS-3116: Add support for tracking the use of the ioBuffers Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/3b40453a Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/3b40453a Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/3b40453a Branch: refs/heads/master Commit: 3b40453a47de9da8a18f5ba22cef4edfd24f1b7e Parents: 355c165 Author: Bryan Call <[email protected]> Authored: Mon Oct 6 19:43:08 2014 -0700 Committer: Bryan Call <[email protected]> Committed: Tue Oct 7 16:31:05 2014 -0700 ---------------------------------------------------------------------- iocore/eventsystem/P_IOBuffer.h | 18 +-------- lib/ts/ink_resource.cc | 76 +++++++++++++++++++++++++++++++++--- lib/ts/ink_resource.h | 33 ++++++++-------- mgmt/web2/WebHttpMessage.cc | 1 + proxy/shared/signals.cc | 2 + 5 files changed, 92 insertions(+), 38 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/3b40453a/iocore/eventsystem/P_IOBuffer.h ---------------------------------------------------------------------- diff --git a/iocore/eventsystem/P_IOBuffer.h b/iocore/eventsystem/P_IOBuffer.h index 2edfc79..0e0a7ab 100644 --- a/iocore/eventsystem/P_IOBuffer.h +++ b/iocore/eventsystem/P_IOBuffer.h @@ -149,14 +149,7 @@ iobuffer_mem_inc(const char *_loc, int64_t _size_index) if (!_loc) _loc = "memory/IOBuffer/UNKNOWN-LOCATION"; - Resource *res = res_lookup(_loc); - ink_assert(strcmp(_loc, res->path) == 0); -#ifdef DEBUG - int64_t r = ink_atomic_increment(&res->value, index_to_buffer_size(_size_index)); - ink_assert(r >= 0); -#else - ink_atomic_increment(&res->value, index_to_buffer_size(_size_index)); -#endif + ResourceTracker::increment(_loc, index_to_buffer_size(_size_index)); } TS_INLINE void @@ -169,14 +162,7 @@ iobuffer_mem_dec(const char *_loc, int64_t _size_index) return; if (!_loc) _loc = "memory/IOBuffer/UNKNOWN-LOCATION"; - Resource *res = res_lookup(_loc); - ink_assert(strcmp(_loc, res->path) == 0); -#ifdef DEBUG - int64_t r = ink_atomic_increment(&res->value, -index_to_buffer_size(_size_index)); - ink_assert(r >= index_to_buffer_size(_size_index)); -#else - ink_atomic_increment(&res->value, -index_to_buffer_size(_size_index)); -#endif + ResourceTracker::increment(_loc, -index_to_buffer_size(_size_index)); } #endif http://git-wip-us.apache.org/repos/asf/trafficserver/blob/3b40453a/lib/ts/ink_resource.cc ---------------------------------------------------------------------- diff --git a/lib/ts/ink_resource.cc b/lib/ts/ink_resource.cc index b23dcf5..5a6a028 100644 --- a/lib/ts/ink_resource.cc +++ b/lib/ts/ink_resource.cc @@ -21,17 +21,81 @@ limitations under the License. */ -#include "libts.h" - #include "ink_assert.h" #include "ink_atomic.h" #include "ink_resource.h" -#include "ink_stack_trace.h" volatile int res_track_memory = 0; // Disabled by default -Resource * -res_lookup(const char */* path ATS_UNUSED */) +std::map<const char*, Resource*> ResourceTracker::_resourceMap; +ink_mutex ResourceTracker::resourceLock = PTHREAD_MUTEX_INITIALIZER; + +/** + * Individual resource to keep track of. A map of these are in the ResourceTracker. + */ +class Resource +{ +public: + Resource(): _incrementCount(0), _decrementCount(0), _value(0) {} + void increment(const int64_t size); + int64_t getValue() const { return _value; } +private: + int64_t _incrementCount; + int64_t _decrementCount; + int64_t _value; +}; + +void Resource::increment(const int64_t size) { + ink_atomic_increment(&_value, size); + if (size >= 0) { + ink_atomic_increment(&_incrementCount, 1); + } else { + ink_atomic_increment(&_decrementCount, 1); + } +} + +void +ResourceTracker::increment(const char *name, const int64_t size) { + Resource &resource = lookup(name); + resource.increment(size); +} + +Resource& +ResourceTracker::lookup(const char *name) { - return NULL; + Resource *resource = NULL; + ink_mutex_acquire(&resourceLock); + std::map<const char*, Resource*>::iterator it = _resourceMap.find(name); + if (it != _resourceMap.end()) { + resource = it->second; + } else { + // create a new entry + resource = new Resource; + _resourceMap[name] = resource; + } + ink_mutex_release(&resourceLock); + ink_release_assert(resource != NULL); + return *resource; +} + +void +ResourceTracker::dump(FILE *fd) { + if (!res_track_memory) { + return; + } + int64_t total = 0; + + 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) { + const Resource &resource = *it->second; + fprintf(fd, "%50s | %20lld\n", it->first, resource.getValue()); + total += resource.getValue(); + } + } + ink_mutex_release(&resourceLock); + fprintf(fd, "%50s | %20lld\n", "TOTAL", total); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/3b40453a/lib/ts/ink_resource.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_resource.h b/lib/ts/ink_resource.h index c62e22c..7be1447 100644 --- a/lib/ts/ink_resource.h +++ b/lib/ts/ink_resource.h @@ -24,13 +24,8 @@ #ifndef __INK_RESOURCE_H__ #define __INK_RESOURCE_H__ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "ink_apidefs.h" -#include "ink_defs.h" -#include "ink_memory.h" +#include "ink_mutex.h" +#include <map> extern volatile int res_track_memory; /* set this to zero to disable resource tracking */ @@ -38,16 +33,22 @@ extern volatile int res_track_memory; /* set this to zero to disable resource #define _RES_PATH(x) __RES_PATH (x) #define RES_PATH(x) x __FILE__ ":" _RES_PATH (__LINE__) -struct Resource +class Resource; + +/** + * Generic class to keep track of memory usage + * Used to keep track of the location in the code that allocated ioBuffer memory + */ +class ResourceTracker { - void *magic; - struct Resource *next; - const char *path; - int64_t value; - int64_t snapshot; - int64_t baseline; +public: + ResourceTracker() {}; + static void increment(const char* name, const int64_t size); + static void dump(FILE *fd); +private: + static Resource& lookup(const char* name); + static std::map<const char*, Resource*> _resourceMap; + static ink_mutex resourceLock; }; -typedef struct Resource Resource; - #endif /* __INK_RESOURCE_H__ */ http://git-wip-us.apache.org/repos/asf/trafficserver/blob/3b40453a/mgmt/web2/WebHttpMessage.cc ---------------------------------------------------------------------- diff --git a/mgmt/web2/WebHttpMessage.cc b/mgmt/web2/WebHttpMessage.cc index 57664bd..00bd238 100644 --- a/mgmt/web2/WebHttpMessage.cc +++ b/mgmt/web2/WebHttpMessage.cc @@ -25,6 +25,7 @@ #include "ink_defs.h" #include "ink_string.h" #include "ink_time.h" +#include "ink_memory.h" #include "WebUtils.h" #include "WebHttpMessage.h" http://git-wip-us.apache.org/repos/asf/trafficserver/blob/3b40453a/proxy/shared/signals.cc ---------------------------------------------------------------------- diff --git a/proxy/shared/signals.cc b/proxy/shared/signals.cc index ed52780..aa623bd 100644 --- a/proxy/shared/signals.cc +++ b/proxy/shared/signals.cc @@ -71,6 +71,7 @@ public: sigusr1_received = 0; // TODO: TS-567 Integrate with debugging allocators "dump" features? ink_freelists_dump(stderr); + ResourceTracker::dump(stderr); if (!end) end = (char *) sbrk(0); if (!snap) @@ -124,6 +125,7 @@ public: } else { // TODO: TS-567 Integrate with debugging allocators "dump" features? ink_freelists_dump(stderr); + ResourceTracker::dump(stderr); } if (!baseline_taken && use_baseline) { ink_freelists_snap_baseline();
