Belle II Software development
DesSerPrePC Class Reference

A class definition of an input module for Sequential ROOT I/O. More...

#include <DesSerPrePC.h>

Inheritance diagram for DesSerPrePC:
DesSer

Public Member Functions

 DesSerPrePC (const std::string &host_recv, int port_recv, const std::string &host_send, int port_send, int shmflag, const std::string &nodename, int nodeid)
 Constructor / Destructor.
 
void DataAcquisition () override
 Module functions to be called from event process.
 
void initialize (bool close_listen=true)
 
int * getPreAllocBuf ()
 Getbuffer.
 
int * getNewBuffer (int nwords, int *delete_flag)
 Getbuffer.
 
void recordTime (int event, double *array)
 store time info.
 
unsigned int calcSimpleChecksum (int *buf, int nwords)
 calculate checksum
 
void clearNumUsedBuf ()
 
void RateMonitor (unsigned int nevt)
 monitor rate
 
int * shmGet (int fd, int size_words)
 Get shared memory.
 
void initializeNode ()
 Module functions to be called from main process.
 
void beginRun ()
 Module functions to be called from event process.
 
void endRun ()
 
void terminate ()
 
int sendByWriteV (RawDataBlockFormat *rawdblk)
 
void Accept (bool close_listen=true)
 
void fillSendHeaderTrailer (SendHeader *hdr, SendTrailer *trl, RawDataBlockFormat *rawdblk)
 
int Send (int socket, char *buf, int size_bytes)
 send buffer
 

Static Public Member Functions

static void printData (const int *buf, int nwords)
 dump error data
 
static double getTimeSec ()
 store time info.
 
static unsigned int calcXORChecksum (const int *buf, int nwords)
 calculate checksum
 
static void shmOpen (char *path_cfg, char *path_sta)
 open shared memory
 

Public Attributes

int max_nevt = 0
 
double max_seconds = 0.0
 time to stop a run
 
int m_compressionLevel = 0
 Compression Level.
 
int n_basf2evt = 0
 No. of sent events.
 
int m_prev_nevt = 0
 No. of prev sent events.
 
std::string m_dump_fname
 dump filename
 
FILE * m_fp_dump = nullptr
 dump file descripter
 
int * m_buffer = nullptr
 buffer
 
int * m_bufary [NUM_PREALLOC_BUF] = {}
 buffer
 
timeval m_t0
 
double m_recvd_totbytes = 0.0
 
double m_recvd_prev_totbytes = 0.0
 
double m_sent_totbytes = 0.0
 
double m_sent_prev_totbytes = 0.0
 
int m_ncycle = 0
 
double cur_time = 0.0
 for time monitoring
 
double m_start_time = 0.0
 
double m_prev_time = 0.0
 
int prev_event = 0
 
int m_num_usedbuf = 0
 
std::string m_nodename
 Node Name for SLC.
 
int m_nodeid = 0
 Node ID for SLC.
 
RunInfoBuffer m_status
 Run info buffer.
 
unsigned int m_exprunsubrun_no
 run no.
 
unsigned int m_prev_exprunsubrun_no
 run no.
 
int m_exp_no = 0
 exp no.
 
int m_data_type = 0
 data type
 
int m_trunc_mask = 0
 trunc mask
 
int m_shmflag = 0
 Use shared memory.
 
int m_shmfd_cfg = 0
 file descripter for shm
 
int m_shmfd_sta = 0
 file descripter for shm
 
int monitor_numeve = 0
 buffer for shared memory
 
int m_start_flag = 0
 start flag
 
CprErrorMessage print_err
 wrapper for B2LOG system
 
PreRawCOPPERFormat_v2 m_pre_rawcpr
 report status to SLC
 
int * m_cfg_buf = nullptr
 buffer for shared memory
 
int * m_cfg_sta = nullptr
 buffer for shared memory
 
int m_run_pause = 0
 flag to show that run-controller pauses a run
 
int m_run_error = 0
 flag to show that there is some errors with which DAQ cannot continue.
 

Protected Types

enum  {
  COPPER = 1 ,
  ROPC = 2
}
 

Protected Member Functions

int Connect ()
 Accept connection.
 
int recvFD (int fd, char *buf, int data_size_byte, int flag)
 receive data
 
int * recvData (int *delete_flag, int *total_m_size_word, int *num_events_in_sendblock, int *num_nodes_in_sendblock)
 receive data
 
void setRecvdBuffer (RawDataBlockFormat *raw_datablk, int *delete_flag)
 attach buffer to RawDataBlock
 
void checkData (RawDataBlockFormat *raw_datablk, unsigned int *eve_copper_0)
 check data contents
 

Protected Attributes

int m_num_connections
 check data contents
 
std::vector< std::string > m_hostname_from
 Reciever basf2 Socket.
 
std::vector< int > m_port_from
 port # to connect data sources
 
std::vector< int > m_socket_recv
 
int event_diff = 0
 
unsigned int m_prev_copper_ctr = 0
 
unsigned int m_prev_evenum = 0
 
int m_socket_send = 0
 Reciever Socket.
 
std::string m_hostname_local
 Destination Host.
 
int m_port_to = 0
 Destination port.
 
std::string p_method
 EvtSocket.
 
int p_method_val = 0
 
RawHeader_v2 tmp_header
 which format is used
 

Detailed Description

A class definition of an input module for Sequential ROOT I/O.

Definition at line 26 of file DesSerPrePC.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
protectedinherited

Definition at line 269 of file DesSer.h.

269 {
270 COPPER = 1,
271 ROPC = 2
272 };

Constructor & Destructor Documentation

◆ DesSerPrePC()

DesSerPrePC ( const std::string & host_recv,
int port_recv,
const std::string & host_send,
int port_send,
int shmflag,
const std::string & nodename,
int nodeid )

Constructor / Destructor.

Definition at line 28 of file DesSerPrePC.cc.

30{
31
32 for (int i = 0 ; i < m_num_connections; i++) {
33 // m_hostname_from.push_back( "localhost");
34 m_hostname_from.push_back(host_recv);
35 // m_port_from.push_back(30000);
36 m_port_from.push_back(port_recv) ;
37 m_socket_recv.push_back(-1);
38 }
39
40 // m_port_to = 31001;
41 m_port_to = port_send;
42 // m_hostname_local = "localhost";
43 m_hostname_local = host_send;
44 m_nodename = nodename;
45
46 m_shmflag = shmflag;
47
48 // B2INFO("DeSerializerPrePC: Constructor done.");
49 printf("[INFO] DeSerializerPrePC: Constructor done.\n"); fflush(stdout);
50}
int m_port_to
Destination port.
Definition DesSer.h:278
std::string m_hostname_local
Destination Host.
Definition DesSer.h:275
std::string m_nodename
Node Name for SLC.
Definition DesSer.h:135
std::vector< int > m_port_from
port # to connect data sources
Definition DesSer.h:246
int m_shmflag
Use shared memory.
Definition DesSer.h:159
int m_num_connections
check data contents
Definition DesSer.h:237
std::vector< std::string > m_hostname_from
Reciever basf2 Socket.
Definition DesSer.h:243

◆ ~DesSerPrePC()

~DesSerPrePC ( )
overridevirtual

Definition at line 54 of file DesSerPrePC.cc.

55{
56}

Member Function Documentation

◆ Accept()

void Accept ( bool close_listen = true)
inherited

Definition at line 365 of file DesSer.cc.

366{
367 //
368 // Connect to cprtb01
369 //
370 struct hostent* host;
371 host = gethostbyname(m_hostname_local.c_str());
372 if (host == NULL) {
373 char temp_buf[500];
374 sprintf(temp_buf, "[FATAL] hostname(%s) cannot be resolved(%s). Check /etc/hosts. Exiting...\n",
375 m_hostname_local.c_str(), strerror(errno));
376 print_err.PrintError(temp_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
377 exit(1);
378 }
379
380 //
381 // Bind and listen
382 //
383 int fd_listen;
384 struct sockaddr_in sock_listen;
385 sock_listen.sin_family = AF_INET;
386 sock_listen.sin_addr.s_addr = *reinterpret_cast<unsigned int*>(host->h_addr_list[0]);
387
388 socklen_t addrlen = sizeof(sock_listen);
389 sock_listen.sin_port = htons(m_port_to);
390 fd_listen = socket(PF_INET, SOCK_STREAM, 0);
391
392 int flags = 1;
393 int ret = setsockopt(fd_listen, SOL_SOCKET, SO_REUSEADDR, &flags, (socklen_t)sizeof(flags));
394 if (ret < 0) {
395 perror("Failed to set REUSEADDR");
396 }
397
398 if (bind(fd_listen, reinterpret_cast<struct sockaddr*>(&sock_listen), sizeof(struct sockaddr)) < 0) {
399 printf("[FATAL] Failed to bind. Maybe other programs have already occupied this port(%d). Exiting...\n",
400 m_port_to); fflush(stdout);
401 // Check the process occupying the port 30000.
402 FILE* fp;
403 char buf[256];
404 char cmdline[500];
405 sprintf(cmdline, "/usr/sbin/ss -ap | grep %d", m_port_to);
406 if ((fp = popen(cmdline, "r")) == NULL) {
407 printf("[WARNING] Failed to run %s\n", cmdline);
408 } else {
409 while (fgets(buf, 256, fp) != NULL) {
410 printf("[ERROR] Failed to bind. output of ss(port %d) : %s\n", m_port_to, buf); fflush(stdout);
411 }
412 pclose(fp);
413 }
414 // Error message
415 char temp_char[500];
416 sprintf(temp_char, "[FATAL] Failed to bind.(%s) Maybe other programs have already occupied this port(%d). Exiting...",
417 strerror(errno), m_port_to);
418 print_err.PrintError(temp_char, __FILE__, __PRETTY_FUNCTION__, __LINE__);
419 exit(1);
420 }
421
422 int val1 = 0;
423 setsockopt(fd_listen, IPPROTO_TCP, TCP_NODELAY, &val1, (socklen_t)sizeof(val1));
424 int backlog = 1;
425 if (listen(fd_listen, backlog) < 0) {
426 char err_buf[500];
427 sprintf(err_buf, "Failed in listen(%s). Exting...", strerror(errno));
428 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
429 exit(-1);
430 }
431
432 //
433 // Accept
434 //
435 int fd_accept;
436 struct sockaddr_in sock_accept;
437 printf("[DEBUG] Accepting... : port %d server %s\n", m_port_to, m_hostname_local.c_str());
438 fflush(stdout);
439
440 if ((fd_accept = accept(fd_listen, reinterpret_cast<struct sockaddr*>(& (sock_accept)), &addrlen)) == 0) {
441 char err_buf[500];
442 sprintf(err_buf, "[FATAL] Failed to accept(%s). Exiting...", strerror(errno));
443 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
444 exit(-1);
445 } else {
446 // B2INFO("Done.");
447 printf("[DEBUG] Done.\n"); fflush(stdout);
448
449 // set timepout option
450 struct timeval timeout;
451 timeout.tv_sec = 1;
452 timeout.tv_usec = 0;
453 ret = setsockopt(fd_accept, SOL_SOCKET, SO_SNDTIMEO, &timeout, (socklen_t)sizeof(timeout));
454 if (ret < 0) {
455 const char temp_char[100] = "[FATAL] Failed to set TIMEOUT. Exiting...";
456 print_err.PrintError(temp_char, __FILE__, __PRETTY_FUNCTION__, __LINE__);
457 exit(-1);
458 }
459 }
460
461 if (close_listen) {
462 close(fd_listen);
463 }
464
465 // int flag = 1;
466 // ret = setsockopt(fd_accept, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(flag) );
467 m_socket_send = fd_accept;
468
469 if (m_status.isAvailable()) {
470 m_status.setOutputPort(ntohs(sock_listen.sin_port));
471 m_status.setOutputAddress(sock_listen.sin_addr.s_addr);
472 // B2INFO("Accepted " << (int)ntohs(sock_listen.sin_port) << " " << (int)sock_listen.sin_addr.s_addr);
473 printf("Accepted. port %d address %d\n", (int)ntohs(sock_listen.sin_port), (int)sock_listen.sin_addr.s_addr); fflush(stdout);
474 }
475
476 return;
477
478}
RunInfoBuffer m_status
Run info buffer.
Definition DesSer.h:141
int m_socket_send
Reciever Socket.
Definition DesSer.h:267
CprErrorMessage print_err
wrapper for B2LOG system
Definition DesSer.h:184

◆ calcXORChecksum()

unsigned int calcXORChecksum ( const int * buf,
int nwords )
staticinherited

calculate checksum

Definition at line 497 of file DesSer.cc.

498{
499 unsigned int checksum = 0;
500 for (int i = 0; i < nwords; i++) {
501 checksum = checksum ^ buf[ i ];
502 }
503 return checksum;
504}

◆ checkData()

void checkData ( RawDataBlockFormat * raw_datablk,
unsigned int * eve_copper_0 )
protected

check data contents

Definition at line 382 of file DesSerPrePC.cc.

383{
384 // int data_size_copper_0 = -1;
385 // int data_size_copper_1 = -1;
386
387 //
388 // Data check
389 //
390 int* temp_buf = raw_datablk->GetBuffer(0);
391 int cpr_num = 0;
392 unsigned int cur_evenum = 0, cur_copper_ctr = 0;
393 unsigned int eve_array[32]; // # of noeds is less than 17
394 unsigned int utime_array[32];// # of noeds is less than 17
395 unsigned int ctime_type_array[32];// # of noeds is less than 17
396
397#ifdef DUMHSLB
398 unsigned int exp_run_ftsw = 0, ctime_trgtype_ftsw = 0, utime_ftsw = 0;
399#endif
400
401 for (int k = 0; k < raw_datablk->GetNumEvents(); k++) {
402 memset(eve_array, 0, sizeof(eve_array));
403 memset(utime_array, 0, sizeof(utime_array));
404 memset(ctime_type_array, 0, sizeof(ctime_type_array));
405
406 int num_nodes_in_sendblock = raw_datablk->GetNumNodes();
407 for (int l = 0; l < num_nodes_in_sendblock; l++) {
408 int entry_id = l + k * num_nodes_in_sendblock;
409
410 //
411 // RawFTSW
412 //
413 if (raw_datablk->CheckFTSWID(entry_id)) {
414 RawFTSWFormat_latest* temp_rawftsw = new RawFTSWFormat_latest;
415 int block_id = 0;
416 temp_rawftsw->SetBuffer(static_cast<int*>(temp_buf) + raw_datablk->GetBufferPos(entry_id),
417 raw_datablk->GetBlockNwords(entry_id), 0, 1, 1);
418 if (temp_rawftsw->GetEveNo(block_id) < 10) {
419 printf("[DEBUG] ######FTSW#########\n");
420 printData(static_cast<int*>(temp_buf) + raw_datablk->GetBufferPos(entry_id), raw_datablk->GetBlockNwords(entry_id));
421 }
422
423#ifdef DUMHSLB
424 exp_run_ftsw = temp_rawftsw->GetExpRunSubrun(block_id);
425 ctime_trgtype_ftsw = temp_rawftsw->GetTTCtimeTRGType(block_id);
426 utime_ftsw = temp_rawftsw->GetTTUtime(block_id);
427#endif
428
429
430#ifndef NO_DATA_CHECK
431 try {
432 temp_rawftsw->CheckData(0, m_prev_evenum, &cur_evenum, m_prev_exprunsubrun_no, &m_exprunsubrun_no);
433 eve_array[ entry_id ] = cur_evenum;
434 } catch (const string& err_str) {
435 char err_buf[500];
436 strcpy(err_buf, err_str.c_str());
437 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
438 exit(1);
439 }
440#endif
441 utime_array[ entry_id ] = temp_rawftsw->GetTTUtime(block_id);
442 ctime_type_array[ entry_id ] = temp_rawftsw->GetTTCtimeTRGType(block_id);
443 delete temp_rawftsw;
444
445 //
446 // RawTLU
447 //
448 } else if (raw_datablk->CheckTLUID(entry_id)) {
449
450 RawTLUFormat* temp_rawtlu = new RawTLUFormat;
451 temp_rawtlu->SetBuffer(static_cast<int*>(temp_buf) + raw_datablk->GetBufferPos(entry_id),
452 raw_datablk->GetBlockNwords(entry_id), 0, 1, 1);
453 if (temp_rawtlu->GetEveNo(0) < 10
454 ) {
455 printf("[DEBUG] ######TLU#########\n");
456 printData(static_cast<int*>(temp_buf) + raw_datablk->GetBufferPos(entry_id), raw_datablk->GetBlockNwords(entry_id));
457 }
458
459#ifndef NO_DATA_CHECK
460 try {
461 temp_rawtlu->CheckData(0, m_prev_evenum, &cur_evenum);
462 eve_array[ entry_id ] = cur_evenum;
463 } catch (const string& err_str) {
464 char err_buf[500];
465 strcpy(err_buf, err_str.c_str());
466 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
467 exit(1);
468 }
469#endif
470 delete temp_rawtlu;
471 } else {
472 //
473 // RawCOPPER
474 //
475 PreRawCOPPERFormat_v2* pre_rawcpr_fmt = new PreRawCOPPERFormat_v2;
476 pre_rawcpr_fmt->SetBuffer(static_cast<int*>(temp_buf) + raw_datablk->GetBufferPos(entry_id),
477 raw_datablk->GetBlockNwords(entry_id), 0, 1, 1);
478
479#ifdef DUMHSLB
480 int block_id = 0;
481 "do not use the following for actual DAQ"
482 (pre_rawcpr_fmt->GetBuffer(block_id))[ RawHeader_v2::POS_EXP_RUN_NO ] = exp_run_ftsw;
483 (pre_rawcpr_fmt->GetBuffer(block_id))[ RawHeader_v2::POS_TTCTIME_TRGTYPE ] = ctime_trgtype_ftsw;
484 (pre_rawcpr_fmt->GetBuffer(block_id))[ RawHeader_v2::POS_TTUTIME ] = utime_ftsw;
485#endif
486
487#ifndef NO_DATA_CHECK
488 try {
489 pre_rawcpr_fmt->CheckData(0, m_prev_evenum, &cur_evenum,
490 m_prev_copper_ctr, &cur_copper_ctr,
492 eve_array[ entry_id ] = cur_evenum;
493 } catch (const string& err_str) {
494 exit(1); // Error in the contents of an event was detected
495 }
496#endif
497
498 utime_array[ entry_id ] = pre_rawcpr_fmt->GetTTUtime(0);
499 ctime_type_array[ entry_id ] = pre_rawcpr_fmt->GetTTCtimeTRGType(0);
500
501 if (cpr_num == 0) {
502 // data_size_copper_0 = raw_datablk->GetBlockNwords(entry_id);
503 *eve_copper_0 = (raw_datablk->GetBuffer(entry_id))[ 3 ];
504 } else if (cpr_num == 1) {
505 // data_size_copper_1 = raw_datablk->GetBlockNwords(entry_id);
506 }
507 cpr_num++;
508 delete pre_rawcpr_fmt;
509 }
510 }
511
512#ifndef NO_DATA_CHECK
513 // event #, ctime, utime over nodes
514 for (int l = 1; l < num_nodes_in_sendblock; l++) {
515 if (eve_array[ 0 ] != eve_array[ l ] ||
516 utime_array[ 0 ] != utime_array[ l ] ||
517 ctime_type_array[ 0 ] != ctime_type_array[ l ]) {
518 char err_buf[500];
519 for (int m = 0; m < num_nodes_in_sendblock; m++) {
520 printf("[DEBUG] node %d eve # %x utime %x ctime %x\n",
521 m, eve_array[ m ], utime_array[ m ], ctime_type_array[ m ]);
522 }
523 sprintf(err_buf, "[FATAL] CORRUPTED DATA: Event or Time record mismatch. Exiting...");
524 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
525 sleep(1234567);
526 exit(-1);
527 }
528 }
529#endif
530
531 // Event # monitor in runchange
532// if (m_prev_runsubrun_no != m_runsubrun_no) {
533// printf("[DEBUG] ##############################################\n");
534// for (int m = 0; m < raw_datablk->GetNumEntries(); m++) {
535// printf("[DEBUG] %d eve %u prev %u\n", m, eve_array[ m ], m_prev_evenum);
536// }
537// printf("[DEBUG] ##############################################\n");
538// fflush(stderr);
539// }
540 m_prev_evenum = cur_evenum;
541 m_prev_copper_ctr = cur_copper_ctr;
543 }
544 return;
545}
static void printData(const int *buf, int nwords)
dump error data
Definition DesSer.cc:507
unsigned int m_prev_exprunsubrun_no
run no.
Definition DesSer.h:147
unsigned int m_exprunsubrun_no
run no.
Definition DesSer.h:144
void CheckData(int n, unsigned int prev_evenum, unsigned int *cur_evenum, unsigned int prev_copper_ctr, unsigned int *cur_copper_ctr, unsigned int prev_exprunsubrun_no, unsigned int *cur_exprunsubrun_no) OVERRIDE_CPP17
check data contents
virtual int GetBlockNwords(int n)
get size of a data block
virtual int CheckFTSWID(int n)
get FTSW ID to check whether this data block is FTSW data or not
virtual int GetBufferPos(int n)
get position of data block in word
virtual void SetBuffer(int *bufin, int nwords, int delete_flag, int num_events, int num_nodes)
set buffer ( delete_flag : m_buffer is freed( = 0 )/ not freed( = 1 ) in Destructor )
virtual int GetNumNodes()
get # of data sources(e.g. # of COPPER boards) in m_buffer
virtual int * GetBuffer(int n)
get nth buffer pointer
virtual int GetNumEvents()
get # of events in m_buffer
virtual int CheckTLUID(int n)
get FTSW ID to check whether this data block is FTSW data or not
unsigned int GetEveNo(int n) OVERRIDE_CPP17
Get event #.
unsigned int GetTTUtime(int n) OVERRIDE_CPP17
get unixtime of the trigger
unsigned int GetTTCtimeTRGType(int n) OVERRIDE_CPP17
Get a word containing ctime and trigger type info.
void CheckData(int n, unsigned int prev_evenum, unsigned int *cur_evenum, unsigned int prev_exprunsubrun_no, unsigned int *cur_exprunsubrun_no) OVERRIDE_CPP17
check the data contents
void CheckData(int n, unsigned int prev_evenum, unsigned int *cur_evenum)
check data contents
unsigned int GetEveNo(int n)
Get Event #.
unsigned int GetExpRunSubrun(int n) OVERRIDE_CPP17
Exp# (10bit) run# (14bit) restart # (8bit)
unsigned int GetTTUtime(int n) OVERRIDE_CPP17
Check if COPPER Magic words are correct.
unsigned int GetTTCtimeTRGType(int n) OVERRIDE_CPP17
get b2l block from "FEE b2link header"

◆ clearNumUsedBuf()

void clearNumUsedBuf ( )
inlineinherited

Definition at line 121 of file DesSer.h.

122 {
123 m_num_usedbuf = 0;
124 return ;
125 }

◆ Connect()

int Connect ( )
protected

Accept connection.

Definition at line 114 of file DesSerPrePC.cc.

115{
116
117 for (int i = 0; i < m_num_connections; i++) {
118
119 if (m_socket_recv[ i ] >= 0) continue; // Already have an established socket
120
121 //
122 // Connect to a downstream node
123 //
124 struct hostent* host;
125 host = gethostbyname(m_hostname_from[ i ].c_str());
126 if (host == NULL) {
127 char err_buf[100];
128 sprintf(err_buf, "[FATAL] hostname(%s) cannot be resolved(%s). Check /etc/hosts. Exiting...", m_hostname_from[ i ].c_str(),
129 strerror(errno));
130 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
131 sleep(1234567);
132 exit(1);
133 }
134
135 struct sockaddr_in socPC;
136 socPC.sin_family = AF_INET;
137 socPC.sin_addr.s_addr = *reinterpret_cast<unsigned int*>(host->h_addr_list[0]);
138 socPC.sin_port = htons(m_port_from[ i ]);
139 int sd = socket(PF_INET, SOCK_STREAM, 0);
140 int val1 = 0;
141 setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, &val1, sizeof(val1));
142
143 struct timeval timeout;
144 timeout.tv_sec = 1;
145 timeout.tv_usec = 0;
146 setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &timeout, (socklen_t)sizeof(timeout));
147
148 printf("[DEBUG] Connecting to %s port %d\n", m_hostname_from[ i ].c_str(), m_port_from[ i ]); fflush(stdout);
149
150 while (1) {
151 if (connect(sd, reinterpret_cast<struct sockaddr*>((&socPC)), sizeof(socPC)) < 0) {
152 perror("Failed to connect. Retrying...");
153 usleep(500000);
154 } else {
155 // B2INFO("Done");
156 printf("[DEBUG] Done\n"); fflush(stdout);
157 break;
158 }
159 }
160 // m_socket_recv.push_back(sd);
161 m_socket_recv[ i ] = sd;
162
163 // check socket paramters
164 int val, len;
165 len = sizeof(val);
166 getsockopt(m_socket_recv[ i ], SOL_SOCKET, SO_RCVBUF, &val, reinterpret_cast<socklen_t*>(&len));
167 // B2INFO("SO_RCVBUF" << val);
168 getsockopt(m_socket_recv[ i ], SOL_SOCKET, SO_SNDBUF, &val, reinterpret_cast<socklen_t*>(&len));
169 // B2DEBUG("SO_SNDBUF" << val);
170 getsockopt(m_socket_recv[ i ], IPPROTO_TCP, TCP_MAXSEG, &val, reinterpret_cast<socklen_t*>(&len));
171 // B2DEBUG("TCP_MAXSEG" << val);
172 getsockopt(m_socket_recv[ i ], IPPROTO_TCP, TCP_NODELAY, &val, reinterpret_cast<socklen_t*>(&len));
173 // B2DEBUG("TCP_NODELAY" << val);
174
175 if (m_status.isAvailable()) {
176 sockaddr_in sa;
177 memset(&sa, 0, sizeof(sockaddr_in));
178 socklen_t sa_len = sizeof(sa);
179 if (getsockname(m_socket_recv[i], reinterpret_cast<struct sockaddr*>(&sa), static_cast<socklen_t*>(&sa_len)) == 0) {
180 m_status.setInputPort(ntohs(sa.sin_port));
181 m_status.setInputAddress(sa.sin_addr.s_addr);
182 }
183 }
184
185 }
186 // B2INFO("[DEBUG] Initialization finished");
187 printf("[DEBUG] Initialization finished\n"); fflush(stdout);
188 return 0;
189}

◆ DataAcquisition()

void DataAcquisition ( )
overridevirtual

Module functions to be called from event process.

Implements DesSer.

Definition at line 549 of file DesSerPrePC.cc.

550{
551 // For data check
552 unsigned int eve_copper_0 = 0;
553 // B2INFO("initializing...");
554 printf("[DEBUG] initializing...\n"); fflush(stdout);
555 initialize();
556
557 // B2INFO("Done.");
558 printf("[DEBUG] Done.\n"); fflush(stdout);
559
560 if (m_start_flag == 0) {
561 //
562 // Connect to eb0: This should be here because we want Serializer first to accept connection from eb1tx
563 //
564 Connect();
565 if (m_status.isAvailable()) {
566 // B2INFO("DeSerializerPrePC: Waiting for Start...\n");
567 printf("[DEBUG] DeSerializerPrePC: Waiting for Start...\n"); fflush(stdout);
568 m_status.reportRunning();
569 }
570 m_start_time = getTimeSec();
571 n_basf2evt = 0;
572 }
573
574 //
575 // Main loop
576 //
577 while (1) {
578 //
579 // Stand-by loop
580 //
581#ifdef NONSTOP
582 if (m_run_pause > 0 || m_run_error > 0) {
583 waitResume();
584 }
585#endif
586
587 clearNumUsedBuf();
588 // RawDataBlock raw_datablk[ NUM_EVT_PER_BASF2LOOP_PC ];
589 RawDataBlockFormat raw_datablk[ NUM_EVT_PER_BASF2LOOP_PC ];
590
591 //
592 // Recv loop
593 //
594 for (int j = 0; j < NUM_EVT_PER_BASF2LOOP_PC; j++) {
595 //
596 // Receive data from COPPER
597 //
598 eve_copper_0 = 0;
599 int delete_flag_from =
600 0; // Delete flag for temp_rawdatablk.It can be set to 1 by setRecvdBuffer if the buffer size is larger than that of pre-allocated buffer.
601 int delete_flag_to =
602 0; // Delete flag for raw_datablk[i]. It can be set to 1 by getNewBuffer if the buffer size is larger than that of pre-allocated buffer.
603 RawDataBlockFormat temp_rawdatablk;
604 try {
605 setRecvdBuffer(&temp_rawdatablk, &delete_flag_from);
606 checkData(&temp_rawdatablk, &eve_copper_0);
607 } catch (const string& err_str) {
608 printf("Error was detected\n"); fflush(stdout);
609 break;
610 }
611 // PreRawCOPPERFormat_v2 pre_rawcopper_v2;
612 // pre_rawcopper_v2.SetBuffer((int*)temp_rawdatablk.GetWholeBuffer(), temp_rawdatablk.TotalBufNwords(),
613 // 0, temp_rawdatablk.GetNumEvents(), temp_rawdatablk.GetNumNodes());
615
616 int temp_num_events, temp_num_nodes;
617 int temp_nwords_to;
618 int* buf_to = NULL;
619#ifdef REDUCED_RAWCOPPER
620 //
621 // Copy reduced buffer
622 //
623 // int* buf_to = getNewBuffer(m_pre_rawcpr.CalcReducedDataSize(&temp_rawdatablk), &delete_flag_to); // basf2-dependent style
624 int* temp_bufin = temp_rawdatablk.GetWholeBuffer();
625 int temp_nwords_from = temp_rawdatablk.TotalBufNwords();
626 temp_num_events = temp_rawdatablk.GetNumEvents();
627 temp_num_nodes = temp_rawdatablk.GetNumNodes();
628 int calced_temp_nwords_to = m_pre_rawcpr.CalcReducedDataSize(temp_bufin, temp_nwords_from, temp_num_events, temp_num_nodes);
629 buf_to = getNewBuffer(calced_temp_nwords_to, &delete_flag_to);
630
631 // m_pre_rawcpr.CopyReducedData(&temp_rawdatablk, buf_to, delete_flag_from); // basf2-dependent style
632 m_pre_rawcpr.CopyReducedData(temp_bufin, temp_nwords_from, temp_num_events, temp_num_nodes, buf_to, &temp_nwords_to);
633 if (calced_temp_nwords_to != temp_nwords_to) {
634 char err_buf[500];
635 sprintf(err_buf,
636 "[FATAL] CORRUPTED DATA: Estimations of reduced event size are inconsistent. CalcReducedDataSize = %d. CopyReducedData = %d. Exiting...",
637 calced_temp_nwords_to, temp_nwords_to);
638 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
639 exit(1);
640 }
641 m_status.copyEventHeader(buf_to);
642#else
643 buf_to = temp_rawdatablk.GetWholeBuffer();
644 temp_nwords_to = temp_rawdatablk.TotalBufNwords();
645 temp_num_events = temp_rawdatablk.GetNumEvents();
646 temp_num_nodes = temp_rawdatablk.GetNumNodes();
647 delete_flag_to = delete_flag_from;
648 delete_flag_from = 0; // to avoid double delete
649#endif
650
651 //
652 // Set buffer to the RawData class stored in DataStore
653 //
654// raw_datablk[ j ].SetBuffer( (int*)temp_rawdatablk.GetWholeBuffer(), temp_rawdatablk.TotalBufNwords(),
655// delete_flag_to, temp_rawdatablk.GetNumEvents(),
656// temp_rawdatablk.GetNumNodes());
657 raw_datablk[ j ].SetBuffer(buf_to, temp_nwords_to, delete_flag_to, temp_num_events, temp_num_nodes);
658
659
660 //
661 // CRC16 check after data reduction
662 //
663#ifdef REDUCED_RAWCOPPER
664 PostRawCOPPERFormat_v2 post_rawcopper_v2; // Should be the latest version before ver.4(PCIe40)
665
666// post_rawcopper_v2.SetBuffer((int*)temp_rawdatablk.GetWholeBuffer(), temp_rawdatablk.TotalBufNwords(),
667// 0, temp_rawdatablk.GetNumEvents(), temp_rawdatablk.GetNumNodes());
668 post_rawcopper_v2.SetBuffer(raw_datablk[ j ].GetWholeBuffer(), raw_datablk[ j ].TotalBufNwords(),
669 0, raw_datablk[ j ].GetNumEvents(), raw_datablk[ j ].GetNumNodes());
670
671 for (int i_finesse_num = 0; i_finesse_num < 4; i_finesse_num ++) {
672 int block_num = 0;
673 if (post_rawcopper_v2.GetFINESSENwords(block_num, i_finesse_num) > 0) {
674 post_rawcopper_v2.CheckCRC16(block_num, i_finesse_num);
675 }
676 }
677
678#endif
679 }
680
681#ifdef NONSTOP
682 // Goto Stand-by loop when run is paused or stopped by error
683 if (m_run_pause != 0 || m_run_error != 0) continue;
684#endif
685
686
688 // From Serializer.cc
690 if (m_start_flag == 0) {
691 m_start_time = getTimeSec();
692 n_basf2evt = 0;
693 }
694 // StoreArray<RawCOPPER> rawcprarray;
695 // StoreArray<RawDataBlock> raw_dblkarray;
696
697 for (int j = 0; j < NUM_EVT_PER_BASF2LOOP_PC; j++) {
698
699 //
700 // Send data
701 //
702 if (m_start_flag == 0) {
703 // B2INFO("SerializerPC: Sending the 1st packet...");
704 printf("[DEBUG] SerializerPC: Sending the 1st packet...\n"); fflush(stdout);
705 }
706
707 try {
708 m_sent_totbytes += sendByWriteV(&(raw_datablk[ j ]));
709 } catch (const string& err_str) {
710#ifdef NONSTOP
711 break;
712#endif
713 print_err.PrintError(const_cast<char*>(err_str.c_str()), __FILE__, __PRETTY_FUNCTION__, __LINE__);
714 exit(1);
715 }
716 if (m_start_flag == 0) {
717 // B2INFO("Done. ");
718 printf("[DEBUG] Done.\n"); fflush(stdout);
719 m_start_flag = 1;
720 }
721 }
722
723#ifdef NONSTOP
724 // Goto Stand-by loop when run is paused or stopped by error
725 if (m_run_pause != 0 || m_run_error != 0) continue;
726#endif
727
728 //
729 // Monitor
730 //
731 if (max_nevt >= 0 || max_seconds >= 0.) {
732#ifdef AIUEO
733 if (n_basf2evt % 10000 == 0) {
734// if ((n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC >= max_nevt && max_nevt > 0)
735// || (getTimeSec() - m_start_time > max_seconds && max_seconds > 0.)) {
736 printf("[DEBUG] RunStop was detected. ( Setting: Max event # %d MaxTime %lf ) Processed Event %d Elapsed Time %lf[s]\n",
737 max_nevt, max_seconds, n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC, getTimeSec() - m_start_time);
738 }
739#endif
740 }
741
742 if ((n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC) % 100000 == 0) {
743 double interval = cur_time - m_prev_time;
744 double total_time = cur_time - m_start_time;
745 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",
746 n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC,
747 (n_basf2evt - m_prev_nevt)*NUM_EVT_PER_BASF2LOOP_PC / interval / 1.e3,
748 (m_recvd_totbytes - m_recvd_prev_totbytes) / interval / 1.e6,
749 (m_sent_totbytes - m_sent_prev_totbytes) / interval / 1.e6,
750 total_time,
751 interval);
752 fflush(stdout);
753
754 m_prev_time = cur_time;
755 m_recvd_prev_totbytes = m_recvd_totbytes;
756 m_sent_prev_totbytes = m_sent_totbytes;
759 }
760
761 n_basf2evt++;
762
763 if (m_status.isAvailable()) {
764 m_status.setOutputNBytes(m_sent_totbytes);
765 m_status.setOutputCount(n_basf2evt * NUM_EVT_PER_BASF2LOOP_PC);
766 }
767
768 }
769
770 return;
771}
int Connect()
Accept connection.
void setRecvdBuffer(RawDataBlockFormat *raw_datablk, int *delete_flag)
attach buffer to RawDataBlock
void checkData(RawDataBlockFormat *raw_datablk, unsigned int *eve_copper_0)
check data contents
int m_start_flag
start flag
Definition DesSer.h:181
int n_basf2evt
No. of sent events.
Definition DesSer.h:70
PreRawCOPPERFormat_v2 m_pre_rawcpr
report status to SLC
Definition DesSer.h:191
int m_run_pause
flag to show that run-controller pauses a run
Definition DesSer.h:227
int m_run_error
flag to show that there is some errors with which DAQ cannot continue.
Definition DesSer.h:230
int * getNewBuffer(int nwords, int *delete_flag)
Getbuffer.
Definition DesSer.cc:77
double cur_time
for time monitoring
Definition DesSer.h:102
int m_prev_nevt
No. of prev sent events.
Definition DesSer.h:73
double max_seconds
time to stop a run
Definition DesSer.h:64
static double getTimeSec()
store time info.
Definition DesSer.cc:480
int CheckCRC16(int n, int finesse_num)
check magic words
int GetFINESSENwords(int n, int finesse) OVERRIDE_CPP17
get data size of FINESSE buffer
void CopyReducedData(int *bufin, int nwords, int num_events, int num_nodes, int *buf_to, int *nwords_to)
reduce and merge header/trailer
int CalcReducedDataSize(int *bufin, int nwords, int num_events, int num_nodes)
reduce and merge header/trailer
virtual int * GetWholeBuffer()
get pointer to buffer(m_buffer)
virtual int TotalBufNwords()
Get total length of m_buffer.

◆ fillSendHeaderTrailer()

void fillSendHeaderTrailer ( SendHeader * hdr,
SendTrailer * trl,
RawDataBlockFormat * rawdblk )
inherited

Definition at line 171 of file DesSer.cc.

172{
173
174 int total_send_nwords =
175 hdr->GetHdrNwords() +
176 rawdblk->TotalBufNwords() +
177 // rawhdr.GetNwords() +
178 trl->GetTrlNwords();
179
180 hdr->SetNwords(total_send_nwords);
181 hdr->SetNumEventsinPacket(rawdblk->GetNumEvents());
182 hdr->SetNumNodesinPacket(rawdblk->GetNumNodes());
183
184 //
185 // For bug check
186 //
187 if (rawdblk->GetNumEntries() == 1) {
188 if (total_send_nwords != (rawdblk->GetBuffer(0))[ 0 ] + 8) {
189 char err_buf[500];
190 sprintf(err_buf, "Length error. total length %d rawdblk length %d. Exting...\n",
191 total_send_nwords, (rawdblk->GetBuffer(0))[ 0 ]);
192 printData(rawdblk->GetBuffer(0), rawdblk->TotalBufNwords());
193 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
194 sleep(1234567);
195 exit(-1);
196 }
197 }
198
199
200 for (int i = 0; i < rawdblk->GetNumEntries(); i++) {
201
202 //copy event # from a tonp COPPER block
203 if (!(rawdblk->CheckFTSWID(i)) && !(rawdblk->CheckTLUID(i))) {
204 tmp_header.SetBuffer(rawdblk->GetBuffer(i));
205 hdr->SetEventNumber(tmp_header.GetEveNo());
206 hdr->SetNodeID(tmp_header.GetNodeID());
207 hdr->SetExpRunWord(tmp_header.GetExpRunSubrun());
208 break;
209 }
210
211 //Error if you cannot find any COPPER block
212 if (i == (rawdblk->GetNumEntries() - 1)) {
213 printf("[DEBUG] i= %d : num entries %d : Tot words %d\n", i, rawdblk->GetNumEntries(), rawdblk->TotalBufNwords());
214 printData(rawdblk->GetBuffer(0), rawdblk->TotalBufNwords());
215
216 const char err_buf[500] = "[FATAL] CORRUPTED DATA: No COPPER blocks in RawDataBlock. Exiting...";
217 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
218 sleep(1234567);
219 exit(-1);
220 }
221 }
222 return;
223}
RawHeader_v2 tmp_header
which format is used
Definition DesSer.h:323
virtual int GetNumEntries()
get # of data blocks = (# of nodes)*(# of events)
void SetNumEventsinPacket(int num_events)
set contents of Header
Definition SendHeader.cc:61
static int GetHdrNwords()
get contents of Header
void SetNwords(int total_data_nwords)
initialize Header
Definition SendHeader.cc:51

◆ getNewBuffer()

int * getNewBuffer ( int nwords,
int * delete_flag )
inherited

Getbuffer.

Definition at line 77 of file DesSer.cc.

78{
79
80 int* temp_buf = NULL;
81 // Prepare buffer
82 if (nwords > BUF_SIZE_WORD) {
83 *delete_flag = 1;
84 temp_buf = new int[ nwords ];
85 } else {
86 if ((temp_buf = getPreAllocBuf()) == 0x0) {
87 char err_buf[500];
88 sprintf(err_buf, "Null pointer from GetPreALlocBuf(). Exting...\n");
89 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
90 sleep(1234567);
91 exit(1);
92 } else {
93 *delete_flag = 0;
94 }
95 }
96
97 return temp_buf;
98
99}
int * getPreAllocBuf()
Getbuffer.
Definition DesSer.cc:57

◆ getPreAllocBuf()

int * getPreAllocBuf ( )
inherited

Getbuffer.

Definition at line 57 of file DesSer.cc.

58{
59 int* tempbuf = 0;
60 if (m_num_usedbuf < NUM_PREALLOC_BUF) {
61 tempbuf = m_bufary[ m_num_usedbuf ];
63 } else {
64 char err_buf[500];
65 sprintf(err_buf,
66 "No pre-allocated buffers are left. %d > %d. Not enough buffers are allocated or "
67 "memory leak or forget to call ClearNumUsedBuf every event loop. Exting...",
68 m_num_usedbuf, NUM_PREALLOC_BUF);
69 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
70 sleep(1234567);
71 exit(1);
72 }
73 return tempbuf;
74}
int m_num_usedbuf
Definition DesSer.h:132
int * m_bufary[NUM_PREALLOC_BUF]
buffer
Definition DesSer.h:85

◆ getTimeSec()

double getTimeSec ( )
staticinherited

store time info.

Definition at line 480 of file DesSer.cc.

481{
482 struct timeval t;
483 gettimeofday(&t, NULL);
484 return (t.tv_sec + t.tv_usec * 1.e-6);
485}

◆ initialize()

void initialize ( bool close_listen = true)
inherited

Definition at line 101 of file DesSer.cc.

102{
103 printf("[DEBUG] DesSer: initialize() started.\n"); fflush(stdout);
104 signal(SIGPIPE, SIG_IGN);
105
106 //
107 // initialize Rx part from DeSerializer**.cc
108 //
109
110 // allocate buffer
111 for (int i = 0 ; i < NUM_PREALLOC_BUF; i++) {
112 m_bufary[i] = new int[ BUF_SIZE_WORD ];
113 }
114 m_buffer = new int[ BUF_SIZE_WORD ];
115
116 // initialize buffer
117 for (int i = 0 ; i < NUM_PREALLOC_BUF; i++) {
118 memset(m_bufary[i], 0, BUF_SIZE_WORD * sizeof(int));
119 }
120
121 // Open message handler
122 clearNumUsedBuf();
123 // Shared memory
124 if (m_shmflag > 0) {
125 if (m_nodename.size() == 0 || m_nodeid < 0) {
126 m_shmflag = 0;
127 } else {
129 m_status.reportReady();
130 }
131 }
132
133 event_diff = 0;
134
135 m_prev_copper_ctr = 0xFFFFFFFF;
136 m_prev_evenum = 0xFFFFFFFF;
137
138
139 //
140 // initialize Rx part from DeSerializer**.cc
141 //
142 m_start_flag = 0;
143 n_basf2evt = -1;
145
146#ifdef DUMMY
147 m_buffer = new int[ BUF_SIZE_WORD ];
148#endif
149 Accept(close_listen);
150#ifdef NONSTOP
151 openRunPauseNshm();
152#endif
153
154
155 // For monitor
156 if (m_status.isAvailable()) {
157 m_status.setOutputNBytes(0);
158 m_status.setOutputCount(0);
159 }
160 // B2INFO("DesSer: initialize() was done.");
161 printf("[DEBUG] DesSer: initialize() was done.\n"); fflush(stdout);
162
163}
int * m_buffer
buffer
Definition DesSer.h:82
int m_compressionLevel
Compression Level.
Definition DesSer.h:67
int m_nodeid
Node ID for SLC.
Definition DesSer.h:138

◆ printData()

void printData ( const int * buf,
int nwords )
staticinherited

dump error data

Definition at line 507 of file DesSer.cc.

508{
509 printf("[DEBUG]");
510 for (int i = 0; i < nwords; i++) {
511 printf("%.8x ", buf[ i ]);
512 if (i % 10 == 9) printf("\n[DEBUG]");
513 }
514 printf("\n[DEBUG]");
515 printf("\n");
516 return;
517}

◆ recordTime()

void recordTime ( int event,
double * array )
inherited

store time info.

Definition at line 488 of file DesSer.cc.

489{
490 if (event >= 50000 && event < 50500) {
491 array[ event - 50000 ] = getTimeSec() - m_start_time;
492 }
493 return;
494}

◆ recvData()

int * recvData ( int * delete_flag,
int * total_m_size_word,
int * num_events_in_sendblock,
int * num_nodes_in_sendblock )
protected

receive data

Definition at line 193 of file DesSerPrePC.cc.

194{
195 int* temp_buf = NULL; // buffer for data-body
196 int flag = 0;
197
198 vector <int> each_buf_nwords;
199 each_buf_nwords.clear();
200 vector <int> each_buf_nodes;
201 each_buf_nodes.clear();
202 vector <int> each_buf_events;
203 each_buf_events.clear();
204
205 *total_buf_nwords = 0;
206 *num_nodes_in_sendblock = 0;
207 *num_events_in_sendblock = 0;
208
209 //
210 // Read Header and obtain data size
211 //
212 int send_hdr_buf[ SendHeader::SENDHDR_NWORDS ];
213
214 // Read header
215 for (int i = 0; i < (int)(m_socket_recv.size()); i++) {
216
217 recvFD(m_socket_recv[ i ], reinterpret_cast<char*>(send_hdr_buf), sizeof(int)*SendHeader::SENDHDR_NWORDS, flag);
218
219 SendHeader send_hdr;
220 send_hdr.SetBuffer(send_hdr_buf);
221
222 int temp_num_events = send_hdr.GetNumEventsinPacket();
223 int temp_num_nodes = send_hdr.GetNumNodesinPacket();
224
225 if (i == 0) {
226 *num_events_in_sendblock = temp_num_events;
227 } else if (*num_events_in_sendblock != temp_num_events) {
228#ifndef NO_DATA_CHECK
229 char err_buf[500];
230 sprintf(err_buf,
231 "[FATAL] CORRUPTED DATA: Different # of events or nodes in SendBlocks( # of eve : %d(socket 0) %d(socket %d), # of nodes: %d(socket 0) %d(socket %d). Exiting...\n",
232 *num_events_in_sendblock, temp_num_events, i, *num_nodes_in_sendblock, temp_num_nodes, i);
233 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
234 sleep(1234567);
235 exit(1);
236#endif
237 }
238
239 *num_nodes_in_sendblock += temp_num_nodes;
240
241 int rawblk_nwords = send_hdr.GetTotalNwords()
242 - SendHeader::SENDHDR_NWORDS
243 - SendTrailer::SENDTRL_NWORDS;
244 *total_buf_nwords += rawblk_nwords;
245
246 //
247 // Data size check1
248 //
249 if (rawblk_nwords > (int)(2.5e6) || rawblk_nwords <= 0) {
250 printData(send_hdr_buf, SendHeader::SENDHDR_NWORDS);
251 char err_buf[500];
252 sprintf(err_buf, "CORRUPTED DATA: Too large event : Header %d %d %d %d :block size %d words\n", i, temp_num_events, temp_num_nodes,
253 send_hdr.GetTotalNwords(), rawblk_nwords);
254 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
255 sleep(123456);
256 exit(1);
257
258 }
259
260 each_buf_nwords.push_back(rawblk_nwords);
261 each_buf_events.push_back(temp_num_events);
262 each_buf_nodes.push_back(temp_num_nodes);
263
264 }
265
266
267 temp_buf = getNewBuffer(*total_buf_nwords, delete_flag); // this include only data body
268 //
269 // Read body
270 //
271 int total_recvd_byte = 0;
272 for (int i = 0; i < (int)(m_socket_recv.size()); i++) {
273
274 try {
275 total_recvd_byte += recvFD(m_socket_recv[ i ], reinterpret_cast<char*>(temp_buf) + total_recvd_byte,
276 each_buf_nwords[ i ] * sizeof(int), flag);
277 } catch (const string& err_str) {
278 if (*delete_flag) {
279 // B2WARNING("Delete buffer before going to Run-pause state");
280 printf("[WARNING] Delete buffer before going to Run-pause state\n"); fflush(stdout);
281 delete temp_buf;
282 }
283 throw (std::move(err_str));
284 }
285 //
286 // Data length check
287 //
288 int temp_length = 0;
289 for (int j = 0; j < each_buf_nodes[ i ] * each_buf_events[ i ]; j++) {
290 int this_length = *(reinterpret_cast<int*>((reinterpret_cast<char*>(temp_buf) + total_recvd_byte - each_buf_nwords[ i ] * sizeof(
291 int) + temp_length)));
292 temp_length += this_length * sizeof(int);
293 }
294 if (temp_length != (int)(each_buf_nwords[ i ] * sizeof(int))) {
295 printf("[DEBUG]*******SENDHDR*********** \n");
296 printData(send_hdr_buf, SendHeader::SENDHDR_NWORDS);
297 printf("[DEBUG]*******BODY***********\n ");
298 printData(temp_buf, (int)(total_recvd_byte / sizeof(int)));
299 char err_buf[500];
300 sprintf(err_buf, "CORRUPTED DATA: Length written on SendHeader(%d) is invalid. Actual data size is %d. Exting...",
301 (int)(*total_buf_nwords * sizeof(int)), temp_length);
302 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
303 sleep(1234567);
304 exit(-1);
305 }
306
307 }
308
309 if ((int)(*total_buf_nwords * sizeof(int)) != total_recvd_byte) {
310 char err_buf[500];
311 sprintf(err_buf, "CORRUPTED DATA: Received data size (%d byte) is not same as expected one (%d) from Sendheader. Exting...",
312 total_recvd_byte, (int)(*total_buf_nwords * sizeof(int)));
313 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
314 sleep(1234567);
315 exit(-1);
316 }
317
318 // Read Traeiler
319 int send_trl_buf[(unsigned int)(SendTrailer::SENDTRL_NWORDS) ];
320 for (int i = 0; i < (int)(m_socket_recv.size()); i++) {
321 try {
322 recvFD(m_socket_recv[ i ], reinterpret_cast<char*>(send_trl_buf), SendTrailer::SENDTRL_NWORDS * sizeof(int), flag);
323 } catch (const string& err_str) {
324 if (*delete_flag) {
325 // B2WARNING("Delete buffer before going to Run-pause state");
326 printf("[WARNING] Delete buffer before going to Run-pause state\n"); fflush(stdout);
327 delete temp_buf;
328 }
329 throw (std::move(err_str));
330 }
331 }
332
333 return temp_buf;
334}
int recvFD(int fd, char *buf, int data_size_byte, int flag)
receive data
void SetBuffer(const int *hdr)
set buffer
Definition SendHeader.cc:37
int GetNumEventsinPacket()
get contents of Header

◆ recvFD()

int recvFD ( int fd,
char * buf,
int data_size_byte,
int flag )
protected

receive data

Definition at line 60 of file DesSerPrePC.cc.

61{
62 int n = 0;
63 while (1) {
64 int read_size = 0;
65 if ((read_size = recv(sock, static_cast<char*>(buf) + n, data_size_byte - n, flag)) < 0) {
66 if (errno == EINTR) {
67 continue;
68 } else if (errno == EAGAIN || errno == EWOULDBLOCK) {
69#ifdef NONSTOP
70 string err_str;
71 callCheckRunPause(err_str);
72#endif
73 continue;
74 } else {
75 perror("[WARNING]");
76 char err_buf[500];
77 sprintf(err_buf, "recv() returned error; ret = %d. : %s %s %d",
78 read_size, __FILE__, __PRETTY_FUNCTION__, __LINE__);
79#ifdef NONSTOP
80 m_run_error = 1;
81 // B2ERROR(err_buf);
82 printf("[WARNING] %s\n", err_buf); fflush(stdout);
83 string err_str = "RUN_ERROR";
84 printf("AIUEO********************\n"); fflush(stdout);
85 throw (err_str);
86#endif
87 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
88 exit(-1);
89 }
90 } else if (read_size == 0) {
91 // Connection is closed ( error )
92 char err_buf[500];
93 sprintf(err_buf, "[WARNING] Connection is closed by peer(%s). readsize = %d %d. : %s %s %d",
94 strerror(errno), read_size, errno, __FILE__, __PRETTY_FUNCTION__, __LINE__);
95#ifdef NONSTOP
96 m_run_error = 1;
97 // B2ERROR(err_buf);
98 printf("%s\n", err_buf); fflush(stdout);
99 string err_str = "RUN_ERROR";
100 throw (err_str);
101#else
102 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
103 exit(-1);
104#endif
105 } else {
106 n += read_size;
107 if (n == data_size_byte)break;
108 }
109 }
110 return n;
111}

◆ Send()

int Send ( int socket,
char * buf,
int size_bytes )
inherited

send buffer

Definition at line 329 of file DesSer.cc.

330{
331 int sent_bytes = 0;
332 while (true) {
333 int ret = 0;
334 if ((ret = send(socket,
335 buf + sent_bytes, size_bytes - sent_bytes, MSG_NOSIGNAL)) < 0) {
336 if (errno == EINTR) {
337 continue;
338 } else if (errno == EAGAIN || errno == EWOULDBLOCK) {
339#ifdef NONSTOP
340 string err_str;
341 callCheckRunPause(err_str);
342#endif
343 continue;
344 } else {
345 char err_buf[500];
346 sprintf(err_buf, "[WARNING] SEND ERROR.(%s)", strerror(errno));
347#ifdef NONSTOP
348 m_run_error = 1;
349 // B2ERROR(err_buf);
350 printf("%s\n", err_buf); fflush(stdout);
351 string err_str = "RUN_ERROR";
352 throw (err_str); // To exit this module, go to DeSerializer** and wait for run-resume.
353#else
354 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
355 exit(1);
356#endif
357 }
358 }
359 sent_bytes += ret;
360 if (sent_bytes == size_bytes) break;
361 }
362 return sent_bytes;
363}

◆ sendByWriteV()

int sendByWriteV ( RawDataBlockFormat * rawdblk)
inherited

Definition at line 227 of file DesSer.cc.

228{
229 SendHeader send_header;
230 SendTrailer send_trailer;
231 fillSendHeaderTrailer(&send_header, &send_trailer, rawdblk);
232
233 enum {
234 NUM_BUFFER = 3
235 };
236 struct iovec iov[ NUM_BUFFER ];
237
238 // check Body data size
239 int rawcopper_nwords = rawdblk->TotalBufNwords();
240
241 //Fill iov info.
242 iov[0].iov_base = reinterpret_cast<char*>(send_header.GetBuffer());
243 iov[0].iov_len = sizeof(int) * send_header.GetHdrNwords();
244
245 iov[1].iov_base = reinterpret_cast<char*>(rawdblk->GetWholeBuffer());
246 iov[1].iov_len = sizeof(int) * rawcopper_nwords;
247
248 iov[2].iov_base = reinterpret_cast<char*>(send_trailer.GetBuffer());
249 iov[2].iov_len = sizeof(int) * send_trailer.GetTrlNwords();
250
251 // Send Multiple buffers
252 int n = 0;
253 while (true) {
254 if ((n = writev(m_socket_send, iov, NUM_BUFFER)) < 0) {
255 if (errno == EINTR) {
256 continue;
257 } else if (errno == EAGAIN || errno == EWOULDBLOCK) {
258#ifdef NONSTOP
259 string err_str;
260 callCheckRunPause(err_str);
261#endif
262 continue;
263 } else {
264 char err_buf[500];
265 sprintf(err_buf, "[WARNING] WRITEV error.(%s) : sent %d bytes, header %lu bytes body %lu trailer %lu\n",
266 strerror(errno), n, iov[0].iov_len, iov[1].iov_len, iov[2].iov_len);
267#ifdef NONSTOP
268 m_run_error = 1;
269 // B2ERROR(err_buf);
270 printf("%s\n", err_buf); fflush(stdout);
271 string err_str = "RUN_ERROR";
272 throw (err_str); // To exit this module, go to DeSerializer** and wait for run-resume.
273#else
274 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
275 exit(1);
276#endif
277 }
278 }
279 break;
280 }
281
282#ifdef DEBUG
283 printf("[DEBUG] *******BODY**********\n");
284 printf("[DEBUG] \n%.8d : ", 0);
285 printData((int*)(iov[1].iov_base), iov[1].iov_len);
286#endif
287
288
289 int total_send_bytes = sizeof(int) * send_header.GetTotalNwords();
290
291
292 //
293 // Retry sending
294 //
295 if (n != total_send_bytes) {
296 // B2WARNING("Serializer: Sent byte(" << n << "bytes) is not same as the event size (" << total_send_bytes << "bytes). Retrying...");
297 printf("[WARNING] Serializer: Sent byte( %d bytes) is not same as the event size ( %d bytes). Retrying...\n", n,
298 total_send_bytes); // back to WARNING due to request from Konno-san on 2018/7/3
299 fflush(stdout);
300
301 double retry_start = getTimeSec();
302 // Send Header
303 if (n < (int)(iov[ 0 ].iov_len)) {
304 n += Send(m_socket_send, static_cast<char*>(iov[ 0 ].iov_base) + n, iov[ 0 ].iov_len - n);
305 }
306
307 if (n < (int)(iov[ 0 ].iov_len + iov[ 1 ].iov_len)) {
308 n += Send(m_socket_send, static_cast<char*>(iov[ 1 ].iov_base) + (n - iov[ 0 ].iov_len), iov[ 1 ].iov_len - (n - iov[ 0 ].iov_len));
309 }
310
311 if (n < (int)(iov[ 0 ].iov_len + iov[ 1 ].iov_len + iov[ 2 ].iov_len)) {
312 // cppcheck-suppress unreadVariable ; the tail send is best effort
313 n += Send(m_socket_send, static_cast<char*>(iov[ 2 ].iov_base) + (n - iov[ 0 ].iov_len - iov[ 1 ].iov_len),
314 iov[ 2 ].iov_len - (n - iov[ 0 ].iov_len - iov[ 1 ].iov_len));
315 }
316 double retry_end = getTimeSec();
317 // B2WARNING("Resending ends. It takes " << retry_end - retry_start << "(s)");
318 printf("[WARNING] Resending ends. It takes %lf (s)\n", retry_end - retry_start);
319 fflush(stdout); // back to WARNING due to request from Konno-san on 2018/7/3
320 }
321 // printf( "[DEBUG] n %d total %d\n", n, total_send_bytes);
322 // delete temp_buf;
323
324 return total_send_bytes;
325
326}
int Send(int socket, char *buf, int size_bytes)
send buffer
Definition DesSer.cc:329
int * GetBuffer(void)
Get Header contents.
Definition SendHeader.cc:32

◆ setRecvdBuffer()

void setRecvdBuffer ( RawDataBlockFormat * raw_datablk,
int * delete_flag )
protected

attach buffer to RawDataBlock

Definition at line 337 of file DesSerPrePC.cc.

338{
339 //
340 // Get data from socket
341 //
342 int total_buf_nwords = 0 ;
343 int num_events_in_sendblock = 0;
344 int num_nodes_in_sendblock = 0;
345
346 if (m_start_flag == 0) {
347 // B2INFO("DeSerializerPrePC: Reading the 1st packet from eb0...");
348 printf("DeSerializerPrePC: Reading the 1st packet from eb0...\n"); fflush(stdout);
349 }
350 int* temp_buf = recvData(delete_flag, &total_buf_nwords, &num_events_in_sendblock,
351 &num_nodes_in_sendblock);
352 if (m_start_flag == 0) {
353 // B2INFO("DeSerializerPrePC: Done. the size of the 1st packet " << total_buf_nwords << " words");
354 printf("DeSerializerPrePC: Done. the size of the 1st packet %d words\n", total_buf_nwords); fflush(stdout);
355 m_start_flag = 1;
356 }
357 m_recvd_totbytes += total_buf_nwords * sizeof(int);
358
359 temp_raw_datablk->SetBuffer(static_cast<int*>(temp_buf), total_buf_nwords, *delete_flag,
360 num_events_in_sendblock, num_nodes_in_sendblock);
361
362 //
363 // check even # and node # in one Sendblock
364 //
365 int num_entries = temp_raw_datablk->GetNumEntries();
366 if (num_entries != num_events_in_sendblock * num_nodes_in_sendblock) {
367 char err_buf[500];
368 sprintf(err_buf,
369 "[FATAL] CORRUPTED DATA: Inconsistent SendHeader value. # of nodes(%d) times # of events(%d) differs from # of entries(%d). Exiting...",
370 num_nodes_in_sendblock, num_events_in_sendblock, num_entries);
371 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
372 sleep(1234567);
373 exit(-1);
374 }
375 return;
376
377}
int * recvData(int *delete_flag, int *total_m_size_word, int *num_events_in_sendblock, int *num_nodes_in_sendblock)
receive data

◆ shmOpen()

void shmOpen ( char * path_cfg,
char * path_sta )
staticinherited

open shared memory

Definition at line 681 of file DesSer.cc.

683{
684 errno = 0;
685 /*m_shmfd_cfg = shm_open( "/cpr_config2", O_CREAT | O_EXCL | O_RDWR, 0666);
686 if (m_shmfd_cfg < 0) {
687 if (errno != EEXIST) {
688 perror("shm_open1");
689 exit(1);
690 }
691 m_shmfd_cfg = shm_open(path_cfg, O_RDWR, 0666);
692 if (m_shmfd_cfg < 0) {
693 printf( "[DEBUG] %s\n", path_cfg);
694 perror("[ERROR] shm_open2");
695 exit(1);
696 }
697 */
698 //}
699 /*
700 m_shmfd_sta = shm_open( "/cpr_status2", O_CREAT | O_EXCL | O_RDWR, 0666);
701 if (m_shmfd_sta < 0) {
702 if (errno != EEXIST) {
703 perror("shm_open1");
704 exit(1);
705 }
706 m_shmfd_sta = shm_open(path_sta , O_RDWR, 0666);
707 if (m_shmfd_sta < 0) {
708 printf( "[DEBUG] %s\n", path_sta);
709 perror("[ERROR] shm_open2");
710 exit(1);
711 }
712 //}
713 int size = 4 * sizeof(int);
714 ftruncate(m_shmfd_cfg, size);
715 ftruncate(m_shmfd_sta, size);
716 */
717}

Member Data Documentation

◆ cur_time

double cur_time = 0.0
inherited

for time monitoring

Definition at line 102 of file DesSer.h.

◆ event_diff

int event_diff = 0
protectedinherited

Definition at line 256 of file DesSer.h.

◆ m_bufary

int* m_bufary[NUM_PREALLOC_BUF] = {}
inherited

buffer

Definition at line 85 of file DesSer.h.

85{};

◆ m_buffer

int* m_buffer = nullptr
inherited

buffer

Definition at line 82 of file DesSer.h.

◆ m_cfg_buf

int* m_cfg_buf = nullptr
inherited

buffer for shared memory

Definition at line 221 of file DesSer.h.

◆ m_cfg_sta

int* m_cfg_sta = nullptr
inherited

buffer for shared memory

Definition at line 224 of file DesSer.h.

◆ m_compressionLevel

int m_compressionLevel = 0
inherited

Compression Level.

Definition at line 67 of file DesSer.h.

◆ m_data_type

int m_data_type = 0
inherited

data type

Definition at line 153 of file DesSer.h.

◆ m_dump_fname

std::string m_dump_fname
inherited

dump filename

Definition at line 76 of file DesSer.h.

◆ m_exp_no

int m_exp_no = 0
inherited

exp no.

Definition at line 150 of file DesSer.h.

◆ m_exprunsubrun_no

unsigned int m_exprunsubrun_no
inherited

run no.

Definition at line 144 of file DesSer.h.

◆ m_fp_dump

FILE* m_fp_dump = nullptr
inherited

dump file descripter

Definition at line 79 of file DesSer.h.

◆ m_hostname_from

std::vector<std::string> m_hostname_from
protectedinherited

Reciever basf2 Socket.

hostname of upstream Data Sources

Definition at line 243 of file DesSer.h.

◆ m_hostname_local

std::string m_hostname_local
protectedinherited

Destination Host.

Definition at line 275 of file DesSer.h.

◆ m_ncycle

int m_ncycle = 0
inherited

Definition at line 100 of file DesSer.h.

◆ m_nodeid

int m_nodeid = 0
inherited

Node ID for SLC.

Definition at line 138 of file DesSer.h.

◆ m_nodename

std::string m_nodename
inherited

Node Name for SLC.

Definition at line 135 of file DesSer.h.

◆ m_num_connections

int m_num_connections
protectedinherited

check data contents

of connections

Definition at line 237 of file DesSer.h.

◆ m_num_usedbuf

int m_num_usedbuf = 0
inherited

of already used buffers

Definition at line 132 of file DesSer.h.

◆ m_port_from

std::vector<int> m_port_from
protectedinherited

port # to connect data sources

Definition at line 246 of file DesSer.h.

◆ m_port_to

int m_port_to = 0
protectedinherited

Destination port.

Definition at line 278 of file DesSer.h.

◆ m_pre_rawcpr

PreRawCOPPERFormat_v2 m_pre_rawcpr
inherited

report status to SLC

Use ver.2 for the moment(ver.4 -> PCI40)

Definition at line 191 of file DesSer.h.

◆ m_prev_copper_ctr

unsigned int m_prev_copper_ctr = 0
protectedinherited

Definition at line 258 of file DesSer.h.

◆ m_prev_evenum

unsigned int m_prev_evenum = 0
protectedinherited

Definition at line 260 of file DesSer.h.

◆ m_prev_exprunsubrun_no

unsigned int m_prev_exprunsubrun_no
inherited

run no.

Definition at line 147 of file DesSer.h.

◆ m_prev_nevt

int m_prev_nevt = 0
inherited

No. of prev sent events.

Definition at line 73 of file DesSer.h.

◆ m_prev_time

double m_prev_time = 0.0
inherited

Definition at line 105 of file DesSer.h.

◆ m_recvd_prev_totbytes

double m_recvd_prev_totbytes = 0.0
inherited

Definition at line 97 of file DesSer.h.

◆ m_recvd_totbytes

double m_recvd_totbytes = 0.0
inherited

Definition at line 96 of file DesSer.h.

◆ m_run_error

int m_run_error = 0
inherited

flag to show that there is some errors with which DAQ cannot continue.

Definition at line 230 of file DesSer.h.

◆ m_run_pause

int m_run_pause = 0
inherited

flag to show that run-controller pauses a run

Definition at line 227 of file DesSer.h.

◆ m_sent_prev_totbytes

double m_sent_prev_totbytes = 0.0
inherited

Definition at line 99 of file DesSer.h.

◆ m_sent_totbytes

double m_sent_totbytes = 0.0
inherited

Definition at line 98 of file DesSer.h.

◆ m_shmfd_cfg

int m_shmfd_cfg = 0
inherited

file descripter for shm

Definition at line 168 of file DesSer.h.

◆ m_shmfd_sta

int m_shmfd_sta = 0
inherited

file descripter for shm

Definition at line 171 of file DesSer.h.

◆ m_shmflag

int m_shmflag = 0
inherited

Use shared memory.

Definition at line 159 of file DesSer.h.

◆ m_socket_recv

std::vector<int> m_socket_recv
protectedinherited

Definition at line 248 of file DesSer.h.

◆ m_socket_send

int m_socket_send = 0
protectedinherited

Reciever Socket.

Definition at line 267 of file DesSer.h.

◆ m_start_flag

int m_start_flag = 0
inherited

start flag

Definition at line 181 of file DesSer.h.

◆ m_start_time

double m_start_time = 0.0
inherited

Definition at line 104 of file DesSer.h.

◆ m_status

RunInfoBuffer m_status
inherited

Run info buffer.

Definition at line 141 of file DesSer.h.

◆ m_t0

timeval m_t0
inherited

Definition at line 95 of file DesSer.h.

◆ m_trunc_mask

int m_trunc_mask = 0
inherited

trunc mask

Definition at line 156 of file DesSer.h.

◆ max_nevt

int max_nevt = 0
inherited

of events in a run

Definition at line 61 of file DesSer.h.

◆ max_seconds

double max_seconds = 0.0
inherited

time to stop a run

Definition at line 64 of file DesSer.h.

◆ monitor_numeve

int monitor_numeve = 0
inherited

buffer for shared memory

buffer for shared memory

Definition at line 178 of file DesSer.h.

◆ n_basf2evt

int n_basf2evt = 0
inherited

No. of sent events.

Definition at line 70 of file DesSer.h.

◆ p_method

std::string p_method
protectedinherited

EvtSocket.

How to handle data

Definition at line 284 of file DesSer.h.

◆ p_method_val

int p_method_val = 0
protectedinherited

Definition at line 285 of file DesSer.h.

◆ prev_event

int prev_event = 0
inherited

Definition at line 106 of file DesSer.h.

◆ print_err

CprErrorMessage print_err
inherited

wrapper for B2LOG system

Definition at line 184 of file DesSer.h.

◆ tmp_header

RawHeader_v2 tmp_header
protectedinherited

which format is used

Definition at line 323 of file DesSer.h.


The documentation for this class was generated from the following files: