From df4f646b70be0b5afa5ba94bcb04ac8522248f82 Mon Sep 17 00:00:00 2001 From: icex2 Date: Wed, 19 Feb 2025 19:40:20 +0100 Subject: [PATCH] =?UTF-8?q?feat(nvgpu):=20Add=20=E2=80=94overwrite-exists?= =?UTF-8?q?=20flag=20for=20profile=20creation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When running this in a script on startup to ensure whatever configuration is required to be created is created, this avoids several commands to first check if the profile exists, delete it, then re-create it. --- src/main/nvgpu/main.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/nvgpu/main.c b/src/main/nvgpu/main.c index b7052bc..a35cc9a 100644 --- a/src/main/nvgpu/main.c +++ b/src/main/nvgpu/main.c @@ -333,7 +333,7 @@ static bool _nv_info(const nv_api_t *nv_api) return true; } -static bool _profile_create(const nv_api_t *nv_api, const char *profile_name) +static bool _profile_create(const nv_api_t *nv_api, const char *profile_name, bool overwrite_exists) { NvDRSSessionHandle session; NVDRS_PROFILE profile; @@ -358,6 +358,26 @@ static bool _profile_create(const nv_api_t *nv_api, const char *profile_name) status = nv_api->NvAPI_DRS_CreateProfile(session, &profile, &profile_handle); + if (status == NVAPI_PROFILE_NAME_IN_USE && overwrite_exists) { + printfln_err("Profile %s already exists, enabled overwrite existing...", profile_name); + + if (!_profile_get(nv_api, profile_name, session, &profile_handle)) { + _session_destroy(nv_api, session); + + return false; + } + + status = nv_api->NvAPI_DRS_DeleteProfile(session, profile_handle); + + if (status != NVAPI_OK) { + PRINT_ERR_WITH_NVAPI_MESSAGE(status, "ERROR: Deleting existing profile"); + return false; + } + + // Retry + status = nv_api->NvAPI_DRS_CreateProfile(session, &profile, &profile_handle); + } + if (status != NVAPI_OK) { PRINT_ERR_WITH_NVAPI_MESSAGE(status, "ERROR: Creating driver profile"); @@ -1294,6 +1314,7 @@ static bool _cmd_nv_info(const nv_api_t *nv_api) static bool _cmd_profile_create(const nv_api_t *nv_api, int argc, char **argv) { const char *profile_name; + bool overwrite_exists; if (argc < 1) { _print_synopsis(); @@ -1303,7 +1324,15 @@ static bool _cmd_profile_create(const nv_api_t *nv_api, int argc, char **argv) profile_name = argv[0]; - return _profile_create(nv_api, profile_name); + overwrite_exists = false; + + for (int i = 1; i < argc; i++) { + if (!strcmp(argv[i], "--overwrite-exists")) { + overwrite_exists = true; + } + } + + return _profile_create(nv_api, profile_name, overwrite_exists); } static bool _cmd_profile_delete(const nv_api_t *nv_api, int argc, char **argv)