Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[metric] fix data race #889

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions include/ylt/metric/counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class basic_static_counter : public static_metric {
}

value_type update(value_type value) {
if (!has_change_) [[unlikely]] {
has_change_ = true;
if (!has_change_.load(std::memory_order::relaxed)) [[unlikely]] {
has_change_.store(true, std::memory_order::relaxed);
}
return default_label_value_.update(value);
}
Expand All @@ -70,7 +70,7 @@ class basic_static_counter : public static_metric {

void serialize(std::string &str) override {
auto value = default_label_value_.value();
if (value == 0 && !has_change_) {
if (value == 0 && !has_change_.load(std::memory_order::relaxed)) {
return;
}

Expand All @@ -81,7 +81,7 @@ class basic_static_counter : public static_metric {
#ifdef CINATRA_ENABLE_METRIC_JSON
void serialize_to_json(std::string &str) override {
auto value = default_label_value_.value();
if (value == 0 && !has_change_) {
if (value == 0 && !has_change_.load(std::memory_order::relaxed)) {
return;
}

Expand Down Expand Up @@ -126,7 +126,7 @@ class basic_static_counter : public static_metric {
str.pop_back();
}

bool has_change_ = false;
std::atomic<bool> has_change_ = false;
uint32_t dupli_count_;
thread_local_value<value_type> default_label_value_;
};
Expand Down
2 changes: 1 addition & 1 deletion include/ylt/metric/summary_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class summary_impl {
template <bool inc_order>
void stat_impl(uint64_t& count,
std::vector<std::pair<int16_t, uint_type>>& result, int i) {
auto piece = arr[i].load(std::memory_order_relaxed);
auto piece = arr[i].load(std::memory_order_acquire);
if (piece) {
if constexpr (inc_order) {
for (int j = 0; j < piece->size(); ++j) {
Expand Down
3 changes: 2 additions & 1 deletion include/ylt/metric/system_metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,14 @@ inline void start_stat(std::weak_ptr<coro_io::period_timer> weak) {
return;
}

ylt_stat();

timer->expires_after(std::chrono::seconds(1));
timer->async_wait([timer](std::error_code ec) {
if (ec) {
return;
}

ylt_stat();
start_stat(timer);
});
}
Expand Down
6 changes: 2 additions & 4 deletions src/metric/benchmark/bench.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ inline void bench_dynamic_counter_mixed_with_delete(
{"a", "b"});
bench_mixed_impl(
counter,
[&, i = 0]() mutable {
++i;
[&]() mutable {
std::array<std::string, 2> label = {
"123e4567-e89b-12d3-a456-426614174000",
std::to_string(get_random(max_cnt))};
Expand All @@ -153,8 +152,7 @@ inline void bench_dynamic_counter_mixed(size_t thd_num,
{"a", "b"});
bench_mixed_impl(
counter,
[&, i = 0]() mutable {
++i;
[&]() mutable {
counter.inc({"123e4567-e89b-12d3-a456-426614174000",
std::to_string(get_random(max_cnt))},
1);
Expand Down
1 change: 0 additions & 1 deletion src/metric/tests/test_metric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1767,7 +1767,6 @@ TEST_CASE("test serialize with multiple threads") {
#if defined(__GNUC__)
TEST_CASE("test system metric") {
start_system_metric();
metric::detail::ylt_stat();

auto s = system_metric_manager::instance().serialize_static();
std::cout << s;
Expand Down
Loading