[or-cvs] Instrument buffers.c and rephist.c memory usage

Nick Mathewson nickm at seul.org
Mon Jun 6 17:03:23 UTC 2005


Update of /home/or/cvsroot/tor/src/or
In directory moria:/tmp/cvs-serv6665/src/or

Modified Files:
	buffers.c main.c rephist.c 
Log Message:
Instrument buffers.c and rephist.c memory usage

Index: buffers.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/buffers.c,v
retrieving revision 1.152
retrieving revision 1.153
diff -u -d -r1.152 -r1.153
--- buffers.c	3 May 2005 10:04:07 -0000	1.152
+++ buffers.c	6 Jun 2005 17:03:21 -0000	1.153
@@ -56,6 +56,9 @@
   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 @@
   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 @@
 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 @@
   SET_GUARDS(buf->mem, size);
   buf->len = size;
 
+  buf_total_alloc += size;
   assert_buf_ok(buf);
   return buf;
 }
@@ -314,6 +320,7 @@
 /** 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 @@
   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 @@
     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 @@
   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 @@
 
   memcpy(next, string, string_len);
   buf->datalen += string_len;
+  buf_total_used += string_len;
 
   if (len2) {
     tor_assert(_buf_end(buf) == buf->mem);

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/main.c,v
retrieving revision 1.505
retrieving revision 1.506
diff -u -d -r1.505 -r1.506
--- main.c	27 May 2005 17:43:43 -0000	1.505
+++ main.c	6 Jun 2005 17:03:21 -0000	1.506
@@ -1058,6 +1058,9 @@
   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 @@
         (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);
 }

Index: rephist.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/rephist.c,v
retrieving revision 1.56
retrieving revision 1.57
diff -u -d -r1.56 -r1.57
--- rephist.c	1 Apr 2005 20:15:55 -0000	1.56
+++ rephist.c	6 Jun 2005 17:03:21 -0000	1.57
@@ -13,6 +13,8 @@
 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 @@
   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 @@
   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 void
 _free_link_history(void *val)
 {
+  rephist_total_alloc -= sizeof(link_history_t);
   tor_free(val);
 }
 
@@ -110,6 +115,7 @@
 {
   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 @@
       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 @@
   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_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_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 @@
       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--;



More information about the tor-commits mailing list