-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalysis.ts
406 lines (369 loc) · 13.2 KB
/
analysis.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
namespace analysis {
import CallNode = data.CallNode;
import SolverCall = data.SolverCall;
type AnalysisCallback = (d: ProfileData) => void;
type ZoomCallback = (d: CallNode) => void;
export interface ProfileData {
root: CallNode;
solverCalls: SolverCall[];
rows: AnalysisRow[];
currentZoom: CallNode;
aggregate: boolean;
maxScore: number;
}
export interface AnalysisRow {
function: string;
node: CallNode;
allNodes: CallNode[];
score: number;
columns: number[];
}
export interface AnalysisColumn {
type: string;
column: string;
name: string;
score: boolean;
description?: string;
}
var options = {
aggregate: false,
signatures: false,
collapseRosette: false,
collapseSolver: false,
columns: <AnalysisColumn[]>[],
contextDepth: 0,
histogramBins: 100
};
var analysisCallbacks: AnalysisCallback[] = [];
var zoomCallbacks: ZoomCallback[] = [];
var columnDenominators: Map<number> = {};
var currentState: data.ProfileState = null;
var currentZoom: CallNode = null;
// init
export function init() {
data.registerUpdateCallback(receiveDataCallback);
}
export function registerAnalysisCallback(cb: AnalysisCallback): void {
analysisCallbacks.push(cb);
}
export function registerZoomCallback(cb: ZoomCallback): void {
zoomCallbacks.push(cb);
}
export function setAggregate(x: boolean): void {
options.aggregate = x;
refresh();
}
export function setColumns(x: AnalysisColumn[]): void {
options.columns = x;
refresh();
}
export function setContextDepth(x: number): void {
options.contextDepth = x;
refresh();
}
export function setHistogramBins(x: number): void {
options.histogramBins = x;
refresh();
}
export function setSignatures(x: boolean): void {
options.signatures = x;
refresh();
}
export function setCollapseRosette(x: boolean): void {
options.collapseRosette = x;
refresh();
}
export function setCollapseSolver(x: boolean): void {
options.collapseSolver = x;
refresh();
}
export function refresh(): void {
if (currentState) receiveDataCallback(currentState);
}
function receiveDataCallback(d: data.ProfileState): void {
// save the state for reuse when options are changed
currentState = d;
// which data to actually use
let root: CallNode = d.root;
if (options.collapseRosette) {
root = collapseRosetteCalls(root);
}
if (options.collapseSolver) {
root = collapseSolverTime(root, d.solverCalls);
}
// compute scores for the data
let maxScore = computeScores(root);
// compute rows for the table
let rows = computeTableRows(root);
if (!currentZoom) {
currentZoom = root;
}
let a: ProfileData = {
root: root,
solverCalls: d.solverCalls,
rows: rows,
currentZoom: currentZoom,
aggregate: options.aggregate,
maxScore: maxScore
};
for (let cb of analysisCallbacks) {
cb(a);
}
}
export function zoomTo(root: CallNode) {
currentZoom = root;
for (let cb of zoomCallbacks) {
cb(currentZoom);
}
}
function computeScores(root: CallNode): number {
if (root === null) return 0.0;
// first pass: compute denominators
let maxValues: Map<number> = {};
let nodes = [root];
while (nodes.length > 0) {
let n = nodes.pop()!;
for (let c of options.columns) {
if (n.hasOwnProperty(c.type)) {
let k = c.type + ":" + c.column;
maxValues[k] = Math.max(maxValues[k] || 0, n[c.type][c.column] || 0);
}
}
nodes.push(...n.children);
}
// second pass: compute scores for each node
nodes.push(root);
let maxScore = 0;
while (nodes.length > 0) {
let n = nodes.pop()!;
let score = 0.0;
for (let c of options.columns) {
if (c.score && n.hasOwnProperty(c.type)) {
let k = c.type + ":" + c.column;
if (maxValues[k] > 0) {
score += (n[c.type][c.column] || 0) / maxValues[k];
}
}
}
n.score = score;
if (score > maxScore) {
maxScore = score;
}
nodes.push(...n.children);
}
return maxScore;
}
// compute scores for aggregated table rows
function updateScoresForRows(rows: AnalysisRow[]) {
if (rows.length == 0) return rows;
// first pass: compute denominators
let maxValues: number[] = [];
for (let r of rows) {
for (let i = 0; i < r.columns.length; i++) {
if (r.columns[i] >= (maxValues[i] || 0)) {
maxValues[i] = r.columns[i];
}
}
}
// second pass: compute scores for each row
for (let r of rows) {
let score = 0.0;
for (let i = 0; i < r.columns.length; i++) {
if (options.columns[i].score && maxValues[i] > 0) {
score += r.columns[i] / maxValues[i];
}
}
r.score = score;
}
}
// get the key used for aggregating nodes together according to context
function getAggregateKeyForNode(node: CallNode): string {
var context = options.contextDepth;
var key = node.name + "(" + node.source + ")";
while (context-- != 0 && node.parent) {
node = node.parent;
key = key + "\\" + node.name + "(" + node.source + ")";
}
if (options.signatures) {
key = (node.inputs["signature"] || []).join("->") + "|" +
(node.outputs["signature"] || []).join("->") + "|" +
key;
}
return key;
}
// compute the analysis rows by aggregating and scoring them
function computeTableRows(root: CallNode): AnalysisRow[] {
if (root === null) return [];
if (options.aggregate) {
// group rows by the aggregate key (wrt context)
let nodes = [root];
let ctxs: Map<CallNode[]> = {};
while (nodes.length > 0) {
let n = nodes.pop()!;
if (n) {
let k = getAggregateKeyForNode(n);
if (!ctxs.hasOwnProperty(k)) ctxs[k] = [];
ctxs[k].push(n);
nodes.push(...n.children);
}
}
// create row for each node
let allRows: AnalysisRow[] = [];
for (let k in ctxs) {
let rows = ctxs[k];
if (rows.length > 0) {
let first = rows[0]!;
// compute the row's data as the total within
let maxScore = 0.0;
let totalValues: Map<number> = {};
for (let n of rows) {
for (let c of options.columns) {
if (n.hasOwnProperty(c.type)) {
let k = c.type + ":" + c.column;
totalValues[k] = (totalValues[k] || 0) + (n[c.type][c.column] || 0);
}
}
maxScore = Math.max(maxScore, n.score);
}
let columns = [];
for (let k of options.columns) {
columns.push(totalValues[k.type + ":" + k.column]);
}
let row: AnalysisRow = {
function: first.name,
node: first,
allNodes: rows,
score: maxScore,
columns: columns
};
allRows.push(row);
}
}
updateScoresForRows(allRows); // this should really be an option
return allRows;
} else {
// create a row for each call
let nodes = [root];
let rows: AnalysisRow[] = [];
while (nodes.length > 0) {
let n = nodes.pop()!;
if (!n) continue;
let values: Map<number> = {};
for (let c of options.columns) {
if (n.hasOwnProperty(c.type)) {
let k = c.type + ":" + c.column;
values[k] = n[c.type][c.column] || 0;
}
}
let columns = [];
for (let k of options.columns) {
columns.push(values[k.type + ":" + k.column]);
}
let row: AnalysisRow = {
function: n.name,
node: n,
allNodes: [n],
score: n.score,
columns: columns
};
rows.push(row);
nodes.push(...n.children);
}
return rows;
}
}
function collapseRosetteCalls(root: CallNode): CallNode {
let rec = (node: CallNode): CallNode => {
let newExcl = undefined;
let newChildren = [];
let modified = false;
for (let c of node.children) {
let newC = rec(c); // recurse to collapse children
if (newC.name[0] == "@") {
if (typeof newExcl === "undefined") {
newExcl = {};
for (let k of Object.keys(node.excl)) {
newExcl[k] = node.excl[k];
}
}
for (let k of Object.keys(newC.excl)) {
newExcl[k] = (newExcl[k] || 0) + newC.excl[k]; // add all c's children
}
for (let cc of newC.children) {
newChildren.push(cc);
}
modified = true;
} else {
if (newC !== c) {
modified = true;
}
newChildren.push(newC); // recurse
}
}
if (modified) {
let newNode = {};
for (let k of Object.keys(node)) {
newNode[k] = node[k];
}
if (typeof newExcl !== "undefined") {
(<CallNode>newNode).excl = newExcl;
}
(<CallNode>newNode).children = newChildren;
return <CallNode>newNode;
} else {
return node;
}
}
let newRoot = rec(root);
return newRoot;
}
// remove all solver time from exclusive time
function collapseSolverTime(root: CallNode, solverCalls: SolverCall[]): CallNode {
let rec = (node: CallNode) => {
let exclDt = 0;
let newChildren = [];
let modified = false;
let start = node.start;
// do the spaces before each child
for (let c of node.children) {
let finish = c.start;
for (let sc of solverCalls) {
let scfinish = typeof sc.finish === "undefined" ? root.finish : sc.finish;
if (scfinish < start) continue;
if (finish < sc.start) break; // todo make not quadratic
let delta = Math.min(finish, scfinish) - Math.max(start, sc.start);
exclDt += delta;
}
let ret = rec(c);
newChildren.push(ret);
if (ret !== c) modified = true;
start = c.finish;
}
// do the space between last child and my end
let finish = node.finish;
for (let sc of solverCalls) {
let scfinish = typeof sc.finish === "undefined" ? root.finish : sc.finish;
if (scfinish < start) continue;
if (finish < sc.start) break;
let delta = Math.min(finish, scfinish) - Math.max(start, sc.start);
exclDt += delta;
}
if (exclDt > 0 || modified) {
let newNode = {};
for (let k of Object.keys(node)) {
newNode[k] = node[k];
}
let newExcl = {};
for (let k of Object.keys(node.excl)) {
newExcl[k] = node.excl[k];
}
(<CallNode>newNode).excl = newExcl;
(<CallNode>newNode).excl["time"] -= exclDt;
(<CallNode>newNode).children = newChildren;
node = (<CallNode>newNode);
}
return node;
}
return rec(root);
}
}