Skip to content

Commit

Permalink
Use windows-process-tree module to list processes, #46433
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachel Macfarlane committed Apr 3, 2018
1 parent 0e85af6 commit 234e95e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 279 deletions.
1 change: 0 additions & 1 deletion build/gulpfile.hygiene.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const indentationFilter = [
'!src/vs/base/common/marked/marked.js',
'!src/vs/base/common/winjs.base.js',
'!src/vs/base/node/terminateProcess.sh',
'!src/vs/base/node/ps-win.ps1',
'!test/assert.js',

// except specific folders
Expand Down
2 changes: 1 addition & 1 deletion build/gulpfile.vscode.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const vscodeResources = [
'out-build/paths.js',
'out-build/vs/**/*.{svg,png,cur,html}',
'out-build/vs/base/common/performance.js',
'out-build/vs/base/node/{stdForkStart.js,terminateProcess.sh,ps-win.ps1}',
'out-build/vs/base/node/{stdForkStart.js,terminateProcess.sh}',
'out-build/vs/base/browser/ui/octiconLabel/octicons/**',
'out-build/vs/workbench/browser/media/*-theme.css',
'out-build/vs/workbench/electron-browser/bootstrap/**',
Expand Down
183 changes: 0 additions & 183 deletions src/vs/base/node/ps-win.ps1

This file was deleted.

129 changes: 35 additions & 94 deletions src/vs/base/node/ps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

'use strict';

import { spawn, exec } from 'child_process';
import * as path from 'path';
import * as nls from 'vs/nls';
import URI from 'vs/base/common/uri';
import { exec } from 'child_process';

export interface ProcessItem {
name: string;
Expand Down Expand Up @@ -121,32 +118,6 @@ export function listProcesses(rootPid: number): Promise<ProcessItem> {

if (process.platform === 'win32') {

console.log(nls.localize('collecting', 'Collecting CPU and memory information. This might take a couple of seconds.'));

interface ProcessInfo {
type: 'processInfo';
name: string;
processId: number;
parentProcessId: number;
commandLine: string;
handles: number;
cpuLoad: number[];
workingSetSize: number;
}

interface TopProcess {
type: 'topProcess';
name: string;
processId: number;
parentProcessId: number;
commandLine: string;
handles: number;
cpuLoad: number[];
workingSetSize: number;
}

type Item = ProcessInfo | TopProcess;

const cleanUNCPrefix = (value: string): string => {
if (value.indexOf('\\\\?\\') === 0) {
return value.substr(4);
Expand All @@ -161,75 +132,45 @@ export function listProcesses(rootPid: number): Promise<ProcessItem> {
}
};

const execMain = path.basename(process.execPath);
const script = URI.parse(require.toUrl('vs/base/node/ps-win.ps1')).fsPath;
const commandLine = `& {& '${script}' -ProcessName '${execMain}' -MaxSamples 3}`;
const cmd = spawn('powershell.exe', ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command', commandLine]);

let stdout = '';
let stderr = '';
cmd.stdout.on('data', data => {
stdout += data.toString();
});
(import('windows-process-tree')).then(windowsProcessTree => {
windowsProcessTree.getProcessList(rootPid, (processList) => {
windowsProcessTree.getProcessCpuUsage(processList, (completeProcessList) => {
const processItems: Map<number, ProcessItem> = new Map();
completeProcessList.forEach(process => {
const commandLine = cleanUNCPrefix(process.commandLine);
processItems.set(process.pid, {
name: findName(commandLine),
cmd: commandLine,
pid: process.pid,
ppid: process.ppid,
load: process.cpu,
mem: process.memory
});
});

cmd.stderr.on('data', data => {
stderr += data.toString();
});
rootItem = processItems.get(rootPid);
if (rootItem) {
processItems.forEach(item => {
let parent = processItems.get(item.ppid);
if (parent) {
if (!parent.children) {
parent.children = [];
}
parent.children.push(item);
}
});

cmd.on('exit', () => {
if (stderr.length > 0) {
reject(new Error(stderr));
return;
}
let processItems: Map<number, ProcessItem> = new Map();
try {
const items: Item[] = JSON.parse(stdout);
for (const item of items) {
if (item.type === 'processInfo') {
let load = 0;
if (item.cpuLoad) {
for (let value of item.cpuLoad) {
load += value;
processItems.forEach(item => {
if (item.children) {
item.children = item.children.sort((a, b) => a.pid - b.pid);
}
load = load / item.cpuLoad.length;
} else {
load = -1;
}
let commandLine = cleanUNCPrefix(item.commandLine);
processItems.set(item.processId, {
name: findName(commandLine),
cmd: commandLine,
pid: item.processId,
ppid: item.parentProcessId,
load: load,
mem: item.workingSetSize
});
resolve(rootItem);
} else {
reject(new Error(`Root process ${rootPid} not found`));
}
}
rootItem = processItems.get(rootPid);
if (rootItem) {
processItems.forEach(item => {
let parent = processItems.get(item.ppid);
if (parent) {
if (!parent.children) {
parent.children = [];
}
parent.children.push(item);
}
});
processItems.forEach(item => {
if (item.children) {
item.children = item.children.sort((a, b) => a.pid - b.pid);
}
});
resolve(rootItem);
} else {
reject(new Error(`Root process ${rootPid} not found`));
}
} catch (error) {
console.log(stdout);
reject(error);
}
});
}, windowsProcessTree.ProcessDataFlag.CommandLine | windowsProcessTree.ProcessDataFlag.Memory);
});
} else { // OS X & Linux

Expand Down

0 comments on commit 234e95e

Please sign in to comment.