Author Topic: Read GPU Temp & Power for NVidia Cards  (Read 6118 times)

MasterFX

  • Newbie
  • *
  • Posts: 2
Read GPU Temp & Power for NVidia Cards
« on: March 31, 2019, 11:07:00 AM »
I have a small inline function in C# to read the GPU Temp and power for NVidia Cards using SMI Tool
Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Text.RegularExpressions;

public class VAInline
{
public void main()
{
VA.WriteToLog("GPU Info");
Process process = new Process();
process.StartInfo.FileName = "C:\\Program Files\\NVIDIA Corporation\\NVSMI\\nvidia-smi.exe";
process.StartInfo.Arguments = "dmon -c 1 -s p"; // monitor only once and power info
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
//* Read the output (or the error)
string output = process.StandardOutput.ReadToEnd();
//Console.WriteLine(output);
string err = process.StandardError.ReadToEnd();
//Console.WriteLine(err);
process.WaitForExit();
string[] numbers = Regex.Split(output, @"\D+");
VA.WriteToLog("Power: " + numbers[2] + " W");
VA.WriteToLog("Temp: " + numbers[3] + " °C");
VA.SetInt("GPUPwr", Convert.ToInt32(numbers[2]));
VA.SetInt("GPUTemp",  Convert.ToInt32(numbers[3]));
}
}
After that you just use Text to speech
Code: [Select]
Say, 'The GPU Temperature is {INT:GPUTemp} degree Celsius and the power consumption is {INT:GPUPwr} Watt

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Re: Read GPU Temp & Power for NVidia Cards
« Reply #1 on: April 03, 2019, 12:58:09 PM »
I think some additional qualifiers may be needed. I have a Nvidia Quadro M2200 on one of my PCs and using the provided code (only the inline function in a VA command) I get the following:



The exception is related to storing data in the VA variable 'GPUTemp'. Debuging in VS shows the contents of the string array 'numbers' as follows:



So at least for my particular test there is no data given to numbers[3] (GPU Temp). Back to my initial statement. Could additional steps related to Nvidia component installation be needed to get this working? I haven't dug into the SMI Tool documentation, but could the parameters or output scrubbing you're doing be specific to your system?


MasterFX

  • Newbie
  • *
  • Posts: 2
Re: Read GPU Temp & Power for NVidia Cards
« Reply #2 on: April 04, 2019, 02:00:51 PM »
Yes thats true. The SMI tool is made for different Graphics card configurations. For example the SMI output without "-s p" looks like this
Code: [Select]
# gpu   pwr gtemp mtemp    sm   mem   enc   dec  mclk  pclk
# Idx     W     C     C     %     %     %     %   MHz   MHz
    0    19    41     -    21    23    20    22   810   696
With -s p
Code: [Select]
# gpu   pwr gtemp mtemp
# Idx     W     C     C
    0    20    41     -
But I think this also depends on the graphics card itself. Maybe I'll try to make it more general by searching for "pwr", "gtemp" etc and only output valid values.

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Re: Read GPU Temp & Power for NVidia Cards
« Reply #3 on: April 05, 2019, 11:39:14 AM »
After further discussion with Pfeil it looks like the Quadro cards may not be supported for this tool, which was causing the issue for me. More information about Nvidia's SMI Tool can be found here.