1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
17 extern int getopt(int argc
, char *const *argv
, const char *shortopts
);
30 #include "nsTraceMalloc.h"
34 static int sort_by_direct
= 0;
35 static int js_mode
= 0;
36 static int do_tree_dump
= 0;
37 static int unified_output
= 0;
38 static char *function_dump
= NULL
;
39 static uint32_t min_subtotal
= 0;
41 static void compute_callsite_totals(tmcallsite
*site
)
45 site
->allocs
.bytes
.total
+= site
->allocs
.bytes
.direct
;
46 site
->allocs
.calls
.total
+= site
->allocs
.calls
.direct
;
47 for (kid
= site
->kids
; kid
; kid
= kid
->siblings
) {
48 compute_callsite_totals(kid
);
49 site
->allocs
.bytes
.total
+= kid
->allocs
.bytes
.total
;
50 site
->allocs
.calls
.total
+= kid
->allocs
.calls
.total
;
54 static void walk_callsite_tree(tmcallsite
*site
, int level
, int kidnum
, FILE *fp
)
57 tmgraphnode
*comp
, *pcomp
, *lib
, *plib
;
58 tmmethodnode
*meth
, *pmeth
;
59 int old_meth_low
, old_comp_low
, old_lib_low
, nkids
;
62 parent
= site
->parent
;
68 pmeth
= parent
->method
;
69 if (pmeth
&& pmeth
!= meth
) {
70 if (!meth
->graphnode
.low
) {
71 meth
->graphnode
.allocs
.bytes
.total
+= site
->allocs
.bytes
.total
;
72 meth
->graphnode
.allocs
.calls
.total
+= site
->allocs
.calls
.total
;
74 if (!tmgraphnode_connect(&(pmeth
->graphnode
), &(meth
->graphnode
), site
))
77 comp
= meth
->graphnode
.up
;
79 pcomp
= pmeth
->graphnode
.up
;
80 if (pcomp
&& pcomp
!= comp
) {
82 comp
->allocs
.bytes
.total
83 += site
->allocs
.bytes
.total
;
84 comp
->allocs
.calls
.total
85 += site
->allocs
.calls
.total
;
87 if (!tmgraphnode_connect(pcomp
, comp
, site
))
93 if (plib
&& plib
!= lib
) {
95 lib
->allocs
.bytes
.total
96 += site
->allocs
.bytes
.total
;
97 lib
->allocs
.calls
.total
98 += site
->allocs
.calls
.total
;
100 if (!tmgraphnode_connect(plib
, lib
, site
))
103 old_lib_low
= lib
->low
;
108 old_comp_low
= comp
->low
;
113 old_meth_low
= meth
->graphnode
.low
;
115 meth
->graphnode
.low
= level
;
120 fprintf(fp
, "%c%*s%3d %3d %s %lu %ld\n",
121 site
->kids
? '+' : '-', level
, "", level
, kidnum
,
122 meth
? tmmethodnode_name(meth
) : "???",
123 (unsigned long)site
->allocs
.bytes
.direct
,
124 (long)site
->allocs
.bytes
.total
);
128 for (kid
= site
->kids
; kid
; kid
= kid
->siblings
) {
129 walk_callsite_tree(kid
, level
, nkids
, fp
);
135 meth
->graphnode
.low
= 0;
153 * Linked list bubble-sort (waterson and brendan went bald hacking this).
155 * Sort the list in non-increasing order, using the expression passed as the
156 * 'lessthan' formal macro parameter. This expression should use 'curr' as
157 * the pointer to the current node (of type nodetype) and 'next' as the next
158 * node pointer. It should return true if curr is less than next, and false
161 #define BUBBLE_SORT_LINKED_LIST(listp, nodetype, lessthan) \
163 nodetype *curr, **currp, *next, **nextp, *tmp; \
166 while ((curr = *currp) != NULL && curr->next) { \
167 nextp = &curr->next; \
168 while ((next = *nextp) != NULL) { \
173 PR_ASSERT(nextp == &curr->next); \
174 curr->next = next->next; \
177 *nextp = next->next; \
178 curr->next = next->next; \
182 nextp = &curr->next; \
187 nextp = &next->next; \
189 currp = &curr->next; \
193 static int tabulate_node(PLHashEntry
*he
, int i
, void *arg
)
195 tmgraphnode
*node
= (tmgraphnode
*) he
;
196 tmgraphnode
**table
= (tmgraphnode
**) arg
;
199 BUBBLE_SORT_LINKED_LIST(&node
->down
, tmgraphnode
,
200 (curr
->allocs
.bytes
.total
< next
->allocs
.bytes
.total
));
201 return HT_ENUMERATE_NEXT
;
204 /* Sort in reverse size order, so biggest node comes first. */
205 static int node_table_compare(const void *p1
, const void *p2
)
207 const tmgraphnode
*node1
, *node2
;
210 node1
= *(const tmgraphnode
**) p1
;
211 node2
= *(const tmgraphnode
**) p2
;
212 if (sort_by_direct
) {
213 key1
= node1
->allocs
.bytes
.direct
;
214 key2
= node2
->allocs
.bytes
.direct
;
216 key1
= node1
->allocs
.bytes
.total
;
217 key2
= node2
->allocs
.bytes
.total
;
219 return (key2
< key1
) ? -1 : (key2
> key1
) ? 1 : 0;
222 static int mean_size_compare(const void *p1
, const void *p2
)
224 const tmgraphnode
*node1
, *node2
;
225 double div1
, div2
, key1
, key2
;
227 node1
= *(const tmgraphnode
**) p1
;
228 node2
= *(const tmgraphnode
**) p2
;
229 div1
= (double)node1
->allocs
.calls
.direct
;
230 div2
= (double)node2
->allocs
.calls
.direct
;
231 if (div1
== 0 || div2
== 0)
232 return (int)(div2
- div1
);
233 key1
= (double)node1
->allocs
.bytes
.direct
/ div1
;
234 key2
= (double)node2
->allocs
.bytes
.direct
/ div2
;
242 static const char *prettybig(uint32_t num
, char *buf
, size_t limit
)
244 if (num
>= 1000000000)
245 PR_snprintf(buf
, limit
, "%1.2fG", (double) num
/ 1e9
);
246 else if (num
>= 1000000)
247 PR_snprintf(buf
, limit
, "%1.2fM", (double) num
/ 1e6
);
248 else if (num
>= 1000)
249 PR_snprintf(buf
, limit
, "%1.2fK", (double) num
/ 1e3
);
251 PR_snprintf(buf
, limit
, "%lu", (unsigned long) num
);
255 static double percent(uint32_t num
, uint32_t total
)
259 return ((double) num
* 100) / (double) total
;
262 static void sort_graphlink_list(tmgraphlink
**listp
, int which
)
264 BUBBLE_SORT_LINKED_LIST(listp
, tmgraphlink
,
265 (TM_LINK_TO_EDGE(curr
, which
)->allocs
.bytes
.total
266 < TM_LINK_TO_EDGE(next
, which
)->allocs
.bytes
.total
));
269 static void dump_graphlink_list(tmgraphlink
*list
, int which
, const char *name
,
277 bytes
.direct
= bytes
.total
= 0;
278 for (link
= list
; link
; link
= link
->next
) {
279 edge
= TM_LINK_TO_EDGE(link
, which
);
280 bytes
.direct
+= edge
->allocs
.bytes
.direct
;
281 bytes
.total
+= edge
->allocs
.bytes
.total
;
286 " %s:{dbytes:%ld, tbytes:%ld, edges:[\n",
287 name
, (long) bytes
.direct
, (long) bytes
.total
);
288 for (link
= list
; link
; link
= link
->next
) {
289 edge
= TM_LINK_TO_EDGE(link
, which
);
291 " {node:%d, dbytes:%ld, tbytes:%ld},\n",
293 (long) edge
->allocs
.bytes
.direct
,
294 (long) edge
->allocs
.bytes
.total
);
298 fputs("<td valign=top>", fp
);
299 for (link
= list
; link
; link
= link
->next
) {
300 edge
= TM_LINK_TO_EDGE(link
, which
);
302 "<a href='#%s'>%s (%1.2f%%)</a>\n",
303 tmgraphnode_name(link
->node
),
304 prettybig(edge
->allocs
.bytes
.total
, buf
, sizeof buf
),
305 percent(edge
->allocs
.bytes
.total
, bytes
.total
));
311 static void dump_graph(tmreader
*tmr
, PLHashTable
*hashtbl
, const char *varname
,
312 const char *title
, FILE *fp
)
315 tmgraphnode
**table
, *node
;
318 char buf1
[16], buf2
[16], buf3
[16], buf4
[16];
320 count
= hashtbl
->nentries
;
321 table
= (tmgraphnode
**) malloc(count
* sizeof(tmgraphnode
*));
326 PL_HashTableEnumerateEntries(hashtbl
, tabulate_node
, table
);
327 qsort(table
, count
, sizeof(tmgraphnode
*), node_table_compare
);
328 for (i
= 0; i
< count
; i
++)
333 "var %s = {\n name:'%s', title:'%s', nodes:[\n",
334 varname
, varname
, title
);
342 "<th>Total/Direct (percents)</th>"
343 "<th>Allocations</th>"
350 for (i
= 0; i
< count
; i
++) {
351 /* Don't bother with truly puny nodes. */
353 if (node
->allocs
.bytes
.total
< min_subtotal
)
356 name
= tmgraphnode_name(node
);
359 " {name:'%s', dbytes:%ld, tbytes:%ld,"
360 " dallocs:%ld, tallocs:%ld,\n",
362 (long) node
->allocs
.bytes
.direct
,
363 (long) node
->allocs
.bytes
.total
,
364 (long) node
->allocs
.calls
.direct
,
365 (long) node
->allocs
.calls
.total
);
367 namelen
= strlen(name
);
370 "<td valign=top><a name='%s'>%.*s%s</a></td>",
372 (namelen
> 40) ? 40 : (int)namelen
, name
,
373 (namelen
> 40) ? "<i>...</i>" : "");
376 "<td valign=top><a href='#%s'><i>down</i></a></td>",
377 tmgraphnode_name(node
->down
));
379 fputs("<td></td>", fp
);
383 "<td valign=top><a href='#%s'><i>next</i></a></td>",
384 tmgraphnode_name(node
->next
));
386 fputs("<td></td>", fp
);
389 "<td valign=top>%s/%s (%1.2f%%/%1.2f%%)</td>"
390 "<td valign=top>%s/%s (%1.2f%%/%1.2f%%)</td>",
391 prettybig(node
->allocs
.bytes
.total
, buf1
, sizeof buf1
),
392 prettybig(node
->allocs
.bytes
.direct
, buf2
, sizeof buf2
),
393 percent(node
->allocs
.bytes
.total
,
394 tmr
->calltree_root
.allocs
.bytes
.total
),
395 percent(node
->allocs
.bytes
.direct
,
396 tmr
->calltree_root
.allocs
.bytes
.total
),
397 prettybig(node
->allocs
.calls
.total
, buf3
, sizeof buf3
),
398 prettybig(node
->allocs
.calls
.direct
, buf4
, sizeof buf4
),
399 percent(node
->allocs
.calls
.total
,
400 tmr
->calltree_root
.allocs
.calls
.total
),
401 percent(node
->allocs
.calls
.direct
,
402 tmr
->calltree_root
.allocs
.calls
.total
));
405 /* NB: we must use 'fin' because 'in' is a JS keyword! */
406 sort_graphlink_list(&node
->in
, TM_EDGE_IN_LINK
);
407 dump_graphlink_list(node
->in
, TM_EDGE_IN_LINK
, "fin", fp
);
408 sort_graphlink_list(&node
->out
, TM_EDGE_OUT_LINK
);
409 dump_graphlink_list(node
->out
, TM_EDGE_OUT_LINK
, "out", fp
);
414 fputs("</tr>\n", fp
);
420 fputs("</table>\n<hr>\n", fp
);
422 qsort(table
, count
, sizeof(tmgraphnode
*), mean_size_compare
);
426 "<tr><th colspan=4>Direct Allocators</th></tr>\n"
429 "<th>Mean Size</th>"
431 "<th>Allocations<th>"
435 for (i
= 0; i
< count
; i
++) {
436 double allocs
, bytes
, mean
, variance
, sigma
;
439 allocs
= (double)node
->allocs
.calls
.direct
;
443 /* Compute direct-size mean and standard deviation. */
444 bytes
= (double)node
->allocs
.bytes
.direct
;
445 mean
= bytes
/ allocs
;
446 variance
= allocs
* node
->sqsum
- bytes
* bytes
;
447 if (variance
< 0 || allocs
== 1)
450 variance
/= allocs
* (allocs
- 1);
451 sigma
= sqrt(variance
);
453 name
= tmgraphnode_name(node
);
454 namelen
= strlen(name
);
457 "<td valign=top>%.*s%s</td>"
458 "<td valign=top>%s</td>"
459 "<td valign=top>%s</td>"
460 "<td valign=top>%s</td>"
462 (namelen
> 65) ? 45 : (int)namelen
, name
,
463 (namelen
> 65) ? "<i>...</i>" : "",
464 prettybig((uint32_t)mean
, buf1
, sizeof buf1
),
465 prettybig((uint32_t)sigma
, buf2
, sizeof buf2
),
466 prettybig(node
->allocs
.calls
.direct
, buf3
, sizeof buf3
));
468 fputs("</table>\n", fp
);
474 static void my_tmevent_handler(tmreader
*tmr
, tmevent
*event
)
476 switch (event
->type
) {
481 "<p><table border=1>"
482 "<tr><th>Counter</th><th>Value</th></tr>\n"
483 "<tr><td>maximum actual stack depth</td><td align=right>%lu</td></tr>\n"
484 "<tr><td>maximum callsite tree depth</td><td align=right>%lu</td></tr>\n"
485 "<tr><td>number of parent callsites</td><td align=right>%lu</td></tr>\n"
486 "<tr><td>maximum kids per parent</td><td align=right>%lu</td></tr>\n"
487 "<tr><td>hits looking for a kid</td><td align=right>%lu</td></tr>\n"
488 "<tr><td>misses looking for a kid</td><td align=right>%lu</td></tr>\n"
489 "<tr><td>steps over other kids</td><td align=right>%lu</td></tr>\n"
490 "<tr><td>callsite recurrences</td><td align=right>%lu</td></tr>\n"
491 "<tr><td>number of stack backtraces</td><td align=right>%lu</td></tr>\n"
492 "<tr><td>backtrace failures</td><td align=right>%lu</td></tr>\n"
493 "<tr><td>backtrace malloc failures</td><td align=right>%lu</td></tr>\n"
494 "<tr><td>backtrace dladdr failures</td><td align=right>%lu</td></tr>\n"
495 "<tr><td>malloc calls</td><td align=right>%lu</td></tr>\n"
496 "<tr><td>malloc failures</td><td align=right>%lu</td></tr>\n"
497 "<tr><td>calloc calls</td><td align=right>%lu</td></tr>\n"
498 "<tr><td>calloc failures</td><td align=right>%lu</td></tr>\n"
499 "<tr><td>realloc calls</td><td align=right>%lu</td></tr>\n"
500 "<tr><td>realloc failures</td><td align=right>%lu</td></tr>\n"
501 "<tr><td>free calls</td><td align=right>%lu</td></tr>\n"
502 "<tr><td>free(null) calls</td><td align=right>%lu</td></tr>\n"
504 (unsigned long) event
->u
.stats
.tmstats
.calltree_maxstack
,
505 (unsigned long) event
->u
.stats
.tmstats
.calltree_maxdepth
,
506 (unsigned long) event
->u
.stats
.tmstats
.calltree_parents
,
507 (unsigned long) event
->u
.stats
.tmstats
.calltree_maxkids
,
508 (unsigned long) event
->u
.stats
.tmstats
.calltree_kidhits
,
509 (unsigned long) event
->u
.stats
.tmstats
.calltree_kidmisses
,
510 (unsigned long) event
->u
.stats
.tmstats
.calltree_kidsteps
,
511 (unsigned long) event
->u
.stats
.tmstats
.callsite_recurrences
,
512 (unsigned long) event
->u
.stats
.tmstats
.backtrace_calls
,
513 (unsigned long) event
->u
.stats
.tmstats
.backtrace_failures
,
514 (unsigned long) event
->u
.stats
.tmstats
.btmalloc_failures
,
515 (unsigned long) event
->u
.stats
.tmstats
.dladdr_failures
,
516 (unsigned long) event
->u
.stats
.tmstats
.malloc_calls
,
517 (unsigned long) event
->u
.stats
.tmstats
.malloc_failures
,
518 (unsigned long) event
->u
.stats
.tmstats
.calloc_calls
,
519 (unsigned long) event
->u
.stats
.tmstats
.calloc_failures
,
520 (unsigned long) event
->u
.stats
.tmstats
.realloc_calls
,
521 (unsigned long) event
->u
.stats
.tmstats
.realloc_failures
,
522 (unsigned long) event
->u
.stats
.tmstats
.free_calls
,
523 (unsigned long) event
->u
.stats
.tmstats
.null_free_calls
);
525 if (event
->u
.stats
.calltree_maxkids_parent
) {
527 tmreader_callsite(tmr
, event
->u
.stats
.calltree_maxkids_parent
);
528 if (site
&& site
->method
) {
529 fprintf(stdout
, "<p>callsite with the most kids: %s</p>",
530 tmmethodnode_name(site
->method
));
534 if (event
->u
.stats
.calltree_maxstack_top
) {
536 tmreader_callsite(tmr
, event
->u
.stats
.calltree_maxstack_top
);
537 fputs("<p>deepest callsite tree path:\n"
539 "<tr><th>Method</th><th>Offset</th></tr>\n",
543 "<tr><td>%s</td><td>0x%08lX</td></tr>\n",
544 site
->method
? tmmethodnode_name(site
->method
) : "???",
545 (unsigned long) site
->offset
);
548 fputs("</table>\n<hr>\n", stdout
);
554 int main(int argc
, char **argv
)
561 tmr
= tmreader_new(program
, NULL
);
567 while ((c
= getopt(argc
, argv
, "djtuf:m:")) != EOF
) {
582 function_dump
= optarg
;
585 min_subtotal
= atoi(optarg
);
589 "usage: %s [-dtu] [-f function-dump-filename] [-m min] [output.html]\n",
596 time_t start
= time(NULL
);
599 "<script language=\"JavaScript\">\n"
600 "function onload() {\n"
601 " document.links[0].__proto__.onmouseover = new Function("
603 " this.href.substring(this.href.lastIndexOf('#') + 1)\");\n"
606 fprintf(stdout
, "%s starting at %s", program
, ctime(&start
));
613 if (tmreader_eventloop(tmr
, "-", my_tmevent_handler
) <= 0)
616 for (i
= j
= 0; i
< argc
; i
++) {
617 fp
= fopen(argv
[i
], "r");
619 fprintf(stderr
, "%s: can't open %s: %s\n",
620 program
, argv
[i
], strerror(errno
));
623 rv
= tmreader_eventloop(tmr
, argv
[i
], my_tmevent_handler
);
634 compute_callsite_totals(&tmr
->calltree_root
);
635 walk_callsite_tree(&tmr
->calltree_root
, 0, 0, stdout
);
639 "<script language='javascript'>\n"
640 "// direct and total byte and allocator-call counts\n"
641 "var dbytes = %ld, tbytes = %ld,"
642 " dallocs = %ld, tallocs = %ld;\n",
643 (long) tmr
->calltree_root
.allocs
.bytes
.direct
,
644 (long) tmr
->calltree_root
.allocs
.bytes
.total
,
645 (long) tmr
->calltree_root
.allocs
.calls
.direct
,
646 (long) tmr
->calltree_root
.allocs
.calls
.total
);
649 dump_graph(tmr
, tmr
->libraries
, "libraries", "Library", stdout
);
651 fputs("<hr>\n", stdout
);
653 dump_graph(tmr
, tmr
->components
, "classes", "Class or Component", stdout
);
654 if (js_mode
|| unified_output
|| function_dump
) {
655 if (js_mode
|| unified_output
|| strcmp(function_dump
, "-") == 0) {
662 fstat(fileno(stdout
), &sb
);
663 if (stat(function_dump
, &fsb
) == 0 &&
664 fsb
.st_dev
== sb
.st_dev
&& fsb
.st_ino
== sb
.st_ino
) {
668 fp
= fopen(function_dump
, "w");
670 fprintf(stderr
, "%s: can't open %s: %s\n",
671 program
, function_dump
, strerror(errno
));
677 dump_graph(tmr
, tmr
->methods
, "methods", "Function or Method", fp
);
682 fputs("function viewnode(graph, index) {\n"
683 " view.location = viewsrc();\n"
685 "function viewnodelink(graph, index) {\n"
686 " var node = graph.nodes[index];\n"
687 " return '<a href=\"javascript:viewnode('"
688 " + graph.name.quote() + ', ' + node.sort"
689 " + ')\" onmouseover=' + node.name.quote() + '>'"
690 " + node.name + '</a>';\n"
692 "function search(expr) {\n"
693 " var re = new RegExp(expr);\n"
695 " var graphs = [libraries, classes, methods]\n"
697 " for (var n = 0; n < (nodes = graphs[n].nodes).length; n++) {\n"
698 " for (var i = 0; i < nodes.length; i++) {\n"
699 " if (re.test(nodes[i].name))\n"
700 " src += viewnodelink(graph, i) + '\\n';\n"
703 " view.location = viewsrc();\n"
705 "function ctrlsrc() {\n"
706 " return \"<form>\\n"
707 "search: <input size=40 onchange='search(this.value)'>\\n"
710 "function viewsrc() {\n"
714 "<frameset rows='10%,*'>\n"
715 " <frame name='ctrl' src='javascript:top.ctrlsrc()'>\n"
716 " <frame name='view' src='javascript:top.viewsrc()'>\n"