srs-control 0.2.2
Loading...
Searching...
No Matches
AnalysisHandle.cpp
Go to the documentation of this file.
2#include "srs/Application.hpp"
10#include <asio/any_io_executor.hpp>
11#include <cassert>
12#include <chrono>
13#include <cstddef>
14#include <cstdint>
15#include <memory>
16#include <mutex>
17#include <spdlog/spdlog.h>
18
19namespace srs::workflow
20{
21
22 AnalysisHandle::AnalysisHandle(App* control, std::size_t n_lines)
23 : is_data_drop_warn_{ control->get_config().warn_if_data_drop }
24 , n_lines_{ n_lines }
25 , app_{ control }
26 , monitor_{ this, &(control->get_io_context()) }
27 , buffer_queue_{ BufferQueue{ { .buffer_size = app_->get_config().data_buffer_size,
28 .reserve_size = control->get_config().buffer_queue_capacity / 2,
29 .queue_capacity = control->get_config().buffer_queue_capacity } } }
30 {
31 spdlog::debug("Handler: Setting the capacity of the buffer queue to {}",
32 control->get_config().buffer_queue_capacity);
33 if (app_->get_config().enable_frame_counter_check)
34 {
35 writers_.enable_frame_count_checker();
36 }
37 }
38
40 {
41
42 auto& report = app_->get_report();
43
44 task_diagram_->register_report(report);
45
46 auto frame_stat = AppReport::FrameReadingStat{};
47
48 frame_stat.total_time_ns = total_time_ns_;
49 frame_stat.total_bytes_read = total_read_data_bytes_;
50 frame_stat.n_frames = total_frame_counts_;
51 report.register_frame_reading_result("Workflow", frame_stat);
52
53 buffer_queue_.register_report(report);
54 }
55
56 void AnalysisHandle::start(asio::any_io_executor executor)
57 {
58 auto _ = ExitLogger{};
59 is_stopped_.store(false);
60 task_diagram_ = std::make_unique<TaskDiagram>(*this, n_lines_);
61
62 const auto& app_config = app_->get_config();
63
64 const auto* frame_count_checker = writers_.get_frame_count_checker();
65 if (app_config.enable_frame_counter_check and frame_count_checker != nullptr)
66 {
67
68 frame_counter_monitor_ = std::make_unique<FrameMissMonitor>(
69 FrameMissMonitor::Config{ .sample_size = 200, .sample_time_ms = 10 }, *frame_count_checker);
70 frame_counter_monitor_->launch(executor);
71 }
72
73 if (print_mode_ == print_speed)
74 {
75 monitor_.start();
76 }
77
78 cv_.notify_all();
79 spdlog::trace("Analysis workflow: entering workflow loop");
80
81 task_diagram_->construct_taskflow_and_run(buffer_queue_, is_stopped_);
82 spdlog::trace("Analysis workflow: workflow loop ended");
83 }
84
86 {
87 // stop();
88 spdlog::debug("Analysis workflow: total read data bytes: {} ", total_read_data_bytes_.load());
89 spdlog::debug("Analysis workflow: total frame counts: {} ", total_frame_counts_.load());
90 }
91 // DataProcessor::~DataProcessor() = default;
92
94 {
95 auto _ = ExitLogger{};
96 // stop may be called before start, where the task_diagram_ is constructed. To prevent task_diagram_ to be
97 // nullptr, wait for a condition variable, notified by the start function.
98 auto lock = std::unique_lock{ cv_mutex_ };
99 cv_.wait(lock, [this]() { return task_diagram_ != nullptr; });
100
101 // CAS operation to guarantee the thread safety
102 auto expected = false;
103 spdlog::trace("Analysis workflow: trying to stop ... ");
104 spdlog::trace("Analysis workflow: current is_stopped status: {}", is_stopped_.load());
105 if (is_stopped_.compare_exchange_strong(expected, true))
106 {
107 const auto _ = ExitLogger{ "scope" };
108
109 while (not task_diagram_->is_taskflow_abort_ready())
110 {
111 // spdlog::trace("waiting for task diagram to be abort ready");
112 }
113 spdlog::trace("Analysis workflow: taskflow is ready to abort.");
114 buffer_queue_.enqueue_empty(task_diagram_->get_n_lines());
115 }
116 }
117
119 {
120 assert(task_diagram_ != nullptr);
121 return *task_diagram_;
122 }
123
125 {
126 const auto data_size = read_data.get_size();
127 total_read_data_bytes_ += data_size;
129
130 time_point_ = clock_.now();
131 auto is_success = buffer_queue_.enqueue(read_data, token);
132 total_time_ns_ += static_cast<uint64_t>((clock_.now() - time_point_).count());
133
134 if (not is_success)
135 {
136 total_drop_data_bytes_ += data_size;
138 {
139 spdlog::warn("Data drop as the buffer queue is full: Current size/capacity: {}/{}.",
140 buffer_queue_.size(),
141 buffer_queue_.capacity());
142 }
143 }
144 }
145} // namespace srs::workflow
The primary interface class of SRS-Control.
auto get_config() const -> const auto &
Class to manage the production and consumption of LargeBuffer asynchronously.
std::pair< moodycamel::ProducerToken, moodycamel::ConsumerToken > Token
A queue token type used for enqueue and dequeue action. The token type consists of one producer token...
Class for buffer for storing large data.
auto get_size() const -> std::size_t
Getter for the size.
std::atomic< uint64_t > total_read_data_bytes_
std::atomic< uint64_t > total_drop_data_bytes_
std::condition_variable cv_
auto get_data_workflow() const -> const TaskDiagram &
void read_data_once(LargeBuffer &read_data, BufferQueue::Token &token)
std::unique_ptr< TaskDiagram > task_diagram_
gsl::not_null< App * > app_
void start(asio::any_io_executor executor)
std::atomic< uint64_t > total_frame_counts_
std::atomic< bool > is_stopped_
AnalysisHandle(App *control, std::size_t n_lines=1)
common::DataPrintMode print_mode_
std::chrono::steady_clock clock_
std::unique_ptr< FrameMissMonitor > frame_counter_monitor_
std::chrono::time_point< std::chrono::steady_clock, std::chrono::nanoseconds > time_point_
Class for analysis taskflow.