Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial stab at process.cpuUsage() #5565

Closed
wants to merge 1,304 commits into from
Closed

Conversation

pmuellr
Copy link

@pmuellr pmuellr commented Mar 4, 2016

Pull Request check-list

Please make sure to review and check all of these items:

  • Does make -j8 test (UNIX) or vcbuild test nosign (Windows) pass with
    this change (including linting)?
  • Is the commit message formatted according to CONTRIBUTING.md?
  • If this change fixes a bug (or a performance problem), is a regression
    test (or a benchmark) included?
  • Is a documentation update included (if this change modifies
    existing APIs, or introduces new ones)?

NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.

Affected core subsystem(s)

process

Description of change

Adds the function cpuUsage() to the global process object. Proposed doc is in the PR, and immediate below:

process.cpuUsage()

Returns the user and system cpu time usage of the current process, in an object with properties user and system, whose values are arrays of integers as [seconds, microseconds] tuples. These values measure time spent in user and system code respectively, and may end up being greater than actual elapsed time if multiple cpu cores are performing work for this process.

console.log(process.cpuUsage());
// { user: [ 0, 43979 ], system: [ 0, 12423 ] }

setTimeout(() => {
  var startTime = process.hrtime();

  // spin the CPU for 500 milliseconds
  while (true) {
    var time = process.hrtime(startTime);

    // calculate elapsed wall clock time in millis
    var elapsed = time[0] * 1000 + time[1] / 1000 / 1000;

    if (elapsed > 500) break;
  }

  console.log(process.cpuUsage());
  // { user: [ 0, 554966 ], system: [ 0, 14881 ] }
}, 1000);

@vkurchatkin vkurchatkin added the semver-minor PRs that contain new features and should be released in the next minor version. label Mar 4, 2016
@mscdex mscdex added the process Issues and PRs related to the process subsystem. label Mar 4, 2016
Local<ArrayBuffer> ab = args[0].As<Uint32Array>()->Buffer();
uint32_t* fields = static_cast<uint32_t*>(ab->GetContents().Data());

// results passed as split values, reassembed in JS
Copy link
Contributor

Choose a reason for hiding this comment

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

s/reassembed/reassembled/

@Trott
Copy link
Member

Trott commented Mar 4, 2016

Should get at least a minimal test that confirms the object returned by process.cpuUsage() conforms with what is documented.

@pmuellr
Copy link
Author

pmuellr commented Mar 4, 2016

Should get at least a minimal test that confirms the object returned by process.cpuUsage() conforms with what is documented.

absolutely @Trott

Figured I'd do the minimal work to see if there's even interest in this, if the approach is generally correct, etc.

The most basic test is to ensure that subsequent values are monotonically increasing, or the same. Would be nice to have one that slept for a 100 ms or so, and then made sure the old / new values were within some range of reasonable-ness, but I fear could easily become flaky. I will also peek at libuv's tests for this.

BTW, purty sure this my first Node.js PR!

@Trott Trott added the wip Issues and PRs that are still a work in progress. label Mar 4, 2016
@evanlucas
Copy link
Contributor

I really like the idea, but should we be adding this onto process? I think we should try to avoid adding anything to process unless absolutely necessary. Maybe a metrics builtin or something like that?

@YurySolovyov
Copy link

Maybe put it to os module?

@mscdex
Copy link
Contributor

mscdex commented Mar 5, 2016

The os module is typically for more global stuff. I think process is a good fit for it, whether it should go directly on it or whether it and memoryUsage() would go on a new .metrics-like namespace like @evanlucas suggested.

// to access ru_utime (user CPU time used) and ru_stime (system CPU time used),
// which are uv_timeval_t structs (long tv_sec, long tv_usec) as tuples
// similar to process.hrtime() values. Only it's seconds/microseconds instead
// of hrtime()'s seconds/nanoseconds.
Copy link
Contributor

Choose a reason for hiding this comment

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

To avoid confusion in user land (ie: to maintain a level of consistency), would it make sense to turn them into nanoseconds?

Copy link
Author

Choose a reason for hiding this comment

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

Have to admit, I'm a bit torn on this:

  • on one hand, rusage() returns seconds / microseconds, and so I'd kinda like to match that
  • on the other hand, process.hrtime() returns [seconds, nanoseconds], so would be nice to be consistent

I'm kinda leaning towards your suggestion, returning [seconds, nanoseconds], especially since folks may be using both hrtime() and cpuUsage() together, to get a "cpu usage %" type value.

If we want to do that, then I think we'd just doc that the resolution is currently at the microsecond level, but the value returned is nanoseconds.

Copy link
Author

Choose a reason for hiding this comment

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

If we align more with hrtime(), perhaps it also makes sense to provide a "diff" function, like hrtime(), where if you pass a previous result of cpuUsage() into the function, it will return a "diff" of the times, instead of the current time. For one of the primary use cases I'm considering, I'd be wanting a diff anyway, so this cuts down a little bit on the amount of code I'd be writing.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's interesting. Would love to see some feedback from others on this.

Copy link
Contributor

Choose a reason for hiding this comment

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

generally +1. I'd favor nanoseconds for consistency also. Also +1 on the diffing. Would be nicely consistent to vanilla js also.

Just a quick thought: Maybe it's worth taking this even further, whether let p = process.cpuUsage(); p.diff(); p.diff() might be a thing.

@jasnell
Copy link
Member

jasnell commented Mar 7, 2016

Needs some form of test case but generally LGTM

@Fishrock123
Copy link
Contributor

@pmuellr Could you go over the checklist in the OP? Thanks! :D

@pmuellr
Copy link
Author

pmuellr commented Mar 8, 2016

@Fishrock123 sure; let me refresh the code given the changes mentioned here (still in process, but doing the diff, and using nanos instead of micros), add some tests, and I'll ensure the checklist is ready to go. Prolly end of this week.

Fishrock123 and others added 16 commits March 9, 2016 12:59
PR-URL: nodejs#5628
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: James M Snell <jasnell@gmail.com>
Avoid putting github templates in the source tarballs.

PR-URL: nodejs#5612
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: nodejs#5622
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Fixes: nodejs#5322
PR-URL: nodejs#5641
Reviewed-By: Robert Lindstädt <robert.lindstaedt@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Instead of leaking the arguments object by passing it as an
argument to a function, copy it's contents to a new array,
then pass the array. This allows V8 to optimize the function
that contains this code, improving performance.

PR-URL: nodejs#4361
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
We recently sandboxed the release CI jobs to their own Jenkins instance
This commit updates the links found in `doc/releases.md` to point
people in the right direction.

PR-URL: nodejs#5632
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: James M Snell <jasnell@gmail.com>
Invoke MSBuild specifying the target platform as generated by Gyp.

Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: nodejs#5627
PR-URL: nodejs#5662
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
PR-URL: nodejs#5665
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Add benjamingr to collaborator list.
Related nodejs#5064

PR-URL: nodejs#5664
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
PR-URL: nodejs#5666
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Glen Keane <glenkeane.94@gmail.com>
PR-URL: nodejs#5668
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
PR-URL: nodejs#5663
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Jeremy Whitlock <jwhitlock@apache.org>
PR-URL: nodejs#5667

Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit adds tests for several known issues.

Refs: nodejs#1901
Refs: nodejs#728
Refs: nodejs#4778
Refs: nodejs#947
Refs: nodejs#2734
PR-URL: nodejs#5653
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reintroduces an intentional typo in a process doc example.

Fixes: nodejs#5644
PR-URL: nodejs#5654
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
BrendonPierson and others added 26 commits April 7, 2016 09:37
decipher.setAuthPadding canged to decipher.setAutoPadding

PR-URL: nodejs#6041
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Klauke <romaaan.git@gmail.com>
This allows third-party tools to check whether or not a handle that
can be unreferenced is unreferenced at a particular time.
Notably, this should be helpful for inspection via AsyncWrap.

Also, this is useful even to node's internals, particularly timers.

Refs: nodejs#5828
Refs: nodejs#5827
PR-URL: nodejs#5834
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
PR-URL: nodejs#6076
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Roman Klauke <romaaan.git@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
* due to: openssl bump in 1f43478

PR-URL: nodejs#6065
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
As requested in nodejs#5221

Original commit message:

  fix debug command processor wrt restart frame.

  R=jkummerow@chromium.org
  BUG=v8:4757
  LOG=N

  Review URL: https://codereview.chromium.org/1700693002

  Cr-Commit-Position: refs/heads/master@{nodejs#33983}

PR-URL: nodejs#6086
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
* use common.mustCall() to verify all tests have run
* eliminate unneeded removeTestFile()
* eliminate unneeded var leaking into global scope
* var -> const
* remove instance of let

PR-URL: nodejs#6050
Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: nodejs#6108
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
ParseArrayIndex() was requesting a Uint32Value(), but assigning it to an
in32_t. This caused slight differences in error message reported in edge
cases of argument parsing. Fixed by getting the IntegerValue() before
checking if the value is < 0. Added test of API that was affected.

PR-URL: nodejs#6084
Reviewed-By: James M Snell <jasnell@gmail.com>
Adds a new topic that provides an overview of the event loop, timers, and
`process.nextTick()` that is based upon a NodeSource "Need to Node" presentation
hosted by @trevnorris: Event Scheduling and the Node.js Event
Loop (https://nodesource.com/resources).

PR-URL: nodejs#4936
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Calvin W. Metcalf <calvin.metcalf@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
There is some unnecessary logic in repl.js. Remove it.

PR-URL: nodejs#6071
Reviewed-By: James M Snell <jasnell@gmail.com>
A win32-only test was verifying that path.win32._makeLong('C:')
would return the current working directory.  This would only work if
current working directory was also on the C: device.  Fix is to grab
the device letter for current working directory, and pass that to
_makeLong().

PR-URL: nodejs#6067
Reviewed-By: Trott - Rich Trott <rtrott@gmail.com>
Reviewed-By: Joao Reis <reis@janeasystems.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Rewriting npm shebang in install.py violates user expectations
among other things.

The shebang in npm.js is kept as #!/usr/bin/env node.

Fixes: nodejs#6095
PR-URL: nodejs#6098
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Fixes: nodejs#6080
PR-URL: nodejs#6124
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
The only test with modifications is `test-stdin-child-proc` that was
passing when it should not because the exit code of the child process
was not being checked.

PR-URL: nodejs#6087
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Claudio Rodriguez <cjrodr@yahoo.com>
This change was to add upon the algorithm description of path.format
by adding examples for unix systems that clarified behavior in
various scenarios.

PR-URL: nodejs#5838
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Klauke <romaaan.git@gmail.com>
Adds additional `targetStart`, `targetEnd`, `sourceStart,
and `sourceEnd` arguments to `Buffer.prototype.compare`
to allow comparison of sub-ranges of two Buffers without
requiring Buffer.prototype.slice()

Fixes: nodejs#521
PR-URL: nodejs#5880
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Introduced in commit 7d8882b, "handle_wrap: expose an `isRefed()` check
to JS".

PR-URL: nodejs#6129
Reviewed-By: Evan Lucas <evanlucas@me.com>
Fixes an issue that prevented scrolling from going past large code
blocks on iOS devices. Also fixes a few minor styling issues that
came up in the discussion.

Fixes: nodejs#5861
PR-URL: nodejs#5878
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Robert Lindstädt <robert.lindstaedt@gmail.com>
Doc tool produces incomplete json when it meets unordered lists that
directly following a heading. Add a default case to processList function
to handle the lists.

PR-URL: nodejs#5966
Fixes: nodejs#1545
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Robert Lindstädt <robert.lindstaedt@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Current tools/doc/json.js only supports one bracket style for optional
params methodName(param0[,param1],param2). Add support to other styles
such as methodName(param0,[param1,]param2) or
methodName(param0[,param1,param2]) or
methodName(param0[,param1[,param2]]).

PR-URL: nodejs#5977
Fixes: nodejs#5976
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Robert Lindstädt <robert.lindstaedt@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
This commit refines the documentation around child.kill(), where kill
attempts against shells will lead to unexpected results. Namely, on
linux the child process of a child process will not terminate, when
its parent gets terminated. This is different across the the
platforms.

PR-URL: nodejs#2098
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Closes: nodejs#2098
PR-URL: nodejs#6132
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: thefourtheye <thechargingvolcano@gmail.com>
PR-URL: nodejs#6132
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: thefourtheye <thechargingvolcano@gmail.com>
Clarify caveats on `maxBuffer` with regards to Unicode output.

Refs: nodejs#1901
PR-URL: nodejs#6030
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Currently we include instructions on how to check the sha of a
downloaded tar-ball, but do not include instruction on how to
get the `SHA256.txt` file. This has led to confusion with people
thinking that the SHA256.txt is included in that tarball.

This commit includes instructions on how to use curl to download the
`SHA256.txt` prior to the instructions on how to verify the sha.

Refs: nodejs/help#113
Refs: nodejs/help#137
PR-URL: nodejs#6120
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@pmuellr
Copy link
Author

pmuellr commented Apr 11, 2016

My git foo is weak. The rebases I've done on my clone/fork look ok, but in the PR, they're pulling in stuff from the old branch I started on (some 5.x branch, which was incorrect, shouda been master from the start).

So, giving up on this branch / PR.

I've got a clean branch / PR here as a replacement: #6157

@pmuellr pmuellr closed this Apr 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
process Issues and PRs related to the process subsystem. semver-minor PRs that contain new features and should be released in the next minor version. wip Issues and PRs that are still a work in progress.
Projects
None yet
Development

Successfully merging this pull request may close these issues.