vscode源码分析【三】程序的启动逻辑,性能问题的追踪

第一篇: vscode源码分析【一】从源码运行vscode
第二篇:vscode源码分析【二】程序的启动逻辑,第一个窗口是如何创建的

启动追踪

代码文件:srcmain.js
如果指定了特定的启动参数:trace
vscode会在启动之初,执行下面的代码:

const contentTracing = require('electron').contentTracing;
		const traceOptions = {
			categoryFilter: args['trace-category-filter'] || '*',
			traceOptions: args['trace-options'] || 'record-until-full,enable-sampling'
		};
		contentTracing.startRecording(traceOptions, () => onReady());

这段代码的主要目的是:从Chromium的内容模块收集跟踪数据,以查找性能瓶颈和程序执行缓慢的操作。
注意,这个操作只能在app.ready事件触发之后才能执行; startRecoding会异步请求所有子进程开始执行追踪操作;
一旦所有子进程都确认了主进程的请求,主进程就会执行startRecoding的回调方法;

结束追踪

在窗口成功启动之后,vscode结束了性能问题的追踪(如果30秒窗口还没启动,那么也会结束性能问题的追踪)
代码文件:vscodeelectron-mainapp.ts(在上一篇博文中,启动第一个窗口,也是在这里执行的)

const windows = appInstantiationService.invokeFunction(accessor => this.openFirstWindow(accessor, electronIpcServer, sharedProcessClient));

stopTracingEventually方法的代码为:

	private stopTracingEventually(windows: ICodeWindow[]): void {
		this.logService.info(`Tracing: waiting for windows to get ready...`);

		let recordingStopped = false;
		const stopRecording = (timeout: boolean) => {
			if (recordingStopped) {
				return;
			}

			recordingStopped = true; // only once

			contentTracing.stopRecording(join(homedir(), `${product.applicationName}-${Math.random().toString(16).slice(-4)}.trace.txt`), path => {
				if (!timeout) {
					if (this.windowsMainService) {
						this.windowsMainService.showMessageBox({
							type: 'info',
							message: localize('trace.message', "Successfully created trace."),
							detail: localize('trace.detail', "Please create an issue and manually attach the following file:
{0}", path),
							buttons: [localize('trace.ok', "Ok")]
						}, this.windowsMainService.getLastActiveWindow());
					}
				} else {
					this.logService.info(`Tracing: data recorded (after 30s timeout) to ${path}`);
				}
			});
		};

		// Wait up to 30s before creating the trace anyways
		const timeoutHandle = setTimeout(() => stopRecording(true), 30000);

		// Wait for all windows to get ready and stop tracing then
		Promise.all(windows.map(window => window.ready())).then(() => {
			clearTimeout(timeoutHandle);
			stopRecording(false);
		});
	}

子进程会缓存跟踪数据,一般不会把跟踪数据发送给主进程(避免发送数据再造成性能消耗),
所以,结束跟踪也是主进程异步地要求所有子进程持久化跟踪数据的。
跟踪结束后,会执行stopRecording的回调函数。
在这里会显示一个提示框,提示用户性能追踪的结果;(如果超了30秒,那么就只记日志了)

 

原文地址:https://www.cnblogs.com/liulun/p/11044738.html