forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratorFileOrCmd.cxx
More file actions
214 lines (193 loc) · 6.03 KB
/
GeneratorFileOrCmd.cxx
File metadata and controls
214 lines (193 loc) · 6.03 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
205
206
207
208
209
210
211
212
213
214
// 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.
/// @author Christian Holm Christensen <cholm@nbi.dk>
#include "SimulationDataFormat/MCUtils.h"
#include "Generators/GeneratorFileOrCmd.h"
// For fifo's and system call
#include <cstdlib>
#include <sys/types.h> // POSIX only
#include <sys/stat.h> // POISX only
#include <cstdio>
// For filesystem operations
#include <filesystem>
// Waits
#include <thread>
// Log messages
#include <fairlogger/Logger.h>
#include <SimConfig/SimConfig.h>
namespace
{
std::string ltrim(const std::string& s, const std::string& what = "\" ' ")
{
auto start = s.find_first_not_of(what);
if (start == std::string::npos) {
return "";
}
return s.substr(start);
}
std::string rtrim(const std::string& s, const std::string& what = "\"' ")
{
auto end = s.find_last_not_of(what);
return s.substr(0, end + 1);
}
std::string trim(const std::string& s, const std::string& what = "\"' ")
{
return rtrim(ltrim(s, what), what);
}
} // namespace
namespace o2
{
namespace eventgen
{
// -----------------------------------------------------------------
void GeneratorFileOrCmd::setup(const GeneratorFileOrCmdParam& param,
const conf::SimConfig& config)
{
setFileNames(param.fileNames);
setCmd(param.cmd);
setOutputSwitch(trim(param.outputSwitch));
setSeedSwitch(trim(param.seedSwitch));
setBmaxSwitch(trim(param.bMaxSwitch));
setNEventsSwitch(trim(param.nEventsSwitch));
setBackgroundSwitch(trim(param.backgroundSwitch));
setSeed(config.getStartSeed());
setNEvents(config.getNEvents());
setBmax(config.getBMax());
}
// -----------------------------------------------------------------
void GeneratorFileOrCmd::setFileNames(const std::string& filenames)
{
std::stringstream s(filenames);
std::string f;
while (std::getline(s, f, ',')) {
mFileNames.push_back(f);
}
}
// -----------------------------------------------------------------
std::string GeneratorFileOrCmd::makeCmdLine() const
{
std::string fileName = mFileNames.front();
std::stringstream s;
s << mCmd << " ";
if (not mSeedSwitch.empty() and mSeedSwitch != "none") {
s << mSeedSwitch << " " << mSeed << " ";
}
if (not mNEventsSwitch.empty() and mNEventsSwitch != "none") {
s << mNEventsSwitch << " " << mNEvents << " ";
}
if (not mBmaxSwitch.empty() and mBmax >= 0 and mBmaxSwitch != "none") {
s << mBmaxSwitch.c_str() << " " << mBmax << " ";
}
s << mOutputSwitch << " " << fileName << " "
<< mBackgroundSwitch;
return s.str();
}
// -----------------------------------------------------------------
bool GeneratorFileOrCmd::executeCmdLine(const std::string& cmd) const
{
LOG(info) << "Command line to execute: \"" << cmd << "\"";
int ret = std::system(cmd.c_str());
if (ret != 0) {
LOG(fatal) << "Failed to spawn \"" << cmd << "\"";
return false;
}
return true;
}
// -----------------------------------------------------------------
bool GeneratorFileOrCmd::makeTemp()
{
mFileNames.clear();
char buf[] = "generatorFifoXXXXXX";
auto fp = mkstemp(buf);
if (fp < 0) {
LOG(fatal) << "Failed to make temporary file: "
<< std::strerror(errno);
return false;
}
mTemporary = std::string(buf);
mFileNames.push_back(mTemporary);
close(fp);
return true;
}
// -----------------------------------------------------------------
bool GeneratorFileOrCmd::removeTemp() const
{
if (mTemporary.empty()) {
LOG(info) << "Temporary file name empty, nothing to remove";
return false;
}
// Get the file we're reading from
std::filesystem::path p(mTemporary);
// Check if the file exists
if (not std::filesystem::exists(p)) {
LOG(info) << "Temporary file " << p << " does not exist";
return true;
}
// Remove temporary file
std::error_code ec;
std::filesystem::remove(p, ec);
if (ec) {
LOG(warn) << "When removing " << p << ": " << ec.message();
}
// Ignore errors when removing the temporary file
return true;
}
// -----------------------------------------------------------------
bool GeneratorFileOrCmd::makeFifo() const
{
// First remove the temporary file if it exists,
// otherwise we may not be able to make the FIFO
removeTemp();
std::string fileName = mFileNames.front();
int ret = mkfifo(fileName.c_str(), 0600);
if (ret != 0) {
LOG(fatal) << "Failed to make fifo \"" << fileName << "\": "
<< std::strerror(errno);
return false;
}
return true;
}
// -----------------------------------------------------------------
bool GeneratorFileOrCmd::ensureFiles()
{
try {
for (auto& f : mFileNames) {
auto c = std::filesystem::canonical(std::filesystem::path(f));
f = c.c_str();
}
} catch (std::exception& e) {
LOG(error) << e.what();
return false;
}
return true;
}
// -----------------------------------------------------------------
void GeneratorFileOrCmd::waitForData(const std::string& filename) const
{
using namespace std::chrono_literals;
// Get the file we're reading from
std::filesystem::path p(filename);
LOG(debug) << "Waiting for data on " << p;
// Wait until child process creates the file
while (not std::filesystem::exists(p)) {
std::this_thread::sleep_for(mWait * 1ms);
}
// Wait until we have more data in the file than just the file
// header
while (std::filesystem::file_size(p) <= 256) {
std::this_thread::sleep_for(mWait * 1ms);
}
// Give the child process 1 second to post the data to the file
LOG(debug) << "Got data in " << p << ", sleeping for a while";
std::this_thread::sleep_for(mWait * 2ms);
}
} /* namespace eventgen */
} /* namespace o2 */