第一篇: 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(在上一篇博文中,啟動第一個窗口,也是在這里執行的)
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秒,那么就只記日志了)