Skip to content

Commit

Permalink
feat: free memory module
Browse files Browse the repository at this point in the history
  • Loading branch information
renanccastro committed Mar 12, 2024
1 parent efb945a commit b1cf02f
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion lib/models/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { KadiraModel } from './0model';
import { EventLoopMonitor } from '../event_loop_monitor.js';
import { Ntp } from '../ntp';
import os from 'os';
import { execSync } from 'child_process';

export const MEMORY_ROUNDING_FACTOR = 100 * 1024; // 100kb
const EVENT_LOOP_ROUNDING_FACTOR = 500; // microseconds
Expand Down Expand Up @@ -47,6 +48,50 @@ export function SystemModel () {

_.extend(SystemModel.prototype, KadiraModel.prototype);

function meminfo() {

Check failure on line 51 in lib/models/system.js

View workflow job for this annotation

GitHub Actions / build

Missing space before function parentheses
var info = {};

Check failure on line 52 in lib/models/system.js

View workflow job for this annotation

GitHub Actions / build

Unexpected var, use let or const instead
var data = fs.readFileSync('/proc/meminfo').toString();

Check failure on line 53 in lib/models/system.js

View workflow job for this annotation

GitHub Actions / build

Unexpected var, use let or const instead

Check failure on line 53 in lib/models/system.js

View workflow job for this annotation

GitHub Actions / build

'fs' is not defined
data.split(/\n/g).forEach(function(line){

Check failure on line 54 in lib/models/system.js

View workflow job for this annotation

GitHub Actions / build

Missing space before function parentheses

Check failure on line 54 in lib/models/system.js

View workflow job for this annotation

GitHub Actions / build

Missing space before opening brace
line = line.split(':');

Check failure on line 55 in lib/models/system.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 4 spaces but found 6

// Ignore invalid lines, if any

Check failure on line 57 in lib/models/system.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 4 spaces but found 6
if (line.length < 2) {

Check failure on line 58 in lib/models/system.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 4 spaces but found 6
return;

Check failure on line 59 in lib/models/system.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 6 spaces but found 10
}

// Remove parseInt call to make all values strings
info[line[0]] = parseInt(line[1].trim(), 10);
});

return info;
}

function getFreeMemory(){
const isLinux = process.platform === "linux";
const isMac = process.platform === "darwin";

try {
if(isLinux){
const info = meminfo();
return info.MemFree + info.Buffers + info.Cached;
}
if(isMac){
const output = execSync('vm_stat').toString();
const pageSizeArray = [...output.matchAll(/page size of (\d*)/g)];
const pageSize = parseInt(pageSizeArray[0][1], 10)
const freePagesMatches = [...output.matchAll(/Pages free:\s*(\d*)/g)];
const freePages = parseInt(freePagesMatches[0][1], 10)
const inactivePagesMatches = [...output.matchAll(/Pages inactive:\s*(\d*)/g)];
const inactivePages = parseInt(inactivePagesMatches[0][1],10)
return pageSize * (freePages + inactivePages)
}
}catch(e){
console.error('failed to get native memory info, falling back to default option', e);
}

return os.freemem();
}

SystemModel.prototype.buildPayload = function () {
let metrics = {};
let now = Ntp._now();
Expand All @@ -60,7 +105,8 @@ SystemModel.prototype.buildPayload = function () {
metrics.memoryExternal = roundUsingFactor(memoryUsage.external, MEMORY_ROUNDING_FACTOR) / (1024 * 1024);
metrics.memoryHeapUsed = roundUsingFactor(memoryUsage.heapUsed, MEMORY_ROUNDING_FACTOR) / (1024 * 1024);
metrics.memoryHeapTotal = roundUsingFactor(memoryUsage.heapTotal, MEMORY_ROUNDING_FACTOR) / (1024 * 1024);
metrics.freeMemory = roundUsingFactor(os.freemem(), MEMORY_ROUNDING_FACTOR) / (1024 * 1024);
const freeMemory = getFreeMemory();
metrics.freeMemory = roundUsingFactor(freeMemory, MEMORY_ROUNDING_FACTOR) / (1024 * 1024);
metrics.totalMemory = roundUsingFactor(os.totalmem(), MEMORY_ROUNDING_FACTOR) / (1024 * 1024);

metrics.newSessions = this.newSessions;
Expand Down

0 comments on commit b1cf02f

Please sign in to comment.