From e044eb974b87e898a90aa36afdb635bf4790a48a Mon Sep 17 00:00:00 2001 From: Krim Date: Fri, 31 Jul 2026 00:13:12 +0200 Subject: [PATCH] fix: resolve LIBCUDA_LOG_LEVEL on first use so early messages honour it g_log_level defaults to 2 and both LOG_WARN and LOG_MSG fire at >= 2, but the variable is only read by log_utils_init(), which preInit() reaches after the dlsym hook and after the Initializing message have already logged. Those messages therefore ignore LIBCUDA_LOG_LEVEL entirely, including the value 1 that the device plugin injects into every container. Resolve the level on first use through pthread_once instead of relying on log_utils_init() having run. Behaviour is unchanged when the variable is unset; LOG_ERROR keeps its unconditional path. Signed-off-by: Krim --- src/include/log_utils.h | 22 ++++++++++++++-------- src/log_utils.c | 13 +++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/include/log_utils.h b/src/include/log_utils.h index 630ee5a9..cbfb2b8b 100755 --- a/src/include/log_utils.h +++ b/src/include/log_utils.h @@ -22,27 +22,33 @@ extern int g_log_level; /* Call once during early initialization to cache LIBCUDA_LOG_LEVEL. */ void log_utils_init(void); +/* + * Level accessor used by the macros below. Resolves LIBCUDA_LOG_LEVEL on first + * use, so messages emitted before log_utils_init() runs honour it as well. + */ +int log_level(void); + #ifdef FILEDEBUG #define LOG_DEBUG(msg, ...) { \ - if (g_log_level >= 4) {\ + if (log_level() >= 4) {\ if (fp1==NULL) fp1 = fopen ("/tmp/vgpulog", "a"); \ fprintf(fp1, "[HAMI-core Debug(%d:%ld:%s:%d)]: "msg"\n",getpid(),pthread_self(),basename(__FILE__),__LINE__,##__VA_ARGS__); \ }\ } #define LOG_INFO(msg, ...) { \ - if (g_log_level >= 3) {\ + if (log_level() >= 3) {\ if (fp1==NULL) fp1 = fopen ("/tmp/vgpulog", "a"); \ fprintf(fp1, "[HAMI-core Info(%d:%ld:%s:%d)]: "msg"\n", getpid(),pthread_self(),basename(__FILE__),__LINE__,##__VA_ARGS__); \ }\ } #define LOG_WARN(msg, ...) { \ - if (g_log_level >= 2) {\ + if (log_level() >= 2) {\ if (fp1==NULL) fp1 = fopen ("/tmp/vgpulog", "a"); \ fprintf(fp1, "[HAMI-core Warn(%d:%ld:%s:%d)]: "msg"\n", getpid(),pthread_self(),basename(__FILE__),__LINE__,##__VA_ARGS__); \ }\ } #define LOG_MSG(msg, ...) { \ - if (g_log_level >= 2) {\ + if (log_level() >= 2) {\ if (fp1==NULL) fp1 = fopen ("/tmp/vgpulog", "a"); \ fprintf(fp1, "[HAMI-core Msg(%d:%ld:%s:%d)]: "msg"\n", getpid(),pthread_self(),basename(__FILE__),__LINE__,##__VA_ARGS__); \ }\ @@ -53,22 +59,22 @@ void log_utils_init(void); } #else #define LOG_DEBUG(msg, ...) { \ - if (g_log_level >= 4) {\ + if (log_level() >= 4) {\ fprintf(stderr, "[HAMI-core Debug(%d:%ld:%s:%d)]: "msg"\n",getpid(),pthread_self(),basename(__FILE__),__LINE__,##__VA_ARGS__); \ }\ } #define LOG_INFO(msg, ...) { \ - if (g_log_level >= 3) {\ + if (log_level() >= 3) {\ fprintf(stderr, "[HAMI-core Info(%d:%ld:%s:%d)]: "msg"\n", getpid(),pthread_self(),basename(__FILE__),__LINE__,##__VA_ARGS__); \ }\ } #define LOG_WARN(msg, ...) { \ - if (g_log_level >= 2) {\ + if (log_level() >= 2) {\ fprintf(stderr, "[HAMI-core Warn(%d:%ld:%s:%d)]: "msg"\n", getpid(),pthread_self(),basename(__FILE__),__LINE__,##__VA_ARGS__); \ }\ } #define LOG_MSG(msg, ...) { \ - if (g_log_level >= 2) {\ + if (log_level() >= 2) {\ fprintf(stderr, "[HAMI-core Msg(%d:%ld:%s:%d)]: "msg"\n", getpid(),pthread_self(),basename(__FILE__),__LINE__,##__VA_ARGS__); \ }\ } diff --git a/src/log_utils.c b/src/log_utils.c index 998cdc01..08005d42 100644 --- a/src/log_utils.c +++ b/src/log_utils.c @@ -1,5 +1,6 @@ #include #include +#include /* * Cached log level, read once from LIBCUDA_LOG_LEVEL by log_utils_init(). @@ -9,6 +10,8 @@ int g_log_level = 2; FILE *fp1 = NULL; +static pthread_once_t log_level_once = PTHREAD_ONCE_INIT; + void log_utils_init(void) { const char *env = getenv("LIBCUDA_LOG_LEVEL"); if (env != NULL) { @@ -16,3 +19,13 @@ void log_utils_init(void) { } /* else: keep default of 2 (warn level) */ } + +/* + * The dlsym hook and preInit() log before preInit() reaches log_utils_init(), + * so reading the cached value directly ignores LIBCUDA_LOG_LEVEL for exactly + * those messages. Resolve on first use instead. + */ +int log_level(void) { + pthread_once(&log_level_once, log_utils_init); + return g_log_level; +}