From a157459630af706c49166a15905f0f553fece2a3 Mon Sep 17 00:00:00 2001 From: LIU Hao Date: Mon, 14 Oct 2024 18:17:48 +0800 Subject: [PATCH] static/logger: Cache FD for each loop --- poseidon/fiber/redis_scan_and_get_future.hpp | 2 +- poseidon/fwd.hpp | 1 + poseidon/static/logger.cpp | 22 ++++++++++++-------- poseidon/static/logger.hpp | 1 + 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/poseidon/fiber/redis_scan_and_get_future.hpp b/poseidon/fiber/redis_scan_and_get_future.hpp index b0a39e70..3444dd53 100644 --- a/poseidon/fiber/redis_scan_and_get_future.hpp +++ b/poseidon/fiber/redis_scan_and_get_future.hpp @@ -18,7 +18,7 @@ class Redis_Scan_and_Get_Future struct Result { cow_string pattern; // input - ::asteria::cow_dictionary pairs; + cow_dictionary pairs; }; private: diff --git a/poseidon/fwd.hpp b/poseidon/fwd.hpp index 29cacff7..7e7cf074 100644 --- a/poseidon/fwd.hpp +++ b/poseidon/fwd.hpp @@ -103,6 +103,7 @@ using ::rocket::recursive_mutex; using ::rocket::condition_variable; using ::rocket::array; using ::rocket::cow_vector; +using ::asteria::cow_dictionary; using ::rocket::cow_hashmap; using ::rocket::static_vector; using ::rocket::cow_string; diff --git a/poseidon/static/logger.cpp b/poseidon/static/logger.cpp index 99d699c7..615f79a8 100644 --- a/poseidon/static/logger.cpp +++ b/poseidon/static/logger.cpp @@ -17,7 +17,7 @@ struct Level_Config bool p_stderr = false; bool trivial = false; cow_string color; - cow_string file; + prehashed_string file; }; struct Message @@ -126,7 +126,6 @@ do_load_level_config(Level_Config& lconf, const Config_File& conf_file, const ch name, conf_value, conf_file.path()); } -inline void do_color(linear_buffer& mtext, const Level_Config& lconf, const char* code) { @@ -141,7 +140,7 @@ do_color(linear_buffer& mtext, const Level_Config& lconf, const char* code) } void -do_write_nothrow(const Level_Config& lconf, const Message& msg) noexcept +do_write_nothrow(cow_dictionary& io_files, const Level_Config& lconf, const Message& msg) noexcept try { linear_buffer mtext; ::rocket::ascii_numput nump; @@ -241,10 +240,13 @@ do_write_nothrow(const Level_Config& lconf, const Message& msg) noexcept if(lconf.p_stderr) (void)! ::write(STDERR_FILENO, mtext.data(), mtext.size()); - if(lconf.file != "") - if(const auto fd = unique_posix_fd(::open(lconf.file.c_str(), - O_WRONLY | O_APPEND | O_CREAT, 0644))) - (void)! ::write(fd, mtext.data(), mtext.size()); + if(lconf.file != "") { + auto r = io_files.try_emplace(lconf.file); + if(r.second) + r.first->second.reset(::open(lconf.file.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644)); + if(r.first->second) + (void)! ::write(r.first->second, mtext.data(), mtext.size()); + } } catch(exception& stdex) { ::fprintf(stderr, @@ -330,9 +332,10 @@ thread_loop() // Write all elements. for(const auto& msg : this->m_io_queue) if((msg.level < levels.size()) && (!levels[msg.level].trivial || (queue_size < 1000))) - do_write_nothrow(levels[msg.level], msg); + do_write_nothrow(this->m_io_files, levels[msg.level], msg); this->m_io_queue.clear(); + this->m_io_files.clear(); io_sync_lock.unlock(); ::sync(); } @@ -379,9 +382,10 @@ synchronize() noexcept // Write all elements. for(const auto& msg : this->m_io_queue) if(msg.level < levels.size()) - do_write_nothrow(levels[msg.level], msg); + do_write_nothrow(this->m_io_files, levels[msg.level], msg); this->m_io_queue.clear(); + this->m_io_files.clear(); io_sync_lock.unlock(); ::sync(); } diff --git a/poseidon/static/logger.hpp b/poseidon/static/logger.hpp index b87ec35e..10d289dd 100644 --- a/poseidon/static/logger.hpp +++ b/poseidon/static/logger.hpp @@ -22,6 +22,7 @@ class Logger mutable recursive_mutex m_io_mutex; cow_vector m_io_queue; + cow_dictionary m_io_files; public: // Creates a logger that outputs to nowhere.