ProfilingPlugin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const { Tracer } = require("chrome-trace-event");
  6. const {
  7. JAVASCRIPT_MODULE_TYPE_AUTO,
  8. JAVASCRIPT_MODULE_TYPE_DYNAMIC,
  9. JAVASCRIPT_MODULE_TYPE_ESM,
  10. WEBASSEMBLY_MODULE_TYPE_ASYNC,
  11. WEBASSEMBLY_MODULE_TYPE_SYNC,
  12. JSON_MODULE_TYPE
  13. } = require("../ModuleTypeConstants");
  14. const createSchemaValidation = require("../util/create-schema-validation");
  15. const { dirname, mkdirpSync } = require("../util/fs");
  16. /** @typedef {import("../../declarations/plugins/debug/ProfilingPlugin").ProfilingPluginOptions} ProfilingPluginOptions */
  17. /** @typedef {import("../util/fs").IntermediateFileSystem} IntermediateFileSystem */
  18. const validate = createSchemaValidation(
  19. require("../../schemas/plugins/debug/ProfilingPlugin.check.js"),
  20. () => require("../../schemas/plugins/debug/ProfilingPlugin.json"),
  21. {
  22. name: "Profiling Plugin",
  23. baseDataPath: "options"
  24. }
  25. );
  26. let inspector = undefined;
  27. try {
  28. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  29. inspector = require("inspector");
  30. } catch (e) {
  31. console.log("Unable to CPU profile in < node 8.0");
  32. }
  33. class Profiler {
  34. constructor(inspector) {
  35. this.session = undefined;
  36. this.inspector = inspector;
  37. this._startTime = 0;
  38. }
  39. hasSession() {
  40. return this.session !== undefined;
  41. }
  42. startProfiling() {
  43. if (this.inspector === undefined) {
  44. return Promise.resolve();
  45. }
  46. try {
  47. this.session = new inspector.Session();
  48. this.session.connect();
  49. } catch (_) {
  50. this.session = undefined;
  51. return Promise.resolve();
  52. }
  53. const hrtime = process.hrtime();
  54. this._startTime = hrtime[0] * 1000000 + Math.round(hrtime[1] / 1000);
  55. return Promise.all([
  56. this.sendCommand("Profiler.setSamplingInterval", {
  57. interval: 100
  58. }),
  59. this.sendCommand("Profiler.enable"),
  60. this.sendCommand("Profiler.start")
  61. ]);
  62. }
  63. sendCommand(method, params) {
  64. if (this.hasSession()) {
  65. return new Promise((res, rej) => {
  66. return this.session.post(method, params, (err, params) => {
  67. if (err !== null) {
  68. rej(err);
  69. } else {
  70. res(params);
  71. }
  72. });
  73. });
  74. } else {
  75. return Promise.resolve();
  76. }
  77. }
  78. destroy() {
  79. if (this.hasSession()) {
  80. this.session.disconnect();
  81. }
  82. return Promise.resolve();
  83. }
  84. stopProfiling() {
  85. return this.sendCommand("Profiler.stop").then(({ profile }) => {
  86. const hrtime = process.hrtime();
  87. const endTime = hrtime[0] * 1000000 + Math.round(hrtime[1] / 1000);
  88. if (profile.startTime < this._startTime || profile.endTime > endTime) {
  89. // In some cases timestamps mismatch and we need to adjust them
  90. // Both process.hrtime and the inspector timestamps claim to be relative
  91. // to a unknown point in time. But they do not guarantee that this is the
  92. // same point in time.
  93. const duration = profile.endTime - profile.startTime;
  94. const ownDuration = endTime - this._startTime;
  95. const untracked = Math.max(0, ownDuration - duration);
  96. profile.startTime = this._startTime + untracked / 2;
  97. profile.endTime = endTime - untracked / 2;
  98. }
  99. return { profile };
  100. });
  101. }
  102. }
  103. /**
  104. * an object that wraps Tracer and Profiler with a counter
  105. * @typedef {Object} Trace
  106. * @property {Tracer} trace instance of Tracer
  107. * @property {number} counter Counter
  108. * @property {Profiler} profiler instance of Profiler
  109. * @property {Function} end the end function
  110. */
  111. /**
  112. * @param {IntermediateFileSystem} fs filesystem used for output
  113. * @param {string} outputPath The location where to write the log.
  114. * @returns {Trace} The trace object
  115. */
  116. const createTrace = (fs, outputPath) => {
  117. const trace = new Tracer();
  118. const profiler = new Profiler(inspector);
  119. if (/\/|\\/.test(outputPath)) {
  120. const dirPath = dirname(fs, outputPath);
  121. mkdirpSync(fs, dirPath);
  122. }
  123. const fsStream = fs.createWriteStream(outputPath);
  124. let counter = 0;
  125. trace.pipe(fsStream);
  126. // These are critical events that need to be inserted so that tools like
  127. // chrome dev tools can load the profile.
  128. trace.instantEvent({
  129. name: "TracingStartedInPage",
  130. id: ++counter,
  131. cat: ["disabled-by-default-devtools.timeline"],
  132. args: {
  133. data: {
  134. sessionId: "-1",
  135. page: "0xfff",
  136. frames: [
  137. {
  138. frame: "0xfff",
  139. url: "webpack",
  140. name: ""
  141. }
  142. ]
  143. }
  144. }
  145. });
  146. trace.instantEvent({
  147. name: "TracingStartedInBrowser",
  148. id: ++counter,
  149. cat: ["disabled-by-default-devtools.timeline"],
  150. args: {
  151. data: {
  152. sessionId: "-1"
  153. }
  154. }
  155. });
  156. return {
  157. trace,
  158. counter,
  159. profiler,
  160. end: callback => {
  161. trace.push("]");
  162. // Wait until the write stream finishes.
  163. fsStream.on("close", () => {
  164. callback();
  165. });
  166. // Tear down the readable trace stream.
  167. trace.push(null);
  168. }
  169. };
  170. };
  171. const PLUGIN_NAME = "ProfilingPlugin";
  172. class ProfilingPlugin {
  173. /**
  174. * @param {ProfilingPluginOptions=} options options object
  175. */
  176. constructor(options = {}) {
  177. validate(options);
  178. this.outputPath = options.outputPath || "events.json";
  179. }
  180. apply(compiler) {
  181. const tracer = createTrace(
  182. compiler.intermediateFileSystem,
  183. this.outputPath
  184. );
  185. tracer.profiler.startProfiling();
  186. // Compiler Hooks
  187. Object.keys(compiler.hooks).forEach(hookName => {
  188. const hook = compiler.hooks[hookName];
  189. if (hook) {
  190. hook.intercept(makeInterceptorFor("Compiler", tracer)(hookName));
  191. }
  192. });
  193. Object.keys(compiler.resolverFactory.hooks).forEach(hookName => {
  194. const hook = compiler.resolverFactory.hooks[hookName];
  195. if (hook) {
  196. hook.intercept(makeInterceptorFor("Resolver", tracer)(hookName));
  197. }
  198. });
  199. compiler.hooks.compilation.tap(
  200. PLUGIN_NAME,
  201. (compilation, { normalModuleFactory, contextModuleFactory }) => {
  202. interceptAllHooksFor(compilation, tracer, "Compilation");
  203. interceptAllHooksFor(
  204. normalModuleFactory,
  205. tracer,
  206. "Normal Module Factory"
  207. );
  208. interceptAllHooksFor(
  209. contextModuleFactory,
  210. tracer,
  211. "Context Module Factory"
  212. );
  213. interceptAllParserHooks(normalModuleFactory, tracer);
  214. interceptAllJavascriptModulesPluginHooks(compilation, tracer);
  215. }
  216. );
  217. // We need to write out the CPU profile when we are all done.
  218. compiler.hooks.done.tapAsync(
  219. {
  220. name: PLUGIN_NAME,
  221. stage: Infinity
  222. },
  223. (stats, callback) => {
  224. if (compiler.watchMode) return callback();
  225. tracer.profiler.stopProfiling().then(parsedResults => {
  226. if (parsedResults === undefined) {
  227. tracer.profiler.destroy();
  228. tracer.end(callback);
  229. return;
  230. }
  231. const cpuStartTime = parsedResults.profile.startTime;
  232. const cpuEndTime = parsedResults.profile.endTime;
  233. tracer.trace.completeEvent({
  234. name: "TaskQueueManager::ProcessTaskFromWorkQueue",
  235. id: ++tracer.counter,
  236. cat: ["toplevel"],
  237. ts: cpuStartTime,
  238. args: {
  239. src_file: "../../ipc/ipc_moji_bootstrap.cc",
  240. src_func: "Accept"
  241. }
  242. });
  243. tracer.trace.completeEvent({
  244. name: "EvaluateScript",
  245. id: ++tracer.counter,
  246. cat: ["devtools.timeline"],
  247. ts: cpuStartTime,
  248. dur: cpuEndTime - cpuStartTime,
  249. args: {
  250. data: {
  251. url: "webpack",
  252. lineNumber: 1,
  253. columnNumber: 1,
  254. frame: "0xFFF"
  255. }
  256. }
  257. });
  258. tracer.trace.instantEvent({
  259. name: "CpuProfile",
  260. id: ++tracer.counter,
  261. cat: ["disabled-by-default-devtools.timeline"],
  262. ts: cpuEndTime,
  263. args: {
  264. data: {
  265. cpuProfile: parsedResults.profile
  266. }
  267. }
  268. });
  269. tracer.profiler.destroy();
  270. tracer.end(callback);
  271. });
  272. }
  273. );
  274. }
  275. }
  276. const interceptAllHooksFor = (instance, tracer, logLabel) => {
  277. if (Reflect.has(instance, "hooks")) {
  278. Object.keys(instance.hooks).forEach(hookName => {
  279. const hook = instance.hooks[hookName];
  280. if (hook && !hook._fakeHook) {
  281. hook.intercept(makeInterceptorFor(logLabel, tracer)(hookName));
  282. }
  283. });
  284. }
  285. };
  286. const interceptAllParserHooks = (moduleFactory, tracer) => {
  287. const moduleTypes = [
  288. JAVASCRIPT_MODULE_TYPE_AUTO,
  289. JAVASCRIPT_MODULE_TYPE_DYNAMIC,
  290. JAVASCRIPT_MODULE_TYPE_ESM,
  291. JSON_MODULE_TYPE,
  292. WEBASSEMBLY_MODULE_TYPE_ASYNC,
  293. WEBASSEMBLY_MODULE_TYPE_SYNC
  294. ];
  295. moduleTypes.forEach(moduleType => {
  296. moduleFactory.hooks.parser
  297. .for(moduleType)
  298. .tap(PLUGIN_NAME, (parser, parserOpts) => {
  299. interceptAllHooksFor(parser, tracer, "Parser");
  300. });
  301. });
  302. };
  303. const interceptAllJavascriptModulesPluginHooks = (compilation, tracer) => {
  304. interceptAllHooksFor(
  305. {
  306. hooks:
  307. require("../javascript/JavascriptModulesPlugin").getCompilationHooks(
  308. compilation
  309. )
  310. },
  311. tracer,
  312. "JavascriptModulesPlugin"
  313. );
  314. };
  315. const makeInterceptorFor = (instance, tracer) => hookName => ({
  316. register: tapInfo => {
  317. const { name, type, fn } = tapInfo;
  318. const newFn =
  319. // Don't tap our own hooks to ensure stream can close cleanly
  320. name === PLUGIN_NAME
  321. ? fn
  322. : makeNewProfiledTapFn(hookName, tracer, {
  323. name,
  324. type,
  325. fn
  326. });
  327. return {
  328. ...tapInfo,
  329. fn: newFn
  330. };
  331. }
  332. });
  333. // TODO improve typing
  334. /** @typedef {(...args: TODO[]) => void | Promise<TODO>} PluginFunction */
  335. /**
  336. * @param {string} hookName Name of the hook to profile.
  337. * @param {Trace} tracer The trace object.
  338. * @param {object} options Options for the profiled fn.
  339. * @param {string} options.name Plugin name
  340. * @param {string} options.type Plugin type (sync | async | promise)
  341. * @param {PluginFunction} options.fn Plugin function
  342. * @returns {PluginFunction} Chainable hooked function.
  343. */
  344. const makeNewProfiledTapFn = (hookName, tracer, { name, type, fn }) => {
  345. const defaultCategory = ["blink.user_timing"];
  346. switch (type) {
  347. case "promise":
  348. return (...args) => {
  349. const id = ++tracer.counter;
  350. tracer.trace.begin({
  351. name,
  352. id,
  353. cat: defaultCategory
  354. });
  355. const promise = /** @type {Promise<*>} */ (fn(...args));
  356. return promise.then(r => {
  357. tracer.trace.end({
  358. name,
  359. id,
  360. cat: defaultCategory
  361. });
  362. return r;
  363. });
  364. };
  365. case "async":
  366. return (...args) => {
  367. const id = ++tracer.counter;
  368. tracer.trace.begin({
  369. name,
  370. id,
  371. cat: defaultCategory
  372. });
  373. const callback = args.pop();
  374. fn(...args, (...r) => {
  375. tracer.trace.end({
  376. name,
  377. id,
  378. cat: defaultCategory
  379. });
  380. callback(...r);
  381. });
  382. };
  383. case "sync":
  384. return (...args) => {
  385. const id = ++tracer.counter;
  386. // Do not instrument ourself due to the CPU
  387. // profile needing to be the last event in the trace.
  388. if (name === PLUGIN_NAME) {
  389. return fn(...args);
  390. }
  391. tracer.trace.begin({
  392. name,
  393. id,
  394. cat: defaultCategory
  395. });
  396. let r;
  397. try {
  398. r = fn(...args);
  399. } catch (error) {
  400. tracer.trace.end({
  401. name,
  402. id,
  403. cat: defaultCategory
  404. });
  405. throw error;
  406. }
  407. tracer.trace.end({
  408. name,
  409. id,
  410. cat: defaultCategory
  411. });
  412. return r;
  413. };
  414. default:
  415. break;
  416. }
  417. };
  418. module.exports = ProfilingPlugin;
  419. module.exports.Profiler = Profiler;