9 #include <daq/rawdata/modules/copper.h>
10 #include <daq/rawdata/DesSerCOPPER.h>
13 #include <sys/ioctl.h>
24 DesSerCOPPER::DesSerCOPPER(
string host_recv,
int port_recv,
const string& host_send,
int port_send,
int shmflag,
25 const std::string& nodename,
int nodeid,
int finesse_bitflag)
27 m_finesse_bit_flag = finesse_bitflag;
29 for (
int i = 0 ; i < m_num_connections; i++) {
31 m_hostname_from.push_back(host_recv);
33 m_port_from.push_back(port_recv) ;
34 m_socket_recv.push_back(-1);
38 m_port_to = port_send;
40 m_hostname_local = host_send;
41 m_nodename = nodename;
48 printf(
"[DEBUG] DeSerializerPrePC: Constructor done.\n"); fflush(stdout);
53 DesSerCOPPER::~DesSerCOPPER()
57 void DesSerCOPPER::DataAcquisition()
61 printf(
"[DEBUG] initializing...\n"); fflush(stdout);
66 printf(
"[DEBUG] Done.\n"); fflush(stdout);
68 unsigned int m_prev_ftsweve32 = 0xffffffff;
70 if (m_start_flag == 0) {
77 if (m_status.isAvailable()) {
79 printf(
"[DEBUG] DeSerializerPrePC: Waiting for Start...\n"); fflush(stdout);
80 m_status.reportRunning();
82 m_start_time = getTimeSec();
94 if (m_run_pause > 0 || m_run_error > 0) {
107 for (
int j = 0; j < NUM_EVT_PER_BASF2LOOP_PC; j++) {
118 int delete_flag = 0, m_size_word = 0;
120 if (m_start_flag == 0) {
121 printf(
"[DEBUG] DeSerializerCOPPER: Reading the 1st event from COPPER FIFO... run %.8x eve %.8x\n", m_prev_exprunsubrun_no,
122 m_prev_ftsweve32); fflush(stdout);
126 temp_buf = readOneEventFromCOPPERFIFO(j, &delete_flag, &m_size_word);
127 m_status.copyEventHeader(temp_buf);
128 if (m_start_flag == 0) {
129 printf(
"[DEBUG] DeSerializerCOPPER: Done. the size of the 1st event is %d words\n", m_size_word); fflush(stdout);
131 const int num_nodes = 1;
132 const int num_events = 1;
134 temp_rawcopper.
SetBuffer(temp_buf, m_size_word, 0, num_events, num_nodes);
137 m_prev_ftsweve32 = temp_rawcopper.
FillTopBlockRawHeader(m_nodeid, m_prev_ftsweve32, m_prev_exprunsubrun_no, &m_exprunsubrun_no);
138 m_prev_exprunsubrun_no = m_exprunsubrun_no;
140 }
catch (
string err_str) {
141 print_err.PrintError(m_shmflag, &m_status, err_str);
148 m_recvd_totbytes += m_size_word *
sizeof(int);
152 raw_datablk[ j ].
SetBuffer(temp_buf, m_size_word, delete_flag, num_events, num_nodes);
154 }
catch (
string err_str) {
155 printf(
"Error was detected\n"); fflush(stdout);
162 if (m_run_pause != 0 || m_run_error != 0)
continue;
169 if (m_start_flag == 0) {
170 m_start_time = getTimeSec();
177 for (
int j = 0; j < NUM_EVT_PER_BASF2LOOP_PC; j++) {
178 if (m_start_flag == 0) {
180 printf(
"[DEBUG] SerializerPC: Sending the 1st packet...\n"); fflush(stdout);
183 m_sent_totbytes += sendByWriteV(&(raw_datablk[ j ]));
184 }
catch (
string err_str) {
188 print_err.PrintError((
char*)err_str.c_str(), __FILE__, __PRETTY_FUNCTION__, __LINE__);
191 if (m_start_flag == 0) {
193 printf(
"[DEBUG] Done.\n"); fflush(stdout);
200 if (m_run_pause != 0 || m_run_error != 0)
continue;
206 if (max_nevt >= 0 || max_seconds >= 0.) {
208 if (n_basf2evt % 10000 == 0) {
211 printf(
"[DEBUG] RunStop was detected. ( Setting: Max event # %d MaxTime %lf ) Processed Event %d Elapsed Time %lf[s]\n",
212 max_nevt, max_seconds, n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC, getTimeSec() - m_start_time);
217 if ((n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC) % 100000 == 0) {
218 double interval = cur_time - m_prev_time;
219 double total_time = cur_time - m_start_time;
220 printf(
"[DEBUG] Event %12d Rate %6.2lf[kHz] Recvd %6.2lf[MB/s] sent %6.2lf[MB/s] RunTime %8.2lf[s] interval %8.4lf[s]\n",
221 n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC,
222 (n_basf2evt - m_prev_nevt)*NUM_EVT_PER_BASF2LOOP_PC / interval / 1.e3,
223 (m_recvd_totbytes - m_recvd_prev_totbytes) / interval / 1.e6,
224 (m_sent_totbytes - m_sent_prev_totbytes) / interval / 1.e6,
229 m_prev_time = cur_time;
230 m_recvd_prev_totbytes = m_recvd_totbytes;
231 m_sent_prev_totbytes = m_sent_totbytes;
232 m_prev_nevt = n_basf2evt;
233 cur_time = getTimeSec();
238 if (m_status.isAvailable()) {
239 m_status.setOutputNBytes(m_sent_totbytes);
240 m_status.setOutputCount(n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC);
253 void DesSerCOPPER::waitResume()
255 if (m_run_pause == 0) {
257 if (checkRunPause())
break;
260 printf(
"###########(DesSerCOPPER) Waiting for Runstop() ###############\n");
269 if (m_cpr_fd != -1) close(m_cpr_fd);
275 printf(
"###########(Ser) Waiting for Resume ###############\n");
279 if (checkRunRecovery()) {
288 printf(
"Done!\n"); fflush(stdout);
290 if (CheckConnection(m_socket_send) < 0) {
291 printf(
"Trying Accept1\n"); fflush(stdout);
293 printf(
"Trying Accept2\n"); fflush(stdout);
302 void DesSerCOPPER::resumeRun()
307 printf(
"###########(Ser) the 1st event sicne the resume ###############\n");
322 int* DesSerCOPPER::readOneEventFromCOPPERFIFO(
const int entry,
int* delete_flag,
int* m_size_word)
327 int* temp_buf = m_bufary[ entry ];
328 temp_buf[0] = BUF_SIZE_WORD ;
335 int recvd_byte = (m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS) *
sizeof(
int);
339 if ((read_size = read(m_cpr_fd, (
char*)m_bufary[entry] + recvd_byte,
sizeof(
int) * BUF_SIZE_WORD - recvd_byte)) < 0) {
340 if (errno == EINTR) {
342 }
else if (errno == EAGAIN || errno == EWOULDBLOCK) {
344 if (recvd_byte > (
int)((m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS) *
sizeof(
int))) {
346 sprintf(err_buf,
"[FATAL] EAGAIN return in the middle of an event( COPPER driver should't do this.). Exting...");
347 print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
354 callCheckRunPause(err_str);
360 sprintf(err_buf,
"[FATAL] Failed to read data from COPPER. Exiting...");
361 print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
365 recvd_byte += read_size;
366 if (recvd_byte - (m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS) *
sizeof(
int) > (
int)(
sizeof(
int) *
367 (m_pre_rawcpr.POS_DATA_LENGTH + 1)))
break;
374 *m_size_word = m_bufary[ entry ][ m_pre_rawcpr.POS_DATA_LENGTH + (m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS) ]
375 + m_pre_rawcpr.SIZE_COPPER_DRIVER_HEADER + m_pre_rawcpr.SIZE_COPPER_DRIVER_TRAILER
376 + m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS +
377 m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS;
382 if ((
int)((*m_size_word - m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS) *
sizeof(
int)) > recvd_byte) {
385 if (*m_size_word > BUF_SIZE_WORD) {
387 temp_buf =
new int[ *m_size_word ];
388 memcpy(temp_buf, m_bufary[ entry ], recvd_byte);
389 recvd_byte += readFD(m_cpr_fd, (
char*)temp_buf + recvd_byte,
390 (*m_size_word - m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS) *
sizeof(
int) - recvd_byte, *delete_flag);
393 recvd_byte += readFD(m_cpr_fd, (
char*)(m_bufary[ entry ]) + recvd_byte,
394 (*m_size_word - m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS) *
sizeof(
int) - recvd_byte, *delete_flag);
397 if ((
int)((*m_size_word - m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS) *
sizeof(
int)) != recvd_byte) {
400 sprintf(err_buf,
"[FATAL] CORRUPTED DATA: Read less bytes(%d) than expected(%lu:%d). Exiting...\n",
402 *m_size_word *
sizeof(
int) - m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS *
sizeof(
int),
403 m_bufary[ entry ][ m_pre_rawcpr.POS_DATA_LENGTH ]);
404 print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
407 }
else if ((
int)((*m_size_word - m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS) *
sizeof(
int)) < recvd_byte) {
409 sprintf(err_buf,
"[FATAL] CORRUPTED DATA: Read more than data size. Exiting...: %d %lu %lu %d %d\n",
410 recvd_byte, *m_size_word *
sizeof(
int), m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS *
sizeof(
int),
411 m_bufary[ entry ][ m_pre_rawcpr.POS_DATA_LENGTH ], m_pre_rawcpr.POS_DATA_LENGTH);
412 print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
419 *m_size_word = 256 + entry;
420 m_bufary[entry][0] = *m_size_word;
426 temp_buf[ 0 ] = *m_size_word;
430 if (n_basf2evt >= 50000 && n_basf2evt < 50500) {
431 cur_time = getTimeSec();
432 time_array2[ n_basf2evt - 50000 ] = cur_time - m_start_time;
437 unsigned int checksum = 0;
438 for (
int i = 0; i < m_bufary[entry][0]; i++) {
439 if (i != 2) checksum += m_bufary[entry][i];
441 m_bufary[entry][2] = checksum;
448 void DesSerCOPPER::initializeCOPPER()
455 if ((m_finesse_bit_flag & 0x1) == 1) {
457 m_use_slot |= 1 << slot_shift;
460 if (((m_finesse_bit_flag >> 1) & 0x1) == 1) {
462 m_use_slot |= 1 << slot_shift;
465 if (((m_finesse_bit_flag >> 2) & 0x1) == 1) {
467 m_use_slot |= 1 << slot_shift;
470 if (((m_finesse_bit_flag >> 3) & 0x1) == 1) {
472 m_use_slot |= 1 << slot_shift;
478 char err_buf[100] =
"[FATAL] Slot is not specified. Exiting...";
479 print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
484 for (slot = 0; slot < 4; slot++) {
485 if (m_use_slot & (1 << slot)) printf(
" %c",
'A' + slot);
495 printf(
"[DEBUG] Opening COPPER...\n"); fflush(stdout);
498 printf(
"[DEBUG] Done.\n"); fflush(stdout);
503 void DesSerCOPPER::openCOPPER()
506 if (m_cpr_fd != -1) {
513 if ((m_cpr_fd = open(
"/dev/copper/copper", O_RDONLY)) == -1) {
515 sprintf(err_buf,
"[FATAL] Failed to open /dev/copper/copper. Exiting... ");
516 print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
522 ioctl(m_cpr_fd, CPRIOSET_LEF_WA_FF, &set_regval);
523 ioctl(m_cpr_fd, CPRIOSET_LEF_WB_FF, &set_regval);
524 ioctl(m_cpr_fd, CPRIOSET_LEF_WC_FF, &set_regval);
525 ioctl(m_cpr_fd, CPRIOSET_LEF_WD_FF, &set_regval);
526 ioctl(m_cpr_fd, CPRIOSET_FINESSE_STA, &m_use_slot,
sizeof(m_use_slot));
530 ioctl(m_cpr_fd, CPRIOSET_LEF_WA_AF, &v,
sizeof(v));
531 ioctl(m_cpr_fd, CPRIOSET_LEF_WB_AF, &v,
sizeof(v));
532 ioctl(m_cpr_fd, CPRIOSET_LEF_WC_AF, &v,
sizeof(v));
533 ioctl(m_cpr_fd, CPRIOSET_LEF_WD_AF, &v,
sizeof(v));
537 printf(
"[DEBUG] DeSerializerCOPPER: openCOPPER() done.\n"); fflush(stdout);
543 int DesSerCOPPER::readFD(
int fd,
char* buf,
int data_size_byte,
int )
549 if ((read_size = read(fd, (
char*)buf + n, data_size_byte - n)) < 0) {
550 if (errno == EINTR) {
552 }
else if (errno == EAGAIN || errno == EWOULDBLOCK) {
555 sprintf(err_buf,
"[FATAL] Return due to EAGAIN in the middle of an event( COPPER driver would't do this.). Exting...");
556 print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
563 callCheckRunPause(err_str);
564 }
catch (
string err_str) {
568 printf(
"[WARNING] Delete buffer before going to Run-pause state\n"); fflush(stdout);
579 printf(
"[ERROR] Failed to read data from COPPER. %s %s %d",
580 __FILE__, __PRETTY_FUNCTION__, __LINE__);
581 string err_str =
"RUN_ERROR";
585 sprintf(err_buf,
"[FATAL] Failed to read data from COPPER. %s %s %d",
586 __FILE__, __PRETTY_FUNCTION__, __LINE__);
587 print_err.PrintError(m_shmflag, &m_status, err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
592 if (n == data_size_byte)
break;
Abstract base class for different kinds of events.