From 29124d3af3d9c0499f3b2d34f7008c8cab078a2d Mon Sep 17 00:00:00 2001 From: Amber Lin Date: Mon, 26 Jun 2017 15:23:32 -0400 Subject: [PATCH] Introduce debug level to Thunk Existing Thunk has printf/fprintf in the code while normally libraries don't print any message. This patch introduces a print machenism similar to how the Linux kernel prints to console based on the log level. The default is not to print any message, but setting HSAKMT_DEBUG_LEVEL will enable the prints. Change-Id: Ic071e122d35a82260218e9914cde4815e69df742 Signed-off-by: Amber Lin [ROCm/ROCR-Runtime commit: ccfe73992997ad050cfd1f742ad2517f6a1ec7a2] --- projects/rocr-runtime/src/libhsakmt.h | 18 ++++++++++++++++++ projects/rocr-runtime/src/openclose.c | 23 ++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/projects/rocr-runtime/src/libhsakmt.h b/projects/rocr-runtime/src/libhsakmt.h index 0cc45ea200..7c4cc19f8c 100644 --- a/projects/rocr-runtime/src/libhsakmt.h +++ b/projects/rocr-runtime/src/libhsakmt.h @@ -64,6 +64,24 @@ extern int PAGE_SHIFT; #define BITMASK(n) (((n) < sizeof(1ULL) * CHAR_BIT ? (1ULL << (n)) : 0) - 1ULL) #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0])) +/* HSA Thunk logging usage */ +extern int hsakmt_debug_level; +#define hsakmt_print(level, fmt, ...) \ + do { if (level <= hsakmt_debug_level) fprintf(stderr, fmt, ##__VA_ARGS__); } while (0) +#define HSAKMT_DEBUG_LEVEL_DEFAULT -1 +#define HSAKMT_DEBUG_LEVEL_ERR 3 +#define HSAKMT_DEBUG_LEVEL_WARNING 4 +#define HSAKMT_DEBUG_LEVEL_INFO 6 +#define HSAKMT_DEBUG_LEVEL_DEBUG 7 +#define pr_err(fmt, ...) \ + hsakmt_print(HSAKMT_DEBUG_LEVEL_ERR, fmt, ##__VA_ARGS__) +#define pr_warn(fmt, ...) \ + hsakmt_print(HSAKMT_DEBUG_LEVEL_WARNING, fmt, ##__VA_ARGS__) +#define pr_info(fmt, ...) \ + hsakmt_print(HSAKMT_DEBUG_LEVEL_INFO, fmt, ##__VA_ARGS__) +#define pr_debug(fmt, ...) \ + hsakmt_print(HSAKMT_DEBUG_LEVEL_DEBUG, fmt, ##__VA_ARGS__) + enum asic_family_type { CHIP_KAVERI = 0, CHIP_HAWAII, diff --git a/projects/rocr-runtime/src/openclose.c b/projects/rocr-runtime/src/openclose.c index 5e8b5e1aa4..204b31c360 100644 --- a/projects/rocr-runtime/src/openclose.c +++ b/projects/rocr-runtime/src/openclose.c @@ -25,6 +25,7 @@ #include "libhsakmt.h" +#include #include #include #include @@ -38,7 +39,7 @@ static const char kfd_device_name[] = "/dev/kfd"; static const char tmp_file[] = "/var/lock/.amd_hsa_thunk_lock"; int amd_hsa_thunk_lock_fd; static pid_t parent_pid = -1; - +int hsakmt_debug_level; static bool is_forked_child(void) { @@ -76,6 +77,25 @@ static inline void init_page_size(void) PAGE_SHIFT = ffs(PAGE_SIZE) - 1; } +/* Normally libraries don't print messages. For debugging purpose, we'll + * print messages if an environment variable, HSAKMT_DEBUG_LEVEL, is set. + */ +static void init_debug_level(void) +{ + char *envvar; + int debug_level; + + hsakmt_debug_level = HSAKMT_DEBUG_LEVEL_DEFAULT; + + envvar = getenv("HSAKMT_DEBUG_LEVEL"); + if (envvar) { + debug_level = atoi(envvar); + if (debug_level >= HSAKMT_DEBUG_LEVEL_ERR && + debug_level <= HSAKMT_DEBUG_LEVEL_DEBUG) + hsakmt_debug_level = debug_level; + } +} + HSAKMT_STATUS HSAKMTAPI hsaKmtOpenKFD(void) { HSAKMT_STATUS result; @@ -93,6 +113,7 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtOpenKFD(void) clear_after_fork(); if (kfd_open_count == 0) { + init_debug_level(); amd_hsa_thunk_lock_fd = 0; fd = open(kfd_device_name, O_RDWR | O_CLOEXEC);