Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

memory.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. 'use strict';
  2. // @ts-check
  3. // ==================================================================================
  4. // memory.js
  5. // ----------------------------------------------------------------------------------
  6. // Description: System Information - library
  7. // for Node.js
  8. // Copyright: (c) 2014 - 2020
  9. // Author: Sebastian Hildebrandt
  10. // ----------------------------------------------------------------------------------
  11. // License: MIT
  12. // ==================================================================================
  13. // 5. Memory
  14. // ----------------------------------------------------------------------------------
  15. const os = require('os');
  16. const exec = require('child_process').exec;
  17. const execSync = require('child_process').execSync;
  18. const util = require('./util');
  19. const fs = require('fs');
  20. let _platform = process.platform;
  21. const _linux = (_platform === 'linux');
  22. const _darwin = (_platform === 'darwin');
  23. const _windows = (_platform === 'win32');
  24. const _freebsd = (_platform === 'freebsd');
  25. const _openbsd = (_platform === 'openbsd');
  26. const _netbsd = (_platform === 'netbsd');
  27. const _sunos = (_platform === 'sunos');
  28. const OSX_RAM_manufacturers = {
  29. '0x014F': 'Transcend Information',
  30. '0x2C00': 'Micron Technology Inc.',
  31. '0x802C': 'Micron Technology Inc.',
  32. '0x80AD': 'Hynix Semiconductor Inc.',
  33. '0x80CE': 'Samsung Electronics Inc.',
  34. '0xAD00': 'Hynix Semiconductor Inc.',
  35. '0xCE00': 'Samsung Electronics Inc.',
  36. '0x02FE': 'Elpida',
  37. '0x5105': 'Qimonda AG i. In.',
  38. '0x8551': 'Qimonda AG i. In.',
  39. '0x859B': 'Crucial',
  40. '0x04CD': 'G-Skill'
  41. };
  42. // _______________________________________________________________________________________
  43. // | R A M | H D |
  44. // |______________________|_________________________| | |
  45. // | active buffers/cache | | |
  46. // |________________________________________________|___________|_________|______________|
  47. // | used free | used free |
  48. // |____________________________________________________________|________________________|
  49. // | total | swap |
  50. // |____________________________________________________________|________________________|
  51. // free (older versions)
  52. // ----------------------------------
  53. // # free
  54. // total used free shared buffers cached
  55. // Mem: 16038 (1) 15653 (2) 384 (3) 0 (4) 236 (5) 14788 (6)
  56. // -/+ buffers/cache: 628 (7) 15409 (8)
  57. // Swap: 16371 83 16288
  58. //
  59. // |------------------------------------------------------------|
  60. // | R A M |
  61. // |______________________|_____________________________________|
  62. // | active (2-(5+6) = 7) | available (3+5+6 = 8) |
  63. // |______________________|_________________________|___________|
  64. // | active | buffers/cache (5+6) | |
  65. // |________________________________________________|___________|
  66. // | used (2) | free (3) |
  67. // |____________________________________________________________|
  68. // | total (1) |
  69. // |____________________________________________________________|
  70. //
  71. // free (since free von procps-ng 3.3.10)
  72. // ----------------------------------
  73. // # free
  74. // total used free shared buffers/cache available
  75. // Mem: 16038 (1) 628 (2) 386 (3) 0 (4) 15024 (5) 14788 (6)
  76. // Swap: 16371 83 16288
  77. //
  78. // |------------------------------------------------------------|
  79. // | R A M |
  80. // |______________________|_____________________________________|
  81. // | | available (6) estimated |
  82. // |______________________|_________________________|___________|
  83. // | active (2) | buffers/cache (5) | free (3) |
  84. // |________________________________________________|___________|
  85. // | total (1) |
  86. // |____________________________________________________________|
  87. //
  88. // Reference: http://www.software-architect.net/blog/article/date/2015/06/12/-826c6e5052.html
  89. // /procs/meminfo - sample (all in kB)
  90. //
  91. // MemTotal: 32806380 kB
  92. // MemFree: 17977744 kB
  93. // MemAvailable: 19768972 kB
  94. // Buffers: 517028 kB
  95. // Cached: 2161876 kB
  96. // SwapCached: 456 kB
  97. // Active: 12081176 kB
  98. // Inactive: 2164616 kB
  99. // Active(anon): 10832884 kB
  100. // Inactive(anon): 1477272 kB
  101. // Active(file): 1248292 kB
  102. // Inactive(file): 687344 kB
  103. // Unevictable: 0 kB
  104. // Mlocked: 0 kB
  105. // SwapTotal: 16768892 kB
  106. // SwapFree: 16768304 kB
  107. // Dirty: 268 kB
  108. // Writeback: 0 kB
  109. // AnonPages: 11568832 kB
  110. // Mapped: 719992 kB
  111. // Shmem: 743272 kB
  112. // Slab: 335716 kB
  113. // SReclaimable: 256364 kB
  114. // SUnreclaim: 79352 kB
  115. function mem(callback) {
  116. return new Promise((resolve) => {
  117. process.nextTick(() => {
  118. let result = {
  119. total: os.totalmem(),
  120. free: os.freemem(),
  121. used: os.totalmem() - os.freemem(),
  122. active: os.totalmem() - os.freemem(), // temporarily (fallback)
  123. available: os.freemem(), // temporarily (fallback)
  124. buffers: 0,
  125. cached: 0,
  126. slab: 0,
  127. buffcache: 0,
  128. swaptotal: 0,
  129. swapused: 0,
  130. swapfree: 0
  131. };
  132. if (_linux) {
  133. fs.readFile('/proc/meminfo', function (error, stdout) {
  134. if (!error) {
  135. const lines = stdout.toString().split('\n');
  136. result.total = parseInt(util.getValue(lines, 'memtotal'), 10);
  137. result.total = result.total ? result.total * 1024 : os.totalmem();
  138. result.free = parseInt(util.getValue(lines, 'memfree'), 10);
  139. result.free = result.free ? result.free * 1024 : os.freemem();
  140. result.used = result.total - result.free;
  141. result.buffers = parseInt(util.getValue(lines, 'buffers'), 10);
  142. result.buffers = result.buffers ? result.buffers * 1024 : 0;
  143. result.cached = parseInt(util.getValue(lines, 'cached'), 10);
  144. result.cached = result.cached ? result.cached * 1024 : 0;
  145. result.slab = parseInt(util.getValue(lines, 'slab'), 10);
  146. result.slab = result.slab ? result.slab * 1024 : 0;
  147. result.buffcache = result.buffers + result.cached + result.slab;
  148. let available = parseInt(util.getValue(lines, 'memavailable'), 10);
  149. result.available = available ? available * 1024 : result.free + result.buffcache;
  150. result.active = result.total - result.available;
  151. result.swaptotal = parseInt(util.getValue(lines, 'swaptotal'), 10);
  152. result.swaptotal = result.swaptotal ? result.swaptotal * 1024 : 0;
  153. result.swapfree = parseInt(util.getValue(lines, 'swapfree'), 10);
  154. result.swapfree = result.swapfree ? result.swapfree * 1024 : 0;
  155. result.swapused = result.swaptotal - result.swapfree;
  156. }
  157. if (callback) { callback(result); }
  158. resolve(result);
  159. });
  160. }
  161. if (_freebsd || _openbsd || _netbsd) {
  162. exec('/sbin/sysctl -a 2>/dev/null | grep -E "hw.realmem|hw.physmem|vm.stats.vm.v_page_count|vm.stats.vm.v_wire_count|vm.stats.vm.v_active_count|vm.stats.vm.v_inactive_count|vm.stats.vm.v_cache_count|vm.stats.vm.v_free_count|vm.stats.vm.v_page_size"', function (error, stdout) {
  163. if (!error) {
  164. let lines = stdout.toString().split('\n');
  165. const pagesize = parseInt(util.getValue(lines, 'vm.stats.vm.v_page_size'), 10);
  166. const inactive = parseInt(util.getValue(lines, 'vm.stats.vm.v_inactive_count'), 10) * pagesize;
  167. const cache = parseInt(util.getValue(lines, 'vm.stats.vm.v_cache_count'), 10) * pagesize;
  168. result.total = parseInt(util.getValue(lines, 'hw.realmem'), 10);
  169. if (isNaN(result.total)) result.total = parseInt(util.getValue(lines, 'hw.physmem'), 10);
  170. result.free = parseInt(util.getValue(lines, 'vm.stats.vm.v_free_count'), 10) * pagesize;
  171. result.buffcache = inactive + cache;
  172. result.available = result.buffcache + result.free;
  173. result.active = result.total - result.free - result.buffcache;
  174. result.swaptotal = 0;
  175. result.swapfree = 0;
  176. result.swapused = 0;
  177. }
  178. if (callback) { callback(result); }
  179. resolve(result);
  180. });
  181. }
  182. if (_sunos) {
  183. if (callback) { callback(result); }
  184. resolve(result);
  185. }
  186. if (_darwin) {
  187. exec('vm_stat 2>/dev/null | grep "Pages active"', function (error, stdout) {
  188. if (!error) {
  189. let lines = stdout.toString().split('\n');
  190. result.active = parseInt(lines[0].split(':')[1], 10) * 4096;
  191. result.buffcache = result.used - result.active;
  192. result.available = result.free + result.buffcache;
  193. }
  194. exec('sysctl -n vm.swapusage 2>/dev/null', function (error, stdout) {
  195. if (!error) {
  196. let lines = stdout.toString().split('\n');
  197. if (lines.length > 0) {
  198. let line = lines[0].replace(/,/g, '.').replace(/M/g, '');
  199. line = line.trim().split(' ');
  200. for (let i = 0; i < line.length; i++) {
  201. if (line[i].toLowerCase().indexOf('total') !== -1) result.swaptotal = parseFloat(line[i].split('=')[1].trim()) * 1024 * 1024;
  202. if (line[i].toLowerCase().indexOf('used') !== -1) result.swapused = parseFloat(line[i].split('=')[1].trim()) * 1024 * 1024;
  203. if (line[i].toLowerCase().indexOf('free') !== -1) result.swapfree = parseFloat(line[i].split('=')[1].trim()) * 1024 * 1024;
  204. }
  205. }
  206. }
  207. if (callback) { callback(result); }
  208. resolve(result);
  209. });
  210. });
  211. }
  212. if (_windows) {
  213. let swaptotal = 0;
  214. let swapused = 0;
  215. try {
  216. util.wmic('pagefile get AllocatedBaseSize, CurrentUsage').then((stdout, error) => {
  217. if (!error) {
  218. let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0);
  219. lines.forEach(function (line) {
  220. if (line !== '') {
  221. line = line.trim().split(/\s\s+/);
  222. swaptotal = swaptotal + parseInt(line[0], 10);
  223. swapused = swapused + parseInt(line[1], 10);
  224. }
  225. });
  226. }
  227. result.swaptotal = swaptotal * 1024 * 1024;
  228. result.swapused = swapused * 1024 * 1024;
  229. result.swapfree = result.swaptotal - result.swapused;
  230. if (callback) { callback(result); }
  231. resolve(result);
  232. });
  233. } catch (e) {
  234. if (callback) { callback(result); }
  235. resolve(result);
  236. }
  237. }
  238. });
  239. });
  240. }
  241. exports.mem = mem;
  242. function memLayout(callback) {
  243. function getManufacturer(manId) {
  244. if ({}.hasOwnProperty.call(OSX_RAM_manufacturers, manId)) {
  245. return (OSX_RAM_manufacturers[manId]);
  246. }
  247. return manId;
  248. }
  249. return new Promise((resolve) => {
  250. process.nextTick(() => {
  251. let result = [];
  252. if (_linux || _freebsd || _openbsd || _netbsd) {
  253. exec('export LC_ALL=C; dmidecode -t memory 2>/dev/null | grep -iE "Size:|Type|Speed|Manufacturer|Form Factor|Locator|Memory Device|Serial Number|Voltage|Part Number"; unset LC_ALL', function (error, stdout) {
  254. if (!error) {
  255. let devices = stdout.toString().split('Memory Device');
  256. devices.shift();
  257. devices.forEach(function (device) {
  258. let lines = device.split('\n');
  259. const sizeString = util.getValue(lines, 'Size');
  260. const size = sizeString.indexOf('GB') >= 0 ? parseInt(sizeString, 10) * 1024 * 1024 * 1024 : parseInt(sizeString, 10) * 1024 * 1024;
  261. if (parseInt(util.getValue(lines, 'Size'), 10) > 0) {
  262. result.push({
  263. size,
  264. bank: util.getValue(lines, 'Bank Locator'),
  265. type: util.getValue(lines, 'Type:'),
  266. clockSpeed: (util.getValue(lines, 'Configured Clock Speed:') ? parseInt(util.getValue(lines, 'Configured Clock Speed:'), 10) : (util.getValue(lines, 'Speed:') ? parseInt(util.getValue(lines, 'Speed:'), 10) : -1)),
  267. formFactor: util.getValue(lines, 'Form Factor:'),
  268. manufacturer: util.getValue(lines, 'Manufacturer:'),
  269. partNum: util.getValue(lines, 'Part Number:'),
  270. serialNum: util.getValue(lines, 'Serial Number:'),
  271. voltageConfigured: parseFloat(util.getValue(lines, 'Configured Voltage:') || -1),
  272. voltageMin: parseFloat(util.getValue(lines, 'Minimum Voltage:') || -1),
  273. voltageMax: parseFloat(util.getValue(lines, 'Maximum Voltage:') || -1),
  274. });
  275. } else {
  276. result.push({
  277. size: 0,
  278. bank: util.getValue(lines, 'Bank Locator'),
  279. type: 'Empty',
  280. clockSpeed: 0,
  281. formFactor: util.getValue(lines, 'Form Factor:'),
  282. partNum: '',
  283. serialNum: '',
  284. voltageConfigured: -1,
  285. voltageMin: -1,
  286. voltageMax: -1,
  287. });
  288. }
  289. });
  290. }
  291. if (!result.length) {
  292. result.push({
  293. size: os.totalmem(),
  294. bank: '',
  295. type: '',
  296. clockSpeed: 0,
  297. formFactor: '',
  298. partNum: '',
  299. serialNum: '',
  300. voltageConfigured: -1,
  301. voltageMin: -1,
  302. voltageMax: -1,
  303. });
  304. // Try Raspberry PI
  305. try {
  306. let stdout = execSync('cat /proc/cpuinfo 2>/dev/null');
  307. let lines = stdout.toString().split('\n');
  308. let model = util.getValue(lines, 'hardware', ':', true).toUpperCase();
  309. let version = util.getValue(lines, 'revision', ':', true).toLowerCase();
  310. if (model === 'BCM2835' || model === 'BCM2708' || model === 'BCM2709' || model === 'BCM2835' || model === 'BCM2837') {
  311. const clockSpeed = {
  312. '0': 400,
  313. '1': 450,
  314. '2': 450,
  315. '3': 3200
  316. };
  317. result[0].clockSpeed = version && version[2] && clockSpeed[version[2]] || 400;
  318. result[0].clockSpeed = version && version[4] && version[4] === 'd' ? '500' : result[0].clockSpeed;
  319. result[0].type = 'LPDDR2';
  320. result[0].type = version && version[2] && version[2] === '3' ? 'LPDDR4' : result[0].type;
  321. result[0].formFactor = 'SoC';
  322. stdout = execSync('vcgencmd get_config sdram_freq 2>/dev/null');
  323. lines = stdout.toString().split('\n');
  324. let freq = parseInt(util.getValue(lines, 'sdram_freq', '=', true), 10) || 0;
  325. if (freq) {
  326. result.clockSpeed = freq;
  327. }
  328. stdout = execSync('vcgencmd measure_volts sdram_p 2>/dev/null');
  329. lines = stdout.toString().split('\n');
  330. let voltage = parseFloat(util.getValue(lines, 'volt', '=', true)) || 0;
  331. if (voltage) {
  332. result[0].voltageConfigured = voltage;
  333. result[0].voltageMin = voltage;
  334. result[0].voltageMax = voltage;
  335. }
  336. }
  337. } catch (e) {
  338. util.noop();
  339. }
  340. }
  341. if (callback) { callback(result); }
  342. resolve(result);
  343. });
  344. }
  345. if (_darwin) {
  346. exec('system_profiler SPMemoryDataType', function (error, stdout) {
  347. if (!error) {
  348. let devices = stdout.toString().split(' BANK ');
  349. let hasBank = true;
  350. if (devices.length === 1) {
  351. devices = stdout.toString().split(' DIMM');
  352. hasBank = false;
  353. }
  354. devices.shift();
  355. devices.forEach(function (device) {
  356. let lines = device.split('\n');
  357. const bank = (hasBank ? 'BANK ' : 'DIMM') + lines[0].trim().split('/')[0];
  358. const size = parseInt(util.getValue(lines, ' Size'));
  359. if (size) {
  360. result.push({
  361. size: size * 1024 * 1024 * 1024,
  362. bank: bank,
  363. type: util.getValue(lines, ' Type:'),
  364. clockSpeed: parseInt(util.getValue(lines, ' Speed:'), 10),
  365. formFactor: '',
  366. manufacturer: getManufacturer(util.getValue(lines, ' Manufacturer:')),
  367. partNum: util.getValue(lines, ' Part Number:'),
  368. serialNum: util.getValue(lines, ' Serial Number:'),
  369. voltageConfigured: -1,
  370. voltageMin: -1,
  371. voltageMax: -1,
  372. });
  373. } else {
  374. result.push({
  375. size: 0,
  376. bank: bank,
  377. type: 'Empty',
  378. clockSpeed: 0,
  379. formFactor: '',
  380. manufacturer: '',
  381. partNum: '',
  382. serialNum: '',
  383. voltageConfigured: -1,
  384. voltageMin: -1,
  385. voltageMax: -1,
  386. });
  387. }
  388. });
  389. }
  390. if (callback) { callback(result); }
  391. resolve(result);
  392. });
  393. }
  394. if (_sunos) {
  395. if (callback) { callback(result); }
  396. resolve(result);
  397. }
  398. if (_windows) {
  399. const memoryTypes = 'Unknown|Other|DRAM|Synchronous DRAM|Cache DRAM|EDO|EDRAM|VRAM|SRAM|RAM|ROM|FLASH|EEPROM|FEPROM|EPROM|CDRAM|3DRAM|SDRAM|SGRAM|RDRAM|DDR|DDR2|DDR2 FB-DIMM|Reserved|DDR3|FBD2|DDR4|LPDDR|LPDDR2|LPDDR3|LPDDR4'.split('|');
  400. const FormFactors = 'Unknown|Other|SIP|DIP|ZIP|SOJ|Proprietary|SIMM|DIMM|TSOP|PGA|RIMM|SODIMM|SRIMM|SMD|SSMP|QFP|TQFP|SOIC|LCC|PLCC|BGA|FPBGA|LGA'.split('|');
  401. try {
  402. util.wmic('memorychip get /value').then((stdout, error) => {
  403. if (!error) {
  404. let devices = stdout.toString().split('BankL');
  405. devices.shift();
  406. devices.forEach(function (device) {
  407. let lines = device.split('\r\n');
  408. result.push({
  409. size: parseInt(util.getValue(lines, 'Capacity', '='), 10) || 0,
  410. bank: util.getValue(lines, 'abel', '='), // BankLabel
  411. type: memoryTypes[parseInt(util.getValue(lines, 'MemoryType', '='), 10)],
  412. clockSpeed: parseInt(util.getValue(lines, 'ConfiguredClockSpeed', '='), 10) || 0,
  413. formFactor: FormFactors[parseInt(util.getValue(lines, 'FormFactor', '='), 10) || 0],
  414. manufacturer: util.getValue(lines, 'Manufacturer', '='),
  415. partNum: util.getValue(lines, 'PartNumber', '='),
  416. serialNum: util.getValue(lines, 'SerialNumber', '='),
  417. voltageConfigured: (parseInt(util.getValue(lines, 'ConfiguredVoltage', '='), 10) || 0) / 1000.0,
  418. voltageMin: (parseInt(util.getValue(lines, 'MinVoltage', '='), 10) || 0) / 1000.0,
  419. voltageMax: (parseInt(util.getValue(lines, 'MaxVoltage', '='), 10) || 0) / 1000.0,
  420. });
  421. });
  422. }
  423. if (callback) { callback(result); }
  424. resolve(result);
  425. });
  426. } catch (e) {
  427. if (callback) { callback(result); }
  428. resolve(result);
  429. }
  430. }
  431. });
  432. });
  433. }
  434. exports.memLayout = memLayout;