Giter VIP home page Giter VIP logo

vscode-windows-process-tree's Introduction

@vscode/windows-process-tree

Build status

npm version

A Node.js library that enables quickly fetching process tree information for a particular process ID on Windows.

Usage

import * as child_process from 'child_process';
import { getProcessTree } from '@vscode/windows-process-tree';

if (process.platform === 'win32') {
  child_process.spawn('cmd.exe');
  getProcessTree(process.pid, (tree) => {
    console.log(tree);
  });
  // { pid: 11168,
  //   name: 'node.exe',
  //   children:
  //    [ { pid: 1472, name: 'cmd.exe', children:[] },

  getProcessTree(0, (tree) => {
    console.log(tree);
  });
  // undefined
}

For the full API look at the typings file.

Why a native node module?

The current convention is to run wmic.exe to query a particular process ID and then parse the output like so:

let cp = require('child_process');

function getChildProcessDetails(pid, cb) {
    let args = ['process', 'where', `parentProcessId=${pid}`, 'get', 'ExecutablePath,ProcessId'];
    cp.execFile('wmic.exe', args, (err, stdout, stderr) => {
        if (err) {
            throw new Error(err);
        }
        if (stderr.length > 0) {
            cb([]);
        }
        var childProcessLines = stdout.split('\n').slice(1).filter(str => !/^\s*$/.test(str));
        var childProcessDetails = childProcessLines.map(str => {
            var s = str.split('  ');
            return { executable: s[0], pid: Number(s[1]) };
        });
        cb(childProcessDetails);
    });
}

This has two problems:

  1. It takes > 100ms* to spin up a process and get the output returned.
  2. It only goes one level deep. Meaning, if the process tree is deeply nested or processes in the tree have many children it will take a lot more time and need a bunch of processes launched.

Both of which are only exacerbated by the fact that this information is something that a consumer may want to poll for.

The native node module uses Windows APIs to get the process details and then they are organized into a tree, getting the entire tree's details in < 20ms*.

* On my machine ๐Ÿ™‚

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

vscode-windows-process-tree's People

Contributors

bpasero avatar deepak1556 avatar dependabot[bot] avatar eugeny avatar joaomoreno avatar lixire avatar lramos15 avatar meganrogge avatar microsoft-github-policy-service[bot] avatar rwe avatar rzhao271 avatar sbatten avatar tyriar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vscode-windows-process-tree's Issues

Invocations to `getProcessTree`/`getProcessList` with different flags are confused

Related to (but neither caused nor fixed by): #46.

Filing because I noticed the issue by inspection, not from a particular known defect.

Basically, because of the gating to native.getProcessList, if one invocation is called with one set of flags, and then with another is made before the first returns, the second invocation will get results from the wrong flags. This could confuse code in cases where e.g. you requested memory info and occasionally do not receive it seemingly randomly.

Instead, there should probably be logic along the lines of:

  • different queues for different sets of flags
  • on completion of an invocation, callbacks run for all queues with fully-satisfied flags
  • if there are queued requests for additional flags, re-run with those flags immediately

Or something like that. Or just always run with all the flags, unless there's a performance reason not to.

More metrics

Hi!

This is a follow up on simonepri/pidtree#6. I'm the original author of pidusage (written for pm2 back in the days). pidusage is a library that facilitate the access to cpu and memory usage, for a given pid, being cross-platform.

For windows, we're spawning wmic.exe to get the following metrics:

    'CreationDate,KernelModeTime,ParentProcessId,ProcessId,UserModeTime,WorkingSetSize'

It works well but is god damn slow!

Would it be possible to add these additional metrics in this module? I'm really not familiar with windows nor with C++ programming on windows or I'd definitely do it myself. This would be a huge improvement for windows users that use pidusage to get cross-platform metrics.

Thanks!

Access violation crash

Unknown: windows_process_tree.node - node.dll - ucrtbase.dll - kernel32.dll - ntdll.dll
NDK - EXCEPTION_ACCESS_VIOLATION_WRITE

RangeError: Maximum call stack size exceeded

@Tyriar from error telemetry (https://ticino-errors.azurewebsites.net/Errors?bucketIdHash=de687ba5-8cd1-98c8-612d-2bea31e9853e):

RangeError: Maximum call stack size exceeded
at Array.findIndex (native)
at buildProcessTree (\node_modules.asar\windows-process-tree\lib\index.js:12:31) 
at rootPid.children.childIndexes.map.c (\node_modules.asar\windows-process-tree\lib\index.js:22:37) 
at Array.map (native)
at buildProcessTree (\node_modules.asar\windows-process-tree\lib\index.js:22:28) 
at rootPid.children.childIndexes.map.c (\node_modules.asar\windows-process-tree\lib\index.js:22:37) 
at Array.map (native)
at buildProcessTree (\node_modules.asar\windows-process-tree\lib\index.js:22:28) 
at rootPid.children.childIndexes.map.c (\node_modules.asar\windows-process-tree\lib\index.js:22:37) 
at Array.map (native)

Avoid EnumProcesses call?

CreateToolhelp32Snapshot appears to also get all processes so we probably don't need to enumerate all processes.

If the tree is computed in native, this could also avoid opening handles all of them. Instead handles to fetch the process name could be done just for the processes that are in the tree.

'Nan::Callback::Call': was declared deprecated

Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
  addon.cc
  process.cc
..\src\addon.cc(88): warning C4996: 'Nan::Callback::Call': was declared deprecated [C:\projects\vscode-windows-process-tree\build\windows_process_tree.vcxproj]
  C:\projects\vscode-windows-process-tree\node_modules\nan\nan.h(1618): note: see declaration of 'Nan::Callback::Call'
  worker.cc
  process_commandline.cc
..\src\worker.cc(54): warning C4996: 'Nan::Callback::Call': was declared deprecated [C:\projects\vscode-windows-process-tree\build\windows_process_tree.vcxproj]
  C:\projects\vscode-windows-process-tree\node_modules\nan\nan.h(1618): note: see declaration of 'Nan::Callback::Call'

Give full executable path for processes

First of all, this really speeds up the terminal title updates. Great job!

However, each process's name contains only the basename of the executable. For microsoft/vscode#31234, I need the full path to the executable.

The documention recommends querying modules to give the full executable path, rather than querying programs. However it seems that would require special logic to handle both 32 bit and 64 bit programs. Retrieving the program's name from its executable might also work (maybe have a function attached to each program result?), given that in the context of the PR I only need the full executable of one program.

failed: Module did not self-register

I am Building an Extension for VSCode and whenever I try to call the getProcessTree method I get the below error.
I have tried creating a new extension but the error remains the same.
I have also installed node again.

Error:
failed: Module did not self-register: '\\?\d:\Github\Test-extension\test\node_modules\windows-process-tree\build\Release\windows_process_tree.node

Node version's tested: 16.3.0 & 14.17.1
Windows Build Version: 19043.1081

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.