1.1 --- a/Hardware/Nvidia/NvidiaGPU.cs Thu Jul 07 20:41:09 2011 +0000
1.2 +++ b/Hardware/Nvidia/NvidiaGPU.cs Thu Jul 07 21:53:09 2011 +0000
1.3 @@ -20,6 +20,7 @@
1.4 the Initial Developer. All Rights Reserved.
1.5
1.6 Contributor(s):
1.7 + Christian Vallières
1.8
1.9 Alternatively, the contents of this file may be used under the terms of
1.10 either the GNU General Public License Version 2 or later (the "GPL"), or
1.11 @@ -52,12 +53,15 @@
1.12 private readonly Sensor[] loads;
1.13 private readonly Sensor control;
1.14 private readonly Sensor memoryLoad;
1.15 + private readonly Control fanControl;
1.16
1.17 - public NvidiaGPU(int adapterIndex, NvPhysicalGpuHandle handle,
1.18 - NvDisplayHandle? displayHandle, ISettings settings)
1.19 - : base(GetName(handle), new Identifier("nvidiagpu",
1.20 - adapterIndex.ToString(CultureInfo.InvariantCulture)), settings)
1.21 - {
1.22 + private bool restoreDefaultFanSpeedRequired;
1.23 + private NvLevel initialFanSpeedValue;
1.24 +
1.25 + public NvidiaGPU(int adapterIndex, NvPhysicalGpuHandle handle,
1.26 + NvDisplayHandle? displayHandle, ISettings settings)
1.27 + : base(GetName(handle), new Identifier("nvidiagpu",
1.28 + adapterIndex.ToString(CultureInfo.InvariantCulture)), settings) {
1.29 this.adapterIndex = adapterIndex;
1.30 this.handle = handle;
1.31 this.displayHandle = displayHandle;
1.32 @@ -103,6 +107,18 @@
1.33 memoryLoad = new Sensor("GPU Memory", 3, SensorType.Load, this, settings);
1.34
1.35 control = new Sensor("GPU Fan", 0, SensorType.Control, this, settings);
1.36 +
1.37 + NvGPUCoolerSettings coolerSettings = GetCoolerSettings();
1.38 + if (coolerSettings.Count > 0) {
1.39 + fanControl = new Control(control, settings,
1.40 + coolerSettings.Cooler[0].DefaultMin,
1.41 + coolerSettings.Cooler[0].DefaultMax);
1.42 + fanControl.ControlModeChanged += ControlModeChanged;
1.43 + fanControl.SoftwareControlValueChanged += SoftwareControlValueChanged;
1.44 + ControlModeChanged(fanControl);
1.45 + control.Control = fanControl;
1.46 + }
1.47 + Update();
1.48 }
1.49
1.50 private static string GetName(NvPhysicalGpuHandle handle) {
1.51 @@ -123,12 +139,26 @@
1.52 settings.Version = NVAPI.GPU_THERMAL_SETTINGS_VER;
1.53 settings.Count = NVAPI.MAX_THERMAL_SENSORS_PER_GPU;
1.54 settings.Sensor = new NvSensor[NVAPI.MAX_THERMAL_SENSORS_PER_GPU];
1.55 - if (NVAPI.NvAPI_GPU_GetThermalSettings != null &&
1.56 + if (!(NVAPI.NvAPI_GPU_GetThermalSettings != null &&
1.57 NVAPI.NvAPI_GPU_GetThermalSettings(handle, (int)NvThermalTarget.ALL,
1.58 - ref settings) != NvStatus.OK) {
1.59 - settings.Count = 0;
1.60 + ref settings) == NvStatus.OK))
1.61 + {
1.62 + settings.Count = 0;
1.63 + }
1.64 + return settings;
1.65 + }
1.66 +
1.67 + private NvGPUCoolerSettings GetCoolerSettings() {
1.68 + NvGPUCoolerSettings settings = new NvGPUCoolerSettings();
1.69 + settings.Version = NVAPI.GPU_COOLER_SETTINGS_VER;
1.70 + settings.Cooler = new NvCooler[NVAPI.MAX_COOLER_PER_GPU];
1.71 + if (!(NVAPI.NvAPI_GPU_GetCoolerSettings != null &&
1.72 + NVAPI.NvAPI_GPU_GetCoolerSettings(handle, 0,
1.73 + ref settings) == NvStatus.OK))
1.74 + {
1.75 + settings.Count = 0;
1.76 }
1.77 - return settings;
1.78 + return settings;
1.79 }
1.80
1.81 private uint[] GetClocks() {
1.82 @@ -144,7 +174,7 @@
1.83
1.84 public override void Update() {
1.85 NvGPUThermalSettings settings = GetThermalSettings();
1.86 - foreach (Sensor sensor in temperatures)
1.87 + foreach (Sensor sensor in temperatures)
1.88 sensor.Value = settings.Sensor[sensor.Index].CurrentTemp;
1.89
1.90 if (fan != null) {
1.91 @@ -167,7 +197,7 @@
1.92 NvPStates states = new NvPStates();
1.93 states.Version = NVAPI.GPU_PSTATES_VER;
1.94 states.PStates = new NvPState[NVAPI.MAX_PSTATES_PER_GPU];
1.95 - if (NVAPI.NvAPI_GPU_GetPStates != null &&
1.96 + if (NVAPI.NvAPI_GPU_GetPStates != null &&
1.97 NVAPI.NvAPI_GPU_GetPStates(handle, ref states) == NvStatus.OK) {
1.98 for (int i = 0; i < 3; i++)
1.99 if (states.PStates[i].Present) {
1.100 @@ -188,12 +218,9 @@
1.101 }
1.102 }
1.103
1.104 - NvGPUCoolerSettings coolerSettings = new NvGPUCoolerSettings();
1.105 - coolerSettings.Version = NVAPI.GPU_COOLER_SETTINGS_VER;
1.106 - coolerSettings.Cooler = new NvCooler[NVAPI.MAX_COOLER_PER_GPU];
1.107 - if (NVAPI.NvAPI_GPU_GetCoolerSettings != null &&
1.108 - NVAPI.NvAPI_GPU_GetCoolerSettings(handle, 0, ref coolerSettings) ==
1.109 - NvStatus.OK && coolerSettings.Count > 0) {
1.110 +
1.111 + NvGPUCoolerSettings coolerSettings = GetCoolerSettings();
1.112 + if (coolerSettings.Count > 0) {
1.113 control.Value = coolerSettings.Cooler[0].CurrentLevel;
1.114 ActivateSensor(control);
1.115 }
1.116 @@ -202,9 +229,8 @@
1.117 memoryInfo.Version = NVAPI.GPU_MEMORY_INFO_VER;
1.118 memoryInfo.Values = new uint[NVAPI.MAX_MEMORY_VALUES_PER_GPU];
1.119 if (NVAPI.NvAPI_GPU_GetMemoryInfo != null && displayHandle.HasValue &&
1.120 - NVAPI.NvAPI_GPU_GetMemoryInfo(displayHandle.Value, ref memoryInfo) ==
1.121 - NvStatus.OK)
1.122 - {
1.123 + NVAPI.NvAPI_GPU_GetMemoryInfo(displayHandle.Value, ref memoryInfo) ==
1.124 + NvStatus.OK) {
1.125 uint totalMemory = memoryInfo.Values[0];
1.126 uint freeMemory = memoryInfo.Values[4];
1.127 float usedMemory = Math.Max(totalMemory - freeMemory, 0);
1.128 @@ -221,9 +247,8 @@
1.129
1.130 r.AppendFormat("Name: {0}{1}", name, Environment.NewLine);
1.131 r.AppendFormat("Index: {0}{1}", adapterIndex, Environment.NewLine);
1.132 -
1.133 - if (displayHandle.HasValue && NVAPI.NvAPI_GetDisplayDriverVersion != null)
1.134 - {
1.135 +
1.136 + if (displayHandle.HasValue && NVAPI.NvAPI_GetDisplayDriverVersion != null) {
1.137 NvDisplayDriverVersion driverVersion = new NvDisplayDriverVersion();
1.138 driverVersion.Version = NVAPI.DISPLAY_DRIVER_VERSION_VER;
1.139 if (NVAPI.NvAPI_GetDisplayDriverVersion(displayHandle.Value,
1.140 @@ -231,7 +256,7 @@
1.141 r.Append("Driver Version: ");
1.142 r.Append(driverVersion.DriverVersion / 100);
1.143 r.Append(".");
1.144 - r.Append((driverVersion.DriverVersion % 100).ToString("00",
1.145 + r.Append((driverVersion.DriverVersion % 100).ToString("00",
1.146 CultureInfo.InvariantCulture));
1.147 r.AppendLine();
1.148 r.Append("Driver Branch: ");
1.149 @@ -269,7 +294,7 @@
1.150 r.AppendLine(status.ToString());
1.151 }
1.152 r.AppendLine();
1.153 - }
1.154 + }
1.155
1.156 if (NVAPI.NvAPI_GPU_GetAllClocks != null) {
1.157 NvClocks allClocks = new NvClocks();
1.158 @@ -290,10 +315,10 @@
1.159 r.AppendLine(status.ToString());
1.160 }
1.161 r.AppendLine();
1.162 - }
1.163 -
1.164 + }
1.165 +
1.166 if (NVAPI.NvAPI_GPU_GetTachReading != null) {
1.167 - int tachValue;
1.168 + int tachValue;
1.169 NvStatus status = NVAPI.NvAPI_GPU_GetTachReading(handle, out tachValue);
1.170
1.171 r.AppendLine("Tachometer");
1.172 @@ -332,7 +357,7 @@
1.173 usages.Version = NVAPI.GPU_USAGES_VER;
1.174 usages.Usage = new uint[NVAPI.MAX_USAGES_PER_GPU];
1.175 NvStatus status = NVAPI.NvAPI_GPU_GetUsages(handle, ref usages);
1.176 -
1.177 +
1.178 r.AppendLine("Usages");
1.179 r.AppendLine();
1.180 if (status == NvStatus.OK) {
1.181 @@ -394,7 +419,7 @@
1.182 NvMemoryInfo memoryInfo = new NvMemoryInfo();
1.183 memoryInfo.Version = NVAPI.GPU_MEMORY_INFO_VER;
1.184 memoryInfo.Values = new uint[NVAPI.MAX_MEMORY_VALUES_PER_GPU];
1.185 - NvStatus status = NVAPI.NvAPI_GPU_GetMemoryInfo(displayHandle.Value,
1.186 + NvStatus status = NVAPI.NvAPI_GPU_GetMemoryInfo(displayHandle.Value,
1.187 ref memoryInfo);
1.188
1.189 r.AppendLine("Memory Info");
1.190 @@ -412,5 +437,45 @@
1.191
1.192 return r.ToString();
1.193 }
1.194 +
1.195 + private void SoftwareControlValueChanged(IControl control) {
1.196 + SaveDefaultFanSpeed();
1.197 + NvGPUCoolerLevels coolerLevels = new NvGPUCoolerLevels();
1.198 + coolerLevels.Version = NVAPI.GPU_COOLER_LEVELS_VER;
1.199 + coolerLevels.Levels = new NvLevel[NVAPI.MAX_COOLER_PER_GPU];
1.200 + coolerLevels.Levels[0].Level = (int)control.SoftwareValue;
1.201 + coolerLevels.Levels[0].Policy = 1;
1.202 + NVAPI.NvAPI_GPU_SetCoolerLevels(handle, 0, ref coolerLevels);
1.203 + }
1.204 +
1.205 + private void SaveDefaultFanSpeed() {
1.206 + if (!restoreDefaultFanSpeedRequired) {
1.207 + NvGPUCoolerSettings coolerSettings = GetCoolerSettings();
1.208 + if (coolerSettings.Count > 0) {
1.209 + restoreDefaultFanSpeedRequired = true;
1.210 + initialFanSpeedValue.Level = coolerSettings.Cooler[0].CurrentLevel;
1.211 + initialFanSpeedValue.Policy = coolerSettings.Cooler[0].CurrentPolicy;
1.212 + }
1.213 + }
1.214 + }
1.215 +
1.216 + private void ControlModeChanged(IControl control) {
1.217 + if (control.ControlMode == ControlMode.Default) {
1.218 + RestoreDefaultFanSpeed();
1.219 + } else {
1.220 + SoftwareControlValueChanged(control);
1.221 + }
1.222 + }
1.223 +
1.224 + private void RestoreDefaultFanSpeed() {
1.225 + if (restoreDefaultFanSpeedRequired) {
1.226 + NvGPUCoolerLevels coolerLevels = new NvGPUCoolerLevels();
1.227 + coolerLevels.Version = NVAPI.GPU_COOLER_LEVELS_VER;
1.228 + coolerLevels.Levels = new NvLevel[NVAPI.MAX_COOLER_PER_GPU];
1.229 + coolerLevels.Levels[0] = initialFanSpeedValue;
1.230 + NVAPI.NvAPI_GPU_SetCoolerLevels(handle, 0, ref coolerLevels);
1.231 + restoreDefaultFanSpeedRequired = false;
1.232 + }
1.233 + }
1.234 }
1.235 }