【问题标题】:Grabbing GPU Utilization using openhardwaremonitor使用 openhardwaremonitor 获取 GPU 利用率
【发布时间】:2020-06-19 05:19:00
【问题描述】:

我正在尝试使用 openhardwaremonitor 获取我的 GPU 的当前利用率 我使用 SensorType.Load 来获取 CPU 的利用率,但对于 GPU,它返回的是内存使用情况。我不确定该怎么做

if (hardware.HardwareType == HardwareType.GpuNvidia)
{
    hardware.Update();
    foreach (var sensors in hardware.Sensors)
    {
        if (sensors.SensorType == SensorType.Load)
        {
            tester.Text = sensors.Name + ": " + sensors.Value.GetValueOrDefault();
            int gpuLoadInt = Convert.ToInt32(sensors.Value.GetValueOrDefault());
            string gpuLoadString = Convert.ToString(Decimal.Round(gpuLoadInt));
            gpuLoadGuage.Value = gpuLoadInt;
            gpuLoadLabel.Text = gpuLoadString + "%";
        }
    }
}

【问题讨论】:

  • OpenHardwareMonitor 的可用文档在 IMO 中很糟糕。我能找到的只是一个显然是在 2010 年创建的 PDF。但是你能尝试使用 SensorType.Level 吗?显然它与加载不同
  • 你说得对,除了源代码之外,文档几乎不存在。遗憾的是,使用 .level 不会返回任何信息
  • 您可以使用当前时钟和最大时钟将某些东西放在一起。传感器具有Max 的属性,但这只是 GPU 在监控期间达到的最大频率,而不是理论最大值。您也可以使用它并将其保存到磁盘并始终使用最高的数字。这将使您计算的Load 百分比在开始时不太准确,但随着用户推动他们的 GPU,随着时间的推移越来越准确

标签: c# openhardwaremonitor


【解决方案1】:

这个库有点棘手,但是一旦你对它有所了解,剩下的就会一点一点地出来。我做了这样的事情......

    using OpenHardwareMonitor.Hardware; // Don't forget this
    public partial class MainWindow : Window, IVisitor // Don't forget to include the interface
    {    
      // we need to call the method every X seconds to get data
      readonly System.Windows.Threading.DispatcherTimer dispatcherTimer = new 
      System.Windows.Threading.DispatcherTimer();
    }
    public MainWindow()
    {
      dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
      dispatcherTimer.Interval = new TimeSpan(0, 0, 2); // <= two seconds
    }

    public void VisitComputer(IComputer computer) => computer.Traverse(this);
    public void VisitHardware(IHardware hardware)
    {
     hardware.Update();
     foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
    }
    public void VisitSensor(ISensor sensor) { }
    public void VisitParameter(IParameter parameter) { }
    
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
      Thread ThreadGpu = new Thread(() => GetGpuInfo());
      ThreadGpu.Start();
    }

    GetGpuInfo()
    {
      Computer computer = new Computer();
      UpdateVisitor updateVisitor = new UpdateVisitor();
       try
       {
        computer.Open(); // You must initialize what you are going to use
        computer.GPUEnabled = true; // You must initialize what you are going to use
        computer.Accept(updateVisitor); // You NEED this line to update

        if (computer.Hardware.Length > 0)
        {
          foreach (var item in computer.Hardware)
          {
            foreach (ISensor gpu in item.Sensors)
            {
              if (gpu.Name == "GPU Memory" && gpu.Index == 1)
                Console.WriteLine("Memory:" + Math.Round((float)gpu.Value) + "Mhz");
              if (gpu.SensorType == SensorType.Temperature)
                Console.WriteLine("Temperature:" + Math.Round((float)gpu.Value) + "°C");
              if (gpu.SensorType == SensorType.Load && gpu.Name == "GPU Core")
                Console.WriteLine("GPU Core:" + gpu.Value);
              if (gpu.SensorType == SensorType.Clock && gpu.Name == "GPU Core")
                Console.WriteLine(gpu.Value + "Mhz");
            }
          }
        }
       }
       catch (Exception ex)
       {
         Console.WriteLine(ex.Message);
       }
       computer.Close(); // Close resources
     }

您也必须使用此 OpenHardwareMonitor 类,否则数据不会更新。您可以在同一个命名空间或另一个类文件中使用它

public class UpdateVisitor : IVisitor
  {
    public void VisitComputer(IComputer computer)
    {
      try
      {
        computer.Traverse(this);
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
      }
    }

    public void VisitHardware(IHardware hardware)
    {
      try
      {
        hardware.Update();
        foreach (IHardware subHardware in hardware.SubHardware)
          subHardware.Accept(this);
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
      }
    }

    public void VisitSensor(ISensor sensor) { }

    public void VisitParameter(IParameter parameter) { }
  }
}

我仍在学习 C#,但希望对您有所帮助

【讨论】:

  • 要重复获取传感器数据,在类级别创建 Computer 和 UpdateVisitor 对象会更好吗?我想知道它在性能方面是否更好
猜你喜欢
  • 2018-04-07
  • 2013-05-13
  • 2011-01-10
  • 2018-07-04
  • 2020-11-27
  • 2019-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多