Hardware/Nvidia/NvidiaGPU.cs
author moel.mich
Sun, 14 Feb 2010 21:09:08 +0000
changeset 45 eadad41d21a6
parent 1 361e324a0ed4
child 56 5cb7eb5bf628
permissions -rw-r--r--
Allow all protocols 2X for T-Balancer.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Drawing;
    41 
    42 namespace OpenHardwareMonitor.Hardware.Nvidia {
    43   public class NvidiaGPU : Hardware, IHardware {
    44 
    45     private string name;
    46     private Image icon;
    47     private int adapterIndex;
    48     private NvPhysicalGpuHandle handle;
    49 
    50     private Sensor[] temperatures;
    51     private Sensor fan = null;
    52 
    53     private bool available;
    54 
    55     public NvidiaGPU(int adapterIndex, NvPhysicalGpuHandle handle) {
    56       try {
    57         string gpuName;
    58         if (NVAPI.NvAPI_GPU_GetFullName(handle, out gpuName) == NvStatus.OK) {
    59           this.name = "NVIDIA " + gpuName.Trim();
    60         } else {
    61           this.name = "NVIDIA";
    62         }
    63         this.icon = Utilities.EmbeddedResources.GetImage("nvidia.png");
    64         this.adapterIndex = adapterIndex;
    65         this.handle = handle;
    66 
    67         NvGPUThermalSettings settings = GetThermalSettings();
    68         temperatures = new Sensor[settings.Count];
    69         for (int i = 0; i < temperatures.Length; i++) {
    70           NvSensor sensor = settings.Sensor[i];
    71           string name;
    72           switch (sensor.Target) {
    73             case NvThermalTarget.BOARD: name = "GPU Board"; break;
    74             case NvThermalTarget.GPU: name = "GPU Core"; break;
    75             case NvThermalTarget.MEMORY: name = "GPU Memory"; break;
    76             case NvThermalTarget.POWER_SUPPLY: name = "GPU Power Supply"; break;
    77             case NvThermalTarget.UNKNOWN: name = "GPU Unknown"; break;
    78             default: name = "GPU"; break;
    79           }
    80           temperatures[i] = new Sensor(name, i, sensor.DefaultMaxTemp, 
    81             SensorType.Temperature, this);
    82           ActivateSensor(temperatures[i]);
    83         }
    84         
    85         int value;
    86         if (NVAPI.NvAPI_GPU_GetTachReading != null &&
    87           NVAPI.NvAPI_GPU_GetTachReading(handle, out value) == NvStatus.OK) {
    88           if (value > 0) {
    89             fan = new Sensor("GPU", 0, SensorType.Fan, this);
    90             ActivateSensor(fan);
    91           }
    92         }
    93 
    94         available = temperatures.Length > 0 || fan != null;
    95       } catch (Exception e) {
    96         System.Windows.Forms.MessageBox.Show(e.Message + "\n" + e.StackTrace);
    97       }
    98     }
    99 
   100     public bool IsAvailable {
   101       get { return available; }
   102     }
   103 
   104     public string Name {
   105       get { return name; }
   106     }
   107 
   108     public string Identifier {
   109       get { return "/nvidiagpu/" + adapterIndex; }
   110     }
   111 
   112     public Image Icon {
   113       get { return icon; }
   114     }
   115 
   116     public string GetReport() {
   117       return null;
   118     }
   119 
   120     private NvGPUThermalSettings GetThermalSettings() {
   121       NvGPUThermalSettings settings = new NvGPUThermalSettings();
   122       settings.Version = NVAPI.GPU_THERMAL_SETTINGS_VER;
   123       settings.Count = NVAPI.MAX_THERMAL_SENSORS_PER_GPU;
   124       settings.Sensor = new NvSensor[NVAPI.MAX_THERMAL_SENSORS_PER_GPU];
   125       if (NVAPI.NvAPI_GPU_GetThermalSettings(handle, (int)NvThermalTarget.ALL,
   126         ref settings) != NvStatus.OK) {
   127         settings.Count = 0;        
   128       }
   129       return settings;
   130     }
   131 
   132     public void Update() {
   133       NvGPUThermalSettings settings = GetThermalSettings();
   134       foreach (Sensor sensor in temperatures) 
   135         sensor.Value = settings.Sensor[sensor.Index].CurrentTemp;
   136 
   137       if (fan != null) {
   138         int value = 0;
   139         NVAPI.NvAPI_GPU_GetTachReading(handle, out value);
   140         fan.Value = value;
   141       }
   142     }
   143   }
   144 }