Revision: 56716
          http://sourceforge.net/p/brlcad/code/56716
Author:   brlcad
Date:     2013-08-09 16:06:43 +0000 (Fri, 09 Aug 2013)
Log Message:
-----------
provide an interface for managing the summary/logging output, allowing calling 
applications to register a printing function (such as printf, bu_log, puts, 
etc).  instead of tying the printing to bu_debug being set, read the 
BU_HEAP_PRINT environment variable.  take some minimal steps to minimize how 
many atexit() handlers are registered in a parallel situation, though the 
handler also checks.

Modified Paths:
--------------
    brlcad/trunk/src/libbu/heap.c

Modified: brlcad/trunk/src/libbu/heap.c
===================================================================
--- brlcad/trunk/src/libbu/heap.c       2013-08-09 16:02:39 UTC (rev 56715)
+++ brlcad/trunk/src/libbu/heap.c       2013-08-09 16:06:43 UTC (rev 56716)
@@ -88,6 +88,20 @@
 static struct cpus per_cpu[MAX_PSW] = {{{{0, 0, 0}}, 0}};
 
 
+typedef int (*heap_func_t)(const char *, ...);
+
+heap_func_t
+bu_heap_log(heap_func_t log)
+{
+    static heap_func_t heap_log = (heap_func_t)&bu_log;
+
+    if (log)
+       heap_log = log;
+
+    return heap_log;
+}
+
+
 static void
 heap_print()
 {
@@ -100,17 +114,21 @@
     size_t total_pages = 0;
     size_t ncpu = bu_avail_cpus();
 
-    /* this may get registered for atexit() multiple times, so make
-     * sure we only do this once
+    struct bu_vls str = BU_VLS_INIT_ZERO;
+
+    /* this may get atexit()-registered multiple times, so make sure
+     * we only do this once
      */
     if (printed++ > 0) {
        return;
     }
 
-    bu_log("=======================\n"
-          "Memory Heap Information\n"
-          "-----------------------\n");
+    heap_func_t log = bu_heap_log(NULL);
 
+    log("=======================\n"
+       "Memory Heap Information\n"
+       "-----------------------\n", NULL);
+
     for (h=0; h < ncpu; h++) {
        for (i=0; i < HEAP_BINS; i++) {
 
@@ -120,20 +138,29 @@
            if (got > 0) {
                /* last page is partial */
                got -= (HEAP_PAGESIZE - per_cpu[h].heap[i].given)/(i+1);
-               bu_log("%04zu [%02zu] => %zu\n", i, per_cpu[h].heap[i].count, 
got);
+               bu_vls_sprintf(&str, "%04zu [%02zu] => %zu\n", i, 
per_cpu[h].heap[i].count, got);
+               log(bu_vls_addr(&str), NULL);
                allocs += got;
            }
            total_pages += per_cpu[h].heap[i].count;
        }
        misses += per_cpu[h].misses;
     }
-    bu_log("-----------------------\n"
-          "size [pages] => count\n"
-          "Heap range: 1-%d bytes\n"
-          "Page size: %d bytes\n"
-          "Pages: %zu (%.2lfMB)\n"
-          "%zu allocs, %zu misses\n"
-          "=======================\n", HEAP_BINS, HEAP_PAGESIZE, total_pages, 
(double)(total_pages * HEAP_PAGESIZE) / (1024.0*1024.0), allocs, misses);
+    bu_vls_sprintf(&str, "-----------------------\n"
+                  "size [pages] => count\n"
+                  "Heap range: 1-%d bytes\n"
+                  "Page size: %d bytes\n"
+                  "Pages: %zu (%.2lfMB)\n"
+                  "%zu allocs, %zu misses\n"
+                  "=======================\n",
+                  HEAP_BINS,
+                  HEAP_PAGESIZE,
+                  total_pages,
+                  (double)(total_pages * HEAP_PAGESIZE) / (1024.0*1024.0),
+                  allocs,
+                  misses);
+    log(bu_vls_addr(&str), NULL);
+    bu_vls_free(&str);
 }
 
 
@@ -142,13 +169,14 @@
 {
     char *ret;
     register size_t smo = sz-1;
-    static int printit = 0;
+    static int registered = 0;
     int oncpu;
     struct heap *heap;
 
     /* what thread are we? */
     oncpu = bu_parallel_id();
 
+#ifdef DEBUG
     if (sz > HEAP_BINS || sz == 0) {
        per_cpu[oncpu].misses++;
 
@@ -161,15 +189,18 @@
        }
        return bu_calloc(1, sz, "heap calloc");
     }
+#endif
 
     heap = &per_cpu[oncpu].heap[smo];
 
     /* init */
     if (heap->count == 0) {
 
-       if (bu_debug && printit == 0) {
-           printit++;
-           atexit(heap_print);
+       if (registered++ == 0) {
+           ret = getenv("BU_HEAP_PRINT");
+           if ((++registered == 2) && (ret && atoi(ret) > 0)) {
+               atexit(heap_print);
+           }
        }
 
        heap->count++;

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Get 100% visibility into Java/.NET code with AppDynamics Lite!
It's a free troubleshooting tool designed for production.
Get down to code-level detail for bottlenecks, with <2% overhead. 
Download for free and get started troubleshooting in minutes. 
http://pubads.g.doubleclick.net/gampad/clk?id=48897031&iu=/4140/ostg.clktrk
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to