Browse Source

Optionally export mallinfo() output on the DirPort

svn:r11232
Peter Palfrader 18 years ago
parent
commit
8c3d14cda5
2 changed files with 47 additions and 1 deletions
  1. 4 1
      ChangeLog
  2. 43 0
      src/or/directory.c

+ 4 - 1
ChangeLog

@@ -15,7 +15,10 @@ Changes in version 0.2.0.6-alpha - 2007-??-??
     - Let directory authorities startup even when they can't generate
       a descriptor immediately, e.g. because they don't know their
       address.
-
+  o Minor features (misc):
+    - Optionally (if built with -DEXPORTMEMINFO) export the output
+      of mallinfo via http, as tor/mallinfo.txt.  Only accessible
+      from localhost.
 
 Changes in version 0.2.0.5-alpha - 2007-08-19
   o Removed features:

+ 43 - 0
src/or/directory.c

@@ -6,6 +6,9 @@ const char directory_c_id[] =
   "$Id$";
 
 #include "or.h"
+#if defined(EXPORTMEMINFO) && defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
+#include <malloc.h>
+#endif
 
 /**
  * \file directory.c
@@ -2193,6 +2196,46 @@ directory_handle_command_get(dir_connection_t *conn, const char *headers,
     goto done;
   }
 
+#if defined(EXPORTMEMINFO) && defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
+#define ADD_MALLINFO_LINE(x) do {                               \
+    tor_snprintf(tmp, sizeof(tmp), "%s %d\n", #x, mi.x);         \
+    smartlist_add(lines, tor_strdup(tmp));                      \
+  } while(0);
+
+  if (!strcmp(url,"/tor/mallinfo.txt") &&
+      (conn->_base.addr == 0x7f000001ul)) {
+    char *result;
+    size_t len;
+    struct mallinfo mi;
+    memset(&mi, 0, sizeof(mi));
+    smartlist_t *lines;
+
+    mi = mallinfo();
+    lines = smartlist_create();
+    char tmp[256];
+
+    ADD_MALLINFO_LINE(arena)
+    ADD_MALLINFO_LINE(ordblks)
+    ADD_MALLINFO_LINE(smblks)
+    ADD_MALLINFO_LINE(hblks)
+    ADD_MALLINFO_LINE(hblkhd)
+    ADD_MALLINFO_LINE(usmblks)
+    ADD_MALLINFO_LINE(fsmblks)
+    ADD_MALLINFO_LINE(uordblks)
+    ADD_MALLINFO_LINE(fordblks)
+    ADD_MALLINFO_LINE(keepcost)
+
+    result = smartlist_join_strings(lines, "", 0, NULL);
+    SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
+    smartlist_free(lines);
+
+    len = strlen(result);
+    write_http_response_header(conn, len, 0, 0);
+    connection_write_to_buf(result, len, TO_CONN(conn));
+    tor_free(result);
+  }
+#endif
+
   /* we didn't recognize the url */
   write_http_status_line(conn, 404, "Not found");