3 Copyright 2014 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
11 return Math
.round(Math
.random()*max
);
15 var dirs1
=['usr1', 'etc1', 'var1'];
16 var dirs2
=['aaa2', 'bbb2', 'ccc2', 'ddd2', 'eee2', 'fff2', 'ggg2', 'hhh2',
17 'frobozz2', 'kazaam2', 'shazam2'];
18 var dirs3
=['iii3', 'jjj3', 'kkk3', 'lll3', 'mmm3', 'nnn3', 'ooo3', 'ppp3',
19 'wonderllama3', 'excelsior3', 'src3'];
20 var filenames
=['awesome.cc', 'rad.h', 'tubular.cxx', 'cool.cc', 'groovy.h',
21 'excellent.c', 'gnarly.h', 'green.C', 'articulate.cc'];
22 //All possible types (we only see a subset in practice): 'ABbCDdGgiNpRrSsTtUuVvWw-?';
23 var nm_symbol_types
= 'trd';
27 var text
= 'var nm_data=[\n';
28 var vtablePercent
= 5;
29 for (var x
=0; x
<numGen
; x
++) {
31 dirs1
[rnd(dirs1
.length
- 1)] + '/' +
32 dirs2
[rnd(dirs2
.length
- 1)] + '/' +
33 dirs3
[rnd(dirs3
.length
- 1)] + '/' +
34 filenames
[rnd(filenames
.length
- 1)];
35 var isVtable
= Math
.floor((Math
.random()*100)+1) <= vtablePercent
;
36 var size
= rnd(maxSize
);
40 symbol_name
= 'sym' + x
.toString(16);
41 type
= nm_symbol_types
.charAt(rnd(nm_symbol_types
.length
- 1));
43 symbol_name
= 'vtable for ' + x
.toString(16);
46 text
= text
+ "{'n': '" + symbol_name
+
49 ", 'p': '" + path
+ "'},\n";
54 var treeified
= to_d3_tree(nm_data
);
55 generateDownloadLink('tree_data=' + JSON
.stringify(treeified
));
58 function generateDownloadLink(content
) {
59 var blob
= new Blob([content
], {type
: 'text/plain'});
60 var link
= document
.createElement('a');
61 link
.download
= 'generated-content.txt';
62 link
.href
= window
.URL
.createObjectURL(blob
);
63 link
.textContent
= 'Download ready, click here.';
64 link
.dataset
.downloadurl
= ['text/plain', link
.download
, link
.href
].join(':');
65 link
.onclick = function(e
) {
66 if ('disabled' in this.dataset
) { return false; }
67 link
.dataset
.disabled
= true;
68 setTimeout(function() { window
.URL
.revokeObjectURL(link
.href
); }, 1500);
70 document
.getElementById('linkcontainer').innerHTML
= '';
71 document
.getElementById('linkcontainer').appendChild(link
);
75 * This function takes in an array of nm records and converts them into a
76 * hierarchical data structure suitable for use in a d3-base treemap layout.
77 * Leaves are individual symbols. The parents of the leaves are logical
78 * groupings by common symbol-type (for BSS, read-only data, code, etc).
79 * Above this, each node represents part of a filesystem path relative
80 * to the parent node. The root node has the name '/', and represents
81 * a root (though not necessarily THE root) of a file system traversal.
82 * The root node also has a special property, 'maxDepth', to which is bound
83 * the deepest level of nesting that was found during conversion: for the
84 * record at path /a/b/c/d.foo, the maxDepth will be 6; the file 'd.foo'
85 * is at depth 4, the type-bucket is depth 5 and the symbols are depth 6.
87 function to_d3_tree(records
) {
88 var result
= {'n': '/', 'children': [], 'k': 'p'};
90 //{'n': 'symbol1', 't': 'b', 's': 1000, 'p': '/usr/local/foo/foo.cc'},
91 for (index
in records
) {
92 var record
= records
[index
];
93 var parts
= record
.p
.split("/");
96 // Walk the tree and find the file that is named by the "location"
97 // field of the record. We create any intermediate nodes required.
98 // This is directly analogous to "mkdir -p".
99 while(parts
.length
> 0) {
100 var part
= parts
.shift();
101 if (part
.length
== 0) continue;
103 node
= _mk_child(node
, part
, record
.s
);
104 node
.k
= 'p'; // p for path
106 node
.lastPathElement
= true;
108 // 'node' is now the file node. Find the symbol-type bucket.
109 node
= _mk_child(node
, record
.t
, record
.s
);
111 node
.k
= 'b'; // b for bucket
113 // 'node' is now the symbol-type bucket. Make the child entry.
114 node
= _mk_child(node
, record
.n
, record
.s
);
115 delete node
.children
;
116 node
.value
= record
.s
;
118 node
.k
= 's'; // s for symbol
121 maxDepth
= Math
.max(maxDepth
, depth
);
123 result
.maxDepth
= maxDepth
;
128 * Given a node and a name, return the child within node.children whose
129 * name matches the specified name. If necessary, a new child node is
130 * created and appended to node.children.
131 * If this method creates a new node, the 'name' attribute is set to the
132 * specified name and the 'children' attribute is an empty array, and
133 * total_size is the specified size. Otherwise, the existing node is
134 * returned and its total_size value is incremented by the specified size.
136 function _mk_child(node
, name
, size
) {
137 var child
= undefined;
138 for (child_index
in node
.children
) {
139 if (node
.children
[child_index
].n
== name
) {
140 child
= node
.children
[child_index
];
143 if (child
=== undefined) {
144 child
= {'n': name
, 'children': []};
145 node
.children
.push(child
);
151 <body style='white-space: pre; font-family: monospace;'
>
152 This script generates sample data for use in D3SymbolTreeMap, and can be used
154 <input type=button onclick='gen();' value='Generate data'
></input>
155 <div id='linkcontainer'
></div>