-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathglobal-data-collector.ts
More file actions
321 lines (291 loc) · 10.8 KB
/
global-data-collector.ts
File metadata and controls
321 lines (291 loc) · 10.8 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
/* 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/. */
import { StringTable } from '../utils/string-table';
import {
getEmptyFrameTable,
getEmptyFuncTable,
getEmptyNativeSymbolTable,
getEmptyRawStackTable,
getEmptyResourceTable,
getEmptySourceTable,
} from './data-structures';
import type {
Lib,
LibMapping,
IndexIntoLibs,
IndexIntoStringTable,
IndexIntoSourceTable,
RawProfileSharedData,
SourceTable,
FrameTable,
RawStackTable,
FuncTable,
ResourceTable,
NativeSymbolTable,
IndexIntoResourceTable,
IndexIntoFuncTable,
ExtensionTable,
IndexIntoNativeSymbolTable,
Address,
Bytes,
} from 'firefox-profiler/types';
import { ResourceType } from 'firefox-profiler/types';
/**
* GlobalDataCollector collects data which is global in the processed profile
* format but per-process or per-thread in the Gecko profile format. It
* de-duplicates elements and builds one shared list of each type.
* For now it only de-duplicates libraries, but in the future we may move more
* tables to be global.
* You could also call this class an "interner".
*/
export class GlobalDataCollector {
_libs: Lib[] = [];
_libKeyToLibIndex: Map<string, IndexIntoLibs> = new Map();
_stringArray: string[] = [];
_stringTable: StringTable = StringTable.withBackingArray(this._stringArray);
_sources: SourceTable = getEmptySourceTable();
_frameTable: FrameTable = getEmptyFrameTable();
_stackTable: RawStackTable = getEmptyRawStackTable();
_funcTable: FuncTable = getEmptyFuncTable();
_resourceTable: ResourceTable = getEmptyResourceTable();
_nativeSymbols: NativeSymbolTable = getEmptyNativeSymbolTable();
_funcKeyToFuncIndex: Map<string, IndexIntoFuncTable> = new Map();
_nativeSymbolKeyToNativeSymbolIndex: Map<string, IndexIntoNativeSymbolTable> =
new Map();
_libIndexToResourceIndex: Map<IndexIntoLibs, IndexIntoResourceTable> =
new Map();
_libNameToResourceIndex: Map<IndexIntoStringTable, IndexIntoResourceTable> =
new Map();
_originToResourceIndex: Map<string, IndexIntoResourceTable> = new Map();
_idToSourceIndex: Map<string, IndexIntoSourceTable> = new Map();
_filenameToSourceIndex: Map<IndexIntoStringTable, IndexIntoSourceTable> =
new Map();
// Return the global index for this library, adding it to the global list if
// necessary.
indexForLib(libMapping: LibMapping | Lib): IndexIntoLibs {
const { debugName, breakpadId } = libMapping;
const libKey = `${debugName}/${breakpadId}`;
let index = this._libKeyToLibIndex.get(libKey);
if (index === undefined) {
index = this._libs.length;
const { arch, name, path, debugPath, codeId } = libMapping;
this._libs.push({
arch,
name,
path,
debugName,
debugPath,
breakpadId,
codeId: codeId ?? null,
});
this._libKeyToLibIndex.set(libKey, index);
}
return index;
}
indexForFunc(
name: IndexIntoStringTable,
isJS: boolean,
relevantForJS: boolean,
resource: IndexIntoResourceTable | -1,
source: IndexIntoSourceTable | null,
lineNumber: number | null,
columnNumber: number | null
): IndexIntoFuncTable {
const funcKey = `${name}-${isJS}-${relevantForJS}-${resource}-${source}-${lineNumber}-${columnNumber}`;
let funcIndex = this._funcKeyToFuncIndex.get(funcKey);
if (funcIndex === undefined) {
funcIndex = this._funcTable.length++;
this._funcTable.name[funcIndex] = name;
this._funcTable.isJS[funcIndex] = isJS;
this._funcTable.relevantForJS[funcIndex] = relevantForJS;
this._funcTable.resource[funcIndex] = resource;
this._funcTable.source[funcIndex] = source;
this._funcTable.lineNumber[funcIndex] = lineNumber;
this._funcTable.columnNumber[funcIndex] = columnNumber;
this._funcKeyToFuncIndex.set(funcKey, funcIndex);
}
return funcIndex;
}
// Return the global index for this source, adding it to the global list if
// necessary.
indexForSource(
id: string | null,
filename: string,
startLine: number = 1,
startColumn: number = 1,
sourceMapURL: string | null = null
): IndexIntoSourceTable {
let index: IndexIntoSourceTable | undefined;
if (id !== null) {
index = this._idToSourceIndex.get(id);
} else {
// For null IDs, use filename-based lookup.
const filenameIndex = this._stringTable.indexForString(filename);
index = this._filenameToSourceIndex.get(filenameIndex);
}
if (index === undefined) {
index = this._sources.length;
const filenameIndex = this._stringTable.indexForString(filename);
const sourceMapURLIndex =
sourceMapURL !== null
? this._stringTable.indexForString(sourceMapURL)
: null;
this._sources.id[index] = id;
this._sources.filename[index] = filenameIndex;
this._sources.startLine[index] = startLine;
this._sources.startColumn[index] = startColumn;
this._sources.sourceMapURL[index] = sourceMapURLIndex;
this._sources.length++;
if (id !== null) {
this._idToSourceIndex.set(id, index);
} else {
this._filenameToSourceIndex.set(filenameIndex, index);
}
}
return index;
}
// Prepopulate resourceTable and this._originToResourceIndex so that future calls
// to indexForURIResource will return an add-on resource for add-on URI origins.
addExtensionOrigins(extensions: ExtensionTable): void {
const resourceTable = this._resourceTable;
for (let i = 0; i < extensions.length; i++) {
const origin = new URL(extensions.baseURL[i]).origin;
let resourceIndex = this._originToResourceIndex.get(origin);
if (resourceIndex === undefined) {
resourceIndex = resourceTable.length++;
this._originToResourceIndex.set(origin, resourceIndex);
const quotedName = JSON.stringify(extensions.name[i]);
const name = `Extension ${quotedName} (ID: ${extensions.id[i]})`;
const idIndex = this._stringTable.indexForString(extensions.id[i]);
resourceTable.lib[resourceIndex] = null;
resourceTable.name[resourceIndex] =
this._stringTable.indexForString(name);
resourceTable.host[resourceIndex] = idIndex;
resourceTable.type[resourceIndex] = ResourceType.Addon;
}
}
}
// Returns the resource index for a "url" or "webhost" resource which is created
// on demand based on the script URI.
indexForURIResource(scriptURI: string) {
// Figure out the origin and host.
let origin;
let host;
try {
const url = new URL(scriptURI);
if (
!(
url.protocol === 'http:' ||
url.protocol === 'https:' ||
url.protocol === 'moz-extension:'
)
) {
throw new Error('not a webhost or extension protocol');
}
origin = url.origin;
host = url.host;
} catch (_e) {
origin = scriptURI;
host = null;
}
let resourceIndex = this._originToResourceIndex.get(origin);
if (resourceIndex !== undefined) {
return resourceIndex;
}
const resourceTable = this._resourceTable;
resourceIndex = resourceTable.length++;
this._originToResourceIndex.set(origin, resourceIndex);
if (host) {
// This is a webhost URL.
resourceTable.lib[resourceIndex] = null;
resourceTable.name[resourceIndex] =
this._stringTable.indexForString(origin);
resourceTable.host[resourceIndex] =
this._stringTable.indexForString(host);
resourceTable.type[resourceIndex] = ResourceType.Webhost;
} else {
// This is a URL, but it doesn't point to something on the web, e.g. a
// chrome url.
resourceTable.lib[resourceIndex] = null;
resourceTable.name[resourceIndex] =
this._stringTable.indexForString(scriptURI);
resourceTable.host[resourceIndex] = null;
resourceTable.type[resourceIndex] = ResourceType.Url;
}
return resourceIndex;
}
indexForLibResource(libIndex: IndexIntoLibs): IndexIntoResourceTable {
let resourceIndex = this._libIndexToResourceIndex.get(libIndex);
if (resourceIndex !== undefined) {
return resourceIndex;
}
const resourceTable = this._resourceTable;
resourceIndex = this._resourceTable.length++;
this._libIndexToResourceIndex.set(libIndex, resourceIndex);
resourceTable.lib[resourceIndex] = libIndex;
resourceTable.name[resourceIndex] = this._stringTable.indexForString(
this._libs[libIndex].name
);
resourceTable.host[resourceIndex] = null;
resourceTable.type[resourceIndex] = ResourceType.Library;
return resourceIndex;
}
indexForNameOnlyLibResource(
libNameStringIndex: IndexIntoStringTable
): IndexIntoResourceTable {
let resourceIndex = this._libNameToResourceIndex.get(libNameStringIndex);
if (resourceIndex !== undefined) {
return resourceIndex;
}
const resourceTable = this._resourceTable;
resourceIndex = this._resourceTable.length++;
this._libNameToResourceIndex.set(libNameStringIndex, resourceIndex);
resourceTable.lib[resourceIndex] = null;
resourceTable.name[resourceIndex] = libNameStringIndex;
resourceTable.host[resourceIndex] = null;
resourceTable.type[resourceIndex] = ResourceType.Library;
return resourceIndex;
}
indexForNativeSymbol(
libIndex: IndexIntoLibs,
address: Address,
name: IndexIntoStringTable,
functionSize: Bytes | null
): IndexIntoNativeSymbolTable {
const key = `${libIndex}-${address}-${name}-${functionSize ?? ''}`;
let nativeSymbolIndex = this._nativeSymbolKeyToNativeSymbolIndex.get(key);
if (nativeSymbolIndex === undefined) {
nativeSymbolIndex = this._nativeSymbols.length++;
this._nativeSymbols.libIndex[nativeSymbolIndex] = libIndex;
this._nativeSymbols.address[nativeSymbolIndex] = address;
this._nativeSymbols.name[nativeSymbolIndex] = name;
this._nativeSymbols.functionSize[nativeSymbolIndex] = functionSize;
this._nativeSymbolKeyToNativeSymbolIndex.set(key, nativeSymbolIndex);
}
return nativeSymbolIndex;
}
getStringTable(): StringTable {
return this._stringTable;
}
getFrameTable(): FrameTable {
return this._frameTable;
}
getStackTable(): RawStackTable {
return this._stackTable;
}
// Package up all de-duplicated global tables so that they can be embedded in
// the profile.
finish(): { libs: Lib[]; shared: RawProfileSharedData } {
const shared: RawProfileSharedData = {
stackTable: this._stackTable,
frameTable: this._frameTable,
funcTable: this._funcTable,
resourceTable: this._resourceTable,
nativeSymbols: this._nativeSymbols,
stringArray: this._stringArray,
sources: this._sources,
};
return { libs: this._libs, shared };
}
}