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
);
29 #include "nsTraceMalloc.h"
33 static int sort_by_direct
= 0;
34 static int js_mode
= 0;
35 static int do_tree_dump
= 0;
36 static int unified_output
= 0;
37 static char *function_dump
= NULL
;
38 static uint32_t min_subtotal
= 0;
40 static void compute_callsite_totals(tmcallsite
*site
)
44 site
->allocs
.bytes
.total
+= site
->allocs
.bytes
.direct
;
45 site
->allocs
.calls
.total
+= site
->allocs
.calls
.direct
;
46 for (kid
= site
->kids
; kid
; kid
= kid
->siblings
) {
47 compute_callsite_totals(kid
);
48 site
->allocs
.bytes
.total
+= kid
->allocs
.bytes
.total
;
49 site
->allocs
.calls
.total
+= kid
->allocs
.calls
.total
;
53 static void walk_callsite_tree(tmcallsite
*site
, int level
, int kidnum
, FILE *fp
)
56 tmgraphnode
*comp
, *pcomp
, *lib
, *plib
;
57 tmmethodnode
*meth
, *pmeth
;
58 int old_meth_low
, old_comp_low
, old_lib_low
, nkids
;
61 parent
= site
->parent
;
67 pmeth
= parent
->method
;
68 if (pmeth
&& pmeth
!= meth
) {
69 if (!meth
->graphnode
.low
) {
70 meth
->graphnode
.allocs
.bytes
.total
+= site
->allocs
.bytes
.total
;
71 meth
->graphnode
.allocs
.calls
.total
+= site
->allocs
.calls
.total
;
73 if (!tmgraphnode_connect(&(pmeth
->graphnode
), &(meth
->graphnode
), site
))
76 comp
= meth
->graphnode
.up
;
78 pcomp
= pmeth
->graphnode
.up
;
79 if (pcomp
&& pcomp
!= comp
) {
81 comp
->allocs
.bytes
.total
82 += site
->allocs
.bytes
.total
;
83 comp
->allocs
.calls
.total
84 += site
->allocs
.calls
.total
;
86 if (!tmgraphnode_connect(pcomp
, comp
, site
))
92 if (plib
&& plib
!= lib
) {
94 lib
->allocs
.bytes
.total
95 += site
->allocs
.bytes
.total
;
96 lib
->allocs
.calls
.total
97 += site
->allocs
.calls
.total
;
99 if (!tmgraphnode_connect(plib
, lib
, site
))
102 old_lib_low
= lib
->low
;
107 old_comp_low
= comp
->low
;
112 old_meth_low
= meth
->graphnode
.low
;
114 meth
->graphnode
.low
= level
;
119 fprintf(fp
, "%c%*s%3d %3d %s %lu %ld\n",
120 site
->kids
? '+' : '-', level
, "", level
, kidnum
,
121 meth
? tmmethodnode_name(meth
) : "???",
122 (unsigned long)site
->allocs
.bytes
.direct
,
123 (long)site
->allocs
.bytes
.total
);
127 for (kid
= site
->kids
; kid
; kid
= kid
->siblings
) {
128 walk_callsite_tree(kid
, level
, nkids
, fp
);
134 meth
->graphnode
.low
= 0;
152 * Linked list bubble-sort (waterson and brendan went bald hacking this).
154 * Sort the list in non-increasing order, using the expression passed as the
155 * 'lessthan' formal macro parameter. This expression should use 'curr' as
156 * the pointer to the current node (of type nodetype) and 'next' as the next
157 * node pointer. It should return true if curr is less than next, and false
160 #define BUBBLE_SORT_LINKED_LIST(listp, nodetype, lessthan) \
162 nodetype *curr, **currp, *next, **nextp, *tmp; \
165 while ((curr = *currp) != NULL && curr->next) { \
166 nextp = &curr->next; \
167 while ((next = *nextp) != NULL) { \
172 PR_ASSERT(nextp == &curr->next); \
173 curr->next = next->next; \
176 *nextp = next->next; \
177 curr->next = next->next; \
181 nextp = &curr->next; \
186 nextp = &next->next; \
188 currp = &curr->next; \
192 static int tabulate_node(PLHashEntry
*he
, int i
, void *arg
)
194 tmgraphnode
*node
= (tmgraphnode
*) he
;
195 tmgraphnode
**table
= (tmgraphnode
**) arg
;
198 BUBBLE_SORT_LINKED_LIST(&node
->down
, tmgraphnode
,
199 (curr
->allocs
.bytes
.total
< next
->allocs
.bytes
.total
));
200 return HT_ENUMERATE_NEXT
;
203 /* Sort in reverse size order, so biggest node comes first. */
204 static int node_table_compare(const void *p1
, const void *p2
)
206 const tmgraphnode
*node1
, *node2
;
209 node1
= *(const tmgraphnode
**) p1
;
210 node2
= *(const tmgraphnode
**) p2
;
211 if (sort_by_direct
) {
212 key1
= node1
->allocs
.bytes
.direct
;
213 key2
= node2
->allocs
.bytes
.direct
;
215 key1
= node1
->allocs
.bytes
.total
;
216 key2
= node2
->allocs
.bytes
.total
;
218 return (key2
< key1
) ? -1 : (key2
> key1
) ? 1 : 0;
221 static int mean_size_compare(const void *p1
, const void *p2
)
223 const tmgraphnode
*node1
, *node2
;
224 double div1
, div2
, key1
, key2
;
226 node1
= *(const tmgraphnode
**) p1
;
227 node2
= *(const tmgraphnode
**) p2
;
228 div1
= (double)node1
->allocs
.calls
.direct
;
229 div2
= (double)node2
->allocs
.calls
.direct
;
230 if (div1
== 0 || div2
== 0)
231 return (int)(div2
- div1
);
232 key1
= (double)node1
->allocs
.bytes
.direct
/ div1
;
233 key2
= (double)node2
->allocs
.bytes
.direct
/ div2
;
241 static const char *prettybig(uint32_t num
, char *buf
, size_t limit
)
243 if (num
>= 1000000000)
244 PR_snprintf(buf
, limit
, "%1.2fG", (double) num
/ 1e9
);
245 else if (num
>= 1000000)
246 PR_snprintf(buf
, limit
, "%1.2fM", (double) num
/ 1e6
);
247 else if (num
>= 1000)
248 PR_snprintf(buf
, limit
, "%1.2fK", (double) num
/ 1e3
);
250 PR_snprintf(buf
, limit
, "%lu", (unsigned long) num
);
254 static double percent(uint32_t num
, uint32_t total
)
258 return ((double) num
* 100) / (double) total
;
261 static void sort_graphlink_list(tmgraphlink
**listp
, int which
)
263 BUBBLE_SORT_LINKED_LIST(listp
, tmgraphlink
,
264 (TM_LINK_TO_EDGE(curr
, which
)->allocs
.bytes
.total
265 < TM_LINK_TO_EDGE(next
, which
)->allocs
.bytes
.total
));
268 static void dump_graphlink_list(tmgraphlink
*list
, int which
, const char *name
,
276 bytes
.direct
= bytes
.total
= 0;
277 for (link
= list
; link
; link
= link
->next
) {
278 edge
= TM_LINK_TO_EDGE(link
, which
);
279 bytes
.direct
+= edge
->allocs
.bytes
.direct
;
280 bytes
.total
+= edge
->allocs
.bytes
.total
;
285 " %s:{dbytes:%ld, tbytes:%ld, edges:[\n",
286 name
, (long) bytes
.direct
, (long) bytes
.total
);
287 for (link
= list
; link
; link
= link
->next
) {
288 edge
= TM_LINK_TO_EDGE(link
, which
);
290 " {node:%d, dbytes:%ld, tbytes:%ld},\n",
292 (long) edge
->allocs
.bytes
.direct
,
293 (long) edge
->allocs
.bytes
.total
);
297 fputs("<td valign=top>", fp
);
298 for (link
= list
; link
; link
= link
->next
) {
299 edge
= TM_LINK_TO_EDGE(link
, which
);
301 "<a href='#%s'>%s (%1.2f%%)</a>\n",
302 tmgraphnode_name(link
->node
),
303 prettybig(edge
->allocs
.bytes
.total
, buf
, sizeof buf
),
304 percent(edge
->allocs
.bytes
.total
, bytes
.total
));
310 static void dump_graph(tmreader
*tmr
, PLHashTable
*hashtbl
, const char *varname
,
311 const char *title
, FILE *fp
)
314 tmgraphnode
**table
, *node
;
317 char buf1
[16], buf2
[16], buf3
[16], buf4
[16];
319 count
= hashtbl
->nentries
;
320 table
= (tmgraphnode
**) malloc(count
* sizeof(tmgraphnode
*));
325 PL_HashTableEnumerateEntries(hashtbl
, tabulate_node
, table
);
326 qsort(table
, count
, sizeof(tmgraphnode
*), node_table_compare
);
327 for (i
= 0; i
< count
; i
++)
332 "var %s = {\n name:'%s', title:'%s', nodes:[\n",
333 varname
, varname
, title
);
341 "<th>Total/Direct (percents)</th>"
342 "<th>Allocations</th>"
349 for (i
= 0; i
< count
; i
++) {
350 /* Don't bother with truly puny nodes. */
352 if (node
->allocs
.bytes
.total
< min_subtotal
)
355 name
= tmgraphnode_name(node
);
358 " {name:'%s', dbytes:%ld, tbytes:%ld,"
359 " dallocs:%ld, tallocs:%ld,\n",
361 (long) node
->allocs
.bytes
.direct
,
362 (long) node
->allocs
.bytes
.total
,
363 (long) node
->allocs
.calls
.direct
,
364 (long) node
->allocs
.calls
.total
);
366 namelen
= strlen(name
);
369 "<td valign=top><a name='%s'>%.*s%s</a></td>",
371 (namelen
> 40) ? 40 : (int)namelen
, name
,
372 (namelen
> 40) ? "<i>...</i>" : "");
375 "<td valign=top><a href='#%s'><i>down</i></a></td>",
376 tmgraphnode_name(node
->down
));
378 fputs("<td></td>", fp
);
382 "<td valign=top><a href='#%s'><i>next</i></a></td>",
383 tmgraphnode_name(node
->next
));
385 fputs("<td></td>", fp
);
388 "<td valign=top>%s/%s (%1.2f%%/%1.2f%%)</td>"
389 "<td valign=top>%s/%s (%1.2f%%/%1.2f%%)</td>",
390 prettybig(node
->allocs
.bytes
.total
, buf1
, sizeof buf1
),
391 prettybig(node
->allocs
.bytes
.direct
, buf2
, sizeof buf2
),
392 percent(node
->allocs
.bytes
.total
,
393 tmr
->calltree_root
.allocs
.bytes
.total
),
394 percent(node
->allocs
.bytes
.direct
,
395 tmr
->calltree_root
.allocs
.bytes
.total
),
396 prettybig(node
->allocs
.calls
.total
, buf3
, sizeof buf3
),
397 prettybig(node
->allocs
.calls
.direct
, buf4
, sizeof buf4
),
398 percent(node
->allocs
.calls
.total
,
399 tmr
->calltree_root
.allocs
.calls
.total
),
400 percent(node
->allocs
.calls
.direct
,
401 tmr
->calltree_root
.allocs
.calls
.total
));
404 /* NB: we must use 'fin' because 'in' is a JS keyword! */
405 sort_graphlink_list(&node
->in
, TM_EDGE_IN_LINK
);
406 dump_graphlink_list(node
->in
, TM_EDGE_IN_LINK
, "fin", fp
);
407 sort_graphlink_list(&node
->out
, TM_EDGE_OUT_LINK
);
408 dump_graphlink_list(node
->out
, TM_EDGE_OUT_LINK
, "out", fp
);
413 fputs("</tr>\n", fp
);
419 fputs("</table>\n<hr>\n", fp
);
421 qsort(table
, count
, sizeof(tmgraphnode
*), mean_size_compare
);
425 "<tr><th colspan=4>Direct Allocators</th></tr>\n"
428 "<th>Mean Size</th>"
430 "<th>Allocations<th>"
434 for (i
= 0; i
< count
; i
++) {
435 double allocs
, bytes
, mean
, variance
, sigma
;
438 allocs
= (double)node
->allocs
.calls
.direct
;
442 /* Compute direct-size mean and standard deviation. */
443 bytes
= (double)node
->allocs
.bytes
.direct
;
444 mean
= bytes
/ allocs
;
445 variance
= allocs
* node
->sqsum
- bytes
* bytes
;
446 if (variance
< 0 || allocs
== 1)
449 variance
/= allocs
* (allocs
- 1);
450 sigma
= sqrt(variance
);
452 name
= tmgraphnode_name(node
);
453 namelen
= strlen(name
);
456 "<td valign=top>%.*s%s</td>"
457 "<td valign=top>%s</td>"
458 "<td valign=top>%s</td>"
459 "<td valign=top>%s</td>"
461 (namelen
> 65) ? 45 : (int)namelen
, name
,
462 (namelen
> 65) ? "<i>...</i>" : "",
463 prettybig((uint32_t)mean
, buf1
, sizeof buf1
),
464 prettybig((uint32_t)sigma
, buf2
, sizeof buf2
),
465 prettybig(node
->allocs
.calls
.direct
, buf3
, sizeof buf3
));
467 fputs("</table>\n", fp
);
473 static void my_tmevent_handler(tmreader
*tmr
, tmevent
*event
)
475 switch (event
->type
) {
480 "<p><table border=1>"
481 "<tr><th>Counter</th><th>Value</th></tr>\n"
482 "<tr><td>maximum actual stack depth</td><td align=right>%lu</td></tr>\n"
483 "<tr><td>maximum callsite tree depth</td><td align=right>%lu</td></tr>\n"
484 "<tr><td>number of parent callsites</td><td align=right>%lu</td></tr>\n"
485 "<tr><td>maximum kids per parent</td><td align=right>%lu</td></tr>\n"
486 "<tr><td>hits looking for a kid</td><td align=right>%lu</td></tr>\n"
487 "<tr><td>misses looking for a kid</td><td align=right>%lu</td></tr>\n"
488 "<tr><td>steps over other kids</td><td align=right>%lu</td></tr>\n"
489 "<tr><td>callsite recurrences</td><td align=right>%lu</td></tr>\n"
490 "<tr><td>number of stack backtraces</td><td align=right>%lu</td></tr>\n"
491 "<tr><td>backtrace failures</td><td align=right>%lu</td></tr>\n"
492 "<tr><td>backtrace malloc failures</td><td align=right>%lu</td></tr>\n"
493 "<tr><td>backtrace dladdr failures</td><td align=right>%lu</td></tr>\n"
494 "<tr><td>malloc calls</td><td align=right>%lu</td></tr>\n"
495 "<tr><td>malloc failures</td><td align=right>%lu</td></tr>\n"
496 "<tr><td>calloc calls</td><td align=right>%lu</td></tr>\n"
497 "<tr><td>calloc failures</td><td align=right>%lu</td></tr>\n"
498 "<tr><td>realloc calls</td><td align=right>%lu</td></tr>\n"
499 "<tr><td>realloc failures</td><td align=right>%lu</td></tr>\n"
500 "<tr><td>free calls</td><td align=right>%lu</td></tr>\n"
501 "<tr><td>free(null) calls</td><td align=right>%lu</td></tr>\n"
503 (unsigned long) event
->u
.stats
.tmstats
.calltree_maxstack
,
504 (unsigned long) event
->u
.stats
.tmstats
.calltree_maxdepth
,
505 (unsigned long) event
->u
.stats
.tmstats
.calltree_parents
,
506 (unsigned long) event
->u
.stats
.tmstats
.calltree_maxkids
,
507 (unsigned long) event
->u
.stats
.tmstats
.calltree_kidhits
,
508 (unsigned long) event
->u
.stats
.tmstats
.calltree_kidmisses
,
509 (unsigned long) event
->u
.stats
.tmstats
.calltree_kidsteps
,
510 (unsigned long) event
->u
.stats
.tmstats
.callsite_recurrences
,
511 (unsigned long) event
->u
.stats
.tmstats
.backtrace_calls
,
512 (unsigned long) event
->u
.stats
.tmstats
.backtrace_failures
,
513 (unsigned long) event
->u
.stats
.tmstats
.btmalloc_failures
,
514 (unsigned long) event
->u
.stats
.tmstats
.dladdr_failures
,
515 (unsigned long) event
->u
.stats
.tmstats
.malloc_calls
,
516 (unsigned long) event
->u
.stats
.tmstats
.malloc_failures
,
517 (unsigned long) event
->u
.stats
.tmstats
.calloc_calls
,
518 (unsigned long) event
->u
.stats
.tmstats
.calloc_failures
,
519 (unsigned long) event
->u
.stats
.tmstats
.realloc_calls
,
520 (unsigned long) event
->u
.stats
.tmstats
.realloc_failures
,
521 (unsigned long) event
->u
.stats
.tmstats
.free_calls
,
522 (unsigned long) event
->u
.stats
.tmstats
.null_free_calls
);
524 if (event
->u
.stats
.calltree_maxkids_parent
) {
526 tmreader_callsite(tmr
, event
->u
.stats
.calltree_maxkids_parent
);
527 if (site
&& site
->method
) {
528 fprintf(stdout
, "<p>callsite with the most kids: %s</p>",
529 tmmethodnode_name(site
->method
));
533 if (event
->u
.stats
.calltree_maxstack_top
) {
535 tmreader_callsite(tmr
, event
->u
.stats
.calltree_maxstack_top
);
536 fputs("<p>deepest callsite tree path:\n"
538 "<tr><th>Method</th><th>Offset</th></tr>\n",
542 "<tr><td>%s</td><td>0x%08lX</td></tr>\n",
543 site
->method
? tmmethodnode_name(site
->method
) : "???",
544 (unsigned long) site
->offset
);
547 fputs("</table>\n<hr>\n", stdout
);
553 int main(int argc
, char **argv
)
560 tmr
= tmreader_new(program
, NULL
);
566 while ((c
= getopt(argc
, argv
, "djtuf:m:")) != EOF
) {
581 function_dump
= optarg
;
584 min_subtotal
= atoi(optarg
);
588 "usage: %s [-dtu] [-f function-dump-filename] [-m min] [output.html]\n",
595 time_t start
= time(NULL
);
598 "<script language=\"JavaScript\">\n"
599 "function onload() {\n"
600 " document.links[0].__proto__.onmouseover = new Function("
602 " this.href.substring(this.href.lastIndexOf('#') + 1)\");\n"
605 fprintf(stdout
, "%s starting at %s", program
, ctime(&start
));
612 if (tmreader_eventloop(tmr
, "-", my_tmevent_handler
) <= 0)
615 for (i
= j
= 0; i
< argc
; i
++) {
616 fp
= fopen(argv
[i
], "r");
618 fprintf(stderr
, "%s: can't open %s: %s\n",
619 program
, argv
[i
], strerror(errno
));
622 rv
= tmreader_eventloop(tmr
, argv
[i
], my_tmevent_handler
);
633 compute_callsite_totals(&tmr
->calltree_root
);
634 walk_callsite_tree(&tmr
->calltree_root
, 0, 0, stdout
);
638 "<script language='javascript'>\n"
639 "// direct and total byte and allocator-call counts\n"
640 "var dbytes = %ld, tbytes = %ld,"
641 " dallocs = %ld, tallocs = %ld;\n",
642 (long) tmr
->calltree_root
.allocs
.bytes
.direct
,
643 (long) tmr
->calltree_root
.allocs
.bytes
.total
,
644 (long) tmr
->calltree_root
.allocs
.calls
.direct
,
645 (long) tmr
->calltree_root
.allocs
.calls
.total
);
648 dump_graph(tmr
, tmr
->libraries
, "libraries", "Library", stdout
);
650 fputs("<hr>\n", stdout
);
652 dump_graph(tmr
, tmr
->components
, "classes", "Class or Component", stdout
);
653 if (js_mode
|| unified_output
|| function_dump
) {
654 if (js_mode
|| unified_output
|| strcmp(function_dump
, "-") == 0) {
661 fstat(fileno(stdout
), &sb
);
662 if (stat(function_dump
, &fsb
) == 0 &&
663 fsb
.st_dev
== sb
.st_dev
&& fsb
.st_ino
== sb
.st_ino
) {
667 fp
= fopen(function_dump
, "w");
669 fprintf(stderr
, "%s: can't open %s: %s\n",
670 program
, function_dump
, strerror(errno
));
676 dump_graph(tmr
, tmr
->methods
, "methods", "Function or Method", fp
);
681 fputs("function viewnode(graph, index) {\n"
682 " view.location = viewsrc();\n"
684 "function viewnodelink(graph, index) {\n"
685 " var node = graph.nodes[index];\n"
686 " return '<a href=\"javascript:viewnode('"
687 " + graph.name.quote() + ', ' + node.sort"
688 " + ')\" onmouseover=' + node.name.quote() + '>'"
689 " + node.name + '</a>';\n"
691 "function search(expr) {\n"
692 " var re = new RegExp(expr);\n"
694 " var graphs = [libraries, classes, methods]\n"
696 " for (var n = 0; n < (nodes = graphs[n].nodes).length; n++) {\n"
697 " for (var i = 0; i < nodes.length; i++) {\n"
698 " if (re.test(nodes[i].name))\n"
699 " src += viewnodelink(graph, i) + '\\n';\n"
702 " view.location = viewsrc();\n"
704 "function ctrlsrc() {\n"
705 " return \"<form>\\n"
706 "search: <input size=40 onchange='search(this.value)'>\\n"
709 "function viewsrc() {\n"
713 "<frameset rows='10%,*'>\n"
714 " <frame name='ctrl' src='javascript:top.ctrlsrc()'>\n"
715 " <frame name='view' src='javascript:top.viewsrc()'>\n"