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