Skip to content

Commit

Permalink
feat replace blocking console log with async log file
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyncs committed Jan 31, 2024
1 parent 29b74d5 commit 6ddecdb
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/linux-x64-gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
cd /work
source /opt/conda/bin/activate
conda activate py38
apt install libspdlog-dev -y
mkdir build && cd build
bash ../generate.sh make
make -j$(nproc) && make install
1 change: 1 addition & 0 deletions builder/manywheel/entrypoint_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ conda activate $PYTHON_VERSION

cd lmdeploy
mkdir -p build && cd build && rm -rf *
apt install libspdlog-dev -y
bash ../generate.sh make
make -j$(nproc) && make install
if [ $? != 0 ]; then
Expand Down
6 changes: 5 additions & 1 deletion src/turbomind/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ target_link_libraries(cuda_utils PUBLIC CUDA::cudart)
add_library(logger STATIC logger.cc)
set_property(TARGET logger PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET logger PROPERTY CUDA_RESOLVE_DEVICE_SYMBOLS ON)
target_link_libraries(logger PUBLIC CUDA::cudart)
if (UNIX)
target_link_libraries(logger PUBLIC CUDA::cudart -lpthread)
else ()
target_link_libraries(logger PUBLIC CUDA::cudart)
endif ()

add_library(cublasAlgoMap STATIC cublasAlgoMap.cc)
set_property(TARGET cublasAlgoMap PROPERTY POSITION_INDEPENDENT_CODE ON)
Expand Down
41 changes: 41 additions & 0 deletions src/turbomind/utils/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@
#include "src/turbomind/utils/logger.h"
#include <cuda_runtime.h>

#ifndef _MSC_VER
#include <spdlog/async.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/hourly_file_sink.h>
#include <spdlog/sinks/rotating_file_sink.h>
#endif

namespace turbomind {

Logger::Logger()
{
#ifndef _MSC_VER
// TODO: use config
SpdLogger::get_instance().set_log_path("/var/log/lmdeploy.log");
SpdLogger::get_instance().init();
#endif
char* is_first_rank_only_char = std::getenv("TM_LOG_FIRST_RANK_ONLY");
bool is_first_rank_only =
(is_first_rank_only_char != nullptr && std::string(is_first_rank_only_char) == "ON") ? true : false;
Expand Down Expand Up @@ -56,4 +68,33 @@ Logger::Logger()
}
}

#ifndef _MSC_VER
void SpdLogger::init()
{
if (inited_) {
return;
}

spdlog::init_thread_pool(8192, 1);

// rotate 500 MB
auto basic_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(path_, 500 * 1024 * 1024, 0);
// async
auto logger = std::make_shared<spdlog::async_logger>(
"logger", basic_sink, spdlog::thread_pool(), spdlog::async_overflow_policy::overrun_oldest);
logger->set_level(spdlog::level::trace);
// ms, thread_id
logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%t] %v");

logger_ = logger;

spdlog::register_logger(logger_);

// real-time refresh
logger_->flush_on(spdlog::level::trace);

inited_ = true;
}
#endif

} // namespace turbomind
54 changes: 47 additions & 7 deletions src/turbomind/utils/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include <map>
#include <string>

#ifndef _MSC_VER
#include <spdlog/spdlog.h>
#endif

#include "src/turbomind/utils/string_utils.h"

namespace turbomind {
Expand All @@ -30,11 +34,41 @@ namespace turbomind {
#undef ERROR
#endif

#ifndef _MSC_VER
class SpdLogger {
public:
static SpdLogger& get_instance()
{
static SpdLogger spdlogger;
return spdlogger;
}
spdlog::logger* get_logger()
{
return logger_.get();
}

void set_log_path(const std::string& path)
{
path_ = path;
}
void init();

private:
SpdLogger() = default;
~SpdLogger() = default;
SpdLogger(const SpdLogger&) = delete;
SpdLogger& operator=(const SpdLogger&) = delete;

std::string path_;
bool inited_ = false;
std::shared_ptr<spdlog::logger> logger_;
};
#endif

class Logger {

public:
enum Level
{
enum Level {
TRACE = 0,
DEBUG = 10,
INFO = 20,
Expand All @@ -47,28 +81,34 @@ class Logger {
thread_local Logger instance;
return instance;
}
Logger(Logger const&) = delete;
Logger(Logger const&) = delete;
void operator=(Logger const&) = delete;

template<typename... Args>
void log(const Level level, const std::string format, const Args&... args)
{
if (level_ <= level) {
std::string fmt = getPrefix(level) + format + "\n";
// FILE* out = level_ < WARNING ? stdout : stderr;
std::string fmt = getPrefix(level) + format + "\n";
std::string logstr = fmtstr(fmt, args...);
#ifdef _MSC_VER
fprintf(stderr, "%s", logstr.c_str());
#else
SpdLogger::get_instance().get_logger()->log(spdlog::level::trace, logstr);
#endif
}
}

template<typename... Args>
void log(const Level level, const int rank, const std::string format, const Args&... args)
{
if (level_ <= level) {
std::string fmt = getPrefix(level, rank) + format + "\n";
// FILE* out = level_ < WARNING ? stdout : stderr;
std::string fmt = getPrefix(level, rank) + format + "\n";
std::string logstr = fmtstr(fmt, args...);
#ifdef _MSC_VER
fprintf(stderr, "%s", logstr.c_str());
#else
SpdLogger::get_instance().get_logger()->log(spdlog::level::trace, logstr);
#endif
}
}

Expand Down

0 comments on commit 6ddecdb

Please sign in to comment.