Welcome to mirror list, hosted at ThFree Co, Russian Federation.

nvml.cpp « sysinfo « src - github.com/jp7677/dxvk-nvapi.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7bea09d588be0ea819d255153523477fc7763129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "nvml.h"
#include "../util/util_string.h"
#include "../util/util_log.h"

namespace dxvk {
    Nvml::Nvml() {
        const auto nvmlModuleName = "nvml.dll";
        m_nvmlModule = ::LoadLibraryA(nvmlModuleName);
        if (m_nvmlModule == nullptr) {
            auto lastError = ::GetLastError();
            if (lastError != ERROR_MOD_NOT_FOUND) // Ignore library not found
                log::write(str::format("Loading ", nvmlModuleName, " failed with error code: ", lastError));

            return;
        }

#define GETPROCADDR(x) m_##x = GetProcAddress<decltype(&x)>(#x)

        GETPROCADDR(nvmlInit_v2);
        GETPROCADDR(nvmlShutdown);
        GETPROCADDR(nvmlErrorString);
        GETPROCADDR(nvmlDeviceGetHandleByPciBusId_v2);
        GETPROCADDR(nvmlDeviceGetPciInfo_v3);
        GETPROCADDR(nvmlDeviceGetClockInfo);
        GETPROCADDR(nvmlDeviceGetTemperature);
        GETPROCADDR(nvmlDeviceGetThermalSettings);
        GETPROCADDR(nvmlDeviceGetPerformanceState);
        GETPROCADDR(nvmlDeviceGetUtilizationRates);
        GETPROCADDR(nvmlDeviceGetVbiosVersion);
        GETPROCADDR(nvmlDeviceGetBusType);
        GETPROCADDR(nvmlDeviceGetDynamicPstatesInfo);

#undef GETPROCADDR

        if (m_nvmlInit_v2 == nullptr
            || m_nvmlShutdown == nullptr
            || m_nvmlErrorString == nullptr
            || m_nvmlDeviceGetHandleByPciBusId_v2 == nullptr)
            log::write(str::format("NVML loaded but initialization failed"));
        else {
            auto result = m_nvmlInit_v2();
            if (result == NVML_SUCCESS)
                return;

            log::write(str::format("NVML loaded but initialization failed with error: ", ErrorString(result)));
        }

        ::FreeLibrary(m_nvmlModule);
        m_nvmlModule = nullptr;
    }

    Nvml::~Nvml() {
        if (m_nvmlModule == nullptr)
            return;

        m_nvmlShutdown();
        ::FreeLibrary(m_nvmlModule);
        m_nvmlModule = nullptr;
    }

    bool Nvml::IsAvailable() const {
        return m_nvmlModule != nullptr;
    }

    const char* Nvml::ErrorString(nvmlReturn_t result) const {
        return m_nvmlErrorString(result);
    }

    nvmlReturn_t Nvml::DeviceGetHandleByPciBusId_v2(const char* pciBusId, nvmlDevice_t* device) const {
        return m_nvmlDeviceGetHandleByPciBusId_v2(pciBusId, device);
    }

    nvmlReturn_t Nvml::DeviceGetPciInfo_v3(nvmlDevice_t device, nvmlPciInfo_t* pci) const {
        return m_nvmlDeviceGetPciInfo_v3
            ? m_nvmlDeviceGetPciInfo_v3(device, pci)
            : NVML_ERROR_FUNCTION_NOT_FOUND;
    }

    nvmlReturn_t Nvml::DeviceGetClockInfo(nvmlDevice_t device, nvmlClockType_t type, unsigned int* clock) const {
        return m_nvmlDeviceGetClockInfo
            ? m_nvmlDeviceGetClockInfo(device, type, clock)
            : NVML_ERROR_FUNCTION_NOT_FOUND;
    }

    nvmlReturn_t Nvml::DeviceGetTemperature(nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int* temp) const {
        return m_nvmlDeviceGetTemperature
            ? m_nvmlDeviceGetTemperature(device, sensorType, temp)
            : NVML_ERROR_FUNCTION_NOT_FOUND;
    }

    nvmlReturn_t Nvml::DeviceGetThermalSettings(nvmlDevice_t device, unsigned int sensorIndex, nvmlGpuThermalSettings_t* pThermalSettings) const {
        return m_nvmlDeviceGetThermalSettings
            ? m_nvmlDeviceGetThermalSettings(device, sensorIndex, pThermalSettings)
            : NVML_ERROR_FUNCTION_NOT_FOUND;
    }

    nvmlReturn_t Nvml::DeviceGetPerformanceState(nvmlDevice_t device, nvmlPstates_t* pState) const {
        return m_nvmlDeviceGetPerformanceState
            ? m_nvmlDeviceGetPerformanceState(device, pState)
            : NVML_ERROR_FUNCTION_NOT_FOUND;
    }

    nvmlReturn_t Nvml::DeviceGetUtilizationRates(nvmlDevice_t device, nvmlUtilization_t* utilization) const {
        return m_nvmlDeviceGetUtilizationRates
            ? m_nvmlDeviceGetUtilizationRates(device, utilization)
            : NVML_ERROR_FUNCTION_NOT_FOUND;
    }

    nvmlReturn_t Nvml::DeviceGetVbiosVersion(nvmlDevice_t device, char* version, unsigned int length) const {
        return m_nvmlDeviceGetVbiosVersion
            ? m_nvmlDeviceGetVbiosVersion(device, version, length)
            : NVML_ERROR_FUNCTION_NOT_FOUND;
    }

    nvmlReturn_t Nvml::DeviceGetBusType(nvmlDevice_t device, nvmlBusType_t* type) const {
        return m_nvmlDeviceGetBusType
            ? m_nvmlDeviceGetBusType(device, type)
            : NVML_ERROR_FUNCTION_NOT_FOUND;
    }

    nvmlReturn_t Nvml::DeviceGetDynamicPstatesInfo(nvmlDevice_t device, nvmlGpuDynamicPstatesInfo_t* pDynamicPstatesInfo) const {
        return m_nvmlDeviceGetDynamicPstatesInfo
            ? m_nvmlDeviceGetDynamicPstatesInfo(device, pDynamicPstatesInfo)
            : NVML_ERROR_FUNCTION_NOT_FOUND;
    }

    NV_THERMAL_TARGET Nvml::ToNvThermalTarget(nvmlThermalTarget_t target) {
        static_assert(static_cast<int>(NVML_THERMAL_TARGET_NONE) == static_cast<int>(NVAPI_THERMAL_TARGET_NONE));
        static_assert(static_cast<int>(NVML_THERMAL_TARGET_GPU) == static_cast<int>(NVAPI_THERMAL_TARGET_GPU));
        static_assert(static_cast<int>(NVML_THERMAL_TARGET_MEMORY) == static_cast<int>(NVAPI_THERMAL_TARGET_MEMORY));
        static_assert(static_cast<int>(NVML_THERMAL_TARGET_POWER_SUPPLY) == static_cast<int>(NVAPI_THERMAL_TARGET_POWER_SUPPLY));
        static_assert(static_cast<int>(NVML_THERMAL_TARGET_BOARD) == static_cast<int>(NVAPI_THERMAL_TARGET_BOARD));
        static_assert(static_cast<int>(NVML_THERMAL_TARGET_VCD_BOARD) == static_cast<int>(NVAPI_THERMAL_TARGET_VCD_BOARD));
        static_assert(static_cast<int>(NVML_THERMAL_TARGET_VCD_INLET) == static_cast<int>(NVAPI_THERMAL_TARGET_VCD_INLET));
        static_assert(static_cast<int>(NVML_THERMAL_TARGET_VCD_OUTLET) == static_cast<int>(NVAPI_THERMAL_TARGET_VCD_OUTLET));
        static_assert(static_cast<int>(NVML_THERMAL_TARGET_ALL) == static_cast<int>(NVAPI_THERMAL_TARGET_ALL));
        static_assert(static_cast<int>(NVML_THERMAL_TARGET_UNKNOWN) == static_cast<int>(NVAPI_THERMAL_TARGET_UNKNOWN));
        return static_cast<NV_THERMAL_TARGET>(target);
    }

    NV_THERMAL_CONTROLLER Nvml::ToNvThermalController(nvmlThermalController_t controller) {
        switch (controller) {
            case NVML_THERMAL_CONTROLLER_NONE:
                return NVAPI_THERMAL_CONTROLLER_NONE;
            case NVML_THERMAL_CONTROLLER_GPU_INTERNAL:
                return NVAPI_THERMAL_CONTROLLER_GPU_INTERNAL;
            case NVML_THERMAL_CONTROLLER_ADM1032:
                return NVAPI_THERMAL_CONTROLLER_ADM1032;
            // case NVML_THERMAL_CONTROLLER_ADT7461:
            //     return NVAPI_THERMAL_CONTROLLER_ADT7461;
            case NVML_THERMAL_CONTROLLER_MAX6649:
                return NVAPI_THERMAL_CONTROLLER_MAX6649;
            case NVML_THERMAL_CONTROLLER_MAX1617:
                return NVAPI_THERMAL_CONTROLLER_MAX1617;
            case NVML_THERMAL_CONTROLLER_LM99:
                return NVAPI_THERMAL_CONTROLLER_LM99;
            case NVML_THERMAL_CONTROLLER_LM89:
                return NVAPI_THERMAL_CONTROLLER_LM89;
            case NVML_THERMAL_CONTROLLER_LM64:
                return NVAPI_THERMAL_CONTROLLER_LM64;
            // case NVML_THERMAL_CONTROLLER_G781:
            //     return NVAPI_THERMAL_CONTROLLER_G781;
            case NVML_THERMAL_CONTROLLER_ADT7473:
                return NVAPI_THERMAL_CONTROLLER_ADT7473;
            case NVML_THERMAL_CONTROLLER_SBMAX6649:
                return NVAPI_THERMAL_CONTROLLER_SBMAX6649;
            case NVML_THERMAL_CONTROLLER_VBIOSEVT:
                return NVAPI_THERMAL_CONTROLLER_VBIOSEVT;
            case NVML_THERMAL_CONTROLLER_OS:
                return NVAPI_THERMAL_CONTROLLER_OS;
            // case NVML_THERMAL_CONTROLLER_NVSYSCON_CANOAS:
            //     return NVAPI_THERMAL_CONTROLLER_NVSYSCON_CANOAS;
            // case NVML_THERMAL_CONTROLLER_NVSYSCON_E551:
            //     return NVAPI_THERMAL_CONTROLLER_NVSYSCON_E551;
            // case NVML_THERMAL_CONTROLLER_MAX6649R:
            //     return NVAPI_THERMAL_CONTROLLER_MAX6649R;
            // case NVML_THERMAL_CONTROLLER_ADT7473S:
            //     return NVAPI_THERMAL_CONTROLLER_ADT7473S;
            default:
                return NVAPI_THERMAL_CONTROLLER_UNKNOWN;
        }
    }

    NV_GPU_BUS_TYPE Nvml::ToNvGpuBusType(nvmlBusType_t type) {
        switch (type) {
            case NVML_BUS_TYPE_PCI:
                return NVAPI_GPU_BUS_TYPE_PCI;
            case NVML_BUS_TYPE_PCIE:
                return NVAPI_GPU_BUS_TYPE_PCI_EXPRESS;
            case NVML_BUS_TYPE_FPCI:
                return NVAPI_GPU_BUS_TYPE_FPCI;
            case NVML_BUS_TYPE_AGP:
                return NVAPI_GPU_BUS_TYPE_AGP;
            default:
                return NVAPI_GPU_BUS_TYPE_UNDEFINED;
        }
    }

    template <typename T>
    T Nvml::GetProcAddress(const char* name) {
        return reinterpret_cast<T>(reinterpret_cast<void*>(::GetProcAddress(m_nvmlModule, name)));
    }
}