第一篇: 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秒,那麼就只記日誌了)
異步