12 #include <framework/logging/Logger.h>
13 #include <framework/pcore/EvtMessage.h>
15 #include <daq/storage/SharedEventBuffer.h>
17 #include <daq/slc/database/DBHandlerException.h>
19 #include <daq/slc/psql/PostgreSQLInterface.h>
21 #include <daq/slc/readout/RunInfoBuffer.h>
23 #include <daq/slc/base/ConfigFile.h>
24 #include <daq/slc/base/Date.h>
25 #include <daq/slc/base/StringUtil.h>
32 #include <sys/statvfs.h>
37 const unsigned long long GB = 1000 * 1024 * 1024;
38 const unsigned long long MAX_FILE_SIZE = 2 * GB;
39 const char* g_table =
"datafiles";
40 unsigned int g_streamersize = 0;
41 char* g_streamerinfo =
new char[1000000];
50 const char* host,
const char* dbtmp)
51 : m_db(db), m_runtype(runtype), m_host(host), m_dbtmp(dbtmp)
63 int getDiskId() {
return m_diskid; }
64 int getFileId() {
return m_fileid; }
67 int open(
const std::string& dir,
int ndisks,
int ,
int ,
int fileid)
78 bool available =
false;
79 for (
int i = 0; i < ndisks; i++) {
80 struct statvfs statfs;
82 sprintf(filename,
"%s%02d", dir.c_str(), m_diskid);
83 statvfs(filename, &statfs);
84 float usage = 1 - ((float)statfs.f_bfree / statfs.f_blocks);
86 sprintf(filename,
"%s%02d/storage/full_flag", dir.c_str(), m_diskid);
87 std::ifstream fin(filename);
92 std::cout <<
"[DEBUG] disk : " << m_diskid <<
" is available" << std::endl;
96 std::cout <<
"[DEBUG] disk : " << m_diskid <<
" is still full" << std::endl;
98 sprintf(filename,
"%s%02d/storage/full_flag", dir.c_str(), m_diskid);
99 std::ofstream fout(filename);
102 B2WARNING(
"disk-" << m_diskid <<
" is full " << usage);
105 if (m_diskid > ndisks) m_diskid = 1;
108 B2FATAL(
"No disk available for writing");
111 std::string filedir = dir + StringUtil::form(
"%02d/storage/%4.4d/%5.5d/",
112 m_diskid, m_expno, m_runno);
113 system((
"mkdir -p " + filedir).c_str());
114 m_filename = StringUtil::form(
"%s.%4.4d.%5.5d.%s.f%5.5d.sroot",
115 m_runtype.c_str(), m_expno, m_runno, m_host.c_str(), m_fileid);
116 m_path = filedir + m_filename;
117 m_file =
::open(
m_path.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0664);
118 if (g_diskid > 0 && g_diskid != m_diskid) {
119 B2FATAL(
"disk-" << m_diskid <<
" is already full! Terminating process..");
124 B2FATAL(
"Failed to open file : " <<
m_path);
129 m_db->execute(
"insert into %s (name, path, host, label, expno, runno, fileno, nevents, chksum, size) "
130 "values ('%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0);",
131 g_table, m_filename.c_str(),
m_path.c_str(), m_host.c_str(),
132 m_runtype.c_str(), m_expno, m_runno, m_fileid);
136 write(g_streamerinfo, g_streamersize,
true);
137 B2INFO(
"New file " <<
m_path <<
" is opened");
145 std::cout <<
"[DEBUG] File closed" << std::endl;
149 stat(
m_path.c_str(), &st);
150 std::string d =
Date(st.st_mtime).toString();
153 m_db->execute(
"update %s set time_close = '%s', chksum = %lu, nevents = %lu, "
154 "size = %lu where name = '%s' and host = '%s';",
155 g_table, d.c_str(), m_chksum, m_nevents, m_filesize,
156 m_filename.c_str(), m_host.c_str());
164 int write(
char* evtbuf,
int nbyte,
bool isstreamer =
false)
166 int ret = ::write(m_file, evtbuf, nbyte);
171 m_chksum = adler32(m_chksum, (
unsigned char*)evtbuf, nbyte);
182 std::string m_runtype;
186 std::string m_filename;
191 std::string m_configname;
194 unsigned long long m_filesize;
195 unsigned long long m_chksum;
196 unsigned long long m_nevents;
201 void signalHandler(
int)
203 if (g_file) g_file->close();
207 int main(
int argc,
char** argv)
210 printf(
"%s : <ibufname> <ibufsize> <hostname> <runtype> <path> <ndisk> "
211 "<filepath_dbtmp> [<obufname> <obufsize> nodename, nodeid]\n", argv[0]);
215 const unsigned interval = 1;
216 const char* ibufname = argv[1];
217 const int ibufsize = atoi(argv[2]);
218 const char* hostname = argv[3];
219 const char* runtype = argv[4];
220 const bool not_record = std::string(runtype).find(std::string(
"null")) != std::string::npos;
221 const char* path = argv[5];
222 const int ndisks = atoi(argv[6]);
223 const char* file_dbtmp = argv[7];
224 const char* obufname = (argc > 7) ? argv[8] :
"";
225 const int obufsize = (argc > 8) ? atoi(argv[9]) : -1;
226 const char* nodename = (argc > 9) ? argv[10] :
"";
227 const int nodeid = (argc > 10) ? atoi(argv[11]) : -1;
228 const unsigned int ninput = (argc > 11) ? atoi(argv[12]) : 1;
231 const bool use_info = nodeid >= 0;
233 info.open(nodename, nodeid);
236 for (
unsigned int ib = 0; ib < ninput; ib++) {
237 ibuf[ib].open(StringUtil::form(
"%s_%d", ibufname, ib), ibufsize * 1000000);
239 signal(SIGINT, signalHandler);
240 signal(SIGKILL, signalHandler);
243 config.get(
"database.dbname"),
244 config.get(
"database.user"),
245 config.get(
"database.password"),
246 config.getInt(
"database.port"));
248 if (obufsize > 0) obuf.open(obufname, obufsize * 1000000);
249 if (use_info) info.reportReady();
250 B2DEBUG(1,
"started recording.");
251 unsigned long long nbyte_out = 0;
252 unsigned int count_out = 0;
253 unsigned int expno = 0;
254 unsigned int runno = 0;
255 unsigned int subno = 0;
256 int* evtbuf =
new int[10000000];
257 g_file =
new FileHandler(db, runtype, hostname, file_dbtmp);
261 unsigned int fileid = 0;
270 if (use_info) info.reportRunning();
272 while ((g_runno = info.getRunNumber()) <= 0) {
275 g_expno = info.getExpNumber();
276 std::cout <<
"[DEBUG] expno = " << g_expno <<
", runno =" << g_runno << std::endl;
278 for (
unsigned int ib = 0; ib < ninput; ib++) {
280 ibuf[ib].read((
int*)&hd,
true,
true);
281 ibuf[ib].read(evtbuf,
true,
true);
283 int nbyte = evtbuf[0];
284 int nword = (nbyte - 1) / 4 + 1;
286 if (not_record)
continue;
287 if (hd.type == MSG_STREAMERINFO) {
288 memcpy(g_streamerinfo, evtbuf, nbyte);
289 g_streamersize = nbyte;
291 if (expno > hd.expno || runno > hd.runno) {
292 B2WARNING(
"Old run was detected => discard event exp = " << hd.expno <<
" (" << expno <<
"), runno" << hd.runno <<
"(" << runno <<
296 if (!newrun || expno < hd.expno || runno < hd.runno) {
303 info.setExpNumber(expno);
304 info.setRunNumber(runno);
305 info.setSubNumber(subno);
306 info.setInputCount(0);
307 info.setInputNBytes(0);
308 info.setOutputCount(0);
309 info.setOutputNBytes(0);
315 oheader->expno = expno;
316 oheader->runno = runno;
321 file.open(path, ndisks, expno, runno, fileid);
327 info.addInputCount(1);
328 info.addInputNBytes(nbyte);
330 if (hd.type == MSG_STREAMERINFO) {
342 file.write((
char*)evtbuf, nbyte);
344 if (!isnew && obufsize > 0 && count_out % interval == 0 && obuf.isWritable(nword)) {
345 obuf.write(evtbuf, nword,
true);
349 info.addOutputCount(1);
350 info.addOutputNBytes(nbyte);
351 info.get()->reserved[0] = file.getFileId();
352 info.get()->reserved[1] = file.getDiskId();
353 info.get()->reserved_f[0] = (float)info.getOutputNBytes() / 1024. / 1024.;
357 B2WARNING(
"no run was initialzed for recording : " << hd.expno <<
"." << hd.runno);
InputHandler which will read XML from plain files, optionally gzip compressed.
virtual ~FileHandler()
empty, virtual destructor
std::string m_path
Search path to look for files.
virtual InputContext * open(const std::string &path) override
create a new FileContext by searching the file system for a file named like path.
FileHandler(const std::string &uri)
Instantiate a new file handler, using the uri as base search path.
Abstract base class for different kinds of events.
int main(int argc, char **argv)
Run all tests.