forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transforms.js
296 lines (283 loc) · 12.8 KB
/
transforms.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
/* 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
/**
* Transforms are the minimal representation some kind of transformation to the data
* that is used to transform the sample and stack information of a profile. They are
* applied in a stack.
*
* When working with a call tree, nodes in the graph are not stable across various
* transformations of the stacks. It doesn't make sense to generate a single ID for
* a node, as the definition of what a node is can change depending on the current
* context. In order to get around this, we use a combination of the CallNodePath,
* implementation, and if the current stacks are inverted to refer to a node in the tree.
* This combination of information will provide a stable reference to a call node for a
* given view into a call tree.
*/
import type {
ThreadIndex,
IndexIntoFuncTable,
IndexIntoResourceTable,
} from './profile';
import type { CallNodePath } from './profile-derived';
import type { ImplementationFilter } from './actions';
/*
* Define all of the transforms on an object to conveniently access $ObjMap and do
* nice things like iterate over every transform type. There is no way to create a
* union from a tuple in flow.
*
* See: https://github.com/facebook/flow/issues/961
*/
export type TransformDefinitions = {
/**
* FocusSubtree transform represents the operation of focusing on a subtree in a call tree.
* The subtree is referenced by a callNodePath (a list of functions to a particular node),
* and an implementation filter to filter out certain stacks and nodes that we don't care
* about. For more details read `docs/call-tree.md`.
*
* Here is a typical case of focusing on the subtree at CallNodePath [A, B, C]
*
* A:3,0 C:2,0
* | / \
* v Focus [A, B, C] v v
* B:3,0 --> D:1,0 F:1,0
* / \ | |
* v v v v
* C:2,0 H:1,0 E:1,1 G:1,1
* / \ \
* v v v
* D:1,0 F:1,0 F:1,1
* | |
* v v
* E:1,1 G:1,1
*
* As well as focusing on a normal subtree, we can also focus on an inverted call tree.
* In this case the CallNodePath will be reversed, and will go from the end of a call
* stack, towards the base, the opposite order of an un-inverted CallNodePath.
*
* Here is a typical case of focusing on the inverted subtree at CallNodePath [Z, Y, X]
*
* 1. Starting call tree -> 2. Invert call tree ->
*
* A:3,0 Z:2,2 E:1,1
* ↓ ↓ ↓
* B:3,0 Y:2,0 D:1,0
* ↙ ↘ ↓ ↓
* X:1,0 C:1,0 X:2,0 C:1,0
* ↙ ↙ ↘ ↙ ↘ ↓
* Y:1,0 X:1,0 D:1,0 B:1,0 C:1,0 B:1,0
* ↓ ↓ ↓ ↓ ↓ ↓
* Z:1,1 Y:1,0 E:1,0 A:1,0 B:1,0 A:1,0
* ↓ ↓
* Z:1,1 A:1,0
*
* --------------------------------------------------------------------------------
*
* -> 3. Focus [Z, Y, X] -> 4. Un-invert call tree
*
* X:2,2 A:2,0
* ↙ ↘ ↓
* B:1,0 C:1,0 B:2,0
* ↓ ↓ ↙ ↘
* A:1,0 B:1,0 X:1,1 C:1,0
* ↓ ↓
* A:1,0 X:1,1
*/
'focus-subtree': {|
+type: 'focus-subtree',
+callNodePath: CallNodePath,
+implementation: ImplementationFilter,
+inverted: boolean,
|},
/**
* This is the same operation as the FocusSubtree, but it is performed on each usage
* of the function across the tree, node just the one usage in a call tree.
*
* A:3,0 X:3,0
* / \ |
* v v Focus X v
* X:1,0 B:2,0 -> Y:3,0
* | | / \
* v v v v
* Y:1,0 X:2,0 C:1,1 X:2,0
* | | |
* v v v
* C:1,1 Y:2,0 Y:2,0
* | |
* v v
* X:2,0 D:2,2
* |
* v
* Y:2,0
* |
* v
* D:2,2
*/
'focus-function': {|
+type: 'focus-function',
+funcIndex: IndexIntoFuncTable,
|},
/**
* The MergeCallNode transform represents merging a CallNode into the parent CallNode. The
* CallNode must match the given CallNodePath. In the call tree below, if the CallNode
* at path [A, B, C] is removed, then the `D` and `F` CallNodes are re-assigned to `B`.
* No self time in this case would change, as `C` was not a leaf CallNode, but the
* structure of the tree was changed slightly. The merging work is done by transforming
* an existing thread's stackTable.
*
* A:3,0 A:3,0
* | |
* v v
* B:3,0 Merge CallNode B:3,0
* / \ [A, B, C] / | \
* v v --> v v v
* C:2,0 H:1,0 D:1,0 F:1,0 H:1,0
* / \ \ | | |
* v v v v v v
* D:1,0 F:1,0 F:1,1 E:1,1 G:1,1 F:1,1
* | |
* v v
* E:1,1 G:1,1
*
*
* When a leaf CallNode is merged, the self time for that CallNode is assigned to the
* parent CallNode. Here the leaf CallNode `E` is merged. `D` goes from having a self
* time of 0 to 1.
* A:3,0 A:3,0
* | |
* v v
* B:3,0 Merge CallNode B:3,0
* / \ [A, B, C, D, E] / \
* v v --> v v
* C:2,0 H:1,0 C:2,0 H:1,0
* / \ \ / \ \
* v v v v v v
* D:1,0 F:1,0 F:1,1 D:1,1 F:1,0 F:1,1
* | | |
* v v v
* E:1,1 G:1,1 G:1,1
*
* This same operation is not applied to an inverted call stack as it has been deemed
* not particularly useful, and prone to not give the expected results.
*/
'merge-call-node': {|
+type: 'merge-call-node',
+callNodePath: CallNodePath,
+implementation: ImplementationFilter,
|},
/**
* The MergeFunctions transform is similar to the MergeCallNode, except it merges a single
* function across the entire call tree, regardless of its location in the tree. It is not
* depended on any particular CallNodePath.
*
* A:3,0 A:3,0
* | |
* v v
* B:3,0 B:3,0
* / \ Merge Func C / | \
* v v --> v v v
* C:2,0 H:1,0 D:1,0 F:1,0 H:1,1
* / \ \ | |
* v v v v v
* D:1,0 F:1,0 C:1,1 E:1,1 G:1,1
* | |
* v v
* E:1,1 G:1,1
*/
'merge-function': {|
+type: 'merge-function',
+funcIndex: IndexIntoFuncTable,
|},
/**
* The DropFunction transform removes samples from the thread that have a function
* somewhere in its stack.
*
* A:4,0 A:1,0
* / \ Drop Func C |
* v v --> v
* B:3,0 C:1,0 B:1,0
* / \ |
* v v v
* C:2,1 D:1,1 D:1,1
* |
* v
* D:1,1
*/
'drop-function': {|
+type: 'drop-function',
+funcIndex: IndexIntoFuncTable,
|},
/**
* Collapse resource takes CallNodes that are of a consecutive library, and collapses
* them into a new collapsed pseudo-stack. Given a call tree like below, where each node
* is defined by either "function_name" or "function_name:library_name":
*
* A A
* / \ |
* v v Collapse firefox v
* B:firefox E:firefox -> firefox
* | | / \
* v v D F
* C:firefox F
* |
* v
* D
*/
'collapse-resource': {|
+type: 'collapse-resource',
+resourceIndex: IndexIntoResourceTable,
// This is the index of the newly created function that represents the collapsed stack.
+collapsedFuncIndex: IndexIntoFuncTable,
+implementation: ImplementationFilter,
|},
/**
* Collapse direct recursion takes a function that calls itself recursively and collapses
* it into a single stack.
*
* A A
* ↓ Collapse direct recursion ↓
* B function B B
* ↓ -> ↓
* B C
* ↓
* B
* ↓
* B
* ↓
* C
*/
'collapse-direction-recursion': {|
+type: 'collapse-direct-recursion',
+funcIndex: IndexIntoFuncTable,
+implementation: ImplementationFilter,
|},
/**
* Collapse the subtree of a function into that function across the entire tree.
*
* A:4,0 A:4,0
* | |
* v v
* B:4,0 B:4,0
* / \ Collapse subtree C / \
* v v --> v v
* C:2,0 H:2,0 C:2,2 H:2,0
* / \ \ |
* v v v v
* D:1,0 F:1,0 C:2,0 C:2,2
* / / / \
* v v v v
* E:1,1 G:1,1 I:1,1 J:1,1
*/
'collapse-function-subtree': {|
+type: 'collapse-function-subtree',
+funcIndex: IndexIntoFuncTable,
|},
};
// Extract the transforms into a union.
export type Transform = $Values<TransformDefinitions>;
// This pulls the string value out of { type } for a transform.
type ExtractType = <T: string, S: { +type: T }>(transform: S) => T;
export type TransformType = $Values<$ObjMap<TransformDefinitions, ExtractType>>;
export type TransformStack = Transform[];
export type TransformStacksPerThread = { [id: ThreadIndex]: TransformStack };