From 4ae6141b31ba875ed78e9477d699c5ac31748c2e Mon Sep 17 00:00:00 2001 From: Nick Babcock Date: Mon, 19 Nov 2018 09:33:05 -0600 Subject: [PATCH] Performance improvements when NIC Enabled (#67) Thanks to @nickbabcock --- Hardware/Computer.cs | 11 +----- Hardware/Nic/Nic.cs | 66 ++++++++++++++------------------- Hardware/Nic/NicGroup.cs | 79 +++++++++++++++++++++++----------------- 3 files changed, 75 insertions(+), 81 deletions(-) diff --git a/Hardware/Computer.cs b/Hardware/Computer.cs index 6e0cffa..2947909 100644 --- a/Hardware/Computer.cs +++ b/Hardware/Computer.cs @@ -401,17 +401,8 @@ namespace OpenHardwareMonitor.Hardware { } public void Traverse(IVisitor visitor) { - if (nicEnabled) { - int newNiccount = NetworkInterface.GetAllNetworkInterfaces().Length; - if (nicCount != newNiccount) { - nicCount = newNiccount; - NICEnabled = false; - NICEnabled = true; - } - } - foreach (IGroup group in groups) - foreach (IHardware hardware in group.Hardware) + foreach (IHardware hardware in group.Hardware) hardware.Accept(visitor); } diff --git a/Hardware/Nic/Nic.cs b/Hardware/Nic/Nic.cs index 3c51d32..0927620 100644 --- a/Hardware/Nic/Nic.cs +++ b/Hardware/Nic/Nic.cs @@ -9,7 +9,6 @@ */ using System; -using System.Diagnostics; using System.Globalization; using System.Net.NetworkInformation; @@ -17,61 +16,52 @@ namespace OpenHardwareMonitor.Hardware.Nic { internal class Nic : Hardware { - private ISettings settings; private Sensor dataUploaded; private Sensor dataDownloaded; private Sensor uploadSpeed; private Sensor downloadSpeed; private Sensor networkUtilization; - private NetworkInterface networkInterface; - private int nicIndex; private DateTime latesTime; - private DateTime presentBootTime; private long bytesUploaded; - private long bytesDownloaded; - private bool shouldTotalFlowUpdate = true; - - public Nic(string name, ISettings Settings, int index, NicGroup nicGroup) - : base(name, new Identifier("NIC",index.ToString(CultureInfo.InvariantCulture)), Settings) + private long bytesDownloaded; + private readonly NetworkInterface networkInterface; + + public Nic(NetworkInterface networkInterface, ISettings settings, int index) + : base(networkInterface.Name, new Identifier("NIC",index.ToString(CultureInfo.InvariantCulture)), settings) { - settings = Settings; - nicIndex = index; - networkInterface = nicGroup.NetworkInterfaces[index]; - presentBootTime = DateTime.Now.AddMilliseconds(-(double)Environment.TickCount); - dataUploaded = new Sensor("Data Uploaded", 2, SensorType.Data, this, - settings); + this.networkInterface = networkInterface; + dataUploaded = new Sensor("Data Uploaded", 2, SensorType.Data, this, settings); ActivateSensor(dataUploaded); - dataDownloaded = new Sensor("Data Downloaded", 3, SensorType.Data, this, - settings); + dataDownloaded = new Sensor("Data Downloaded", 3, SensorType.Data, this, settings); ActivateSensor(dataDownloaded); - uploadSpeed = new Sensor("Upload Speed", 7, SensorType.Throughput, this, - settings); + uploadSpeed = new Sensor("Upload Speed", 7, SensorType.Throughput, this, settings); ActivateSensor(uploadSpeed); - downloadSpeed = new Sensor("Download Speed", 8, SensorType.Throughput, this, - settings); + downloadSpeed = new Sensor("Download Speed", 8, SensorType.Throughput, this, settings); ActivateSensor(downloadSpeed); - networkUtilization = new Sensor("Network Utilization", 1, SensorType.Load, this, - settings); + networkUtilization = new Sensor("Network Utilization", 1, SensorType.Load, this, settings); ActivateSensor(networkUtilization); bytesUploaded = NetworkInterface.GetIPStatistics().BytesSent; bytesDownloaded = NetworkInterface.GetIPStatistics().BytesReceived; latesTime = DateTime.Now; } - public override HardwareType HardwareType - { - get - { - return HardwareType.NIC; - } - } - - internal NetworkInterface NetworkInterface - { - get { return networkInterface; } - } - + public override HardwareType HardwareType + { + get + { + return HardwareType.NIC; + } + } + + internal NetworkInterface NetworkInterface + { + get + { + return networkInterface; + } + } + public override void Update() { DateTime newTime = DateTime.Now; @@ -82,7 +72,7 @@ namespace OpenHardwareMonitor.Hardware.Nic long dBytesDownloaded = interfaceStats.BytesReceived - bytesDownloaded; uploadSpeed.Value = (float)dBytesUploaded / dt; downloadSpeed.Value = (float)dBytesDownloaded / dt; - networkUtilization.Value = Clamp((float)Math.Max(dBytesUploaded, dBytesDownloaded) * 800 / NetworkInterface.Speed / dt, 0,100); + networkUtilization.Value = Clamp((float)Math.Max(dBytesUploaded, dBytesDownloaded) * 800 / NetworkInterface.Speed / dt, 0, 100); bytesUploaded = interfaceStats.BytesSent; bytesDownloaded = interfaceStats.BytesReceived; dataUploaded.Value = ((float)bytesUploaded / 1073741824); diff --git a/Hardware/Nic/NicGroup.cs b/Hardware/Nic/NicGroup.cs index 771c73d..77e3c4c 100644 --- a/Hardware/Nic/NicGroup.cs +++ b/Hardware/Nic/NicGroup.cs @@ -1,5 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; +using System.Linq; using System.Net.NetworkInformation; using System.Text; @@ -7,28 +7,41 @@ namespace OpenHardwareMonitor.Hardware.Nic { internal class NicGroup : IGroup { - - private List hardware = new List(); - private NetworkInterface[] _networkInterfaces; + private readonly ISettings _settings; + private List _hardware = new List(); public NicGroup(ISettings settings) { - int p = (int)Environment.OSVersion.Platform; - if ((p == 4) || (p == 128)) + _settings = settings; + ScanNics(settings); + NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged; + NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAddressChanged; + } + + private void ScanNics(ISettings settings) + { + NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); + _hardware = NetworkInterfaces.Where(DesiredNetworkType) + .Select((x, i) => new Nic(x, settings, i)) + .ToList(); + } + + private void NetworkChange_NetworkAddressChanged(object sender, System.EventArgs e) + { + ScanNics(_settings); + } + + private static bool DesiredNetworkType(NetworkInterface nic) + { + switch (nic.NetworkInterfaceType) { - hardware = new List(); - return; + case NetworkInterfaceType.Loopback: + case NetworkInterfaceType.Tunnel: + case NetworkInterfaceType.Unknown: + return false; + default: + return true; } - _networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); - for (int i = 0; i < _networkInterfaces.Length; i++) - { - if (_networkInterfaces[i].NetworkInterfaceType != NetworkInterfaceType.Unknown && _networkInterfaces[i].NetworkInterfaceType != NetworkInterfaceType.Loopback && _networkInterfaces[i].NetworkInterfaceType != NetworkInterfaceType.Tunnel) - { - hardware.Add(new Nic(_networkInterfaces[i].Name, settings, i, this)); - } - - } - } public string GetReport() @@ -38,7 +51,7 @@ namespace OpenHardwareMonitor.Hardware.Nic var report = new StringBuilder(); - foreach (Nic hw in hardware) + foreach (Nic hw in _hardware) { report.AppendLine(hw.NetworkInterface.Description); report.AppendLine(hw.NetworkInterface.OperationalStatus.ToString()); @@ -54,21 +67,21 @@ namespace OpenHardwareMonitor.Hardware.Nic return report.ToString(); } - public IEnumerable Hardware => hardware; - public NetworkInterface[] NetworkInterfaces - { - get - { - return _networkInterfaces; - } - set - { - _networkInterfaces = value; - } - } + public IEnumerable Hardware + { + get + { + return _hardware; + } + } + + public NetworkInterface[] NetworkInterfaces { get; set; } + public void Close() { - foreach (Hardware nic in hardware) + NetworkChange.NetworkAddressChanged -= NetworkChange_NetworkAddressChanged; + NetworkChange.NetworkAvailabilityChanged -= NetworkChange_NetworkAddressChanged; + foreach (var nic in _hardware) nic.Close(); } }