コード例 #1
0
        private static string GetName(NvApi.NvPhysicalGpuHandle handle)
        {
            if (NvApi.NvAPI_GPU_GetFullName(handle, out string gpuName) == NvApi.NvStatus.OK)
            {
                return("NVIDIA " + gpuName.Trim());
            }

            return("NVIDIA");
        }
コード例 #2
0
        private static string GetName(NvApi.NvPhysicalGpuHandle handle)
        {
            if (NvApi.NvAPI_GPU_GetFullName(handle, out string gpuName) == NvApi.NvStatus.OK)
            {
                gpuName = gpuName.Trim();
                if (gpuName.StartsWith("NVIDIA", StringComparison.OrdinalIgnoreCase))
                {
                    return(gpuName);
                }

                return("NVIDIA " + gpuName.Trim());
            }

            return("NVIDIA");
        }
コード例 #3
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();
        }
コード例 #4
0
ファイル: NvidiaGroup.cs プロジェクト: NitramAnaip/deCo2me
        public NvidiaGroup(ISettings settings)
        {
            if (!NvApi.IsAvailable)
            {
                return;
            }


            _report.AppendLine("NvApi");
            _report.AppendLine();

            if (NvApi.NvAPI_GetInterfaceVersionString(out string version) == NvApi.NvStatus.OK)
            {
                _report.Append("Version: ");
                _report.AppendLine(version);
            }

            NvApi.NvPhysicalGpuHandle[] handles = new NvApi.NvPhysicalGpuHandle[NvApi.MAX_PHYSICAL_GPUS];
            if (NvApi.NvAPI_EnumPhysicalGPUs == null)
            {
                _report.AppendLine("Error: NvAPI_EnumPhysicalGPUs not available");
                _report.AppendLine();
                return;
            }

            NvApi.NvStatus status = NvApi.NvAPI_EnumPhysicalGPUs(handles, out int count);
            if (status != NvApi.NvStatus.OK)
            {
                _report.AppendLine("Status: " + status);
                _report.AppendLine();
                return;
            }

            IDictionary <NvApi.NvPhysicalGpuHandle, NvApi.NvDisplayHandle> displayHandles = new Dictionary <NvApi.NvPhysicalGpuHandle, NvApi.NvDisplayHandle>();

            if (NvApi.NvAPI_EnumNvidiaDisplayHandle != null && NvApi.NvAPI_GetPhysicalGPUsFromDisplay != null)
            {
                status = NvApi.NvStatus.OK;
                int i = 0;
                while (status == NvApi.NvStatus.OK)
                {
                    NvApi.NvDisplayHandle displayHandle = new NvApi.NvDisplayHandle();
                    status = NvApi.NvAPI_EnumNvidiaDisplayHandle(i, ref displayHandle);
                    i++;

                    if (status == NvApi.NvStatus.OK)
                    {
                        NvApi.NvPhysicalGpuHandle[] handlesFromDisplay = new NvApi.NvPhysicalGpuHandle[NvApi.MAX_PHYSICAL_GPUS];
                        if (NvApi.NvAPI_GetPhysicalGPUsFromDisplay(displayHandle, handlesFromDisplay, out uint countFromDisplay) == NvApi.NvStatus.OK)
                        {
                            for (int j = 0; j < countFromDisplay; j++)
                            {
                                if (!displayHandles.ContainsKey(handlesFromDisplay[j]))
                                {
                                    displayHandles.Add(handlesFromDisplay[j], displayHandle);
                                }
                            }
                        }
                    }
                }
            }

            _report.Append("Number of GPUs: ");
            _report.AppendLine(count.ToString(CultureInfo.InvariantCulture));

            for (int i = 0; i < count; i++)
            {
                displayHandles.TryGetValue(handles[i], out NvApi.NvDisplayHandle displayHandle);
                _hardware.Add(new NvidiaGpu(i, handles[i], displayHandle, settings));
            }

            _report.AppendLine();
        }
コード例 #5
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();
        }