private async Task PollNetworkUtilizationAsync() { var utilizationTable = _canQueryAdapterUtilization ? "Win32_PerfRawData_Tcpip_NetworkAdapter" : "Win32_PerfRawData_Tcpip_NetworkInterface"; var query = $@" SELECT Name, Timestamp_Sys100NS, BytesReceivedPersec, BytesSentPersec, PacketsReceivedPersec, PacketsSentPersec FROM {utilizationTable}"; var queryTime = DateTime.UtcNow.ToEpochTime(); var combinedUtil = new Interface.InterfaceUtilization { DateEpoch = queryTime, InAvgBps = 0, OutAvgBps = 0 }; using (var q = Wmi.Query(Endpoint, query)) { foreach (var data in await q.GetDynamicResultAsync().ConfigureAwait(false)) { var perfData = new PerfRawData(this, data); var name = perfData.Identifier; var iface = Interfaces.Find(i => name == GetCounterName(i.Name)); if (iface == null) { continue; } iface.InBps = (float)perfData.GetCalculatedValue("BytesReceivedPersec", 10000000); iface.OutBps = (float)perfData.GetCalculatedValue("BytesSentPersec", 10000000); iface.InPps = (float)perfData.GetCalculatedValue("PacketsReceivedPersec", 10000000); iface.OutPps = (float)perfData.GetCalculatedValue("PacketsSentPersec", 10000000); var util = new Interface.InterfaceUtilization { DateEpoch = queryTime, InAvgBps = iface.InBps, OutAvgBps = iface.OutBps }; var netData = NetHistory.GetOrAdd(iface.Name, k => new List <Interface.InterfaceUtilization>(1024)); UpdateHistoryStorage(netData, util); if (PrimaryInterfaces.Contains(iface)) { combinedUtil.InAvgBps += util.InAvgBps; combinedUtil.OutAvgBps += util.OutAvgBps; } } } UpdateHistoryStorage(CombinedNetHistory, combinedUtil); }
private double GetCalculatedValue(PerfRawData previousData, string property, double scale) { var timestampDiff = Math.Max(1, Timestamp - previousData.Timestamp); var valueDiff = Convert.ToUInt64(_data[property]) - Convert.ToUInt64(previousData._data[property]); var scaledValueDiff = valueDiff * scale; return(scaledValueDiff / timestampDiff); }
private async Task PollVolumePerformanceUtilizationAsync() { const string query = @" SELECT Name, Timestamp_Sys100NS, DiskReadBytesPersec, DiskWriteBytesPersec FROM Win32_PerfRawData_PerfDisk_LogicalDisk"; var queryTime = DateTime.UtcNow.ToEpochTime(); var combinedUtil = new Volume.VolumePerformanceUtilization { DateEpoch = queryTime, ReadAvgBps = 0, WriteAvgBps = 0 }; using (var q = Wmi.Query(Endpoint, query)) { foreach (var data in await q.GetDynamicResultAsync().ConfigureAwait(false)) { var perfData = new PerfRawData(this, data); var name = perfData.Identifier; var iface = Volumes.Find(i => name == GetCounterName(i.Name)); if (iface == null) { continue; } iface.ReadBps = (float)perfData.GetCalculatedValue("DiskReadBytesPersec", 10000000); iface.WriteBps = (float)perfData.GetCalculatedValue("DiskWriteBytesPersec", 10000000); var util = new Volume.VolumePerformanceUtilization { DateEpoch = queryTime, ReadAvgBps = iface.ReadBps, WriteAvgBps = iface.WriteBps }; var netData = VolumePerformanceHistory.GetOrAdd(iface.Name, k => new List <Volume.VolumePerformanceUtilization>(1024)); UpdateHistoryStorage(netData, util); //if (PrimaryInterfaces.Contains(iface)) { combinedUtil.ReadAvgBps += util.ReadAvgBps; combinedUtil.WriteAvgBps += util.WriteAvgBps; } } } UpdateHistoryStorage(CombinedVolumePerformanceHistory, combinedUtil); }
public PerfRawData(WmiNode node, ManagementObject data) { _data = data; Classname = data.ClassPath.ClassName; Identifier = (string)_data["Name"]; Timestamp = Convert.ToUInt64(_data["Timestamp_Sys100NS"]); string cacheKey = $"{Classname}.{Identifier}"; // "previous.previousData = null" to cleanup previousData from previousData. Otherwise we get a linked list which never cleans up. node.previousPerfDataCache.AddOrUpdate(cacheKey, s => _previousData = this, (s, previous) => { previous._previousData = null; _previousData = previous; return(this); }); }
private async Task PollCpuUtilizationAsync() { var query = IsVMHost ? "SELECT Name, Timestamp_Sys100NS, PercentTotalRunTime FROM Win32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor WHERE Name = '_Total'" : "SELECT Name, Timestamp_Sys100NS, PercentProcessorTime FROM Win32_PerfRawData_PerfOS_Processor WHERE Name = '_Total'"; var property = IsVMHost ? "PercentTotalRunTime" : "PercentProcessorTime"; using (var q = Wmi.Query(Endpoint, query)) { var data = await q.GetFirstResultAsync().ConfigureAwait(false); if (data == null) { return; } var perfData = new PerfRawData(this, data); if (IsVMHost) { CPULoad = (short)(perfData.GetCalculatedValue(property, 100D) / NumberOfLogicalProcessors); } else { CPULoad = (short)Math.Round((1 - perfData.GetCalculatedValue(property)) * 100); } var cpuUtilization = new CPUUtilization { DateEpoch = DateTime.UtcNow.ToEpochTime(), AvgLoad = CPULoad }; UpdateHistoryStorage(CPUHistory, cpuUtilization); } }