Repository navigation
Add option to collect cpu usage from processes - #13
Conversation
| pid: number, | ||
| name: string, | ||
| memory?: number, | ||
| cpu?: number, |
There was a problem hiding this comment.
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.
| getProcessTree(process.pid, (tree) => { | ||
| assert.equal(tree.name, 'node.exe'); | ||
| assert.equal(tree.pid, process.pid); | ||
| assert.notEqual(tree.cpu, undefined); |
There was a problem hiding this comment.
Maybe checking typeof cpu and 0 <= cpu <= 100 would be a better check?
| return kt.QuadPart + ut.QuadPart; | ||
| } | ||
|
|
||
| void GetCpuUsage(ProcessInfo process_info[1024], uint32_t* process_count, BOOL first_pass) { |
There was a problem hiding this comment.
I think this needs some comments explaining how the CPU time calculation works.
d377ac3 to
94469f5
Compare
| NONE = 0, | ||
| MEMORY = 1 | ||
| MEMORY = 1, | ||
| CPU = 2 |
There was a problem hiding this comment.
Remove CPU flag
| const requestQueue = []; | ||
| let cpuUsageRequestInProgress = false; | ||
|
|
||
| const requestQueue = { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
We should refactor this to share code with getProcessTree, I think we need to pass in a queue and a filter function
| * @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 |
There was a problem hiding this comment.
Indentation, we should add an editor config file :)
| callback: callback | ||
| }); | ||
|
|
||
| if (!cpuUsageRequestInProgress) { |
There was a problem hiding this comment.
I'm not sure we need this blocking flag
| FILETIME sysIdleTime, sysKernelTime, sysUserTime; | ||
| if (GetProcessTimes(hProcess, &creationTime, &exitTime, &kernelTime, &userTime) | ||
| && GetSystemTimes(&sysIdleTime, &sysKernelTime, &sysUserTime)) { | ||
| if (first_pass) { |
|
|
||
| FILETIME creationTime, exitTime, kernelTime, userTime; | ||
| FILETIME sysIdleTime, sysKernelTime, sysUserTime; | ||
| if (GetProcessTimes(hProcess, &creationTime, &exitTime, &kernelTime, &userTime) |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Comment to explain function
| #include <nan.h> | ||
| #include "process.h" | ||
| #include "worker.h" | ||
| #include <cmath> |
There was a problem hiding this comment.
Is this being used?
There was a problem hiding this comment.
Yeah, it imports isnan
Daniel Imms (Tyriar)
left a comment
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Let's start filling in the JS Doc here as the API is getting a little more complex
READY : Update node-gyp before npm install
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++.