DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
TriggerInhibitAgent.cpp
Go to the documentation of this file.
1
11
12#include "logging/Logging.hpp"
13
14#include <memory>
15#include <string>
16#include <utility>
17
21#define TRACE_NAME "TriggerInhibitAgent" // NOLINT
22enum
23{
25 TLVL_WORK_STEPS = 10
26};
27
28namespace dunedaq {
29namespace dfmodules {
30
31TriggerInhibitAgent::TriggerInhibitAgent(const std::string& parent_name,
32 std::shared_ptr<trigdecreceiver_t> our_input,
33 std::shared_ptr<triginhsender_t> our_output)
34 : NamedObject(parent_name + "::TriggerInhibitAgent")
35 , m_thread(std::bind(&TriggerInhibitAgent::do_work, this, std::placeholders::_1))
36 , m_queue_timeout(100)
37 , m_threshold_for_inhibit(1)
38 , m_trigger_decision_receiver(our_input)
39 , m_trigger_inhibit_sender(our_output)
40 , m_trigger_number_at_start_of_processing_chain(0)
41 , m_trigger_number_at_end_of_processing_chain(0)
42{}
43
44void
46{
47 TLOG_DEBUG(TLVL_ENTER_EXIT_METHODS) << get_name() << ": Entering start_checking() method";
48 m_thread.start_working_thread();
49 TLOG() << get_name() << " successfully started";
50 TLOG_DEBUG(TLVL_ENTER_EXIT_METHODS) << get_name() << ": Exiting start_checking() method";
51}
52
53void
55{
56 TLOG_DEBUG(TLVL_ENTER_EXIT_METHODS) << get_name() << ": Entering stop_checking() method";
57 m_thread.stop_working_thread();
58 TLOG() << get_name() << " successfully stopped";
59 TLOG_DEBUG(TLVL_ENTER_EXIT_METHODS) << get_name() << ": Exiting stop_checking() method";
60}
61
62void
63TriggerInhibitAgent::do_work(std::atomic<bool>& running_flag)
64{
65 TLOG_DEBUG(TLVL_ENTER_EXIT_METHODS) << get_name() << ": Entering do_work() method";
66
67 // configuration (hard-coded, for now; will be input from calling code later)
68 int fake_busy_interval_sec = 0;
69 std::chrono::seconds chrono_fake_busy_interval(fake_busy_interval_sec);
70 int fake_busy_duration_sec = 0;
71 std::chrono::seconds chrono_fake_busy_duration(fake_busy_duration_sec);
72 int min_interval_between_inhibit_messages_msec = 0;
73 std::chrono::milliseconds chrono_min_interval_between_inhibit_messages(min_interval_between_inhibit_messages_msec);
74
75 // initialization
76 enum LocalState
77 {
78 no_update,
79 free_state,
80 busy_state
81 };
82 std::chrono::steady_clock::time_point current_time = std::chrono::steady_clock::now();
83 // std::chrono::steady_clock::time_point start_time_of_latest_fake_busy = current_time - chrono_fake_busy_duration;
84 std::chrono::steady_clock::time_point last_sent_time = current_time;
85 LocalState requested_state = no_update;
86 LocalState current_state = free_state;
87 int32_t received_message_count = 0;
88 int32_t sent_message_count = 0;
89
90 // work loop
91 while (running_flag.load()) {
92
93 // check if a TriggerDecision message has arrived, and save the trigger
94 // number contained within it, if one has arrived
95 try {
97 ++received_message_count;
98 TLOG_DEBUG(TLVL_WORK_STEPS) << get_name() << ": Popped the TriggerDecision for trigger number "
99 << trig_dec.trigger_number << " off the input queue";
101 } catch (const iomanager::TimeoutExpired& excpt) {
102 // it is perfectly reasonable that there will be no data in the queue some
103 // fraction of the times that we check, so we just continue on and try again later
104 }
105
106 // to-do: add some logic to fake inhibits
107
108 // check if A) we are supposed to be checking the trigger_number difference, and
109 // B) if so, whether an Inhibit should be asserted or cleared
110 uint32_t threshold = m_threshold_for_inhibit.load(); // NOLINT
111 if (threshold > 0) {
114 if (temp_trig_num_at_start >= temp_trig_num_at_end &&
115 (temp_trig_num_at_start - temp_trig_num_at_end) >= threshold) {
116 if (current_state == free_state) {
117 requested_state = busy_state;
118 }
119 } else {
120 if (current_state == busy_state) {
121 requested_state = free_state;
122 }
123 }
124 }
125
126 // to-do: add some logic to periodically send a message even if nothing has changed
127
128 // send an Inhibit messages, if needed (either Busy or Free state)
129 if (requested_state != no_update && requested_state != current_state) {
130 if ((std::chrono::steady_clock::now() - last_sent_time) >= chrono_min_interval_between_inhibit_messages) {
131 dfmessages::TriggerInhibit inhibit_message;
132 if (requested_state == busy_state) {
133 inhibit_message.busy = true;
134 } else {
135 inhibit_message.busy = false;
136 }
137
138 TLOG_DEBUG(TLVL_WORK_STEPS) << get_name() << ": Pushing a TriggerInhibit message with busy state set to "
139 << inhibit_message.busy << " onto the output queue";
140 try {
141 m_trigger_inhibit_sender->send(std::move(inhibit_message), m_queue_timeout);
142 ++sent_message_count;
143#if 0
144 // temporary logging
145 std::ostringstream oss_sent;
146 oss_sent << ": Successfully pushed a TriggerInhibit message with busy state set to " << inhibit_message.busy
147 << " onto the output queue";
148 TLOG() << ProgressUpdate(ERS_HERE, get_name(), oss_sent.str());
149#endif
150 // if we successfully pushed the message to the Sink, then we assume that the
151 // receiver will get it, and we update our internal state accordingly
152 current_state = requested_state;
153 requested_state = no_update;
154 last_sent_time = std::chrono::steady_clock::now();
155 } catch (const iomanager::TimeoutExpired& excpt) {
156 // It is not ideal if we fail to send the inhibit message out, but rather than
157 // retrying some unknown number of times, we simply output a TRACE message and
158 // go on. This has the benefit of being responsive with pulling TriggerDecision
159 // messages off the input queue, and maybe our Busy/Free state will have changed
160 // by the time that the receiver is ready to receive more messages.
161 TLOG_DEBUG(TLVL_WORK_STEPS) << get_name()
162 << ": TIMEOUT pushing a TriggerInhibit message onto the output queue";
163 }
164 }
165 }
166 }
167
168 std::ostringstream oss_summ;
169 oss_summ << ": Exiting the do_work() method, received " << received_message_count
170 << " TriggerDecision messages and sent " << sent_message_count
171 << " TriggerInhibit messages of all types (both Busy and Free).";
172 TLOG() << ProgressUpdate(ERS_HERE, get_name(), oss_summ.str());
173 TLOG_DEBUG(TLVL_ENTER_EXIT_METHODS) << get_name() << ": Exiting do_work() method";
174}
175
176} // namespace dfmodules
177} // namespace dunedaq
#define ERS_HERE
@ TLVL_ENTER_EXIT_METHODS
std::shared_ptr< triginhsender_t > m_trigger_inhibit_sender
std::shared_ptr< trigdecreceiver_t > m_trigger_decision_receiver
std::atomic< daqdataformats::trigger_number_t > m_trigger_number_at_start_of_processing_chain
dunedaq::utilities::WorkerThread m_thread
std::atomic< daqdataformats::trigger_number_t > m_trigger_number_at_end_of_processing_chain
TriggerInhibitAgent(const std::string &, std::shared_ptr< trigdecreceiver_t >, std::shared_ptr< triginhsender_t >)
TriggerInhibitAgent Constructor.
const std::string & get_name() const final
Get the name of this NamedObejct.
#define TLVL_ENTER_EXIT_METHODS
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
#define TLOG(...)
Definition macro.hpp:22
uint64_t trigger_number_t
Type used to represent trigger number.
Definition Types.hpp:24
Including Qt Headers.
A message containing information about a Trigger from Data Selection (or a TriggerDecisionEmulator)
trigger_number_t trigger_number
The trigger number assigned to this TriggerDecision.
Represents a message indicating whether TriggerDecisions should be inhibited.
bool busy
Whether the system is busy.