2023-03-26 20:42:18 +08:00
|
|
|
#include "common/bar.h"
|
|
|
|
|
#include "common/parsing.h"
|
|
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2023-03-26 20:42:18 +08:00
|
|
|
#include "detection/host/host.h"
|
|
|
|
|
#include "detection/gpu/gpu.h"
|
|
|
|
|
#include "modules/gpu/gpu.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-03-26 20:42:18 +08:00
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#define FF_GPU_NUM_FORMAT_ARGS 6
|
|
|
|
|
|
|
|
|
|
static void printGPUResult(FFinstance* instance, FFGPUOptions* options, uint8_t index, const FFGPUResult* gpu)
|
|
|
|
|
{
|
|
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
2023-06-18 12:32:47 +08:00
|
|
|
ffPrintLogoAndKey(instance, FF_GPU_MODULE_NAME, index, &options->moduleArgs.key, &options->moduleArgs.keyColor);
|
2023-03-26 20:42:18 +08:00
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY output = ffStrbufCreateA(gpu->vendor.length + 1 + gpu->name.length);
|
2023-03-26 20:42:18 +08:00
|
|
|
|
|
|
|
|
if(gpu->vendor.length > 0 && !ffStrbufStartsWith(&gpu->name, &gpu->vendor))
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppend(&output, &gpu->vendor);
|
|
|
|
|
ffStrbufAppendC(&output, ' ');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffStrbufAppend(&output, &gpu->name);
|
|
|
|
|
|
|
|
|
|
if(gpu->coreCount != FF_GPU_CORE_COUNT_UNSET)
|
|
|
|
|
ffStrbufAppendF(&output, " (%d)", gpu->coreCount);
|
|
|
|
|
|
|
|
|
|
if(gpu->temperature == gpu->temperature) //FF_GPU_TEMP_UNSET
|
|
|
|
|
ffStrbufAppendF(&output, " - %.1f°C", gpu->temperature);
|
|
|
|
|
|
|
|
|
|
if(gpu->dedicated.total != FF_GPU_VMEM_SIZE_UNSET && gpu->dedicated.total != 0)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&output, " (");
|
|
|
|
|
|
|
|
|
|
if(gpu->dedicated.used != FF_GPU_VMEM_SIZE_UNSET)
|
|
|
|
|
{
|
|
|
|
|
ffParseSize(gpu->dedicated.used, instance->config.binaryPrefixType, &output);
|
|
|
|
|
ffStrbufAppendS(&output, " / ");
|
|
|
|
|
}
|
|
|
|
|
ffParseSize(gpu->dedicated.total, instance->config.binaryPrefixType, &output);
|
|
|
|
|
if(gpu->dedicated.used != FF_GPU_VMEM_SIZE_UNSET)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendS(&output, ", ");
|
|
|
|
|
ffAppendPercentNum(instance, &output, (uint8_t) (gpu->dedicated.used * 100 / gpu->dedicated.total), 50, 80, false);
|
|
|
|
|
}
|
|
|
|
|
ffStrbufAppendC(&output, ')');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffStrbufPutTo(&output, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const char* type;
|
|
|
|
|
if(gpu->type == FF_GPU_TYPE_INTEGRATED)
|
|
|
|
|
type = "Integrated";
|
|
|
|
|
else if(gpu->type == FF_GPU_TYPE_DISCRETE)
|
|
|
|
|
type = "Discrete";
|
|
|
|
|
else
|
|
|
|
|
type = "Unknown";
|
|
|
|
|
|
|
|
|
|
ffPrintFormat(instance, FF_GPU_MODULE_NAME, index, &options->moduleArgs, FF_GPU_NUM_FORMAT_ARGS, (FFformatarg[]){
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->vendor},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->name},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->driver},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_DOUBLE, &gpu->temperature},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_INT, &gpu->coreCount},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRING, type},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffPrintGPU(FFinstance* instance, FFGPUOptions* options)
|
|
|
|
|
{
|
2023-06-12 11:30:18 +08:00
|
|
|
FF_LIST_AUTO_DESTROY gpus = ffListCreate(sizeof (FFGPUResult));
|
|
|
|
|
const char* error = ffDetectGPU(instance, options, &gpus);
|
|
|
|
|
if (error)
|
|
|
|
|
{
|
|
|
|
|
ffPrintError(instance, FF_GPU_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-03-26 20:42:18 +08:00
|
|
|
|
|
|
|
|
FF_LIST_AUTO_DESTROY selectedGPUs;
|
2023-06-12 11:30:18 +08:00
|
|
|
ffListInitA(&selectedGPUs, sizeof(const FFGPUResult*), gpus.length);
|
2023-03-26 20:42:18 +08:00
|
|
|
|
2023-06-12 11:30:18 +08:00
|
|
|
FF_LIST_FOR_EACH(FFGPUResult, gpu, gpus)
|
2023-03-26 20:42:18 +08:00
|
|
|
{
|
|
|
|
|
if(gpu->type == FF_GPU_TYPE_INTEGRATED && options->hideType == FF_GPU_TYPE_INTEGRATED)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if(gpu->type == FF_GPU_TYPE_DISCRETE && options->hideType == FF_GPU_TYPE_DISCRETE)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
* (const FFGPUResult**) ffListAdd(&selectedGPUs) = gpu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < selectedGPUs.length; i++)
|
|
|
|
|
printGPUResult(instance, options, selectedGPUs.length == 1 ? 0 : (uint8_t) (i + 1), * (const FFGPUResult**) ffListGet(&selectedGPUs, i));
|
|
|
|
|
|
|
|
|
|
if(selectedGPUs.length == 0)
|
|
|
|
|
ffPrintError(instance, FF_GPU_MODULE_NAME, 0, &options->moduleArgs, "No GPUs found");
|
2023-06-12 11:30:18 +08:00
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFGPUResult, gpu, gpus)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufDestroy(&gpu->vendor);
|
|
|
|
|
ffStrbufDestroy(&gpu->name);
|
|
|
|
|
ffStrbufDestroy(&gpu->driver);
|
|
|
|
|
}
|
2023-03-26 20:42:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffInitGPUOptions(FFGPUOptions* options)
|
|
|
|
|
{
|
|
|
|
|
options->moduleName = FF_GPU_MODULE_NAME;
|
|
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
|
|
|
|
|
|
|
|
|
options->forceVulkan = false;
|
|
|
|
|
options->temp = false;
|
|
|
|
|
options->hideType = FF_GPU_TYPE_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ffParseGPUCommandOptions(FFGPUOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_GPU_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "force-vulkan"))
|
2023-03-26 20:42:18 +08:00
|
|
|
{
|
|
|
|
|
options->forceVulkan = ffOptionParseBoolean(value);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "temp"))
|
2023-03-26 20:42:18 +08:00
|
|
|
{
|
|
|
|
|
options->temp = ffOptionParseBoolean(value);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(subKey, "hide-type"))
|
2023-03-26 20:42:18 +08:00
|
|
|
{
|
|
|
|
|
options->hideType = (FFGPUType) ffOptionParseEnum(key, value, (FFKeyValuePair[]) {
|
|
|
|
|
{ "none", FF_GPU_TYPE_UNKNOWN },
|
|
|
|
|
{ "intergrated", FF_GPU_TYPE_INTEGRATED },
|
|
|
|
|
{ "discrete", FF_GPU_TYPE_DISCRETE },
|
|
|
|
|
{},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyGPUOptions(FFGPUOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 15:01:54 +08:00
|
|
|
void ffParseGPUJsonObject(FFinstance* instance, yyjson_val* module)
|
2023-03-26 20:42:18 +08:00
|
|
|
{
|
|
|
|
|
FFGPUOptions __attribute__((__cleanup__(ffDestroyGPUOptions))) options;
|
|
|
|
|
ffInitGPUOptions(&options);
|
|
|
|
|
|
|
|
|
|
if (module)
|
|
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
2023-03-26 20:42:18 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
2023-06-19 15:21:49 +08:00
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
2023-06-10 15:01:54 +08:00
|
|
|
continue;
|
|
|
|
|
|
2023-03-26 20:42:18 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
|
|
|
|
continue;
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "temp"))
|
2023-03-26 20:42:18 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
options.temp = yyjson_get_bool(val);
|
2023-03-26 20:42:18 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "forceVulkan"))
|
2023-03-26 20:42:18 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
options.forceVulkan = yyjson_get_bool(val);
|
2023-03-26 20:42:18 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "hideType"))
|
2023-03-26 20:42:18 +08:00
|
|
|
{
|
|
|
|
|
int value;
|
|
|
|
|
const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) {
|
|
|
|
|
{ "none", FF_GPU_TYPE_UNKNOWN },
|
|
|
|
|
{ "intergrated", FF_GPU_TYPE_INTEGRATED },
|
|
|
|
|
{ "discrete", FF_GPU_TYPE_DISCRETE },
|
|
|
|
|
{},
|
|
|
|
|
});
|
|
|
|
|
if (error)
|
|
|
|
|
ffPrintError(instance, FF_GPU_MODULE_NAME, 0, &options.moduleArgs, "Invalid %s value: %s", key, error);
|
|
|
|
|
else
|
|
|
|
|
options.hideType = (FFGPUType) value;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffPrintError(instance, FF_GPU_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffPrintGPU(instance, &options);
|
|
|
|
|
}
|