예제 #1
0
        public override void Update()
        {
            if (temperatures.Any(sensor => sensorConfig.GetSensorEvaluate(sensor.IdentifierString)))
            {
                NvGPUThermalSettings settings = GetThermalSettings();
                foreach (Sensor sensor in temperatures)
                {
                    sensor.Value = settings.Sensor[sensor.Index].CurrentTemp;
                }
            }
            else
            {
                foreach (Sensor sensor in temperatures)
                {
                    sensor.Value = null;
                }
            }

            bool tachReadingOk = false;

            if (sensorConfig.GetSensorEvaluate(fan.IdentifierString))
            {
                if (NVAPI.NvAPI_GPU_GetTachReading != null &&
                    NVAPI.NvAPI_GPU_GetTachReading(handle, out int fanValue) == NvStatus.OK)
                {
                    fan.Value = fanValue;
                    ActivateSensor(fan);
                    tachReadingOk = true;
                }
            }
            else
            {
                fan.Value = null;
            }

            if (clocks.Any(sensor => sensorConfig.GetSensorEvaluate(sensor.IdentifierString)))
            {
                uint[] values = GetClocks();
                if (values != null)
                {
                    clocks[1].Value = 0.001f * values[8];
                    if (values[30] != 0)
                    {
                        clocks[0].Value = 0.0005f * values[30];
                        clocks[2].Value = 0.001f * values[30];
                    }
                    else
                    {
                        clocks[0].Value = 0.001f * values[0];
                        clocks[2].Value = 0.001f * values[14];
                    }
                }
            }
            else
            {
                for (int i = 0; i < clocks.Length; i++)
                {
                    clocks[i].Value = null;
                }
            }

            if (sensorConfig.GetSensorEvaluate(voltage.IdentifierString))
            {
                var gpuVoltageStatus = new NvGpuVoltageStatus
                {
                    Version  = NVAPI.GPU_VOLTAGE_STATUS_VER,
                    Unknown2 = new uint[8],
                    Unknown3 = new uint[8]
                };

                if (NVAPI.NvAPI_GPU_GetCurrentVoltage != null &&
                    NVAPI.NvAPI_GPU_GetCurrentVoltage(handle, ref gpuVoltageStatus) == NvStatus.OK)
                {
                    voltage.Value = gpuVoltageStatus.ValueInuV / 1E06f;
                    ActivateSensor(voltage);
                }
            }
            else
            {
                voltage.Value = null;
            }

            if (loads.Any(sensor => sensorConfig.GetSensorEvaluate(sensor.IdentifierString)))
            {
                var infoEx = new NvDynamicPstatesInfoEx
                {
                    Version            = NVAPI.GPU_DYNAMIC_PSTATES_INFO_EX_VER,
                    UtilizationDomains =
                        new NvUtilizationDomainEx[NVAPI.NVAPI_MAX_GPU_UTILIZATIONS]
                };

                if (NVAPI.NvAPI_GPU_GetDynamicPstatesInfoEx != null &&
                    NVAPI.NvAPI_GPU_GetDynamicPstatesInfoEx(handle, ref infoEx) == NvStatus.OK)
                {
                    for (int i = 0; i < loads.Length; i++)
                    {
                        if (infoEx.UtilizationDomains[i].Present)
                        {
                            loads[i].Value = infoEx.UtilizationDomains[i].Percentage;
                            ActivateSensor(loads[i]);
                        }
                    }
                }
                else
                {
                    var info = new NvDynamicPstatesInfo
                    {
                        Version            = NVAPI.GPU_DYNAMIC_PSTATES_INFO_VER,
                        UtilizationDomains =
                            new NvUtilizationDomain[NVAPI.NVAPI_MAX_GPU_UTILIZATIONS]
                    };

                    if (NVAPI.NvAPI_GPU_GetDynamicPstatesInfo != null &&
                        NVAPI.NvAPI_GPU_GetDynamicPstatesInfo(handle, ref info) == NvStatus.OK)
                    {
                        for (int i = 0; i < loads.Length; i++)
                        {
                            if (info.UtilizationDomains[i].Present)
                            {
                                loads[i].Value = info.UtilizationDomains[i].Percentage;
                                ActivateSensor(loads[i]);
                            }
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < loads.Length; i++)
                {
                    loads[i].Value = null;
                }
            }

            if (sensorConfig.GetSensorEvaluate(control.IdentifierString) ||
                sensorConfig.GetSensorEvaluate(fan.IdentifierString))
            {
                var coolerSettings   = GetCoolerSettings();
                var coolerSettingsOk = false;
                if (coolerSettings.Count > 0)
                {
                    control.Value = coolerSettings.Cooler[0].CurrentLevel;
                    ActivateSensor(control);
                    coolerSettingsOk = true;
                }

                if (!tachReadingOk || !coolerSettingsOk)
                {
                    var coolersStatus = GetFanCoolersStatus();
                    if (coolersStatus.Count > 0)
                    {
                        if (!coolerSettingsOk)
                        {
                            control.Value = coolersStatus.Items[0].CurrentLevel;
                            ActivateSensor(control);
                            coolerSettingsOk = true;
                        }
                        if (!tachReadingOk)
                        {
                            fan.Value = coolersStatus.Items[0].CurrentRpm;
                            ActivateSensor(fan);
                            tachReadingOk = true;
                        }
                    }
                }
            }
            else
            {
                control.Value = null;
                fan.Value     = null;
            }

            if (!(!sensorConfig.GetSensorEvaluate(memoryAvail.IdentifierString) &&
                  !sensorConfig.GetSensorEvaluate(memoryUsed.IdentifierString) &&
                  !sensorConfig.GetSensorEvaluate(memoryFree.IdentifierString) &&
                  !sensorConfig.GetSensorEvaluate(memoryLoad.IdentifierString)))
            {
                NvDisplayDriverMemoryInfo memoryInfo = new NvDisplayDriverMemoryInfo
                {
                    Version = NVAPI.DISPLAY_DRIVER_MEMORY_INFO_VER,
                    Values  = new uint[NVAPI.MAX_MEMORY_VALUES_PER_GPU]
                };

                if (NVAPI.NvAPI_GetDisplayDriverMemoryInfo != null && displayHandle.HasValue &&
                    NVAPI.NvAPI_GetDisplayDriverMemoryInfo(displayHandle.Value, ref memoryInfo) ==
                    NvStatus.OK)
                {
                    uint  totalMemory = memoryInfo.Values[0];
                    uint  freeMemory  = memoryInfo.Values[4];
                    float usedMemory  = Math.Max(totalMemory - freeMemory, 0);
                    memoryFree.Value  = (float)freeMemory / 1024 / 1024;
                    memoryAvail.Value = (float)totalMemory / 1024 / 1024;
                    memoryUsed.Value  = usedMemory / 1024 / 1024;
                    memoryLoad.Value  = 100f * usedMemory / totalMemory;
                    ActivateSensor(memoryAvail);
                    ActivateSensor(memoryUsed);
                    ActivateSensor(memoryFree);
                    ActivateSensor(memoryLoad);
                }
            }
            else
            {
                memoryAvail.Value = null;
                memoryUsed.Value  = null;
                memoryFree.Value  = null;
                memoryLoad.Value  = null;
            }

            if (power != null)
            {
                if (sensorConfig.GetSensorEvaluate(power.IdentifierString))
                {
                    var channels = new NvGpuPowerMonitorPowerChannelStatus[NVAPI.POWER_STATUS_CHANNEL_COUNT];
                    for (int i = 0; i < channels.Length; i++)
                    {
                        channels[i].Rsvd = new byte[NVAPI.POWER_STATUS_RSVD_SIZE];
                    }

                    var powerStatus = new NvGpuPowerStatus
                    {
                        Version  = NVAPI.GPU_POWER_MONITOR_STATUS_VER,
                        Rsvd     = new byte[NVAPI.POWER_STATUS_RSVD_SIZE],
                        Channels = channels
                    };

                    if (NVAPI.NvAPI_GPU_PowerMonitorGetStatus != null &&
                        NVAPI.NvAPI_GPU_PowerMonitorGetStatus(handle, ref powerStatus) == NvStatus.OK)
                    {
                        power.Value = powerStatus.TotalGpuPowermW * 1E-03f;
                        ActivateSensor(power);
                    }
                }
                else
                {
                    power.Value = null;
                }
            }

            // update VRAM usage
            if (dedicatedVramUsagePerformCounter != null)
            {
                try
                {
                    if (sensorConfig.GetSensorEvaluate(memoryUsageDedicated.IdentifierString))
                    {
                        memoryUsageDedicated.Value = dedicatedVramUsagePerformCounter.NextValue() / SCALE;
                        ActivateSensor(memoryUsageDedicated);
                    }
                    else
                    {
                        memoryUsageDedicated.Value = null;
                    }
                }
                catch { memoryUsageDedicated.Value = null; }
            }

            if (sharedVramUsagePerformCounter != null)
            {
                try
                {
                    if (sensorConfig.GetSensorEvaluate(memoryUsageShared.IdentifierString))
                    {
                        memoryUsageShared.Value = (float)sharedVramUsagePerformCounter.NextValue() / SCALE;
                        ActivateSensor(memoryUsageShared);
                    }
                    else
                    {
                        memoryUsageShared.Value = null;
                    }
                }
                catch { memoryUsageShared.Value = null; }
            }

            try
            {
                if (sensorConfig.GetSensorEvaluate(processMemoryUsageDedicated.IdentifierString))
                {
                    lock (_performanceCounterLock)
                    {
                        processMemoryUsageDedicated.Value = dedicatedVramUsageProcessPerformCounter == null
                        ? 0f : (float)dedicatedVramUsageProcessPerformCounter.NextValue() / SCALE;
                    }
                    ActivateSensor(processMemoryUsageDedicated);
                }
                else
                {
                    processMemoryUsageDedicated.Value = null;
                }
            }
            catch { processMemoryUsageDedicated.Value = null; }

            try
            {
                if (sensorConfig.GetSensorEvaluate(processMemoryUsageShared.IdentifierString))
                {
                    lock (_performanceCounterLock)
                    {
                        processMemoryUsageShared.Value = sharedVramUsageProcessPerformCounter == null
                        ? 0f : (float)sharedVramUsageProcessPerformCounter.NextValue() / SCALE;
                    }
                    ActivateSensor(processMemoryUsageShared);
                }
                else
                {
                    processMemoryUsageShared.Value = null;
                }
            }
            catch { processMemoryUsageShared.Value = null; }

            if (pcieThroughputRx != null)
            {
                if (sensorConfig.GetSensorEvaluate(pcieThroughputRx.IdentifierString))
                {
                    if (NVML.NvmlDeviceGetPcieThroughput(device.Value,
                                                         NVML.NvmlPcieUtilCounter.RxBytes, out uint value)
                        == NVML.NvmlReturn.Success)
                    {
                        pcieThroughputRx.Value = value / 1024f;
                        ActivateSensor(pcieThroughputRx);
                    }
                }
                else
                {
                    pcieThroughputRx.Value = null;
                }
            }

            if (pcieThroughputTx != null)
            {
                if (sensorConfig.GetSensorEvaluate(pcieThroughputTx.IdentifierString))
                {
                    if (NVML.NvmlDeviceGetPcieThroughput(device.Value,
                                                         NVML.NvmlPcieUtilCounter.TxBytes, out uint value)
                        == NVML.NvmlReturn.Success)
                    {
                        pcieThroughputTx.Value = value / 1024f;
                        ActivateSensor(pcieThroughputTx);
                    }
                }
                else
                {
                    pcieThroughputTx.Value = null;
                }
            }

            if (sensorConfig.GetSensorEvaluate(monitorRefreshRate.IdentifierString))
            {
                if (NVAPI.NvAPI_GetVBlankCounter(displayHandle.Value, out uint pCounter)
                    == NvStatus.OK)
                {
                    var deltaTicks = stopwatch.ElapsedTicks;
                    stopwatch.Restart();

                    lock (_displayLock)
                    {
                        var currentRefreshRate = (float)(pCounter - lastpCounter) / deltaTicks * Stopwatch.Frequency;
                        refreshRateBuffer.Add(currentRefreshRate);
                        var refreshRateFiltered = (float)Math.Ceiling(refreshRateBuffer.RefreshRates.Average());
                        monitorRefreshRate.Value = refreshRateFiltered > refreshRateCurrentWindowHandle ? refreshRateCurrentWindowHandle : refreshRateFiltered;
                    }

                    lastpCounter = pCounter;
                    ActivateSensor(monitorRefreshRate);
                }
            }
            else
            {
                monitorRefreshRate.Value = null;
            }
        }