-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
deoptigate.js
277 lines (242 loc) · 7.73 KB
/
deoptigate.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
'use strict'
/* eslint-disable camelcase */
const LogReader = require('v8-tools-core/logreader')
const { Profile } = require('v8-tools-core/profile')
const IcEntry = require('./lib/log-processing/ic-entry')
const DeoptEntry = require('./lib/log-processing/deopt-entry')
const CodeEntry = require('./lib/log-processing/code-entry')
const { parseOptimizationState } = require('./lib/log-processing/optimization-state')
const groupByFileAndLocation = require('./lib/grouping/group-by-file-and-location')
function maybeNumber(s) {
if (s == null) return -1
return parseInt(s)
}
function formatName(entry) {
if (!entry) return '<unknown>'
const name = entry.func.getName()
const re = /(.*):([0-9]+):([0-9]+)$/
const array = re.exec(name)
if (!array) return { fnFile: name, line: -1, column: -1 }
return {
fnFile: array[1]
, line: maybeNumber(array[2])
, column: maybeNumber(array[3])
}
}
function locationKey(file, line, column) {
return `${file}:${line}:${column}`
}
const propertyICParser = [
parseInt, parseInt, parseInt, null, null, parseInt, null, null, null
]
class DeoptProcessor extends LogReader {
constructor(root, { silentErrors = true } = {}) {
super()
this._root = root
this._silentErrors = silentErrors
// passing dispatch table that references `this` before invoking super
// doesn't work, so we set it afterwards
this.dispatchTable_ = {
// Collect info about CRUD of code
'code-creation': {
parsers: [
null, parseInt, parseInt, parseInt, parseInt, null, 'var-args'
]
, processor: this._processCodeCreation.bind(this)
}
, 'code-move': {
parsers: [ parseInt, parseInt ]
, processor: this._processCodeMove.bind(this)
}
, 'code-delete': {
parsers: [ parseInt ]
, processor: this._processCodeDelete.bind(this)
}
, 'sfi-move': {
parsers: [ parseInt, parseInt ]
, processor: this._processFunctionMove.bind(this)
}
// Collect deoptimization info
, 'code-deopt': {
parsers: [
parseInt, parseInt, parseInt, parseInt, parseInt, null, null, null
]
, processor: this._processCodeDeopt.bind(this)
}
// Collect IC info
, 'LoadIC': {
parsers : propertyICParser
, processor: this._processPropertyIC.bind(this, 'LoadIC')
}
, 'StoreIC': {
parsers : propertyICParser
, processor: this._processPropertyIC.bind(this, 'StoreIC')
}
, 'KeyedLoadIC': {
parsers : propertyICParser
, processor: this._processPropertyIC.bind(this, 'KeyedLoadIC')
}
, 'KeyedStoreIC': {
parsers : propertyICParser
, processor: this._processPropertyIC.bind(this, 'KeyedStoreIC')
}
, 'StoreInArrayLiteralIC': {
parsers : propertyICParser
, processor: this._processPropertyIC.bind(this, 'StoreInArrayLiteralIC')
}
}
this._deserializedEntriesNames = []
this._profile = new Profile()
this.entriesIC = new Map()
this.entriesDeopt = new Map()
this.entriesCode = new Map()
}
functionInfo(pc) {
const entry = this._profile.findEntry(pc)
if (entry == null) return { fnFile: '', state: -1 }
const { fnFile, line, column } = formatName(entry)
return { fnFile, line, column, state: entry.state }
}
_processPropertyIC(
type
, pc
, line
, column
, old_state
, new_state
, map
, propertyKey
, modifier
, slow_reason
) {
const { fnFile, state } = this.functionInfo(pc)
const key = locationKey(fnFile, line, column)
if (!this.entriesIC.has(key)) {
const entry = new IcEntry(fnFile, line, column)
this.entriesIC.set(key, entry)
}
const icEntry = this.entriesIC.get(key)
icEntry.addUpdate(type, old_state, new_state, propertyKey, map, state)
}
// timestamp is in micro seconds
// https://cs.chromium.org/chromium/src/v8/src/log.cc?l=892&rcl=8fecf0eff7357c1bee222f76c4e2f6fdd8759797
_processCodeDeopt(
timestamp
, size
, code
, inliningId
, scriptOffset
, bailoutType
, sourcePositionText
, deoptReasonText
) {
const { fnFile, state } = this.functionInfo(code)
const { file, line, column } = DeoptEntry.disassembleSourcePosition(sourcePositionText)
const key = locationKey(file, line, column)
if (!this.entriesDeopt.has(key)) {
const entry = new DeoptEntry(fnFile, file, line, column)
this.entriesDeopt.set(key, entry)
}
const deoptEntry = this.entriesDeopt.get(key)
deoptEntry.addUpdate(timestamp, bailoutType, deoptReasonText, state, inliningId)
}
_processCodeCreation(
type, kind, timestamp, start, size, name, maybe_func
) {
name = this._deserializedEntriesNames[start] || name
if (maybe_func.length) {
const funcAddr = parseInt(maybe_func[0])
const state = parseOptimizationState(maybe_func[1])
this._profile.addFuncCode(
type, name, timestamp, start, size, funcAddr, state
)
const isScript = type === 'Script'
const isUserFunction = type === 'LazyCompile'
if (isUserFunction || isScript) {
let { fnFile, line, column } = this.functionInfo(start)
// only interested in Node.js anonymous wrapper function
// (function (exports, require, module, __filename, __dirname) {
const isNodeWrapperFunction = (line === 1 && column === 1)
if (isScript && !isNodeWrapperFunction) return
const key = locationKey(fnFile, line, column)
if (!this.entriesCode.has(key)) {
this.entriesCode.set(key, new CodeEntry({ fnFile, line, column, isScript }))
}
const code = this.entriesCode.get(key)
code.addUpdate(timestamp, state)
}
} else {
this._profile.addCode(type, name, timestamp, start, size)
}
}
_processCodeMove(from, to) {
this._profile.moveCode(from, to)
}
_processCodeDelete(start) {
this._profile.deleteCode(start)
}
_processFunctionMove(from, to) {
this._profile.moveFunc(from, to)
}
// @override
printError(msg) {
if (this._silentErrors) return
console.trace()
console.error(msg)
}
processString(string) {
var end = string.length
var current = 0
var next = 0
var line
while (current < end) {
next = string.indexOf('\n', current)
if (next === -1) break
line = string.substring(current, next)
current = next + 1
this.processLogLine(line)
}
}
filterIcStateChanges() {
const emptyEntries = new Set()
for (const [ key, entry ] of this.entriesIC) {
entry.filterIcStateChanges()
if (entry.updates.length === 0) emptyEntries.add(key)
}
for (const key of emptyEntries) this.entriesIC.delete(key)
}
toObject() {
const ics = []
for (const entry of this.entriesIC.values()) {
ics.push(entry.hashmap)
}
const deopts = []
for (const entry of this.entriesDeopt.values()) {
deopts.push(entry.hashmap)
}
const codes = []
for (const entry of this.entriesCode.values()) {
codes.push(entry.hashmap)
}
return { ics, deopts, codes, root: this._root }
}
toJSON(indent = 2) {
return JSON.stringify(this.toObject(), null, indent)
}
}
async function processLogContent(lines, root) {
const deoptProcessor = new DeoptProcessor(root)
for await (const line of lines) {
deoptProcessor.processLogLine(line)
}
deoptProcessor.filterIcStateChanges()
return deoptProcessor
}
function deoptigate(groupedByFile) {
const groupedByFileAndLocation = groupByFileAndLocation(groupedByFile)
return groupedByFileAndLocation
}
module.exports = {
processLogContent
, deoptigate
}