diff --git a/src/include/utils.h b/src/include/utils.h index 8a40012e..60b3fe07 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -5,6 +5,7 @@ #include +char* vgpu_getenv(const char* name); int try_lock_unified_lock(); int try_unlock_unified_lock(); diff --git a/src/libvgpu.c b/src/libvgpu.c index 49ef9878..a95b89aa 100644 --- a/src/libvgpu.c +++ b/src/libvgpu.c @@ -92,7 +92,7 @@ FUNC_ATTR_VISIBLE void* dlsym(void* handle, const char* symbol) { break; } } - char *path_search=getenv("CUDA_REDIRECT"); + char *path_search=vgpu_getenv("CUDA_REDIRECT"); if ((path_search!=NULL) && (strlen(path_search)>0)){ vgpulib = dlopen(path_search,RTLD_LAZY); }else{ diff --git a/src/log_utils.c b/src/log_utils.c index 998cdc01..79e9139a 100644 --- a/src/log_utils.c +++ b/src/log_utils.c @@ -1,5 +1,6 @@ #include #include +#include "include/utils.h" /* * Cached log level, read once from LIBCUDA_LOG_LEVEL by log_utils_init(). @@ -10,7 +11,7 @@ int g_log_level = 2; FILE *fp1 = NULL; void log_utils_init(void) { - const char *env = getenv("LIBCUDA_LOG_LEVEL"); + const char *env = vgpu_getenv("LIBCUDA_LOG_LEVEL"); if (env != NULL) { g_log_level = atoi(env); } diff --git a/src/multiprocess/multiprocess_memory_limit.c b/src/multiprocess/multiprocess_memory_limit.c index ff6cea1f..e6c3b28b 100755 --- a/src/multiprocess/multiprocess_memory_limit.c +++ b/src/multiprocess/multiprocess_memory_limit.c @@ -20,6 +20,7 @@ #include "include/process_utils.h" #include "include/memory_limit.h" #include "multiprocess/multiprocess_memory_limit.h" +#include "../include/utils.h" #ifndef SEM_WAIT_TIME @@ -93,7 +94,7 @@ void sig_swap_stub(int signo){ // get device memory from env size_t get_limit_from_env(const char* env_name) { - char* env_limit = getenv(env_name); + char* env_limit = vgpu_getenv(env_name); if (env_limit == NULL) { // fprintf(stderr, "No %s set in environment\n", env_name); return 0; @@ -1028,7 +1029,7 @@ void child_reinit_flag() { int set_active_oom_killer() { char *oom_killer_env; - oom_killer_env = getenv("ACTIVE_OOM_KILLER"); + oom_killer_env = vgpu_getenv("ACTIVE_OOM_KILLER"); if (oom_killer_env!=NULL){ if (strcmp(oom_killer_env,"false") == 0) return 0; @@ -1044,7 +1045,7 @@ int set_active_oom_killer() { int set_env_utilization_switch() { char *utilization_env; - utilization_env = getenv("GPU_CORE_UTILIZATION_POLICY"); + utilization_env = vgpu_getenv("GPU_CORE_UTILIZATION_POLICY"); if (utilization_env!=NULL){ if ((strcmp(utilization_env,"FORCE") ==0 ) || (strcmp(utilization_env,"force") ==0)) return 1; @@ -1074,7 +1075,7 @@ void try_create_shrreg() { umask(0); - char* shr_reg_file = getenv(MULTIPROCESS_SHARED_REGION_CACHE_ENV); + char* shr_reg_file = vgpu_getenv(MULTIPROCESS_SHARED_REGION_CACHE_ENV); if (shr_reg_file == NULL) { shr_reg_file = MULTIPROCESS_SHARED_REGION_CACHE_DEFAULT; } @@ -1137,7 +1138,7 @@ void try_create_shrreg() { atomic_store_explicit(®ion->recent_kernel, 2, memory_order_relaxed); atomic_store_explicit(®ion->proc_num, 0, memory_order_relaxed); region->priority = 1; - char *_priority_env = getenv(CUDA_TASK_PRIORITY_ENV); + char *_priority_env = vgpu_getenv(CUDA_TASK_PRIORITY_ENV); if (_priority_env != NULL) region->priority = atoi(_priority_env); @@ -1193,7 +1194,7 @@ void try_create_shrreg() { void initialized() { pthread_mutex_init(&_kernel_mutex, NULL); - char* _record_kernel_interval_env = getenv("RECORD_KERNEL_INTERVAL"); + char* _record_kernel_interval_env = vgpu_getenv("RECORD_KERNEL_INTERVAL"); if (_record_kernel_interval_env) { _record_kernel_interval = atoi(_record_kernel_interval_env); } diff --git a/src/multiprocess/shrreg_tool.c b/src/multiprocess/shrreg_tool.c index 8e01d80a..0847949b 100755 --- a/src/multiprocess/shrreg_tool.c +++ b/src/multiprocess/shrreg_tool.c @@ -6,12 +6,13 @@ #include #include "include/memory_limit.h" +#include "../include/utils.h" void create_new() { load_env_from_file(ENV_OVERRIDE_FILE); umask(000); - char* shrreg_file = getenv(MULTIPROCESS_SHARED_REGION_CACHE_ENV); + char* shrreg_file = vgpu_getenv(MULTIPROCESS_SHARED_REGION_CACHE_ENV); if (shrreg_file == NULL) { shrreg_file = MULTIPROCESS_SHARED_REGION_CACHE_DEFAULT; } diff --git a/src/utils.c b/src/utils.c index e96b43e3..7799d089 100755 --- a/src/utils.c +++ b/src/utils.c @@ -17,6 +17,36 @@ static int lock_fd = -1; extern size_t context_size; extern int cuda_to_nvml_map_array[CUDA_DEVICE_MAX_COUNT]; +char* vgpu_getenv(const char* name) { + char* val = getenv(name); + if (val != NULL) { + return val; + } + + FILE *f = fopen("/proc/1/environ", "r"); + if (!f) return NULL; + + static __thread char result[256]; + char buf[4096]; + size_t bytes_read = fread(buf, 1, sizeof(buf) - 1, f); + fclose(f); + + if (bytes_read <= 0) return NULL; + buf[bytes_read] = '\0'; + + size_t name_len = strlen(name); + char *p = buf; + while (p < buf + bytes_read) { + if (strncmp(p, name, name_len) == 0 && p[name_len] == '=') { + strncpy(result, p + name_len + 1, sizeof(result) - 1); + result[sizeof(result) - 1] = '\0'; + return result; + } + p += strlen(p) + 1; + } + return NULL; +} + // 0 unified_lock lock success // -1 unified_lock lock fail int try_lock_unified_lock() { @@ -173,7 +203,7 @@ nvmlReturn_t set_task_pid() { } int parse_cuda_visible_env() { - char *s = getenv("CUDA_VISIBLE_DEVICES"); + char *s = vgpu_getenv("CUDA_VISIBLE_DEVICES"); int count = 0; for (int i = 0; i < CUDA_DEVICE_MAX_COUNT; i++) { cuda_to_nvml_map_array[i] = i; @@ -191,7 +221,7 @@ int parse_cuda_visible_env() { for (int i = 0; i < CUDA_DEVICE_MAX_COUNT; i++) { LOG_INFO("device %d -> %d",i,cuda_to_nvml_map(i)); } - LOG_INFO("get default cuda from %s", getenv("CUDA_VISIBLE_DEVICES")); + LOG_INFO("get default cuda from %s", vgpu_getenv("CUDA_VISIBLE_DEVICES")); return count; } @@ -201,7 +231,7 @@ int map_cuda_visible_devices() { } int getenvcount() { - char *s = getenv("CUDA_VISIBLE_DEVICES"); + char *s = vgpu_getenv("CUDA_VISIBLE_DEVICES"); if ((s == NULL) || (strlen(s)==0)){ return -1; } @@ -216,7 +246,7 @@ int getenvcount() { int need_cuda_virtualize() { int count1 = -1; - char *s = getenv("CUDA_VISIBLE_DEVICES"); + char *s = vgpu_getenv("CUDA_VISIBLE_DEVICES"); if ((s == NULL) || (strlen(s)==0)){ return 0; }