13#include <framework/logging/Logger.h>
14#include <framework/pcore/EvtMessage.h>
16#include <daq/storage/SharedEventBuffer.h>
18#include <daq/slc/psql/PostgreSQLInterface.h>
20#include <daq/slc/database/DBHandlerException.h>
22#include <daq/slc/readout/RunInfoBuffer.h>
24#include <daq/slc/base/ConfigFile.h>
25#include <daq/slc/base/Date.h>
26#include <daq/slc/base/StringUtil.h>
33#include <sys/statvfs.h>
38const unsigned long long GB = 1000 * 1024 * 1024;
39const unsigned long long MAX_FILE_SIZE = 8 * GB;
40const char* g_table =
"datafiles";
41unsigned int g_streamersize = 0;
42char* g_streamerinfo =
new char[1000000];
44std::string g_file_diskid;
47bool g_is_arich =
false;
53 const char* host,
const char* dbtmp)
54 : m_db(db), m_runtype(runtype), m_host(host), m_dbtmp(dbtmp)
71 int getDiskId() {
return m_diskid; }
72 int getFileId() {
return m_fileid; }
75 int open(
const std::string& dir,
int ndisks,
int expno,
int runno,
int fileid)
83 m_expno = (g_expno > 0) ? g_expno : expno;
84 m_runno = (g_runno > 0) ? g_runno : runno;
86 bool available =
false;
88 struct statvfs statfs;
95 std::ifstream fin(g_file_diskid.c_str());
99 for (
int i = 0; i < ndisks; i++) {
100 sprintf(filename,
"%s%02d", dir.c_str(), m_diskid);
101 statvfs(filename, &statfs);
102 float usage = 1 - ((float)statfs.f_bfree / statfs.f_blocks);
103 sprintf(filename,
"%s%02d/storage/full_flag", dir.c_str(), m_diskid);
104 std::ifstream fin(filename);
123 std::cout <<
"[DEBUG] disk : " << m_diskid <<
" is available" << std::endl;
126 std::cout <<
"[DEBUG] disk : " << m_diskid <<
" is with full_flag" << std::endl;
128 std::ofstream fout(filename);
131 B2WARNING(
"disk-" << m_diskid <<
" is full " << usage);
134 if (m_diskid > ndisks) m_diskid = 1;
138 B2FATAL(
"No disk available for writing " << __FILE__ <<
":" << __LINE__);
141 std::string filedir = dir + StringUtil::form(
"%02d/storage/%4.4d/%5.5d/", m_diskid, expno, runno);
142 system((
"mkdir -p " + filedir).c_str());
143 m_filename = StringUtil::form(
"%s.%4.4d.%5.5d.%s.f%5.5d.sroot",
144 m_runtype.c_str(), expno, runno, m_host.c_str(), m_fileid);
145 m_path = filedir + m_filename;
146 m_file = ::open(m_path.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0664);
148 std::ofstream fout(g_file_diskid.c_str());
151 B2FATAL(
"Failed to open file : " << m_path);
156 m_db->execute(
"insert into %s (name, path, host, label, expno, runno, fileno, nevents, chksum, size) "
157 "values ('%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0);",
158 g_table, m_filename.c_str(), m_path.c_str(), m_host.c_str(),
159 m_runtype.c_str(), m_expno, m_runno, m_fileid);
163 write(g_streamerinfo, g_streamersize,
true);
164 B2INFO(
"New file " << m_path <<
" is opened");
172 std::cout <<
"[DEBUG] File closed" << std::endl;
176 stat(m_path.c_str(), &st);
177 std::string d =
Date(st.st_mtime).toString();
179 m_db->execute(
"update %s set time_close = '%s', chksum = %lu, nevents = %lu, "
180 "size = %lu where name = '%s' and host = '%s';",
181 g_table, d.c_str(), m_chksum, m_nevents, m_filesize,
182 m_filename.c_str(), m_host.c_str());
190 int write(
char* evtbuf,
int nbyte,
bool isstreamer =
false)
192 m_chksum = adler32(m_chksum, (
unsigned char*)evtbuf, nbyte);
193 int ret = ::write(m_file, evtbuf, nbyte);
208 std::string m_runtype;
212 std::string m_filename;
217 std::string m_configname;
220 unsigned long long m_filesize;
221 unsigned long long m_chksum;
222 unsigned long long m_nevents;
227void signalHandler(
int)
229 if (g_file) g_file->close();
233int main(
int argc,
char** argv)
236 printf(
"%s : <ibufname> <ibufsize> <hostname> <runtype> <path> <ndisk> "
237 "<filepath_dbtmp> [<obufname> <obufsize> nodename, nodeid]\n", argv[0]);
240 const unsigned interval = 1;
241 const char* ibufname = argv[1];
242 const int ibufsize = atoi(argv[2]);
243 const char* hostname = argv[3];
244 const char* runtype = argv[4];
245 const bool not_record = std::string(runtype).find(std::string(
"null")) != std::string::npos;
246 const char* path = argv[5];
247 const int ndisks = atoi(argv[6]);
248 const char* file_dbtmp = argv[7];
249 const char* obufname = argv[8];
250 const int obufsize = (argc > 8) ? atoi(argv[9]) : -1;
251 const char* nodename = (argc > 9) ? argv[10] :
"";
252 g_is_arich = StringUtil::find(runtype,
"arich");
253 const int nodeid = (argc > 10) ? atoi(argv[11]) : -1;
254 const unsigned int ninput = (argc > 11) ? atoi(argv[12]) : 1;
255 g_file_diskid = StringUtil::form(
"/tmp/%s_diskid", nodename);
258 const bool use_info = nodeid >= 0;
260 info.open(nodename, nodeid);
263 for (
unsigned int ib = 0; ib < ninput; ib++) {
264 ibuf[ib].open(StringUtil::form(
"%s_%d", ibufname, ib), ibufsize * 1000000);
266 signal(SIGINT, signalHandler);
267 signal(SIGKILL, signalHandler);
270 config.get(
"database.dbname"),
271 config.get(
"database.user"),
272 config.get(
"database.password"),
273 config.getInt(
"database.port"));
275 if (obufsize > 0) obuf.open(obufname, obufsize * 1000000);
276 if (use_info) info.reportReady();
277 B2DEBUG(1,
"started recording.");
278 unsigned long long nbyte_out = 0;
279 unsigned int count_out = 0;
280 unsigned int expno = 0;
281 unsigned int runno = 0;
282 unsigned int subno = 0;
283 int* evtbuf =
new int[10000000];
284 g_file =
new FileHandler(db, runtype, hostname, file_dbtmp);
288 unsigned int fileid = 0;
297 if (use_info) info.reportRunning();
300 while ((g_runno = info.getRunNumber()) <= 0) {
303 g_expno = info.getExpNumber();
304 std::cout <<
"[DEBUG] expno = " << g_expno <<
", runno =" << g_runno << std::endl;
307 for (
unsigned int ib = 0; ib < ninput; ib++) {
309 ibuf[ib].read((
int*)&hd,
true,
true);
310 ibuf[ib].read(evtbuf,
true,
true);
312 int nbyte = evtbuf[0];
313 int nword = (nbyte - 1) / 4 + 1;
315 if (hd.type == MSG_STREAMERINFO) {
316 memcpy(g_streamerinfo, evtbuf, nbyte);
317 g_streamersize = nbyte;
319 if (expno > hd.expno || runno > hd.runno) {
320 B2WARNING(
"The old run was detected => discard event exp = " << hd.expno <<
321 " (" << expno <<
"), runno" << hd.runno <<
"(" << runno <<
")");
324 if (!newrun || expno < hd.expno || runno < hd.runno) {
331 info.setExpNumber(expno);
332 info.setRunNumber(runno);
333 info.setSubNumber(subno);
334 info.setInputCount(0);
335 info.setInputNBytes(0);
336 info.setOutputCount(0);
337 info.setOutputNBytes(0);
343 oheader->expno = expno;
344 oheader->runno = runno;
350 std::string filename = StringUtil::form(
"%s%02d", path, g_diskid);
351 struct statvfs statfs;
352 statvfs(filename.c_str(), &statfs);
353 float usage = 1 - ((float)statfs.f_bfree / statfs.f_blocks);
354 if (usage > 0.9) g_diskid = 0;
355 file.open(path, ndisks, expno, runno, fileid);
363 info.addInputCount(1);
364 info.addInputNBytes(nbyte);
366 if (hd.type == MSG_STREAMERINFO) {
370 if (nbyte_out > MAX_FILE_SIZE) {
374 std::string filename = StringUtil::form(
"%s%02d", path, g_diskid);
375 struct statvfs statfs;
376 statvfs(filename.c_str(), &statfs);
377 float usage = 1 - ((float)statfs.f_bfree / statfs.f_blocks);
379 B2WARNING(
"disk-" << g_diskid <<
" is already full. Stopping Run#" << runno);
380 std::cout <<
"[STOP=RUNCONTROL]" << std::endl;
384 file.open(path, ndisks, expno, runno, fileid);
388 file.write((
char*)evtbuf, nbyte);
391 info.addOutputCount(1);
392 info.addOutputNBytes(nbyte);
393 info.get()->reserved[0] = file.getFileId();
394 info.get()->reserved[1] = file.getDiskId();
395 info.get()->reserved_f[0] = (float)info.getOutputNBytes() / 1024. / 1024.;
399 B2WARNING(
"no run was initialzed for recording : " << hd.expno <<
"." << hd.runno);
404 if (!isnew && obufsize > 0 && count_out % interval == 0 && obuf.isWritable(nword)) {
405 obuf.write(evtbuf, nword,
true);
Abstract base class for different kinds of events.