From cdaf5e9281499099ac8237e4718dbcc7fc4fb92d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 7 Sep 2023 09:20:52 +0800 Subject: [PATCH] OpenCL: JSON format support --- src/detection/opencl/opencl.c | 3 +++ src/modules/opencl/opencl.c | 28 +++++++++++++++++++++++++++- src/modules/opencl/opencl.h | 1 + 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/detection/opencl/opencl.c b/src/detection/opencl/opencl.c index d8cb4a66b..0526de2e2 100644 --- a/src/detection/opencl/opencl.c +++ b/src/detection/opencl/opencl.c @@ -49,14 +49,17 @@ static const char* openCLHandleData(OpenCLData* data, FFOpenCLResult* result) if(ffStrStartsWithIgnCase(version, "OpenCL ")) versionPretty = version + strlen("OpenCL "); ffStrbufSetS(&result->version, versionPretty); + ffStrbufTrim(&result->version, ' '); ffStrbufEnsureFree(&result->device, 128); data->ffclGetDeviceInfo(deviceID, CL_DEVICE_NAME, result->device.allocated, result->device.chars, NULL); ffStrbufRecalculateLength(&result->device); + ffStrbufTrim(&result->device, ' '); ffStrbufEnsureFree(&result->vendor, 32); data->ffclGetDeviceInfo(deviceID, CL_DEVICE_VENDOR, result->vendor.allocated, result->vendor.chars, NULL); ffStrbufRecalculateLength(&result->vendor); + ffStrbufTrim(&result->vendor, ' '); return NULL; } diff --git a/src/modules/opencl/opencl.c b/src/modules/opencl/opencl.c index 4218dbfd2..afe3efa24 100644 --- a/src/modules/opencl/opencl.c +++ b/src/modules/opencl/opencl.c @@ -41,7 +41,7 @@ void ffPrintOpenCL(FFOpenCLOptions* options) void ffInitOpenCLOptions(FFOpenCLOptions* options) { - ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_OPENCL_MODULE_NAME, ffParseOpenCLCommandOptions, ffParseOpenCLJsonObject, ffPrintOpenCL, NULL); + ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_OPENCL_MODULE_NAME, ffParseOpenCLCommandOptions, ffParseOpenCLJsonObject, ffPrintOpenCL, ffGenerateOpenCLJson); ffOptionInitModuleArg(&options->moduleArgs); } @@ -76,3 +76,29 @@ void ffParseOpenCLJsonObject(FFOpenCLOptions* options, yyjson_val* module) ffPrintError(FF_OPENCL_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key); } } + +void ffGenerateOpenCLJson(FF_MAYBE_UNUSED FFOpenCLOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +{ + FFOpenCLResult opencl; + ffStrbufInit(&opencl.version); + ffStrbufInit(&opencl.device); + ffStrbufInit(&opencl.vendor); + + const char* error = ffDetectOpenCL(&opencl); + + if(error != NULL) + { + yyjson_mut_obj_add_str(doc, module, "error", error); + } + else + { + yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); + yyjson_mut_obj_add_strbuf(doc, obj, "version", &opencl.version); + yyjson_mut_obj_add_strbuf(doc, obj, "device", &opencl.device); + yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &opencl.vendor); + } + + ffStrbufDestroy(&opencl.version); + ffStrbufDestroy(&opencl.device); + ffStrbufDestroy(&opencl.vendor); +} diff --git a/src/modules/opencl/opencl.h b/src/modules/opencl/opencl.h index f5e2a32c2..592f453bf 100644 --- a/src/modules/opencl/opencl.h +++ b/src/modules/opencl/opencl.h @@ -9,3 +9,4 @@ void ffInitOpenCLOptions(FFOpenCLOptions* options); bool ffParseOpenCLCommandOptions(FFOpenCLOptions* options, const char* key, const char* value); void ffDestroyOpenCLOptions(FFOpenCLOptions* options); void ffParseOpenCLJsonObject(FFOpenCLOptions* options, yyjson_val* module); +void ffGenerateOpenCLJson(FFOpenCLOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module);