Skip to content

Commit bb9c18c

Browse files
Pavel Harbanaucodebot
authored andcommitted
replace atomics with stop events in periodic metrics
1 parent 4120ed9 commit bb9c18c

File tree

4 files changed

+28
-34
lines changed

4 files changed

+28
-34
lines changed

apps/services/app_execution_metrics/executor_metrics_manager.h

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,20 @@ class app_executor_metrics_service
5252
{
5353
metrics_exec = &metrics_exec_;
5454

55-
if (stopped.exchange(false, std::memory_order_relaxed)) {
56-
sync_event wait_all;
57-
58-
defer_until_success(*metrics_exec, timers, [this, token = wait_all.get_token()]() mutable {
59-
// Start the executor metrics backend.
60-
exec_metrics_backend.start(report_period, timers.create_unique_timer(*metrics_exec), notifier);
61-
});
62-
wait_all.wait();
63-
}
55+
sync_event wait_all;
56+
57+
defer_until_success(*metrics_exec, timers, [this, token = wait_all.get_token()]() mutable {
58+
// Start the executor metrics backend.
59+
exec_metrics_backend.start(report_period, timers.create_unique_timer(*metrics_exec), notifier);
60+
});
61+
wait_all.wait();
6462
}
6563

6664
/// Stops the metrics backend.
6765
void stop()
6866
{
69-
if (not stopped.exchange(true, std::memory_order_relaxed)) {
70-
sync_event wait_all;
71-
defer_until_success(
72-
*metrics_exec, timers, [this, token = wait_all.get_token()]() mutable { exec_metrics_backend.stop(); });
73-
wait_all.wait();
74-
}
67+
// Blocking call.
68+
exec_metrics_backend.stop();
7569
}
7670

7771
/// Application timers manager.
@@ -84,8 +78,6 @@ class app_executor_metrics_service
8478
executor_metrics_backend exec_metrics_backend;
8579
/// Notifier used by the backend to notify metrics.
8680
executor_metrics_notifier& notifier;
87-
/// Operation status of this service.
88-
std::atomic<bool> stopped{true};
8981
};
9082

9183
/// Callback called on session destruction.

apps/services/metrics/periodic_metrics_report_controller.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,12 @@ class periodic_metrics_report_controller
4545
if (!report_period.count()) {
4646
return;
4747
}
48-
if (stopped.exchange(false, std::memory_order_relaxed)) {
49-
std::promise<void> exit_signal;
50-
auto fut = exit_signal.get_future();
51-
defer_until_success(executor, timers, [this, &exit_signal]() mutable {
52-
timer.run();
53-
exit_signal.set_value();
54-
});
55-
fut.wait();
56-
}
48+
stop_manager.reset();
49+
50+
sync_event wait_all;
51+
defer_until_success(executor, timers, [this, token = wait_all.get_token()]() mutable { timer.run(); });
52+
// Block waiting for the controller to start.
53+
wait_all.wait();
5754
}
5855

5956
/// Stops the metrics report timer.
@@ -62,20 +59,23 @@ class periodic_metrics_report_controller
6259
if (!report_period.count()) {
6360
return;
6461
}
65-
if (not stopped.exchange(true, std::memory_order_relaxed)) {
66-
sync_event wait_all;
67-
defer_until_success(executor, timers, [this, token = wait_all.get_token()]() mutable { timer.stop(); });
68-
wait_all.wait();
69-
}
62+
63+
// Signal stop to asynchronous timer thread.
64+
stop_manager.stop();
65+
// Stop the timer.
66+
timer.stop();
7067
}
7168

7269
private:
7370
/// Trigger metrics report in all registered producers.
7471
void report_metrics()
7572
{
76-
if (stopped.load(std::memory_order_relaxed)) {
73+
auto token = stop_manager.get_token();
74+
// Do not rearm the timer and process metrics if stop was requested.
75+
if (SRSRAN_UNLIKELY(token.is_stop_requested())) {
7776
return;
7877
}
78+
7979
// Rearm the timer.
8080
timer.run();
8181

@@ -92,7 +92,8 @@ class periodic_metrics_report_controller
9292
unique_timer timer;
9393
/// Metrics report period.
9494
std::chrono::milliseconds report_period{0};
95-
std::atomic<bool> stopped{true};
95+
/// Manager used for stopping this controller.
96+
stop_event_source stop_manager;
9697
/// List of metrics producers managed by this controller.
9798
std::vector<metrics_producer*> producers;
9899
};

include/srsran/support/executors/metrics/executor_metrics_backend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class executor_metrics_backend : public executor_metrics_channel_registry
4343
// See interface for documentation.
4444
executor_metrics_channel& add_channel(const std::string& exec_name) override;
4545

46-
/// Stops periodic metrics reporting.
46+
/// Stops periodic metrics reporting. This method is blocking.
4747
void stop();
4848

4949
/// Start the backend worker using the passed timer and periodically report metrics to the given notifier object.

lib/support/executors/metrics/executor_metrics_backend.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void executor_metrics_backend::stop()
3939
{
4040
// Signal stop to asynchronous timer thread.
4141
stop_control.stop();
42+
// Stop the timer.
4243
timer.stop();
4344

4445
notifier = nullptr;

0 commit comments

Comments
 (0)