Skip to content

Commit 1fe13de

Browse files
author
pippocao
committed
Perf: eliminate spurious worker wakeups on the log write path
- siso/miso ring buffers computed the low-space flag from a read cursor that was only refreshed when the buffer was nearly full, so after 50% cumulative usage every log call kept waking the worker for no reason; refresh the cursor before raising the flag - let the worker keep draining while the previous round made progress instead of always sleeping the full 66 ms interval
1 parent 60ef4d3 commit 1fe13de

7 files changed

Lines changed: 29 additions & 8 deletions

File tree

src/bq_log/log/log_imp.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,14 +487,16 @@ namespace bq {
487487
merged_log_level_bitmap_ = tmp;
488488
}
489489

490-
void log_imp::process(bool is_force_flush)
490+
bool log_imp::process(bool is_force_flush)
491491
{
492492
constexpr uint64_t flush_io_min_interval_ms = 100;
493493
uint64_t current_epoch_ms = 0;
494+
bool did_work = false;
494495
while (true) {
495496
auto read_chunk = buffer_->read_chunk();
496497
scoped_log_buffer_handle<log_buffer> scoped_read_chunk(*buffer_, read_chunk);
497498
if (read_chunk.result == enum_buffer_result_code::success) {
499+
did_work = true;
498500
bq::log_entry_handle log_item(read_chunk.data_addr, read_chunk.data_size);
499501
current_epoch_ms = log_item.get_log_head().timestamp_epoch;
500502
process_log_chunk(log_item);
@@ -514,6 +516,7 @@ namespace bq {
514516
last_flush_io_epoch_ms_ = current_epoch_ms;
515517
}
516518
}
519+
return did_work;
517520
}
518521

519522
void log_imp::sync_process(bool is_force_flush)

src/bq_log/log/log_imp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace bq {
7878

7979
bq::string& get_config();
8080

81-
void process(bool is_force_flush);
81+
bool process(bool is_force_flush);
8282

8383
void sync_process(bool is_force_flush);
8484

src/bq_log/log/log_manager.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,24 @@ namespace bq {
160160
return false;
161161
}
162162

163-
void log_manager::process_by_worker(log_imp* target_log, bool is_force_flush)
163+
bool log_manager::process_by_worker(log_imp* target_log, bool is_force_flush)
164164
{
165165
bq::platform::scoped_spin_lock_read_crazy scoped_lock(logs_lock_);
166166
if (phase::working != phase_.load(bq::platform::memory_order::relaxed)) {
167-
return;
167+
return false;
168168
}
169+
bool did_work = false;
169170
if (target_log) {
170-
target_log->process(is_force_flush);
171+
did_work = target_log->process(is_force_flush);
171172
} else {
172173
for (decltype(log_imp_list_)::size_type i = 0; i < log_imp_list_.size(); ++i) {
173174
auto& log_impl = log_imp_list_[i];
174175
if (log_impl->get_thread_mode() == log_thread_mode::async) {
175-
log_imp_list_[i]->process(is_force_flush);
176+
did_work = log_imp_list_[i]->process(is_force_flush) || did_work;
176177
}
177178
}
178179
}
180+
return did_work;
179181
}
180182

181183
void log_manager::force_flush_all()

src/bq_log/log/log_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace bq {
3636

3737
bool reset_config(const bq::string& log_name, const bq::string& config_content);
3838

39-
void process_by_worker(log_imp* target_log, bool is_force_flush);
39+
bool process_by_worker(log_imp* target_log, bool is_force_flush);
4040

4141
uint32_t get_logs_count() const;
4242

src/bq_log/log/log_worker.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,13 @@ namespace bq {
9999
}
100100
// normal process
101101
else {
102-
manager_->process_by_worker((thread_mode_ == log_thread_mode::async) ? nullptr : log_target_, false);
102+
bool did_work = manager_->process_by_worker((thread_mode_ == log_thread_mode::async) ? nullptr : log_target_, false);
103+
if (did_work) {
104+
if (is_cancelled()) {
105+
break;
106+
}
107+
continue;
108+
}
103109
}
104110
if (is_cancelled()) {
105111
break;

src/bq_log/types/buffer/miso_ring_buffer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ namespace bq {
211211
handle.data_addr = new_block.chunk_head.data;
212212
used_blocks_count += need_block_count;
213213
handle.low_space_flag = ((used_blocks_count << 1) >= aligned_blocks_count_);
214+
if (handle.low_space_flag) {
215+
read_cursor_ref = cursors_.read_cursor_.load_acquire();
216+
used_blocks_count = static_cast<uint32_t>(current_write_cursor + need_block_count - read_cursor_ref);
217+
handle.low_space_flag = ((used_blocks_count << 1) >= aligned_blocks_count_);
218+
}
214219
#if defined(BQ_LOG_BUFFER_DEBUG)
215220
++result_code_statistics_[(int32_t)enum_buffer_result_code::success];
216221
#endif

src/bq_log/types/buffer/siso_ring_buffer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ namespace bq {
119119
new_block.to_chunk_head().data_size = size;
120120

121121
handle.low_space_flag = ((aligned_blocks_count_ - left_space) << 1) >= aligned_blocks_count_;
122+
if (handle.low_space_flag) {
123+
head_->wt_reading_cursor_cache_ = head_->reading_cursor().load_acquire();
124+
left_space = static_cast<uint32_t>(head_->wt_reading_cursor_cache_ + aligned_blocks_count_ - head_->wt_writing_cursor_cache_);
125+
handle.low_space_flag = ((aligned_blocks_count_ - left_space) << 1) >= aligned_blocks_count_;
126+
}
122127
handle.result = enum_buffer_result_code::success;
123128
#if defined(BQ_LOG_BUFFER_DEBUG)
124129
++result_code_statistics_[(int32_t)enum_buffer_result_code::success];

0 commit comments

Comments
 (0)