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>
37const char* g_table =
"datafiles";
38unsigned int g_streamersize = 0;
39char* g_streamerinfo =
new char[1000000];
48 const char* host,
const char* dbtmp)
49 : m_db(db), m_runtype(runtype), m_host(host), m_dbtmp(dbtmp)
66 int getDiskId() {
return m_diskid; }
67 int getFileId() {
return m_fileid; }
70 int open(
const std::string& dir,
int ndisks,
int ,
int ,
int fileid)
81 bool available =
false;
82 for (
int i = 0; i < ndisks; i++) {
83 struct statvfs statfs;
85 sprintf(filename,
"%s%02d", dir.c_str(), m_diskid);
86 statvfs(filename, &statfs);
87 float usage = 1 - ((float)statfs.f_bfree / statfs.f_blocks);
89 sprintf(filename,
"%s%02d/storage/full_flag", dir.c_str(), m_diskid);
90 std::ifstream fin(filename);
95 std::cout <<
"[DEBUG] disk : " << m_diskid <<
" is available" << std::endl;
99 std::cout <<
"[DEBUG] disk : " << m_diskid <<
" is still full" << std::endl;
101 sprintf(filename,
"%s%02d/storage/full_flag", dir.c_str(), m_diskid);
102 std::ofstream fout(filename);
105 B2WARNING(
"disk-" << m_diskid <<
" is full " << usage);
108 if (m_diskid > ndisks) m_diskid = 1;
111 B2FATAL(
"No disk available for writing");
114 std::string filedir = dir + StringUtil::form(
"%02d/storage/%4.4d/%5.5d/",
115 m_diskid, m_expno, m_runno);
116 system((
"mkdir -p " + filedir).c_str());
117 m_filename = StringUtil::form(
"%s.%4.4d.%5.5d.%s.f%5.5d.sroot",
118 m_runtype.c_str(), m_expno, m_runno, m_host.c_str(), m_fileid);
119 m_path = filedir + m_filename;
120 m_file = ::open(m_path.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0664);
121 if (g_diskid > 0 && g_diskid != m_diskid) {
122 B2FATAL(
"disk-" << m_diskid <<
" is already full! Terminating process..");
127 B2FATAL(
"Failed to open file : " << m_path);
132 m_db->execute(
"insert into %s (name, path, host, label, expno, runno, fileno, nevents, chksum, size) "
133 "values ('%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0);",
134 g_table, m_filename.c_str(), m_path.c_str(), m_host.c_str(),
135 m_runtype.c_str(), m_expno, m_runno, m_fileid);
139 write(g_streamerinfo, g_streamersize,
true);
140 B2INFO(
"New file " << m_path <<
" is opened");
148 std::cout <<
"[DEBUG] File closed" << std::endl;
152 stat(m_path.c_str(), &st);
153 std::string d =
Date(st.st_mtime).toString();
156 m_db->execute(
"update %s set time_close = '%s', chksum = %lu, nevents = %lu, "
157 "size = %lu where name = '%s' and host = '%s';",
158 g_table, d.c_str(), m_chksum, m_nevents, m_filesize,
159 m_filename.c_str(), m_host.c_str());
167 int write(
char* evtbuf,
int nbyte,
bool isstreamer =
false)
169 int ret = ::write(m_file, evtbuf, nbyte);
174 m_chksum = adler32(m_chksum, (
unsigned char*)evtbuf, nbyte);
185 std::string m_runtype;
189 std::string m_filename;
194 std::string m_configname;
197 unsigned long long m_filesize;
198 unsigned long long m_chksum;
199 unsigned long long m_nevents;
204void signalHandler(
int)
206 if (g_file) g_file->close();
210int main(
int argc,
char** argv)
213 printf(
"%s : <ibufname> <ibufsize> <hostname> <runtype> <path> <ndisk> "
214 "<filepath_dbtmp> [<obufname> <obufsize> nodename, nodeid]\n", argv[0]);
218 const unsigned interval = 1;
219 const char* ibufname = argv[1];
220 const int ibufsize = atoi(argv[2]);
221 const char* hostname = argv[3];
222 const char* runtype = argv[4];
223 const bool not_record = std::string(runtype).find(std::string(
"null")) != std::string::npos;
224 const char* path = argv[5];
225 const int ndisks = atoi(argv[6]);
226 const char* file_dbtmp = argv[7];
227 const char* obufname = argv[8];
228 const int obufsize = (argc > 8) ? atoi(argv[9]) : -1;
229 const char* nodename = (argc > 9) ? argv[10] :
"";
230 const int nodeid = (argc > 10) ? atoi(argv[11]) : -1;
231 const unsigned int ninput = (argc > 11) ? atoi(argv[12]) : 1;
234 const bool use_info = nodeid >= 0;
236 info.open(nodename, nodeid);
239 for (
unsigned int ib = 0; ib < ninput; ib++) {
240 ibuf[ib].open(StringUtil::form(
"%s_%d", ibufname, ib), ibufsize * 1000000);
242 signal(SIGINT, signalHandler);
243 signal(SIGKILL, signalHandler);
246 config.get(
"database.dbname"),
247 config.get(
"database.user"),
248 config.get(
"database.password"),
249 config.getInt(
"database.port"));
251 if (obufsize > 0) obuf.open(obufname, obufsize * 1000000);
252 if (use_info) info.reportReady();
253 B2DEBUG(1,
"started recording.");
254 unsigned long long nbyte_out [[maybe_unused]] = 0;
255 unsigned int count_out = 0;
256 unsigned int expno = 0;
257 unsigned int runno = 0;
258 unsigned int subno = 0;
259 int* evtbuf =
new int[10000000];
260 g_file =
new FileHandler(db, runtype, hostname, file_dbtmp);
264 unsigned int fileid = 0;
273 if (use_info) info.reportRunning();
275 while ((g_runno = info.getRunNumber()) <= 0) {
278 g_expno = info.getExpNumber();
279 std::cout <<
"[DEBUG] expno = " << g_expno <<
", runno =" << g_runno << std::endl;
281 for (
unsigned int ib = 0; ib < ninput; ib++) {
283 ibuf[ib].read((
int*)&hd,
true,
true);
284 ibuf[ib].read(evtbuf,
true,
true);
286 int nbyte = evtbuf[0];
287 int nword = (nbyte - 1) / 4 + 1;
289 if (not_record)
continue;
290 if (hd.type == MSG_STREAMERINFO) {
291 memcpy(g_streamerinfo, evtbuf, nbyte);
292 g_streamersize = nbyte;
294 if (expno > hd.expno || runno > hd.runno) {
295 B2WARNING(
"Old run was detected => discard event exp = " << hd.expno <<
" (" << expno <<
"), runno" << hd.runno <<
"(" << runno <<
299 if (!newrun || expno < hd.expno || runno < hd.runno) {
306 info.setExpNumber(expno);
307 info.setRunNumber(runno);
308 info.setSubNumber(subno);
309 info.setInputCount(0);
310 info.setInputNBytes(0);
311 info.setOutputCount(0);
312 info.setOutputNBytes(0);
318 oheader->expno = expno;
319 oheader->runno = runno;
324 file.open(path, ndisks, expno, runno, fileid);
330 info.addInputCount(1);
331 info.addInputNBytes(nbyte);
333 if (hd.type == MSG_STREAMERINFO) {
337 file.write((
char*)evtbuf, nbyte);
339 if (!isnew && obufsize > 0 && count_out % interval == 0 && obuf.isWritable(nword)) {
340 obuf.write(evtbuf, nword,
true);
344 info.addOutputCount(1);
345 info.addOutputNBytes(nbyte);
346 info.get()->reserved[0] = file.getFileId();
347 info.get()->reserved[1] = file.getDiskId();
348 info.get()->reserved_f[0] = (float)info.getOutputNBytes() / 1024. / 1024.;
352 B2WARNING(
"no run was initialzed for recording : " << hd.expno <<
"." << hd.runno);
Abstract base class for different kinds of events.