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;
52 FileHandler(
DBInterface* db,
const std::string& runtype,
53 const char* host,
const char* dbtmp)
54 : m_db(db), m_runtype(runtype), m_host(host), m_dbtmp(dbtmp)
66 ~FileHandler()
throw()
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,
reinterpret_cast<unsigned char*
>(evtbuf), nbyte);
193 int ret = ::write(m_file, evtbuf, nbyte);
208 std::string m_runtype;
212 std::string m_filename;
219 unsigned long long m_filesize;
220 unsigned long long m_chksum;
221 unsigned long long m_nevents;
226void signalHandler(
int)
228 if (g_file) g_file->close();
232int main(
int argc,
char** argv)
235 printf(
"%s : <ibufname> <ibufsize> <hostname> <runtype> <path> <ndisk> "
236 "<filepath_dbtmp> [<obufname> <obufsize> nodename, nodeid]\n", argv[0]);
239 const unsigned interval = 1;
240 const char* ibufname = argv[1];
241 const int ibufsize = atoi(argv[2]);
242 const char* hostname = argv[3];
243 const char* runtype = argv[4];
244 const bool not_record = std::string(runtype).find(std::string(
"null")) != std::string::npos;
245 const char* path = argv[5];
246 const int ndisks = atoi(argv[6]);
247 const char* file_dbtmp = argv[7];
248 const int obufsize = (argc > 8) ? atoi(argv[9]) : -1;
249 const char* nodename = (argc > 9) ? argv[10] :
"";
250 g_is_arich = StringUtil::find(runtype,
"arich");
251 const int nodeid = (argc > 10) ? atoi(argv[11]) : -1;
252 const unsigned int ninput = (argc > 11) ? atoi(argv[12]) : 1;
253 g_file_diskid = StringUtil::form(
"/tmp/%s_diskid", nodename);
256 const bool use_info = nodeid >= 0;
258 info.open(nodename, nodeid);
261 for (
unsigned int ib = 0; ib < ninput; ib++) {
262 ibuf[ib].open(StringUtil::form(
"%s_%d", ibufname, ib), ibufsize * 1000000);
264 signal(SIGINT, signalHandler);
265 signal(SIGKILL, signalHandler);
268 config.get(
"database.dbname"),
269 config.get(
"database.user"),
270 config.get(
"database.password"),
271 config.getInt(
"database.port"));
273 if (obufsize > 0) obuf.open(argv[8], obufsize * 1000000);
274 if (use_info) info.reportReady();
275 B2DEBUG(1,
"started recording.");
276 unsigned long long nbyte_out = 0;
277 unsigned int count_out = 0;
278 unsigned int expno = 0;
279 unsigned int runno = 0;
280 unsigned int subno = 0;
281 int* evtbuf =
new int[10000000];
282 g_file =
new FileHandler(db, runtype, hostname, file_dbtmp);
286 unsigned int fileid = 0;
295 if (use_info) info.reportRunning();
298 while ((g_runno = info.getRunNumber()) <= 0) {
301 g_expno = info.getExpNumber();
302 std::cout <<
"[DEBUG] expno = " << g_expno <<
", runno =" << g_runno << std::endl;
305 for (
unsigned int ib = 0; ib < ninput; ib++) {
307 ibuf[ib].read(
reinterpret_cast<int*
>(&hd),
true,
true);
308 ibuf[ib].read(evtbuf,
true,
true);
310 int nbyte = evtbuf[0];
311 int nword = (nbyte - 1) / 4 + 1;
313 if (hd.type == MSG_STREAMERINFO) {
314 memcpy(g_streamerinfo, evtbuf, nbyte);
315 g_streamersize = nbyte;
317 if (expno > hd.expno || runno > hd.runno) {
318 B2WARNING(
"The old run was detected => discard event exp = " << hd.expno <<
319 " (" << expno <<
"), runno" << hd.runno <<
"(" << runno <<
")");
322 if (!newrun || expno < hd.expno || runno < hd.runno) {
329 info.setExpNumber(expno);
330 info.setRunNumber(runno);
331 info.setSubNumber(subno);
332 info.setInputCount(0);
333 info.setInputNBytes(0);
334 info.setOutputCount(0);
335 info.setOutputNBytes(0);
341 oheader->expno = expno;
342 oheader->runno = runno;
348 std::string filename = StringUtil::form(
"%s%02d", path, g_diskid);
349 struct statvfs statfs;
350 statvfs(filename.c_str(), &statfs);
351 float usage = 1 - ((float)statfs.f_bfree / statfs.f_blocks);
352 if (usage > 0.9) g_diskid = 0;
353 file.open(path, ndisks, expno, runno, fileid);
361 info.addInputCount(1);
362 info.addInputNBytes(nbyte);
364 if (hd.type == MSG_STREAMERINFO) {
368 if (nbyte_out > MAX_FILE_SIZE) {
372 std::string filename = StringUtil::form(
"%s%02d", path, g_diskid);
373 struct statvfs statfs;
374 statvfs(filename.c_str(), &statfs);
375 float usage = 1 - ((float)statfs.f_bfree / statfs.f_blocks);
377 B2WARNING(
"disk-" << g_diskid <<
" is already full. Stopping Run#" << runno);
378 std::cout <<
"[STOP=RUNCONTROL]" << std::endl;
382 file.open(path, ndisks, expno, runno, fileid);
386 file.write(
reinterpret_cast<char*
>(evtbuf), nbyte);
389 info.addOutputCount(1);
390 info.addOutputNBytes(nbyte);
391 info.get()->reserved[0] = file.getFileId();
392 info.get()->reserved[1] = file.getDiskId();
393 info.get()->reserved_f[0] = (float)info.getOutputNBytes() / 1024. / 1024.;
397 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.