示例#1
0
        public void Close()
        {
            foreach (Hardware gpu in _hardware)
            {
                gpu.Close();
            }

            NvidiaML.Close();
        }
示例#2
0
        public NvidiaGpu(int adapterIndex, NvApi.NvPhysicalGpuHandle handle, NvApi.NvDisplayHandle?displayHandle, ISettings settings)
            : base(GetName(handle), new Identifier("gpu", adapterIndex.ToString(CultureInfo.InvariantCulture)), settings)
        {
            _adapterIndex  = adapterIndex;
            _handle        = handle;
            _displayHandle = displayHandle;

            bool hasPciBusId = NvApi.NvAPI_GPU_GetBusId(handle, out uint busId) == NvApi.NvStatus.OK;

            NvApi.NvGPUThermalSettings thermalSettings = GetThermalSettings();
            _temperatures = new Sensor[thermalSettings.Count];
            for (int i = 0; i < _temperatures.Length; i++)
            {
                NvApi.NvSensor sensor = thermalSettings.Sensor[i];
                string         name;
                switch (sensor.Target)
                {
                case NvApi.NvThermalTarget.BOARD:
                    name = "GPU Board";
                    break;

                case NvApi.NvThermalTarget.GPU:
                    name = "GPU Core";
                    break;

                case NvApi.NvThermalTarget.MEMORY:
                    name = "GPU Memory";
                    break;

                case NvApi.NvThermalTarget.POWER_SUPPLY:
                    name = "GPU Power Supply";
                    break;

                case NvApi.NvThermalTarget.UNKNOWN:
                    name = "GPU Unknown";
                    break;

                default:
                    name = "GPU";
                    break;
                }

                _temperatures[i] = new Sensor(name, i, SensorType.Temperature, this, new ParameterDescription[0], settings);
                ActivateSensor(_temperatures[i]);
            }

            if (NvApi.NvAPI_GPU_GetTachReading != null && NvApi.NvAPI_GPU_GetTachReading(handle, out _) == NvApi.NvStatus.OK)
            {
                _fan = new Sensor("GPU", 0, SensorType.Fan, this, settings);
            }
            else if (NvApi.NvAPI_GPU_ClientFanCoolersGetStatus != null && GetCoolerSettings().Count > 0)
            {
                _fan = new Sensor("GPU", 0, SensorType.Fan, this, settings);
            }

            _clocks    = new Sensor[3];
            _clocks[0] = new Sensor("GPU Core", 0, SensorType.Clock, this, settings);
            _clocks[1] = new Sensor("GPU Memory", 1, SensorType.Clock, this, settings);
            _clocks[2] = new Sensor("GPU Shader", 2, SensorType.Clock, this, settings);
            for (int i = 0; i < _clocks.Length; i++)
            {
                ActivateSensor(_clocks[i]);
            }

            _loads    = new Sensor[4];
            _loads[0] = new Sensor("GPU Core", 0, SensorType.Load, this, settings);
            _loads[1] = new Sensor("GPU Memory Controller", 1, SensorType.Load, this, settings);
            _loads[2] = new Sensor("GPU Video Engine", 2, SensorType.Load, this, settings);
            _loads[3] = new Sensor("GPU Bus", 4, SensorType.Load, this, settings);

            _memoryLoad  = new Sensor("GPU Memory", 3, SensorType.Load, this, settings);
            _memoryFree  = new Sensor("GPU Memory Free", 1, SensorType.SmallData, this, settings);
            _memoryUsed  = new Sensor("GPU Memory Used", 2, SensorType.SmallData, this, settings);
            _memoryAvail = new Sensor("GPU Memory Total", 3, SensorType.SmallData, this, settings);

            _control = new Sensor("GPU Fan", 0, SensorType.Control, this, settings);

            NvApi.NvGPUCoolerSettings coolerSettings = GetCoolerSettings();
            if (coolerSettings.Count > 0)
            {
                _fanControl = new Control(_control, settings, coolerSettings.Cooler[0].DefaultMin, coolerSettings.Cooler[0].DefaultMax);
                _fanControl.ControlModeChanged          += ControlModeChanged;
                _fanControl.SoftwareControlValueChanged += SoftwareControlValueChanged;
                ControlModeChanged(_fanControl);
                _control.Control = _fanControl;
            }

            if (NvidiaML.IsAvailable)
            {
                if (hasPciBusId)
                {
                    _nvmlDevice = NvidiaML.NvmlDeviceGetHandleByPciBusId($" 0000:{busId:X2}:00.0") ?? NvidiaML.NvmlDeviceGetHandleByIndex(_adapterIndex);
                }
                else
                {
                    _nvmlDevice = NvidiaML.NvmlDeviceGetHandleByIndex(_adapterIndex);
                }

                if (_nvmlDevice.HasValue)
                {
                    _powerUsage = new Sensor("GPU Package", 0, SensorType.Power, this, settings);

                    _pcieThroughputRx = new Sensor("GPU PCIe Rx", 0, SensorType.Throughput, this, settings);
                    _pcieThroughputTx = new Sensor("GPU PCIe Tx", 1, SensorType.Throughput, this, settings);
                }
            }

            Update();
        }
示例#3
0
        public override void Update()
        {
            NvApi.NvGPUThermalSettings settings = GetThermalSettings();
            foreach (Sensor sensor in _temperatures)
            {
                sensor.Value = settings.Sensor[sensor.Index].CurrentTemp;
            }

            bool readTach = false;

            if (_fan != null)
            {
                if (NvApi.NvAPI_GPU_GetTachReading(_handle, out int value) == NvApi.NvStatus.OK)
                {
                    _fan.Value = value;
                    ActivateSensor(_fan);

                    readTach = true;
                }
            }

            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];
                }
            }

            NvApi.NvPStates states = new NvApi.NvPStates {
                Version = NvApi.GPU_PSTATES_VER, PStates = new NvApi.NvPState[NvApi.MAX_PSTATES_PER_GPU]
            };
            if (NvApi.NvAPI_GPU_GetPStates != null && NvApi.NvAPI_GPU_GetPStates(_handle, ref states) == NvApi.NvStatus.OK)
            {
                for (int i = 0; i < _loads.Length; i++)
                {
                    if (states.PStates[i].Present)
                    {
                        _loads[i].Value = states.PStates[i].Percentage;
                        ActivateSensor(_loads[i]);
                    }
                }
            }
            else
            {
                NvApi.NvUsages usages = new NvApi.NvUsages {
                    Version = NvApi.GPU_USAGES_VER, Usage = new uint[NvApi.MAX_USAGES_PER_GPU]
                };
                if (NvApi.NvAPI_GPU_GetUsages != null && NvApi.NvAPI_GPU_GetUsages(_handle, ref usages) == NvApi.NvStatus.OK)
                {
                    _loads[0].Value = usages.Usage[2];
                    _loads[1].Value = usages.Usage[6];
                    _loads[2].Value = usages.Usage[10];
                    for (int i = 0; i < 3; i++)
                    {
                        ActivateSensor(_loads[i]);
                    }
                }
            }

            bool readCoolerSettings = false;

            NvApi.NvGPUCoolerSettings coolerSettings = GetCoolerSettings();
            if (coolerSettings.Count > 0)
            {
                _control.Value = coolerSettings.Cooler[0].CurrentLevel;
                ActivateSensor(_control);

                readCoolerSettings = true;
            }

            if (!readTach || !readCoolerSettings)
            {
                NvApi.NvFanCoolersStatus coolersStatus = GetFanCoolersStatus();
                if (coolersStatus.Count > 0)
                {
                    if (!readCoolerSettings)
                    {
                        _control.Value = coolersStatus.Items[0].CurrentLevel;
                        ActivateSensor(_control);
                    }

                    if (!readTach && _fan != null)
                    {
                        _fan.Value = coolersStatus.Items[0].CurrentRpm;
                        ActivateSensor(_fan);
                    }
                }
            }

            NvApi.NvMemoryInfo memoryInfo = new NvApi.NvMemoryInfo {
                Version = NvApi.GPU_MEMORY_INFO_VER, Values = new uint[NvApi.MAX_MEMORY_VALUES_PER_GPU]
            };
            if (NvApi.NvAPI_GPU_GetMemoryInfo != null && _displayHandle.HasValue && NvApi.NvAPI_GPU_GetMemoryInfo(_displayHandle.Value, ref memoryInfo) == NvApi.NvStatus.OK)
            {
                uint  totalMemory = memoryInfo.Values[0];
                uint  freeMemory  = memoryInfo.Values[4];
                float usedMemory  = Math.Max(totalMemory - freeMemory, 0);
                _memoryFree.Value  = (float)freeMemory / 1024;
                _memoryAvail.Value = (float)totalMemory / 1024;
                _memoryUsed.Value  = usedMemory / 1024;
                _memoryLoad.Value  = 100f * usedMemory / totalMemory;
                ActivateSensor(_memoryAvail);
                ActivateSensor(_memoryUsed);
                ActivateSensor(_memoryFree);
                ActivateSensor(_memoryLoad);
            }

            if (NvidiaML.IsAvailable && _nvmlDevice.HasValue)
            {
                int?result = NvidiaML.NvmlDeviceGetPowerUsage(_nvmlDevice.Value);
                if (result.HasValue)
                {
                    _powerUsage.Value = (float)result.Value / 1000;
                    ActivateSensor(_powerUsage);
                }

                // In MB/s, throughput sensors are passed as in KB/s.
                uint?rx = NvidiaML.NvmlDeviceGetPcieThroughput(_nvmlDevice.Value, NvidiaML.NvmlPcieUtilCounter.RxBytes);
                if (rx.HasValue)
                {
                    _pcieThroughputRx.Value = rx * 1024;
                    ActivateSensor(_pcieThroughputRx);
                }

                uint?tx = NvidiaML.NvmlDeviceGetPcieThroughput(_nvmlDevice.Value, NvidiaML.NvmlPcieUtilCounter.TxBytes);
                if (tx.HasValue)
                {
                    _pcieThroughputTx.Value = tx * 1024;
                    ActivateSensor(_pcieThroughputTx);
                }
            }
        }
示例#4
0
        public override void Update()
        {
            NvApi.NvGPUThermalSettings settings = GetThermalSettings();
            foreach (Sensor sensor in _temperatures)
            {
                sensor.Value = settings.Sensor[sensor.Index].CurrentTemp;
            }

            if (_fan != null)
            {
                NvApi.NvAPI_GPU_GetTachReading(_handle, out int value);
                _fan.Value = value;
            }

            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];
                }
            }

            NvApi.NvPStates states = new NvApi.NvPStates {
                Version = NvApi.GPU_PSTATES_VER, PStates = new NvApi.NvPState[NvApi.MAX_PSTATES_PER_GPU]
            };
            if (NvApi.NvAPI_GPU_GetPStates != null && NvApi.NvAPI_GPU_GetPStates(_handle, ref states) == NvApi.NvStatus.OK)
            {
                for (int i = 0; i < 3; i++)
                {
                    if (states.PStates[i].Present)
                    {
                        _loads[i].Value = states.PStates[i].Percentage;
                        ActivateSensor(_loads[i]);
                    }
                }
            }
            else
            {
                NvApi.NvUsages usages = new NvApi.NvUsages {
                    Version = NvApi.GPU_USAGES_VER, Usage = new uint[NvApi.MAX_USAGES_PER_GPU]
                };
                if (NvApi.NvAPI_GPU_GetUsages != null && NvApi.NvAPI_GPU_GetUsages(_handle, ref usages) == NvApi.NvStatus.OK)
                {
                    _loads[0].Value = usages.Usage[2];
                    _loads[1].Value = usages.Usage[6];
                    _loads[2].Value = usages.Usage[10];
                    for (int i = 0; i < 3; i++)
                    {
                        ActivateSensor(_loads[i]);
                    }
                }
            }

            NvApi.NvGPUCoolerSettings coolerSettings = GetCoolerSettings();
            if (coolerSettings.Count > 0)
            {
                _control.Value = coolerSettings.Cooler[0].CurrentLevel;
                ActivateSensor(_control);
            }

            NvApi.NvMemoryInfo memoryInfo = new NvApi.NvMemoryInfo {
                Version = NvApi.GPU_MEMORY_INFO_VER, Values = new uint[NvApi.MAX_MEMORY_VALUES_PER_GPU]
            };
            if (NvApi.NvAPI_GPU_GetMemoryInfo != null && _displayHandle.HasValue && NvApi.NvAPI_GPU_GetMemoryInfo(_displayHandle.Value, ref memoryInfo) == NvApi.NvStatus.OK)
            {
                uint  totalMemory = memoryInfo.Values[0];
                uint  freeMemory  = memoryInfo.Values[4];
                float usedMemory  = Math.Max(totalMemory - freeMemory, 0);
                _memoryFree.Value  = (float)freeMemory / 1024;
                _memoryAvail.Value = (float)totalMemory / 1024;
                _memoryUsed.Value  = usedMemory / 1024;
                _memoryLoad.Value  = 100f * usedMemory / totalMemory;
                ActivateSensor(_memoryAvail);
                ActivateSensor(_memoryUsed);
                ActivateSensor(_memoryFree);
                ActivateSensor(_memoryLoad);
            }

            if (NvidiaML.IsAvailable && _nvmlDevice.HasValue)
            {
                var result = NvidiaML.NvmlDeviceGetPowerUsage(_nvmlDevice.Value);
                if (result.HasValue)
                {
                    _powerUsage.Value = (float)result.Value / 1000;
                    ActivateSensor(_powerUsage);
                }
            }
        }
示例#5
0
        public NvidiaGpu(int adapterIndex, PhysicalGPU physicalGpu, ISettings settings)
            : base(GetName(physicalGpu),
                   new Identifier("gpu-nvidia", adapterIndex.ToString(CultureInfo.InvariantCulture)),
                   settings)
        {
            _adapterIndex = adapterIndex;
            _physicalGpu  = physicalGpu;

            int busId = -1;

            try
            {
                busId = physicalGpu.BusInformation.BusId;
            }
            catch (Exception e) when(e is NVIDIAApiException or NVIDIANotSupportedException)
            {
            }

            try
            {
                GPUThermalSensor[] thermalSensors = physicalGpu.ThermalInformation.ThermalSensors.ToArray();
                _temperatures = new Sensor[thermalSensors.Length];

                for (int i = 0; i < thermalSensors.Length; i++)
                {
                    GPUThermalSensor sensor = thermalSensors[i];

                    string name = sensor.Target switch
                    {
                        ThermalSettingsTarget.GPU => "GPU Core",
                        ThermalSettingsTarget.Memory => "GPU Memory",
                        ThermalSettingsTarget.PowerSupply => "GPU Power Supply",
                        ThermalSettingsTarget.Board => "GPU Board",
                        ThermalSettingsTarget.VisualComputingBoard => "GPU Visual Computing Board",
                        ThermalSettingsTarget.VisualComputingInlet => "GPU Visual Computing Inlet",
                        ThermalSettingsTarget.VisualComputingOutlet => "GPU Visual Computing Outlet",
                        _ => "GPU"
                    };

                    _temperatures[i] = new Sensor(name, i, SensorType.Temperature, this, new ParameterDescription[0], settings);
                    ActivateSensor(_temperatures[i]);
                }
            }
            catch (Exception e) when(e is NVIDIAApiException or NVIDIANotSupportedException)
            {
            }

            try
            {
                KeyValuePair <PublicClockDomain, ClockDomainInfo>[] clocks = physicalGpu.CurrentClockFrequencies.Clocks.OrderBy(x => x.Key).ToArray();
                _clocks = new Sensor[clocks.Length];

                for (int i = 0; i < clocks.Length; i++)
                {
                    KeyValuePair <PublicClockDomain, ClockDomainInfo> clock = clocks[i];

                    string name = clock.Key switch
                    {
                        PublicClockDomain.Graphics => "GPU Core",
                        PublicClockDomain.Memory => "GPU Memory",
                        PublicClockDomain.Processor => "GPU Shader",
                        PublicClockDomain.Video => "GPU Video",
                        _ => null
                    };

                    _clocks[i] = new Sensor(name, i, SensorType.Clock, this, settings);
                    ActivateSensor(_clocks[i]);
                }
            }
            catch (Exception e) when(e is NVIDIAApiException or NVIDIANotSupportedException)
            {
            }

            try
            {
                GPUUsageDomainStatus[] usages = physicalGpu.UsageInformation.UtilizationDomainsStatus.ToArray();
                _loads = new Sensor[usages.Length + 1];

                for (int i = 0; i < usages.Length; i++)
                {
                    string name = usages[i].Domain switch
                    {
                        UtilizationDomain.GPU => "GPU Core",
                        UtilizationDomain.FrameBuffer => "GPU Memory Controller",
                        UtilizationDomain.VideoEngine => "GPU Video Engine",
                        UtilizationDomain.BusInterface => "GPU Bus",
                        _ => null
                    };

                    _loads[i] = new Sensor(name, i, SensorType.Load, this, settings);
                    ActivateSensor(_loads[i]);
                }

                _loads[_loads.Length - 1] = new Sensor("GPU Memory", _loads.Length - 1, SensorType.Load, this, settings);
                ActivateSensor(_loads[_loads.Length - 1]);
            }
            catch (Exception e) when(e is NVIDIAApiException or NVIDIANotSupportedException)
            {
            }

            try
            {
                GPUCooler[] coolers = physicalGpu.CoolerInformation.Coolers.ToArray();
                if (coolers.Length > 0)
                {
                    _controls    = new Sensor[coolers.Length];
                    _fanControls = new Control[coolers.Length];
                    _fans        = new Sensor[coolers.Length];

                    for (int i = 0; i < coolers.Length; i++)
                    {
                        GPUCooler cooler = coolers[i];
                        string    name   = "GPU Fan" + (coolers.Length > 1 ? " " + (cooler.CoolerId) : string.Empty);

                        _fans[i] = new Sensor(name, i, SensorType.Fan, this, settings);
                        ActivateSensor(_fans[i]);

                        _controls[i] = new Sensor(name, i, SensorType.Control, this, settings);
                        ActivateSensor(_controls[i]);

                        _fanControls[i] = new Control(_controls[i], settings, cooler.DefaultMinimumLevel, cooler.DefaultMaximumLevel);
                        _fanControls[i].ControlModeChanged          += ControlModeChanged;
                        _fanControls[i].SoftwareControlValueChanged += SoftwareControlValueChanged;
                        _controls[i].Control = _fanControls[i];

                        ControlModeChanged(_fanControls[i]);
                    }
                }
            }
            catch (Exception e) when(e is NVIDIAApiException or NVIDIANotSupportedException)
            {
            }

            try
            {
                GPUPowerTopologyStatus[] powerTopologies = physicalGpu.PowerTopologyInformation.PowerTopologyEntries.ToArray();
                if (powerTopologies.Length > 0)
                {
                    _powers = new Sensor[powerTopologies.Length];

                    for (int i = 0; i < powerTopologies.Length; i++)
                    {
                        GPUPowerTopologyStatus powerTopology = powerTopologies[i];

                        string name = powerTopology.Domain switch
                        {
                            PowerTopologyDomain.GPU => "GPU Power",
                            PowerTopologyDomain.Board => "GPU Board Power",
                            _ => null
                        };

                        _powers[i] = new Sensor(name, i + (_loads?.Length ?? 0), SensorType.Load, this, settings);
                        ActivateSensor(_powers[i]);
                    }
                }
            }
            catch (Exception e) when(e is NVIDIAApiException or NVIDIANotSupportedException)
            {
            }

            if (NvidiaML.IsAvailable || NvidiaML.Initialize())
            {
                if (busId != -1)
                {
                    _nvmlDevice = NvidiaML.NvmlDeviceGetHandleByPciBusId($" 0000:{busId:X2}:00.0") ?? NvidiaML.NvmlDeviceGetHandleByIndex(_adapterIndex);
                }
                else
                {
                    _nvmlDevice = NvidiaML.NvmlDeviceGetHandleByIndex(_adapterIndex);
                }

                if (_nvmlDevice.HasValue)
                {
                    _powerUsage = new Sensor("GPU Package", 0, SensorType.Power, this, settings);

                    _pcieThroughputRx = new Sensor("GPU PCIe Rx", 0, SensorType.Throughput, this, settings);
                    _pcieThroughputTx = new Sensor("GPU PCIe Tx", 1, SensorType.Throughput, this, settings);

                    if (!Software.OperatingSystem.IsUnix)
                    {
                        NvidiaML.NvmlPciInfo?pciInfo = NvidiaML.NvmlDeviceGetPciInfo(_nvmlDevice.Value);

                        if (pciInfo is { } pci)
                        {
                            string[] deviceIds = D3DDisplayDevice.GetDeviceIdentifiers();
                            if (deviceIds != null)
                            {
                                foreach (string deviceId in deviceIds)
                                {
                                    if (deviceId.IndexOf("VEN_" + pci.pciVendorId.ToString("X"), StringComparison.OrdinalIgnoreCase) != -1 &&
                                        deviceId.IndexOf("DEV_" + pci.pciDeviceId.ToString("X"), StringComparison.OrdinalIgnoreCase) != -1 &&
                                        deviceId.IndexOf("SUBSYS_" + pci.pciSubSystemId.ToString("X"), StringComparison.OrdinalIgnoreCase) != -1)
                                    {
                                        bool isMatch = false;

                                        string actualDeviceId = D3DDisplayDevice.GetActualDeviceIdentifier(deviceId);

                                        try
                                        {
                                            if (Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\nvlddmkm\Enum", adapterIndex.ToString(), null) is string adapterPnpId)
                                            {
                                                if (actualDeviceId.IndexOf(adapterPnpId, StringComparison.OrdinalIgnoreCase) != -1 ||
                                                    adapterPnpId.IndexOf(actualDeviceId, StringComparison.OrdinalIgnoreCase) != -1)
                                                {
                                                    isMatch = true;
                                                }
                                            }
                                        }
                                        catch
                                        {
                                            // Ignored.
                                        }

                                        if (!isMatch)
                                        {
                                            try
                                            {
                                                string path = actualDeviceId;
                                                path = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" + path;

                                                if (Registry.GetValue(path, "LocationInformation", null) is string locationInformation)
                                                {
                                                    // For example:
                                                    // @System32\drivers\pci.sys,#65536;PCI bus %1, device %2, function %3;(38,0,0)

                                                    int index = locationInformation.IndexOf('(');
                                                    if (index != -1)
                                                    {
                                                        index++;
                                                        int secondIndex = locationInformation.IndexOf(',', index);
                                                        if (secondIndex != -1)
                                                        {
                                                            string bus = locationInformation.Substring(index, secondIndex - index);

                                                            if (pci.bus.ToString() == bus)
                                                            {
                                                                isMatch = true;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            catch
                                            {
                                                // Ignored.
                                            }
                                        }

                                        if (isMatch && D3DDisplayDevice.GetDeviceInfoByIdentifier(deviceId, out D3DDisplayDevice.D3DDeviceInfo deviceInfo))
                                        {
                                            int nodeSensorIndex   = (_loads?.Length ?? 0) + (_powers?.Length ?? 0);
                                            int memorySensorIndex = 3; // There are three normal GPU memory sensors.

                                            _d3dDeviceId = deviceId;

                                            _gpuDedicatedMemoryUsage = new Sensor("D3D Dedicated Memory Used", memorySensorIndex++, SensorType.SmallData, this, settings);
                                            _gpuSharedMemoryUsage    = new Sensor("D3D Shared Memory Used", memorySensorIndex, SensorType.SmallData, this, settings);

                                            _gpuNodeUsage          = new Sensor[deviceInfo.Nodes.Length];
                                            _gpuNodeUsagePrevValue = new long[deviceInfo.Nodes.Length];
                                            _gpuNodeUsagePrevTick  = new DateTime[deviceInfo.Nodes.Length];

                                            foreach (D3DDisplayDevice.D3DDeviceNodeInfo node in deviceInfo.Nodes.OrderBy(x => x.Name))
                                            {
                                                _gpuNodeUsage[node.Id]          = new Sensor(node.Name, nodeSensorIndex++, SensorType.Load, this, settings);
                                                _gpuNodeUsagePrevValue[node.Id] = node.RunningTime;
                                                _gpuNodeUsagePrevTick[node.Id]  = node.QueryTime;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            _memoryFree  = new Sensor("GPU Memory Free", 0, SensorType.SmallData, this, settings);
            _memoryUsed  = new Sensor("GPU Memory Used", 1, SensorType.SmallData, this, settings);
            _memoryTotal = new Sensor("GPU Memory Total", 2, SensorType.SmallData, this, settings);

            Update();
        }
示例#6
0
        public NvidiaGpu(int adapterIndex, NvApi.NvPhysicalGpuHandle handle, NvApi.NvDisplayHandle?displayHandle, ISettings settings)
            : base(GetName(handle), new Identifier("gpu-nvidia", adapterIndex.ToString(CultureInfo.InvariantCulture)), settings)
        {
            _adapterIndex  = adapterIndex;
            _handle        = handle;
            _displayHandle = displayHandle;

            bool hasPciBusId = NvApi.NvAPI_GPU_GetBusId(handle, out uint busId) == NvApi.NvStatus.OK;

            NvApi.NvGPUThermalSettings thermalSettings = GetThermalSettings();
            _temperatures = new Sensor[thermalSettings.Count];
            for (int i = 0; i < _temperatures.Length; i++)
            {
                NvApi.NvSensor sensor = thermalSettings.Sensor[i];
                string         name;
                switch (sensor.Target)
                {
                case NvApi.NvThermalTarget.BOARD:
                    name = "GPU Board";
                    break;

                case NvApi.NvThermalTarget.GPU:
                    name = "GPU Core";
                    break;

                case NvApi.NvThermalTarget.MEMORY:
                    name = "GPU Memory";
                    break;

                case NvApi.NvThermalTarget.POWER_SUPPLY:
                    name = "GPU Power Supply";
                    break;

                case NvApi.NvThermalTarget.UNKNOWN:
                    name = "GPU Unknown";
                    break;

                default:
                    name = "GPU";
                    break;
                }

                _temperatures[i] = new Sensor(name, i, SensorType.Temperature, this, new ParameterDescription[0], settings);
                ActivateSensor(_temperatures[i]);
            }

            if (NvApi.NvAPI_GPU_GetTachReading != null && NvApi.NvAPI_GPU_GetTachReading(handle, out _) == NvApi.NvStatus.OK)
            {
                _fan = new Sensor("GPU", 0, SensorType.Fan, this, settings);
            }
            else if (NvApi.NvAPI_GPU_ClientFanCoolersGetStatus != null && GetCoolerSettings().Count > 0)
            {
                _fan = new Sensor("GPU", 0, SensorType.Fan, this, settings);
            }

            _clocks    = new Sensor[3];
            _clocks[0] = new Sensor("GPU Core", 0, SensorType.Clock, this, settings);
            _clocks[1] = new Sensor("GPU Memory", 1, SensorType.Clock, this, settings);
            _clocks[2] = new Sensor("GPU Shader", 2, SensorType.Clock, this, settings);
            for (int i = 0; i < _clocks.Length; i++)
            {
                ActivateSensor(_clocks[i]);
            }

            _loads    = new Sensor[4];
            _loads[0] = new Sensor("GPU Core", 0, SensorType.Load, this, settings);
            _loads[1] = new Sensor("GPU Memory Controller", 1, SensorType.Load, this, settings);
            _loads[2] = new Sensor("GPU Video Engine", 2, SensorType.Load, this, settings);
            _loads[3] = new Sensor("GPU Bus", 4, SensorType.Load, this, settings);

            _memoryLoad  = new Sensor("GPU Memory", 3, SensorType.Load, this, settings);
            _memoryFree  = new Sensor("GPU Memory Free", 1, SensorType.SmallData, this, settings);
            _memoryUsed  = new Sensor("GPU Memory Used", 2, SensorType.SmallData, this, settings);
            _memoryAvail = new Sensor("GPU Memory Total", 3, SensorType.SmallData, this, settings);

            _control = new Sensor("GPU Fan", 0, SensorType.Control, this, settings);

            NvApi.NvGPUCoolerSettings coolerSettings = GetCoolerSettings();
            if (coolerSettings.Count > 0)
            {
                _fanControl = new Control(_control, settings, coolerSettings.Cooler[0].DefaultMin, coolerSettings.Cooler[0].DefaultMax);
                _fanControl.ControlModeChanged          += ControlModeChanged;
                _fanControl.SoftwareControlValueChanged += SoftwareControlValueChanged;
                ControlModeChanged(_fanControl);
                _control.Control = _fanControl;
            }

            if (NvidiaML.IsAvailable || NvidiaML.Initialize())
            {
                if (hasPciBusId)
                {
                    _nvmlDevice = NvidiaML.NvmlDeviceGetHandleByPciBusId($" 0000:{busId:X2}:00.0") ?? NvidiaML.NvmlDeviceGetHandleByIndex(_adapterIndex);
                }
                else
                {
                    _nvmlDevice = NvidiaML.NvmlDeviceGetHandleByIndex(_adapterIndex);
                }

                if (_nvmlDevice.HasValue)
                {
                    _powerUsage = new Sensor("GPU Package", 0, SensorType.Power, this, settings);

                    _pcieThroughputRx = new Sensor("GPU PCIe Rx", 0, SensorType.Throughput, this, settings);
                    _pcieThroughputTx = new Sensor("GPU PCIe Tx", 1, SensorType.Throughput, this, settings);

                    if (!Software.OperatingSystem.IsUnix)
                    {
                        NvidiaML.NvmlPciInfo?pciInfo = NvidiaML.NvmlDeviceGetPciInfo(_nvmlDevice.Value);

                        if (pciInfo is { } pci)
                        {
                            string[] deviceIdentifiers = D3DDisplayDevice.GetDeviceIdentifiers();
                            if (deviceIdentifiers != null)
                            {
                                foreach (string deviceIdentifier in deviceIdentifiers)
                                {
                                    if (deviceIdentifier.IndexOf("VEN_" + pci.pciVendorId.ToString("X"), StringComparison.OrdinalIgnoreCase) != -1 &&
                                        deviceIdentifier.IndexOf("DEV_" + pci.pciDeviceId.ToString("X"), StringComparison.OrdinalIgnoreCase) != -1 &&
                                        deviceIdentifier.IndexOf("SUBSYS_" + pci.pciSubSystemId.ToString("X"), StringComparison.OrdinalIgnoreCase) != -1)
                                    {
                                        bool isMatch = false;

                                        try
                                        {
                                            if (Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\nvlddmkm\Enum", adapterIndex.ToString(), null) is string adapterPnpId)
                                            {
                                                if (deviceIdentifier.IndexOf(adapterPnpId.Replace('\\', '#'), StringComparison.OrdinalIgnoreCase) != -1)
                                                {
                                                    isMatch = true;
                                                }
                                            }
                                        }
                                        catch
                                        {
                                            // Ignored.
                                        }

                                        if (!isMatch)
                                        {
                                            try
                                            {
                                                string path = deviceIdentifier;
                                                if (path.StartsWith(@"\\?\"))
                                                {
                                                    path = path.Substring(4);
                                                }

                                                path = path.Replace('#', '\\');
                                                int index = path.IndexOf('{');
                                                if (index != -1)
                                                {
                                                    path = path.Substring(0, index);
                                                }

                                                path = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" + path;

                                                if (Registry.GetValue(path, "LocationInformation", null) is string locationInformation)
                                                {
                                                    // For example:
                                                    // @System32\drivers\pci.sys,#65536;PCI bus %1, device %2, function %3;(38,0,0)

                                                    index = locationInformation.IndexOf('(');
                                                    if (index != -1)
                                                    {
                                                        index++;
                                                        int secondIndex = locationInformation.IndexOf(',', index);
                                                        if (secondIndex != -1)
                                                        {
                                                            string bus = locationInformation.Substring(index, secondIndex - index);

                                                            if (pci.bus.ToString() == bus)
                                                            {
                                                                isMatch = true;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            catch
                                            {
                                                // Ignored.
                                            }
                                        }

                                        if (isMatch && D3DDisplayDevice.GetDeviceInfoByIdentifier(deviceIdentifier, out D3DDisplayDevice.D3DDeviceInfo deviceInfo))
                                        {
                                            _windowsDeviceName = deviceIdentifier;

                                            _gpuDedicatedMemoryUsage = new Sensor("D3D Dedicated Memory Used", _memorySensorIndex++, SensorType.SmallData, this, settings);
                                            _gpuSharedMemoryUsage    = new Sensor("D3D Shared Memory Used", _memorySensorIndex++, SensorType.SmallData, this, settings);

                                            _gpuNodeUsage          = new Sensor[deviceInfo.Nodes.Length];
                                            _gpuNodeUsagePrevValue = new long[deviceInfo.Nodes.Length];
                                            _gpuNodeUsagePrevTick  = new DateTime[deviceInfo.Nodes.Length];

                                            foreach (D3DDisplayDevice.D3DDeviceNodeInfo node in deviceInfo.Nodes)
                                            {
                                                _gpuNodeUsage[node.Id]          = new Sensor(node.Name, _nodeSensorIndex++, SensorType.Load, this, settings);
                                                _gpuNodeUsagePrevValue[node.Id] = node.RunningTime;
                                                _gpuNodeUsagePrevTick[node.Id]  = node.QueryTime;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            Update();
        }
示例#7
0
        public override void Update()
        {
            if (_windowsDeviceName != null && D3DDisplayDevice.GetDeviceInfoByIdentifier(_windowsDeviceName, out D3DDisplayDevice.D3DDeviceInfo deviceInfo))
            {
                _gpuDedicatedMemoryUsage.Value = 1f * deviceInfo.GpuDedicatedUsed / 1024 / 1024;
                _gpuSharedMemoryUsage.Value    = 1f * deviceInfo.GpuSharedUsed / 1024 / 1024;
                ActivateSensor(_gpuDedicatedMemoryUsage);
                ActivateSensor(_gpuSharedMemoryUsage);

                foreach (D3DDisplayDevice.D3DDeviceNodeInfo node in deviceInfo.Nodes)
                {
                    long runningTimeDiff = node.RunningTime - _gpuNodeUsagePrevValue[node.Id];
                    long timeDiff        = node.QueryTime.Ticks - _gpuNodeUsagePrevTick[node.Id].Ticks;

                    _gpuNodeUsage[node.Id].Value    = 100f * runningTimeDiff / timeDiff;
                    _gpuNodeUsagePrevValue[node.Id] = node.RunningTime;
                    _gpuNodeUsagePrevTick[node.Id]  = node.QueryTime;
                    ActivateSensor(_gpuNodeUsage[node.Id]);
                }
            }

            NvApi.NvGPUThermalSettings settings = GetThermalSettings();
            // settings.Count is 0 when no valid data available, this happens when you try to read out this value with a high polling interval.
            if (settings.Count > 0)
            {
                foreach (Sensor sensor in _temperatures)
                {
                    sensor.Value = settings.Sensor[sensor.Index].CurrentTemp;
                }
            }

            bool readTach = false;

            if (_fan != null)
            {
                if (NvApi.NvAPI_GPU_GetTachReading(_handle, out int value) == NvApi.NvStatus.OK)
                {
                    _fan.Value = value;
                    ActivateSensor(_fan);

                    readTach = true;
                }
            }

            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];
                }
            }

            NvApi.NvPStates states = new() { Version = NvApi.GPU_PSTATES_VER, PStates = new NvApi.NvPState[NvApi.MAX_PSTATES_PER_GPU] };
            if (NvApi.NvAPI_GPU_GetPStates != null && NvApi.NvAPI_GPU_GetPStates(_handle, ref states) == NvApi.NvStatus.OK)
            {
                for (int i = 0; i < _loads.Length; i++)
                {
                    if (states.PStates[i].Present)
                    {
                        _loads[i].Value = states.PStates[i].Percentage;
                        ActivateSensor(_loads[i]);
                    }
                }
            }
            else
            {
                NvApi.NvUsages usages = new() { Version = NvApi.GPU_USAGES_VER, Usage = new uint[NvApi.MAX_USAGES_PER_GPU] };
                if (NvApi.NvAPI_GPU_GetUsages != null && NvApi.NvAPI_GPU_GetUsages(_handle, ref usages) == NvApi.NvStatus.OK)
                {
                    _loads[0].Value = usages.Usage[2];
                    _loads[1].Value = usages.Usage[6];
                    _loads[2].Value = usages.Usage[10];
                    for (int i = 0; i < 3; i++)
                    {
                        ActivateSensor(_loads[i]);
                    }
                }
            }

            bool readCoolerSettings = false;

            NvApi.NvGPUCoolerSettings coolerSettings = GetCoolerSettings();
            if (coolerSettings.Count > 0)
            {
                _control.Value = coolerSettings.Cooler[0].CurrentLevel;
                ActivateSensor(_control);

                readCoolerSettings = true;
            }

            if (!readTach || !readCoolerSettings)
            {
                NvApi.NvFanCoolersStatus coolersStatus = GetFanCoolersStatus();
                if (coolersStatus.Count > 0)
                {
                    if (!readCoolerSettings)
                    {
                        _control.Value = coolersStatus.Items[0].CurrentLevel;
                        ActivateSensor(_control);
                    }

                    if (!readTach && _fan != null)
                    {
                        _fan.Value = coolersStatus.Items[0].CurrentRpm;
                        ActivateSensor(_fan);
                    }
                }
            }

            NvApi.NvMemoryInfo memoryInfo = new() { Version = NvApi.GPU_MEMORY_INFO_VER, Values = new uint[NvApi.MAX_MEMORY_VALUES_PER_GPU] };
            if (NvApi.NvAPI_GPU_GetMemoryInfo != null && _displayHandle.HasValue && NvApi.NvAPI_GPU_GetMemoryInfo(_displayHandle.Value, ref memoryInfo) == NvApi.NvStatus.OK)
            {
                uint  totalMemory = memoryInfo.Values[0];
                uint  freeMemory  = memoryInfo.Values[4];
                float usedMemory  = Math.Max(totalMemory - freeMemory, 0);
                _memoryFree.Value  = (float)freeMemory / 1024;
                _memoryAvail.Value = (float)totalMemory / 1024;
                _memoryUsed.Value  = usedMemory / 1024;
                _memoryLoad.Value  = 100f * usedMemory / totalMemory;
                ActivateSensor(_memoryAvail);
                ActivateSensor(_memoryUsed);
                ActivateSensor(_memoryFree);
                ActivateSensor(_memoryLoad);
            }

            if (NvidiaML.IsAvailable && _nvmlDevice.HasValue)
            {
                int?result = NvidiaML.NvmlDeviceGetPowerUsage(_nvmlDevice.Value);
                if (result.HasValue)
                {
                    _powerUsage.Value = (float)result.Value / 1000;
                    ActivateSensor(_powerUsage);
                }

                // In MB/s, throughput sensors are passed as in KB/s.
                uint?rx = NvidiaML.NvmlDeviceGetPcieThroughput(_nvmlDevice.Value, NvidiaML.NvmlPcieUtilCounter.RxBytes);
                if (rx.HasValue)
                {
                    _pcieThroughputRx.Value = rx * 1024;
                    ActivateSensor(_pcieThroughputRx);
                }

                uint?tx = NvidiaML.NvmlDeviceGetPcieThroughput(_nvmlDevice.Value, NvidiaML.NvmlPcieUtilCounter.TxBytes);
                if (tx.HasValue)
                {
                    _pcieThroughputTx.Value = tx * 1024;
                    ActivateSensor(_pcieThroughputTx);
                }
            }
        }

        public override string GetReport()