예제 #1
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();
        }
예제 #2
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[] 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))
                                        {
                                            int nodeSensorIndex   = (_loads?.Length ?? 0) + (_powers?.Length ?? 0);
                                            int memorySensorIndex = 3; // There are three normal GPU memory sensors.

                                            _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.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();
        }