Instrument buffers.c and rephist.c memory usage

svn:r4317
This commit is contained in:
Nick Mathewson 2005-06-06 17:03:21 +00:00
parent 7c8d5c3178
commit baa10cbbfa
3 changed files with 34 additions and 0 deletions

View File

@ -56,6 +56,9 @@ struct buf_t {
size_t datalen; /**< Number of bytes currently in <b>mem</b>. */
};
uint64_t buf_total_used = 0;
uint64_t buf_total_alloc = 0;
/** Size, in bytes, for newly allocated buffers. Should be a power of 2. */
#define INITIAL_BUF_SIZE (4*1024)
/** Size, in bytes, for minimum 'shrink' size for buffers. Buffers may start
@ -164,6 +167,7 @@ static void buf_resize(buf_t *buf, size_t new_capacity)
buf->mem = GUARDED_MEM(tor_realloc(RAW_MEM(buf->mem),
ALLOC_LEN(new_capacity)));
SET_GUARDS(buf->mem, new_capacity);
buf_total_alloc += (new_capacity - buf->len);
buf->cur = buf->mem+offset;
if (offset + buf->datalen > buf->len) {
/* We need to move data now that we are done growing. The buffer
@ -273,6 +277,7 @@ buf_shrink(buf_t *buf)
static INLINE void buf_remove_from_front(buf_t *buf, size_t n) {
tor_assert(buf->datalen >= n);
buf->datalen -= n;
buf_total_used -= n;
if (buf->datalen) {
buf->cur = _wrap_ptr(buf, buf->cur+n);
} else {
@ -301,6 +306,7 @@ buf_t *buf_new_with_capacity(size_t size) {
SET_GUARDS(buf->mem, size);
buf->len = size;
buf_total_alloc += size;
assert_buf_ok(buf);
return buf;
}
@ -314,6 +320,7 @@ buf_t *buf_new()
/** Remove all data from <b>buf</b> */
void buf_clear(buf_t *buf)
{
buf_total_used -= buf->datalen;
buf->datalen = 0;
buf->cur = buf->mem;
}
@ -344,6 +351,8 @@ void buf_free(buf_t *buf) {
assert_buf_ok(buf);
buf->magic = 0xDEADBEEF;
free(RAW_MEM(buf->mem));
buf_total_alloc -= buf->len;
buf_total_used -= buf->datalen;
tor_free(buf);
}
@ -366,6 +375,7 @@ static INLINE int read_to_buf_impl(int s, size_t at_most, buf_t *buf,
return 0;
} else { /* we read some bytes */
buf->datalen += read_result;
buf_total_used += read_result;
if (buf->datalen > buf->highwater)
buf->highwater = buf->datalen;
log_fn(LOG_DEBUG,"Read %d bytes. %d on inbuf.",read_result,
@ -434,6 +444,7 @@ read_to_buf_tls_impl(tor_tls *tls, size_t at_most, buf_t *buf, char *next)
if (r<0)
return r;
buf->datalen += r;
buf_total_used += r;
if (buf->datalen > buf->highwater)
buf->highwater = buf->datalen;
log_fn(LOG_DEBUG,"Read %d bytes. %d on inbuf; %d pending",r,
@ -631,6 +642,7 @@ write_to_buf(const char *string, size_t string_len, buf_t *buf)
memcpy(next, string, string_len);
buf->datalen += string_len;
buf_total_used += string_len;
if (len2) {
tor_assert(_buf_end(buf) == buf->mem);

View File

@ -1058,6 +1058,9 @@ dumpstats(int severity) {
connection_t *conn;
time_t now = time(NULL);
time_t elapsed;
extern uint64_t buf_total_used;
extern uint64_t buf_total_alloc;
extern uint64_t rephist_total_alloc;
log(severity, "Dumping stats:");
@ -1121,6 +1124,12 @@ dumpstats(int severity) {
(int) (stats_n_bytes_written/elapsed));
}
log(severity, "--------------- Dumping memory information:");
log(severity, "In buffers: "U64_FORMAT" used/"U64_FORMAT" allocated.",
U64_PRINTF_ARG(buf_total_used), U64_PRINTF_ARG(buf_total_alloc));
log(severity, "In rephist: "U64_FORMAT" used.",
U64_PRINTF_ARG(rephist_total_alloc));
rep_hist_dump_stats(now,severity);
rend_service_dump_stats(severity);
}

View File

@ -13,6 +13,8 @@ const char rephist_c_id[] = "$Id$";
static void bw_arrays_init(void);
static void predicted_ports_init(void);
uint64_t rephist_total_alloc;
/** History of an OR-\>OR link. */
typedef struct link_history_t {
/** When did we start tracking this list? */
@ -67,6 +69,7 @@ static or_history_t *get_or_history(const char* id)
hist = (or_history_t*) strmap_get(history_map, hexid);
if (!hist) {
hist = tor_malloc_zero(sizeof(or_history_t));
rephist_total_alloc += sizeof(or_history_t);
hist->link_history_map = strmap_new();
hist->since = hist->changed = time(NULL);
strmap_set(history_map, hexid, hist);
@ -93,6 +96,7 @@ static link_history_t *get_link_history(const char *from_id,
lhist = (link_history_t*) strmap_get(orhist->link_history_map, to_hexid);
if (!lhist) {
lhist = tor_malloc_zero(sizeof(link_history_t));
rephist_total_alloc += sizeof(link_history_t);
lhist->since = lhist->changed = time(NULL);
strmap_set(orhist->link_history_map, to_hexid, lhist);
}
@ -102,6 +106,7 @@ static link_history_t *get_link_history(const char *from_id,
static void
_free_link_history(void *val)
{
rephist_total_alloc -= sizeof(link_history_t);
tor_free(val);
}
@ -110,6 +115,7 @@ free_or_history(void *_hist)
{
or_history_t *hist = _hist;
strmap_free(hist->link_history_map, _free_link_history);
rephist_total_alloc -= sizeof(or_history_t);
tor_free(hist);
}
@ -347,6 +353,7 @@ void rep_history_clean(time_t before)
strmap_iter_get(lhist_it, &hd2, &link_history_p);
link_history = link_history_p;
if (link_history->changed < before) {
rephist_total_alloc -= sizeof(link_history_t);
tor_free(link_history);
lhist_it = strmap_iter_next_rmv(or_history->link_history_map,lhist_it);
continue;
@ -495,6 +502,7 @@ static bw_array_t *bw_array_new(void) {
bw_array_t *b;
time_t start;
b = tor_malloc_zero(sizeof(bw_array_t));
rephist_total_alloc += sizeof(bw_array_t);
start = time(NULL);
b->cur_obs_time = start;
b->next_period = start + NUM_SECS_BW_SUM_INTERVAL;
@ -626,10 +634,12 @@ static smartlist_t *predicted_ports_list=NULL;
static smartlist_t *predicted_ports_times=NULL;
static void add_predicted_port(uint16_t port, time_t now) {
/* XXXX we could just use uintptr_t here, I think. */
uint16_t *tmp_port = tor_malloc(sizeof(uint16_t));
time_t *tmp_time = tor_malloc(sizeof(time_t));
*tmp_port = port;
*tmp_time = now;
rephist_total_alloc += sizeof(uint16_t) + sizeof(time_t);
smartlist_add(predicted_ports_list, tmp_port);
smartlist_add(predicted_ports_times, tmp_time);
}
@ -641,8 +651,10 @@ static void predicted_ports_init(void) {
}
static void predicted_ports_free(void) {
rephist_total_alloc -= smartlist_len(predicted_ports_list)*sizeof(uint16_t);
SMARTLIST_FOREACH(predicted_ports_list, char *, cp, tor_free(cp));
smartlist_free(predicted_ports_list);
rephist_total_alloc -= smartlist_len(predicted_ports_times)*sizeof(time_t);
SMARTLIST_FOREACH(predicted_ports_times, char *, cp, tor_free(cp));
smartlist_free(predicted_ports_times);
}
@ -697,6 +709,7 @@ smartlist_t *rep_hist_get_predicted_ports(time_t now) {
log_fn(LOG_DEBUG, "Expiring predicted port %d", *tmp_port);
smartlist_del(predicted_ports_list, i);
smartlist_del(predicted_ports_times, i);
rephist_total_alloc -= sizeof(uint16_t)+sizeof(time_t);
tor_free(tmp_port);
tor_free(tmp_time);
i--;