clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -O3 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name DesSer.cc -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/data/b2soft/buildbot/development/build -fcoverage-compilation-dir=/data/b2soft/buildbot/development/build -resource-dir /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/lib/clang/21 -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/c++ -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/c++/x86_64-redhat-linux -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/c++/backward -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/include -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/python3.12 -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/include/CLHEP -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/Geant4 -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/include/root -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/include/belle_legacy -I include/ -D _PACKAGE_="daq" -D G4UI_USE_TCSH -D RaveDllExport= -D HAS_SQLITE -D HAS_CALLGRIND -I include -I /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/libxml2 -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/bin/../lib64/gcc/x86_64-redhat-linux/15.2.0/../../../../include/c++ -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/bin/../lib64/gcc/x86_64-redhat-linux/15.2.0/../../../../include/c++/x86_64-redhat-linux -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/bin/../lib64/gcc/x86_64-redhat-linux/15.2.0/../../../../include/c++/backward -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/bin/../lib64/gcc/x86_64-redhat-linux/15.2.0/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-missing-braces -Wno-unused-command-line-argument -std=c++20 -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /scan_build/2026-05-31-004316-385593-1 -x c++ daq/rawdata/src/DesSer.cc
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | #include <daq/rawdata/DesSer.h> |
| 9 | |
| 10 | #include <netdb.h> |
| 11 | #include <netinet/tcp.h> |
| 12 | |
| 13 | #include <sys/mman.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <sys/uio.h> |
| 16 | #include <unistd.h> |
| 17 | |
| 18 | #include <csignal> |
| 19 | |
| 20 | #include <csignal> |
| 21 | |
| 22 | |
| 23 | using namespace std; |
| 24 | using namespace Belle2; |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | DesSer::DesSer() |
| 31 | { |
| 32 | |
| 33 | m_num_connections = 1; |
| 34 | |
| 35 | m_exprunsubrun_no = 0; |
| 36 | |
| 37 | m_prev_exprunsubrun_no = 0xFFFFFFFF; |
| 38 | |
| 39 | #ifdef NONSTOP |
| 40 | m_run_pause = 0; |
| 41 | |
| 42 | m_run_error = 0; |
| 43 | #endif |
| 44 | |
| 45 | |
| 46 | printf("[DEBUG] DesSer: Constructor done.\n"); fflush(stdout); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
| 51 | DesSer::~DesSer() |
| 52 | { |
| 53 | |
| 54 | } |
| 55 | |
| 56 | |
| 57 | int* DesSer::getPreAllocBuf() |
| 58 | { |
| 59 | int* tempbuf = 0; |
| 60 | if (m_num_usedbuf < NUM_PREALLOC_BUF) { |
| 61 | tempbuf = m_bufary[ m_num_usedbuf ]; |
| 62 | 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 | } |
| 75 | |
| 76 | |
| 77 | int* DesSer::getNewBuffer(int nwords, int* delete_flag) |
| 78 | { |
| 79 | |
| 80 | int* temp_buf = NULL; |
| 81 | |
| 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 | } |
| 100 | |
| 101 | void DesSer::initialize(bool close_listen) |
| 102 | { |
| 103 | printf("[DEBUG] DesSer: initialize() started.\n"); fflush(stdout); |
| 104 | signal(SIGPIPE, SIG_IGN); |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 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 | |
| 117 | for (int i = 0 ; i < NUM_PREALLOC_BUF; i++) { |
| 118 | memset(m_bufary[i], 0, BUF_SIZE_WORD * sizeof(int)); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | clearNumUsedBuf(); |
| 123 | |
| 124 | if (m_shmflag > 0) { |
| 125 | if (m_nodename.size() == 0 || m_nodeid < 0) { |
| 126 | m_shmflag = 0; |
| 127 | } else { |
| 128 | m_status.open(m_nodename, m_nodeid); |
| 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 | |
| 141 | |
| 142 | m_start_flag = 0; |
| 143 | n_basf2evt = -1; |
| 144 | m_compressionLevel = 0; |
| 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 | |
| 156 | if (m_status.isAvailable()) { |
| 157 | m_status.setOutputNBytes(0); |
| 158 | m_status.setOutputCount(0); |
| 159 | } |
| 160 | |
| 161 | printf("[DEBUG] DesSer: initialize() was done.\n"); fflush(stdout); |
| 162 | |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | void DesSer::fillSendHeaderTrailer(SendHeader* hdr, SendTrailer* trl, RawDataBlockFormat* rawdblk) |
| 172 | { |
| 173 | |
| 174 | int total_send_nwords = |
| 175 | hdr->GetHdrNwords() + |
| 176 | rawdblk->TotalBufNwords() + |
| 177 | |
| 178 | trl->GetTrlNwords(); |
| 179 | |
| 180 | hdr->SetNwords(total_send_nwords); |
| 181 | hdr->SetNumEventsinPacket(rawdblk->GetNumEvents()); |
| 182 | hdr->SetNumNodesinPacket(rawdblk->GetNumNodes()); |
| 183 | |
| 184 | |
| 185 | |
| 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 | |
| 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 | |
| 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 | 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 | } |
| 224 | |
| 225 | |
| 226 | |
| 227 | int DesSer::sendByWriteV(RawDataBlockFormat* rawdblk) |
| 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 | |
| 239 | int rawcopper_nwords = rawdblk->TotalBufNwords(); |
| 240 | |
| 241 | |
| 242 | iov[0].iov_base = (char*)send_header.GetBuffer(); |
| 243 | iov[0].iov_len = sizeof(int) * send_header.GetHdrNwords(); |
| 244 | |
| 245 | iov[1].iov_base = (char*)rawdblk->GetWholeBuffer(); |
| 246 | iov[1].iov_len = sizeof(int) * rawcopper_nwords; |
| 247 | |
| 248 | iov[2].iov_base = (char*)send_trailer.GetBuffer(); |
| 249 | iov[2].iov_len = sizeof(int) * send_trailer.GetTrlNwords(); |
| 250 | |
| 251 | |
| 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 | |
| 270 | printf("%s\n", err_buf); fflush(stdout); |
| 271 | string err_str = "RUN_ERROR"; |
| 272 | throw (err_str); |
| 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 | |
| 294 | |
| 295 | if (n != total_send_bytes) { |
| 296 | |
| 297 | printf("[WARNING] Serializer: Sent byte( %d bytes) is not same as the event size ( %d bytes). Retrying...\n", n, |
| 298 | total_send_bytes); |
| 299 | fflush(stdout); |
| 300 | |
| 301 | double retry_start = getTimeSec(); |
| 302 | |
| 303 | if (n < (int)(iov[ 0 ].iov_len)) { |
| 304 | n += Send(m_socket_send, (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, (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 | n += Send(m_socket_send, (char*)iov[ 2 ].iov_base + (n - iov[ 0 ].iov_len - iov[ 1 ].iov_len), |
| 313 | iov[ 2 ].iov_len - (n - iov[ 0 ].iov_len - iov[ 1 ].iov_len)); |
| 314 | } |
| 315 | double retry_end = getTimeSec(); |
| 316 | |
| 317 | printf("[WARNING] Resending ends. It takes %lf (s)\n", retry_end - retry_start); |
| 318 | fflush(stdout); |
| 319 | } |
| 320 | |
| 321 | |
| 322 | |
| 323 | return total_send_bytes; |
| 324 | |
| 325 | } |
| 326 | |
| 327 | |
| 328 | int DesSer::Send(int socket, char* buf, int size_bytes) |
| 329 | { |
| 330 | int sent_bytes = 0; |
| 331 | while (true) { |
| 332 | int ret = 0; |
| 333 | if ((ret = send(socket, |
| 334 | buf + sent_bytes, size_bytes - sent_bytes, MSG_NOSIGNAL)) < 0) { |
| 335 | if (errno == EINTR) { |
| 336 | continue; |
| 337 | } else if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 338 | #ifdef NONSTOP |
| 339 | string err_str; |
| 340 | callCheckRunPause(err_str); |
| 341 | #endif |
| 342 | continue; |
| 343 | } else { |
| 344 | char err_buf[500]; |
| 345 | sprintf(err_buf, "[WARNING] SEND ERROR.(%s)", strerror(errno)); |
| 346 | #ifdef NONSTOP |
| 347 | m_run_error = 1; |
| 348 | |
| 349 | printf("%s\n", err_buf); fflush(stdout); |
| 350 | string err_str = "RUN_ERROR"; |
| 351 | throw (err_str); |
| 352 | #else |
| 353 | print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); |
| 354 | exit(1); |
| 355 | #endif |
| 356 | } |
| 357 | } |
| 358 | sent_bytes += ret; |
| 359 | if (sent_bytes == size_bytes) break; |
| 360 | } |
| 361 | return sent_bytes; |
| 362 | } |
| 363 | |
| 364 | void DesSer::Accept(bool close_listen) |
| 365 | { |
| 366 | |
| 367 | |
| 368 | |
| 369 | struct hostent* host; |
| 370 | host = gethostbyname(m_hostname_local.c_str()); |
| 371 | if (host == NULL) { |
| 1 | Assuming 'host' is not equal to NULL | |
|
| |
| 372 | char temp_buf[500]; |
| 373 | sprintf(temp_buf, "[FATAL] hostname(%s) cannot be resolved(%s). Check /etc/hosts. Exiting...\n", |
| 374 | m_hostname_local.c_str(), strerror(errno)); |
| 375 | print_err.PrintError(temp_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); |
| 376 | exit(1); |
| 377 | } |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | int fd_listen; |
| 383 | struct sockaddr_in sock_listen; |
| 384 | sock_listen.sin_family = AF_INET; |
| 385 | sock_listen.sin_addr.s_addr = *(unsigned int*)host->h_addr_list[0]; |
| 386 | |
| 387 | socklen_t addrlen = sizeof(sock_listen); |
| 388 | sock_listen.sin_port = htons(m_port_to); |
| 389 | fd_listen = socket(PF_INET, SOCK_STREAM, 0); |
| 390 | |
| 391 | int flags = 1; |
| 392 | int ret = setsockopt(fd_listen, SOL_SOCKET, SO_REUSEADDR, &flags, (socklen_t)sizeof(flags)); |
| 393 | if (ret < 0) { |
| |
| 394 | perror("Failed to set REUSEADDR"); |
| 395 | } |
| 396 | |
| 397 | if (bind(fd_listen, (struct sockaddr*)&sock_listen, sizeof(struct sockaddr)) < 0) { |
| |
| 398 | printf("[FATAL] Failed to bind. Maybe other programs have already occupied this port(%d). Exiting...\n", |
| 399 | m_port_to); fflush(stdout); |
| 400 | |
| 401 | FILE* fp; |
| 402 | char buf[256]; |
| 403 | char cmdline[500]; |
| 404 | sprintf(cmdline, "/usr/sbin/ss -ap | grep %d", m_port_to); |
| 405 | if ((fp = popen(cmdline, "r")) == NULL) { |
| 5 | | Assuming that 'popen' fails | |
|
| |
| |
| 406 | printf("[WARNING] Failed to run %s\n", cmdline); |
| 407 | } |
| 408 | while (fgets(buf, 256, fp) != NULL) { |
| 8 | | Stream pointer might be NULL |
|
| 409 | printf("[ERROR] Failed to bind. output of ss(port %d) : %s\n", m_port_to, buf); fflush(stdout); |
| 410 | } |
| 411 | |
| 412 | fclose(fp); |
| 413 | char temp_char[500]; |
| 414 | sprintf(temp_char, "[FATAL] Failed to bind.(%s) Maybe other programs have already occupied this port(%d). Exiting...", |
| 415 | strerror(errno), m_port_to); |
| 416 | print_err.PrintError(temp_char, __FILE__, __PRETTY_FUNCTION__, __LINE__); |
| 417 | exit(1); |
| 418 | } |
| 419 | |
| 420 | int val1 = 0; |
| 421 | setsockopt(fd_listen, IPPROTO_TCP, TCP_NODELAY, &val1, (socklen_t)sizeof(val1)); |
| 422 | int backlog = 1; |
| 423 | if (listen(fd_listen, backlog) < 0) { |
| 424 | char err_buf[500]; |
| 425 | sprintf(err_buf, "Failed in listen(%s). Exting...", strerror(errno)); |
| 426 | print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); |
| 427 | exit(-1); |
| 428 | } |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | int fd_accept; |
| 434 | struct sockaddr_in sock_accept; |
| 435 | printf("[DEBUG] Accepting... : port %d server %s\n", m_port_to, m_hostname_local.c_str()); |
| 436 | fflush(stdout); |
| 437 | |
| 438 | if ((fd_accept = accept(fd_listen, (struct sockaddr*) & (sock_accept), &addrlen)) == 0) { |
| 439 | char err_buf[500]; |
| 440 | sprintf(err_buf, "[FATAL] Failed to accept(%s). Exiting...", strerror(errno)); |
| 441 | print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__); |
| 442 | exit(-1); |
| 443 | } else { |
| 444 | |
| 445 | printf("[DEBUG] Done.\n"); fflush(stdout); |
| 446 | |
| 447 | |
| 448 | struct timeval timeout; |
| 449 | timeout.tv_sec = 1; |
| 450 | timeout.tv_usec = 0; |
| 451 | ret = setsockopt(fd_accept, SOL_SOCKET, SO_SNDTIMEO, &timeout, (socklen_t)sizeof(timeout)); |
| 452 | if (ret < 0) { |
| 453 | char temp_char[100] = "[FATAL] Failed to set TIMEOUT. Exiting..."; |
| 454 | print_err.PrintError(temp_char, __FILE__, __PRETTY_FUNCTION__, __LINE__); |
| 455 | exit(-1); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | if (close_listen) { |
| 460 | close(fd_listen); |
| 461 | } |
| 462 | |
| 463 | |
| 464 | |
| 465 | m_socket_send = fd_accept; |
| 466 | |
| 467 | if (m_status.isAvailable()) { |
| 468 | m_status.setOutputPort(ntohs(sock_listen.sin_port)); |
| 469 | m_status.setOutputAddress(sock_listen.sin_addr.s_addr); |
| 470 | |
| 471 | printf("Accepted. port %d address %d\n", (int)ntohs(sock_listen.sin_port), (int)sock_listen.sin_addr.s_addr); fflush(stdout); |
| 472 | } |
| 473 | |
| 474 | return; |
| 475 | |
| 476 | } |
| 477 | |
| 478 | double DesSer::getTimeSec() |
| 479 | { |
| 480 | struct timeval t; |
| 481 | gettimeofday(&t, NULL); |
| 482 | return (t.tv_sec + t.tv_usec * 1.e-6); |
| 483 | } |
| 484 | |
| 485 | |
| 486 | void DesSer::recordTime(int event, double* array) |
| 487 | { |
| 488 | if (event >= 50000 && event < 50500) { |
| 489 | array[ event - 50000 ] = getTimeSec() - m_start_time; |
| 490 | } |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | |
| 495 | unsigned int DesSer::calcXORChecksum(int* buf, int nwords) |
| 496 | { |
| 497 | unsigned int checksum = 0; |
| 498 | for (int i = 0; i < nwords; i++) { |
| 499 | checksum = checksum ^ buf[ i ]; |
| 500 | } |
| 501 | return checksum; |
| 502 | } |
| 503 | |
| 504 | |
| 505 | void DesSer::printData(int* buf, int nwords) |
| 506 | { |
| 507 | printf("[DEBUG]"); |
| 508 | for (int i = 0; i < nwords; i++) { |
| 509 | printf("%.8x ", buf[ i ]); |
| 510 | if (i % 10 == 9) printf("\n[DEBUG]"); |
| 511 | } |
| 512 | printf("\n[DEBUG]"); |
| 513 | printf("\n"); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | |
| 518 | #ifdef NONSTOP |
| 519 | void DesSer::openRunPauseNshm() |
| 520 | { |
| 521 | char path_shm[100] = "/cpr_pause_resume"; |
| 522 | int fd = shm_open(path_shm, O_RDONLY, 0666); |
| 523 | if (fd < 0) { |
| 524 | printf("[DEBUG] %s\n", path_shm); |
| 525 | perror("[ERROR] shm_open2"); |
| 526 | exit(1); |
| 527 | } |
| 528 | m_ptr = (int*)mmap(NULL, sizeof(int), PROT_READ, MAP_SHARED, fd, 0); |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | int DesSer::checkRunPause() |
| 533 | { |
| 534 | #ifdef NONSTOP_SLC |
| 535 | if (m_status.getState() == m_status.PAUSING) { |
| 536 | #else |
| 537 | if (*m_ptr) { |
| 538 | #endif |
| 539 | return 1; |
| 540 | } else { |
| 541 | return 0; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | int DesSer::checkRunRecovery() |
| 546 | { |
| 547 | #ifdef NONSTOP_SLC |
| 548 | if (m_status.getState() == m_status.RESUMING) { |
| 549 | #else |
| 550 | if (*m_ptr) { |
| 551 | #endif |
| 552 | return 0; |
| 553 | } else { |
| 554 | return 1; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | void DesSer::resumeRun() |
| 559 | { |
| 560 | #ifdef NONSTOP_DEBUG |
| 561 | printf("\033[34m"); |
| 562 | printf("###########(Ser) the 1st event sicne the resume ###############\n"); |
| 563 | fflush(stdout); |
| 564 | printf("\033[0m"); |
| 565 | #endif |
| 566 | m_run_error = 0; |
| 567 | m_run_pause = 0; |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | |
| 572 | void DesSer::pauseRun() |
| 573 | { |
| 574 | m_run_pause = 1; |
| 575 | #ifdef NONSTOP_DEBUG |
| 576 | printf("###########(Ser) Pause the run ###############\n"); |
| 577 | fflush(stdout); |
| 578 | #endif |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | void DesSer::callCheckRunPause(const string& err_str) |
| 586 | { |
| 587 | #ifdef NONSTOP_DEBUG |
| 588 | printf("\033[34m"); |
| 589 | printf("###########(DesSer) TIMEOUT. ###############\n"); |
| 590 | fflush(stdout); |
| 591 | printf("\033[0m"); |
| 592 | #endif |
| 593 | if (checkRunPause()) { |
| 594 | #ifdef NONSTOP_DEBUG |
| 595 | printf("\033[31m"); |
| 596 | printf("###########(DesSer) Pause is detected. ###############\n"); |
| 597 | fflush(stdout); |
| 598 | printf("\033[0m"); |
| 599 | #endif |
| 600 | m_run_pause = 1; |
| 601 | throw (err_str); |
| 602 | } |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | |
| 607 | int DesSer::CheckConnection(int socket) |
| 608 | { |
| 609 | |
| 610 | |
| 611 | |
| 612 | int ret; |
| 613 | char buffer[100000]; |
| 614 | int eagain_cnt = 0; |
| 615 | int tot_ret = 0; |
| 616 | printf("CC1\n"); fflush(stdout); |
| 617 | ret = send(socket, buffer, 0, MSG_DONTWAIT); |
| 618 | printf("CC2\n"); fflush(stdout); |
| 619 | switch (ret) { |
| 620 | case 0: |
| 621 | break; |
| 622 | case -1: |
| 623 | if (errno == EAGAIN) { |
| 624 | printf("EAGAIN %d cnt %d recvd %d\n", socket, eagain_cnt, tot_ret); fflush(stdout); |
| 625 | |
| 626 | if (eagain_cnt > 100) { |
| 627 | return 0; |
| 628 | } |
| 629 | usleep(10000); |
| 630 | eagain_cnt++; |
| 631 | } else { |
| 632 | printf("ERROR %d errno %d err %s\n", socket, errno, strerror(errno)); fflush(stdout); |
| 633 | close(socket); |
| 634 | return -1; |
| 635 | } |
| 636 | default: |
| 637 | printf("Return value %d of send is strange. Exting...\n", ret); |
| 638 | fflush(stdout); |
| 639 | exit(1); |
| 640 | } |
| 641 | |
| 642 | while (true) { |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | ret = recv(socket, buffer, sizeof(buffer), MSG_DONTWAIT); |
| 649 | switch (ret) { |
| 650 | case 0: |
| 651 | printf("EOF %d\n", socket); fflush(stdout); |
| 652 | close(socket); |
| 653 | return -1; |
| 654 | case -1: |
| 655 | if (errno == EAGAIN) { |
| 656 | printf("EAGAIN %d cnt %d recvd %d\n", socket, eagain_cnt, tot_ret); fflush(stdout); |
| 657 | |
| 658 | if (eagain_cnt > 100) { |
| 659 | return 0; |
| 660 | } |
| 661 | usleep(10000); |
| 662 | eagain_cnt++; |
| 663 | } else { |
| 664 | printf("ERROR %d errno %d err %s\n", socket, errno, strerror(errno)); fflush(stdout); |
| 665 | close(socket); |
| 666 | return -1; |
| 667 | } |
| 668 | break; |
| 669 | default: |
| 670 | tot_ret += ret; |
| 671 | printf("Flushing data in socket buffer : sockid = %d %d bytes tot %d bytes\n", socket, ret, tot_ret); fflush(stdout); |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | |
| 677 | #endif |
| 678 | |
| 679 | void DesSer::shmOpen(char*, char*) |
| 680 | |
| 681 | { |
| 682 | errno = 0; |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | } |
| 716 | |
| 717 | |
| 718 | |