DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
ReadoutApplication.cpp
Go to the documentation of this file.
1
20#include "confmodel/Session.hpp"
21
25
28
31
33#include "confmodel/GeoId.hpp"
36#include "confmodel/Service.hpp"
37
43
52
53
54
56
57#include "logging/Logging.hpp"
58#include <fmt/core.h>
59
60#include <string>
61#include <vector>
62
63// using namespace dunedaq;
64// using namespace dunedaq::appmodel;
65
66namespace dunedaq {
67namespace appmodel {
68
69//-----------------------------------------------------------------------------
70
71std::vector<const confmodel::Resource*>
75
76std::vector<const confmodel::DaqModule*>
78{
79
80 TLOG_DEBUG(6) << "Generating modules for application " << this->UID();
81
82 ConfigObjectFactory obj_fac(this);
83 // conffwk::Configuration& confdb = this->configuration();
84
85 //
86 // Extract basic configuration objects
87 //
88
89 // Data reader
90 auto reader_conf = get_data_reader();
91 if (reader_conf == 0) {
92 throw(BadConf(ERS_HERE, "No DataReaderModule configuration given"));
93 }
94 std::string reader_class = reader_conf->get_template_for();
95
96 // Link handler
97 auto dlh_conf = get_link_handler();
98 // What is template for?
99 auto dlh_class = dlh_conf->get_template_for();
100
101 auto tph_conf = get_tp_handler();
102 if (tph_conf==nullptr && get_tp_generation_enabled()) {
103 throw(BadConf(ERS_HERE, "TP generation is enabled but there is no TP data handler configuration"));
104 }
105
106 std::string tph_class = "";
107 if (tph_conf != nullptr && get_tp_generation_enabled()) {
108 tph_class = tph_conf->get_template_for();
109 }
110
111 //
112 // Process the queue rules looking for inputs to our DL/TP handler modules
113 //
114 const QueueDescriptor* dlh_input_qdesc = nullptr;
115 const QueueDescriptor* dlh_reqinput_qdesc = nullptr;
116 const QueueDescriptor* tp_input_qdesc = nullptr;
117 // const QueueDescriptor* tpReqInputQDesc = nullptr;
118 const QueueDescriptor* fa_output_qdesc = nullptr;
119
120 for (auto rule : get_queue_rules()) {
121 auto destination_class = rule->get_destination_class();
122 auto data_type = rule->get_descriptor()->get_data_type();
123 // Why datahander here?
124 if (destination_class == "DataHandlerModule" || destination_class == dlh_class || destination_class == tph_class) {
125 if (data_type == "DataRequest") {
126 dlh_reqinput_qdesc = rule->get_descriptor();
127 } else if ((data_type == "TriggerPrimitive" || data_type == "TriggerPrimitiveVector") && get_tp_generation_enabled()) {
128 tp_input_qdesc = rule->get_descriptor();
129 } else {
130 dlh_input_qdesc = rule->get_descriptor();
131 }
132 } else if (destination_class == "FragmentAggregatorModule") {
133 fa_output_qdesc = rule->get_descriptor();
134 }
135 }
136
137 //
138 // Process the network rules looking for the Fragment Aggregator and TP handler data reuest inputs
139 //
140 const NetworkConnectionDescriptor* fa_net_desc = nullptr;
141 const NetworkConnectionDescriptor* tp_net_desc = nullptr;
142 const NetworkConnectionDescriptor* ta_net_desc = nullptr;
143 const NetworkConnectionDescriptor* ts_net_desc = nullptr;
144 for (auto rule : get_network_rules()) {
145 auto endpoint_class = rule->get_endpoint_class();
146 auto data_type = rule->get_descriptor()->get_data_type();
147
148 if (endpoint_class == "FragmentAggregatorModule") {
149 fa_net_desc = rule->get_descriptor();
150 } else if (data_type == "TPSet") {
151 tp_net_desc = rule->get_descriptor();
152 } else if (data_type == "TriggerActivity") {
153 ta_net_desc = rule->get_descriptor();
154 } else if (data_type == "TimeSync") {
155 ts_net_desc = rule->get_descriptor();
156 }
157 }
158
159 // Create here the Queue on which all data fragments are forwarded to the fragment aggregator
160 // and a container for the queues of data request to TP handler and DLH
161 if (fa_output_qdesc == nullptr) {
162 throw(BadConf(ERS_HERE, "No fragment output queue descriptor given"));
163 }
164 std::vector<const confmodel::Connection*> req_queues;
165 conffwk::ConfigObject frag_queue_obj = obj_fac.create_queue_obj(fa_output_qdesc);
166
167 //
168 // Scan Detector 2 DAQ connections to extract sender, receiver and stream information
169 //
170
171 std::vector<const confmodel::DaqModule*> modules;
172
173 // Loop over the detector to daq connections and generate one data reader per connection
174 // and the cooresponding datalink handlers
175
176 // Collect all streams
177 std::vector<const confmodel::DetectorStream*> all_enabled_det_streams;
178 std::map<uint32_t, const confmodel::Connection*> data_queues_by_sid;
179
180 // std::vector<const conffwk::ConfigObject*> d2d_conn_objs;
181 uint16_t conn_idx = 0;
182
183 for (auto d2d_conn : get_detector_connections()) {
184 if (d2d_conn->is_disabled(*session)) {
185 TLOG_DEBUG(7) << "Ignoring disabled DetectorToDaqConnection " << d2d_conn->UID();
186 continue;
187 }
188
189 TLOG_DEBUG(6) << "Processing DetectorToDaqConnection " << d2d_conn->UID();
190
191 // Are these tests necessary? Schema does not allow 0 cardinality
192 // for these relationships!! TODO
193 if (d2d_conn->senders().empty()) {
194 throw(BadConf(ERS_HERE, "DetectorToDaqConnection does not contain senders"));
195 }
196 if (d2d_conn->receiver() == nullptr) {
197 throw(BadConf(ERS_HERE, "DetectorToDaqConnection does not contain a receiver"));
198 }
199
200 // Find senders and receiver
201 auto det_senders = d2d_conn->senders();
202 auto det_receiver = d2d_conn->receiver();
203
204 std::vector<const confmodel::DetectorStream*> enabled_det_streams;
205 // Loop over streams
206 for (auto stream : d2d_conn->streams()) {
207
208 // Are we sure?
209 if (stream->is_disabled(*session)) {
210 TLOG_DEBUG(7) << "Ignoring disabled DetectorStream " << stream->UID();
211 continue;
212 }
213
214 all_enabled_det_streams.push_back(stream);
215 enabled_det_streams.push_back(stream);
216 }
217
218
219 // Here I want to resolve the type of connection (network, felix, or?)
220 // Rules of engagement: if the receiver interface is network or felix, the receivers should be castable to the counterpart
221 if (reader_class == "DPDKReaderModule" || reader_class == "SocketReaderModule") {
222 if (!d2d_conn->castable("NetworkDetectorToDaqConnection")) {
223 throw(BadConf(ERS_HERE, fmt::format("{} requires NetworkDetectorToDaqConnection, found {} of class {}", reader_class, d2d_conn->UID(), d2d_conn->class_name())));
224 }
225 if ((reader_class == "DPDKReaderModule" && !det_receiver->cast<appmodel::DPDKReceiver>()) ||
226 (reader_class == "SocketReaderModule" && !det_receiver->cast<appmodel::SocketReceiver>())) {
227 throw(BadConf(ERS_HERE, fmt::format("{} requires NWDetDataReceiver, found {} of class {}", reader_class, det_receiver->UID(), det_receiver->class_name())));
228 }
229 }
230 else if (reader_class == "FelixReaderModule") {
231 if (!d2d_conn->castable("FelixDetectorToDaqConnection")) {
232 throw(BadConf(ERS_HERE, fmt::format("{} requires FelixDetectorToDaqConnection, found {} of class {}", reader_class, d2d_conn->UID(), d2d_conn->class_name())));
233 }
234 if (!det_receiver->cast<appmodel::FelixDataReceiver>()) {
235 throw(BadConf(ERS_HERE, fmt::format("FelixReaderModule requires FelixDataReceiver, found {} of class {}", det_receiver->UID(), det_receiver->class_name())));
236 }
237 }
238 // }
239
240 //-----------------------------------------------------------------
241 //
242 // Create DataReaderModule object
243 //
244
245 //
246 // Instantiate DataReaderModule of type DPDKReaderModule
247 //
248
249 // Create the Data reader object
250
251 std::string reader_uid(fmt::format("datareader-{}-{}", this->UID(), std::to_string(conn_idx++)));
252 TLOG_DEBUG(6) << fmt::format("creating OKS configuration object for Data reader class {} with id {}", reader_class, reader_uid);
253 auto reader_obj = obj_fac.create(reader_class, reader_uid);
254
255 // Populate configuration and interfaces (leave output queues for later)
256 reader_obj.set_obj("configuration", &reader_conf->config_object());
257 reader_obj.set_objs("connections", {&d2d_conn->config_object()});
258
259 // Create the raw data queues
260 std::vector<const conffwk::ConfigObject*> data_queue_objs;
261 // keep a map for convenience
262
263 // Create data queues
264 for (auto ds : enabled_det_streams) {
265 conffwk::ConfigObject queue_obj = obj_fac.create_queue_sid_obj(dlh_input_qdesc, ds);
266 const auto* data_queue = obj_fac.get_dal<confmodel::Connection>(queue_obj.UID());
267 data_queue_objs.push_back(&data_queue->config_object());
268 data_queues_by_sid[ds->get_source_id()] = data_queue;
269 }
270
271 reader_obj.set_objs("outputs", data_queue_objs);
272
273 modules.push_back(obj_fac.get_dal<confmodel::DaqModule>(reader_obj.UID()));
274
275 }
276
277
278 //-----------------------------------------------------------------
279 //
280 // Prepare the tp handlers and related queues
281 //
282 std::vector<const confmodel::Connection*> tp_queues;
284
285 // Create TP handler object
286 auto tph_conf_obj = tph_conf->config_object();
287 auto tpsrc_ids = get_tp_source_ids();
288
289 for (auto sid : tpsrc_ids) {
290 conffwk::ConfigObject tp_queue_obj;
291 conffwk::ConfigObject tpreq_queue_obj;
292 std::string tp_uid("tphandler-" + std::to_string(sid->get_sid()));
293 auto tph_obj = obj_fac.create(tph_class, tp_uid);
294 tph_obj.set_by_val<uint32_t>("source_id", sid->get_sid());
295 tph_obj.set_by_val<uint32_t>("detector_id", 1); // 1 == kDAQ
296 tph_obj.set_by_val<bool>("post_processing_enabled", get_ta_generation_enabled());
297 tph_obj.set_obj("module_configuration", &tph_conf_obj);
298
299 // Create the TPs aggregator queue (from RawData Handlers to TP handlers)
300 tp_queue_obj = obj_fac.create_queue_sid_obj(tp_input_qdesc, sid->get_sid());
301 tp_queue_obj.set_by_val<uint32_t>("recv_timeout_ms", 50);
302 tp_queue_obj.set_by_val<uint32_t>("send_timeout_ms", 1);
303
304 tp_queues.push_back(obj_fac.get_dal<confmodel::Connection>(tp_queue_obj.UID()));
305 // Create tp data requests queue from Fragment Aggregator
306 tpreq_queue_obj = obj_fac.create_queue_sid_obj(dlh_reqinput_qdesc, sid->get_sid());
307 req_queues.push_back(obj_fac.get_dal<confmodel::Connection>(tpreq_queue_obj.UID()));
308
309 // Create the tp(set) publishing service
310 conffwk::ConfigObject tp_net_obj = obj_fac.create_net_obj(tp_net_desc, tp_uid);
311
312 // Create the ta(set) publishing service
313 conffwk::ConfigObject ta_net_obj = obj_fac.create_net_obj(ta_net_desc, tp_uid);
314
315 // Register queues with tp hankder
316 tph_obj.set_objs("inputs", { &tp_queue_obj, &tpreq_queue_obj });
317 tph_obj.set_objs("outputs", { &tp_net_obj, &ta_net_obj, &frag_queue_obj });
318 modules.push_back(obj_fac.get_dal<confmodel::DaqModule>(tph_obj.UID()));
319 }
320 }
321
322 // Add output queueus of tps
323 std::vector<const conffwk::ConfigObject*> tp_queue_objs;
324 for (auto q : tp_queues) {
325 tp_queue_objs.push_back(&q->config_object());
326 }
327
328 //-----------------------------------------------------------------
329 //
330 // Create datalink handlers
331 //
332 // Recover the emulation flag
333 auto emulation_mode = reader_conf->get_emulation_mode();
334 for (auto ds : all_enabled_det_streams) {
335
336 uint32_t sid = ds->get_source_id();
337 TLOG_DEBUG(6) << fmt::format("Processing stream {}, id {}, det id {}", ds->UID(), ds->get_source_id(), ds->get_geo_id()->get_detector_id());
338 std::string uid(fmt::format("DLH-{}", sid));
339 TLOG_DEBUG(6) << fmt::format("creating OKS configuration object for Data Link Handler class {}, if {}", dlh_class, sid);
340 auto dlh_obj = obj_fac.create(dlh_class, uid);
341 dlh_obj.set_by_val<uint32_t>("source_id", sid);
342 dlh_obj.set_by_val<uint32_t>("detector_id", ds->get_geo_id()->get_detector_id());
343 dlh_obj.set_by_val<bool>("post_processing_enabled", get_tp_generation_enabled());
344 dlh_obj.set_by_val<bool>("emulation_mode", emulation_mode);
345 dlh_obj.set_obj("geo_id", &ds->get_geo_id()->config_object());
346 dlh_obj.set_obj("module_configuration", &dlh_conf->config_object());
347
348 std::vector<const conffwk::ConfigObject*> dlh_ins, dlh_outs;
349
350 // Add datalink-handler queue to the inputs
351 dlh_ins.push_back(&data_queues_by_sid.at(sid)->config_object());
352
353 // Create request queue
354 conffwk::ConfigObject req_queue_obj = obj_fac.create_queue_sid_obj(dlh_reqinput_qdesc, ds);
355
356
357 // Add the requessts queue dal pointer to the outputs of the FragmentAggregatorModule
358 req_queues.push_back(obj_fac.get_dal<confmodel::Connection>(req_queue_obj.UID()));
359 dlh_ins.push_back(&req_queue_obj);
360 dlh_outs.push_back(&frag_queue_obj);
361
362
363 // Time Sync network connection
364 if (dlh_conf->get_generate_timesync()) {
365 // Add timestamp endpoint
366 conffwk::ConfigObject ts_net_obj = obj_fac.create_net_obj(ts_net_desc, std::to_string(sid));
367 dlh_outs.push_back(&ts_net_obj);
368 }
369
370 for (auto tpq : tp_queue_objs) {
371 dlh_outs.push_back(tpq);
372 }
373 dlh_obj.set_objs("inputs", dlh_ins);
374 dlh_obj.set_objs("outputs", dlh_outs);
375
376 modules.push_back(obj_fac.get_dal<confmodel::DaqModule>(dlh_obj.UID()));
377 }
378
379
380 // Finally create Fragment Aggregator
381 std::string faUid("fragmentaggregator-" + UID());
382 // conffwk::ConfigObject frag_aggr;
383 TLOG_DEBUG(7) << "creating OKS configuration object for Fragment Aggregator class ";
384 auto frag_aggr = obj_fac.create("FragmentAggregatorModule", faUid);
385 conffwk::ConfigObject fa_net_obj = obj_fac.create_net_obj(fa_net_desc);
386
387 // Process special Network rules!
388 // Looking for Fragment rules from DFAppplications in current Session
389 auto sessionApps = session->enabled_applications();
390 std::vector<conffwk::ConfigObject> fragOutObjs;
391 for (auto app : sessionApps) {
392 auto dfapp = app->cast<appmodel::DFApplication>();
393 if (dfapp == nullptr)
394 continue;
395
396 auto dfNRules = dfapp->get_network_rules();
397 for (auto rule : dfNRules) {
398 auto descriptor = rule->get_descriptor();
399 auto data_type = descriptor->get_data_type();
400 if (data_type == "Fragment") {
401 std::string dreqNetUid(descriptor->get_uid_base() + dfapp->UID());
402 // conffwk::ConfigObject frag_conn;
403 // confdb.create(dbfile, "NetworkConnection", dreqNetUid, frag_conn);
404 auto frag_conn = obj_fac.create("NetworkConnection", dreqNetUid);
405
406 frag_conn.set_by_val<std::string>("data_type", descriptor->get_data_type());
407
408 // Override capacity, set to 2x expected number of Fragments
409 frag_conn.set_by_val<int>("capacity", all_enabled_det_streams.size() * 2);
410 frag_conn.set_by_val<std::string>("connection_type", descriptor->get_connection_type());
411
412 auto serviceObj = descriptor->get_associated_service()->config_object();
413 frag_conn.set_obj("associated_service", &serviceObj);
414 fragOutObjs.push_back(frag_conn);
415 } // If network rule has TriggerDecision type of data
416 } // Loop over Apps network rules
417 } // loop over Session specific Apps
418
419 // Add output queueus of data requests and Fragments
420 std::vector<const conffwk::ConfigObject*> fa_output_objs;
421 for (auto& fNet : fragOutObjs) {
422 fa_output_objs.push_back(&fNet);
423 }
424
425 for (auto& q : req_queues) {
426 fa_output_objs.push_back(&q->config_object());
427 }
428
429 frag_aggr.set_objs("inputs", { &fa_net_obj, &frag_queue_obj });
430 frag_aggr.set_objs("outputs", fa_output_objs);
431
432 modules.push_back(obj_fac.get_dal<confmodel::DaqModule>(frag_aggr.UID()));
433
434 return modules;
435}
436
437} // namespace appmodel
438} // namespace dunedaq
#define ERS_HERE
conffwk::ConfigObject create_queue_sid_obj(const QueueDescriptor *qdesc, uint32_t src_id) const
conffwk::ConfigObject create_net_obj(const NetworkConnectionDescriptor *ndesc, std::string uid) const
Helper function that gets a network connection config.
const T * get_dal(std::string uid) const
conffwk::ConfigObject create_queue_obj(const QueueDescriptor *qdesc, std::string uid="") const
conffwk::ConfigObject create(const std::string &class_name, const std::string &id) const
const dunedaq::appmodel::DataHandlerConf * get_tp_handler() const
Get "tp_handler" relationship value.
const std::vector< const dunedaq::confmodel::DetectorToDaqConnection * > & get_detector_connections() const
Get "detector_connections" relationship value. The list of detector channels to be read out by this r...
bool get_ta_generation_enabled() const
Get "ta_generation_enabled" attribute value.
const std::vector< const dunedaq::appmodel::SourceIDConf * > & get_tp_source_ids() const
Get "tp_source_ids" relationship value.
const dunedaq::appmodel::DataHandlerConf * get_link_handler() const
Get "link_handler" relationship value.
virtual std::vector< const dunedaq::confmodel::DaqModule * > generate_modules(const confmodel::Session *) const override
const dunedaq::appmodel::DataReaderConf * get_data_reader() const
Get "data_reader" relationship value.
virtual std::vector< const Resource * > contained_resources() const override
bool get_tp_generation_enabled() const
Get "tp_generation_enabled" attribute value.
const std::vector< const dunedaq::appmodel::NetworkConnectionRule * > & get_network_rules() const
Get "network_rules" relationship value.
const std::vector< const dunedaq::appmodel::QueueConnectionRule * > & get_queue_rules() const
Get "queue_rules" relationship value.
void set_by_val(const std::string &name, T value)
Set attribute value.
void set_objs(const std::string &name, const std::vector< const ConfigObject * > &o, bool skip_non_null_check=false)
Set relationship multi-value.
const std::string & UID() const noexcept
Return object identity.
void set_obj(const std::string &name, const ConfigObject *o, bool skip_non_null_check=false)
Set relationship single-value.
const std::string & UID() const noexcept
conffwk entry point
std::vector< const dunedaq::confmodel::Resource * > to_resources(const std::vector< T * > &vector_of_children)
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
The DUNE-DAQ namespace.
Definition DataStore.hpp:57