diff --git a/lib/cpu.js b/lib/cpu.js index d69811b..0cc1644 100644 --- a/lib/cpu.js +++ b/lib/cpu.js @@ -862,12 +862,6 @@ function getCpu() { } result = cpuBrandManufacturer(result); result.revision = util.getValue(lines, 'revision', ':'); - result.cache.l1d = 0; - result.cache.l1i = 0; - result.cache.l2 = util.getValue(lines, 'l2cachesize', ':'); - result.cache.l3 = util.getValue(lines, 'l3cachesize', ':'); - if (result.cache.l2) { result.cache.l2 = parseInt(result.cache.l2, 10) * 1024; } - if (result.cache.l3) { result.cache.l3 = parseInt(result.cache.l3, 10) * 1024; } result.vendor = util.getValue(lines, 'manufacturer', ':'); result.speedMax = Math.round(parseFloat(util.getValue(lines, 'maxclockspeed', ':').replace(/,/g, '.')) / 10.0) / 100; if (result.speed === 0 && (result.brand.indexOf('AMD') > -1 || result.brand.toLowerCase().indexOf('ryzen') > -1)) { @@ -914,26 +908,7 @@ function getCpu() { result.cores = result.cores * countProcessors; result.physicalCores = result.physicalCores * countProcessors; } - const parts = data[1].split(/\n\s*\n/); - parts.forEach(function (part) { - lines = part.split('\r\n'); - const cacheType = util.getValue(lines, 'CacheType'); - const level = util.getValue(lines, 'Level'); - const installedSize = util.getValue(lines, 'InstalledSize'); - // L1 Instructions - if (level === '3' && cacheType === '3') { - result.cache.l1i = parseInt(installedSize, 10); - } - // L1 Data - if (level === '3' && cacheType === '4') { - result.cache.l1d = parseInt(installedSize, 10); - } - // L1 all - if (level === '3' && cacheType === '5' && !result.cache.l1i && !result.cache.l1d) { - result.cache.l1i = parseInt(installedSize, 10) / 2; - result.cache.l1d = parseInt(installedSize, 10) / 2; - } - }); + result.cache = parseWinCache(data[0], data[1]); const hyperv = data[2] ? data[2].toString().toLowerCase() : ''; result.virtualization = hyperv.indexOf('true') !== -1; @@ -1483,42 +1458,17 @@ function cpuCache(callback) { } if (_windows) { try { - util.powerShell('Get-CimInstance Win32_processor | select L2CacheSize, L3CacheSize | fl').then((stdout, error) => { - if (!error) { - let lines = stdout.split('\r\n'); - result.l1d = 0; - result.l1i = 0; - result.l2 = util.getValue(lines, 'l2cachesize', ':'); - result.l3 = util.getValue(lines, 'l3cachesize', ':'); - if (result.l2) { result.l2 = parseInt(result.l2, 10) * 1024; } - if (result.l3) { result.l3 = parseInt(result.l3, 10) * 1024; } - } - util.powerShell('Get-CimInstance Win32_CacheMemory | select CacheType,InstalledSize,Level | fl').then((stdout, error) => { - if (!error) { - const parts = stdout.split(/\n\s*\n/); - parts.forEach(function (part) { - const lines = part.split('\r\n'); - const cacheType = util.getValue(lines, 'CacheType'); - const level = util.getValue(lines, 'Level'); - const installedSize = util.getValue(lines, 'InstalledSize'); - // L1 Instructions - if (level === '3' && cacheType === '3') { - result.l1i = parseInt(installedSize, 10); - } - // L1 Data - if (level === '3' && cacheType === '4') { - result.l1d = parseInt(installedSize, 10); - } - // L1 all - if (level === '3' && cacheType === '5' && !result.l1i && !result.l1d) { - result.l1i = parseInt(installedSize, 10) / 2; - result.l1d = parseInt(installedSize, 10) / 2; - } - }); - } - if (callback) { callback(result); } - resolve(result); - }); + const workload = []; + workload.push(util.powerShell('Get-CimInstance Win32_processor | select L2CacheSize, L3CacheSize | fl')); + workload.push(util.powerShell('Get-CimInstance Win32_CacheMemory | select CacheType,InstalledSize,Level | fl')); + + Promise.all( + workload + ).then((data) => { + result = parseWinCache(data[0], data[1]); + + if (callback) { callback(result); } + resolve(result); }); } catch (e) { if (callback) { callback(result); } @@ -1529,6 +1479,61 @@ function cpuCache(callback) { }); } +function parseWinCache(linesProc, linesCache) { + let result = { + l1d: null, + l1i: null, + l2: null, + l3: null, + }; + + // Win32_processor + let lines = linesProc.split('\r\n'); + result.l1d = 0; + result.l1i = 0; + result.l2 = util.getValue(lines, 'l2cachesize', ':'); + result.l3 = util.getValue(lines, 'l3cachesize', ':'); + if (result.l2) { result.l2 = parseInt(result.l2, 10) * 1024; } + if (result.l3) { result.l3 = parseInt(result.l3, 10) * 1024; } + + // Win32_CacheMemory + const parts = linesCache.split(/\n\s*\n/); + let l1i = 0; + let l1d = 0; + let l2 = 0; + parts.forEach(function (part) { + const lines = part.split('\r\n'); + const cacheType = util.getValue(lines, 'CacheType'); + const level = util.getValue(lines, 'Level'); + const installedSize = util.getValue(lines, 'InstalledSize'); + // L1 Instructions + if (level === '3' && cacheType === '3') { + result.l1i = result.l1i + parseInt(installedSize, 10) * 1024; + } + // L1 Data + if (level === '3' && cacheType === '4') { + result.l1d = result.l1d + parseInt(installedSize, 10) * 1024; + } + // L1 all + if (level === '3' && cacheType === '5') { + l1i = parseInt(installedSize, 10) / 2; + l1d = parseInt(installedSize, 10) / 2; + } + // L2 + if (level === '4' && cacheType === '5') { + l2 = l2 + parseInt(installedSize, 10) * 1024; + } + }); + if (!result.l1i && !result.l1d) { + result.l1i = l1i; + result.l1d = l1d; + } + if (l2) { + result.l2 = l2; + } + return result; +} + exports.cpuCache = cpuCache; // --------------------------