-
Notifications
You must be signed in to change notification settings - Fork 284
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
Using process.cpuUsage to get percentage #283
Comments
Sorry no one has responded in 10 days. You might consider opening an issue for this in the main repo. |
Sad |
I don't know, seems like they got a pretty good response in nodejs/node#8728. |
(Locking this issue. For legit help questions, feel free to open a new issue.) |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I'm trying to find the process' percentage of cpu usage of the system and the values that I'm getting are way off from what system process managers report: e.g. top/htop and activity monitor
First approach: adding user and system time and dividing by elapsed time - as seen here nodejs/node#6157 (comment)
This example does not work due to passing invalid arguments to
secNSec2ms
Fixing those issues and creating a loop so that I can compare it to other monitors ends up with this:
Now on my macbook pro (2.9 GHz Intel Core i5 - dual core but each core has 2 hyperthreads so it is effectively 4 cores, and
os.cpus()
would report 4 cpus), this consistently gives about 99.7%.Changing this line to account for number of cores
var cpuPercent = (100 * (elapUserMS + elapSystMS) / elapTimeMS).toFixed(1) + '%'
gives 24.9 pretty consistently. However htop shows usage for that process at around 32% and activity monitor ranges from 26-45%. Which makes me think that a lot is not accounted for.If I save the values so that the sleeping time is included and rotated:
then the percentage is consistently 8.3%, but htop sees about 35% and activity monitor ranges from 25-45 again.
I tried another method in an express app where I stored previous values of
process.cpuUsage()
andos.cpus()
and set a timer for every 1000ms where I would compute the percentage and update the previous values. This involved adding all of the times fromos.cpus()
(which are millis) and taking the difference from the previous values to get the total cpu time in that period (and would account for number of cores). Then I used that instead of hrtime, but these results seemed to be significantly larger. Monitors showed about 0.1 to 0.2% but the node app's reported usage ranged from 1 to 45%.Is measuring elapsed time with
hrtime
the right way to do this natively?From searching the web, it looks like a lot of libraries are just reading in process tables or executing shell commands to get the percentage.
The text was updated successfully, but these errors were encountered: