forked from gpustack/fastfetch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Xiaodong Ye <[email protected]>
- Loading branch information
1 parent
884fd09
commit 4422cf1
Showing
7 changed files
with
381 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
#include "gpu_driver_specific.h" | ||
|
||
#include "common/library.h" | ||
#include "mtml.h" | ||
|
||
struct FFMtmlData | ||
{ | ||
FF_LIBRARY_SYMBOL(mtmlDeviceGetBrand) | ||
FF_LIBRARY_SYMBOL(mtmlDeviceGetIndex) | ||
FF_LIBRARY_SYMBOL(mtmlDeviceGetName) | ||
FF_LIBRARY_SYMBOL(mtmlDeviceGetPciInfo) | ||
FF_LIBRARY_SYMBOL(mtmlDeviceGetUUID) | ||
FF_LIBRARY_SYMBOL(mtmlDeviceInitGpu) | ||
FF_LIBRARY_SYMBOL(mtmlDeviceInitMemory) | ||
FF_LIBRARY_SYMBOL(mtmlGpuGetMaxClock) | ||
FF_LIBRARY_SYMBOL(mtmlGpuGetTemperature) | ||
FF_LIBRARY_SYMBOL(mtmlGpuGetUtilization) | ||
FF_LIBRARY_SYMBOL(mtmlLibraryCountDevice) | ||
FF_LIBRARY_SYMBOL(mtmlLibraryInitDeviceByIndex) | ||
FF_LIBRARY_SYMBOL(mtmlLibraryInitDeviceByPciSbdf) | ||
FF_LIBRARY_SYMBOL(mtmlLibraryInitSystem) | ||
FF_LIBRARY_SYMBOL(mtmlMemoryGetTotal) | ||
FF_LIBRARY_SYMBOL(mtmlMemoryGetUsed) | ||
FF_LIBRARY_SYMBOL(mtmlMemoryGetUtilization) | ||
|
||
bool inited; | ||
MtmlLibrary *lib; | ||
MtmlSystem *sys; | ||
} mtmlData; | ||
|
||
const char *ffDetectMthreadsGpuCount(uint32_t *result, const char *soName) | ||
{ | ||
#ifndef FF_DISABLE_DLOPEN | ||
|
||
if (!mtmlData.inited) | ||
{ | ||
mtmlData.inited = true; | ||
FF_LIBRARY_LOAD(libmtml, NULL, "dlopen mtml failed", soName, 1); | ||
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libmtml, mtmlLibraryInit) | ||
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libmtml, mtmlLibraryShutDown) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceGetBrand) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceGetIndex) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceGetName) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceGetPciInfo) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceGetUUID) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceInitGpu) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceInitMemory) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlGpuGetMaxClock) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlGpuGetTemperature) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlGpuGetUtilization) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlLibraryCountDevice) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlLibraryInitDeviceByIndex) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlLibraryInitDeviceByPciSbdf) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlLibraryInitSystem) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlMemoryGetTotal) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlMemoryGetUsed) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlMemoryGetUtilization) | ||
|
||
if (ffmtmlLibraryInit(&mtmlData.lib) != MTML_SUCCESS) | ||
{ | ||
mtmlData.ffmtmlLibraryInitSystem = NULL; | ||
return "mtmlLibraryInit failed"; | ||
} | ||
if (mtmlData.ffmtmlLibraryInitSystem(mtmlData.lib, &mtmlData.sys) != MTML_SUCCESS) | ||
{ | ||
mtmlData.ffmtmlLibraryInitSystem = NULL; | ||
return "mtmlLibraryInitSystem failed"; | ||
} | ||
atexit((void *)ffmtmlLibraryShutDown); | ||
libmtml = NULL; // don't close mtml | ||
} | ||
|
||
if (mtmlData.ffmtmlLibraryInitSystem == NULL) | ||
return "loading mtml library failed"; | ||
|
||
uint32_t count; | ||
if (mtmlData.ffmtmlLibraryCountDevice(mtmlData.lib, &count) != MTML_SUCCESS) | ||
return "mtmlLibraryCountDevice() failed"; | ||
|
||
*result = count; | ||
return NULL; | ||
#else | ||
|
||
FF_UNUSED(cond, result, soName); | ||
return "dlopen is disabled"; | ||
|
||
#endif | ||
} | ||
|
||
const char *ffDetectMthreadsGpuInfo(const FFGpuDriverCondition *cond, FFGpuDriverResult result, const char *soName) | ||
{ | ||
#ifndef FF_DISABLE_DLOPEN | ||
|
||
if (!mtmlData.inited) | ||
{ | ||
mtmlData.inited = true; | ||
FF_LIBRARY_LOAD(libmtml, NULL, "dlopen mtml failed", soName, 1); | ||
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libmtml, mtmlLibraryInit) | ||
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libmtml, mtmlLibraryShutDown) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceGetBrand) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceGetIndex) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceGetName) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceGetPciInfo) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceGetUUID) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceInitGpu) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlDeviceInitMemory) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlGpuGetMaxClock) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlGpuGetTemperature) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlGpuGetUtilization) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlLibraryCountDevice) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlLibraryInitDeviceByIndex) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlLibraryInitDeviceByPciSbdf) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlLibraryInitSystem) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlMemoryGetTotal) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlMemoryGetUsed) | ||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(libmtml, mtmlData, mtmlMemoryGetUtilization) | ||
|
||
if (ffmtmlLibraryInit(&mtmlData.lib) != MTML_SUCCESS) | ||
{ | ||
mtmlData.ffmtmlLibraryInitSystem = NULL; | ||
return "mtmlLibraryInit failed"; | ||
} | ||
if (mtmlData.ffmtmlLibraryInitSystem(mtmlData.lib, &mtmlData.sys) != MTML_SUCCESS) | ||
{ | ||
mtmlData.ffmtmlLibraryInitSystem = NULL; | ||
return "mtmlLibraryInitSystem failed"; | ||
} | ||
atexit((void *)ffmtmlLibraryShutDown); | ||
libmtml = NULL; // don't close mtml | ||
} | ||
|
||
if (mtmlData.ffmtmlLibraryInitSystem == NULL) | ||
return "loading mtml library failed"; | ||
|
||
MtmlDevice *device = NULL; | ||
if (cond->type & FF_GPU_DRIVER_CONDITION_TYPE_BUS_ID) | ||
{ | ||
char pciBusIdStr[32]; | ||
snprintf(pciBusIdStr, sizeof(pciBusIdStr) - 1, "%04x:%02x:%02x.%d", cond->pciBusId.domain, cond->pciBusId.bus, cond->pciBusId.device, cond->pciBusId.func); | ||
|
||
MtmlReturn ret = mtmlData.ffmtmlLibraryInitDeviceByPciSbdf(mtmlData.lib, pciBusIdStr, &device); | ||
if (ret != MTML_SUCCESS) | ||
return "mtmlLibraryInitDeviceByPciSbdf() failed"; | ||
} | ||
else if (cond->type & FF_GPU_DRIVER_CONDITION_TYPE_DEVICE_ID) | ||
{ | ||
uint32_t count; | ||
if (mtmlData.ffmtmlLibraryCountDevice(mtmlData.lib, &count) != MTML_SUCCESS) | ||
return "mtmlLibraryCountDevice() failed"; | ||
|
||
for (uint32_t i = 0; i < count; i++, device = NULL) | ||
{ | ||
if (mtmlData.ffmtmlLibraryInitDeviceByIndex(mtmlData.lib, i, &device) != MTML_SUCCESS) | ||
continue; | ||
|
||
MtmlPciInfo pciInfo; | ||
if (mtmlData.ffmtmlDeviceGetPciInfo(device, &pciInfo) != MTML_SUCCESS) | ||
continue; | ||
|
||
if (pciInfo.pciDeviceId != ((cond->pciDeviceId.deviceId << 16u) | cond->pciDeviceId.vendorId) || | ||
pciInfo.pciSubsystemId != cond->pciDeviceId.subSystemId) | ||
continue; | ||
|
||
break; | ||
} | ||
if (!device) | ||
return "Device not found"; | ||
} | ||
else | ||
{ | ||
return "Unknown condition type"; | ||
} | ||
|
||
MtmlBrandType brand; | ||
if (mtmlData.ffmtmlDeviceGetBrand(device, &brand) == MTML_SUCCESS) | ||
{ | ||
switch (brand) | ||
{ | ||
case MTML_BRAND_MTT: | ||
*result.type = FF_GPU_TYPE_DISCRETE; | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
if (result.temp) | ||
{ | ||
MtmlGpu *gpu = NULL; | ||
if (mtmlData.ffmtmlDeviceInitGpu(device, &gpu) == MTML_SUCCESS) | ||
{ | ||
uint32_t value; | ||
if (mtmlData.ffmtmlGpuGetTemperature(gpu, &value) == MTML_SUCCESS) | ||
*result.temp = value; | ||
} | ||
} | ||
|
||
if (result.memory) | ||
{ | ||
MtmlMemory *mem = NULL; | ||
if (mtmlData.ffmtmlDeviceInitMemory(device, &mem) == MTML_SUCCESS) | ||
{ | ||
unsigned long long total; | ||
if (mtmlData.ffmtmlMemoryGetTotal(mem, &total) == MTML_SUCCESS) | ||
result.memory->total = total; | ||
|
||
unsigned long long used; | ||
if (mtmlData.ffmtmlMemoryGetUsed(mem, &used) == MTML_SUCCESS) | ||
result.memory->used = used; | ||
} | ||
} | ||
|
||
if (result.frequency) | ||
{ | ||
MtmlGpu *gpu = NULL; | ||
if (mtmlData.ffmtmlDeviceInitGpu(device, &gpu) == MTML_SUCCESS) | ||
{ | ||
uint32_t clockMHz; | ||
if (mtmlData.ffmtmlGpuGetMaxClock(gpu, &clockMHz) == MTML_SUCCESS) | ||
*result.frequency = clockMHz / 1000.; | ||
} | ||
} | ||
|
||
return NULL; | ||
|
||
#else | ||
|
||
FF_UNUSED(cond, result, soName); | ||
return "dlopen is disabled"; | ||
|
||
#endif | ||
} |
Oops, something went wrong.