UNPKG

backtrace-node

Version:
130 lines 4.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var fs = tslib_1.__importStar(require("fs")); var process = tslib_1.__importStar(require("process")); var sys = process.platform; var memInfoRe = /^(.+):\s+(\d+)\s*(.+)?$/; var memInfoToAttr = { MemTotal: 'system.memory.total', MemFree: 'system.memory.free', MemAvailable: 'system.memory.available', Buffers: 'system.memory.buffers', Cached: 'system.memory.cached', SwapCached: 'system.memory.swap.cached', Active: 'system.memory.active', Inactive: 'system.memory.inactive', SwapTotal: 'system.memory.swap.total', SwapFree: 'system.memory.swap.free', Dirty: 'system.memory.dirty', Writeback: 'system.memory.writeback', Slab: 'system.memory.slab', VmallocTotal: 'system.memory.vmalloc.total', VmallocUsed: 'system.memory.vmalloc.used', VmallocChunk: 'system.memory.vmalloc.chunk', }; function readMemoryInformation() { if (sys === 'win32') { return {}; } var result = {}; var file = ''; try { file = fs.readFileSync('/proc/meminfo', { encoding: 'utf8' }); } catch (err) { return {}; } var lines = file.split('\n'); for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) { var line = lines_1[_i]; if (!line) { continue; } var match = line.match(memInfoRe); if (!match) { continue; } var name_1 = match[1]; var attrName = memInfoToAttr[name_1]; if (!attrName) { continue; } var number = parseInt(match[2], 10); var units = match[3]; if (number === 0) { units = 'B'; } if (units === 'B' || units === 'bytes') { number *= 1; } else if (units === 'kB') { number *= 1024; } else { continue; } result[attrName] = number; } return result; } exports.readMemoryInformation = readMemoryInformation; function readProcessStatus() { if (sys === 'win32') { return {}; } // Justification for doing this synchronously: // * We need to collect this information in the process uncaughtException handler, in which the // event loop is not safe to use. // * We are collecting a snapshot of virtual memory used. If this is done asynchronously, then // we may pick up virtual memory information for a time different than the moment we are // interested in. // * procfs is a virtual filesystem; there is no disk I/O to block on. It's synchronous anyway. var contents; try { contents = fs.readFileSync('/proc/self/status', { encoding: 'utf8' }); } catch (err) { return {}; } var result = {}; // tslint:disable-next-line: prefer-for-of for (var i = 0; i < _procSelfStatusData.length; i += 1) { var item = _procSelfStatusData[i]; var match = contents.match(item.re); if (!match) { continue; } result[item.attr] = item.parse(match[1]); } return result; } exports.readProcessStatus = readProcessStatus; function parseKb(str) { return parseInt(str, 10) * 1024; } var _procSelfStatusData = [ { re: /^nonvoluntary_ctxt_switches:\s+(\d+)$/m, parse: parseInt, attr: 'sched.cs.involuntary', }, { re: /^voluntary_ctxt_switches:\s+(\d+)$/m, parse: parseInt, attr: 'sched.cs.voluntary', }, { re: /^FDSize:\s+(\d+)$/m, parse: parseInt, attr: 'descriptor.count' }, { re: /^FDSize:\s+(\d+)$/m, parse: parseInt, attr: 'descriptor.count' }, { re: /^VmData:\s+(\d+)\s+kB$/m, parse: parseKb, attr: 'vm.data.size' }, { re: /^VmLck:\s+(\d+)\s+kB$/m, parse: parseKb, attr: 'vm.locked.size' }, { re: /^VmPTE:\s+(\d+)\s+kB$/m, parse: parseKb, attr: 'vm.pte.size' }, { re: /^VmHWM:\s+(\d+)\s+kB$/m, parse: parseKb, attr: 'vm.rss.peak' }, { re: /^VmRSS:\s+(\d+)\s+kB$/m, parse: parseKb, attr: 'vm.rss.size' }, { re: /^VmLib:\s+(\d+)\s+kB$/m, parse: parseKb, attr: 'vm.shared.size' }, { re: /^VmStk:\s+(\d+)\s+kB$/m, parse: parseKb, attr: 'vm.stack.size' }, { re: /^VmSwap:\s+(\d+)\s+kB$/m, parse: parseKb, attr: 'vm.swap.size' }, { re: /^VmPeak:\s+(\d+)\s+kB$/m, parse: parseKb, attr: 'vm.vma.peak' }, { re: /^VmSize:\s+(\d+)\s+kB$/m, parse: parseKb, attr: 'vm.vma.size' }, ]; //# sourceMappingURL=processHelper.js.map