-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathDDSConfigHelpers.cxx
More file actions
204 lines (192 loc) · 6.42 KB
/
DDSConfigHelpers.cxx
File metadata and controls
204 lines (192 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "DDSConfigHelpers.h"
#include "ChannelSpecHelpers.h"
#include <map>
#include <iostream>
#include <cstring>
#include <regex>
#include <fmt/format.h>
#include <libgen.h>
namespace o2::framework
{
struct ChannelProperties {
std::string_view key;
std::string_view value;
};
struct ChannelRewriter : FairMQChannelConfigParser {
void beginChannel() override
{
names.push_back(-1);
isZMQ.push_back(false);
hasAddress.push_back(false);
isWrite.push_back(false);
propertiesBegin.push_back(propertyIndex);
}
void endChannel() override
{
propertiesEnd.push_back(propertyIndex);
if (names.back() == -1) {
throw std::runtime_error("Channel does not have a name.");
}
// If we have a zmq channel which does not have an address,
// use a DDS property.
if (isZMQ.back() && (hasAddress.back() == false)) {
requiresProperties.push_back(channelIndex);
}
channelIndex++;
}
void property(std::string_view key, std::string_view value) override
{
properties.push_back({key, value});
if (key == "address") {
hasAddress.back() = true;
}
if (key == "transport" && value == "zeromq") {
isZMQ.back() = true;
}
// Channels that bind need to write the bound address
if (key == "method" && value == "bind") {
isWrite.back() = true;
}
if (key == "name") {
names.back() = propertyIndex;
}
propertyIndex++;
}
int propertyIndex = 0;
int channelIndex = 0;
std::vector<ChannelProperties> properties;
std::vector<int> names;
std::vector<int> requiresProperties;
std::vector<int> propertiesBegin;
std::vector<int> propertiesEnd;
std::vector<bool> isWrite;
std::vector<bool> isZMQ;
std::vector<bool> hasAddress;
};
void dumpDeviceSpec2DDS(std::ostream& out,
std::string const& workflowSuffix,
const std::vector<DeviceSpec>& specs,
const std::vector<DeviceExecution>& executions,
const CommandInfo& commandInfo)
{
out << R"(<topology name="o2-dataflow">)"
"\n";
assert(specs.size() == executions.size());
std::vector<ChannelRewriter> rewriters;
rewriters.resize(specs.size());
// Find out if we need properties
// and a property for each zmq channel which does not have
// and address.
for (size_t di = 0; di < specs.size(); ++di) {
auto& rewriter = rewriters[di];
auto& execution = executions[di];
for (size_t cci = 0; cci < execution.args.size(); cci++) {
const char* arg = execution.args[cci];
if (!arg) {
break;
}
if (strcmp(arg, "--channel-config") == 0) {
if (cci + 1 == execution.args.size()) {
throw std::runtime_error("wrong channel config found");
}
ChannelSpecHelpers::parseChannelConfig(execution.args[cci + 1], rewriter);
}
}
for (int ci : rewriter.requiresProperties) {
out << " "
<< fmt::format("<property name=\"fmqchan_{}\" />\n", rewriter.properties[rewriter.names[ci]].value);
}
}
float timeout = 0.0;
for (size_t di = 0; di < specs.size(); ++di) {
auto& spec = specs[di];
auto& execution = executions[di];
if (execution.args.empty()) {
continue;
}
out << " "
<< fmt::format("<decltask name=\"{}{}\">\n", spec.id, workflowSuffix);
out << " "
<< R"(<exe reachable="true">)";
out << std::regex_replace(fmt::format("sleep {}; ", timeout) + commandInfo.command, std::regex{"--dds(?!-)"}, "--dump") << " | ";
timeout += 0.2;
for (auto ei : execution.environ) {
out << fmt::format(ei,
fmt::arg("timeslice0", spec.inputTimesliceId),
fmt::arg("timeslice1", spec.inputTimesliceId + 1),
fmt::arg("timeslice4", spec.inputTimesliceId + 4))
<< " ";
}
std::string accumulatedChannelPrefix;
char* s = strdup(execution.args[0]);
out << basename(s) << " ";
free(s);
for (size_t ai = 1; ai < execution.args.size(); ++ai) {
const char* arg = execution.args[ai];
if (!arg) {
break;
}
if (strcmp(arg, "--id") == 0 && ai + 1 < execution.args.size()) {
out << fmt::format(R"(--id {}_dds%TaskIndex%_%CollectionIndex% )", execution.args[ai + 1]);
ai++;
continue;
}
// Do not print out the driver client explicitly
if (strcmp(arg, "--driver-client-backend") == 0) {
ai++;
continue;
}
if (strcmp(arg, "--control") == 0) {
ai++;
continue;
}
if (strcmp(arg, "--channel-prefix") == 0 &&
ai + 1 < execution.args.size() &&
*execution.args[ai + 1] == 0) {
ai++;
continue;
}
if (strpbrk(arg, "' ;@") != nullptr || arg[0] == 0) {
out << fmt::format(R"("{}" )", arg);
} else if (strpbrk(arg, "\"") != nullptr || arg[0] == 0) {
out << fmt::format(R"('{}' )", arg);
} else {
out << fmt::format(R"({} )", arg);
}
}
out << "--plugin odc";
if (accumulatedChannelPrefix.empty() == false) {
out << " --channel-config \"" << accumulatedChannelPrefix << "\"";
}
out << "</exe>\n";
auto& rewriter = rewriters[di];
if (rewriter.requiresProperties.empty() == false) {
out << " <properties>\n";
for (auto pi : rewriter.requiresProperties) {
out << fmt::format(
" <name access=\"{}\">fmqchan_{}</name>\n",
rewriter.isWrite[pi] ? "write" : "read",
rewriter.properties[rewriter.names[pi]].value);
}
out << " </properties>\n";
}
out << " </decltask>\n";
}
out << " <declcollection name=\"DPL\">\n <tasks>\n";
for (const auto& spec : specs) {
out << fmt::format(" <name>{}{}</name>\n", spec.id, workflowSuffix);
}
out << " </tasks>\n </declcollection>\n";
out << "</topology>\n";
}
} // namespace o2::framework