KEMBAR78
Add option to collect cpu usage from processes by RMacfarlane · Pull Request #13 · microsoft/vscode-windows-process-tree · GitHub
Skip to content

Conversation

@RMacfarlane
Copy link
Contributor

Calculates the cpu usage of a process as the change in kernel + user time the process has used over the change in kernel + user time the system has used after sleeping for a second.

Currently this is being calculated for all processes when the flag is set instead of just the processes under rootId, as I was having trouble serializing the structure of cpu information back and forth between javascript and C++.

@RMacfarlane RMacfarlane self-assigned this Mar 16, 2018
@RMacfarlane RMacfarlane requested a review from Tyriar March 16, 2018 21:46
pid: number,
name: string,
memory?: number,
cpu?: number,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the API should look something like this (after the TS change is merged):

export interface ProcessCpuTreeNode extends ProcessTreeNode {
  cpu: number
};
export function getProcessCpuUsage(tree: ProcessTreeNode, callback: (tree: ProcessCpuTreeNode): void;

That way we don't need to complicate getProcessTree and everything is separated nicely. Also the small amount of time needed to cross the native boundary doesn't matter since this is a long running async operation.

test.js Outdated
getProcessTree(process.pid, (tree) => {
assert.equal(tree.name, 'node.exe');
assert.equal(tree.pid, process.pid);
assert.notEqual(tree.cpu, undefined);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe checking typeof cpu and 0 <= cpu <= 100 would be a better check?

src/process.cc Outdated
return kt.QuadPart + ut.QuadPart;
}

void GetCpuUsage(ProcessInfo process_info[1024], uint32_t* process_count, BOOL first_pass) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs some comments explaining how the CPU time calculation works.

src/process.h Outdated
NONE = 0,
MEMORY = 1
MEMORY = 1,
CPU = 2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove CPU flag

lib/index.ts Outdated
const requestQueue = [];
let cpuUsageRequestInProgress = false;

const requestQueue = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should type this so it's clear what they do.

// This prevents too many requests from being made, there is also a crash that
// can occur when performing multiple calls to CreateToolhelp32Snapshot at
// once.
if (!requestInProgress) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should refactor this to share code with getProcessTree, I think we need to pass in a queue and a filter function

lib/index.ts Outdated
* @param callback The callback to use with the returned list of processes
*/
export function getProcessCpuUsage(processList: IProcessInfo[], callback: (tree: IProcessCpuInfo[]) => void): void {
// Push the request to the queue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation, we should add an editor config file :)

lib/index.ts Outdated
callback: callback
});

if (!cpuUsageRequestInProgress) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need this blocking flag

src/process.cc Outdated
FILETIME sysIdleTime, sysKernelTime, sysUserTime;
if (GetProcessTimes(hProcess, &creationTime, &exitTime, &kernelTime, &userTime)
&& GetSystemTimes(&sysIdleTime, &sysKernelTime, &sysUserTime)) {
if (first_pass) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation


FILETIME creationTime, exitTime, kernelTime, userTime;
FILETIME sysIdleTime, sysKernelTime, sysUserTime;
if (GetProcessTimes(hProcess, &creationTime, &exitTime, &kernelTime, &userTime)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to handle when the if condition is false (cpu = undefined?)

}

ULONGLONG GetTotalTime(const FILETIME* kernelTime, const FILETIME* userTime) {
ULARGE_INTEGER kt, ut;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment to explain function

@Tyriar Tyriar added this to the 0.2.0 milestone Mar 20, 2018
#include <nan.h>
#include "process.h"
#include "worker.h"
#include <cmath>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this being used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it imports isnan

Copy link
Member

@Tyriar Tyriar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after the 2 remaining comments, great job 😃


export function getProcessTree(rootPid: number, callback: (tree: IProcessTreeNode) => void, flags?: ProcessDataFlag): void;

export function getProcessList(rootPid: number, callback: (processList: IProcessInfo[]) => void, flags?: ProcessDataFlag): void;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's start filling in the JS Doc here as the API is getting a little more complex

@RMacfarlane RMacfarlane merged commit 2d6a504 into master Mar 22, 2018
@RMacfarlane RMacfarlane deleted the rmacfarlane/cpu branch March 22, 2018 17:58
vmihdal pushed a commit to vmihdal/vscode-windows-process-tree that referenced this pull request Aug 13, 2021
READY : Update node-gyp before npm install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants