Skip to content

Commit

Permalink
networkInterfaces(), cpu() improvements windows
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhildebrandt committed Mar 9, 2021
1 parent 8ef639e commit d49c332
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 79 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -30,7 +30,7 @@
[![Sponsoring][sponsor-badge]][sponsor-url]
[![MIT license][license-img]][license-url]

This is amazing. Started as a small project just for myself, it now has > 10,000 lines of code, > 400 versions published, > 3 mio downloads per month, > 30 mio downloads overall. Thank you to all who contributed to this project!
This is amazing. Started as a small project just for myself, it now has > 10,000 lines of code, > 400 versions published, up to 3 mio downloads per month, > 30 mio downloads overall. Thank you to all who contributed to this project!

## New Version 5.0

Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Expand Up @@ -209,7 +209,7 @@
<div class="title">Downloads last month</div>
</div>
<div class="col-xl-4 col-lg-4 col-md-4 col-12">
<div class="numbers">390</div>
<div class="numbers">393</div>
<div class="title">Dependents</div>
</div>
</div>
Expand Down
150 changes: 75 additions & 75 deletions lib/cpu.js
Expand Up @@ -750,88 +750,88 @@ function getCpu() {
}
if (_windows) {
try {
util.wmic('cpu get /value').then((stdout, error) => {
if (!error) {
let lines = stdout.split('\r\n');
let name = util.getValue(lines, 'name', '=') || '';
if (name.indexOf('@') >= 0) {
result.brand = name.split('@')[0].trim();
result.speed = name.split('@')[1] ? parseFloat(name.split('@')[1].trim()) : 0;
_cpu_speed = result.speed;
} else {
result.brand = name.trim();
result.speed = 0;
}
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)) {
result.speed = getAMDSpeed(result.brand);
}
if (result.speed === 0) {
result.speed = result.speedMax;
}
result.speedMin = result.speed;
const workload = [];
workload.push(util.wmic('cpu get /value'));
workload.push(util.wmic('path Win32_CacheMemory get CacheType,InstalledSize,Purpose'));

Promise.all(
workload
).then(data => {
let lines = data[0].split('\r\n');
let name = util.getValue(lines, 'name', '=') || '';
if (name.indexOf('@') >= 0) {
result.brand = name.split('@')[0].trim();
result.speed = name.split('@')[1] ? parseFloat(name.split('@')[1].trim()) : 0;
_cpu_speed = result.speed;
} else {
result.brand = name.trim();
result.speed = 0;
}
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)) {
result.speed = getAMDSpeed(result.brand);
}
if (result.speed === 0) {
result.speed = result.speedMax;
}
result.speedMin = result.speed;

let description = util.getValue(lines, 'description', '=').split(' ');
for (let i = 0; i < description.length; i++) {
if (description[i].toLowerCase().startsWith('family') && (i + 1) < description.length && description[i + 1]) {
result.family = description[i + 1];
}
if (description[i].toLowerCase().startsWith('model') && (i + 1) < description.length && description[i + 1]) {
result.model = description[i + 1];
}
if (description[i].toLowerCase().startsWith('stepping') && (i + 1) < description.length && description[i + 1]) {
result.stepping = description[i + 1];
}
}
// socket type
const socketId = util.getValue(lines, 'UpgradeMethod', '=');
if (socketTypes[socketId]) {
result.socket = socketTypes[socketId];
}
// # threads / # cores
const countProcessors = util.countLines(lines, 'Caption');
const countThreads = util.getValue(lines, 'NumberOfLogicalProcessors', '=');
const countCores = util.getValue(lines, 'NumberOfCores', '=');
if (countProcessors) {
result.processors = parseInt(countProcessors) || 1;
let description = util.getValue(lines, 'description', '=').split(' ');
for (let i = 0; i < description.length; i++) {
if (description[i].toLowerCase().startsWith('family') && (i + 1) < description.length && description[i + 1]) {
result.family = description[i + 1];
}
if (countCores && countThreads) {
result.cores = parseInt(countThreads) || util.cores();
result.physicalCores = parseInt(countCores) || util.cores();
if (description[i].toLowerCase().startsWith('model') && (i + 1) < description.length && description[i + 1]) {
result.model = description[i + 1];
}
if (countProcessors > 1) {
result.cores = result.cores * countProcessors;
result.physicalCores = result.physicalCores * countProcessors;
if (description[i].toLowerCase().startsWith('stepping') && (i + 1) < description.length && description[i + 1]) {
result.stepping = description[i + 1];
}
}
util.wmic('path Win32_CacheMemory get CacheType,InstalledSize,Purpose').then((stdout, error) => {
if (!error) {
let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0);
lines.forEach(function (line) {
if (line !== '') {
line = line.trim().split(/\s\s+/);
// L1 Instructions
if (line[2] === 'L1 Cache' && line[0] === '3') {
result.cache.l1i = parseInt(line[1], 10);
}
// L1 Data
if (line[2] === 'L1 Cache' && line[0] === '4') {
result.cache.l1d = parseInt(line[1], 10);
}
}
});
// socket type
const socketId = util.getValue(lines, 'UpgradeMethod', '=');
if (socketTypes[socketId]) {
result.socket = socketTypes[socketId];
}
// # threads / # cores
const countProcessors = util.countLines(lines, 'Caption');
const countThreads = util.getValue(lines, 'NumberOfLogicalProcessors', '=');
const countCores = util.getValue(lines, 'NumberOfCores', '=');
if (countProcessors) {
result.processors = parseInt(countProcessors) || 1;
}
if (countCores && countThreads) {
result.cores = parseInt(countThreads) || util.cores();
result.physicalCores = parseInt(countCores) || util.cores();
}
if (countProcessors > 1) {
result.cores = result.cores * countProcessors;
result.physicalCores = result.physicalCores * countProcessors;
}
lines = data[1].split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0);
lines.forEach(function (line) {
if (line !== '') {
line = line.trim().split(/\s\s+/);
// L1 Instructions
if (line[2] === 'L1 Cache' && line[0] === '3') {
result.cache.l1i = parseInt(line[1], 10);
}
// L1 Data
if (line[2] === 'L1 Cache' && line[0] === '4') {
result.cache.l1d = parseInt(line[1], 10);
}
}
resolve(result);
});
resolve(result);
});
} catch (e) {
resolve(result);
Expand Down
8 changes: 6 additions & 2 deletions lib/network.js
Expand Up @@ -221,6 +221,7 @@ function parseLinesWindowsNics(sections, nconfigsections) {
let netEnabled = util.getValue(lines, 'NetEnabled', '=');
let adapterType = util.getValue(lines, 'AdapterTypeID', '=') === '9' ? 'wireless' : 'wired';
let ifacename = util.getValue(lines, 'Name', '=').replace(/\]/g, ')').replace(/\[/g, '(');
let iface = util.getValue(lines, 'NetConnectionID', '=').replace(/\]/g, ')').replace(/\[/g, '(');
if (ifacename.toLowerCase().indexOf('wi-fi') >= 0 || ifacename.toLowerCase().indexOf('wireless') >= 0) {
adapterType = 'wireless';
}
Expand All @@ -230,6 +231,7 @@ function parseLinesWindowsNics(sections, nconfigsections) {
mac: util.getValue(lines, 'MACAddress', '=').toLowerCase(),
dhcp: util.getValue(linesNicConfig, 'dhcpEnabled', '=').toLowerCase(),
name: ifacename,
iface,
netEnabled: netEnabled === 'TRUE',
speed: isNaN(speed) ? null : speed,
operstate: util.getValue(lines, 'NetConnectionStatus', '=') === '2' ? 'up' : 'down',
Expand All @@ -243,7 +245,7 @@ function parseLinesWindowsNics(sections, nconfigsections) {
}

function getWindowsNics() {
const cmd = util.getWmic() + ' nic get MACAddress, name, NetEnabled, Speed, NetConnectionStatus, AdapterTypeId /value';
const cmd = util.getWmic() + ' nic get MACAddress, name, NetConnectionId, NetEnabled, Speed, NetConnectionStatus, AdapterTypeId /value';
const cmdnicconfig = util.getWmic() + ' nicconfig get dhcpEnabled /value';
try {
const nsections = execSync(cmd, util.execOptsWin).split(/\n\s*\n/);
Expand Down Expand Up @@ -774,6 +776,7 @@ function networkInterfaces(callback, rescan = true) {
_dhcpNics = getLinuxDHCPNics();
}
for (let dev in ifaces) {
let iface = dev;
let ip4 = '';
let ip4subnet = '';
let ip6 = '';
Expand Down Expand Up @@ -873,6 +876,7 @@ function networkInterfaces(callback, rescan = true) {
dnsSuffix = getWindowsIfaceDNSsuffix(dnsSuffixes.ifaces, dev);
nics.forEach(detail => {
if (detail.mac === mac) {
iface = detail.iface || iface;
ifaceName = detail.name;
dhcp = detail.dhcp;
operstate = detail.operstate;
Expand All @@ -895,7 +899,7 @@ function networkInterfaces(callback, rescan = true) {
}
const virtual = internal ? false : testVirtualNic(dev, ifaceName, mac);
result.push({
iface: dev,
iface,
ifaceName,
ip4,
ip4subnet,
Expand Down

0 comments on commit d49c332

Please sign in to comment.