-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathbottom-box.ts
More file actions
191 lines (177 loc) · 5.76 KB
/
bottom-box.ts
File metadata and controls
191 lines (177 loc) · 5.76 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
/* 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 { ResourceType } from 'firefox-profiler/types';
import type {
Thread,
IndexIntoStackTable,
IndexIntoCallNodeTable,
BottomBoxInfo,
SamplesLikeTable,
} from 'firefox-profiler/types';
import type { CallNodeInfo } from './call-node-info';
import {
getCallNodeFramePerStack,
getNativeSymbolInfo,
getNativeSymbolsForCallNode,
getTotalNativeSymbolTimingsForCallNode,
} from './profile-data';
import { mapGetKeyWithMaxValue } from 'firefox-profiler/utils';
import { getTotalLineTimingsForCallNode } from './line-timings';
import { getTotalAddressTimingsForCallNode } from './address-timings';
/**
* Calculate the BottomBoxInfo for a call node, i.e. information about which
* things should be shown in the profiler UI's "bottom box" when this call node
* is double-clicked.
*
* We always want to update all panes in the bottom box when a new call node is
* double-clicked, so that we don't show inconsistent information side-by-side.
*/
export function getBottomBoxInfoForCallNode(
callNodeIndex: IndexIntoCallNodeTable,
callNodeInfo: CallNodeInfo,
thread: Thread,
samples: SamplesLikeTable
): BottomBoxInfo {
const {
stackTable,
frameTable,
funcTable,
stringTable,
resourceTable,
nativeSymbols,
} = thread;
const funcIndex = callNodeInfo.funcForNode(callNodeIndex);
const sourceIndex = funcTable.source[funcIndex];
const resource = funcTable.resource[funcIndex];
const libIndex =
resource !== -1 && resourceTable.type[resource] === ResourceType.Library
? resourceTable.lib[resource]
: null;
const callNodeFramePerStack = getCallNodeFramePerStack(
callNodeIndex,
callNodeInfo,
stackTable
);
// If we have at least one native symbol to show assembly for, pick
// the one with the highest total. But first, create the full list of
// native symbols for this call node, including even those symbols
// that aren't hit by any samples in the current view, so that the
// list is stable regardless of the current preview selection.
const nativeSymbolsForCallNode = getNativeSymbolsForCallNode(
callNodeFramePerStack,
frameTable
);
let initialNativeSymbol = null;
const nativeSymbolTimings = getTotalNativeSymbolTimingsForCallNode(
samples,
callNodeFramePerStack,
frameTable
);
const hottestNativeSymbol = mapGetKeyWithMaxValue(nativeSymbolTimings);
if (hottestNativeSymbol !== undefined) {
nativeSymbolsForCallNode.add(hottestNativeSymbol);
initialNativeSymbol = hottestNativeSymbol;
}
const nativeSymbolsForCallNodeArr = [...nativeSymbolsForCallNode];
nativeSymbolsForCallNodeArr.sort((a, b) => a - b);
if (
nativeSymbolsForCallNodeArr.length !== 0 &&
initialNativeSymbol === null
) {
initialNativeSymbol = nativeSymbolsForCallNodeArr[0];
}
const nativeSymbolInfosForCallNode = nativeSymbolsForCallNodeArr.map(
(nativeSymbolIndex) =>
getNativeSymbolInfo(
nativeSymbolIndex,
nativeSymbols,
frameTable,
stringTable
)
);
// Compute the hottest line and instruction address, so we can ask the
// source and assembly view to scroll those into view.
const funcLine = funcTable.lineNumber[funcIndex];
const lineTimings = getTotalLineTimingsForCallNode(
samples,
callNodeFramePerStack,
frameTable,
funcLine
);
const hottestLine = mapGetKeyWithMaxValue(lineTimings);
const addressTimings = getTotalAddressTimingsForCallNode(
samples,
callNodeFramePerStack,
frameTable,
initialNativeSymbol
);
const hottestInstructionAddress = mapGetKeyWithMaxValue(addressTimings);
return {
libIndex,
sourceIndex,
nativeSymbols: nativeSymbolInfosForCallNode,
initialNativeSymbol:
initialNativeSymbol !== null
? nativeSymbolsForCallNodeArr.indexOf(initialNativeSymbol)
: null,
scrollToLineNumber: hottestLine,
scrollToInstructionAddress: hottestInstructionAddress,
highlightedLineNumber: null,
highlightedInstructionAddress: null,
};
}
/**
* Get bottom box info for a stack frame. This is similar to
* getBottomBoxInfoForCallNode but works directly with stack indexes.
*/
export function getBottomBoxInfoForStackFrame(
stackIndex: IndexIntoStackTable,
thread: Thread
): BottomBoxInfo {
const {
stackTable,
frameTable,
funcTable,
resourceTable,
nativeSymbols,
stringTable,
} = thread;
const frameIndex = stackTable.frame[stackIndex];
const funcIndex = frameTable.func[frameIndex];
const sourceIndex = funcTable.source[funcIndex];
const resource = funcTable.resource[funcIndex];
const libIndex =
resource !== -1 && resourceTable.type[resource] === ResourceType.Library
? resourceTable.lib[resource]
: null;
// Get native symbol for this frame
const nativeSymbol = frameTable.nativeSymbol[frameIndex];
const nativeSymbolInfos =
nativeSymbol !== null
? [
getNativeSymbolInfo(
nativeSymbol,
nativeSymbols,
frameTable,
stringTable
),
]
: [];
const instructionAddress =
nativeSymbol !== null ? frameTable.address[frameIndex] : -1;
// Extract line number from the frame
const lineNumber = frameTable.line[frameIndex];
return {
libIndex,
sourceIndex,
nativeSymbols: nativeSymbolInfos,
initialNativeSymbol: 0,
scrollToLineNumber: lineNumber ?? undefined,
highlightedLineNumber: lineNumber,
scrollToInstructionAddress:
instructionAddress !== -1 ? instructionAddress : undefined,
highlightedInstructionAddress:
instructionAddress !== -1 ? instructionAddress : null,
};
}