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

Posted liulun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vscode源码分析程序的启动逻辑,性能问题的追踪相关的知识,希望对你有一定的参考价值。

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

启动追踪

代码文件:src\\main.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秒窗口还没启动,那么也会结束性能问题的追踪)
代码文件:vs\\code\\electron-main\\app.ts
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:\\n0", 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秒,那么久只记日志了)

 

 

以上是关于vscode源码分析程序的启动逻辑,性能问题的追踪的主要内容,如果未能解决你的问题,请参考以下文章

vscode源码分析窗口里的主要元素

vscode源码分析程序的启动逻辑

链路追踪 SkyWalking 源码分析 —— 调试环境搭建

vscode源码分析主进程启动消息通信服务

pinpoint web报警机制源码解析

性能工具之JMeter5.0核心类StandardJMeterEngine源码分析