forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
old-cleopatra-profile-format.js
393 lines (363 loc) · 11.7 KB
/
old-cleopatra-profile-format.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import { UniqueStringArray } from '../utils/unique-string-array';
import { resourceTypes } from './profile-data';
import { CURRENT_VERSION } from './gecko-profile-versioning.js';
/**
* The "old cleopatra format" is the profile format that was used by the
* cleopatra version before the big rewrite. Profiles of this format are still
* in the profile store, and there are links to those profiles strewn across
* bugzilla. We want to be able to display those profiles.
* This file's purpose is to convert "old cleopatra" profiles into
* processed profiles of "processed profile format" version zero.
* The result will be run through the processed profile format
* compatibility conversion. Consequently, when the processed profile
* format changes, this file does not need to be touched and instead we will
* automatically take advantage of the process profile format conversion.
*
* A lot of this code will remind you of very similar code in
* process-profile.js. However, we intentionally do not share code with it:
* We want process-profile to be exclusively concerned with converting the
* most recent Gecko profile format into the most recent processed profile
* format.
* Some of the code below is basically a snapshot of the processing code as
* it was before the versioning scheme was introduced.
*/
export function isOldCleopatraFormat(profile: Object): boolean {
return (
'format' in profile &&
profile.format === 'profileJSONWithSymbolicationTable,1'
);
}
type OldCleopatraMarker = {
name: string,
data: ?Object,
time: number,
};
type OldCleopatraSample = {
frames: number[],
extraInfo: Object,
};
type OldCleopatraProfileThread = {
samples: OldCleopatraSample[],
markers: OldCleopatraMarker[],
name: string,
};
type OldCleopatraProfileJSON =
| {
threads: { [threadIndex: string]: OldCleopatraProfileThread },
}
| OldCleopatraSample[];
type OldCleopatraProfile = {
format: 'profileJSONWithSymbolicationTable,1',
meta: Object,
profileJSON: OldCleopatraProfileJSON,
symbolicationTable: { [symbolIndex: string]: string },
};
function _getRealScriptURI(url: ?string): ?string {
if (url) {
const urls = url.split(' -> ');
return urls[urls.length - 1];
}
return url;
}
function _cleanFunctionName(functionName: string) {
const ignoredPrefix = 'non-virtual thunk to ';
if (functionName.startsWith && functionName.startsWith(ignoredPrefix)) {
return functionName.substr(ignoredPrefix.length);
}
return functionName;
}
function _convertThread(
thread: OldCleopatraProfileThread,
interval: number,
symbolicationTable
) {
const stringTable = new UniqueStringArray(symbolicationTable);
const frameTable = {
length: 0,
category: [],
location: [],
implementation: [],
line: [],
optimizations: [],
func: undefined,
address: undefined,
};
const stackTable = {
length: 0,
frame: [],
prefix: [],
};
const samples = {
length: 0,
responsiveness: [],
rss: [],
stack: [],
time: [],
uss: [],
};
const markers = {
length: 0,
data: [],
name: [],
time: [],
};
const frameMap = new Map();
const stackMap = new Map();
function convertStack(frames) {
// We map every stack frame to a frame.
// Then we walk the stack, creating "stacks" (= (prefix stack, frame) pairs)
// as needed, and arrive at the sample's stackIndex.
let prefix = null;
for (let i = 0; i < frames.length; i++) {
const frameSymbolicationTableIndex = frames[i];
let frameIndex = frameMap.get(frameSymbolicationTableIndex);
if (frameIndex === undefined) {
frameIndex = frameTable.length++;
frameTable.location[frameIndex] = frameSymbolicationTableIndex;
frameTable.category[frameIndex] = null;
frameTable.implementation[frameIndex] = null;
frameTable.line[frameIndex] = null;
frameTable.optimizations[frameIndex] = null;
frameMap.set(frameSymbolicationTableIndex, frameIndex);
}
const stackMapKey =
prefix !== null ? `${prefix}:${frameIndex}` : `:${frameIndex}`;
let stackIndex = stackMap.get(stackMapKey);
if (stackIndex === undefined) {
stackIndex = stackTable.length++;
stackTable.prefix[stackIndex] = prefix;
stackTable.frame[stackIndex] = frameIndex;
stackMap.set(stackMapKey, stackIndex);
}
prefix = stackIndex;
}
return prefix;
}
let lastSampleTime = 0;
for (let i = 0; i < thread.samples.length; i++) {
const sample = thread.samples[i];
// sample has the shape: {
// frames: [symbolicationTableIndices for the stack frames]
// extraInfo: {
// responsiveness,
// time,
// }
// }
const stackIndex = convertStack(sample.frames);
const sampleIndex = samples.length++;
samples.stack[sampleIndex] = stackIndex;
const hasTime =
'time' in sample.extraInfo && typeof sample.extraInfo.time === 'number';
const sampleTime = hasTime
? sample.extraInfo.time
: lastSampleTime + interval;
samples.time[sampleIndex] = sampleTime;
lastSampleTime = sampleTime;
samples.responsiveness[sampleIndex] =
'responsiveness' in sample.extraInfo
? sample.extraInfo.responsiveness
: null;
samples.rss[sampleIndex] =
'rss' in sample.extraInfo ? sample.extraInfo.rss : null;
samples.uss[sampleIndex] =
'uss' in sample.extraInfo ? sample.extraInfo.uss : null;
}
for (let i = 0; i < thread.markers.length; i++) {
const marker = thread.markers[i];
const markerIndex = markers.length++;
const data = marker.data;
if (data && 'stack' in data) {
// data.stack is an array of strings
const stackIndex = convertStack(
data.stack.map(s => stringTable.indexForString(s))
);
data.stack = {
markers: { schema: { name: 0, time: 1, data: 2 }, data: [] },
name: 'SyncProfile',
samples: {
schema: {
stack: 0,
time: 1,
responsiveness: 2,
rss: 3,
uss: 4,
frameNumber: 5,
power: 6,
},
data: [[stackIndex, marker.time]],
},
};
}
markers.data[markerIndex] = marker.data;
markers.name[markerIndex] = stringTable.indexForString(marker.name);
markers.time[markerIndex] = marker.time;
}
const funcTable = {
length: 0,
name: [],
resource: [],
address: [],
isJS: [],
};
function addFunc(name, resource, address, isJS) {
const index = funcTable.length++;
funcTable.name[index] = name;
funcTable.resource[index] = resource;
funcTable.address[index] = address;
funcTable.isJS[index] = isJS;
}
const resourceTable = {
length: 0,
type: [],
name: [],
lib: [],
icon: [],
addonId: [],
};
function addLibResource(name, lib) {
const index = resourceTable.length++;
resourceTable.type[index] = resourceTypes.library;
resourceTable.name[index] = name;
resourceTable.lib[index] = lib;
}
function addUrlResource(urlStringIndex) {
const index = resourceTable.length++;
resourceTable.type[index] = resourceTypes.url;
resourceTable.name[index] = urlStringIndex;
}
const libNameToResourceIndex = new Map();
const urlToResourceIndex = new Map();
const stringTableIndexToNewFuncIndex = new Map();
frameTable.func = frameTable.location.map(locationIndex => {
let funcNameIndex = locationIndex;
let funcIndex = stringTableIndexToNewFuncIndex.get(funcNameIndex);
if (funcIndex !== undefined) {
return funcIndex;
}
let resourceIndex = -1;
const addressRelativeToLib = -1;
let isJS = false;
const locationString = stringTable.getString(funcNameIndex);
if (!locationString.startsWith('0x')) {
const cppMatch =
/^(.*) \(in ([^)]*)\) (\+ [0-9]+)$/.exec(locationString) ||
/^(.*) \(in ([^)]*)\) (\(.*:.*\))$/.exec(locationString) ||
/^(.*) \(in ([^)]*)\)$/.exec(locationString);
if (cppMatch) {
funcNameIndex = stringTable.indexForString(
_cleanFunctionName(cppMatch[1])
);
const libraryNameStringIndex = stringTable.indexForString(cppMatch[2]);
funcIndex = stringTableIndexToNewFuncIndex.get(funcNameIndex);
if (funcIndex !== undefined) {
return funcIndex;
}
if (libNameToResourceIndex.has(libraryNameStringIndex)) {
resourceIndex = libNameToResourceIndex.get(libraryNameStringIndex);
} else {
resourceIndex = resourceTable.length;
libNameToResourceIndex.set(libraryNameStringIndex, resourceIndex);
addLibResource(libraryNameStringIndex, -1);
}
} else {
const jsMatch =
/^(.*) \((.*):([0-9]+)\)$/.exec(locationString) ||
/^()(.*):([0-9]+)$/.exec(locationString);
if (jsMatch) {
isJS = true;
const scriptURI = _getRealScriptURI(jsMatch[2]);
if (urlToResourceIndex.has(scriptURI)) {
resourceIndex = urlToResourceIndex.get(scriptURI);
} else {
resourceIndex = resourceTable.length;
urlToResourceIndex.set(scriptURI, resourceIndex);
const urlStringIndex = scriptURI
? stringTable.indexForString(scriptURI)
: null;
addUrlResource(urlStringIndex);
}
}
}
}
funcIndex = funcTable.length;
addFunc(funcNameIndex, resourceIndex, addressRelativeToLib, isJS);
stringTableIndexToNewFuncIndex.set(funcNameIndex, funcIndex);
return funcIndex;
});
frameTable.address = frameTable.func.map(
funcIndex => funcTable.address[funcIndex]
);
delete frameTable.location;
let threadName = thread.name;
let processType;
if (threadName === 'Content') {
processType = 'tab';
} else if (threadName === 'Plugin') {
processType = 'plugin';
} else {
processType = 'default';
}
if (threadName === 'Content') {
threadName = 'GeckoMain';
}
return {
libs: [],
name: threadName,
processType,
frameTable,
funcTable,
resourceTable,
stackTable,
markers,
samples,
stringArray: stringTable.serializeToArray(),
};
}
function arrayFromArrayLikeObject<T>(obj: { [index: string]: T }): T[] {
const result: T[] = [];
for (const index in obj) {
result[+index] = obj[index];
}
return result;
}
function _extractThreadList(
profileJSON: OldCleopatraProfileJSON
): OldCleopatraProfileThread[] {
if (Array.isArray(profileJSON)) {
// Ancient versions of the old cleopatra format did not have a threads list
// or markers. Instead, profileJSON was just the list of samples.
const oneThread = {
name: 'GeckoMain',
markers: [],
samples: profileJSON,
};
return [oneThread];
}
return arrayFromArrayLikeObject(profileJSON.threads);
}
/**
* Convert the old cleopatra format into the serialized processed format
* version zero.
* @param {object} profile The input profile.
* @returns A "processed" profile that needs to be run through the
* "processed format" compatibility conversion.
*/
export function convertOldCleopatraProfile(
profile: OldCleopatraProfile
): Object {
const { meta, profileJSON } = profile;
const threads = _extractThreadList(profileJSON);
const symbolicationTable = arrayFromArrayLikeObject(
profile.symbolicationTable
);
return {
meta: Object.assign({}, meta, { version: CURRENT_VERSION }),
threads: threads.map(t =>
_convertThread(t, meta.interval, symbolicationTable)
),
};
}