• Stars
    star
    507
  • Rank 87,068 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cross-platform process cpu % and memory usage of a PID

pidusage

Lint MacOS Ubuntu Windows Alpine Code coverage npm version license

Cross-platform process cpu % and memory usage of a PID.

Synopsis

Ideas from https://github.com/arunoda/node-usage but with no C-bindings.

Please note that if you need to check a Node.JS script process cpu and memory usage, you can use process.cpuUsage and process.memoryUsage since node v6.1.0. This script remain useful when you have no control over the remote script, or if the process is not a Node.JS process.

Usage

var pidusage = require('pidusage')

pidusage(process.pid, function (err, stats) {
  console.log(stats)
  // => {
  //   cpu: 10.0,            // percentage (from 0 to 100*vcore)
  //   memory: 357306368,    // bytes
  //   ppid: 312,            // PPID
  //   pid: 727,             // PID
  //   ctime: 867000,        // ms user + system time
  //   elapsed: 6650000,     // ms since the start of the process
  //   timestamp: 864000000  // ms since epoch
  // }
  cb()
})

// It supports also multiple pids
pidusage([727, 1234], function (err, stats) {
  console.log(stats)
  // => {
  //   727: {
  //     cpu: 10.0,            // percentage (from 0 to 100*vcore)
  //     memory: 357306368,    // bytes
  //     ppid: 312,            // PPID
  //     pid: 727,             // PID
  //     ctime: 867000,        // ms user + system time
  //     elapsed: 6650000,     // ms since the start of the process
  //     timestamp: 864000000  // ms since epoch
  //   },
  //   1234: {
  //     cpu: 0.1,             // percentage (from 0 to 100*vcore)
  //     memory: 3846144,      // bytes
  //     ppid: 727,            // PPID
  //     pid: 1234,            // PID
  //     ctime: 0,             // ms user + system time
  //     elapsed: 20000,       // ms since the start of the process
  //     timestamp: 864000000  // ms since epoch
  //   }
  // }
})

// If no callback is given it returns a promise instead
const stats = await pidusage(process.pid)
console.log(stats)
// => {
//   cpu: 10.0,            // percentage (from 0 to 100*vcore)
//   memory: 357306368,    // bytes
//   ppid: 312,            // PPID
//   pid: 727,             // PID
//   ctime: 867000,        // ms user + system time
//   elapsed: 6650000,     // ms since the start of the process
//   timestamp: 864000000  // ms since epoch
// }

// Avoid using setInterval as they could overlap with asynchronous processing
function compute(cb) {
  pidusage(process.pid, function (err, stats) {
    console.log(stats)
    // => {
    //   cpu: 10.0,            // percentage (from 0 to 100*vcore)
    //   memory: 357306368,    // bytes
    //   ppid: 312,            // PPID
    //   pid: 727,             // PID
    //   ctime: 867000,        // ms user + system time
    //   elapsed: 6650000,     // ms since the start of the process
    //   timestamp: 864000000  // ms since epoch
    // }
    cb()
  })
}

function interval(time) {
  setTimeout(function() {
    compute(function() {
      interval(time)
    })
  }, time)
}

// Compute statistics every second:
interval(1000)

// Above example using async/await
const compute = async () => {
  const stats = await pidusage(process.pid)
  // do something
}

// Compute statistics every second:
const interval = async (time) => {
  setTimeout(async () => {
    await compute()
    interval(time)
  }, time)
}

interval(1000)

Compatibility

Property Linux FreeBSD NetBSD SunOS macOS Win AIX Alpine
cpu ℹ️
memory
pid
ctime
elapsed
timestamp

= Working ℹ️ = Not Accurate = Should Work = Not Working

Please if your platform is not supported or if you have reported wrong readings file an issue.

By default, pidusage will use procfile parsing on most unix systems. If you want to use ps instead use the usePs option:

pidusage(pid, {usePs: true})

API

pidusage(pids, [options = {}], [callback]) ⇒ [Promise.<Object>]

Get pid informations.

Kind: global function Returns: Promise.<Object> - Only when the callback is not provided. Access: public

Param Type Description
pids Number | Array.<Number> | String | Array.<String> A pid or a list of pids.
[options] object Options object. See the table below.
[callback] function Called when the statistics are ready. If not provided a promise is returned instead.

options

Setting the options programatically will override environment variables

Param Type Environment variable Default Description
[usePs] boolean PIDUSAGE_USE_PS false When true uses ps instead of proc files to fetch process information
[maxage] number PIDUSAGE_MAXAGE 60000 Max age of a process on history.

PIDUSAGE_SILENT=1 can be used to remove every console message triggered by pidusage.

pidusage.clear()

If needed this function can be used to delete all in-memory metrics and clear the event loop. This is not necessary before exiting as the interval we're registring does not hold up the event loop.

Related

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.

More Repositories

1

dpicker

A framework-agnostic minimal date picker
JavaScript
185
star
2

explorer

Explore and share. Highly-configurable directory listing made with nodejs.
JavaScript
178
star
3

esql

PHP Extended SQL
PHP
91
star
4

tmux-current-pane-hostname

Tmux plugin that enables displaying hostname and user of the current pane in your status bar.
Shell
70
star
5

fclone

Clone objects by dropping circular references
JavaScript
62
star
6

pm2-notify

SMTP PM2 notifier
JavaScript
60
star
7

pmu

PHP Monorepo Utility
PHP
53
star
8

relieve

Ease the implementation of multi processing accross your microservices
JavaScript
47
star
9

signalhubws

Signalhub implementation with websockets
JavaScript
41
star
10

node-rpi-monit

Light nodejs raspberry pi monitor (cpu, mem, temp, network etc)
JavaScript
37
star
11

rxrest

Reactive rest library
JavaScript
33
star
12

dat-daemon

Dat as a daemon
JavaScript
24
star
13

dotfiles

Dotfiles
Vim Script
20
star
14

pidusage-tree

Pidusage tree
JavaScript
18
star
15

IPCEE

IPC combined with EventEmitter
JavaScript
15
star
16

leverage-the-power-of-symfony-components-within-apiplatform

leverage-the-power-of-symfony-components-within-apiplatform
HTML
14
star
17

native-messaging-swarm

TCP/UDP swarm on top of WebExtension's native messaging protocol
JavaScript
13
star
18

promise-spawner

Lazy nodejs spawner with stdout/stderr streams and error handling through promises
JavaScript
12
star
19

caligo

Scalable URL obfuscator service
Go
11
star
20

jhaml

Stream based javascript implementation of Haml templating engine
JavaScript
10
star
21

symfony-messenger-redis

Redis adapter for symfony/messenger
PHP
10
star
22

api-platform-talk-beyond-rest-demo

The project behind my talk about "Api Platform beyond REST"
PHP
8
star
23

noel

Christmas thing
JavaScript
7
star
24

gulp-sym

Gulp symlink module
JavaScript
6
star
25

SeedBundle

Seed helper through symfony commands
PHP
5
star
26

nodejs-http-memtest

Memory graphs on a few http servers with nodejs
Shell
5
star
27

browser.runtime

nodejs bridge to support browser.runtime api
JavaScript
5
star
28

renice

Change cpu process priority
JavaScript
5
star
29

micro-virtual-list

Micro virtual list
JavaScript
5
star
30

EzSeed

Easy seedbox to share with your friends or your family !
PHP
5
star
31

vagrant-tutoriel-nginx-php-nodejs

Tutoriel vagrant php/nginx/nodejs
Ruby
5
star
32

learn-rxjs

todo
TypeScript
4
star
33

snake

🐍snake built with 🚂 choojs
JavaScript
4
star
34

esa

Edge side APIs for front-end developpers
HTML
4
star
35

audiocogs

Fork from audiocogs - DGPlayer which reads MP3 and FLAC - with playlist integration
JavaScript
4
star
36

api-platform-laravel

PHP
4
star
37

incolore

Image hoster
Go
3
star
38

ssestream

ServerSide event stream
JavaScript
3
star
39

cma

Cli mail
JavaScript
3
star
40

supervisordstat

Run pidstat over supervisord processes.
Go
3
star
41

maree-bass-arcade

Marée bass arcade
SCSS
3
star
42

choo-redux

Choo redux bridge
JavaScript
3
star
43

cqrs-api-platform

Attempt at making api platform cqrs compliant (WIP)
PHP
3
star
44

sse-livereload

Experimental livereload server that uses browserify to build and server side events to reload the page automatically.
JavaScript
3
star
45

thoughts-for-paris

Some thoughts for paris - Nov 13 2015
JavaScript
3
star
46

serializer-bench

PHP
3
star
47

gulp-xliff-to-json

Gulp xliff to json
JavaScript
2
star
48

api-platform-alice-sf5

PHP
2
star
49

laravel-api-platform

PHP
2
star
50

api-platform-by-examples

PHP
2
star
51

native-installer

Cross-browser native messaging installer for webextensions
JavaScript
2
star
52

ansi-rainbow

Rainbowify string with https://github.com/sindresorhus/ansi-styles
JavaScript
2
star
53

dotfiles-backup

My dot files
Vim Script
2
star
54

example-apiplatform-cqrs

PHP
2
star
55

pidusage-promise

Pidusage as a promise
JavaScript
1
star
56

TCPEE

TCP version of IPCEE
JavaScript
1
star
57

rockexplorer

Rust
1
star
58

tmdb

TheMovieDB nodejs API wrapper
JavaScript
1
star
59

relieve-failsafe

Relieve through tcp
JavaScript
1
star
60

explorer-m3u

M3u generation plugin for explorer
JavaScript
1
star
61

mdtobbcode

Markdown To BBCode
JavaScript
1
star
62

cronrs

Rust
1
star
63

lerna-babel-browserify-bootstrap

JavaScript
1
star
64

explorer-unrar

Explorer unrar plugin
JavaScript
1
star
65

first-existing-path

First existing path from an array
JavaScript
1
star
66

bootstrap-react-browserify

Bootstrap for react + browserify
HTML
1
star
67

api-platform-blackfire-sandbox

Sandbox for blackfire profiling with API Platform
TypeScript
1
star
68

nextjs-mdx-highlight

MDX
1
star
69

mapper

PHP object to object mapper
PHP
1
star
70

cours-http-pokemon

JavaScript
1
star
71

pong-mercure

test
JavaScript
1
star
72

tamagotchi

JavaScript
1
star
73

automapper

PHP
1
star
74

playground

test
JavaScript
1
star
75

php-wasm-example

PHP
1
star
76

repro6340

PHP
1
star
77

angular-testing

Todo app without tests to exercise (correction in the test branch)
TypeScript
1
star
78

empreinte-digitale-messenger

PHP
1
star
79

laravel

PHP
1
star
80

nextjs-mdx-standalone-memory

reproducer https://github.com/vercel/next.js/issues/57613
MDX
1
star
81

self-referencing-metadata-api-platform

Attempt to reproduce #3344 #3349 #3343 api-platform/core
PHP
1
star
82

shared-mem-wasm

C
1
star
83

formation-contrib

TypeScript
1
star
84

tamagotchi-1

JavaScript
1
star
85

api-platform-doctrine-mapper

PHP
1
star
86

symfony-tag-removal-repro

PHP
1
star
87

docs-website

Documentation website
1
star
88

state

API Platform State component
1
star
89

cours-http-pokedex

JavaScript
1
star
90

vulcain-caddy

Go
1
star
91

validator

API Platform validator component
1
star
92

my-api

TypeScript
1
star
93

my-test-api

TypeScript
1
star
94

secret-santa

Secret santa symfony command
PHP
1
star
95

api-platform-2316

see issue 2316
PHP
1
star
96

learn-ngrx

todo
TypeScript
1
star
97

mercure-php

Experimental php mercure draft implementation
PHP
1
star
98

chatbot

PHP
1
star
99

spotify-static-playlists

Generate a web page with your spotify playlists.
JavaScript
1
star
100

markdown-to-bbcode

Properly formatted markdown to bbcode. The core tags like b,i,h2,h3,code,img,url
HTML
1
star