Skip to content

Commit

Permalink
refactor: replace direct process.pid usage with getProcessId method (#…
Browse files Browse the repository at this point in the history
…213352)

* refactor: replace direct process.pid usage with getProcessId method

* remove console.log
  • Loading branch information
bpasero authored May 24, 2024
1 parent 36d9174 commit c1ebab9
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 8 deletions.
5 changes: 0 additions & 5 deletions src/vs/base/parts/sandbox/electron-sandbox/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ import { IpcRenderer, ProcessMemoryInfo, WebFrame } from 'vs/base/parts/sandbox/
*/
export interface ISandboxNodeProcess extends INodeProcess {

/**
* The process.pid property returns the process ID of the process.
*/
readonly pid: number;

/**
* The process.platform property returns a string identifying the operating system platform
* on which the Node.js process is running.
Expand Down
1 change: 0 additions & 1 deletion src/vs/base/parts/sandbox/electron-sandbox/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@
* @type {ISandboxNodeProcess}
*/
process: {
get pid() { return process.pid; },
get platform() { return process.platform; },
get arch() { return process.arch; },
get env() { return { ...process.env }; },
Expand Down
1 change: 1 addition & 0 deletions src/vs/platform/native/common/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export interface ICommonNativeHostService {
hasWSLFeatureInstalled(): Promise<boolean>;

// Process
getProcessId(): Promise<number | undefined>;
killProcess(pid: number, code: string): Promise<void>;

// Clipboard
Expand Down
5 changes: 5 additions & 0 deletions src/vs/platform/native/electron-main/nativeHostMainService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,11 @@ export class NativeHostMainService extends Disposable implements INativeHostMain

//#region Process

async getProcessId(windowId: number | undefined): Promise<number | undefined> {
const window = this.windowById(undefined, windowId);
return window?.win?.webContents.getOSProcessId();
}

async killProcess(windowId: number | undefined, pid: number, code: string): Promise<void> {
process.kill(pid, code);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { VSBuffer } from 'vs/base/common/buffer';
import { IWorkspaceTrustManagementService } from 'vs/platform/workspace/common/workspaceTrust';
import { IPaneCompositePartService } from 'vs/workbench/services/panecomposite/browser/panecomposite';
import { StartupTimings } from 'vs/workbench/contrib/performance/browser/startupTimings';
import { process } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { coalesce } from 'vs/base/common/arrays';

interface ITracingData {
Expand Down Expand Up @@ -160,6 +159,7 @@ export class NativeStartupTimings extends StartupTimings implements IWorkbenchCo
return undefined; // unexpected arguments for startup heap statistics
}

const windowProcessId = await this._nativeHostService.getProcessId();
const used = (performance as unknown as { memory?: { usedJSHeapSize?: number } }).memory?.usedJSHeapSize ?? 0; // https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory

let minorGCs = 0;
Expand All @@ -170,7 +170,7 @@ export class NativeStartupTimings extends StartupTimings implements IWorkbenchCo
try {
const traceContents: { traceEvents: ITracingData[] } = JSON.parse((await this._fileService.readFile(URI.file(this._environmentService.args['trace-startup-file']))).value.toString());
for (const event of traceContents.traceEvents) {
if (event.pid !== process.pid) {
if (event.pid !== windowProcessId) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export class TestNativeHostService implements INativeHostService {
async getOSVirtualMachineHint(): Promise<number> { return 0; }
async getOSColorScheme(): Promise<IColorScheme> { return { dark: true, highContrast: false }; }
async hasWSLFeatureInstalled(): Promise<boolean> { return false; }
async getProcessId(): Promise<number> { throw new Error('Method not implemented.'); }
async killProcess(): Promise<void> { }
async setDocumentEdited(edited: boolean): Promise<void> { }
async openExternal(url: string): Promise<boolean> { return false; }
Expand Down

0 comments on commit c1ebab9

Please sign in to comment.