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];
47 FileHandler(DBInterface* db,
const std::string& runtype,
48 const char* host,
const char* dbtmp)
49 : m_db(db), m_runtype(runtype), m_host(host), m_dbtmp(dbtmp)
61 ~FileHandler() throw()
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);
136 }
catch (
const DBHandlerException& e) {
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());
160 }
catch (
const DBHandlerException& e) {
167 int write(
char* evtbuf,
int nbyte,
bool isstreamer =
false)
169 int ret = ::write(m_file, evtbuf, nbyte);
174 m_chksum = adler32(m_chksum,
reinterpret_cast<unsigned char*
>(evtbuf), nbyte);
185 std::string m_runtype;
189 std::string m_filename;
196 unsigned long long m_filesize;
197 unsigned long long m_chksum;
198 unsigned long long m_nevents;
203void signalHandler(
int)
205 if (g_file) g_file->close();
209int main(
int argc,
char** argv)
212 printf(
"%s : <ibufname> <ibufsize> <hostname> <runtype> <path> <ndisk> "
213 "<filepath_dbtmp> [<obufname> <obufsize> nodename, nodeid]\n", argv[0]);
217 const unsigned interval = 1;
218 const char* ibufname = argv[1];
219 const int ibufsize = atoi(argv[2]);
220 const char* hostname = argv[3];
221 const char* runtype = argv[4];
222 const bool not_record = std::string(runtype).find(std::string(
"null")) != std::string::npos;
223 const char* path = argv[5];
224 const int ndisks = atoi(argv[6]);
225 const char* file_dbtmp = argv[7];
226 const int obufsize = (argc > 8) ? atoi(argv[9]) : -1;
227 const char* nodename = (argc > 9) ? argv[10] :
"";
228 const int nodeid = (argc > 10) ? atoi(argv[11]) : -1;
229 const unsigned int ninput = (argc > 11) ? atoi(argv[12]) : 1;
232 const bool use_info = nodeid >= 0;
234 info.open(nodename, nodeid);
237 for (
unsigned int ib = 0; ib < ninput; ib++) {
238 ibuf[ib].open(StringUtil::form(
"%s_%d", ibufname, ib), ibufsize * 1000000);
240 signal(SIGINT, signalHandler);
241 signal(SIGKILL, signalHandler);
244 config.get(
"database.dbname"),
245 config.get(
"database.user"),
246 config.get(
"database.password"),
247 config.getInt(
"database.port"));
249 if (obufsize > 0) obuf.open(argv[8], obufsize * 1000000);
250 if (use_info) info.reportReady();
251 B2DEBUG(1,
"started recording.");
252 unsigned long long nbyte_out [[maybe_unused]] = 0;
253 unsigned int count_out = 0;
254 unsigned int expno = 0;
255 unsigned int runno = 0;
256 unsigned int subno = 0;
257 int* evtbuf =
new int[10000000];
258 g_file =
new FileHandler(db, runtype, hostname, file_dbtmp);
262 unsigned int fileid = 0;
271 if (use_info) info.reportRunning();
273 while ((g_runno = info.getRunNumber()) <= 0) {
276 g_expno = info.getExpNumber();
277 std::cout <<
"[DEBUG] expno = " << g_expno <<
", runno =" << g_runno << std::endl;
279 for (
unsigned int ib = 0; ib < ninput; ib++) {
281 ibuf[ib].read(
reinterpret_cast<int*
>(&hd),
true,
true);
282 ibuf[ib].read(evtbuf,
true,
true);
284 int nbyte = evtbuf[0];
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) {
334 file.write(
reinterpret_cast<char*
>(evtbuf), nbyte);
336 int nword = (nbyte - 1) / 4 + 1;
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.