8 #include "des_ser_PCIe40_main.h"
11 #ifdef SPLIT_ECL_ECLTRG
12 const std::vector<int> splitted_ch {16};
16 unsigned int* data_1[NUM_SENDER_THREADS];
17 unsigned int* data_2[NUM_SENDER_THREADS];
20 pthread_mutex_t mtx1_ch[NUM_SENDER_THREADS];
21 pthread_mutex_t mtx2_ch[NUM_SENDER_THREADS];
23 pthread_mutex_t mtx_sender_log;
26 int buffer_filled[NUM_SENDER_THREADS][2];
27 int copy_nwords[NUM_SENDER_THREADS][2];
34 zmq::socket_t* zmq_writer[NUM_SENDER_THREADS];
35 zmq::socket_t* zmq_reader[NUM_SENDER_THREADS];
41 bool exit_on_error = false ;
44 unsigned int max_number_of_messages = 10;
50 unsigned int dmaerr_no_data = 0;
51 unsigned int dmaerr_bad_size = 0;
52 unsigned int dmaerr_bad_size_dmatrl = 0;
53 unsigned int dmaerr_bad_word_size = 0;
54 unsigned int dmaerr_bad_header = 0;
55 unsigned int dmaerr_bad_dmatrl = 0;
60 unsigned int total_crc_good[NUM_SENDER_THREADS] = {0};
61 int total_crc_errors[NUM_SENDER_THREADS] = {0};
62 unsigned int err_flag_cnt[NUM_SENDER_THREADS] = {0};
63 unsigned int cur_evtnum[NUM_SENDER_THREADS] = {0};
65 unsigned int err_not_reduced[NUM_SENDER_THREADS] = {0};
66 unsigned int err_bad_7f7f[NUM_SENDER_THREADS] = {0};
67 unsigned int err_bad_runnum[NUM_SENDER_THREADS] = {0};
68 unsigned int err_bad_linknum[NUM_SENDER_THREADS] = {0};
69 unsigned int err_bad_evenum[NUM_SENDER_THREADS] = {0};
70 unsigned int err_bad_ffaa[NUM_SENDER_THREADS] = {0};
71 unsigned int err_bad_ff55[NUM_SENDER_THREADS] = {0};
72 unsigned int err_bad_linksize[NUM_SENDER_THREADS] = {0};
73 unsigned int err_link_eve_jump[NUM_SENDER_THREADS] = {0};
74 unsigned int crc_err_ch[NUM_SENDER_THREADS][ MAX_PCIE40_CH];
79 std::map< string, unsigned int > host_nodeid;
83 unsigned int n_messages[17] = {0};
104 const int CRC16_XMODEM_TABLE[] = {
105 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
106 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
107 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
108 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
109 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
110 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
111 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
112 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
113 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
114 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
115 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
116 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
117 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
118 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
119 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
120 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
121 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
122 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
123 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
124 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
125 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
126 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
127 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
128 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
129 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
130 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
131 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
132 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
133 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
134 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
135 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
136 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
139 void crc_calc(
unsigned int& crc,
const unsigned int& data)
141 int byte1, byte2, byte3, byte4 ;
143 byte2 = (data & 0xFF00) >> 8;
144 byte3 = (data & 0xFF0000) >> 16;
145 byte4 = (data & 0xFF000000) >> 24;
146 crc = (((crc) << 8) & 0xff00) ^ CRC16_XMODEM_TABLE[(((crc) >> 8) & 0xff)^byte4] ;
147 crc = (((crc) << 8) & 0xff00) ^ CRC16_XMODEM_TABLE[(((crc) >> 8) & 0xff)^byte3] ;
148 crc = (((crc) << 8) & 0xff00) ^ CRC16_XMODEM_TABLE[(((crc) >> 8) & 0xff)^byte2] ;
149 crc = (((crc) << 8) & 0xff00) ^ CRC16_XMODEM_TABLE[(((crc) >> 8) & 0xff)^byte1] ;
152 unsigned int get_crc(
unsigned int* data,
int length,
unsigned int initial_value)
154 unsigned int result = initial_value ;
156 for (
int i = 0 ; i < length ; ++i) {
157 crc_calc(result, data[ i ]) ;
164 int getEventNumber(
unsigned int* data)
166 if (0 != data)
return data[4] ;
170 void printHeader(
unsigned int* data)
173 printf(
"Header : %8X%8X%8X%8X%8X%8X%8X%8X\n", data[7], data[6], data[5], data[4],
174 data[3], data[2], data[1], data[0]) ;
175 }
else printf(
"No data\n") ;
178 void printTrailer(
unsigned int* data)
181 printf(
"Trailer : %8X%8X%8X%8X%8X%8X%8X%8X\n", data[7], data[6], data[5], data[4],
182 data[3], data[2], data[1], data[0]) ;
183 }
else printf(
"No data\n") ;
186 void printData(
unsigned int* data)
189 printf(
"Data : %8X%8X%8X%8X%8X%8X%8X%8X\n", data[7], data[6], data[5], data[4],
190 data[3], data[2], data[1], data[0]) ;
191 }
else printf(
"No data\n") ;
194 void writeToFile(std::ofstream& the_file,
unsigned int* data,
int size)
196 the_file <<
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl ;
197 for (
int i = 0 ; i < 8 * (size - 2) ; ++i) {
198 the_file << std::hex << data[ i ] << std::endl ;
202 void printLine(
unsigned int* data,
int pos)
204 pos = pos - (pos % 8);
205 printf(
"pos %.8x : ", pos);
206 for (
int i = pos; i < pos + 8; i++) {
207 printf(
" %.8x", data[ pos + i]);
213 void printEventData(
unsigned int* data)
215 int eventSize = ((data[ 0 ] & 0xFFFF)) - 1 ;
217 for (
int i = 0 ; i < eventSize; ++i) {
218 printf(
"%.8x ", data[ i ]);
219 if (i % 8 == 7)printf(
"\n eve %.3x : ", i);
224 void printEventData(
unsigned int* data,
int size)
226 printf(
"%.8x : ", 0);
228 for (
int i = 0 ; i < size; ++i) {
229 printf(
"%.8x ", data[ i ]);
230 if (i % 8 == 7)printf(
"\n%.8x : ", i + 1);
232 }
else printf(
"No data\n") ;
237 void printEventData(
unsigned int* data,
int size,
int sender_id)
239 printf(
"thread %d : %.8x : ", sender_id, 0);
241 for (
int i = 0 ; i < size; ++i) {
242 printf(
"%.8x ", data[ i ]);
243 if (i % 8 == 7)printf(
"\nthread %d : %.8x : ", sender_id, i + 1);
245 }
else printf(
"No data\n") ;
250 void printFullData(
unsigned int* data)
252 printf(
"Header : %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
253 data[0], data[1], data[2], data[3],
254 data[4], data[5], data[6], data[7]) ;
258 int eventSize = ((data[ 0 ] & 0xFF)) - 1 ;
260 printf(
" data 0 : ");
261 for (
int i = 0 ; i < eventSize * 8 ; ++i) {
262 printf(
"%.8x ", data[ i ]);
263 if (i % 8 == 7)printf(
"\n data %.3x : ", i);
269 printf(
"\nTrailer : %8X %8X %8X %8X %8X %8X %8X %8X\n", data[8 * eventSize + 7], data[8 * eventSize + 6],
270 data[8 * eventSize + 5], data[8 * eventSize + 4],
271 data[8 * eventSize + 3], data[8 * eventSize + 2], data[8 * eventSize + 1], data[8 * eventSize]) ;
275 int get1stChannel(
unsigned int*& data)
278 unsigned int event_length = data[ Belle2::RawHeader_latest::POS_NWORDS ];
280 for (
int i = 0; i < MAX_PCIE40_CH; i++) {
282 if (i < MAX_PCIE40_CH - 1) {
283 linksize = data[ POS_TABLE_POS + (i + 1) ] - data[ POS_TABLE_POS + i ];
285 linksize = event_length - (data[ POS_TABLE_POS + (MAX_PCIE40_CH - 1) ] + LEN_ROB_TRAILER);
296 void printEventNumberError(
unsigned int*& data,
const unsigned int evtnum,
const unsigned int exprun,
const int eve_diff,
305 unsigned int event_length = data[ Belle2::RawHeader_latest::POS_NWORDS ];
306 char err_buf[2000] = {0};
307 int reduced_flag = 1;
308 if (data[ MAGIC_7F7F_POS ] & 0x00008000) {
312 pthread_mutex_lock(&(mtx_sender_log));
313 n_messages[ 10 ] = n_messages[ 10 ] + 1 ;
314 if (reduced_flag == 1) {
316 "[FATAL] thread %d : %s ch=%d : ERROR_EVENT : Invalid event_number. Exiting...: cur 32bit eve %u preveve %u for all channels : prun %u crun %u\n %s %s %d\n",
317 sender_id, hostnamebuf, get1stChannel(data),
318 data[EVENUM_POS], evtnum + (eve_diff - 1),
319 exprun, data[RUNNO_POS],
320 __FILE__, __PRETTY_FUNCTION__, __LINE__);
323 "[FATAL] thread %d : %s ch=%d : ERROR_EVENT : Invalid event_number. Exiting...: cur 32bit eve %u preveve %u ( ",
324 sender_id, hostnamebuf, get1stChannel(data),
325 data[EVENUM_POS], evtnum + (eve_diff - 1));
327 unsigned int temp_eve = 0;
328 for (
int i = 0; i < MAX_PCIE40_CH; i++) {
330 if (i < MAX_PCIE40_CH - 1) {
331 linksize = data[ POS_TABLE_POS + (i + 1) ] - data[ POS_TABLE_POS + i ];
333 linksize = event_length - (data[ POS_TABLE_POS + (MAX_PCIE40_CH - 1) ] + LEN_ROB_TRAILER);
335 if (linksize <= 0)
continue;
336 temp_pos = data[ POS_TABLE_POS + i ] + OFFSET_HDR;
337 temp_eve = data[ temp_pos +
338 Belle2::PreRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER +
339 Belle2::PreRawCOPPERFormat_latest::POS_TT_TAG ];
341 if (evtnum + eve_diff != temp_eve) {
342 sprintf(err_buf + strlen(err_buf),
343 "ch %d eve 0x%.8x : ",
347 sprintf(err_buf + strlen(err_buf),
"prun %u crun %u\n %s %s %d\n",
348 exprun, data[RUNNO_POS],
349 __FILE__, __PRETTY_FUNCTION__, __LINE__);
351 printf(
"%s\n", err_buf); fflush(stdout);
352 printEventData(data, event_length, sender_id);
353 err_bad_evenum[sender_id]++;
354 pthread_mutex_unlock(&(mtx_sender_log));
359 void checkUtimeCtimeTRGType(
unsigned int*& data,
const int sender_id)
361 unsigned int event_length = data[ Belle2::RawHeader_latest::POS_NWORDS ];
362 unsigned int new_exprun = data[ Belle2::RawHeader_latest::POS_EXP_RUN_NO ] ;
363 unsigned int new_evtnum = data[ Belle2::RawHeader_latest::POS_EVE_NO ] ;
367 if ((data[ MAGIC_7F7F_POS ] & 0xFFFF0000) != 0x7F7F0000) {
368 char err_buf[500] = {0};
369 pthread_mutex_lock(&(mtx_sender_log));
371 "[FATAL] thread %d : %s : ERROR_EVENT : Invalid Magic word in ReadOut Board header( 0x%.8x ) : It must be 0x7f7f???? : eve %d exp %d run %d sub %d : %s %s %d",
372 sender_id, hostnamebuf, data[ MAGIC_7F7F_POS ],
374 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
375 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
376 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
377 __FILE__, __PRETTY_FUNCTION__, __LINE__);
378 printf(
"%s\n", err_buf); fflush(stdout);
379 printEventData(data, event_length, sender_id);
380 pthread_mutex_unlock(&(mtx_sender_log));
381 #ifndef NO_ERROR_STOP
389 if (!(data[ MAGIC_7F7F_POS ] & 0x00008000)) {
391 pthread_mutex_lock(&(mtx_sender_log));
392 printf(
"[FATAL] thread %d : %s : This function cannot be used for already reduced data. 7f7f header is 0x%.8x : eve %d exp %d run %d sub %d : %s %s %d\n",
393 sender_id, hostnamebuf, data[ MAGIC_7F7F_POS ],
395 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
396 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
397 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
398 __FILE__, __PRETTY_FUNCTION__, __LINE__);
399 printEventData(data, event_length, sender_id);
400 pthread_mutex_unlock(&(mtx_sender_log));
401 #ifndef NO_ERROR_STOP
410 int flag = 0, err_flag = 0, err_ch = -1;
411 unsigned int temp_utime = 0, temp_ctime_trgtype = 0, temp_eve = 0, temp_exprun = 0;
412 unsigned int utime[MAX_PCIE40_CH], ctime_trgtype[MAX_PCIE40_CH], eve[MAX_PCIE40_CH], exprun[MAX_PCIE40_CH];
413 int used_ch[MAX_PCIE40_CH] = {0};
417 memset(utime, 0,
sizeof(utime));
418 memset(ctime_trgtype, 0,
sizeof(ctime_trgtype));
419 memset(eve, 0,
sizeof(eve));
420 memset(exprun, 0,
sizeof(exprun));
422 for (
int i = 0; i < MAX_PCIE40_CH; i++) {
423 unsigned int temp_ctime_trgtype_footer = 0, temp_eve_footer = 0;
425 if (i < MAX_PCIE40_CH - 1) {
426 linksize = data[ POS_TABLE_POS + (i + 1) ] - data[ POS_TABLE_POS + i ];
428 linksize = event_length - (data[ POS_TABLE_POS + (MAX_PCIE40_CH - 1) ] + LEN_ROB_TRAILER);
436 int temp_pos = data[ POS_TABLE_POS + i ] + OFFSET_HDR;
437 ctime_trgtype[ i ] = data[ temp_pos +
438 Belle2::PreRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER +
439 Belle2::PreRawCOPPERFormat_latest::POS_TT_CTIME_TYPE ];
440 eve[ i ] = data[ temp_pos +
441 Belle2::PreRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER +
442 Belle2::PreRawCOPPERFormat_latest::POS_TT_TAG ];
443 utime[ i ] = data[ temp_pos +
444 Belle2::PreRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER +
445 Belle2::PreRawCOPPERFormat_latest::POS_TT_UTIME ];
446 exprun[ i ] = data[ temp_pos +
447 Belle2::PreRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER +
448 Belle2::PreRawCOPPERFormat_latest::POS_EXP_RUN ];
449 temp_ctime_trgtype_footer = data[ temp_pos + linksize +
450 - (Belle2::PreRawCOPPERFormat_latest::SIZE_B2LFEE_TRAILER +
451 Belle2::PreRawCOPPERFormat_latest::SIZE_B2LHSLB_TRAILER) +
452 Belle2::PreRawCOPPERFormat_latest::POS_TT_CTIME_B2LFEE ];
453 temp_eve_footer = data[ temp_pos + linksize +
454 - (Belle2::PreRawCOPPERFormat_latest::SIZE_B2LFEE_TRAILER +
455 Belle2::PreRawCOPPERFormat_latest::SIZE_B2LHSLB_TRAILER) +
456 Belle2::PreRawCOPPERFormat_latest::POS_CHKSUM_B2LFEE ];
459 temp_ctime_trgtype = ctime_trgtype[ i ];
461 temp_utime = utime[ i ];
462 temp_exprun = exprun[ i ];
466 if (temp_eve != new_evtnum) {
467 pthread_mutex_lock(&(mtx_sender_log));
468 printf(
"[FATAL] thread %d : %s ch=%d : ERROR_EVENT : Invalid event_number. Exiting...: eve in ROBheader = 0x%.8x , ch %d 's eve = 0x%.8x : exp %d run %d sub %d : %s %s %d\n",
469 sender_id, hostnamebuf, i,
470 new_evtnum, i, temp_eve,
471 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
472 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
473 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
474 __FILE__, __PRETTY_FUNCTION__, __LINE__);
475 printEventData(data, event_length, sender_id);
476 pthread_mutex_unlock(&(mtx_sender_log));
481 if (temp_ctime_trgtype != ctime_trgtype[ i ] || temp_utime != utime[ i ] ||
482 temp_eve != eve[ i ] || temp_exprun != exprun[ i ]) {
492 if (temp_ctime_trgtype != temp_ctime_trgtype_footer || (temp_eve & 0xffff) != ((temp_eve_footer >> 16) & 0xffff)) {
493 pthread_mutex_lock(&(mtx_sender_log));
494 printf(
"[FATAL] thread %d : %s ch=%d : ERROR_EVENT : mismatch(finesse %d) between header(ctime 0x%.8x eve 0x%.8x) and footer(ctime 0x%.8x eve_crc16 0x%.8x). Exiting... : exp %d run %d sub %d : %s %s %d\n",
495 sender_id, hostnamebuf, i, i,
496 temp_ctime_trgtype, temp_eve, temp_ctime_trgtype_footer, temp_eve_footer,
497 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
498 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
499 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
500 __FILE__, __PRETTY_FUNCTION__, __LINE__);
501 printEventData(data, event_length, sender_id);
502 pthread_mutex_unlock(&(mtx_sender_log));
512 pthread_mutex_lock(&(mtx_sender_log));
514 "[FATAL] thread %d : %s ch= %d or %d : ERROR_EVENT : mismatch header value over FINESSEs ( between ch %d and ch %d ). Exiting...: ",
515 sender_id, hostnamebuf, err_ch, first_ch, err_ch, first_ch);
516 for (
int i = 0; i < MAX_PCIE40_CH; i++) {
517 if (used_ch[ i ] == 1) {
518 sprintf(err_buf + strlen(err_buf),
519 "\nch = %d ctimeTRGtype 0x%.8x utime 0x%.8x eve 0x%.8x exprun 0x%.8x",
520 i, ctime_trgtype[ i ], utime[ i ], eve[ i ], exprun[ i ]);
523 printf(
"%s\n", err_buf); fflush(stdout);
524 pthread_mutex_unlock(&(mtx_sender_log));
532 int checkDMAHeader(
unsigned int*& data,
unsigned int& size,
double& dsize,
int& total_pages,
int& index_pages)
535 pthread_mutex_lock(&(mtx_sender_log));
536 n_messages[ 0 ] = n_messages[ 0 ] + 1 ;
537 if (n_messages[ 0 ] < max_number_of_messages) {
538 printf(
"[WARNING] Null pointer to data buffer\n") ;
541 pthread_mutex_unlock(&(mtx_sender_log));
545 unsigned int fragment_size = data[ DMA_WORDS_OF_256BITS ] & 0xFFFF ;
546 dsize += fragment_size * 32 ;
548 if (((data[ DMA_WORDS_OF_256BITS ] & 0xFFFF0000) != 0xEEEE0000) ||
549 (data[ DMA_HDR_MAGIC ] != 0xAAAAEEEE) ||
550 ((data[ DMA_SIZE_IN_BYTES ] & 0xFFFF) != 0xAAAA)) {
551 pthread_mutex_lock(&(mtx_sender_log));
552 n_messages[ 4 ] = n_messages[ 4 ] + 1 ;
553 if (n_messages[ 4 ] < max_number_of_messages) {
554 printf(
"[FATAL] Invalid DMA header format. ( %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x )\n",
555 data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]) ;
559 pthread_mutex_unlock(&(mtx_sender_log));
560 #ifndef NO_ERROR_STOP
564 }
else if ((data[ DMA_WORDS_OF_256BITS ] & 0xFFFF) > MAX_DMA_WORDS_OF_256BITS) {
565 pthread_mutex_lock(&(mtx_sender_log));
566 n_messages[ 2 ] = n_messages[ 2 ] + 1 ;
567 if (n_messages[ 2 ] < max_number_of_messages) {
568 printf(
"[FATAL] Too large DMA packet(= %lf bytes). ( %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x )\n",
570 data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]) ;
573 pthread_mutex_unlock(&(mtx_sender_log));
574 #ifndef NO_ERROR_STOP
578 }
else if (((data[ DMA_SIZE_IN_BYTES ] & 0xFFFF0000) >> 16) != (fragment_size * 32)) {
579 pthread_mutex_lock(&(mtx_sender_log));
580 n_messages[ 3 ] = n_messages[ 3 ] + 1 ;
581 if (n_messages[ 3 ] < max_number_of_messages) {
582 printf(
"[FATAL] Inconsistent between byte-size( = %u ) and 8words-size( = %u ) in DMA header. ( %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x )\n"
584 (data[ DMA_SIZE_IN_BYTES ] & 0xFFFF0000) >> 16, fragment_size * 32,
585 data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]) ;
587 dmaerr_bad_word_size++;
588 pthread_mutex_unlock(&(mtx_sender_log));
589 #ifndef NO_ERROR_STOP
596 if (data[ 8 * (fragment_size - 1) ] != fragment_size) {
597 pthread_mutex_lock(&(mtx_sender_log));
598 n_messages[ 5 ] = n_messages[ 5 ] + 1 ;
599 if (n_messages[ 5 ] < max_number_of_messages) {
600 printf(
"Bad size in trailer : size %.8x size in hdr %.8x bef %.8x\n", data[8 * (fragment_size - 1)], fragment_size,
601 data[ 8 * (fragment_size - 2) ]) ;
602 printLine(data, 8 * (fragment_size - 1));
606 if (data[ 8 * (fragment_size - 2) ] == fragment_size) {
607 dmaerr_bad_size_dmatrl++;
610 pthread_mutex_unlock(&(mtx_sender_log));
625 total_pages = (data[ 4 ] & 0xFFFF0000) >> 16 ;
626 index_pages = (data[ 4 ] & 0xFFFF) ;
628 size = fragment_size ;
643 gettimeofday(&t, NULL);
644 return (t.tv_sec + t.tv_usec * 1.e-6);
647 void reduceHdrTrl(
unsigned int* data,
unsigned int& event_nwords)
651 unsigned int event_length = data[ Belle2::RawHeader_latest::POS_NWORDS ];
652 if (event_length > 0x100000) {
653 pthread_mutex_lock(&(mtx_sender_log));
654 printf(
"[FATAL] Too large event size. : 0x%.8x : %u words. Exiting...\n",
655 data[ Belle2::RawHeader_latest::POS_NWORDS ],
657 printEventData(data, (event_length & 0xfffff));
658 pthread_mutex_unlock(&(mtx_sender_log));
662 unsigned int* temp_data =
new unsigned int[event_length];
663 memset(temp_data, 0, event_length *
sizeof(
unsigned int));
665 if (data[ Belle2::RawHeader_latest::POS_VERSION_HDRNWORDS ] & 0x00008000) {
667 data[ Belle2::RawHeader_latest::POS_VERSION_HDRNWORDS ] =
668 data[ Belle2::RawHeader_latest::POS_VERSION_HDRNWORDS ] & 0xffff7fff;
670 data[ Belle2::RawHeader_latest::POS_TRUNC_MASK_DATATYPE ] = 0;
672 pthread_mutex_lock(&(mtx_sender_log));
673 printf(
"[FATAL] reduceHdrTrl() must not be used for already reduced-event. 7f7f word = %.8x . Exiting...\n",
674 data[ Belle2::RawHeader_latest::POS_VERSION_HDRNWORDS ]);
676 pthread_mutex_unlock(&(mtx_sender_log));
681 unsigned int cur_pos = 0;
682 unsigned int dst_cur_pos = 0;
683 memcpy(temp_data + dst_cur_pos, data + cur_pos,
684 Belle2::RawHeader_latest::POS_CH_POS_TABLE *
sizeof(
unsigned int));
686 cur_pos = Belle2::RawHeader_latest::POS_CH_POS_TABLE;
687 dst_cur_pos = Belle2::RawHeader_latest::POS_CH_POS_TABLE;
691 int red_linksize = 0;
693 temp_data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE ] = data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE ];
694 for (
int i = 0; i < MAX_PCIE40_CH ; i++) {
697 if (i < MAX_PCIE40_CH - 1) {
698 linksize = data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + (i + 1) ]
699 - data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + i ];
701 linksize = event_length - (data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + (MAX_PCIE40_CH - 1) ] +
702 Belle2::RawTrailer_latest::RAWTRAILER_NWORDS);
706 pthread_mutex_lock(&(mtx_sender_log));
707 printf(
"[FATAL] event size(= %d ) for ch %d is negative. Exiting...\n",
709 printEventData(data, (event_length & 0xfffff));
711 pthread_mutex_unlock(&(mtx_sender_log));
713 }
else if (linksize == 0) {
715 if (i < MAX_PCIE40_CH - 1) {
716 temp_data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + i + 1 ] =
717 temp_data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + i ];
721 red_linksize = linksize
722 - (Belle2::PreRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER
723 + Belle2::PreRawCOPPERFormat_latest::SIZE_B2LFEE_HEADER
724 - Belle2::PostRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER
725 - Belle2::PostRawCOPPERFormat_latest::SIZE_B2LFEE_HEADER)
726 - (Belle2::PreRawCOPPERFormat_latest::SIZE_B2LHSLB_TRAILER
727 + Belle2::PreRawCOPPERFormat_latest::SIZE_B2LFEE_TRAILER
728 - Belle2::PostRawCOPPERFormat_latest::SIZE_B2LHSLB_TRAILER
729 - Belle2::PostRawCOPPERFormat_latest::SIZE_B2LFEE_TRAILER);
731 if (i < MAX_PCIE40_CH - 1) {
732 temp_data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + i + 1 ] =
733 temp_data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + i ] + red_linksize;
738 dst_cur_pos = temp_data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + i ];
739 cur_pos = data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + i ];
742 temp_data[ dst_cur_pos + Belle2::PostRawCOPPERFormat_latest::POS_B2LHSLB_MAGIC]
743 = data[ cur_pos + Belle2::PreRawCOPPERFormat_latest::POS_MAGIC_B2LHSLB ];
744 dst_cur_pos = dst_cur_pos + Belle2::PostRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER;
745 cur_pos = cur_pos + Belle2::PreRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER;
749 temp_data[ dst_cur_pos + Belle2::PostRawCOPPERFormat_latest::POS_B2L_CTIME ]
750 = data[ cur_pos + Belle2::PreRawCOPPERFormat_latest::POS_B2L_CTIME ];
751 dst_cur_pos = dst_cur_pos + Belle2::PostRawCOPPERFormat_latest::SIZE_B2LFEE_HEADER;
752 cur_pos = cur_pos + Belle2::PreRawCOPPERFormat_latest::SIZE_B2LFEE_HEADER;
755 int numwords_det_buffer = red_linksize
756 - Belle2::PostRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER
757 - Belle2::PostRawCOPPERFormat_latest::SIZE_B2LFEE_HEADER
758 - Belle2::PostRawCOPPERFormat_latest::SIZE_B2LHSLB_TRAILER
759 - Belle2::PostRawCOPPERFormat_latest::SIZE_B2LFEE_TRAILER;
761 memcpy(temp_data + dst_cur_pos, data + cur_pos,
762 numwords_det_buffer *
sizeof(
unsigned int));
763 dst_cur_pos = dst_cur_pos + numwords_det_buffer;
764 cur_pos = cur_pos + numwords_det_buffer;
767 temp_data[ dst_cur_pos + Belle2::PostRawCOPPERFormat_latest::POS_B2LFEE_ERRCNT_CRC16 ]
768 = data[ cur_pos + Belle2::PreRawCOPPERFormat_latest::POS_CHKSUM_B2LFEE ];
769 dst_cur_pos += Belle2::PostRawCOPPERFormat_latest::SIZE_B2LFEE_TRAILER;
770 cur_pos += Belle2::PreRawCOPPERFormat_latest::SIZE_B2LFEE_TRAILER;
773 temp_data[ dst_cur_pos + Belle2::PostRawCOPPERFormat_latest::POS_B2LHSLB_TRL_MAGIC ]
774 = data[ cur_pos + Belle2::PreRawCOPPERFormat_latest::POS_CHKSUM_B2LHSLB ];
775 dst_cur_pos += Belle2::PostRawCOPPERFormat_latest::SIZE_B2LHSLB_TRAILER;
776 cur_pos += Belle2::PreRawCOPPERFormat_latest::SIZE_B2LHSLB_TRAILER;
781 temp_data[ dst_cur_pos + Belle2::RawTrailer_latest::POS_ERROR_BIT_CH1 ] =
782 data[ cur_pos + Belle2::RawTrailer_latest::POS_ERROR_BIT_CH1 ];
783 temp_data[ dst_cur_pos + Belle2::RawTrailer_latest::POS_ERROR_CH2 ] =
784 data[ cur_pos + Belle2::RawTrailer_latest::POS_ERROR_CH2 ];
785 temp_data[ dst_cur_pos + Belle2::RawTrailer_latest::POS_CHKSUM ] =
786 data[ cur_pos + Belle2::RawTrailer_latest::POS_CHKSUM ];
787 temp_data[ dst_cur_pos + Belle2::RawTrailer_latest::POS_TERM_WORD ] =
788 data[ cur_pos + Belle2::RawTrailer_latest::POS_TERM_WORD ];
790 dst_cur_pos += Belle2::RawTrailer_latest::RAWTRAILER_NWORDS;
791 cur_pos += Belle2::RawTrailer_latest::RAWTRAILER_NWORDS;
793 if (dst_cur_pos > cur_pos) {
794 pthread_mutex_lock(&(mtx_sender_log));
795 printf(
"[FATAL] reduced data-size ( %d words ) in reduceHdrTrl() is larger than the original size ( %d words). Exiting...\n",
796 dst_cur_pos, cur_pos);
798 pthread_mutex_unlock(&(mtx_sender_log));
802 event_nwords = dst_cur_pos;
803 temp_data[ Belle2::RawHeader_latest::POS_NWORDS ] = event_nwords;
805 memset(data, 0, event_nwords *
sizeof(
unsigned int));
806 memcpy(data, temp_data, dst_cur_pos *
sizeof(
unsigned int));
815 int checkEventData(
int sender_id,
unsigned int* data,
unsigned int event_nwords,
unsigned int& exprun,
816 unsigned int& evtnum,
unsigned int node_id, std::vector< int > valid_ch)
818 int expected_number_of_links = valid_ch.size() ;
819 int reduced_flag = 1;
822 unsigned int new_exprun = data[ Belle2::RawHeader_latest::POS_EXP_RUN_NO ] ;
823 unsigned int new_evtnum = data[ Belle2::RawHeader_latest::POS_EVE_NO ] ;
829 unsigned int event_length = data[ EVENT_LEN_POS ];
830 if (event_length > 0x100000) {
831 pthread_mutex_lock(&(mtx_sender_log));
832 printf(
"[FATAL] thread %d : %s : ERROR_EVENT : Too large event size. : 0x%.8x : %d words. : exp %d run %d sub %d : Exiting...\n",
833 sender_id, hostnamebuf,
834 data[ EVENT_LEN_POS ], data[ EVENT_LEN_POS ],
835 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
836 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
837 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK)
840 printEventData(data, (event_length & 0xfffff), sender_id);
841 pthread_mutex_unlock(&(mtx_sender_log));
843 }
else if (event_length == 0) {
844 pthread_mutex_lock(&(mtx_sender_log));
845 printf(
"[FATAL] thread %d : %s : ERROR_EVENT : Specified event size is zero. : 0x%.8x : %u words. : exp %d run %d sub %d : Exiting...\n",
846 sender_id, hostnamebuf,
847 data[ EVENT_LEN_POS ], event_length,
848 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
849 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
850 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK)
852 printEventData(data, 24, sender_id);
853 pthread_mutex_unlock(&(mtx_sender_log));
860 if ((data[ MAGIC_7F7F_POS ] & 0xFFFF0000) != 0x7F7F0000) {
861 char err_buf[500] = {0};
862 pthread_mutex_lock(&(mtx_sender_log));
863 n_messages[ 7 ] = n_messages[ 7 ] + 1 ;
864 if (n_messages[ 7 ] < max_number_of_messages) {
866 "[FATAL] thread %d : %s : ERROR_EVENT : Invalid Magic word in ReadOut Board header( 0x%.8x ) : It must be 0x7f7f???? : exp %d run %d sub %d",
867 sender_id, hostnamebuf, data[ MAGIC_7F7F_POS ],
868 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
869 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
870 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK)
872 printf(
"%s\n", err_buf); fflush(stdout);
873 printEventData(data, event_length, sender_id);
875 err_bad_7f7f[sender_id]++;
876 pthread_mutex_unlock(&(mtx_sender_log));
877 #ifndef NO_ERROR_STOP
885 data[ NODEID_POS ] = node_id;
890 int ffaa_pos = 0, ff55_pos_from_end = 0;
891 if (data[ MAGIC_7F7F_POS ] & 0x00008000) {
894 ffaa_pos = Belle2::PreRawCOPPERFormat_latest::POS_MAGIC_B2LHSLB;
895 ff55_pos_from_end = - Belle2::PreRawCOPPERFormat_latest::SIZE_B2LHSLB_TRAILER +
896 Belle2::PreRawCOPPERFormat_latest::POS_CHKSUM_B2LHSLB;
918 ffaa_pos = Belle2::PostRawCOPPERFormat_latest::POS_B2LHSLB_MAGIC;
919 ff55_pos_from_end = - Belle2::PostRawCOPPERFormat_latest::SIZE_B2LHSLB_TRAILER +
920 Belle2::PostRawCOPPERFormat_latest::POS_B2LHSLB_TRL_MAGIC;
921 if (data[ ERR_POS ] != 0) {
922 pthread_mutex_lock(&(mtx_sender_log));
923 printf(
"[FATAL] thread %d : %s : Inconsistency between header(no error found by FPGA) %.8x and errorbit %.8x (error-bit is non-zero) : exp %d run %d sub %d\n",
924 sender_id, hostnamebuf, data[ MAGIC_7F7F_POS ], data[ ERR_POS ],
925 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
926 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
927 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK)
929 printEventData(data, event_length, sender_id);
930 pthread_mutex_unlock(&(mtx_sender_log));
931 #ifndef NO_ERROR_STOP
940 if (evtnum + NUM_SENDER_THREADS != data[EVENUM_POS]) {
941 if (exprun == data[RUNNO_POS]
943 printEventNumberError(data, evtnum, exprun, NUM_SENDER_THREADS, sender_id);
944 #ifndef NO_ERROR_STOP
955 exprun = data[RUNNO_POS];
957 if (exprun != data[RUNNO_POS]) {
958 if (new_evtnum < 0 || new_evtnum >= NUM_SENDER_THREADS) {
959 pthread_mutex_lock(&(mtx_sender_log));
960 n_messages[ 9 ] = n_messages[ 9 ] + 1 ;
961 if (n_messages[ 9 ] < max_number_of_messages) {
962 printf(
"[FATAL] thread %d : %s ch=%d : ERROR_EVENT : Bad exprun(now %.8x prev. %.8x) : exp %d run %d sub %d : Exiting...\n",
963 sender_id, hostnamebuf, get1stChannel(data),
964 exprun, data[RUNNO_POS],
965 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
966 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
967 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK)
969 printEventData(data, event_length, sender_id);
971 err_bad_runnum[sender_id]++;
972 pthread_mutex_unlock(&(mtx_sender_log));
973 #ifndef NO_ERROR_STOP
977 if (sender_id == 0) {
978 printf(
"[DEBUG] thread %d : Run number was changed. cur exprun %.8x prev. exprun %.8x cur eve %.8x : exp %d run %d sub %d\n",
979 sender_id, data[RUNNO_POS], exprun, new_evtnum,
980 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
981 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
982 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK)
988 exprun = data[RUNNO_POS];
992 total_crc_good[sender_id] = 0;
993 total_crc_errors[sender_id] = 0;
994 err_flag_cnt[sender_id] = 0;
995 cur_evtnum[sender_id] = 0;
996 err_not_reduced[sender_id] = 0;
997 err_bad_7f7f[sender_id] = 0;
998 err_bad_runnum[sender_id] = 0;
999 err_bad_linknum[sender_id] = 0;
1000 err_bad_evenum[sender_id] = 0;
1001 err_bad_ffaa[sender_id] = 0;
1002 err_bad_ff55[sender_id] = 0;
1003 err_bad_linksize[sender_id] = 0;
1004 err_link_eve_jump[sender_id] = 0;
1012 unsigned int ctime = data[ Belle2::RawHeader_latest::POS_TTCTIME_TRGTYPE ] ;
1013 unsigned int utime = data[ Belle2::RawHeader_latest::POS_TTUTIME ] ;
1015 unsigned int crc_init = 0xFFFF ;
1016 unsigned int f_crc[ 4 ] = { ctime, new_evtnum, utime, new_exprun } ;
1017 unsigned int first_crc = 0;
1020 unsigned int cur_pos = 0 ;
1021 int non_crc_counts = 0;
1024 unsigned int first_b2lctime = 0;
1025 int first_b2lctime_flag = 0;
1027 if (reduced_flag == 1) {
1028 first_crc = get_crc(f_crc, 4, crc_init) ;
1029 non_crc_counts = NON_CRC_COUNTS_REDUCED;
1031 err_flag_cnt[sender_id]++;
1034 first_crc = crc_init;
1035 non_crc_counts = NON_CRC_COUNTS_NOTREDUCED;
1038 int first_eve_flag = 0;
1044 for (
int i = 0; i < MAX_PCIE40_CH; i++) {
1045 if (i == 0) first_b2lctime_flag = 0;
1048 if (i < MAX_PCIE40_CH - 1) {
1049 linksize = data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + (i + 1) ]
1050 - data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + i ];
1052 linksize = event_length - (data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + (MAX_PCIE40_CH - 1) ] +
1053 Belle2::RawTrailer_latest::RAWTRAILER_NWORDS);
1055 if (linksize <= 0)
continue;
1056 cur_pos = data[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + i ] + OFFSET_HDR;
1061 if (valid_ch[link_cnt] != i) {
1062 pthread_mutex_lock(&(mtx_sender_log));
1063 n_messages[ 11 ] = n_messages[ 11 ] + 1 ;
1064 if (n_messages[ 11 ] < max_number_of_messages) {
1065 printf(
"[FATAL] thread %d : %s ch=%d or %d : ERROR_EVENT : HSLB or PCIe40 channel found in data is ch %d but the next channel must be ch %d according to masking register info. of PCIe40. Please check the status of channel masking. : exp %d run %d sub %d\n",
1067 hostnamebuf, i, valid_ch[link_cnt],
1068 i, valid_ch[link_cnt],
1069 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1070 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1071 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK)
1073 printEventData(data, event_length, sender_id);
1075 err_bad_linknum[sender_id]++;
1076 pthread_mutex_unlock(&(mtx_sender_log));
1077 #ifndef NO_ERROR_STOP
1085 if ((data[ cur_pos + ffaa_pos ] & 0xFFFF0000) != 0xFFAA0000) {
1086 pthread_mutex_lock(&(mtx_sender_log));
1087 n_messages[ 12 ] = n_messages[ 12 ] + 1 ;
1088 if (n_messages[ 12 ] < max_number_of_messages) {
1091 "[FATAL] thread %d : %s ch=%d : ERROR_EVENT : HSLB or PCIe40 header magic word(0xffaa) is invalid. header %.8x : exp %d run %d sub %d : %s %s %d\n",
1094 data[ cur_pos + ffaa_pos ],
1095 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1096 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1097 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
1098 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1099 printf(
"%s\n", err_buf); fflush(stdout);
1100 printLine(data, cur_pos + ffaa_pos);
1101 printEventData(data, event_length, sender_id);
1103 err_bad_ffaa[sender_id]++;
1104 pthread_mutex_unlock(&(mtx_sender_log));
1105 #ifndef NO_ERROR_STOP
1113 if (new_evtnum % 1000000 == 1000) {
1114 if (reduced_flag == 1) {
1118 t_st = localtime(&timer);
1119 if (first_b2lctime_flag == 0) {
1120 first_b2lctime = data[ cur_pos +
1121 Belle2::PostRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER +
1122 Belle2::PostRawCOPPERFormat_latest::POS_B2L_CTIME ];
1123 first_b2lctime_flag = 1;
1125 pthread_mutex_lock(&(mtx_sender_log));
1126 printf(
"[DEBUG] thread %d : eve %u ch %3d B2Lctime 0x%.8x diff %.2lf [us] : exp %d run %d sub %d : %s",
1127 sender_id, new_evtnum, i,
1129 Belle2::PostRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER +
1130 Belle2::PostRawCOPPERFormat_latest::POS_B2L_CTIME ],
1131 ((
int)(data[ cur_pos +
1132 Belle2::PostRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER +
1133 Belle2::PostRawCOPPERFormat_latest::POS_B2L_CTIME ]
1134 - first_b2lctime)) / 127.22,
1135 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1136 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1137 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
1139 pthread_mutex_unlock(&(mtx_sender_log));
1144 if (first_eve_flag == 0) {
1151 unsigned int eve_link_8bits = data[ cur_pos + ffaa_pos ] & 0x000000ff;
1152 if ((new_evtnum & 0x000000FF) != eve_link_8bits) {
1153 pthread_mutex_lock(&(mtx_sender_log));
1154 err_link_eve_jump[sender_id]++;
1155 if (err_link_eve_jump[sender_id] < max_number_of_messages) {
1156 char err_buf[500] = {0};
1158 "[FATAL] thread %d : %s ch=%d : ERROR_EVENT : Invalid event_number (= lower 8bits in ffaa header -> 0x%.2x). Exiting...: eve 0x%.8x ffaa header 0x%.8x : exp %d run %d sub %d : %s %s %d",
1161 data[ cur_pos + ffaa_pos ] & 0xff, new_evtnum, data[ cur_pos + ffaa_pos ],
1162 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1163 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1164 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
1165 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1166 printf(
"%s\n", err_buf); fflush(stdout);
1167 printEventData(data, event_length, sender_id);
1169 pthread_mutex_unlock(&(mtx_sender_log));
1170 #ifndef NO_ERROR_STOP
1178 unsigned int ch_ffaa = (data[ cur_pos + ffaa_pos ] >> 8) & 0x000000ff;
1179 if ((
unsigned int)i != ch_ffaa) {
1180 pthread_mutex_lock(&(mtx_sender_log));
1181 printf(
"[FATAL] thread %d : %s ch=%d : ERROR_EVENT : HSLB or PCIe40 channel-number is differnt. It should be ch %d in the channel table in the ROB header buf ffaa header info says ch is %d (%.8x). : exp %d run %d sub %d : %s %s %d\n",
1182 sender_id, hostnamebuf, i,
1183 i, (data[ cur_pos + ffaa_pos ] >> 8) & 0xff,
1184 data[ cur_pos + ffaa_pos ],
1185 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1186 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1187 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
1188 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1189 printEventData(data, event_length, sender_id);
1190 pthread_mutex_unlock(&(mtx_sender_log));
1191 #ifndef NO_ERROR_STOP
1197 #ifdef SPLIT_ECL_ECLTRG
1201 unsigned int ecl_ecltrg_1stword = 0;
1202 if (reduced_flag == 0) {
1203 ecl_ecltrg_1stword = data[ cur_pos + ffaa_pos +
1204 Belle2::PreRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER +
1205 Belle2::PreRawCOPPERFormat_latest::SIZE_B2LFEE_HEADER ];
1207 ecl_ecltrg_1stword = data[ cur_pos + ffaa_pos +
1208 Belle2::PostRawCOPPERFormat_latest::SIZE_B2LHSLB_HEADER +
1209 Belle2::PostRawCOPPERFormat_latest::SIZE_B2LFEE_HEADER ];
1212 if (((ecl_ecltrg_1stword & 0xffff0000) >> 16) == 0) {
1214 for (
int j = 0; j < splitted_ch.size(); j++) {
1215 if (splitted_ch[j] == i) {
1216 pthread_mutex_lock(&(mtx_sender_log));
1217 printf(
"[FATAL] thread %d : %s ch=%d : ECL data(1st word = %.8x , eve = %.8x ) are detected in ECLTRG channel. Maybe, fiber connection mismatch. Exiting... : exp %d run %d sub %d : %s %s %d\n",
1222 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1223 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1224 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
1225 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1226 printEventData(data, event_length, sender_id);
1227 pthread_mutex_unlock(&(mtx_sender_log));
1233 int ecltrg_flag = 0;
1234 for (
int j = 0; j < splitted_ch.size(); j++) {
1235 if (splitted_ch[j] == i) {
1241 if (ecltrg_flag == 0) {
1242 pthread_mutex_lock(&(mtx_sender_log));
1243 printf(
"[FATAL] thread %d : %s ch=%d : ECLTRG data(1st word = %.8x , eve = %.8x ) are detected in ECL channel. Maybe, fiber connection mismatch. Exiting... : exp %d run %d sub %d : %s %s %d\n",
1248 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1249 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1250 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
1251 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1252 printEventData(data, event_length, sender_id);
1253 pthread_mutex_unlock(&(mtx_sender_log));
1263 if (cur_pos + linksize > event_nwords - Belle2::RawTrailer_latest::RAWTRAILER_NWORDS) {
1264 pthread_mutex_lock(&(mtx_sender_log));
1265 n_messages[ 13 ] = n_messages[ 13 ] + 1 ;
1266 if (n_messages[ 13 ] < max_number_of_messages) {
1267 printf(
"[FATAL] thread %d : %s ch=%d : ERROR_EVENT : The end position ( %d words ) of this channel data exceeds event size( %d words ). Exiting... : exp %d run %d sub %d : %s %s %d\n",
1270 (cur_pos + linksize), event_nwords,
1271 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1272 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1273 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
1274 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1277 printEventData(data, event_length, sender_id);
1278 err_bad_linksize[sender_id]++;
1279 pthread_mutex_unlock(&(mtx_sender_log));
1280 #ifndef NO_ERROR_STOP
1288 if (((data[ cur_pos + linksize + ff55_pos_from_end ]) & 0xFFFF0000) != 0xFF550000) {
1289 pthread_mutex_lock(&(mtx_sender_log));
1290 n_messages[ 14 ] = n_messages[ 14 ] + 1 ;
1291 if (n_messages[ 14 ] < max_number_of_messages) {
1294 "[FATAL] thread %d : %s ch=%d : ERROR_EVENT : HSLB or PCIe40 trailer magic word(0xff55) is invalid. foooter %.8x (pos.=0x%.x) : exp %d run %d sub %d : %s %s %d",
1297 data[ cur_pos + linksize + ff55_pos_from_end ], cur_pos + linksize + ff55_pos_from_end,
1298 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1299 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1300 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
1301 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1302 printf(
"%s\n", err_buf); fflush(stdout);
1303 printEventData(data, event_length + 16, sender_id);
1305 err_bad_ff55[sender_id]++;
1306 pthread_mutex_unlock(&(mtx_sender_log));
1307 #ifndef NO_ERROR_STOP
1317 unsigned int crc_data = data[ cur_pos + linksize - 2 ] & 0xFFFF ;
1318 int size = linksize - non_crc_counts;
1319 unsigned int value = crc_data;
1320 unsigned int* data_for_crc = data + cur_pos + CRC_START_POS;
1322 if (get_crc(data_for_crc, size, first_crc) != value) {
1323 pthread_mutex_lock(&(mtx_sender_log));
1326 printf(
"[FATAL] thread %d : %s ch=%d : ERROR_EVENT : PRE CRC16 error or POST B2link event CRC16 error. data(%x) calc(%x) : eve %d exp %d run %d sub %d : %s %s %d\n",
1329 value, get_crc(data_for_crc, size, first_crc),
1331 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1332 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1333 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
1334 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1335 printEventData(data, event_length, sender_id);
1338 crc_err_ch[sender_id][i]++;
1339 total_crc_errors[sender_id]++;
1340 pthread_mutex_unlock(&(mtx_sender_log));
1341 #ifndef NO_ERROR_STOP
1345 total_crc_good[sender_id]++ ;
1353 if (new_evtnum % 1000000 == 0) {
1355 pthread_mutex_lock(&(mtx_sender_log));
1356 printf(
"[DEBUG] thread %d : CRC Good calc %.4X data %.4X eve %u ch %d crcOK %u crcNG %d errflag %u : exp %d run %d sub %d\n",
1358 get_crc(data_for_crc, size, first_crc),
1359 value, new_evtnum, i, total_crc_good[sender_id], total_crc_errors[sender_id], err_flag_cnt[sender_id],
1360 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1361 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1362 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK)
1364 int temp_err_cnt = 0;
1366 for (
int j = 0; j < MAX_PCIE40_CH; j++) {
1367 if (crc_err_ch[sender_id][j] > 0) {
1368 if (temp_err_cnt == 0) {
1369 printf(
"[DEBUG] thread %d : crc_err_cnt : ", sender_id);
1372 printf(
"ch %d %u : ", j, crc_err_ch[sender_id][j]);
1375 if (temp_err_cnt != 0) {
1379 pthread_mutex_unlock(&(mtx_sender_log));
1386 cur_pos = cur_pos + linksize ;
1387 if (((data[ cur_pos ] & 0xFFFF0000) == 0x7FFF0000)) break ;
1394 if (cur_pos != event_nwords - Belle2::RawTrailer_latest::RAWTRAILER_NWORDS) {
1395 pthread_mutex_lock(&(mtx_sender_log));
1396 printf(
"[FATAL] thread %d : %s : ERROR_EVENT : The end position of channel data( %d-th word ) does not coincide with the start of RawTrailer( %d-th word ). Exiting... : exp %d run %d sub %d : %s %s %d\n",
1399 cur_pos, event_nwords - Belle2::RawTrailer_latest::RAWTRAILER_NWORDS,
1400 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1401 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1402 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
1403 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1404 printEventData(data, event_length, sender_id);
1405 pthread_mutex_unlock(&(mtx_sender_log));
1406 #ifndef NO_ERROR_STOP
1415 if (link_cnt != expected_number_of_links) {
1416 pthread_mutex_lock(&(mtx_sender_log));
1417 printf(
"[FATAL] thread %d : %s : ERROR_EVENT : # of links(%d) in data is not the same as exptected(=%d). : Exiting... : exp %d run %d sub %d : %s %s %d\n",
1420 link_cnt, expected_number_of_links,
1421 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1422 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1423 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
1424 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1426 printEventData(data, event_length, sender_id);
1427 pthread_mutex_unlock(&(mtx_sender_log));
1428 #ifndef NO_ERROR_STOP
1437 if (new_evtnum == 0) {
1438 pthread_mutex_lock(&(mtx_sender_log));
1439 printf(
"[DEBUG] thread %d : Printing the 1st event.\n", sender_id);
1440 printEventData(data, event_length, sender_id);
1441 pthread_mutex_unlock(&(mtx_sender_log));
1449 int ret = DATACHECK_OK;
1450 if (reduced_flag == 0) {
1451 checkUtimeCtimeTRGType(data, sender_id);
1452 pthread_mutex_lock(&(mtx_sender_log));
1453 if (err_not_reduced[sender_id] < max_number_of_messages) {
1454 printf(
"[WARNING] thread %d : %s ch=%d : ERROR_EVENT : Error-flag was set by the data-check module in PCIe40 FPGA. : eve %d prev thr eve %d : exp %d run %d sub %d : %s %s %d\n",
1456 hostnamebuf, -1, new_evtnum, evtnum,
1457 (new_exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
1458 (new_exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
1459 (new_exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
1460 __FILE__, __PRETTY_FUNCTION__, __LINE__);
1461 printEventData(data, event_length, sender_id);
1463 err_not_reduced[sender_id]++;
1464 pthread_mutex_unlock(&(mtx_sender_log));
1466 ret = DATACHECK_OK_BUT_ERRFLAG_IN_HDR;
1469 evtnum = data[EVENUM_POS];
1473 void checkEventGenerator(
unsigned int* data,
int i,
unsigned int size)
1476 pthread_mutex_lock(&(mtx_sender_log));
1477 printf(
"No data\n") ;
1478 pthread_mutex_unlock(&(mtx_sender_log));
1482 if (i != getEventNumber(data)) {
1483 pthread_mutex_lock(&(mtx_sender_log));
1484 printf(
"Event number mismatch %d %d\n",
1485 getEventNumber(data), i) ;
1486 pthread_mutex_unlock(&(mtx_sender_log));
1490 if ((data[7] != 0) || (data[6] != 0)) {
1491 pthread_mutex_lock(&(mtx_sender_log));
1492 printf(
"Bad header 3 %.8x %.8x\n", data[7], data[6]) ;
1494 pthread_mutex_unlock(&(mtx_sender_log));
1495 }
else if ((data[ 0 ] & 0xFFFF) != size) {
1496 pthread_mutex_lock(&(mtx_sender_log));
1497 printf(
"Bad size %d %d\n", data[0] & 0xFFFF, size) ;
1498 printLine(data, EVENT_LEN_POS);
1499 pthread_mutex_unlock(&(mtx_sender_log));
1500 }
else if (((data[ 2 ] & 0xFFFF0000) >> 16) != (size * 32)) {
1501 pthread_mutex_lock(&(mtx_sender_log));
1502 printf(
"Bad word size %d %d\n", (data[ 2 ] & 0xFFFF0000) >> 16, size * 32) ;
1504 pthread_mutex_unlock(&(mtx_sender_log));
1505 }
else if (((data[ 0 ] & 0xFFFF0000) != 0xEEEE0000) ||
1506 (data[ 1 ] != 0xAAAAEEEE) ||
1507 ((data[ 2 ] & 0xFFFF) != 0xAAAA)) {
1508 pthread_mutex_lock(&(mtx_sender_log));
1509 printf(
"Bad header 4\n") ;
1511 printEventData(data, size);
1512 pthread_mutex_unlock(&(mtx_sender_log));
1515 if (data[ 8 * (size - 1) ] != size) {
1516 pthread_mutex_lock(&(mtx_sender_log));
1517 printf(
"Bad size in trailer %.8x %.8x\n", data[8 * (size - 1)], size) ;
1518 printLine(data, 8 * (size - 1));
1519 pthread_mutex_unlock(&(mtx_sender_log));
1520 }
else if ((data[ 8 * (size - 1) + 1 ] != 0) || (data[ 8 * (size - 1) + 2 ] != 0) ||
1521 (data[ 8 * (size - 1) + 3 ] != 0) || (data[ 8 * (size - 1) + 4 ] != 0) ||
1522 (data[ 8 * (size - 1) + 5 ] != 0) || (data[ 8 * (size - 1) + 6 ] != 0) ||
1523 (data[ 8 * (size - 1) + 7 ] != 0)) {
1524 pthread_mutex_lock(&(mtx_sender_log));
1525 printf(
"Bad trailer\n") ;
1526 printTrailer(&data[ 8 * (size - 1) ]) ;
1527 pthread_mutex_unlock(&(mtx_sender_log));
1530 for (
unsigned int j = 1 ; j < (size - 1) ; ++j) {
1531 if (data[ 8 * j ] != j) {
1532 pthread_mutex_lock(&(mtx_sender_log));
1533 printf(
"Bad data number %d %d\n", data[8 * j], j) ;
1534 pthread_mutex_unlock(&(mtx_sender_log));
1535 }
else if (data[8 * j + 1] != 0) {
1536 pthread_mutex_lock(&(mtx_sender_log));
1537 printf(
"Bad data\n") ;
1538 printData(&data[8 * j]) ;
1539 pthread_mutex_unlock(&(mtx_sender_log));
1540 }
else if ((data[8 * j + 2] != 0xFFFFFFFF) || (data[8 * j + 3] != 0xEEEEEEEE) ||
1541 (data[8 * j + 4] != 0xDDDDDDDD) || (data[8 * j + 5] != 0xCCCCCCCC) ||
1542 (data[8 * j + 6] != 0xBBBBBBBB) || (data[8 * j + 7] != 0xAAAAAAAA)) {
1543 pthread_mutex_lock(&(mtx_sender_log));
1544 printf(
"Bad data\n") ;
1545 printData(&data[8 * j]) ;
1546 pthread_mutex_unlock(&(mtx_sender_log));
1552 unsigned short CalcCRC16LittleEndian(
unsigned short crc16,
const int buf[],
int nwords)
1558 pthread_mutex_lock(&(mtx_sender_log));
1559 sprintf(err_buf,
"nwords value(%d) is invalid. Cannot calculate CRC16. Exiting...\n %s %s %d\n",
1560 nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__);
1561 printf(
"%s", err_buf); fflush(stdout);
1562 pthread_mutex_unlock(&(mtx_sender_log));
1563 string err_str = err_buf;
1567 const unsigned short CRC16Table0x1021[ 256 ] = {
1568 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
1569 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
1570 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
1571 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
1572 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
1573 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
1574 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
1575 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
1577 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
1578 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
1579 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
1580 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
1581 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
1582 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
1583 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
1584 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
1586 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
1587 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
1588 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
1589 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
1590 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
1591 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
1592 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
1593 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
1595 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
1596 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
1597 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
1598 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
1599 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
1600 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
1601 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
1602 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
1605 int cnt = 0, nints = 0;
1607 while (nwords != 0) {
1609 unsigned char temp_buf = *((
unsigned char*)(buf + nints) + (-(cnt % 4) + 3));
1610 crc16 = CRC16Table0x1021[(crc16 >> (16 - CHAR_BIT)) ^ temp_buf ] ^ (crc16 << CHAR_BIT);
1612 if ((cnt % 4) == 3) {
1626 int fillDataContents(
int* buf,
int nwords_per_fee,
unsigned int node_id,
int ncpr,
int nhslb,
int run)
1628 int nwords = NW_SEND_HEADER + NW_SEND_TRAILER +
1629 ncpr * (NW_RAW_HEADER +
1630 (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb
1635 buf[ offset + 0 ] = nwords;
1636 buf[ offset + 1 ] = 6;
1637 buf[ offset + 2 ] = (1 << 16) | ncpr;
1638 unsigned int exp_run = run << 8;
1639 buf[ offset + 3 ] = exp_run;
1640 buf[ offset + 5 ] = node_id;
1641 offset += NW_SEND_HEADER;
1643 for (
int k = 0; k < ncpr; k++) {
1647 int cpr_nwords = NW_RAW_HEADER +
1648 (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb
1650 unsigned int ctime = CTIME_VAL;
1651 unsigned int utime = 0x98765432;
1653 buf[ offset + 0 ] = cpr_nwords;
1654 #ifdef DUMMY_REDUCED_DATA
1655 buf[ offset + 1 ] = 0x7f7f020c;
1657 buf[ offset + 1 ] = 0x7f7f820c;
1659 buf[ offset + 2 ] = exp_run;
1660 printf(
"run_no %d\n", exp_run); fflush(stdout);
1661 buf[ offset + 4 ] = ctime;
1662 buf[ offset + 5 ] = utime;
1663 buf[ offset + 6 ] = node_id + k;
1664 buf[ offset + 7 ] = 0x34567890;
1665 offset += NW_RAW_HEADER;
1667 for (
int i = 0; i < nhslb ; i++) {
1668 #ifdef DUMMY_REDUCED_DATA
1669 buf[ offset + 0 ] = nwords_per_fee + 3;
1670 buf[ offset + 1 ] = 0xffaa0000;
1671 buf[ offset + 2 ] = ctime;
1673 buf[ offset + 0 ] = nwords_per_fee + 7;
1674 buf[ offset + 1 ] = 0xffaa0000;
1675 buf[ offset + 3 ] = ctime;
1676 buf[ offset + 4 ] = utime;
1677 buf[ offset + 5 ] = exp_run;
1678 buf[ offset + 6 ] = ctime;
1680 offset += NW_B2L_HEADER;
1682 for (
int j = offset; j < offset + nwords_per_fee; j++) {
1685 offset += nwords_per_fee;
1687 #ifdef DUMMY_REDUCED_DATA
1689 buf[ offset + 1 ] = 0xff550000;
1691 buf[ offset ] = ctime;
1692 buf[ offset + 1 ] = 0;
1693 buf[ offset + 2 ] = 0xff550000;
1696 offset += NW_B2L_TRAILER;
1698 buf[ offset ] = 0x0;
1699 buf[ offset + 1 ] = 0x0;
1700 buf[ offset + 2 ] = 0x0;
1701 buf[ offset + 3 ] = 0x7fff0006;
1702 offset += NW_RAW_TRAILER;
1707 buf[ offset + 1 ] = 0x7fff0000;
1708 offset += NW_SEND_TRAILER;
1712 void split_Ecltrg(
int sender_id,
unsigned int* data, std::vector< int > valid_ch,
1713 unsigned int* data_main,
unsigned int* data_splitted,
1714 int& event_nwords_main,
int& event_nwords_splitted,
1715 unsigned int splitted_node_id, std::vector< int > splitted_ch)
1717 unsigned int event_length = data[ Belle2::RawHeader_latest::POS_NWORDS ];
1725 if (event_length > 0x100000) {
1726 pthread_mutex_lock(&(mtx_sender_log));
1727 printf(
"[FATAL] Too large event size. : sdrid %d : 0x%.8x : %d words. Exiting...\n", sender_id, data[ EVENT_LEN_POS ],
1728 data[ EVENT_LEN_POS ]);
1729 printEventData(data, (event_length & 0xfffff));
1730 pthread_mutex_unlock(&(mtx_sender_log));
1732 }
else if (event_length == 0) {
1733 pthread_mutex_lock(&(mtx_sender_log));
1734 printf(
"[FATAL] Specified event size is zero. : 0x%.8x : %u words. Exiting...\n",
1735 data[ EVENT_LEN_POS ], event_length);
1736 printEventData(data, 24);
1737 pthread_mutex_unlock(&(mtx_sender_log));
1742 if ((data[ MAGIC_7F7F_POS ] & 0xFFFF0000) != 0x7F7F0000) {
1743 pthread_mutex_lock(&(mtx_sender_log));
1744 n_messages[ 7 ] = n_messages[ 7 ] + 1 ;
1745 if (n_messages[ 7 ] < max_number_of_messages) {
1746 printf(
"Bad code 7F7F ( 0x%.8x )\n", data[ MAGIC_7F7F_POS ]) ;
1748 printEventData(data, event_length);
1750 err_bad_7f7f[sender_id]++;
1751 pthread_mutex_unlock(&(mtx_sender_log));
1752 #ifndef NO_ERROR_STOP
1760 memcpy(data_main, data, Belle2::RawHeader_latest::POS_CH_POS_TABLE *
sizeof(
unsigned int));
1761 memcpy(data_splitted, data, Belle2::RawHeader_latest::POS_CH_POS_TABLE *
sizeof(
unsigned int));
1762 data_splitted[ Belle2::RawHeader_latest::POS_NODE_ID ] = splitted_node_id;
1765 int cur_ch_main = 0;
1766 int prev_ch_main = -1;
1767 int cur_ch_splitted = 0;
1768 int prev_ch_splitted = -1;
1769 int cur_pos_main = Belle2::RawHeader_latest::RAWHEADER_NWORDS;
1770 int cur_pos_splitted = Belle2::RawHeader_latest::RAWHEADER_NWORDS;
1774 int cnt_splitted = 0;
1776 for (
int i = 0; i < MAX_PCIE40_CH; i++) {
1780 linksize = data[ POS_TABLE_POS + (i + 1) ] - data[ POS_TABLE_POS + i ];
1782 linksize = event_length - (data[ POS_TABLE_POS + 47 ] + LEN_ROB_TRAILER);
1784 if (linksize <= 0)
continue;
1785 cur_pos = data[ POS_TABLE_POS + i ] + OFFSET_HDR;
1788 if (valid_ch[link_cnt] != i) {
1789 pthread_mutex_lock(&(mtx_sender_log));
1790 n_messages[ 11 ] = n_messages[ 11 ] + 1 ;
1791 if (n_messages[ 11 ] < max_number_of_messages) {
1792 printf(
"[FATAL] A valid ch in data(=%d) is not equal to regeister value(%d) for masking\n", i, valid_ch[link_cnt]) ;
1793 printEventData(data, event_length);
1795 err_bad_linknum[sender_id]++;
1796 pthread_mutex_unlock(&(mtx_sender_log));
1797 #ifndef NO_ERROR_STOP
1803 int splitted_ch_flag = 0;
1804 for (
int j = 0; j < splitted_ch.size(); j++) {
1805 if (splitted_ch[j] == i) {
1806 splitted_ch_flag = 1;
1812 if (splitted_ch_flag == 0) {
1813 data_main[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + i ] = cur_pos_main;
1814 for (
int j = prev_ch_main + 1; j < i; j++) {
1815 data_main[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + j ] = cur_pos_main;
1817 memcpy(data_main + cur_pos_main, data + cur_pos, linksize *
sizeof(
unsigned int));
1818 cur_pos_main += linksize;
1822 data_splitted[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + i ] = cur_pos_splitted;
1823 for (
int j = prev_ch_splitted + 1; j < i; j++) {
1824 data_splitted[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + j ] = cur_pos_splitted;
1826 memcpy(data_splitted + cur_pos_splitted, data + cur_pos, linksize *
sizeof(
unsigned int));
1827 cur_pos_splitted += linksize;
1828 prev_ch_splitted = i;
1834 if (cnt_main == 0 || cnt_splitted == 0) {
1835 pthread_mutex_lock(&(mtx_sender_log));
1836 printf(
"[FATAL] No channels for ECL(# of used ch = %d) or ECLTRG(# of used ch = %d) data. Exiting...\n",
1837 cnt_main, cnt_splitted);
1838 pthread_mutex_unlock(&(mtx_sender_log));
1843 for (
int i = prev_ch_main + 1; i < MAX_PCIE40_CH; i++) {
1844 data_main[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + i ] = cur_pos_main;
1846 for (
int i = prev_ch_splitted + 1; i < MAX_PCIE40_CH; i++) {
1847 data_splitted[ Belle2::RawHeader_latest::POS_CH_POS_TABLE + i ] = cur_pos_splitted;
1851 unsigned int eve_size_main = cur_pos_main + Belle2::RawTrailer_latest::RAWTRAILER_NWORDS;
1852 unsigned int eve_size_splitted = cur_pos_splitted + Belle2::RawTrailer_latest::RAWTRAILER_NWORDS;
1853 data_main[ Belle2::RawHeader_latest::POS_NWORDS ] = eve_size_main;
1854 data_splitted[ Belle2::RawHeader_latest::POS_NWORDS ] = eve_size_splitted;
1855 event_nwords_main = eve_size_main;
1856 event_nwords_splitted = eve_size_splitted;
1859 memcpy(data_main + cur_pos_main, data + event_length - Belle2::RawTrailer_latest::RAWTRAILER_NWORDS,
1860 Belle2::RawTrailer_latest::RAWTRAILER_NWORDS *
sizeof(
unsigned int));
1861 memcpy(data_splitted + cur_pos_splitted, data + event_length - Belle2::RawTrailer_latest::RAWTRAILER_NWORDS,
1862 Belle2::RawTrailer_latest::RAWTRAILER_NWORDS *
sizeof(
unsigned int));
1865 memcpy(data, data_main, eve_size_main *
sizeof(
unsigned int));
1866 memcpy(data + eve_size_main, data_splitted, eve_size_splitted *
sizeof(
unsigned int));
1885 void* sender(
void* arg)
1891 int sender_id = snd_arg->sender_id;
1892 unsigned int node_id = snd_arg->node_id;
1893 vector<int> valid_ch = snd_arg->valid_ch;
1898 int total_words = 0;
1899 unsigned int* buff =
new unsigned int[MAX_EVENT_WORDS];
1901 #ifdef SPLIT_ECL_ECLTRG
1902 vector<int> valid_main_ch;
1903 vector<int> valid_splitted_ch;
1904 unsigned int* buff_main =
new unsigned int[MAX_EVENT_WORDS];
1905 unsigned int* buff_splitted =
new unsigned int[MAX_EVENT_WORDS];
1907 int split_main_use = 0;
1908 int split_sub_use = 0;
1911 for (
int k = 0; k < valid_ch.size(); k++) {
1912 int splitted_ch_flag = 0;
1913 for (
int l = 0; l < splitted_ch.size(); l++) {
1914 if (splitted_ch[l] == valid_ch[k]) {
1915 splitted_ch_flag = 1;
1919 if (splitted_ch_flag == 0) {
1920 valid_main_ch.push_back(valid_ch[k]);
1923 valid_splitted_ch.push_back(valid_ch[k]);
1928 if (split_main_use == 0 && split_sub_use == 0) {
1929 pthread_mutex_lock(&(mtx_sender_log));
1930 printf(
"[FATAL] thread %d : No channels are used for this PCIe40 board (ECL/ECLTRG) in %s. Please mask this readout PC with runcontrol GUI (or exclude sub-system if this is the only readout PC of the sub-system). Exiting..\n",
1931 sender_id, hostnamebuf);
1933 pthread_mutex_unlock(&(mtx_sender_log));
1942 int port_to = 31000 + sender_id + 1;
1949 struct sockaddr_in sock_listen;
1950 sock_listen.sin_family = AF_INET;
1952 sock_listen.sin_addr.s_addr = htonl(INADDR_ANY);
1954 socklen_t addrlen =
sizeof(sock_listen);
1955 sock_listen.sin_port = htons(port_to);
1956 fd_listen = socket(PF_INET, SOCK_STREAM, 0);
1959 int ret = setsockopt(fd_listen, SOL_SOCKET, SO_REUSEADDR, &flags, (socklen_t)
sizeof(flags));
1961 perror(
"Failed to set REUSEADDR");
1964 if (bind(fd_listen, (
struct sockaddr*)&sock_listen,
sizeof(
struct sockaddr)) < 0) {
1965 pthread_mutex_lock(&(mtx_sender_log));
1966 printf(
"[FATAL] thread %d : %s : Failed to bind(%s). Maybe other programs have already occupied this port(%d). Exiting...\n",
1967 sender_id, hostnamebuf, strerror(errno),
1968 port_to); fflush(stdout);
1969 pthread_mutex_unlock(&(mtx_sender_log));
1975 sprintf(cmdline,
"/usr/sbin/ss -ap | grep %d", port_to);
1976 if ((fp = popen(cmdline,
"r")) == NULL) {
1977 pthread_mutex_lock(&(mtx_sender_log));
1978 printf(
"[WARNING] thread %d : Failed to run %s\n", sender_id,
1980 pthread_mutex_unlock(&(mtx_sender_log));
1983 while (fgets(buf, 256, fp) != NULL) {
1984 pthread_mutex_lock(&(mtx_sender_log));
1985 printf(
"[DEBUG] thread %d : Port %d is used by : %s\n", sender_id,
1986 port_to, buf); fflush(stdout);
1987 pthread_mutex_unlock(&(mtx_sender_log));
1994 setsockopt(fd_listen, IPPROTO_TCP, TCP_NODELAY, &val1, (socklen_t)
sizeof(val1));
1996 if (listen(fd_listen, backlog) < 0) {
1998 pthread_mutex_lock(&(mtx_sender_log));
1999 sprintf(err_buf,
"[FATAL] thread %d : %s : Failed in listen(%s). Exting...",
2000 sender_id, hostnamebuf,
2002 printf(
"%s\n", err_buf); fflush(stdout);
2003 pthread_mutex_unlock(&(mtx_sender_log));
2012 struct sockaddr_in sock_accept;
2013 pthread_mutex_lock(&(mtx_sender_log));
2014 printf(
"[DEBUG] thread %d : Accepting... : port %d\n", sender_id,
2017 pthread_mutex_unlock(&(mtx_sender_log));
2019 if ((fd_accept = accept(fd_listen, (
struct sockaddr*) & (sock_accept), &addrlen)) == 0) {
2021 pthread_mutex_lock(&(mtx_sender_log));
2022 sprintf(err_buf,
"[FATAL] thread %d : %s : Failed to accept(%s). Exiting...",
2023 sender_id, hostnamebuf,
2025 printf(
"%s\n", err_buf); fflush(stdout);
2026 pthread_mutex_unlock(&(mtx_sender_log));
2031 pthread_mutex_lock(&(mtx_sender_log));
2032 printf(
"[INFO] thread %d : Connection(port %d) from eb0 was accepted\n", sender_id, port_to); fflush(stdout);
2033 pthread_mutex_unlock(&(mtx_sender_log));
2036 struct timeval timeout;
2038 timeout.tv_usec = 0;
2039 ret = setsockopt(fd_accept, SOL_SOCKET, SO_SNDTIMEO, &timeout, (socklen_t)
sizeof(timeout));
2041 pthread_mutex_lock(&(mtx_sender_log));
2043 sprintf(err_buf,
"[FATAL] thread %d : %s : Failed to set TIMEOUT. Exiting...", sender_id, hostnamebuf);
2044 printf(
"%s\n", err_buf); fflush(stdout);
2045 pthread_mutex_unlock(&(mtx_sender_log));
2055 double init_time = getTimeSec();
2056 double prev_time = init_time;
2058 unsigned long long int cnt = 0;
2059 unsigned long long int prev_cnt = 0;
2060 unsigned long long int start_cnt = 3000;
2062 unsigned int exprun = 0;
2063 unsigned int evtnum = 0;
2068 unsigned int tot_event_nwords = 0;
2071 int j = 0; j < MAX_EVENT; j++
2081 zmq::message_t zevent;
2082 zmq_reader[sender_id]->recv(&zevent);
2083 memcpy(buff + NW_SEND_HEADER, zevent.data(), zevent.size());
2084 tot_event_nwords = zevent.size() /
sizeof(
unsigned int);
2089 if (buffer_id == 0) {
2091 if (buffer_filled[sender_id][0] == 1)
break;
2095 pthread_mutex_lock(&(mtx1_ch[sender_id]));
2096 memcpy((buff + NW_SEND_HEADER), data_1[sender_id], copy_nwords[sender_id][0] *
sizeof(
unsigned int));
2097 tot_event_nwords = copy_nwords[sender_id][0];
2098 buffer_filled[sender_id][0] = 0;
2099 pthread_mutex_unlock(&(mtx1_ch[sender_id]));
2104 if (buffer_filled[sender_id][1] == 1)
break;
2109 pthread_mutex_lock(&(mtx2_ch[sender_id]));
2110 memcpy((buff + NW_SEND_HEADER), data_2[sender_id], copy_nwords[sender_id][1] *
sizeof(
unsigned int));
2111 tot_event_nwords = copy_nwords[sender_id][1];
2112 buffer_filled[sender_id][1] = 0;
2113 pthread_mutex_unlock(&(mtx2_ch[sender_id]));
2122 pthread_mutex_lock(&(mtx_sender_log));
2123 printf(
"[FATAL] thread %d : %s : buffer in sender is NULL(= %p )\n", sender_id, hostnamebuf, buff); fflush(stdout);
2124 pthread_mutex_unlock(&(mtx_sender_log));
2128 #ifdef SPLIT_ECL_ECLTRG
2129 int event_nwords_main = 0, event_nwords_splitted = 0;
2130 if (split_main_use == 1 && split_sub_use == 1) {
2131 split_Ecltrg(sender_id, buff + NW_SEND_HEADER, valid_ch,
2132 buff_main, buff_splitted, event_nwords_main, event_nwords_splitted, ECLTRG_NODE_ID, splitted_ch);
2133 tot_event_nwords = event_nwords_main + event_nwords_splitted;
2134 }
else if (split_main_use == 1 && split_sub_use == 0) {
2135 event_nwords_main = tot_event_nwords;
2136 }
else if (split_main_use == 0 && split_sub_use == 1) {
2137 event_nwords_splitted = tot_event_nwords;
2139 pthread_mutex_lock(&(mtx_sender_log));
2140 printf(
"[FATAL] thread %d : %s : No channels are used for this PCIe40 board (ECL/ECLTRG). Please mask this readout PC with runcontrol GUI (or exclude sub-system if this is the only readout PC of the sub-system). Exiting..\n",
2141 sender_id, hostnamebuf);
2143 pthread_mutex_unlock(&(mtx_sender_log));
2148 unsigned int prev_exprun = exprun;
2149 unsigned int prev_evtnum = evtnum;
2151 #ifdef SPLIT_ECL_ECLTRG
2152 for (
int k = 0; k < NUM_SUB_EVE ; k++) {
2154 unsigned int* eve_buff = NULL;
2155 unsigned int event_nwords = 0;
2158 #ifdef SPLIT_ECL_ECLTRG
2160 if (split_main_use == 0)
continue;
2161 event_nwords = event_nwords_main;
2162 eve_buff = buff + NW_SEND_HEADER;
2163 ret = checkEventData(sender_id, eve_buff, event_nwords_main,
2164 exprun, evtnum, node_id, valid_main_ch);
2165 }
else if (k == 1) {
2166 if (split_sub_use == 0)
continue;
2167 exprun = prev_exprun;
2168 evtnum = prev_evtnum;
2169 event_nwords = event_nwords_splitted;
2170 eve_buff = buff + NW_SEND_HEADER + event_nwords_main;
2171 ret = checkEventData(sender_id, eve_buff, event_nwords_splitted,
2172 exprun, evtnum, ECLTRG_NODE_ID, valid_splitted_ch);
2174 pthread_mutex_lock(&(mtx_sender_log));
2175 printf(
"[FATAL] thread %d : # of sub-events must be 1 or 2(for ECL,ECLTRG). k = %d Exiting... : exp %d run %d sub %d : %s %s %d\n",
2177 (exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
2178 (exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
2179 (exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
2180 __FILE__, __PRETTY_FUNCTION__, __LINE__);
2183 pthread_mutex_unlock(&(mtx_sender_log));
2186 event_nwords = tot_event_nwords;
2187 eve_buff = buff + NW_SEND_HEADER;
2188 ret = checkEventData(sender_id, eve_buff, event_nwords, exprun, evtnum, node_id, valid_ch);
2191 if (ret != DATACHECK_OK) {
2192 if (ret == DATACHECK_OK_BUT_ERRFLAG_IN_HDR) {
2194 unsigned int reduced_event_nwords = 0;
2195 pthread_mutex_lock(&(mtx_sender_log));
2196 printf(
"[WARNING] thread %d : fake-error events are detected. Header and trailer reduction will be made and data are checked again.\n",
2199 pthread_mutex_unlock(&(mtx_sender_log));
2200 reduceHdrTrl(eve_buff, reduced_event_nwords);
2201 tot_event_nwords -= (event_nwords - reduced_event_nwords);
2202 event_nwords = reduced_event_nwords;
2204 exprun = prev_exprun;
2205 evtnum = prev_evtnum;
2208 #ifdef SPLIT_ECL_ECLTRG
2210 if (event_nwords_splitted != 0) {
2211 memcpy(buff_splitted, eve_buff + event_nwords_main, event_nwords_splitted *
sizeof(
unsigned int));
2212 memcpy(eve_buff + reduced_event_nwords, buff_splitted, event_nwords_splitted *
sizeof(
unsigned int));
2214 event_nwords_main = reduced_event_nwords;
2215 ret = checkEventData(sender_id, eve_buff, reduced_event_nwords, exprun, evtnum, node_id, valid_main_ch);
2217 event_nwords_splitted = reduced_event_nwords;
2218 ret = checkEventData(sender_id, eve_buff, reduced_event_nwords, exprun, evtnum, node_id, valid_splitted_ch);
2221 ret = checkEventData(sender_id, eve_buff, reduced_event_nwords, exprun, evtnum, node_id, valid_ch);
2224 if (ret != DATACHECK_OK) {
2225 pthread_mutex_lock(&(mtx_sender_log));
2226 printf(
"[FATAL] thread %d : %s : checkEventData() detected an error after reduceHdrTrl(). Exiting...\n", sender_id, hostnamebuf);
2228 pthread_mutex_unlock(&(mtx_sender_log));
2231 pthread_mutex_lock(&(mtx_sender_log));
2232 printf(
"[WARNING] thread %d : %s : Data-check was passed. This event is treated as a normal event.\n", sender_id, hostnamebuf);
2234 printEventData(eve_buff, reduced_event_nwords);
2236 pthread_mutex_unlock(&(mtx_sender_log));
2239 pthread_mutex_lock(&(mtx_sender_log));
2240 printf(
"[FATAL] thread %d : %s : checkEventData() detected an error. Exiting...\n", sender_id, hostnamebuf);
2242 pthread_mutex_unlock(&(mtx_sender_log));
2247 if (eve_buff[ 1 ] & 0xfffff000 != 0x7f7f0000 ||
2248 eve_buff[ event_nwords - 1 ] != 0x7fff0006) {
2249 pthread_mutex_lock(&(mtx_sender_log));
2250 printf(
"[FATAL] thread %d : %s : ERROR_EVENT : Invalid Magic word in header( pos=0x%x, %.8x ) and/or trailer( pos=0x%x, 0x%.8x ) : eve %d exp %d run %d sub %d : %s %s %d\n",
2251 sender_id, hostnamebuf,
2253 event_nwords - 1, eve_buff[ event_nwords - 1 ],
2255 (exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
2256 (exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
2257 (exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
2258 __FILE__, __PRETTY_FUNCTION__, __LINE__);
2259 printEventData(eve_buff, event_nwords, sender_id);
2261 pthread_mutex_unlock(&(mtx_sender_log));
2264 #ifdef SPLIT_ECL_ECLTRG
2277 buff[ 0 ] = tot_event_nwords + NW_SEND_HEADER + NW_SEND_TRAILER;
2279 #ifdef SPLIT_ECL_ECLTRG
2280 if (split_main_use == 1 && split_sub_use == 1) {
2281 buff[ 2 ] = 0x00010002;
2283 buff[ 2 ] = 0x00010001;
2286 buff[ 2 ] = 0x00010001;
2288 buff[ 3 ] = buff[ NW_SEND_HEADER + 2 ];
2289 buff[ 4 ] = buff[ NW_SEND_HEADER + 3 ];
2290 buff[ 5 ] = buff[ NW_SEND_HEADER + 6 ];
2294 buff[ tot_event_nwords + NW_SEND_HEADER ] = 0x0;
2295 buff[ tot_event_nwords + NW_SEND_HEADER + 1 ] = 0x7fff0007;
2305 if ((buff[ NW_SEND_HEADER + 1 ] & 0xfffff000) != 0x7f7f0000 ||
2306 buff[ NW_SEND_HEADER + tot_event_nwords - 1 ] != 0x7fff0006) {
2307 pthread_mutex_lock(&(mtx_sender_log));
2309 printf(
"[FATAL] thread %d : %s : ERROR_EVENT : Invalid Magic word in the 1st header( pos=0x%x, 0x%.8x ) and/or the last trailer( pos=0x%x, 0x%.8x ) : eve %d exp %d run %d sub %d : %s %s %d\n",
2310 sender_id, hostnamebuf, NW_SEND_HEADER + 1, buff[ NW_SEND_HEADER + 1 ],
2311 NW_SEND_HEADER + tot_event_nwords - 1, buff[ NW_SEND_HEADER + tot_event_nwords - 1 ],
2313 (exprun & Belle2::RawHeader_latest::EXP_MASK) >> Belle2::RawHeader_latest::EXP_SHIFT,
2314 (exprun & Belle2::RawHeader_latest::RUNNO_MASK) >> Belle2::RawHeader_latest::RUNNO_SHIFT,
2315 (exprun & Belle2::RawHeader_latest::SUBRUNNO_MASK),
2316 __FILE__, __PRETTY_FUNCTION__, __LINE__);
2317 printEventData(buff, tot_event_nwords + NW_SEND_HEADER + NW_SEND_TRAILER, sender_id);
2319 pthread_mutex_unlock(&(mtx_sender_log));
2324 if ((ret = write(fd_accept, (
char*)buff + sent_bytes, (tot_event_nwords + NW_SEND_HEADER + NW_SEND_TRAILER)
2325 *
sizeof(
unsigned int) - sent_bytes)) <= 0) {
2326 if (errno == EINTR) {
2328 }
else if (errno == EAGAIN || errno == EWOULDBLOCK) {
2331 perror(
"[DEBuG] write() failed");
2332 pthread_mutex_lock(&(mtx_sender_log));
2333 printf(
"[FATAL] thread %d : %s : write() failed. Return value of write() = %d\n", sender_id, hostnamebuf, ret);
2335 pthread_mutex_unlock(&(mtx_sender_log));
2340 if (sent_bytes == (
int)((tot_event_nwords + NW_SEND_HEADER + NW_SEND_TRAILER)
2341 *
sizeof(
unsigned int))) {
2343 }
else if (sent_bytes > (
int)((tot_event_nwords + NW_SEND_HEADER + NW_SEND_TRAILER)
2344 *
sizeof(
unsigned int))) {
2345 pthread_mutex_lock(&(mtx_sender_log));
2346 printf(
"[FATAL] thread %d : %s : Too many bytes are sent\n", sender_id, hostnamebuf);
2348 pthread_mutex_unlock(&(mtx_sender_log));
2354 if (buffer_id == 0) {
2362 if (cnt == start_cnt) init_time = getTimeSec();
2363 if (cnt % 1000000 == 1) {
2364 if (cnt > start_cnt) {
2365 double cur_time = getTimeSec();
2366 pthread_mutex_lock(&(mtx_sender_log));
2367 printf(
"[INFO] thread %d : evt %lld time %.1lf dataflow %.1lf MB/s rate %.2lf kHz : so far dataflow %.1lf MB/s rate %.2lf kHz size %d\n",
2369 cnt, cur_time - init_time,
2370 NUM_CLIENTS_PER_THREAD * (cnt - prev_cnt)*total_words *
sizeof(
int) / 1000000. / (cur_time - prev_time),
2371 (cnt - prev_cnt) / (cur_time - prev_time) / 1000.,
2372 NUM_CLIENTS_PER_THREAD * (cnt - start_cnt)*total_words *
sizeof(
int) / 1000000. / (cur_time - init_time),
2373 (cnt - start_cnt) / (cur_time - init_time) / 1000., total_words);
2376 pthread_mutex_unlock(&(mtx_sender_log));
2377 prev_time = cur_time;
2392 int main(
int argc,
char** argv)
2398 bool isData = true ;
2399 bool writeInFile = false ;
2401 pthread_mutex_lock(&(mtx_sender_log));
2402 printf(
"[FATAL] Invalid usage of %s : %s <node ID>, node ID = 0x0, if you are not using the Belle II DAQ system.\n",
2404 pthread_mutex_unlock(&(mtx_sender_log));
2410 unsigned int pcie40_node_id = (
unsigned int)strtol(argv[1], &endptr, 0);
2419 host_nodeid[
"rsvd1" ] = 0x01000001;
2420 host_nodeid[
"rsvd2" ] = 0x01000002;
2421 host_nodeid[
"rsvd3" ] = 0x01000003;
2422 host_nodeid[
"rsvd4" ] = 0x01000004;
2423 host_nodeid[
"rsvd5" ] = 0x01000005;
2424 host_nodeid[
"rcdc1" ] = 0x02000001;
2425 host_nodeid[
"rcdc2" ] = 0x02000002;
2426 host_nodeid[
"rcdc3" ] = 0x02000003;
2427 host_nodeid[
"rcdc4" ] = 0x02000004;
2428 host_nodeid[
"rcdc5" ] = 0x02000005;
2429 host_nodeid[
"rcdc6" ] = 0x02000006;
2430 host_nodeid[
"rcdc7" ] = 0x02000007;
2431 host_nodeid[
"rcdc8" ] = 0x02000008;
2432 host_nodeid[
"rtop1" ] = 0x03000001;
2433 host_nodeid[
"rtop2" ] = 0x03000002;
2434 host_nodeid[
"rari1" ] = 0x04000001;
2435 host_nodeid[
"rari2" ] = 0x04000002;
2436 host_nodeid[
"recl1" ] = 0x05000001;
2437 host_nodeid[
"recl2" ] = 0x05000002;
2438 host_nodeid[
"recl3" ] = 0x05000003;
2439 host_nodeid[
"rklm1" ] = 0x07000001;
2440 host_nodeid[
"rtrg1" ] = 0x10000001;
2443 gethostname(hostnamebuf,
sizeof(hostnamebuf));
2444 if (pcie40_node_id != NODE_ID_TEST_BENCH) {
2445 std::map<string, unsigned int>::iterator itr;
2446 itr = host_nodeid.find(hostnamebuf);
2447 if (itr != host_nodeid.end()) {
2448 if (itr->second != pcie40_node_id) {
2449 pthread_mutex_lock(&(mtx_sender_log));
2450 printf(
"[FATAL] Node_id argument ( 0x%.8x ) is invalid. Node_id for %s is 0x%.8x. Exiting...\n",
2451 pcie40_node_id, (itr->first).c_str(), itr->second);
2452 pthread_mutex_unlock(&(mtx_sender_log));
2455 pthread_mutex_lock(&(mtx_sender_log));
2456 printf(
"[DEBUG] (hostname %s, nodeid 0x%.8x ) concides with stored info.( %s 0x%.8x )\n", hostnamebuf, pcie40_node_id,
2457 (itr->first).c_str(), itr->second); fflush(stdout);
2458 pthread_mutex_unlock(&(mtx_sender_log));
2461 pthread_mutex_lock(&(mtx_sender_log));
2462 printf(
"[FATAL] This sever's hostname is not for a PCIe40 ROPC( %s ). Please use 0x%.8x for a test. Exiting...\n", hostnamebuf,
2463 NODE_ID_TEST_BENCH);
2464 pthread_mutex_unlock(&(mtx_sender_log));
2475 zmq::context_t ctx(0);
2476 const pid_t pid = getpid();
2477 for (
int i = 0; i < NUM_SENDER_THREADS; i++) {
2478 zmq_writer[i] =
new zmq::socket_t(ctx, ZMQ_PAIR);
2479 zmq_reader[i] =
new zmq::socket_t(ctx, ZMQ_PAIR);
2481 snprintf(zpath,
sizeof(zpath),
"inproc:///dev/shm/des_ser_PCIe40_main.%d.%d.ipc", pid, i);
2482 zmq_writer[i]->bind(zpath);
2483 zmq_reader[i]->connect(zpath);
2489 for (
int i = 0; i < NUM_SENDER_THREADS; i++) {
2490 data_1[i] =
new unsigned int[MAX_EVENT_WORDS];
2491 data_2[i] =
new unsigned int[MAX_EVENT_WORDS];
2498 double init_time = getTimeSec();
2499 double prev_time = init_time;
2500 unsigned long long int cnt = 0;
2501 unsigned long long int prev_cnt = 0;
2502 unsigned long long int start_cnt = 300000;
2504 int buffer_id[NUM_SENDER_THREADS];
2506 int total_words = 0;
2508 for (
int i = 0; i < NUM_SENDER_THREADS; i++) {
2510 buffer_filled[i][0] = 0;
2511 buffer_filled[i][1] = 0;
2512 copy_nwords[i][0] = 0;
2513 copy_nwords[i][1] = 0;
2521 printf(
"[DEBUG] Initializing PCIe40 readout...\n"); fflush(stdout);
2523 std::ofstream the_file ;
2524 if (writeInFile) the_file.open(
"data_file.txt") ;
2525 double triggerRate = 400 ;
2526 double data_size = 0. ;
2528 int res = ecs_open(0, 0) ;
2531 pthread_mutex_lock(&(mtx_sender_log));
2532 printf(
"ERROR: Could not open device (BAR 0)\n") ;
2533 pthread_mutex_unlock(&(mtx_sender_log));
2535 pthread_mutex_lock(&(mtx_sender_log));
2536 printf(
"SUCCESS: Device opened for ECS 0\n");
2537 pthread_mutex_unlock(&(mtx_sender_log));
2540 res = ecs_open(0, 2) ;
2542 pthread_mutex_lock(&(mtx_sender_log));
2543 printf(
"ERROR: Could not open device (BAR 2)\n") ;
2544 pthread_mutex_unlock(&(mtx_sender_log));
2546 pthread_mutex_lock(&(mtx_sender_log));
2547 printf(
"SUCCESS: Device opened for ECS 2\n");
2548 pthread_mutex_unlock(&(mtx_sender_log));
2553 pthread_mutex_lock(&(mtx_sender_log));
2554 printf(
"ERROR: Could not open device (DMA)\n") ;
2555 pthread_mutex_unlock(&(mtx_sender_log));
2557 pthread_mutex_lock(&(mtx_sender_log));
2558 printf(
"SUCCESS: Device opened for DMA\n");
2559 pthread_mutex_unlock(&(mtx_sender_log));
2564 unsigned int masks0 = ecs_read(0, 2, 0x50520) ;
2565 unsigned int masks1 = ecs_read(0, 2, 0x50540) ;
2566 std::vector< int > valid_ch ;
2568 for (
int i = 0 ; i < 24 ; i++) {
2569 if ((masks0 & (1 << i)) != 0) valid_ch.push_back(i) ;
2571 for (
int i = 24 ; i < MAX_PCIE40_CH ; i++) {
2572 if ((masks1 & (1 << (i - 24))) != 0) valid_ch.push_back(i) ;
2587 int num_of_chs = valid_ch.size() ;
2588 pthread_mutex_lock(&(mtx_sender_log));
2589 printf(
"[DEBUG] # of used channels = %d\n", num_of_chs); fflush(stdout);
2590 pthread_mutex_unlock(&(mtx_sender_log));
2591 if (num_of_chs <= 0) {
2592 pthread_mutex_lock(&(mtx_sender_log));
2593 printf(
"[FATAL] %s : No channels are used for this PCIe40 board. Please mask this readout PC with runcontrol GUI (or exclude sub-system if this is the only readout PC of the sub-system). Exiting..\n",
2596 pthread_mutex_unlock(&(mtx_sender_log));
2601 for (
int i = 0; i < NUM_SENDER_THREADS; i++) {
2602 for (
int j = 0; j < MAX_PCIE40_CH; j++) {
2603 crc_err_ch[i][j] = 0;
2610 ecs_write(0, 2, 0x00050400, 0x0);
2611 ecs_write(0, 2, 0x00050400, 0x4);
2612 ecs_write(0, 2, 0x00050400, 0x0);
2614 unsigned int initial_value = pcie40_getNbWordInEvent(0) ;
2615 if (initial_value == 0) pcie40_setNbWordInEvent(0, 0xFF) ;
2616 pcie40_dmaReset(0) ;
2619 pcie40_enableGenerator(0) ;
2620 pcie40_useDataFromGenerator(0) ;
2622 pcie40_disableGenerator(0) ;
2623 pcie40_useDataFromFibers(0) ;
2626 int t_rate = 10416.666 / ((double) triggerRate) - 1 ;
2627 pcie40_setGeneratorTriggerFrequency(0, t_rate) ;
2628 pcie40_setNbEvents(0, 0) ;
2630 pcie40_resetSPages(0) ;
2631 pcie40_dmaSetReadout(0) ;
2632 pcie40_setSizeFromHeader(0) ;
2634 pcie40_setNbWordInEvent(0, size) ;
2635 pcie40_setBusyLevel(0, 0x502) ;
2638 pthread_mutex_lock(&(mtx_sender_log));
2639 printf(
"[DEBUG] PCIe40 readout was initialized.\n"); fflush(stdout);
2640 pthread_mutex_unlock(&(mtx_sender_log));
2646 pthread_t sender_thr[NUM_SENDER_THREADS];
2650 for (
int i = 0; i < NUM_SENDER_THREADS; i++) {
2651 snd_argv[i].sender_id = i;
2652 snd_argv[i].valid_ch = valid_ch;
2653 snd_argv[i].node_id = pcie40_node_id;
2654 int ret = pthread_create(&(sender_thr[i]), NULL, sender, &(snd_argv[i]));
2656 pthread_mutex_lock(&(mtx_sender_log));
2657 printf(
"[FATAL] Failed to create a thread. ret = %d. Exting...\n", ret);
2659 pthread_mutex_unlock(&(mtx_sender_log));
2665 pthread_mutex_lock(&(mtx_sender_log));
2667 printf(
"[FATAL] CRC check by software is disabled now !! Relying on check in PCIe40 firmware\n"); fflush(stdout);
2668 pthread_mutex_unlock(&(mtx_sender_log));
2676 unsigned int* data ;
2677 unsigned int* combined_data = NULL;
2678 unsigned int* buf_combined =
new unsigned int[ MAX_EVENT_WORDS ];
2679 int new_buf_combined = 0;
2680 long long int get_sp_cnt = 0 ;
2684 unsigned int evtnum = 0;
2685 unsigned int exprun = 0;
2686 unsigned int prev_exprun = 0;
2688 unsigned int esize = 0 ;
2689 int total_pages = 0 ;
2690 int index_pages = 0 ;
2691 int previous_index = 0 ;
2692 unsigned int frag_size = 0 ;
2694 double m_start_time = getTimeSec();
2695 double m_prev_time = 0.;
2696 unsigned int event_words = 0;
2697 double total_size_bytes = 0.;
2698 double prev_total_size_bytes = 0.;
2699 double total_eve_cnt = 0.;
2700 double prev_total_eve_cnt = 0.;
2702 int first_event_flag = 0;
2703 unsigned int evecnt = 0;
2704 unsigned int prev_evecnt = 0;
2706 int dma_hdr_offset = 0;
2711 pthread_mutex_lock(&(mtx_sender_log));
2712 printf(
"[INFO] des_ser_PCIe40_main: Reading the 1st event from a PCIe40 board...\n"); fflush(stdout);
2713 pthread_mutex_unlock(&(mtx_sender_log));
2721 rv = pcie40_dmaStart(0) ;
2724 for (
int j = 0 ; j < rv * S_PAGE_SLOT_NMB ; ++j) {
2726 data = pcie40_getSuperPageCopy(0, (get_sp_cnt / S_PAGE_SLOT_NMB) % S_PAGES, get_sp_cnt % S_PAGE_SLOT_NMB) ;
2728 checkEventGenerator(data, get_sp_cnt, size);
2731 int ret = checkDMAHeader(data, frag_size, data_size, total_pages, index_pages) ;
2733 if (first_event_flag == 0) {
2734 pthread_mutex_lock(&(mtx_sender_log));
2735 printf(
"[INFO] des_ser_PCIe40_main: Done. the size of the 1st packet is %d bytes.\n", (
int)data_size); fflush(stdout);
2736 pthread_mutex_unlock(&(mtx_sender_log));
2737 first_event_flag = 1;
2740 if (first_flag == 0 && index_pages != 0 && ret < 1) {
2741 pthread_mutex_lock(&(mtx_sender_log));
2742 printf(
"Invalid index error : tot %d index %d ret %d\n", total_pages, index_pages, ret);
2743 pthread_mutex_unlock(&(mtx_sender_log));
2749 if (total_pages > 1 && total_pages <= 0xffff) {
2753 if (index_pages == 0) {
2755 if (total_pages * S_PAGE_SLOT_SIZE / 4 > MAX_EVENT_WORDS) {
2756 new_buf_combined = 1;
2757 combined_data =
new unsigned int[ total_pages * S_PAGE_SLOT_SIZE / 4 ] ;
2759 new_buf_combined = 0;
2760 combined_data = buf_combined;
2763 esize += frag_size ;
2766 if (combined_data == NULL) {
2767 pthread_mutex_lock(&(mtx_sender_log));
2768 printf(
"[FATAL] Data buffer is not yet allocated. %p\n", combined_data);
2769 pthread_mutex_unlock(&(mtx_sender_log));
2774 memcpy(&combined_data[ previous_index ], data + DMA_HDR_WORDS, 8 * (frag_size - 2) * 4) ;
2777 previous_index = previous_index + 8 * (frag_size - 2) ;
2780 if (index_pages != (total_pages - 1)) {
2782 if ((get_sp_cnt > 0) && ((get_sp_cnt % S_PAGE_SLOT_NMB) == 0)) pcie40_freeSuperPage(0, 1) ;
2788 }
else if (total_pages == 1) {
2791 combined_data = data;
2792 new_buf_combined = 2;
2793 dma_hdr_offset = DMA_HDR_WORDS;
2795 pthread_mutex_lock(&(mtx_sender_log));
2796 printf(
"Invalid total pages %d\n", total_pages);
2797 pthread_mutex_unlock(&(mtx_sender_log));
2802 if (exit_on_error) exit(0) ;
2810 if (combined_data != NULL) {
2812 event_words = combined_data[ dma_hdr_offset + EVENT_LEN_POS ];
2813 if (event_words >= 0 && event_words < 32000) {
2814 total_size_bytes += ((double)event_words) * 4.;
2817 pthread_mutex_lock(&(mtx_sender_log));
2818 printf(
"Strange event size %.8x ret %d\n", event_words, ret);
2819 printFullData(combined_data + dma_hdr_offset);
2820 pthread_mutex_unlock(&(mtx_sender_log));
2826 previous_index = 0 ;
2831 if (event_words > 0 && event_words < MAX_EVENT_WORDS) {
2836 unsigned int* temp_data = combined_data + dma_hdr_offset;
2837 if (evtnum + 1 != temp_data[EVENUM_POS]) {
2838 if (exprun == temp_data[RUNNO_POS]
2840 printEventNumberError(temp_data, evtnum, exprun, 1, -1);
2841 #ifndef NO_ERROR_STOP
2847 if (exprun != prev_exprun || exprun == 0) {
2848 m_start_time = getTimeSec();
2849 m_prev_time = m_start_time;
2853 prev_total_eve_cnt = 0;
2854 total_size_bytes = 0.;
2855 prev_total_size_bytes = 0.;
2857 evtnum = temp_data[EVENUM_POS];
2858 prev_exprun = exprun;
2859 exprun = temp_data[RUNNO_POS];
2864 client_id = client_id % NUM_SENDER_THREADS;
2867 zmq_writer[client_id]->send(combined_data + dma_hdr_offset, event_words *
sizeof(
unsigned int));
2870 if (buffer_id[client_id] == 0) {
2872 if (buffer_filled[client_id][0] == 0)
break;
2877 pthread_mutex_lock(&(mtx1_ch[client_id]));
2878 memcpy(data_1[client_id], combined_data + dma_hdr_offset, event_words *
sizeof(
unsigned int));
2879 copy_nwords[client_id][0] = event_words;
2880 buffer_filled[client_id][0] = 1;
2881 pthread_mutex_unlock(&(mtx1_ch[client_id]));
2885 if (buffer_filled[client_id][1] == 0)
break;
2889 pthread_mutex_lock(&(mtx2_ch[client_id]));
2891 memcpy(data_2[client_id], combined_data + dma_hdr_offset, event_words *
sizeof(
unsigned int));
2892 copy_nwords[client_id][1] = event_words;
2893 buffer_filled[client_id][1] = 1;
2894 pthread_mutex_unlock(&(mtx2_ch[client_id]));
2898 if (buffer_id[client_id] == 0) {
2899 buffer_id[client_id] = 1;
2901 buffer_id[client_id] = 0;
2906 pthread_mutex_lock(&(mtx_sender_log));
2907 printf(
"[FATAL] Invalid event-size %d\n", event_words);
2909 pthread_mutex_unlock(&(mtx_sender_log));
2916 previous_index = 0 ;
2917 if (new_buf_combined == 1) {
2918 delete [] combined_data ;
2919 combined_data = NULL;
2920 }
else if (new_buf_combined == 2) {
2927 if ((evecnt % 100000) == 0 ||
2928 ((evecnt % 10000) == 0 && 0 < evecnt && evecnt < 100000) ||
2931 unsigned int sum_total_crc_good = 0;
2932 unsigned int sum_total_crc_errors = 0;
2933 unsigned int sum_err_flag_cnt = 0;
2934 unsigned int sum_cur_evtnum = 0;
2935 unsigned int sum_err_not_reduced = 0;
2936 unsigned int sum_err_bad_7f7f = 0;
2937 unsigned int sum_err_bad_runnum = 0;
2938 unsigned int sum_err_bad_linknum = 0;
2939 unsigned int sum_err_bad_evenum = 0;
2940 unsigned int sum_err_bad_ffaa = 0;
2941 unsigned int sum_err_bad_ff55 = 0;
2942 unsigned int sum_err_bad_linksize = 0;
2943 unsigned int sum_err_link_eve_jump = 0;
2944 unsigned int sum_crc_err_ch[ MAX_PCIE40_CH] = {0};
2947 for (
int l = 0; l < NUM_SENDER_THREADS; l++) {
2948 sum_total_crc_good += total_crc_good[l];
2949 sum_total_crc_errors += total_crc_errors[l];
2950 sum_err_flag_cnt += err_flag_cnt[l];
2951 sum_cur_evtnum += cur_evtnum[l];
2952 sum_err_not_reduced += err_not_reduced[l];
2953 sum_err_bad_7f7f += err_bad_7f7f[l];
2954 sum_err_bad_runnum += err_bad_runnum[l];
2955 sum_err_bad_linknum += err_bad_linknum[l];
2956 sum_err_bad_evenum += err_bad_evenum[l];
2957 sum_err_bad_ffaa += err_bad_ffaa[l];
2958 sum_err_bad_ff55 += err_bad_ff55[l];
2959 sum_err_bad_linksize += err_bad_linksize[l];
2960 sum_err_link_eve_jump += err_link_eve_jump[l];
2969 for (
int m = 0; m < MAX_PCIE40_CH; m++) {
2970 sum_crc_err_ch[m] += crc_err_ch[l][m];
2975 double cur_time = getTimeSec();
2976 double total_time = cur_time - m_start_time;
2977 double interval = cur_time - m_prev_time;
2978 m_prev_time = cur_time;
2982 t_st = localtime(&timer);
2983 pthread_mutex_lock(&(mtx_sender_log));
2985 printf(
"[DEBUG] Event %12d Rate %6.2lf[kHz] Recvd %6.2lf[MB/s] RunTime %8.2lf[s] interval %8.4lf[s] evenum %12d exprun 0x%.8x eve_size %6.2lf[kB] numch %d nonred %u crcok %u crcng %u evejump %d bad_7f7f %d bad_runnum %d bad_linknum %d bad_evenum %d bad_ffaa %d bad_ff55 %d bad_linksize %d no_data %d bad_header %d bad_size %d bad_size_dmatrl %d bad_dmatrl %d bad_word_size %d %s",
2986 evecnt, (evecnt - prev_evecnt) / interval / 1.e3,
2987 (total_size_bytes - prev_total_size_bytes) / interval / 1.e6,
2988 total_time, interval,
2990 (total_size_bytes - prev_total_size_bytes) / (total_eve_cnt - prev_total_eve_cnt) / 1.e3,
2992 sum_err_not_reduced, sum_total_crc_good, sum_total_crc_errors, sum_err_link_eve_jump,
2994 sum_err_bad_runnum, sum_err_bad_linknum, sum_err_bad_evenum, sum_err_bad_ffaa, sum_err_bad_ff55, sum_err_bad_linksize,
2995 dmaerr_no_data, dmaerr_bad_header, dmaerr_bad_size, dmaerr_bad_size_dmatrl, dmaerr_bad_dmatrl, dmaerr_bad_word_size,
2998 pthread_mutex_unlock(&(mtx_sender_log));
2999 prev_total_size_bytes = total_size_bytes;
3000 prev_evecnt = evecnt;
3001 prev_total_eve_cnt = total_eve_cnt;
3005 if (writeInFile) writeToFile(the_file, data, esize) ;
3006 if ((get_sp_cnt > 0) && ((get_sp_cnt % S_PAGE_SLOT_NMB) == 0)) pcie40_freeSuperPage(0, 1) ;
3014 if (cnt == start_cnt) init_time = getTimeSec();
3015 if (cnt % 10000 == 1) {
3016 if (cnt > start_cnt) {
3017 double cur_time = getTimeSec();
3018 pthread_mutex_lock(&(mtx_sender_log));
3019 printf(
"run %d evt %lld time %.1lf dataflow %.1lf MB/s rate %.2lf kHz : so far dataflow %.1lf MB/s rate %.2lf kHz size %d\n",
3022 cur_time - init_time,
3023 NUM_SENDER_THREADS * (cnt - prev_cnt)*total_words *
sizeof(
int) / 1000000. / (cur_time - prev_time),
3024 (cnt - prev_cnt) / (cur_time - prev_time) / 1000.,
3025 NUM_SENDER_THREADS * (cnt - start_cnt)*total_words *
sizeof(
int) / 1000000. / (cur_time - init_time),
3026 (cnt - start_cnt) / (cur_time - init_time) / 1000., total_words);
3028 pthread_mutex_unlock(&(mtx_sender_log));
3029 prev_time = cur_time;
3043 if (writeInFile) the_file.close() ;
3048 for (
int i = 0; i < NUM_SENDER_THREADS; i++) {
3049 pthread_join(sender_thr[i], NULL);
3051 pthread_mutex_destroy(&(mtx1_ch[i]));
3052 pthread_mutex_destroy(&(mtx2_ch[i]));
3057 pthread_mutex_destroy(&mtx_sender_log);
int main(int argc, char **argv)
Run all tests.