From 36f353e374967393be03fa087a30f8d7ebba0136 Mon Sep 17 00:00:00 2001 From: Zonghang Li Date: Tue, 28 Jan 2025 13:06:08 +0400 Subject: [PATCH] check env path before calling fio to ensure we can find it --- common/common.cpp | 2 +- common/profiler.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index 8cc7a964..851d7ebf 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1486,7 +1486,7 @@ struct llama_init_result llama_init_from_gpt_params(gpt_params & params) { if (auto_schedule) { // get device profile - LOG_INF("Start profiling this device, this may take some seconds ...\n"); + LOG_INF("\nstart profiling this device, this may take some seconds ...\n"); dev_info.rank = params.rank; llama_profile_device(&dev_info, model, ml, params.gpu_mem, params.n_predict, params.n_ctx, params.cpuparams.n_threads, params.flash_attn); } diff --git a/common/profiler.cpp b/common/profiler.cpp index 26fc5fbf..24743c21 100644 --- a/common/profiler.cpp +++ b/common/profiler.cpp @@ -3,13 +3,13 @@ #include "ggml.h" #include "ggml-backend.h" #include "llama.h" +#include #if defined(_WIN32) || defined(_WIN64) #include #elif defined(__linux__) #include #include - #include #include #include #include @@ -828,6 +828,44 @@ static size_t get_default_readahead_size() { #endif } +static std::vector split(const std::string & str, char delimiter) { + std::vector tokens; + std::stringstream ss(str); + std::string token; + while (std::getline(ss, token, delimiter)) { + tokens.push_back(token); + } + return tokens; +} + +static bool path_exist_in_env(const std::string & path, const std::string & env_path) { + auto paths = split(env_path, ':'); + return std::find(paths.begin(), paths.end(), path) != paths.end(); +} + +static bool path_exist_in_fs(const std::string & path) { + struct stat info; + return (stat(path.c_str(), &info) == 0 && (info.st_mode & S_IFDIR)); +} + +static void check_env_path() { + const char * cur_env_path = std::getenv("PATH"); + std::string update_env_path = cur_env_path ? cur_env_path : ""; + std::vector paths_to_check = {"/opt/homebrew/bin", "/usr/local/bin"}; + + for (const auto & path : paths_to_check) { + if (!path_exist_in_env(path, update_env_path) && path_exist_in_fs(path)) { + if (!update_env_path.empty() && update_env_path.back() != ':') { + update_env_path += ':'; + } + update_env_path += path; + LOG_INF("add missing path: %s, current env path: %s\n", path.c_str(), update_env_path.c_str()); + } + } + + setenv("PATH", update_env_path.c_str(), 1); +} + static void external_fio_impl(float * read_bw, float * write_bw, bool op_rand, int n_threads) { const char * test_file = "fio_test"; const char * fio_conf_template = R"( @@ -890,6 +928,8 @@ numjobs=%d const char * output_file = "fio_output.log"; const char * conf_file = "config.fio"; + check_env_path(); // ensure the fio bin file can be found + int num_try = 0; int ret; while (num_try < 2) {