private static string GetName(string deviceId)
        {
            var path = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" + D3DDisplayDevice.GetActualDeviceIdentifier(deviceId);

            if (Registry.GetValue(path, "DeviceDesc", null) is string deviceDesc)
            {
                return(deviceDesc.Split(';').Last());
            }

            return("Intel Integrated Graphics");
        }
Exemplo n.º 2
0
        public AmdGpu(AtiAdlxx.ADLAdapterInfo adapterInfo, ISettings settings)
            : base(adapterInfo.AdapterName.Trim(), new Identifier("gpu-amd", adapterInfo.AdapterIndex.ToString(CultureInfo.InvariantCulture)), settings)
        {
            _adapterIndex = adapterInfo.AdapterIndex;
            BusNumber     = adapterInfo.BusNumber;
            DeviceNumber  = adapterInfo.DeviceNumber;

            _temperatureCore    = new Sensor("GPU Core", 0, SensorType.Temperature, this, settings);
            _temperatureMemory  = new Sensor("GPU Memory", 1, SensorType.Temperature, this, settings);
            _temperatureVddc    = new Sensor("GPU VR VDDC", 2, SensorType.Temperature, this, settings);
            _temperatureMvdd    = new Sensor("GPU VR MVDD", 3, SensorType.Temperature, this, settings);
            _temperatureSoC     = new Sensor("GPU VR SoC", 4, SensorType.Temperature, this, settings);
            _temperatureLiquid  = new Sensor("GPU Liquid", 5, SensorType.Temperature, this, settings);
            _temperaturePlx     = new Sensor("GPU PLX", 6, SensorType.Temperature, this, settings);
            _temperatureHotSpot = new Sensor("GPU Hot Spot", 7, SensorType.Temperature, this, settings);

            _coreClock   = new Sensor("GPU Core", 0, SensorType.Clock, this, settings);
            _socClock    = new Sensor("GPU SoC", 1, SensorType.Clock, this, settings);
            _memoryClock = new Sensor("GPU Memory", 2, SensorType.Clock, this, settings);

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

            _coreVoltage   = new Sensor("GPU Core", 0, SensorType.Voltage, this, settings);
            _memoryVoltage = new Sensor("GPU Memory", 1, SensorType.Voltage, this, settings);
            _socVoltage    = new Sensor("GPU SoC", 2, SensorType.Voltage, this, settings);

            _coreLoad   = new Sensor("GPU Core", 0, SensorType.Load, this, settings);
            _memoryLoad = new Sensor("GPU Memory", 1, SensorType.Load, this, settings);

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

            _powerCore  = new Sensor("GPU Core", 0, SensorType.Power, this, settings);
            _powerPpt   = new Sensor("GPU PPT", 1, SensorType.Power, this, settings);
            _powerSoC   = new Sensor("GPU SoC", 2, SensorType.Power, this, settings);
            _powerTotal = new Sensor("GPU Package", 3, SensorType.Power, this, settings);

            _fullscreenFps = new Sensor("Fullscreen FPS", 0, SensorType.Factor, this, settings);

            if (!Software.OperatingSystem.IsUnix)
            {
                string[] deviceIds = D3DDisplayDevice.GetDeviceIdentifiers();
                if (deviceIds != null)
                {
                    foreach (string deviceId in deviceIds)
                    {
                        string actualDeviceId = D3DDisplayDevice.GetActualDeviceIdentifier(deviceId);

                        if ((actualDeviceId.IndexOf(adapterInfo.PNPString, StringComparison.OrdinalIgnoreCase) != -1 ||
                             adapterInfo.PNPString.IndexOf(actualDeviceId, StringComparison.OrdinalIgnoreCase) != -1) &&
                            D3DDisplayDevice.GetDeviceInfoByIdentifier(deviceId, out D3DDisplayDevice.D3DDeviceInfo deviceInfo))
                        {
                            _d3dDeviceId = deviceId;

                            int nodeSensorIndex   = 2;
                            int memorySensorIndex = 0;

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

                            break;
                        }
                    }
                }
            }

            int supported = 0;
            int enabled   = 0;
            int version   = 0;

            if (AtiAdlxx.ADL_Method_Exists("ADL2_Adapter_FrameMetrics_Caps") && AtiAdlxx.ADL2_Adapter_FrameMetrics_Caps(_context, _adapterIndex, ref supported) == AtiAdlxx.ADLStatus.ADL_OK)
            {
                if (supported == AtiAdlxx.ADL_TRUE && AtiAdlxx.ADL2_Adapter_FrameMetrics_Start(_context, _adapterIndex, 0) == AtiAdlxx.ADLStatus.ADL_OK)
                {
                    _frameMetricsStarted = true;
                    _fullscreenFps.Value = -1;
                    ActivateSensor(_fullscreenFps);
                }
            }

            if (AtiAdlxx.ADL_Overdrive_Caps(_adapterIndex, ref supported, ref enabled, ref version) == AtiAdlxx.ADLStatus.ADL_OK)
            {
                _overdriveApiSupported    = supported == AtiAdlxx.ADL_TRUE;
                _currentOverdriveApiLevel = version;
            }
            else
            {
                _currentOverdriveApiLevel = -1;
            }

            if (_currentOverdriveApiLevel >= 5)
            {
                if (AtiAdlxx.ADL2_Main_Control_Create(AtiAdlxx.Main_Memory_Alloc, _adapterIndex, ref _context) != AtiAdlxx.ADLStatus.ADL_OK)
                {
                    _context = IntPtr.Zero;
                }
            }

            AtiAdlxx.ADLFanSpeedInfo fanSpeedInfo = new();
            if (AtiAdlxx.ADL_Overdrive5_FanSpeedInfo_Get(_adapterIndex, 0, ref fanSpeedInfo) != AtiAdlxx.ADLStatus.ADL_OK)
            {
                fanSpeedInfo.MaxPercent = 100;
                fanSpeedInfo.MinPercent = 0;
            }

            _fanControl = new Control(_controlSensor, settings, fanSpeedInfo.MinPercent, fanSpeedInfo.MaxPercent);
            _fanControl.ControlModeChanged          += ControlModeChanged;
            _fanControl.SoftwareControlValueChanged += SoftwareControlValueChanged;
            ControlModeChanged(_fanControl);
            _controlSensor.Control = _fanControl;

            Update();
        }
Exemplo n.º 3
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();
        }