@adobe/cgroup-metrics

中文文档
3.0.6发布于 2022/01/11MITCommonJS近 30 天下载 1.1万

Node Module to read cgroup memory data

原始使用文档
文档目录8 节
  1. CGROUP-METRICS
  2. Memory Metrics:
  3. CPU Metrics:
  4. Installation
  5. Usage
  6. Error Handling
  7. Contributing
  8. Licensing

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.

版本与发布记录

@adobe/cgroup-metrics 共发布 7 个版本,首次发布于 2020/04/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.6latest2022/01/114 年前
3.0.52021/08/185 年前
3.0.42021/08/055 年前
3.0.32021/03/305 年前
3.0.22021/02/035 年前
3.0.12020/07/226 年前

关于 @adobe/cgroup-metrics 的常见问题

@adobe/cgroup-metrics 是什么?

Node Module to read cgroup memory data

如何安装 @adobe/cgroup-metrics?

在项目目录运行 npm install @adobe/cgroup-metrics,或使用 pnpm add / yarn add / bun add。

@adobe/cgroup-metrics 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/adobe__cgroup-metrics 是否存在。

@adobe/cgroup-metrics 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@adobe/cgroup-metrics 可以商用吗?

@adobe/cgroup-metrics 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@adobe/cgroup-metrics 的下载量有多少?

根据 npm 官方下载统计,@adobe/cgroup-metrics 在 2026/10/05 – 2026/11/03 这 30 天内被下载 11,400 次,日均约 380 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adobe/cgroup-metrics 还在维护吗?

最新版本 3.0.6 发布于 2022/01/11(4 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/04/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。