agrobot_base/AgroBase/AgroBase/Models/PerformanceModel.cs

176 lines
6.1 KiB
C#

using OpenHardwareMonitor.Hardware;
using System.Diagnostics;
using System.Threading;
using System;
namespace AgroBase.Models
{
public class PerformanceModel
{
private static Computer computer;
public PerformanceModel()
{
if (computer == null)
{
computer = new Computer()
{
CPUEnabled = true,
GPUEnabled = true,
RAMEnabled = true,
HDDEnabled = true,
};
try
{
computer.Open();
}
catch (ArgumentOutOfRangeException ex)
{
}
catch (Exception ex)
{
}
}
}
public float? CPU_load { get; set; }
public float? GPU_load { get; set; }
public float? HDD_load { get; set; }
public float? RAM_usage { get; set; }
public float? CPU_temp { get; set; }
public float? GPU_temp { get; set; }
public int ActiveThreads { get; set; }
public int WorkerThreads { get; set; }
public int CompletionPortThreads { get; set; }
public int MaxWorkerThreads { get; set; }
public int MaxCompletionPortThreads { get; set; }
public string PerformanceStr
{
get
{
return FuncoesGlobais.MontarStringPerformance(
GPU_load,
CPU_load,
RAM_usage,
GPU_temp,
CPU_temp
);
}
}
DateTime UltimaLeituraPerformance = DateTime.Now;
public void AtualizarDadosPerformance()
{
if (computer == null || computer.Hardware == null)
{
return;
}
if (DateTime.Now > UltimaLeituraPerformance.AddSeconds(1))
{
UltimaLeituraPerformance = DateTime.Now;
float? cpuLoad = null;
float? gpuLoad = null;
float? hddLoad = null;
float? ramUsage = null;
float? cpuTemp = null;
float? gpuTemp = null;
foreach (var hardwareItem in computer.Hardware)
{
hardwareItem.Update();
if (hardwareItem.HardwareType == HardwareType.CPU)
{
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Load && sensor.Name == "CPU Total")
{
cpuLoad = sensor.Value;
}
if (sensor.SensorType == SensorType.Temperature && sensor.Name == "CPU Package")
{
cpuTemp = sensor.Value;
}
}
}
if (hardwareItem.HardwareType == HardwareType.GpuNvidia || hardwareItem.HardwareType == HardwareType.GpuAti)
{
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Load && sensor.Name == "GPU Core")
{
gpuLoad = sensor.Value;
}
if (sensor.SensorType == SensorType.Temperature && sensor.Name == "GPU Core")
{
gpuTemp = sensor.Value;
}
}
}
if (hardwareItem.HardwareType == HardwareType.RAM)
{
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Load && sensor.Name == "Memory")
{
ramUsage = sensor.Value;
}
}
}
if (hardwareItem.HardwareType == HardwareType.HDD)
{
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Load && sensor.Name == "Used Space")
{
hddLoad = sensor.Value;
}
}
}
}
int activeThreads = Process.GetCurrentProcess().Threads.Count;
ThreadPool.GetAvailableThreads(out int workerThreads, out int completionPortThreads);
ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxCompletionPortThreads);
CPU_load = cpuLoad;
GPU_load = gpuLoad;
HDD_load = hddLoad;
RAM_usage = ramUsage;
CPU_temp = cpuTemp;
GPU_temp = gpuTemp;
ActiveThreads = activeThreads;
WorkerThreads = workerThreads;
MaxWorkerThreads = maxWorkerThreads;
CompletionPortThreads = completionPortThreads;
MaxCompletionPortThreads = maxCompletionPortThreads;
}
}
public PerformanceModel Clone()
{
return new PerformanceModel()
{
ActiveThreads = ActiveThreads,
CompletionPortThreads = CompletionPortThreads,
MaxWorkerThreads = MaxWorkerThreads,
MaxCompletionPortThreads = MaxCompletionPortThreads,
CPU_load = CPU_load,
GPU_load = GPU_load,
RAM_usage = RAM_usage,
CPU_temp = CPU_temp,
GPU_temp = GPU_temp,
HDD_load = HDD_load,
WorkerThreads = WorkerThreads
};
}
}
}