srs-control 0.2.2
Loading...
Searching...
No Matches
DataMonitor.cpp
Go to the documentation of this file.
1#include "DataMonitor.hpp"
5#include <asio/awaitable.hpp>
6#include <asio/detached.hpp>
7#include <asio/error.hpp>
8#include <asio/impl/co_spawn.hpp>
9#include <asio/redirect_error.hpp>
10#include <asio/use_awaitable.hpp>
11#include <chrono>
12#include <fmt/color.h>
13#include <memory>
14#include <spdlog/common.h>
15#include <spdlog/pattern_formatter.h>
16#include <spdlog/sinks/stdout_color_sinks.h>
17#include <spdlog/spdlog.h>
18#include <string>
19#include <system_error>
20#include <utility>
21
22namespace srs::workflow
23{
25 : analysis_handle_{ analysis_handle }
26 , io_context_{ io_context }
28 {
29 }
30
31 auto DataMonitor::print_cycle() -> asio::awaitable<void>
32 {
33 auto _ = ExitLogger{};
34 clock_.expires_after(period_);
35 console_ = spdlog::stdout_color_st("Data Monitor");
36 auto console_format = std::make_unique<spdlog::pattern_formatter>(
37 "[%H:%M:%S] <%n> %v", spdlog::pattern_time_type::local, std::string(""));
38 console_->set_formatter(std::move(console_format));
39 console_->flush_on(spdlog::level::info);
40 console_->set_level(spdlog::level::info);
41 const auto buffer_size = analysis_handle_->get_app().get_config().data_buffer_size;
42 const auto& task_workflow = analysis_handle_->get_data_workflow();
43 [[maybe_unused]] const auto* frame_count_checker =
44 analysis_handle_->get_sink_manager().get_frame_count_checker();
45 auto error_code = std::error_code{};
46 while (not is_cancelled_.load())
47 {
48 co_await clock_.async_wait(asio::redirect_error(asio::use_awaitable, error_code));
49 if (error_code == asio::error::operation_aborted)
50 {
51 break;
52 }
53 clock_.expires_after(period_);
54
55 auto read_total_bytes_count = analysis_handle_->get_read_data_bytes();
56 auto read_total_hits_count = analysis_handle_->get_processed_hit_number();
57 auto frame_counts = analysis_handle_->get_frame_counts();
58 auto write_total_bytes_count = task_workflow.get_data_bytes();
59 auto drop_total_bytes_count = analysis_handle_->get_drop_data_bytes();
60
61 auto time_now = std::chrono::steady_clock::now();
62 auto time_duration = std::chrono::duration_cast<std::chrono::microseconds>(time_now - last_print_time_);
63
64 auto bytes_read = static_cast<double>(read_total_bytes_count - last_read_data_bytes_);
65 auto bytes_write = static_cast<double>(write_total_bytes_count - last_write_data_bytes_);
66 auto bytes_drop = static_cast<double>(drop_total_bytes_count - last_drop_data_bytes_);
67 auto hits_processed = static_cast<double>(read_total_hits_count - last_processed_hit_num_);
68 auto frame_counts_diff = frame_counts - last_frame_counts_;
69
70 last_read_data_bytes_ = read_total_bytes_count;
71 last_write_data_bytes_ = write_total_bytes_count;
72 last_drop_data_bytes_ = drop_total_bytes_count;
73 last_processed_hit_num_ = read_total_hits_count;
74 last_frame_counts_ = frame_counts;
75 last_print_time_ = time_now;
76
77 const auto time_duration_us = static_cast<double>(time_duration.count());
78 current_received_bytes_MBps_ = bytes_read / time_duration_us;
79 current_write_bytes_MBps_ = bytes_write / time_duration_us;
80 current_drop_bytes_MBps_ = bytes_drop / time_duration_us;
81 current_hits_ps_ = hits_processed / time_duration_us * 1e6;
82 const auto frame_rate = (frame_counts_diff == 0) ? 0.
83 : bytes_read / static_cast<double>(frame_counts_diff) /
84 static_cast<double>(buffer_size) * 100.;
85
87 console_->info("read (buf)|write|drop rate: {} ({:>2.0f}%) | {} | {} {}. \r",
89 frame_rate,
93 }
94 }
95
97 {
98 const auto read_speed_MBps = current_received_bytes_MBps_;
99 const auto write_speed_MBps = current_write_bytes_MBps_;
100 const auto drop_speed_MBps = current_drop_bytes_MBps_;
101
102 const auto scale = (read_speed_MBps < 1.) ? 1000. : 1.;
103 unit_string_ = (read_speed_MBps < 1.) ? fmt::format(fmt::emphasis::bold, "KB/s")
104 : fmt::format(fmt::emphasis::bold, "MB/s");
106 fmt::format(fg(fmt::color::yellow) | fmt::emphasis::bold, "{:>7.5}", scale * read_speed_MBps);
108 fmt::format(fg(fmt::color::spring_green) | fmt::emphasis::bold, "{:^7.5}", scale * write_speed_MBps);
110 fmt::format(fg(fmt::color::orange_red) | fmt::emphasis::bold, "{:<7.5}", scale * drop_speed_MBps);
111 }
112
113 void DataMonitor::start() { asio::co_spawn(*io_context_, print_cycle(), asio::detached); }
114
116 {
117 auto _ = ExitLogger{};
118 clock_.cancel();
119 is_cancelled_.store(true);
120 spdlog::debug("DataMonitor: rate polling clock is killed.");
121 }
122} // namespace srs::workflow
std::chrono::milliseconds period_
DataMonitor(AnalysisHandle *analysis_handle, io_context_type *io_context)
std::chrono::time_point< std::chrono::steady_clock > last_print_time_
asio::steady_timer clock_
gsl::not_null< AnalysisHandle * > analysis_handle_
std::shared_ptr< spdlog::logger > console_
auto print_cycle() -> asio::awaitable< void >
gsl::not_null< io_context_type * > io_context_
std::atomic< bool > is_cancelled_
asio::thread_pool io_context_type