Skip to content

Commit f970b27

Browse files
poor-circlehowardlau1999
authored andcommitted
[metric] fix data race (alibaba#889)
fix data race in metric
1 parent 4e0eb70 commit f970b27

File tree

5 files changed

+10
-12
lines changed

5 files changed

+10
-12
lines changed

include/ylt/metric/counter.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class basic_static_counter : public static_metric {
5858
}
5959

6060
value_type update(value_type value) {
61-
if (!has_change_) [[unlikely]] {
62-
has_change_ = true;
61+
if (!has_change_.load(std::memory_order::relaxed)) [[unlikely]] {
62+
has_change_.store(true, std::memory_order::relaxed);
6363
}
6464
return default_label_value_.update(value);
6565
}
@@ -70,7 +70,7 @@ class basic_static_counter : public static_metric {
7070

7171
void serialize(std::string &str) override {
7272
auto value = default_label_value_.value();
73-
if (value == 0 && !has_change_) {
73+
if (value == 0 && !has_change_.load(std::memory_order::relaxed)) {
7474
return;
7575
}
7676

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

@@ -126,7 +126,7 @@ class basic_static_counter : public static_metric {
126126
str.pop_back();
127127
}
128128

129-
bool has_change_ = false;
129+
std::atomic<bool> has_change_ = false;
130130
uint32_t dupli_count_;
131131
thread_local_value<value_type> default_label_value_;
132132
};

include/ylt/metric/summary_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class summary_impl {
131131
template <bool inc_order>
132132
void stat_impl(uint64_t& count,
133133
std::vector<std::pair<int16_t, uint_type>>& result, int i) {
134-
auto piece = arr[i].load(std::memory_order_relaxed);
134+
auto piece = arr[i].load(std::memory_order_acquire);
135135
if (piece) {
136136
if constexpr (inc_order) {
137137
for (int j = 0; j < piece->size(); ++j) {

include/ylt/metric/system_metric.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,14 @@ inline void start_stat(std::weak_ptr<coro_io::period_timer> weak) {
381381
return;
382382
}
383383

384+
ylt_stat();
385+
384386
timer->expires_after(std::chrono::seconds(1));
385387
timer->async_wait([timer](std::error_code ec) {
386388
if (ec) {
387389
return;
388390
}
389391

390-
ylt_stat();
391392
start_stat(timer);
392393
});
393394
}

src/metric/benchmark/bench.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ inline void bench_dynamic_counter_mixed_with_delete(
134134
{"a", "b"});
135135
bench_mixed_impl(
136136
counter,
137-
[&, i = 0]() mutable {
138-
++i;
137+
[&]() mutable {
139138
std::array<std::string, 2> label = {
140139
"123e4567-e89b-12d3-a456-426614174000",
141140
std::to_string(get_random(max_cnt))};
@@ -153,8 +152,7 @@ inline void bench_dynamic_counter_mixed(size_t thd_num,
153152
{"a", "b"});
154153
bench_mixed_impl(
155154
counter,
156-
[&, i = 0]() mutable {
157-
++i;
155+
[&]() mutable {
158156
counter.inc({"123e4567-e89b-12d3-a456-426614174000",
159157
std::to_string(get_random(max_cnt))},
160158
1);

src/metric/tests/test_metric.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,6 @@ TEST_CASE("test serialize with multiple threads") {
17671767
#if defined(__GNUC__)
17681768
TEST_CASE("test system metric") {
17691769
start_system_metric();
1770-
metric::detail::ylt_stat();
17711770

17721771
auto s = system_metric_manager::instance().serialize_static();
17731772
std::cout << s;

0 commit comments

Comments
 (0)