From 7d703fe57e0236ce05db54bf8aa1c1d4090f293c Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sun, 2 Jul 2023 22:17:27 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/smartmetrics.classes.smartmetrics.ts | 32 ++++++++++++++++++++----- ts/smartmetrics.plugins.ts | 4 +++- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 7cf61fe..1e6173a 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@pushrocks/smartmetrics', - version: '2.0.1', + version: '2.0.2', description: 'easy system metrics' } diff --git a/ts/smartmetrics.classes.smartmetrics.ts b/ts/smartmetrics.classes.smartmetrics.ts index 9ef9370..0dbdc54 100644 --- a/ts/smartmetrics.classes.smartmetrics.ts +++ b/ts/smartmetrics.classes.smartmetrics.ts @@ -6,6 +6,7 @@ export class SmartMetrics { public sourceNameArg: string; public logger: plugins.smartlog.Smartlog; public registry: plugins.promClient.Registry; + public maxMemoryMB: number; public async setup() { const collectDefaultMetrics = plugins.promClient.collectDefaultMetrics; @@ -17,6 +18,27 @@ export class SmartMetrics { this.logger = loggerArg; this.sourceNameArg = sourceNameArg; this.setup(); + this.checkMemoryLimits(); + } + + private checkMemoryLimits() { + let heapStats = plugins.v8.getHeapStatistics(); + let maxHeapSizeMB = heapStats.heap_size_limit / 1024 / 1024; + let totalSystemMemoryMB = plugins.os.totalmem() / 1024 / 1024; + + let dockerMemoryLimitMB = totalSystemMemoryMB; + try { + let dockerMemoryLimitBytes = plugins.fs.readFileSync('/sys/fs/cgroup/memory/memory.limit_in_bytes', 'utf8'); + dockerMemoryLimitMB = parseInt(dockerMemoryLimitBytes, 10) / 1024 / 1024; + } catch (error) { + // Ignore - this will fail if not running in a Docker container + } + + this.maxMemoryMB = Math.min(totalSystemMemoryMB, dockerMemoryLimitMB); + + if (maxHeapSizeMB > this.maxMemoryMB) { + throw new Error('Node.js process can use more memory than is available'); + } } public start() { @@ -53,7 +75,6 @@ export class SmartMetrics { const pids = await plugins.pidtree(process.pid); const stats = await plugins.pidusage([process.pid, ...pids]); - // lets compute cpu usage let cpuPercentage = 0; for (const stat of Object.keys(stats)) { if (!stats[stat]) continue; @@ -61,16 +82,15 @@ export class SmartMetrics { } let cpuUsageText = `${Math.round(cpuPercentage * 100) / 100} %`; - // lets compute memory usage let memoryUsageBytes = 0; for (const stat of Object.keys(stats)) { if (!stats[stat]) continue; memoryUsageBytes += stats[stat].memory; } - let memoryPercentage = Math.round((memoryUsageBytes / 1000000000) * 100 * 100) / 100; - let memoryUsageText = `${memoryPercentage}% | ${this.formatBytes( - memoryUsageBytes - )} / ${this.formatBytes(1000000000)}`; + + // Correct memory usage percentage calculation + let memoryPercentage = Math.round((memoryUsageBytes / (this.maxMemoryMB * 1024 * 1024)) * 100 * 100) / 100; + let memoryUsageText = `${memoryPercentage}% | ${this.formatBytes(memoryUsageBytes)} / ${this.formatBytes(this.maxMemoryMB * 1024 * 1024)}`; console.log(`${cpuUsageText} ||| ${memoryUsageText} `); diff --git a/ts/smartmetrics.plugins.ts b/ts/smartmetrics.plugins.ts index cff21ee..fabd5c2 100644 --- a/ts/smartmetrics.plugins.ts +++ b/ts/smartmetrics.plugins.ts @@ -1,7 +1,9 @@ // node native +import * as v8 from 'v8'; import * as os from 'os'; +import * as fs from 'fs'; -export { os }; +export { v8, os, fs }; // pushrocks scope import * as smartdelay from '@pushrocks/smartdelay';