forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofilingModel.ts
More file actions
338 lines (288 loc) · 8.3 KB
/
profilingModel.ts
File metadata and controls
338 lines (288 loc) · 8.3 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { IV8Profile, IV8ProfileNode } from 'vs/platform/profiling/common/profiling';
// #region
// https://github.com/microsoft/vscode-js-profile-visualizer/blob/6e7401128ee860be113a916f80fcfe20ac99418e/packages/vscode-js-profile-core/src/cpu/model.ts#L4
export interface IProfileModel {
nodes: ReadonlyArray<IComputedNode>;
locations: ReadonlyArray<ILocation>;
samples: ReadonlyArray<number>;
timeDeltas: ReadonlyArray<number>;
rootPath?: string;
duration: number;
}
export interface IComputedNode {
id: number;
selfTime: number;
aggregateTime: number;
children: number[];
parent?: number;
locationId: number;
}
export interface ISourceLocation {
lineNumber: number;
columnNumber: number;
// source: Dap.Source;
relativePath?: string;
}
export interface CdpCallFrame {
functionName: string;
scriptId: string;
url: string;
lineNumber: number;
columnNumber: number;
}
export interface CdpPositionTickInfo {
line: number;
ticks: number;
}
export interface INode {
id: number;
// category: Category;
callFrame: CdpCallFrame;
src?: ISourceLocation;
}
export interface ILocation extends INode {
selfTime: number;
aggregateTime: number;
ticks: number;
}
export interface IAnnotationLocation {
callFrame: CdpCallFrame;
locations: ISourceLocation[];
}
export interface IProfileNode extends IV8ProfileNode {
locationId?: number;
positionTicks?: (CdpPositionTickInfo & {
startLocationId?: number;
endLocationId?: number;
})[];
}
export interface ICpuProfileRaw extends IV8Profile {
// $vscode?: IJsDebugAnnotations;
nodes: IProfileNode[];
}
/**
* Recursive function that computes and caches the aggregate time for the
* children of the computed now.
*/
const computeAggregateTime = (index: number, nodes: IComputedNode[]): number => {
const row = nodes[index];
if (row.aggregateTime) {
return row.aggregateTime;
}
let total = row.selfTime;
for (const child of row.children) {
total += computeAggregateTime(child, nodes);
}
return (row.aggregateTime = total);
};
const ensureSourceLocations = (profile: ICpuProfileRaw): ReadonlyArray<IAnnotationLocation> => {
let locationIdCounter = 0;
const locationsByRef = new Map<string, { id: number; callFrame: CdpCallFrame; location: ISourceLocation }>();
const getLocationIdFor = (callFrame: CdpCallFrame) => {
const ref = [
callFrame.functionName,
callFrame.url,
callFrame.scriptId,
callFrame.lineNumber,
callFrame.columnNumber,
].join(':');
const existing = locationsByRef.get(ref);
if (existing) {
return existing.id;
}
const id = locationIdCounter++;
locationsByRef.set(ref, {
id,
callFrame,
location: {
lineNumber: callFrame.lineNumber + 1,
columnNumber: callFrame.columnNumber + 1,
// source: {
// name: maybeFileUrlToPath(callFrame.url),
// path: maybeFileUrlToPath(callFrame.url),
// sourceReference: 0,
// },
},
});
return id;
};
for (const node of profile.nodes) {
node.locationId = getLocationIdFor(node.callFrame);
node.positionTicks = node.positionTicks?.map(tick => ({
...tick,
// weirdly, line numbers here are 1-based, not 0-based. The position tick
// only gives line-level granularity, so 'mark' the entire range of source
// code the tick refers to
startLocationId: getLocationIdFor({
...node.callFrame,
lineNumber: tick.line - 1,
columnNumber: 0,
}),
endLocationId: getLocationIdFor({
...node.callFrame,
lineNumber: tick.line,
columnNumber: 0,
}),
}));
}
return [...locationsByRef.values()]
.sort((a, b) => a.id - b.id)
.map(l => ({ locations: [l.location], callFrame: l.callFrame }));
};
/**
* Computes the model for the given profile.
*/
export const buildModel = (profile: ICpuProfileRaw): IProfileModel => {
if (!profile.timeDeltas || !profile.samples) {
return {
nodes: [],
locations: [],
samples: profile.samples || [],
timeDeltas: profile.timeDeltas || [],
// rootPath: profile.$vscode?.rootPath,
duration: profile.endTime - profile.startTime,
};
}
const { samples, timeDeltas } = profile;
const sourceLocations = ensureSourceLocations(profile);
const locations: ILocation[] = sourceLocations.map((l, id) => {
const src = l.locations[0]; //getBestLocation(profile, l.locations);
return {
id,
selfTime: 0,
aggregateTime: 0,
ticks: 0,
// category: categorize(l.callFrame, src),
callFrame: l.callFrame,
src,
};
});
const idMap = new Map<number /* id in profile */, number /* incrementing ID */>();
const mapId = (nodeId: number) => {
let id = idMap.get(nodeId);
if (id === undefined) {
id = idMap.size;
idMap.set(nodeId, id);
}
return id;
};
// 1. Created a sorted list of nodes. It seems that the profile always has
// incrementing IDs, although they are just not initially sorted.
const nodes = new Array<IComputedNode>(profile.nodes.length);
for (let i = 0; i < profile.nodes.length; i++) {
const node = profile.nodes[i];
// make them 0-based:
const id = mapId(node.id);
nodes[id] = {
id,
selfTime: 0,
aggregateTime: 0,
locationId: node.locationId as number,
children: node.children?.map(mapId) || [],
};
for (const child of node.positionTicks || []) {
if (child.startLocationId) {
locations[child.startLocationId].ticks += child.ticks;
}
}
}
for (const node of nodes) {
for (const child of node.children) {
nodes[child].parent = node.id;
}
}
// 2. The profile samples are the 'bottom-most' node, the currently running
// code. Sum of these in the self time.
const duration = profile.endTime - profile.startTime;
let lastNodeTime = duration - timeDeltas[0];
for (let i = 0; i < timeDeltas.length - 1; i++) {
const d = timeDeltas[i + 1];
nodes[mapId(samples[i])].selfTime += d;
lastNodeTime -= d;
}
// Add in an extra time delta for the last sample. `timeDeltas[0]` is the
// time before the first sample, and the time of the last sample is only
// derived (approximately) by the missing time in the sum of deltas. Save
// some work by calculating it here.
if (nodes.length) {
nodes[mapId(samples[timeDeltas.length - 1])].selfTime += lastNodeTime;
timeDeltas.push(lastNodeTime);
}
// 3. Add the aggregate times for all node children and locations
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
const location = locations[node.locationId];
location.aggregateTime += computeAggregateTime(i, nodes);
location.selfTime += node.selfTime;
}
return {
nodes,
locations,
samples: samples.map(mapId),
timeDeltas,
// rootPath: profile.$vscode?.rootPath,
duration,
};
};
export class BottomUpNode {
public static root() {
return new BottomUpNode({
id: -1,
selfTime: 0,
aggregateTime: 0,
ticks: 0,
callFrame: {
functionName: '(root)',
lineNumber: -1,
columnNumber: -1,
scriptId: '0',
url: '',
},
});
}
public children: { [id: number]: BottomUpNode } = {};
public aggregateTime = 0;
public selfTime = 0;
public ticks = 0;
public childrenSize = 0;
public get id() {
return this.location.id;
}
public get callFrame() {
return this.location.callFrame;
}
public get src() {
return this.location.src;
}
constructor(public readonly location: ILocation, public readonly parent?: BottomUpNode) { }
public addNode(node: IComputedNode) {
this.selfTime += node.selfTime;
this.aggregateTime += node.aggregateTime;
}
}
export const processNode = (aggregate: BottomUpNode, node: IComputedNode, model: IProfileModel, initialNode = node) => {
let child = aggregate.children[node.locationId];
if (!child) {
child = new BottomUpNode(model.locations[node.locationId], aggregate);
aggregate.childrenSize++;
aggregate.children[node.locationId] = child;
}
child.addNode(initialNode);
if (node.parent) {
processNode(child, model.nodes[node.parent], model, initialNode);
}
};
//#endregion
export interface BottomUpSample {
selfTime: number;
totalTime: number;
location: string;
url: string;
caller: { percentage: number; location: string }[];
percentage: number;
isSpecial: boolean;
}