1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/** @file
A brief file description
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#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
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;
/**
* Individual resource to keep track of. A map of these are in the ResourceTracker.
*/
class Resource
{
public:
Resource() : _incrementCount(0), _decrementCount(0), _value(0), _symbol(NULL) { _name[0] = '\0'; }
void increment(const int64_t size);
int64_t
getValue() const
{
return _value;
}
int64_t
getIncrement() const
{
return _incrementCount;
}
int64_t
getDecrement() const
{
return _decrementCount;
}
void
setSymbol(const void *symbol)
{
_symbol = symbol;
}
void
setName(const char *name)
{
strncpy(_name, name, sizeof(_name));
_name[sizeof(_name) - 1] = '\0';
}
void
setName(const void *symbol, const char *name)
{
Dl_info info;
dladdr(const_cast<void *>(symbol), &info);
snprintf(_name, sizeof(_name), "%s/%s", name, info.dli_sname);
}
const char *
getName() const
{
return _name;
}
const void *
getSymbol() const
{
return _symbol;
}
private:
int64_t _incrementCount;
int64_t _decrementCount;
int64_t _value;
const void *_symbol;
char _name[128];
};
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);
const char *lookup_name = resource.getName();
if (lookup_name[0] == '\0') {
resource.setName(name);
}
resource.increment(size);
}
void
ResourceTracker::increment(const void *symbol, const int64_t size, const char *name)
{
Resource &resource = lookup((const char *)symbol);
if (resource.getSymbol() == NULL && name != NULL) {
resource.setName(symbol, name);
resource.setSymbol(symbol);
}
resource.increment(size);
}
Resource &
ResourceTracker::lookup(const char *name)
{
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, "\n%-10s | %-10s | %-20s | %-10s | %-50s\n", "Allocs", "Frees", "Size In-use", "Avg Size", "Location");
fprintf(fd, "-----------|------------|----------------------|------------|"
"--------------------------------------------------------------------\n");
for (std::map<const char *, Resource *>::const_iterator it = _resourceMap.begin(); it != _resourceMap.end(); ++it) {
const Resource &resource = *it->second;
int64_t average_size = 0;
if (resource.getIncrement() - resource.getDecrement() > 0) {
average_size = resource.getValue() / (resource.getIncrement() - resource.getDecrement());
}
fprintf(fd, "%10" PRId64 " | %10" PRId64 " | %20" PRId64 " | %10" PRId64 " | %-50s\n", resource.getIncrement(),
resource.getDecrement(), resource.getValue(), average_size, resource.getName());
total += resource.getValue();
}
fprintf(fd, " %20" PRId64 " | | %-50s\n", total, "TOTAL");
fprintf(fd, "--------------------------------------------------------------"
"--------------------------------------------------------------------\n");
}
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");
}
}
|