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

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

启动追踪

代码文件:src\main.js
若是指定了特定的启动参数:trace
vscode会在启动之初,执行下面的代码:windows

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秒窗口还没启动,那么也会结束性能问题的追踪)
代码文件:vs\code\electron-main\app.ts(在上一篇博文中,启动第一个窗口,也是在这里执行的)app

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

stopTracingEventually方法的代码为:dom

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:\n{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秒,那么就只记日志了)

 异步

相关文章
相关标签/搜索