forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbstracing
executable file
·270 lines (257 loc) · 5.88 KB
/
bstracing
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
#!/usr/bin/env node
"use strict";
//@ts-check
const fs = require("fs");
const readline = require("readline");
const path = require("path");
/**
*
* @param {string} file
* @param {(line:string)=>void} lineCb
* @param {()=>void} finish
*/
function processEntry(file, lineCb, finish) {
let input = fs.createReadStream(file);
input.on("error", function (error) {
console.error(error.message);
console.error(
"make sure you are running after bsb building and in the top directory"
);
process.exit(2);
});
const rl = readline.createInterface({
input: input,
crlfDelay: Infinity,
});
rl.on("line", lineCb);
rl.on("close", finish);
}
class Interval {
/**
*
* @param {number} start
* @param {number} end
*/
constructor(start, end) {
this.start = start;
this.end = end;
/**
* @type {string[]}
*/
this.targets = [];
}
}
class Threads {
constructor() {
/**
* @type {number[]}
*/
this.workers = [];
}
/**
*
* @param {{start : number; end : number}} target
*/
alloc(target) {
for (let i = 0; i < this.workers.length; ++i) {
if (this.workers[i] <= target.start) {
this.workers[i] = target.end;
return i;
}
}
this.workers.push(target.end);
return this.workers.length - 1;
}
}
/**
*
* @param {Map<string,Interval>} map
* @param {string} key
* @param {Interval} def
*
* */
function setDefault(map, key, def) {
if (map.has(key)) {
return map.get(key);
}
map.set(key, def);
return def;
}
// https://github.com/catapult-project/catapult/blob/master/tracing/tracing/base/color_scheme.html#L50
const colors = [
"thread_state_uninterruptible",
"thread_state_iowait",
"thread_state_running",
"thread_state_runnable",
"thread_state_sleeping",
"thread_state_unknown",
"background_memory_dump",
"light_memory_dump",
"detailed_memory_dump",
"vsync_highlight_color",
"generic_work",
"good",
"bad",
"terrible",
"black",
"grey",
"white",
"yellow",
"olive",
"rail_response",
"rail_animation",
"rail_idle",
"rail_load",
"startup",
"heap_dump_stack_frame",
"heap_dump_object_type",
"heap_dump_child_node_arrow",
"cq_build_running",
"cq_build_passed",
"cq_build_failed",
"cq_build_abandoned",
"cq_build_attempt_runnig",
"cq_build_attempt_passed",
"cq_build_attempt_failed",
];
let allocated = new Map();
function getColorName(obj, cat) {
obj.cat = cat;
let i;
if (allocated.has(cat)) {
i = allocated.get(cat);
} else {
allocated.set(cat, allocated.size);
}
obj.cname = colors[i % colors.length];
return;
}
/**
*
* @param {Interval} target
*/
function category(target, obj) {
let targets = target.targets;
if (targets.length === 1) {
let curTar = targets[0];
if (curTar.endsWith(".d")) {
getColorName(obj, "dep");
} else if (curTar.endsWith(".mlast") || curTar.endsWith(".mliast")) {
getColorName(obj, "parse");
} else if (curTar.endsWith(".cmi")) {
getColorName(obj, "cmi");
} else if (curTar.endsWith(".cmj")) {
getColorName(obj, "cmj-only");
} else {
getColorName(obj, "unknown");
}
} else {
getColorName(obj, "cmj");
}
obj.name = target.targets.map((x) => path.parse(x).base).join(",");
return obj;
}
/**
* @param {string} file
* @param {boolean} showAll
* @param {string} outputFile
*/
function readIntervals(file, showAll, outputFile) {
let lastEndSeen = 0;
/**
* @type {Map<string,Interval>}
*/
let targets = new Map();
let offset = 0;
processEntry(
file,
(line) => {
let lineTrim = line.trim();
if (lineTrim.startsWith("#")) {
return;
}
let [start, end, _, name, cmdHash] = lineTrim.split("\t");
cmdHash += "/" + end;
if (+end < lastEndSeen) {
// This is a guess
// it could be wrong, when there's multiple small compilation
if (showAll) {
offset += lastEndSeen + 1000;
console.log(`new session starting from: ${name} : ${offset}`);
} else {
targets = new Map();
}
}
lastEndSeen = +end; // new mark
setDefault(
targets,
cmdHash,
new Interval(Number(start) + offset, Number(end) + offset)
).targets.push(name);
},
() => {
let sorted = [...targets.values()].sort((a, b) => {
return a.start - b.start;
});
let jsonArray = [];
let threads = new Threads();
for (let target of sorted) {
jsonArray.push(
category(target, {
ph: "X",
pid: 0,
dur: (target.end - target.start) * 1000,
ts: target.start * 1000,
tid: threads.alloc(target),
args: {},
})
);
}
console.log(` ${outputFile} is produced, loade it via chrome://tracing/`);
fs.writeFileSync(outputFile, JSON.stringify(jsonArray), "utf8");
}
);
}
let logName = ".ninja_log";
/**
* @type {string}
*/
var file;
/**
*
* @param ps {string[]}
*/
function tryLocation(ps) {
for (let p of ps) {
let log = path.join(p, logName);
if (fs.existsSync(log)) {
file = log;
return;
}
}
console.error(
`no .ninja_log found in specified paths, make sure you set bstracing to the proper directory`
);
process.exit(2);
}
let showAll = false;
let curDate = new Date();
let outputFile = `tracing_${curDate.getHours()}_${curDate.getMinutes()}_${curDate.getSeconds()}.json`;
{
let index = process.argv.indexOf(`-C`);
if (index >= 0) {
let p = process.argv[index + 1];
tryLocation([p, path.join(p, "lib", "bs")]);
} else {
tryLocation([".", path.join("lib", "bs")]);
}
if (process.argv.includes("-all")) {
showAll = true;
}
index = process.argv.indexOf(`-o`);
if (index >= 0) {
outputFile = process.argv[index + 1];
}
}
console.log("loading build log", file, "is used");
readIntervals(file, showAll, outputFile);