Julian Ortega
Julian Ortega

Reputation: 1

Problems with the sensor of type Power for GPU in LibreHardwareMonitorLib

I am working with a C# service that uses the LibreHardwareMonitorLib library to gather hardware metrics, including GPU power consumption data. However, I have encountered an issue with the Power sensor corresponding to the GPU Package, which always returns a value of 0.0 in the service, even though the same code works fine in a console application.

In the console application, the Power sensor with the name GPU Package is detected and returns the correct power value. However, in the service, although the sensor is present (according to the logs), it does not enter the if condition that checks for this sensor and never retrieves a value other than 0.0.

The code used is the same in both applications (console and service), and all other sensors work correctly, making this behavior inexplicable. Specifically, the GPU power sensor is not functioning as expected in the service, whereas it works fine in the console application, where it retrieves the correct values.

Console application code and part of the console output:

using LibreHardwareMonitor.Hardware;
using System;

namespace prueba
{
    internal class Program
    {
        static void Main(string[] args)
        {
            float gpuLoad = 0.0f, gpuVramUsed = 0.0f, gpuVramTotal = 0.0f;
            float gpuClock = 0.0f, gpuPower = 0.0f, gpuTemp = 0.0f;

            Computer computer = new Computer
            {
                IsGpuEnabled = true
            };
            computer.Open();

            foreach (IHardware hardware in computer.Hardware)
            {
                if (hardware.HardwareType == HardwareType.GpuNvidia || hardware.HardwareType == HardwareType.GpuAmd)
                {
                    Console.WriteLine($"GPU detectada: {hardware.Name}");
                    Console.WriteLine("Sensores disponibles:");

                    hardware.Update(); // Actualiza los sensores antes de leerlos

                    foreach (ISensor sensor in hardware.Sensors)
                    {
                        Console.WriteLine($"- Nombre: {sensor.Name}, Tipo: {sensor.SensorType}, Valor: {sensor.Value}");

                        // Carga de la GPU
                        if (sensor.SensorType == SensorType.Load && sensor.Name.Contains("GPU Core"))
                        {
                            gpuLoad = sensor.Value ?? 0.0f;
                        }

                        // VRAM usada
                        if (sensor.SensorType == SensorType.SmallData && sensor.Name.Contains("GPU Memory Used"))
                        {
                            gpuVramUsed = sensor.Value ?? 0.0f;
                        }

                        // VRAM total
                        if (sensor.SensorType == SensorType.SmallData && sensor.Name.Contains("GPU Memory Total"))
                        {
                            gpuVramTotal = sensor.Value ?? 0.0f;
                        }

                        // Reloj de la GPU
                        if (sensor.SensorType == SensorType.Clock && sensor.Name.Contains("GPU Core"))
                        {
                            gpuClock = sensor.Value ?? 0.0f;
                        }

                        // Potencia de la GPU
                        if (sensor.SensorType == SensorType.Power && sensor.Name.Contains("GPU Package"))
                        {
                            gpuPower = sensor.Value ?? 0.0f;
                        }

                        // Temperatura de la GPU
                        if (sensor.SensorType == SensorType.Temperature && sensor.Name.Contains("GPU Core"))
                        {
                            gpuTemp = sensor.Value ?? 0.0f;
                        }
                    }

                    // Imprime los valores extraídos
                    Console.WriteLine("\nValores extraídos:");
                    Console.WriteLine($"- Carga de la GPU: {gpuLoad}%");
                    Console.WriteLine($"- VRAM Usada: {gpuVramUsed} MB");
                    Console.WriteLine($"- VRAM Total: {gpuVramTotal} MB");
                    Console.WriteLine($"- Reloj de la GPU: {gpuClock} MHz");
                    Console.WriteLine($"- Potencia de la GPU: {gpuPower} W");
                    Console.WriteLine($"- Temperatura de la GPU: {gpuTemp} °C");
                }
            }

            computer.Close();
            Console.WriteLine("\nProceso completado.");

            
        }
    }
}

Console:

Service code ->

 static (float temperature, float tdp, float gpuLoad, float gpuVramUsed, float gpuVramTotal, float gpuClock, float gpuPower, float gpuTemp) GetHardwareInfo(Computer computer)
 {
     float temperature = 0.0f;
     float tdp = 0.0f;
     float gpuLoad = 0.0f;
     float gpuVramUsed = 0.0f;
     float gpuVramTotal = 0.0f;
     float gpuClock = 0.0f;
     float gpuPower = 0.0f;
     float gpuTemp = 0.0f;

     foreach (IHardware hardware in computer.Hardware)
     {
         hardware.Update(); // Actualiza los sensores
         

         //sensores de CPU
         if (hardware.HardwareType == HardwareType.Cpu)
         {
             foreach (ISensor sensor in hardware.Sensors)
             {
                 Logger.Log($"Sensor detectado CPU: {sensor.Name} ({sensor.SensorType}) - Valor: {sensor.Value}\n");
                 // Buscamos el sensor de temperatura de la CPU
                 if (sensor.SensorType == SensorType.Temperature)
                 {
                     temperature = sensor.Value ?? 0.0f;

                 }

                 // Buscamos el sensor de TDP de la CPU
                 if (sensor.SensorType == SensorType.Power && sensor.Name.Contains("Package"))
                 {
                     tdp = sensor.Value ?? 0.0f;
                 }
             }
         }

         //sensores de GPU
         if (hardware.HardwareType == HardwareType.GpuNvidia || hardware.HardwareType == HardwareType.GpuAmd)
         {
             foreach (ISensor sensor in hardware.Sensors)
             {

                 Logger.Log($"Sensor detectado GPU: {sensor.Name} ({sensor.SensorType}) - Valor: {sensor.Value}\n");
                 // Carga de la GPU
                 if (sensor.SensorType == SensorType.Load && sensor.Name.Contains("GPU Core"))
                 {
                     gpuLoad = sensor.Value ?? 0.0f;
                 }

                 // VRAM usada
                 if (sensor.SensorType == SensorType.SmallData && sensor.Name.Contains("GPU Memory Used"))
                 {
                     gpuVramUsed = sensor.Value ?? 0.0f;
                 }

                 // VRAM total
                 if (sensor.SensorType == SensorType.SmallData && sensor.Name.Contains("GPU Memory Total"))
                 {
                     gpuVramTotal = sensor.Value ?? 0.0f;
                 }

                 // Reloj de la GPU
                 if (sensor.SensorType == SensorType.Clock && sensor.Name.Contains("GPU Core"))
                 {
                     gpuClock = sensor.Value ?? 0.0f;
                 }

                 // Potencia de la GPU
                 if (sensor.SensorType == SensorType.Power && sensor.Name.Equals("GPU Package"))
                 {
                     gpuPower = sensor.Value ?? 0.0f;

                   
                 }
                 
                 

                 // Temperatura de la GPU
                 if (sensor.SensorType == SensorType.Temperature && sensor.Name.Contains("GPU Core"))
                 {
                     gpuTemp = sensor.Value ?? 0.0f;
                     
                 }
             }
         }

     }
    
     return (temperature, tdp, gpuLoad, gpuVramUsed, gpuVramTotal, gpuClock, gpuPower, gpuTemp);
 }

Reading from my log file: 2025-02-07 11:57:27 - Enviando datos: 54|61,6|20|1833|12288|975,00|0,0|53

Let's see if anyone knows why this is happening and can help me. Thank you very much!

Upvotes: 0

Views: 27

Answers (0)

Related Questions