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

cpu.js 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. 'use strict';
  2. // @ts-check
  3. // ==================================================================================
  4. // cpu.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. // 4. CPU
  14. // ----------------------------------------------------------------------------------
  15. const os = require('os');
  16. const exec = require('child_process').exec;
  17. const fs = require('fs');
  18. const util = require('./util');
  19. let _platform = process.platform;
  20. const _linux = (_platform === 'linux');
  21. const _darwin = (_platform === 'darwin');
  22. const _windows = (_platform === 'win32');
  23. const _freebsd = (_platform === 'freebsd');
  24. const _openbsd = (_platform === 'openbsd');
  25. const _netbsd = (_platform === 'netbsd');
  26. const _sunos = (_platform === 'sunos');
  27. let _cpu_speed = '0.00';
  28. let _current_cpu = {
  29. user: 0,
  30. nice: 0,
  31. system: 0,
  32. idle: 0,
  33. irq: 0,
  34. load: 0,
  35. tick: 0,
  36. ms: 0,
  37. currentload: 0,
  38. currentload_user: 0,
  39. currentload_system: 0,
  40. currentload_nice: 0,
  41. currentload_idle: 0,
  42. currentload_irq: 0,
  43. raw_currentload: 0,
  44. raw_currentload_user: 0,
  45. raw_currentload_system: 0,
  46. raw_currentload_nice: 0,
  47. raw_currentload_idle: 0,
  48. raw_currentload_irq: 0
  49. };
  50. let _cpus = [];
  51. let _corecount = 0;
  52. const AMDBaseFrequencies = {
  53. '8346': '1.8',
  54. '8347': '1.9',
  55. '8350': '2.0',
  56. '8354': '2.2',
  57. '8356|SE': '2.4',
  58. '8356': '2.3',
  59. '8360': '2.5',
  60. '2372': '2.1',
  61. '2373': '2.1',
  62. '2374': '2.2',
  63. '2376': '2.3',
  64. '2377': '2.3',
  65. '2378': '2.4',
  66. '2379': '2.4',
  67. '2380': '2.5',
  68. '2381': '2.5',
  69. '2382': '2.6',
  70. '2384': '2.7',
  71. '2386': '2.8',
  72. '2387': '2.8',
  73. '2389': '2.9',
  74. '2393': '3.1',
  75. '8374': '2.2',
  76. '8376': '2.3',
  77. '8378': '2.4',
  78. '8379': '2.4',
  79. '8380': '2.5',
  80. '8381': '2.5',
  81. '8382': '2.6',
  82. '8384': '2.7',
  83. '8386': '2.8',
  84. '8387': '2.8',
  85. '8389': '2.9',
  86. '8393': '3.1',
  87. '2419EE': '1.8',
  88. '2423HE': '2.0',
  89. '2425HE': '2.1',
  90. '2427': '2.2',
  91. '2431': '2.4',
  92. '2435': '2.6',
  93. '2439SE': '2.8',
  94. '8425HE': '2.1',
  95. '8431': '2.4',
  96. '8435': '2.6',
  97. '8439SE': '2.8',
  98. '4122': '2.2',
  99. '4130': '2.6',
  100. '4162EE': '1.7',
  101. '4164EE': '1.8',
  102. '4170HE': '2.1',
  103. '4174HE': '2.3',
  104. '4176HE': '2.4',
  105. '4180': '2.6',
  106. '4184': '2.8',
  107. '6124HE': '1.8',
  108. '6128HE': '2.0',
  109. '6132HE': '2.2',
  110. '6128': '2.0',
  111. '6134': '2.3',
  112. '6136': '2.4',
  113. '6140': '2.6',
  114. '6164HE': '1.7',
  115. '6166HE': '1.8',
  116. '6168': '1.9',
  117. '6172': '2.1',
  118. '6174': '2.2',
  119. '6176': '2.3',
  120. '6176SE': '2.3',
  121. '6180SE': '2.5',
  122. '3250': '2.5',
  123. '3260': '2.7',
  124. '3280': '2.4',
  125. '4226': '2.7',
  126. '4228': '2.8',
  127. '4230': '2.9',
  128. '4234': '3.1',
  129. '4238': '3.3',
  130. '4240': '3.4',
  131. '4256': '1.6',
  132. '4274': '2.5',
  133. '4276': '2.6',
  134. '4280': '2.8',
  135. '4284': '3.0',
  136. '6204': '3.3',
  137. '6212': '2.6',
  138. '6220': '3.0',
  139. '6234': '2.4',
  140. '6238': '2.6',
  141. '6262HE': '1.6',
  142. '6272': '2.1',
  143. '6274': '2.2',
  144. '6276': '2.3',
  145. '6278': '2.4',
  146. '6282SE': '2.6',
  147. '6284SE': '2.7',
  148. '6308': '3.5',
  149. '6320': '2.8',
  150. '6328': '3.2',
  151. '6338P': '2.3',
  152. '6344': '2.6',
  153. '6348': '2.8',
  154. '6366': '1.8',
  155. '6370P': '2.0',
  156. '6376': '2.3',
  157. '6378': '2.4',
  158. '6380': '2.5',
  159. '6386': '2.8',
  160. 'FX|4100': '3.6',
  161. 'FX|4120': '3.9',
  162. 'FX|4130': '3.8',
  163. 'FX|4150': '3.8',
  164. 'FX|4170': '4.2',
  165. 'FX|6100': '3.3',
  166. 'FX|6120': '3.6',
  167. 'FX|6130': '3.6',
  168. 'FX|6200': '3.8',
  169. 'FX|8100': '2.8',
  170. 'FX|8120': '3.1',
  171. 'FX|8140': '3.2',
  172. 'FX|8150': '3.6',
  173. 'FX|8170': '3.9',
  174. 'FX|4300': '3.8',
  175. 'FX|4320': '4.0',
  176. 'FX|4350': '4.2',
  177. 'FX|6300': '3.5',
  178. 'FX|6350': '3.9',
  179. 'FX|8300': '3.3',
  180. 'FX|8310': '3.4',
  181. 'FX|8320': '3.5',
  182. 'FX|8350': '4.0',
  183. 'FX|8370': '4.0',
  184. 'FX|9370': '4.4',
  185. 'FX|9590': '4.7',
  186. 'FX|8320E': '3.2',
  187. 'FX|8370E': '3.3',
  188. '1950X': '3.4',
  189. '1920X': '3.5',
  190. '1920': '3.2',
  191. '1900X': '3.8',
  192. '1800X': '3.6',
  193. '1700X': '3.4',
  194. 'Pro 1700X': '3.5',
  195. '1700': '3.0',
  196. 'Pro 1700': '3.0',
  197. '1600X': '3.6',
  198. '1600': '3.2',
  199. 'Pro 1600': '3.2',
  200. '1500X': '3.5',
  201. 'Pro 1500': '3.5',
  202. '1400': '3.2',
  203. '1300X': '3.5',
  204. 'Pro 1300': '3.5',
  205. '1200': '3.1',
  206. 'Pro 1200': '3.1',
  207. '2200U': '2.5',
  208. '2300U': '2.0',
  209. 'Pro 2300U': '2.0',
  210. '2500U': '2.0',
  211. 'Pro 2500U': '2.2',
  212. '2700U': '2.0',
  213. 'Pro 2700U': '2.2',
  214. '2600H': '3.2',
  215. '2800H': '3.3',
  216. '7601': '2.2',
  217. '7551': '2.0',
  218. '7501': '2.0',
  219. '74501': '2.3',
  220. '7401': '2.0',
  221. '7351': '2.4',
  222. '7301': '2.2',
  223. '7281': '2.1',
  224. '7251': '2.1',
  225. '7551P': '2.0',
  226. '7401P': '2.0',
  227. '7351P': '2.4',
  228. '2300X': '3.5',
  229. '2500X': '3.6',
  230. '2600': '3.4',
  231. '2600E': '3.1',
  232. '2600X': '3.6',
  233. '2700': '3.2',
  234. '2700E': '2.8',
  235. '2700X': '3.7',
  236. 'Pro 2700X': '3.6',
  237. '2920': '3.5',
  238. '2950': '3.5',
  239. '2970WX': '3.0',
  240. '2990WX': '3.0',
  241. '3200U': '2.6',
  242. '3300U': '2.1',
  243. '3500U': '2.1',
  244. '3550H': '2.1',
  245. '3580U': '2.1',
  246. '3700U': '2.3',
  247. '3750H': '2.3',
  248. '3780U': '2.3',
  249. '3500X': '3.6',
  250. '3600': '3.6',
  251. 'Pro 3600': '3.6',
  252. '3600X': '3.8',
  253. 'Pro 3700': '3.6',
  254. '3700X': '3.6',
  255. '3800X': '3.9',
  256. '3900': '3.1',
  257. 'Pro 3900': '3.1',
  258. '3900X': '3.8',
  259. '3950X': '3.5',
  260. '3960X': '3.8',
  261. '3970X': '3.7',
  262. '7232P': '3.1',
  263. '7302P': '3.0',
  264. '7402P': '2.8',
  265. '7502P': '2.5',
  266. '7702P': '2.0',
  267. '7252': '3.1',
  268. '7262': '3.2',
  269. '7272': '2.9',
  270. '7282': '2.8',
  271. '7302': '3.0',
  272. '7352': '2.3',
  273. '7402': '2.8',
  274. '7452': '2.35',
  275. '7502': '2.5',
  276. '7542': '2.9',
  277. '7552': '2.2',
  278. '7642': '2.3',
  279. '7702': '2.0',
  280. '7742': '2.25',
  281. '7H12': '2.6'
  282. };
  283. const socketTypes = {
  284. 1: 'Other',
  285. 2: 'Unknown',
  286. 3: 'Daughter Board',
  287. 4: 'ZIF Socket',
  288. 5: 'Replacement/Piggy Back',
  289. 6: 'None',
  290. 7: 'LIF Socket',
  291. 8: 'Slot 1',
  292. 9: 'Slot 2',
  293. 10: '370 Pin Socket',
  294. 11: 'Slot A',
  295. 12: 'Slot M',
  296. 13: '423',
  297. 14: 'A (Socket 462)',
  298. 15: '478',
  299. 16: '754',
  300. 17: '940',
  301. 18: '939',
  302. 19: 'mPGA604',
  303. 20: 'LGA771',
  304. 21: 'LGA775',
  305. 22: 'S1',
  306. 23: 'AM2',
  307. 24: 'F (1207)',
  308. 25: 'LGA1366',
  309. 26: 'G34',
  310. 27: 'AM3',
  311. 28: 'C32',
  312. 29: 'LGA1156',
  313. 30: 'LGA1567',
  314. 31: 'PGA988A',
  315. 32: 'BGA1288',
  316. 33: 'rPGA988B',
  317. 34: 'BGA1023',
  318. 35: 'BGA1224',
  319. 36: 'LGA1155',
  320. 37: 'LGA1356',
  321. 38: 'LGA2011',
  322. 39: 'FS1',
  323. 40: 'FS2',
  324. 41: 'FM1',
  325. 42: 'FM2',
  326. 43: 'LGA2011-3',
  327. 44: 'LGA1356-3',
  328. 45: 'LGA1150',
  329. 46: 'BGA1168',
  330. 47: 'BGA1234',
  331. 48: 'BGA1364',
  332. 49: 'AM4',
  333. 50: 'LGA1151',
  334. 51: 'BGA1356',
  335. 52: 'BGA1440',
  336. 53: 'BGA1515',
  337. 54: 'LGA3647-1',
  338. 55: 'SP3',
  339. 56: 'SP3r2',
  340. 57: 'LGA2066',
  341. 58: 'BGA1392',
  342. 59: 'BGA1510',
  343. 60: 'BGA1528'
  344. };
  345. function cpuBrandManufacturer(res) {
  346. res.brand = res.brand.replace(/\(R\)+/g, '®').replace(/\s+/g, ' ').trim();
  347. res.brand = res.brand.replace(/\(TM\)+/g, '™').replace(/\s+/g, ' ').trim();
  348. res.brand = res.brand.replace(/\(C\)+/g, '©').replace(/\s+/g, ' ').trim();
  349. res.brand = res.brand.replace(/CPU+/g, '').replace(/\s+/g, ' ').trim();
  350. res.manufacturer = res.brand.split(' ')[0];
  351. let parts = res.brand.split(' ');
  352. parts.shift();
  353. res.brand = parts.join(' ');
  354. return res;
  355. }
  356. function getAMDSpeed(brand) {
  357. let result = '0.00';
  358. for (let key in AMDBaseFrequencies) {
  359. if ({}.hasOwnProperty.call(AMDBaseFrequencies, key)) {
  360. let parts = key.split('|');
  361. let found = 0;
  362. parts.forEach(item => {
  363. if (brand.indexOf(item) > -1) {
  364. found++;
  365. }
  366. });
  367. if (found === parts.length) {
  368. result = AMDBaseFrequencies[key];
  369. }
  370. }
  371. }
  372. return result;
  373. }
  374. // --------------------------
  375. // CPU - brand, speed
  376. function getCpu() {
  377. return new Promise((resolve) => {
  378. process.nextTick(() => {
  379. const UNKNOWN = 'unknown';
  380. let result = {
  381. manufacturer: UNKNOWN,
  382. brand: UNKNOWN,
  383. vendor: '',
  384. family: '',
  385. model: '',
  386. stepping: '',
  387. revision: '',
  388. voltage: '',
  389. speed: '0.00',
  390. speedmin: '',
  391. speedmax: '',
  392. governor: '',
  393. cores: util.cores(),
  394. physicalCores: util.cores(),
  395. processors: 1,
  396. socket: '',
  397. cache: {}
  398. };
  399. if (_darwin) {
  400. exec('sysctl machdep.cpu hw.cpufrequency_max hw.cpufrequency_min hw.packages hw.physicalcpu_max hw.ncpu', function (error, stdout) {
  401. // if (!error) {
  402. let lines = stdout.toString().split('\n');
  403. const modelline = util.getValue(lines, 'machdep.cpu.brand_string');
  404. result.brand = modelline.split('@')[0].trim();
  405. result.speed = modelline.split('@')[1].trim();
  406. result.speed = parseFloat(result.speed.replace(/GHz+/g, '')).toFixed(2);
  407. _cpu_speed = result.speed;
  408. result = cpuBrandManufacturer(result);
  409. result.speedmin = (util.getValue(lines, 'hw.cpufrequency_min') / 1000000000.0).toFixed(2);
  410. result.speedmax = (util.getValue(lines, 'hw.cpufrequency_max') / 1000000000.0).toFixed(2);
  411. result.vendor = util.getValue(lines, 'machdep.cpu.vendor');
  412. result.family = util.getValue(lines, 'machdep.cpu.family');
  413. result.model = util.getValue(lines, 'machdep.cpu.model');
  414. result.stepping = util.getValue(lines, 'machdep.cpu.stepping');
  415. const countProcessors = util.getValue(lines, 'hw.packages');
  416. const countCores = util.getValue(lines, 'hw.physicalcpu_max');
  417. const countThreads = util.getValue(lines, 'hw.ncpu');
  418. if (countProcessors) {
  419. result.processors = parseInt(countProcessors) || 1;
  420. }
  421. if (countCores && countThreads) {
  422. result.cores = parseInt(countThreads) || util.cores();
  423. result.physicalCores = parseInt(countCores) || util.cores();
  424. }
  425. // }
  426. cpuCache().then(res => {
  427. result.cache = res;
  428. resolve(result);
  429. });
  430. });
  431. }
  432. if (_linux) {
  433. let modelline = '';
  434. let lines = [];
  435. if (os.cpus()[0] && os.cpus()[0].model) modelline = os.cpus()[0].model;
  436. exec('export LC_ALL=C; lscpu; echo -n "Governor: "; cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null; echo; unset LC_ALL', function (error, stdout) {
  437. if (!error) {
  438. lines = stdout.toString().split('\n');
  439. }
  440. modelline = util.getValue(lines, 'model name') || modelline;
  441. result.brand = modelline.split('@')[0].trim();
  442. result.speed = modelline.split('@')[1] ? parseFloat(modelline.split('@')[1].trim()).toFixed(2) : '0.00';
  443. if (result.speed === '0.00' && (result.brand.indexOf('AMD') > -1 || result.brand.toLowerCase().indexOf('ryzen') > -1)) {
  444. result.speed = getAMDSpeed(result.brand);
  445. }
  446. if (result.speed === '0.00') {
  447. let current = getCpuCurrentSpeedSync();
  448. if (current.avg !== 0) result.speed = current.avg.toFixed(2);
  449. }
  450. _cpu_speed = result.speed;
  451. result.speedmin = Math.round(parseFloat(util.getValue(lines, 'cpu min mhz').replace(/,/g, '.')) / 10.0) / 100;
  452. result.speedmin = result.speedmin ? parseFloat(result.speedmin).toFixed(2) : '';
  453. result.speedmax = Math.round(parseFloat(util.getValue(lines, 'cpu max mhz').replace(/,/g, '.')) / 10.0) / 100;
  454. result.speedmax = result.speedmax ? parseFloat(result.speedmax).toFixed(2) : '';
  455. result = cpuBrandManufacturer(result);
  456. result.vendor = util.getValue(lines, 'vendor id');
  457. // if (!result.vendor) { result.vendor = util.getValue(lines, 'anbieterkennung'); }
  458. result.family = util.getValue(lines, 'cpu family');
  459. // if (!result.family) { result.family = util.getValue(lines, 'prozessorfamilie'); }
  460. result.model = util.getValue(lines, 'model:');
  461. // if (!result.model) { result.model = util.getValue(lines, 'modell:'); }
  462. result.stepping = util.getValue(lines, 'stepping');
  463. result.revision = util.getValue(lines, 'cpu revision');
  464. result.cache.l1d = util.getValue(lines, 'l1d cache');
  465. if (result.cache.l1d) { result.cache.l1d = parseInt(result.cache.l1d) * (result.cache.l1d.indexOf('K') !== -1 ? 1024 : 1); }
  466. result.cache.l1i = util.getValue(lines, 'l1i cache');
  467. if (result.cache.l1i) { result.cache.l1i = parseInt(result.cache.l1i) * (result.cache.l1i.indexOf('K') !== -1 ? 1024 : 1); }
  468. result.cache.l2 = util.getValue(lines, 'l2 cache');
  469. if (result.cache.l2) { result.cache.l2 = parseInt(result.cache.l2) * (result.cache.l2.indexOf('K') !== -1 ? 1024 : 1); }
  470. result.cache.l3 = util.getValue(lines, 'l3 cache');
  471. if (result.cache.l3) { result.cache.l3 = parseInt(result.cache.l3) * (result.cache.l3.indexOf('K') !== -1 ? 1024 : 1); }
  472. const threadsPerCore = util.getValue(lines, 'thread(s) per core') || '1';
  473. const processors = util.getValue(lines, 'socket(s)') || '1';
  474. let threadsPerCoreInt = parseInt(threadsPerCore, 10);
  475. let processorsInt = parseInt(processors, 10);
  476. result.physicalCores = result.cores / threadsPerCoreInt;
  477. result.processors = processorsInt;
  478. result.governor = util.getValue(lines, 'governor') || '';
  479. // socket type
  480. let lines2 = [];
  481. exec('export LC_ALL=C; dmidecode –t 4 2>/dev/null | grep "Upgrade: Socket"; unset LC_ALL', function (error2, stdout2) {
  482. lines2 = stdout2.toString().split('\n');
  483. if (lines2 && lines2.length) {
  484. result.socket = util.getValue(lines2, 'Upgrade').replace('Socket', '').trim();
  485. }
  486. resolve(result);
  487. });
  488. });
  489. }
  490. if (_freebsd || _openbsd || _netbsd) {
  491. let modelline = '';
  492. let lines = [];
  493. if (os.cpus()[0] && os.cpus()[0].model) modelline = os.cpus()[0].model;
  494. exec('export LC_ALL=C; dmidecode -t 4; dmidecode -t 7 unset LC_ALL', function (error, stdout) {
  495. let cache = [];
  496. if (!error) {
  497. const data = stdout.toString().split('# dmidecode');
  498. const processor = data.length > 1 ? data[1] : '';
  499. cache = data.length > 2 ? data[2].split('Cache Information') : [];
  500. lines = processor.split('\n');
  501. }
  502. result.brand = modelline.split('@')[0].trim();
  503. result.speed = modelline.split('@')[1] ? parseFloat(modelline.split('@')[1].trim()).toFixed(2) : '0.00';
  504. if (result.speed === '0.00' && (result.brand.indexOf('AMD') > -1 || result.brand.toLowerCase().indexOf('ryzen') > -1)) {
  505. result.speed = getAMDSpeed(result.brand);
  506. }
  507. if (result.speed === '0.00') {
  508. let current = getCpuCurrentSpeedSync();
  509. if (current.avg !== 0) result.speed = current.avg.toFixed(2);
  510. }
  511. _cpu_speed = result.speed;
  512. result.speedmin = '';
  513. result.speedmax = Math.round(parseFloat(util.getValue(lines, 'max speed').replace(/Mhz/g, '')) / 10.0) / 100;
  514. result.speedmax = result.speedmax ? parseFloat(result.speedmax).toFixed(2) : '';
  515. result = cpuBrandManufacturer(result);
  516. result.vendor = util.getValue(lines, 'manufacturer');
  517. let sig = util.getValue(lines, 'signature');
  518. sig = sig.split(',');
  519. for (var i = 0; i < sig.length; i++) {
  520. sig[i] = sig[i].trim();
  521. }
  522. result.family = util.getValue(sig, 'Family', ' ', true);
  523. result.model = util.getValue(sig, 'Model', ' ', true);
  524. result.stepping = util.getValue(sig, 'Stepping', ' ', true);
  525. result.revision = '';
  526. const voltage = parseFloat(util.getValue(lines, 'voltage'));
  527. result.voltage = isNaN(voltage) ? '' : voltage.toFixed(2);
  528. for (let i = 0; i < cache.length; i++) {
  529. lines = cache[i].split('\n');
  530. let cacheType = util.getValue(lines, 'Socket Designation').toLowerCase().replace(' ', '-').split('-');
  531. cacheType = cacheType.length ? cacheType[0] : '';
  532. const sizeParts = util.getValue(lines, 'Installed Size').split(' ');
  533. let size = parseInt(sizeParts[0], 10);
  534. const unit = sizeParts.length > 1 ? sizeParts[1] : 'kb';
  535. size = size * (unit === 'kb' ? 1024 : (unit === 'mb' ? 1024 * 1024 : (unit === 'gb' ? 1024 * 1024 * 1024 : 1)));
  536. if (cacheType) {
  537. if (cacheType === 'l1') {
  538. result.cache[cacheType + 'd'] = size / 2;
  539. result.cache[cacheType + 'i'] = size / 2;
  540. } else {
  541. result.cache[cacheType] = size;
  542. }
  543. }
  544. }
  545. // socket type
  546. result.socket = util.getValue(lines, 'Upgrade').replace('Socket', '').trim();
  547. // # threads / # cores
  548. const threadCount = util.getValue(lines, 'thread count').trim();
  549. const coreCount = util.getValue(lines, 'core count').trim();
  550. if (coreCount && threadCount) {
  551. result.cores = threadCount;
  552. result.physicalCores = coreCount;
  553. }
  554. resolve(result);
  555. });
  556. }
  557. if (_sunos) {
  558. resolve(result);
  559. }
  560. if (_windows) {
  561. try {
  562. util.wmic('cpu get /value').then((stdout, error) => {
  563. if (!error) {
  564. let lines = stdout.split('\r\n');
  565. let name = util.getValue(lines, 'name', '=') || '';
  566. if (name.indexOf('@') >= 0) {
  567. result.brand = name.split('@')[0].trim();
  568. result.speed = name.split('@')[1] ? parseFloat(name.split('@')[1].trim()).toFixed(2) : '0.00';
  569. _cpu_speed = result.speed;
  570. } else {
  571. result.brand = name.trim();
  572. result.speed = '0.00';
  573. }
  574. result = cpuBrandManufacturer(result);
  575. result.revision = util.getValue(lines, 'revision', '=');
  576. result.cache.l1d = 0;
  577. result.cache.l1i = 0;
  578. result.cache.l2 = util.getValue(lines, 'l2cachesize', '=');
  579. result.cache.l3 = util.getValue(lines, 'l3cachesize', '=');
  580. if (result.cache.l2) { result.cache.l2 = parseInt(result.cache.l2, 10) * 1024; }
  581. if (result.cache.l3) { result.cache.l3 = parseInt(result.cache.l3, 10) * 1024; }
  582. result.vendor = util.getValue(lines, 'manufacturer', '=');
  583. result.speedmax = Math.round(parseFloat(util.getValue(lines, 'maxclockspeed', '=').replace(/,/g, '.')) / 10.0) / 100;
  584. result.speedmax = result.speedmax ? parseFloat(result.speedmax).toFixed(2) : '';
  585. if (result.speed === '0.00' && (result.brand.indexOf('AMD') > -1 || result.brand.toLowerCase().indexOf('ryzen') > -1)) {
  586. result.speed = getAMDSpeed(result.brand);
  587. }
  588. if (result.speed === '0.00') {
  589. result.speed = result.speedmax;
  590. }
  591. let description = util.getValue(lines, 'description', '=').split(' ');
  592. for (let i = 0; i < description.length; i++) {
  593. if (description[i].toLowerCase().startsWith('family') && (i + 1) < description.length && description[i + 1]) {
  594. result.family = description[i + 1];
  595. }
  596. if (description[i].toLowerCase().startsWith('model') && (i + 1) < description.length && description[i + 1]) {
  597. result.model = description[i + 1];
  598. }
  599. if (description[i].toLowerCase().startsWith('stepping') && (i + 1) < description.length && description[i + 1]) {
  600. result.stepping = description[i + 1];
  601. }
  602. }
  603. // socket type
  604. const socketId = util.getValue(lines, 'UpgradeMethod', '=');
  605. if (socketTypes[socketId]) {
  606. result.socket = socketTypes[socketId];
  607. }
  608. // # threads / # cores
  609. const countProcessors = util.countLines(lines, 'Caption');
  610. const countThreads = util.getValue(lines, 'NumberOfLogicalProcessors', '=');
  611. const countCores = util.getValue(lines, 'NumberOfCores', '=');
  612. if (countProcessors) {
  613. result.processors = parseInt(countProcessors) || 1;
  614. }
  615. if (countCores && countThreads) {
  616. result.cores = parseInt(countThreads) || util.cores();
  617. result.physicalCores = parseInt(countCores) || util.cores();
  618. }
  619. if (countProcessors > 1) {
  620. result.cores = result.cores * countProcessors;
  621. result.physicalCores = result.physicalCores * countProcessors;
  622. }
  623. }
  624. util.wmic('path Win32_CacheMemory get CacheType,InstalledSize,Purpose').then((stdout, error) => {
  625. if (!error) {
  626. let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0);
  627. lines.forEach(function (line) {
  628. if (line !== '') {
  629. line = line.trim().split(/\s\s+/);
  630. // L1 Instructions
  631. if (line[2] === 'L1 Cache' && line[0] === '3') {
  632. result.cache.l1i = parseInt(line[1], 10);
  633. }
  634. // L1 Data
  635. if (line[2] === 'L1 Cache' && line[0] === '4') {
  636. result.cache.l1d = parseInt(line[1], 10);
  637. }
  638. }
  639. });
  640. }
  641. resolve(result);
  642. });
  643. });
  644. } catch (e) {
  645. resolve(result);
  646. }
  647. }
  648. });
  649. });
  650. }
  651. // --------------------------
  652. // CPU - Processor Data
  653. function cpu(callback) {
  654. return new Promise((resolve) => {
  655. process.nextTick(() => {
  656. getCpu().then(result => {
  657. if (callback) { callback(result); }
  658. resolve(result);
  659. });
  660. });
  661. });
  662. }
  663. exports.cpu = cpu;
  664. // --------------------------
  665. // CPU - current speed - in GHz
  666. function getCpuCurrentSpeedSync() {
  667. let cpus = os.cpus();
  668. let minFreq = 999999999;
  669. let maxFreq = 0;
  670. let avgFreq = 0;
  671. let cores = [];
  672. if (cpus && cpus.length) {
  673. for (let i in cpus) {
  674. if ({}.hasOwnProperty.call(cpus, i)) {
  675. avgFreq = avgFreq + cpus[i].speed;
  676. if (cpus[i].speed > maxFreq) maxFreq = cpus[i].speed;
  677. if (cpus[i].speed < minFreq) minFreq = cpus[i].speed;
  678. }
  679. cores.push(parseFloat(((cpus[i].speed + 1) / 1000).toFixed(2)));
  680. }
  681. avgFreq = avgFreq / cpus.length;
  682. return {
  683. min: parseFloat(((minFreq + 1) / 1000).toFixed(2)),
  684. max: parseFloat(((maxFreq + 1) / 1000).toFixed(2)),
  685. avg: parseFloat(((avgFreq + 1) / 1000).toFixed(2)),
  686. cores: cores
  687. };
  688. } else {
  689. return {
  690. min: 0,
  691. max: 0,
  692. avg: 0,
  693. cores: cores
  694. };
  695. }
  696. }
  697. function cpuCurrentspeed(callback) {
  698. return new Promise((resolve) => {
  699. process.nextTick(() => {
  700. let result = getCpuCurrentSpeedSync();
  701. if (result.avg === 0 && _cpu_speed !== '0.00') {
  702. const currCpuSpeed = parseFloat(_cpu_speed);
  703. result = {
  704. min: currCpuSpeed,
  705. max: currCpuSpeed,
  706. avg: currCpuSpeed,
  707. cores: []
  708. };
  709. }
  710. if (callback) { callback(result); }
  711. resolve(result);
  712. });
  713. });
  714. }
  715. exports.cpuCurrentspeed = cpuCurrentspeed;
  716. // --------------------------
  717. // CPU - temperature
  718. // if sensors are installed
  719. function cpuTemperature(callback) {
  720. return new Promise((resolve) => {
  721. process.nextTick(() => {
  722. let result = {
  723. main: -1.0,
  724. cores: [],
  725. max: -1.0
  726. };
  727. if (_linux) {
  728. const cmd = 'cat /sys/class/hwmon/hwmon1/temp*_la*;echo "---";cat /sys/class/hwmon/hwmon1/temp*_i*';
  729. exec(cmd, function (error, stdout) {
  730. if (!error) {
  731. let parts = stdout.toString().split('---');
  732. let labels = parts[0].split('\n');
  733. let temps = parts[1].split('\n');
  734. temps.shift();
  735. for (let i = 0; i < temps.length; i++) {
  736. if (temps[i] && (labels[i] === undefined || (labels[i] && labels[i].toLowerCase().startsWith('core')))) {
  737. result.cores.push(Math.round(parseInt(temps[i], 10) / 100) / 10);
  738. } else if (temps[i] && labels[i] && result.main === -1) {
  739. result.main = Math.round(parseInt(temps[i], 10) / 100) / 10;
  740. }
  741. }
  742. if (result.cores.length > 0) {
  743. if (result.main === -1) {
  744. result.main = Math.round(result.cores.reduce((a, b) => a + b, 0) / result.cores.length);
  745. }
  746. let maxtmp = Math.max.apply(Math, result.cores);
  747. result.max = (maxtmp > result.main) ? maxtmp : result.main;
  748. }
  749. if (result.main !== -1) {
  750. if (result.max === -1) {
  751. result.max = result.main;
  752. }
  753. if (callback) { callback(result); }
  754. resolve(result);
  755. return;
  756. }
  757. }
  758. exec('sensors', function (error, stdout) {
  759. if (!error) {
  760. let lines = stdout.toString().split('\n');
  761. let tdieTemp = -1;
  762. lines.forEach(function (line) {
  763. let regex = /[+-]([^°]*)/g;
  764. let temps = line.match(regex);
  765. let firstPart = line.split(':')[0].toUpperCase();
  766. if (firstPart.indexOf('PHYSICAL') !== -1 || firstPart.indexOf('PACKAGE') !== -1) {
  767. result.main = parseFloat(temps);
  768. }
  769. if (firstPart.indexOf('CORE ') !== -1) {
  770. result.cores.push(parseFloat(temps));
  771. }
  772. if (firstPart.indexOf('TDIE') !== -1 && tdieTemp === -1) {
  773. tdieTemp = parseFloat(temps);
  774. }
  775. });
  776. if (result.cores.length > 0) {
  777. if (result.main === -1) {
  778. result.main = Math.round(result.cores.reduce((a, b) => a + b, 0) / result.cores.length);
  779. }
  780. let maxtmp = Math.max.apply(Math, result.cores);
  781. result.max = (maxtmp > result.main) ? maxtmp : result.main;
  782. } else {
  783. if (result.main === -1 && tdieTemp !== -1) {
  784. result.main = tdieTemp;
  785. result.max = tdieTemp;
  786. }
  787. }
  788. if (result.main !== -1.0 || result.max !== -1.0) {
  789. if (callback) { callback(result); }
  790. resolve(result);
  791. return;
  792. }
  793. }
  794. fs.stat('/sys/class/thermal/thermal_zone0/temp', function (err) {
  795. if (err === null) {
  796. fs.readFile('/sys/class/thermal/thermal_zone0/temp', function (error, stdout) {
  797. if (!error) {
  798. let lines = stdout.toString().split('\n');
  799. if (lines.length > 0) {
  800. result.main = parseFloat(lines[0]) / 1000.0;
  801. result.max = result.main;
  802. }
  803. }
  804. if (callback) { callback(result); }
  805. resolve(result);
  806. });
  807. } else {
  808. exec('/opt/vc/bin/vcgencmd measure_temp', function (error, stdout) {
  809. if (!error) {
  810. let lines = stdout.toString().split('\n');
  811. if (lines.length > 0 && lines[0].indexOf('=')) {
  812. result.main = parseFloat(lines[0].split('=')[1]);
  813. result.max = result.main;
  814. }
  815. }
  816. if (callback) { callback(result); }
  817. resolve(result);
  818. });
  819. }
  820. });
  821. });
  822. });
  823. }
  824. if (_freebsd || _openbsd || _netbsd) {
  825. exec('sysctl dev.cpu | grep temp', function (error, stdout) {
  826. if (!error) {
  827. let lines = stdout.toString().split('\n');
  828. let sum = 0;
  829. lines.forEach(function (line) {
  830. const parts = line.split(':');
  831. if (parts.length > 1) {
  832. const temp = parseFloat(parts[1].replace(',', '.'));
  833. if (temp > result.max) result.max = temp;
  834. sum = sum + temp;
  835. result.cores.push(temp);
  836. }
  837. });
  838. if (result.cores.length) {
  839. result.main = Math.round(sum / result.cores.length * 100) / 100;
  840. }
  841. }
  842. if (callback) { callback(result); }
  843. resolve(result);
  844. });
  845. }
  846. if (_darwin) {
  847. let osxTemp = null;
  848. try {
  849. osxTemp = require('osx-temperature-sensor');
  850. } catch (er) {
  851. osxTemp = null;
  852. }
  853. if (osxTemp) {
  854. result = osxTemp.cpuTemperature();
  855. }
  856. if (callback) { callback(result); }
  857. resolve(result);
  858. }
  859. if (_sunos) {
  860. if (callback) { callback(result); }
  861. resolve(result);
  862. }
  863. if (_windows) {
  864. try {
  865. util.wmic('/namespace:\\\\root\\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature').then((stdout, error) => {
  866. if (!error) {
  867. let sum = 0;
  868. let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0);
  869. lines.forEach(function (line) {
  870. let value = (parseInt(line, 10) - 2732) / 10;
  871. sum = sum + value;
  872. if (value > result.max) result.max = value;
  873. result.cores.push(value);
  874. });
  875. if (result.cores.length) {
  876. result.main = sum / result.cores.length;
  877. }
  878. }
  879. if (callback) { callback(result); }
  880. resolve(result);
  881. });
  882. } catch (e) {
  883. if (callback) { callback(result); }
  884. resolve(result);
  885. }
  886. }
  887. });
  888. });
  889. }
  890. exports.cpuTemperature = cpuTemperature;
  891. // --------------------------
  892. // CPU Flags
  893. function cpuFlags(callback) {
  894. return new Promise((resolve) => {
  895. process.nextTick(() => {
  896. let result = '';
  897. if (_windows) {
  898. try {
  899. exec('reg query "HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0" /v FeatureSet', util.execOptsWin, function (error, stdout) {
  900. if (!error) {
  901. let flag_hex = stdout.split('0x').pop().trim();
  902. let flag_bin_unpadded = parseInt(flag_hex, 16).toString(2);
  903. let flag_bin = '0'.repeat(32 - flag_bin_unpadded.length) + flag_bin_unpadded;
  904. // empty flags are the reserved fields in the CPUID feature bit list
  905. // as found on wikipedia:
  906. // https://en.wikipedia.org/wiki/CPUID
  907. let all_flags = [
  908. 'fpu', 'vme', 'de', 'pse', 'tsc', 'msr', 'pae', 'mce', 'cx8', 'apic',
  909. '', 'sep', 'mtrr', 'pge', 'mca', 'cmov', 'pat', 'pse-36', 'psn', 'clfsh',
  910. '', 'ds', 'acpi', 'mmx', 'fxsr', 'sse', 'sse2', 'ss', 'htt', 'tm', 'ia64', 'pbe'
  911. ];
  912. for (let f = 0; f < all_flags.length; f++) {
  913. if (flag_bin[f] === '1' && all_flags[f] !== '') {
  914. result += ' ' + all_flags[f];
  915. }
  916. }
  917. result = result.trim();
  918. }
  919. if (callback) { callback(result); }
  920. resolve(result);
  921. });
  922. } catch (e) {
  923. if (callback) { callback(result); }
  924. resolve(result);
  925. }
  926. }
  927. if (_linux) {
  928. exec('export LC_ALL=C; lscpu; unset LC_ALL', function (error, stdout) {
  929. if (!error) {
  930. let lines = stdout.toString().split('\n');
  931. lines.forEach(function (line) {
  932. if (line.split(':')[0].toUpperCase().indexOf('FLAGS') !== -1) {
  933. result = line.split(':')[1].trim().toLowerCase();
  934. }
  935. });
  936. }
  937. if (!result) {
  938. fs.readFile('/proc/cpuinfo', function (error, stdout) {
  939. if (!error) {
  940. let lines = stdout.toString().split('\n');
  941. result = util.getValue(lines, 'features', ':', true).toLowerCase();
  942. }
  943. if (callback) { callback(result); }
  944. resolve(result);
  945. });
  946. } else {
  947. if (callback) { callback(result); }
  948. resolve(result);
  949. }
  950. });
  951. }
  952. if (_freebsd || _openbsd || _netbsd) {
  953. exec('export LC_ALL=C; dmidecode -t 4 2>/dev/null; unset LC_ALL', function (error, stdout) {
  954. let flags = [];
  955. if (!error) {
  956. let parts = stdout.toString().split('\tFlags:');
  957. const lines = parts.length > 1 ? parts[1].split('\tVersion:')[0].split['\n'] : [];
  958. lines.forEach(function (line) {
  959. let flag = (line.indexOf('(') ? line.split('(')[0].toLowerCase() : '').trim().replace(/\t/g, '');
  960. if (flag) {
  961. flags.push(flag);
  962. }
  963. });
  964. }
  965. result = flags.join(' ').trim();
  966. if (callback) { callback(result); }
  967. resolve(result);
  968. });
  969. }
  970. if (_darwin) {
  971. exec('sysctl machdep.cpu.features', function (error, stdout) {
  972. if (!error) {
  973. let lines = stdout.toString().split('\n');
  974. if (lines.length > 0 && lines[0].indexOf('machdep.cpu.features:') !== -1) {
  975. result = lines[0].split(':')[1].trim().toLowerCase();
  976. }
  977. }
  978. if (callback) { callback(result); }
  979. resolve(result);
  980. });
  981. }
  982. if (_sunos) {
  983. if (callback) { callback(result); }
  984. resolve(result);
  985. }
  986. });
  987. });
  988. }
  989. exports.cpuFlags = cpuFlags;
  990. // --------------------------
  991. // CPU Cache
  992. function cpuCache(callback) {
  993. return new Promise((resolve) => {
  994. process.nextTick(() => {
  995. let result = {
  996. l1d: -1,
  997. l1i: -1,
  998. l2: -1,
  999. l3: -1,
  1000. };
  1001. if (_linux) {
  1002. exec('export LC_ALL=C; lscpu; unset LC_ALL', function (error, stdout) {
  1003. if (!error) {
  1004. let lines = stdout.toString().split('\n');
  1005. lines.forEach(function (line) {
  1006. let parts = line.split(':');
  1007. if (parts[0].toUpperCase().indexOf('L1D CACHE') !== -1) {
  1008. result.l1d = parseInt(parts[1].trim()) * (parts[1].indexOf('K') !== -1 ? 1024 : 1);
  1009. }
  1010. if (parts[0].toUpperCase().indexOf('L1I CACHE') !== -1) {
  1011. result.l1i = parseInt(parts[1].trim()) * (parts[1].indexOf('K') !== -1 ? 1024 : 1);
  1012. }
  1013. if (parts[0].toUpperCase().indexOf('L2 CACHE') !== -1) {
  1014. result.l2 = parseInt(parts[1].trim()) * (parts[1].indexOf('K') !== -1 ? 1024 : 1);
  1015. }
  1016. if (parts[0].toUpperCase().indexOf('L3 CACHE') !== -1) {
  1017. result.l3 = parseInt(parts[1].trim()) * (parts[1].indexOf('K') !== -1 ? 1024 : 1);
  1018. }
  1019. });
  1020. }
  1021. if (callback) { callback(result); }
  1022. resolve(result);
  1023. });
  1024. }
  1025. if (_freebsd || _openbsd || _netbsd) {
  1026. exec('export LC_ALL=C; dmidecode -t 7 2>/dev/null; unset LC_ALL', function (error, stdout) {
  1027. let cache = [];
  1028. if (!error) {
  1029. const data = stdout.toString();
  1030. cache = data.split('Cache Information');
  1031. cache.shift();
  1032. }
  1033. for (let i = 0; i < cache.length; i++) {
  1034. const lines = cache[i].split('\n');
  1035. let cacheType = util.getValue(lines, 'Socket Designation').toLowerCase().replace(' ', '-').split('-');
  1036. cacheType = cacheType.length ? cacheType[0] : '';
  1037. const sizeParts = util.getValue(lines, 'Installed Size').split(' ');
  1038. let size = parseInt(sizeParts[0], 10);
  1039. const unit = sizeParts.length > 1 ? sizeParts[1] : 'kb';
  1040. size = size * (unit === 'kb' ? 1024 : (unit === 'mb' ? 1024 * 1024 : (unit === 'gb' ? 1024 * 1024 * 1024 : 1)));
  1041. if (cacheType) {
  1042. if (cacheType === 'l1') {
  1043. result.cache[cacheType + 'd'] = size / 2;
  1044. result.cache[cacheType + 'i'] = size / 2;
  1045. } else {
  1046. result.cache[cacheType] = size;
  1047. }
  1048. }
  1049. }
  1050. if (callback) { callback(result); }
  1051. resolve(result);
  1052. });
  1053. }
  1054. if (_darwin) {
  1055. exec('sysctl hw.l1icachesize hw.l1dcachesize hw.l2cachesize hw.l3cachesize', function (error, stdout) {
  1056. if (!error) {
  1057. let lines = stdout.toString().split('\n');
  1058. lines.forEach(function (line) {
  1059. let parts = line.split(':');
  1060. if (parts[0].toLowerCase().indexOf('hw.l1icachesize') !== -1) {
  1061. result.l1d = parseInt(parts[1].trim()) * (parts[1].indexOf('K') !== -1 ? 1024 : 1);
  1062. }
  1063. if (parts[0].toLowerCase().indexOf('hw.l1dcachesize') !== -1) {
  1064. result.l1i = parseInt(parts[1].trim()) * (parts[1].indexOf('K') !== -1 ? 1024 : 1);
  1065. }
  1066. if (parts[0].toLowerCase().indexOf('hw.l2cachesize') !== -1) {
  1067. result.l2 = parseInt(parts[1].trim()) * (parts[1].indexOf('K') !== -1 ? 1024 : 1);
  1068. }
  1069. if (parts[0].toLowerCase().indexOf('hw.l3cachesize') !== -1) {
  1070. result.l3 = parseInt(parts[1].trim()) * (parts[1].indexOf('K') !== -1 ? 1024 : 1);
  1071. }
  1072. });
  1073. }
  1074. if (callback) { callback(result); }
  1075. resolve(result);
  1076. });
  1077. }
  1078. if (_sunos) {
  1079. if (callback) { callback(result); }
  1080. resolve(result);
  1081. }
  1082. if (_windows) {
  1083. try {
  1084. util.wmic('cpu get l2cachesize, l3cachesize /value').then((stdout, error) => {
  1085. if (!error) {
  1086. let lines = stdout.split('\r\n');
  1087. result.l1d = 0;
  1088. result.l1i = 0;
  1089. result.l2 = util.getValue(lines, 'l2cachesize', '=');
  1090. result.l3 = util.getValue(lines, 'l3cachesize', '=');
  1091. if (result.l2) { result.l2 = parseInt(result.l2, 10) * 1024; }
  1092. if (result.l3) { result.l3 = parseInt(result.l3, 10) * 1024; }
  1093. }
  1094. util.wmic('path Win32_CacheMemory get CacheType,InstalledSize,Purpose').then((stdout, error) => {
  1095. if (!error) {
  1096. let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0);
  1097. lines.forEach(function (line) {
  1098. if (line !== '') {
  1099. line = line.trim().split(/\s\s+/);
  1100. // L1 Instructions
  1101. if (line[2] === 'L1 Cache' && line[0] === '3') {
  1102. result.l1i = parseInt(line[1], 10);
  1103. }
  1104. // L1 Data
  1105. if (line[2] === 'L1 Cache' && line[0] === '4') {
  1106. result.l1d = parseInt(line[1], 10);
  1107. }
  1108. }
  1109. });
  1110. }
  1111. if (callback) { callback(result); }
  1112. resolve(result);
  1113. });
  1114. });
  1115. } catch (e) {
  1116. if (callback) { callback(result); }
  1117. resolve(result);
  1118. }
  1119. }
  1120. });
  1121. });
  1122. }
  1123. exports.cpuCache = cpuCache;
  1124. // --------------------------
  1125. // CPU - current load - in %
  1126. function getLoad() {
  1127. return new Promise((resolve) => {
  1128. process.nextTick(() => {
  1129. let loads = os.loadavg().map(function (x) { return x / util.cores(); });
  1130. let avgload = parseFloat((Math.max.apply(Math, loads)).toFixed(2));
  1131. let result = {};
  1132. let now = Date.now() - _current_cpu.ms;
  1133. if (now >= 200) {
  1134. _current_cpu.ms = Date.now();
  1135. const cpus = os.cpus();
  1136. let totalUser = 0;
  1137. let totalSystem = 0;
  1138. let totalNice = 0;
  1139. let totalIrq = 0;
  1140. let totalIdle = 0;
  1141. let cores = [];
  1142. _corecount = (cpus && cpus.length) ? cpus.length : 0;
  1143. for (let i = 0; i < _corecount; i++) {
  1144. const cpu = cpus[i].times;
  1145. totalUser += cpu.user;
  1146. totalSystem += cpu.sys;
  1147. totalNice += cpu.nice;
  1148. totalIdle += cpu.idle;
  1149. totalIrq += cpu.irq;
  1150. let tmp_tick = (_cpus && _cpus[i] && _cpus[i].totalTick ? _cpus[i].totalTick : 0);
  1151. let tmp_load = (_cpus && _cpus[i] && _cpus[i].totalLoad ? _cpus[i].totalLoad : 0);
  1152. let tmp_user = (_cpus && _cpus[i] && _cpus[i].user ? _cpus[i].user : 0);
  1153. let tmp_system = (_cpus && _cpus[i] && _cpus[i].sys ? _cpus[i].sys : 0);
  1154. let tmp_nice = (_cpus && _cpus[i] && _cpus[i].nice ? _cpus[i].nice : 0);
  1155. let tmp_idle = (_cpus && _cpus[i] && _cpus[i].idle ? _cpus[i].idle : 0);
  1156. let tmp_irq = (_cpus && _cpus[i] && _cpus[i].irq ? _cpus[i].irq : 0);
  1157. _cpus[i] = cpu;
  1158. _cpus[i].totalTick = _cpus[i].user + _cpus[i].sys + _cpus[i].nice + _cpus[i].irq + _cpus[i].idle;
  1159. _cpus[i].totalLoad = _cpus[i].user + _cpus[i].sys + _cpus[i].nice + _cpus[i].irq;
  1160. _cpus[i].currentTick = _cpus[i].totalTick - tmp_tick;
  1161. _cpus[i].load = (_cpus[i].totalLoad - tmp_load);
  1162. _cpus[i].load_user = (_cpus[i].user - tmp_user);
  1163. _cpus[i].load_system = (_cpus[i].sys - tmp_system);
  1164. _cpus[i].load_nice = (_cpus[i].nice - tmp_nice);
  1165. _cpus[i].load_idle = (_cpus[i].idle - tmp_idle);
  1166. _cpus[i].load_irq = (_cpus[i].irq - tmp_irq);
  1167. cores[i] = {};
  1168. cores[i].load = _cpus[i].load / _cpus[i].currentTick * 100;
  1169. cores[i].load_user = _cpus[i].load_user / _cpus[i].currentTick * 100;
  1170. cores[i].load_system = _cpus[i].load_system / _cpus[i].currentTick * 100;
  1171. cores[i].load_nice = _cpus[i].load_nice / _cpus[i].currentTick * 100;
  1172. cores[i].load_idle = _cpus[i].load_idle / _cpus[i].currentTick * 100;
  1173. cores[i].load_irq = _cpus[i].load_irq / _cpus[i].currentTick * 100;
  1174. cores[i].raw_load = _cpus[i].load;
  1175. cores[i].raw_load_user = _cpus[i].load_user;
  1176. cores[i].raw_load_system = _cpus[i].load_system;
  1177. cores[i].raw_load_nice = _cpus[i].load_nice;
  1178. cores[i].raw_load_idle = _cpus[i].load_idle;
  1179. cores[i].raw_load_irq = _cpus[i].load_irq;
  1180. }
  1181. let totalTick = totalUser + totalSystem + totalNice + totalIrq + totalIdle;
  1182. let totalLoad = totalUser + totalSystem + totalNice + totalIrq;
  1183. let currentTick = totalTick - _current_cpu.tick;
  1184. result = {
  1185. avgload: avgload,
  1186. currentload: (totalLoad - _current_cpu.load) / currentTick * 100,
  1187. currentload_user: (totalUser - _current_cpu.user) / currentTick * 100,
  1188. currentload_system: (totalSystem - _current_cpu.system) / currentTick * 100,
  1189. currentload_nice: (totalNice - _current_cpu.nice) / currentTick * 100,
  1190. currentload_idle: (totalIdle - _current_cpu.idle) / currentTick * 100,
  1191. currentload_irq: (totalIrq - _current_cpu.irq) / currentTick * 100,
  1192. raw_currentload: (totalLoad - _current_cpu.load),
  1193. raw_currentload_user: (totalUser - _current_cpu.user),
  1194. raw_currentload_system: (totalSystem - _current_cpu.system),
  1195. raw_currentload_nice: (totalNice - _current_cpu.nice),
  1196. raw_currentload_idle: (totalIdle - _current_cpu.idle),
  1197. raw_currentload_irq: (totalIrq - _current_cpu.irq),
  1198. cpus: cores
  1199. };
  1200. _current_cpu = {
  1201. user: totalUser,
  1202. nice: totalNice,
  1203. system: totalSystem,
  1204. idle: totalIdle,
  1205. irq: totalIrq,
  1206. tick: totalTick,
  1207. load: totalLoad,
  1208. ms: _current_cpu.ms,
  1209. currentload: result.currentload,
  1210. currentload_user: result.currentload_user,
  1211. currentload_system: result.currentload_system,
  1212. currentload_nice: result.currentload_nice,
  1213. currentload_idle: result.currentload_idle,
  1214. currentload_irq: result.currentload_irq,
  1215. raw_currentload: result.raw_currentload,
  1216. raw_currentload_user: result.raw_currentload_user,
  1217. raw_currentload_system: result.raw_currentload_system,
  1218. raw_currentload_nice: result.raw_currentload_nice,
  1219. raw_currentload_idle: result.raw_currentload_idle,
  1220. raw_currentload_irq: result.raw_currentload_irq,
  1221. };
  1222. } else {
  1223. let cores = [];
  1224. for (let i = 0; i < _corecount; i++) {
  1225. cores[i] = {};
  1226. cores[i].load = _cpus[i].load / _cpus[i].currentTick * 100;
  1227. cores[i].load_user = _cpus[i].load_user / _cpus[i].currentTick * 100;
  1228. cores[i].load_system = _cpus[i].load_system / _cpus[i].currentTick * 100;
  1229. cores[i].load_nice = _cpus[i].load_nice / _cpus[i].currentTick * 100;
  1230. cores[i].load_idle = _cpus[i].load_idle / _cpus[i].currentTick * 100;
  1231. cores[i].load_irq = _cpus[i].load_irq / _cpus[i].currentTick * 100;
  1232. cores[i].raw_load = _cpus[i].load;
  1233. cores[i].raw_load_user = _cpus[i].load_user;
  1234. cores[i].raw_load_system = _cpus[i].load_system;
  1235. cores[i].raw_load_nice = _cpus[i].load_nice;
  1236. cores[i].raw_load_idle = _cpus[i].load_idle;
  1237. cores[i].raw_load_irq = _cpus[i].load_irq;
  1238. }
  1239. result = {
  1240. avgload: avgload,
  1241. currentload: _current_cpu.currentload,
  1242. currentload_user: _current_cpu.currentload_user,
  1243. currentload_system: _current_cpu.currentload_system,
  1244. currentload_nice: _current_cpu.currentload_nice,
  1245. currentload_idle: _current_cpu.currentload_idle,
  1246. currentload_irq: _current_cpu.currentload_irq,
  1247. raw_currentload: _current_cpu.raw_currentload,
  1248. raw_currentload_user: _current_cpu.raw_currentload_user,
  1249. raw_currentload_system: _current_cpu.raw_currentload_system,
  1250. raw_currentload_nice: _current_cpu.raw_currentload_nice,
  1251. raw_currentload_idle: _current_cpu.raw_currentload_idle,
  1252. raw_currentload_irq: _current_cpu.raw_currentload_irq,
  1253. cpus: cores
  1254. };
  1255. }
  1256. resolve(result);
  1257. });
  1258. });
  1259. }
  1260. function currentLoad(callback) {
  1261. return new Promise((resolve) => {
  1262. process.nextTick(() => {
  1263. getLoad().then(result => {
  1264. if (callback) { callback(result); }
  1265. resolve(result);
  1266. });
  1267. });
  1268. });
  1269. }
  1270. exports.currentLoad = currentLoad;
  1271. // --------------------------
  1272. // PS - full load
  1273. // since bootup
  1274. function getFullLoad() {
  1275. return new Promise((resolve) => {
  1276. process.nextTick(() => {
  1277. const cpus = os.cpus();
  1278. let totalUser = 0;
  1279. let totalSystem = 0;
  1280. let totalNice = 0;
  1281. let totalIrq = 0;
  1282. let totalIdle = 0;
  1283. let result = 0;
  1284. if (cpus && cpus.length) {
  1285. for (let i = 0, len = cpus.length; i < len; i++) {
  1286. const cpu = cpus[i].times;
  1287. totalUser += cpu.user;
  1288. totalSystem += cpu.sys;
  1289. totalNice += cpu.nice;
  1290. totalIrq += cpu.irq;
  1291. totalIdle += cpu.idle;
  1292. }
  1293. let totalTicks = totalIdle + totalIrq + totalNice + totalSystem + totalUser;
  1294. result = (totalTicks - totalIdle) / totalTicks * 100.0;
  1295. } else {
  1296. result = 0;
  1297. }
  1298. resolve(result);
  1299. });
  1300. });
  1301. }
  1302. function fullLoad(callback) {
  1303. return new Promise((resolve) => {
  1304. process.nextTick(() => {
  1305. getFullLoad().then(result => {
  1306. if (callback) { callback(result); }
  1307. resolve(result);
  1308. });
  1309. });
  1310. });
  1311. }
  1312. exports.fullLoad = fullLoad;