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(在上一篇博文中,启动第一个窗口,也是在这里执行的)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值