npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@adobe/cgroup-metrics

v3.0.6

Published

Node Module to read cgroup memory data

Downloads

2,995

Readme

Version License codecov Travis

CGROUP-METRICS

Node Module for reading cgroup metrics. Reads from /sys/fs/cgroup/.

Memory Metrics:

Memory reads from path /sys/fs/cgroup/memory/memory:

Raw values:

  • stat.rss: # of bytes of anonymous and swap cache memory
  • kmem.usage_in_bytes: current kernel memory allocation
  • limit_in_bytes: limit of memory usage

Calculated values:

  • containerUsage(): stats.rss + kmem.usage_in_bytes
  • containerUsagePercentage():stats.rss + kmem.usage_in_bytes / limit_in_bytes

CPU Metrics:

Raw CPU values: CPU reads from path /sys/fs/cgroup/:

  • cpuacct.usage: total CPU time (in nanoseconds) since the start of the container obtained by this cgroup (CPU time obtained by all the tasks) in the system
  • cpuacct.stat: reports the user and system CPU time consumed by all tasks in this cgroup (including tasks lower in the hierarchy)
    • user: CPU time (in nanoseconds) spent by tasks of the cgroup in user mode
    • system: CPU time (in nanoseconds) spent by tasks of the cgroup in kernel mode
    • timestamp: timestamp of when the measurement was taken

Both calls will return an object containing one or more CpuMetric objects for a specific cpu task:

  • cpuNanosSinceContainerStart: total CPU time (in nanoseconds) since the start of the container obtained by this cgroup in the system
  • timestamp: timestamp of when the measurement was taken

Calculated CPU values:

  • calculateUsage: takes two instances of calls to cpuacct.usage or cpuacct.stat and returns the calculated usage in percentage of CPU time: second time since container start - first time since container start / total time

Installation

npm install cgroup-metrics

Usage

You can access each metric separately using the async functions for each metric

Memory Metrics

const cgroup = require('cgroup-metrics');

const memory = cgroup.memory();
const containerUsage = await memory.containerUsage();
console.log(containerUsage);

const containerUsagePercentage = await memory.containerUsagePercentage(containerUsage);
console.log(containerUsagePercentage);

CPU Metrics

const cpu = cgroup.cpu();

/* Returns an object like:
* {
*    cpuNanosSinceContainerStart: 120234,
*    timestamp: 153686574
* }
* */
const cpuacct_usage = await cpu.usage();
console.log(`Total CPU time since start of container (ns): ${cpuacct_usage.cpuNanosSinceContainerStart}`);


/* Returns an object like:
* {
*     user: {
*               cpuNanosSinceContainerStart: 120234,
*               timestamp: 153686574
*          },
*    system: {
*               cpuNanosSinceContainerStart: 120234,
*               timestamp: 153686574
*           },
* }
* */
const cpuacct_stats = await cpu.stat();
console.log(`CPU user count object: ${cpuacct_stat.user}`);
console.log(`CPU system count object: ${cpuacct_stat.system}`);

const calculateUsage = await cpu.calculateUsage(cpuacct_usage1, cpuacct_usage2);

All Metrics

Or you can use the function metrics to get an object of all the metrics:

const metrics = await cgroup.metrics();

console.log(`Container usage: ${metrics.memory.containerUsage}`);
console.log(`Container usage percentage: ${metrics.memory.containerUsagePercentage}`);

console.log(`Total CPU time since start of container (ns): ${metrics.cpuacct.usagecpuNanosSinceContainerStart}`);
console.log(`CPU user count: ${metrics.cpuacct.stat.user}`);
console.log(`CPU system count: ${metrics.cpuacct.stat.system}`);

If you call metrics with parameter flatten set to true, it will return a flattened (1D) js object:

const metrics = await cgroup.metrics(true);
console.log(`Memory usage in the container: ${metrics["memory.containerUsage"]}`)

Error Handling

If there is no container running or there is an issue reading the file path, the function call will error something like this:

Error: Error reading file /sys/fs/cgroup/memory/memory.stat, Message: ENOENT: no such file or directory, open '/sys/fs/cgroup/memory/memory.stat'

If one of the files is empty, it will return an error like this:

Error: Error reading file /sys/fs/cgroup/memory/memory.stat, Message: File is empty

If a file is malformed, it will return an error like this:

Error: One or more metrics are malformed. containerUsage: 1234, limit: NaN

Or:

Error reading file /sys/fs/cgroup/cpuacct/cpuacct.stat, Message: Cannot read property 'split' of undefined

Contributing

Contributions are welcomed! Read the Contributing Guide for more information.

Licensing

This project is licensed under the Apache V2 License. See LICENSE for more information.