Changeset 15472
- Timestamp:
- 05/01/13 13:05:59 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/EventBuilder.c
r15471 r15472 1 2 // // // #define EVTDEBUG 1 // #define EVTDEBUG 3 2 4 3 #define NUMSOCK 1 //set to 7 for old configuration … … 70 69 int CloseRunFile (uint32_t runId, uint32_t closeTime, uint32_t maxEvt); 71 70 72 73 71 int evtCtrl_frstPtr; 74 72 int evtCtrl_lastPtr; 75 76 77 73 78 74 int g_maxProc; … … 102 98 int gw_runStat; 103 99 104 //int gi_memStat = +1;105 106 100 uint32_t actrun = 0; 107 101 108 109 102 uint gi_NumConnect[NBOARDS]; //4 crates * 10 boards 110 111 //uint gi_EvtStart= 0 ;112 //uint gi_EvtRead = 0 ;113 //uint gi_EvtBad = 0 ;114 //uint gi_EvtTot = 0 ;115 //size_t gi_usedMem = 0 ;116 117 //uint gw_EvtTot = 0 ;118 //uint gp_EvtTot = 0 ;119 120 //PIX_MAP g_pixMap[NPIX];121 103 122 104 EVT_STAT gi; … … 124 106 125 107 EVT_CTRL evtCtrl[MAX_EVT * MAX_RUN]; //control of events during processing 126 127 //#define MXSTR 1000128 //char str[MXSTR];129 108 130 109 void factPrintf(int severity, int id, const char *fmt, ...) … … 195 174 196 175 RUN_CTRL runCtrl[MAX_RUN]; 197 //RUN_TAIL runTail[MAX_RUN];198 199 176 200 177 /* … … 202 179 */ 203 180 204 205 181 typedef union 206 182 { 207 int8_t B[MAX_LEN];208 int16_t S[MAX_LEN / 2];209 int32_t I[MAX_LEN / 4];210 int64_t L[MAX_LEN / 8];183 uint8_t B[MAX_LEN]; 184 uint16_t S[MAX_LEN / 2]; 185 uint32_t I[MAX_LEN / 4]; 186 uint64_t L[MAX_LEN / 8]; 211 187 } CNV_FACT; 212 188 … … 216 192 int32_t bufPos; //next byte to read to the buffer next 217 193 int32_t bufLen; //number of bytes left to read 218 // size_t bufLen; //number of bytes left to read size_t might be better219 194 int32_t skip; //number of bytes skipped before start of event 220 195 … … 222 197 int sockStat; //-1 if socket not yet connected , 99 if not exist 223 198 int socket; //contains the sockets 199 224 200 struct sockaddr_in SockAddr; //IP for each socket 225 201 … … 230 206 int fadVers; // Version of FAD 231 207 int ftmTyp; // trigger type 232 int board; // boardID (softwareID: 0..40 )233 208 int Port; 234 209 … … 236 211 237 212 } READ_STRUCT; 238 239 240 typedef union241 {242 int8_t B[2];243 int16_t S;244 } SHORT_BYTE;245 246 247 248 249 250 SHORT_BYTE start, stop;251 252 READ_STRUCT rd[MAX_SOCK]; //buffer to read IP and afterwards store in mBuffer253 254 255 213 256 214 /*-----------------------------------------------------------------*/ … … 288 246 */ 289 247 290 int j; 291 int optval = 1; //activate keepalive 292 socklen_t optlen = sizeof (optval); 293 294 295 if (sid % 7 >= NUMSOCK) { 296 //this is a not used socket, so do nothing ... 297 rs->sockStat = 77; 298 rs->rBuf = NULL ; 299 return 0; 300 } 301 302 if (rs->sockStat == 0) { //close socket if open 303 j = close (rs->socket); 304 if (j > 0) { 248 //close socket if open 249 if (rs->sockStat == 0) 250 { 251 if (close (rs->socket) > 0) { 305 252 factPrintf(kFatal, 771, "Closing socket %d failed: %m (close,rc=%d)", sid, errno); 306 253 } else { … … 338 285 return -2; 339 286 } 340 optval = 1; 341 if (setsockopt (rs->socket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) { 287 288 int optval = 1; 289 if (setsockopt (rs->socket, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(int)) < 0) { 342 290 factPrintf(kInfo, 173, "Setting SO_KEEPALIVE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno); 343 291 } 344 292 optval = 10; //start after 10 seconds 345 if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) {293 if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPIDLE, &optval, sizeof(int)) < 0) { 346 294 factPrintf(kInfo, 173, "Setting TCP_KEEPIDLE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno); 347 295 } 348 296 optval = 10; //do every 10 seconds 349 if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) {297 if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPINTVL, &optval, sizeof(int)) < 0) { 350 298 factPrintf(kInfo, 173, "Setting TCP_KEEPINTVL for socket %d failed: %m (setsockopt,rc=%d)", sid, errno); 351 299 } 352 300 optval = 2; //close after 2 unsuccessful tries 353 if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) {301 if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPCNT, &optval, sizeof(int)) < 0) { 354 302 factPrintf(kInfo, 173, "Setting TCP_KEEPCNT for socket %d failed: %m (setsockopt,rc=%d)", sid, errno); 355 303 } … … 365 313 /*-----------------------------------------------------------------*/ 366 314 367 368 369 370 int 371 mBufInit () 372 { 373 // initialize mBuffer (mark all entries as unused\empty) 374 375 //uint32_t actime = g_actTime + 50000000; 376 377 for (int i = 0; i < MAX_EVT * MAX_RUN; i++) { 378 evtCtrl[i].evNum = evtCtrl[i].nRoi = -1; 379 evtCtrl[i].runNum = 0; 380 381 //evtCtrl[i].mBuffer_idx = -1; 382 evtCtrl[i].evtStat = -1; 383 } 384 385 //actRun.FADhead = malloc (NBOARDS * sizeof (PEVNT_HEADER)); 386 387 evtCtrl_frstPtr = 0; 388 evtCtrl_lastPtr = 0; 389 390 return 0; 391 392 } /*-----------------------------------------------------------------*/ 393 394 int checkRoiConsistency(int i, int roi[]); 395 396 int mBufEvt(int sk) 315 int checkRoiConsistency(const CNV_FACT *rbuf, int roi[]) 316 { 317 int xjr = -1; 318 int xkr = -1; 319 320 //points to the very first roi 321 int roiPtr = sizeof(PEVNT_HEADER)/2 + 2; 322 323 roi[0] = ntohs(rbuf->S[roiPtr]); 324 325 for (int jr = 0; jr < 9; jr++) 326 { 327 roi[jr] = ntohs(rbuf->S[roiPtr]); 328 329 if (roi[jr]<0 || roi[jr]>1024) 330 { 331 factPrintf(kError, 999, "Illegal roi in channel %d (allowed: 0<=roi<=1024)", jr, roi[jr]); 332 return 0; 333 } 334 335 // Check that the roi of pixels jr are compatible with the one of pixel 0 336 if (jr!=8 && roi[jr]!=roi[0]) 337 { 338 xjr = jr; 339 break; 340 } 341 342 // Check that the roi of all other DRS chips on boards are compatible 343 for (int kr = 1; kr < 4; kr++) 344 { 345 const int kroi = ntohs(rbuf->S[roiPtr]); 346 if (kroi != roi[jr]) 347 { 348 xjr = jr; 349 xkr = kr; 350 break; 351 } 352 roiPtr += kroi+4; 353 } 354 } 355 356 if (xjr>=0) 357 { 358 if (xkr<0) 359 factPrintf(kFatal, 1, "Inconsistent Roi accross chips [DRS=%d], expected %d, got %d", xjr, roi[0], roi[xjr]); 360 else 361 factPrintf(kFatal, 1, "Inconsistent Roi accross channels [DRS=%d Ch=%d], expected %d, got %d", xjr, xkr, roi[xjr], ntohs(rbuf->S[roiPtr])); 362 363 return 0; 364 } 365 366 if (roi[8] < roi[0]) 367 { 368 factPrintf(kError, 712, "Mismatch of roi (%d) in channel 8. Should be larger or equal than the roi (%d) in channel 0.", roi[8], roi[0]); 369 //gj.badRoiB++; 370 //gj.badRoi[b]++; 371 return 0; 372 } 373 374 return 1; 375 } 376 377 int mBufEvt(const READ_STRUCT *rs) 397 378 { 398 379 // generate a new Event into mBuffer: … … 404 385 // < 0 if no space left 405 386 406 // int evFree;407 // int headmem = 0;408 // size_t needmem = 0;409 410 387 int nRoi[9]; 411 if (!checkRoiConsistency( sk, nRoi))388 if (!checkRoiConsistency(rs->rBuf, nRoi)) 412 389 return -9999; 413 390 414 //const int b = sk / 7; 415 416 const int evID = rd[sk].evtID; 417 const uint runID = rd[sk].runID; 418 const int trgTyp = rd[sk].ftmTyp; 419 const int trgNum = rd[sk].ftmID; 420 const int fadNum = rd[sk].evtID; 421 422 int beg = (evtCtrl_lastPtr + MAX_EVT*MAX_RUN - 1) % (MAX_EVT*MAX_RUN); 423 int end = (evtCtrl_frstPtr + MAX_EVT*MAX_RUN - 1) % (MAX_EVT*MAX_RUN); 424 425 for (int k=beg; k!=end; k--, k += MAX_EVT*MAX_RUN, k %= MAX_EVT*MAX_RUN) 391 const int evID = rs->evtID; 392 const uint runID = rs->runID; 393 const int trgTyp = rs->ftmTyp; 394 const int trgNum = rs->ftmID; 395 const int fadNum = rs->evtID; 396 397 const int beg = (evtCtrl_lastPtr + MAX_EVT*MAX_RUN - 1) % (MAX_EVT*MAX_RUN); 398 const int end = (evtCtrl_frstPtr + MAX_EVT*MAX_RUN - 1) % (MAX_EVT*MAX_RUN); 399 400 for (int k=beg; k!=end; k=(k+MAX_EVT*MAX_RUN-1)%(MAX_EVT*MAX_RUN)) 426 401 { 427 402 // If the run is different, go on searching. … … 432 407 continue; 433 408 434 // The event in the control structure has an event id with 435 // a lower event id than the current one. All previous events 436 // should have an even lower event id, so there is no way it 437 // can be found in the structure. 438 if (evtCtrl[k].evNum < evID/* && runID == evtCtrl[k].runNum*/) 409 // If the ID of the new event if higher than the last one stored 410 // in that run, we have to assign a new slot (leave the loop) 411 if (evID > evtCtrl[k].evNum/* && runID == evtCtrl[k].runNum*/) 439 412 break; 440 413 … … 442 415 continue; 443 416 444 // is it ok ???? 417 // We have found an entry with the same runID and evtID 418 // Check if ROI is consistent 445 419 if (evtCtrl[k].nRoi != nRoi[0] || evtCtrl[k].nRoiTM != nRoi[8]) 446 420 { … … 462 436 } 463 437 464 //event does not yet exist; create it 465 466 if (end-beg==1 || (end==0 && beg==MAX_EVT*MAX_RUN-1)) //no space available in ctrl 438 // Check if the control structure still has space left 439 if (end-beg==1 || (end==0 && beg==MAX_EVT*MAX_RUN-1)) 467 440 { 468 441 factPrintf(kError, 881, "No control slot to keep event %d (run %d) %d %d", evID, runID, beg, end); … … 522 495 } 523 496 524 int evFree = jold;497 const int evFree = jold; 525 498 526 499 factPrintf(kInfo, 503, "New run %d (evID=%d, evFree=%d) registered with roi=%d and roi_tm=%d", … … 540 513 } 541 514 515 // The new entry which is populated is the one lastPtr is pointing to 542 516 const int k = evtCtrl_lastPtr; 543 517 … … 569 543 // in two instructions. Must be done only _after_ the contents 570 544 // have been initialized 571 evtCtrl_lastPtr = (evtCtrl_lastPtr+1) % MAX_EVT * MAX_RUN;545 evtCtrl_lastPtr = (evtCtrl_lastPtr+1) % (MAX_EVT*MAX_RUN); 572 546 573 547 return k; … … 706 680 } 707 681 708 int checkRoiConsistency(int i, int roi[]) 709 { 710 int xjr = -1; 711 int xkr = -1; 712 713 //points to the very first roi 714 int roiPtr = sizeof(PEVNT_HEADER)/2 + 2; 715 716 roi[0] = ntohs(rd[i].rBuf->S[roiPtr]); 717 718 for (int jr = 0; jr < 9; jr++) 682 // i == board 683 void copyData(CNV_FACT *rbuf, int i, int evID) 684 { 685 // swapEventHeaderBytes: End of the header. to channels now 686 int eStart = 36; 687 for (int ePatchesCount = 0; ePatchesCount<4*9; ePatchesCount++) 719 688 { 720 roi[jr] = ntohs(rd[i].rBuf->S[roiPtr]); 721 722 if (roi[jr]<0 || roi[jr]>1024) 723 { 724 factPrintf(kError, 999, "Illegal roi in channel %d (allowed: 0<=roi<=1024)", jr, roi[jr]); 725 return 0; 726 } 727 728 // Check that the roi of pixels jr are compatible with the one of pixel 0 729 if (jr!=8 && roi[jr]!=roi[0]) 730 { 731 xjr = jr; 732 break; 733 } 734 735 // Check that the roi of all other DRS chips on boards are compatible 736 for (int kr = 1; kr < 4; kr++) 737 { 738 const int kroi = ntohs(rd[i].rBuf->S[roiPtr]); 739 if (kroi != roi[jr]) 740 { 741 xjr = jr; 742 xkr = kr; 743 break; 744 } 745 roiPtr += kroi+4; 746 } 689 rbuf->S[eStart+0] = ntohs(rbuf->S[eStart+0]);//id 690 rbuf->S[eStart+1] = ntohs(rbuf->S[eStart+1]);//start_cell 691 rbuf->S[eStart+2] = ntohs(rbuf->S[eStart+2]);//roi 692 rbuf->S[eStart+3] = ntohs(rbuf->S[eStart+3]);//filling 693 694 eStart += 4+rbuf->S[eStart+2];//skip the pixel data 747 695 } 748 696 749 if (xjr>=0) 750 { 751 if (xkr<0) 752 factPrintf(kFatal, 1, "Inconsistent Roi accross chips [DRS=%d], expected %d, got %d", xjr, roi[0], roi[xjr]); 753 else 754 factPrintf(kFatal, 1, "Inconsistent Roi accross channels [DRS=%d Ch=%d], expected %d, got %d", xjr, xkr, roi[xjr], ntohs(rd[i].rBuf->S[roiPtr])); 755 756 return 0; 757 } 758 759 //const int b = i / 7; 760 761 /* 762 if (roi[0]<0 || roi[0] > 1024) 763 { 764 factPrintf(kError, 999, "Illegal roi in channel 0: %d (allowed: 0<=roi<=1024)", roi[0]); 765 gj.badRoiR++; 766 gj.badRoi[b]++; 767 return 0; 768 } 769 */ 770 /* 771 for (int jr = 1; jr < 8; jr++) 772 { 773 if (roi[jr] != roi[0]) 774 { 775 factPrintf(kError, 711, "Mismatch of roi (%d) in channel %d with roi (%d) in channel 0.", roi[jr], jr, roi[0]); 776 gj.badRoiB++; 777 gj.badRoi[b]++; 778 return 0; 779 } 780 } 781 */ 782 if (roi[8] < roi[0]) 783 { 784 factPrintf(kError, 712, "Mismatch of roi (%d) in channel 8. Should be larger or equal than the roi (%d) in channel 0.", roi[8], roi[0]); 785 //gj.badRoiB++; 786 //gj.badRoi[b]++; 787 return 0; 788 } 789 790 return 1; 791 } 792 793 void swapEventHeaderBytes(int i) 794 { 795 //End of the header. to channels now 796 int eStart = 36; 797 for (int ePatchesCount = 0; ePatchesCount<4*9;ePatchesCount++) 798 { 799 rd[i].rBuf->S[eStart+0] = ntohs(rd[i].rBuf->S[eStart+0]);//id 800 rd[i].rBuf->S[eStart+1] = ntohs(rd[i].rBuf->S[eStart+1]);//start_cell 801 rd[i].rBuf->S[eStart+2] = ntohs(rd[i].rBuf->S[eStart+2]);//roi 802 rd[i].rBuf->S[eStart+3] = ntohs(rd[i].rBuf->S[eStart+3]);//filling 803 804 eStart += 4+rd[i].rBuf->S[eStart+2];//skip the pixel data 805 } 806 } 807 808 void copyData(int i, int evID, /*int roi,*/ int boardId) 809 { 810 swapEventHeaderBytes(i); 811 812 memcpy(&evtCtrl[evID].FADhead[boardId].start_package_flag, 813 &rd[i].rBuf->S[0], sizeof(PEVNT_HEADER)); 697 memcpy(&evtCtrl[evID].FADhead[i], rbuf, sizeof(PEVNT_HEADER)); 814 698 815 699 int src = sizeof(PEVNT_HEADER) / 2; 816 700 817 701 // consistency of ROIs have been checked already (is it all correct?) 818 const int roi = r d[i].rBuf->S[src+2];702 const int roi = rbuf->S[src+2]; 819 703 820 704 // different sort in FAD board..... … … 826 710 src++; 827 711 828 const int pixC = r d[i].rBuf->S[src++]; // start-cell829 const int pixR = r d[i].rBuf->S[src++]; // roi712 const int pixC = rbuf->S[src++]; // start-cell 713 const int pixR = rbuf->S[src++]; // roi 830 714 //here we should check if pixH is correct .... 831 715 832 const int pixS = boardId* 36 + drs * 9 + px;716 const int pixS = i * 36 + drs * 9 + px; 833 717 src++; 834 718 … … 836 720 837 721 const int dest1 = pixS * roi; 838 memcpy(&evtCtrl[evID].fEvent->Adc_Data[dest1], 839 &rd[i].rBuf->S[src], roi * 2); 722 memcpy(&evtCtrl[evID].fEvent->Adc_Data[dest1], &rbuf->S[src], roi * 2); 840 723 841 724 src += pixR; … … 843 726 if (px == 8) 844 727 { 845 const int tmS = boardId* 4 + drs;728 const int tmS = i * 4 + drs; 846 729 847 730 //and we have additional TM info … … 853 736 evtCtrl[evID].fEvent->StartTM[tmS] = (pixC + pixR - roi) % 1024; 854 737 855 memcpy(&evtCtrl[evID].fEvent->Adc_Data[dest2], 856 &rd[i].rBuf->S[srcT], roi * 2); 738 memcpy(&evtCtrl[evID].fEvent->Adc_Data[dest2], &rbuf->S[srcT], roi * 2); 857 739 } 858 740 else 859 741 { 860 742 evtCtrl[evID].fEvent->StartTM[tmS] = -1; 861 862 //ETIENNE because the TM channels are always processed during drs calib,863 //set them to zero if they are not present864 //I suspect that it may be more efficient to set all the allocated mem to865 //zero when allocating it866 // dest = tmS*roi[0] + NPIX*roi[0];867 // bzero(&mBuffer[evID].fEvent->Adc_Data[dest],roi[0]*2);868 743 } 869 744 } … … 873 748 874 749 875 void876 initReadFAD ()877 {878 return;879 } /*-----------------------------------------------------------------*/880 881 //struct rnd882 //{883 // int val;884 // int idx;885 //};886 //887 //struct rnd random_arr[MAX_SOCK];888 //889 //int compare(const void *f1, const void *f2)890 //{891 // struct rnd *r1 = (struct rnd*)f1;892 // struct rnd *r2 = (struct rnd*)f2;893 // return r1->val - r2->val;894 //}895 896 897 750 void *readFAD (void *ptr) 898 751 { … … 901 754 factPrintf(kInfo, -1, "Start initializing (readFAD)"); 902 755 903 // int cpu = 7; //read thread 904 // cpu_set_t mask; 905 906 /* CPU_ZERO initializes all the bits in the mask to zero. */ 907 // CPU_ZERO (&mask); 908 /* CPU_SET sets only the bit corresponding to cpu. */ 909 // cpu = 7; 910 // CPU_SET (cpu, &mask); 911 912 /* sched_setaffinity returns 0 in success */ 913 // if (sched_setaffinity (0, sizeof (mask), &mask) == -1) { 914 // snprintf (str, MXSTR, "W ---> can not create affinity to %d", cpu); 915 // factOut (kWarn, -1, str); 916 // } 917 756 READ_STRUCT rd[NBOARDS]; //buffer to read IP and afterwards store in mBuffer 918 757 919 758 const int minLen = sizeof(PEVNT_HEADER); //min #bytes needed to check header: full header for debug 920 759 921 start.S = 0xFB01;922 stop.S = 0x04FE;760 //start.S = 0xFB01; 761 //stop.S = 0x04FE; 923 762 924 763 /* initialize run control logics */ … … 944 783 if (gi_resetS > 0) { 945 784 //make sure all sockets are preallocated as 'not exist' 946 for (int i = 0; i < MAX_SOCK; i++) {785 for (int i = 0; i < NBOARDS; i++) { 947 786 rd[i].socket = -1; 948 787 rd[i].sockStat = 99; … … 960 799 } 961 800 962 963 if (gi_resetR > 0) { 964 //resetEvtStat (); 965 gj.bufTot = gj.maxEvt = gj.xxxEvt = 0; 966 gj.usdMem = gj.maxMem = gj.xxxMem = 0; 967 gj.totMem = tgb_memory; 968 gj.bufNew = gj.bufEvt = 0; 969 //gj.badRoiE = gj.badRoiR = gj.badRoiB = 0; 801 if (gi_resetR > 0) 802 { 803 gj.bufTot = gj.maxEvt = gj.xxxEvt = 0; 804 gj.usdMem = gj.maxMem = gj.xxxMem = 0; 805 gj.totMem = tgb_memory; 806 gj.bufNew = gj.bufEvt = 0; 970 807 gj.evtSkip = gj.evtWrite = gj.evtErr = 0; 971 808 972 //for (int b = 0; b < NBOARDS; b++) 973 // gj.badRoi[b] = 0; 974 975 mBufInit (); //initialize buffers 809 // initialize mBuffer (mark all entries as unused\empty) 810 for (int i = 0; i < MAX_EVT * MAX_RUN; i++) 811 { 812 evtCtrl[i].evNum = evtCtrl[i].nRoi = -1; 813 evtCtrl[i].runNum = 0; 814 evtCtrl[i].evtStat = -1; 815 } 816 817 evtCtrl_frstPtr = 0; 818 evtCtrl_lastPtr = 0; 976 819 977 820 factPrintf(kInfo, -1, "End initializing (readFAD)"); … … 1013 856 const int p0 = s0==0 ? ntohs (g_port[b].sockAddr.sin_port) : 0; 1014 857 1015 int k = b * 7; 1016 for (int p = p0 + 1; p < p0 + 8; p++, k++) 1017 GenSock (s0, k, p, &g_port[b].sockAddr, &rd[k]); //generate address and socket 858 GenSock(s0, b, p0+1, &g_port[b].sockAddr, &rd[b]); //generate address and socket 1018 859 1019 860 sockDef[b] = g_port[b].sockDef; … … 1026 867 actBoards++; 1027 868 1028 //count number of succesfull actions1029 // int numok = 0;1030 1031 /*1032 for (i=0; i<MAX_SOCK/7; i++)1033 {1034 random_arr[i].val = rand();1035 random_arr[i].idx = i;1036 }1037 1038 qsort(random_arr, MAX_SOCK/7, sizeof(struct rnd), compare);1039 1040 for (int iii = 0; iii < MAX_SOCK/7; iii++) { //check all sockets if something to read1041 1042 b = random_arr[iii].idx;1043 i = b*7;1044 p = 0;1045 */1046 1047 869 //check all sockets if something to read 1048 for (int i = 0; i < MAX_SOCK; i+=7)870 for (int i = 0; i < NBOARDS; i++) 1049 871 { 1050 872 // Do not try to connect this socket 1051 873 if (rd[i].sockStat > 0) 1052 874 continue; 1053 1054 const int board = i / 7 ;1055 //const int p = i % 7 ;1056 875 1057 876 if (rd[i].sockStat == -1) … … 1071 890 // Success (rd[i].sockStat == 0) 1072 891 1073 if (sockDef[ board] > 0)892 if (sockDef[i] > 0) 1074 893 { 1075 894 rd[i].bufTyp = 0; // expect a header … … 1085 904 rd[i].skip = 0; // start empty 1086 905 1087 gi_NumConnect[board] += cntsock; 1088 1089 //gi.numConn[b]++; 1090 gj.numConn[board]++; 1091 1092 factPrintf(kInfo, -1, "New connection %d (number of connections: %d)", board, gj.numConn[board]); 906 gi_NumConnect[i] += cntsock; 907 gj.numConn[i]++; 908 909 factPrintf(kInfo, -1, "New connection %d (number of connections: %d)", i, gj.numConn[i]); 1093 910 } 1094 911 … … 1125 942 factPrintf(kInfo, 441, "Socket %d closed by FAD", i); 1126 943 1127 const int s0 = sockDef[ board] > 0 ? +1 : -1;944 const int s0 = sockDef[i] > 0 ? +1 : -1; 1128 945 GenSock(s0, i, 0, NULL, &rd[i]); 1129 946 1130 947 //gi.gotErr[b]++; 1131 948 1132 gi_NumConnect[ board]-= cntsock ;949 gi_NumConnect[i]-= cntsock ; 1133 950 //gi.numConn[b]--; 1134 gj.numConn[ board]--;951 gj.numConn[i]--; 1135 952 1136 953 continue; … … 1138 955 // Success (jrd > 0) 1139 956 1140 gj.rateBytes[ board] += jrd;957 gj.rateBytes[i] += jrd; 1141 958 1142 959 // are we skipping this board ... … … 1166 983 for (k = 0; k < rd[i].bufPos - 1; k++) 1167 984 { 1168 if (rd[i].rBuf->B[k] == start.B[1] && rd[i].rBuf->B[k+1] == start.B[0]) 985 //start.S = 0xFB01; 986 if (rd[i].rBuf->B[k] == 0xfb && rd[i].rBuf->B[k+1] == 0x01) 1169 987 break; 1170 988 } … … 1228 1046 rd[i].ftmID = rd[i].rBuf->I[3]; //(FTMevt) 1229 1047 rd[i].evtID = rd[i].rBuf->I[4]; //(FADevt) 1230 rd[i].runID = rd[i].rBuf->I[11]==0 ? (int)g_actTime : rd[i].rBuf->I[11];1048 rd[i].runID = rd[i].rBuf->I[11]==0 ? g_actTime : rd[i].rBuf->I[11]; 1231 1049 rd[i].bufTyp = 1; //ready to read full record 1232 1050 rd[i].bufLen = rd[i].fadLen - rd[i].bufPos; … … 1244 1062 continue; 1245 1063 1246 if (rd[i].rBuf->B[rd[i].fadLen - 1] != stop.B[0] || 1247 rd[i].rBuf->B[rd[i].fadLen - 2] != stop.B[1]) 1064 // stop.S = 0x04FE; 1065 if (rd[i].rBuf->B[rd[i].fadLen - 1] != 0xfe || 1066 rd[i].rBuf->B[rd[i].fadLen - 2] != 0x04) 1248 1067 { 1249 1068 //gi.evtErr++; 1250 factPrintf(kError, 301, "End-of-event flag wrong on socket %3d for event %4d (len=%5d), expected %3d %3d,got %3d %3d",1251 i, rd[i].evtID, rd[i].fadLen, stop.B[0], stop.B[1],1069 factPrintf(kError, 301, "End-of-event flag wrong on socket %3d for event %4d (len=%5d), got %3d %3d", 1070 i, rd[i].evtID, rd[i].fadLen, 1252 1071 rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]); 1253 1072 … … 1267 1086 1268 1087 //get index into mBuffer for this event (create if needed) 1269 const int idx = mBufEvt( i);1088 const int idx = mBufEvt(&rd[i]); 1270 1089 1271 1090 // no free entry in mBuffer, retry later … … 1316 1135 const int fadCrate = fadBoard>>8; 1317 1136 1318 if ( board!= (fadCrate * 10 + (fadBoard&0xff)))1137 if (i != (fadCrate * 10 + (fadBoard&0xff))) 1319 1138 { 1320 1139 factPrintf(kWarn, 301, "Board ID mismatch. Expected %d, got %d (C=%d, B=%d)", 1321 board, fadBoard, fadCrate, fadBoard&0xff);1140 i, fadBoard, fadCrate, fadBoard&0xff); 1322 1141 } 1323 1142 1324 if (evtCtrl[idx].board[ board] != -1)1143 if (evtCtrl[idx].board[i] != -1) 1325 1144 { 1326 1145 factPrintf(kWarn, 501, "Got event %5d from board %3d (i=%3d, len=%5d) twice: Starts with %3d %3d - ends with %3d %3d", 1327 evtCtrl[idx].evNum, board, i, rd[i].fadLen,1146 evtCtrl[idx].evNum, i, i, rd[i].fadLen, 1328 1147 rd[i].rBuf->B[0], rd[i].rBuf->B[1], 1329 1148 rd[i].rBuf->B[rd[i].fadLen - 2], … … 1333 1152 1334 1153 // Copy data from rd[i] to mBuffer[evID] 1335 copyData( i, idx, board);1154 copyData(rd[i].rBuf, i, idx); 1336 1155 1337 1156 // now we have stored a new board contents into Event structure 1338 1157 1339 1158 evtCtrl[idx].fEvent->NumBoards++; 1340 evtCtrl[idx].board[ board] = board;1159 evtCtrl[idx].board[i] = i; 1341 1160 evtCtrl[idx].nBoard++; 1342 1161 evtCtrl[idx].evtStat = evtCtrl[idx].nBoard; … … 1392 1211 continue; 1393 1212 } 1394 1395 1213 gi_SecTime = g_actTime; 1396 1214 1397 gj.bufNew = gj.bufEvt =0;1215 gj.bufNew = 0; 1398 1216 1399 1217 //loop over all active events and flag those older than read-timeout 1400 1218 //delete those that are written to disk .... 1219 1220 const int count = (evtCtrl_lastPtr-evtCtrl_frstPtr+MAX_EVT*MAX_RUN)%(MAX_EVT*MAX_RUN); 1221 1401 1222 for (int k0=evtCtrl_frstPtr; k0!=evtCtrl_lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN) 1402 1223 { 1403 1224 // Check the more likely case first: incomplete events 1404 if (evtCtrl[k0].evtStat> 0 && evtCtrl[k0].evtStat<92)1225 if (evtCtrl[k0].evtStat>=0 && evtCtrl[k0].evtStat<92) 1405 1226 { 1406 1227 gj.bufNew++; //incomplete event in Buffer … … 1410 1231 continue; 1411 1232 1233 // This will result in the emission of a dim service. 1234 // It doesn't matter if that takes comparably long, 1235 // because we have to stop the run anyway. 1412 1236 reportIncomplete(k0); 1413 1237 … … 1420 1244 1421 1245 // complete event in Buffer 1422 if (evtCtrl[k0].evtStat >= 95)1423 gj.bufEvt++;1246 //if (evtCtrl[k0].evtStat >= 95) 1247 // gj.bufEvt++; 1424 1248 1425 1249 // Check the less likely case: 'useless' or 'delete' … … 1432 1256 1433 1257 if (k0==evtCtrl_frstPtr) 1434 evtCtrl_frstPtr = (evtCtrl_frstPtr+1) % MAX_EVT * MAX_RUN;1258 evtCtrl_frstPtr = (evtCtrl_frstPtr+1) % (MAX_EVT*MAX_RUN); 1435 1259 else 1436 1260 factPrintf(kError, -1, "Freed a non-first slot"); … … 1438 1262 gj.evtWrite++; 1439 1263 gj.rateWrite++; 1264 1265 continue; 1440 1266 } 1441 1267 1268 // The first events in the queue are either incomplete or 1269 // can be deleted (processing finished). As soon as we reach the 1270 // first complete events which processing is pending, we can stop. 1271 // All other events (if everything works well) must have the same state. 1272 break; 1442 1273 } 1274 1275 // The number of complete events in the buffer is the total number of 1276 // events in the buffer minus the number of incomplete events. 1277 gj.bufEvt = count - gj.bufNew; 1443 1278 1444 1279 gj.deltaT = 1000; //temporary, must be improved … … 1455 1290 1456 1291 factStat (gj); 1457 factStatNew (gi);1292 //factStatNew (gi); 1458 1293 gj.rateNew = gj.rateWrite = 0; 1459 1294 gj.maxMem = gj.usdMem; … … 1494 1329 factPrintf(kInfo, -1, "Close all sockets..."); 1495 1330 1496 for (int i = 0; i < MAX_SOCK; i++)1331 for (int i = 0; i < NBOARDS; i++) 1497 1332 { 1498 1333 if (rd[i].sockStat != 0) … … 1500 1335 1501 1336 GenSock(-1, i, 0, NULL, &rd[i]); //close and destroy open socket 1502 if (i%7) 1503 continue; 1504 1505 gi_NumConnect[i / 7]-= cntsock ; 1506 //gi.numConn[i / 7]--; 1507 gj.numConn[i / 7]--; 1508 sockDef[i / 7] = 0; //flag ro recreate the sockets ... 1509 rd[i / 7].sockStat = -1; //and try to open asap 1337 1338 gi_NumConnect[i]-= cntsock ; 1339 gj.numConn[i]--; 1340 sockDef[i] = 0; //flag ro recreate the sockets ... 1341 rd[i].sockStat = -1; //and try to open asap 1510 1342 } 1511 1343 } … … 1514 1346 if (gi_resetR > 0) 1515 1347 { 1516 //flag all events as 'read finished' 1517 for (int k0=evtCtrl_frstPtr; k0!=evtCtrl_lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN) 1518 { 1519 if (evtCtrl[k0].evtStat > 0 && evtCtrl[k0].evtStat < 90) 1348 //and clear all buffers (might have to wait until all others are done) 1349 while (evtCtrl_frstPtr!=evtCtrl_lastPtr) 1350 { 1351 const int k0=evtCtrl_frstPtr; 1352 1353 // flag incomplete events as 'read finished' 1354 if (evtCtrl[k0].evtStat>0 && evtCtrl[k0].evtStat < 90) 1355 evtCtrl[k0].evtStat = 91; 1356 1357 if (evtCtrl[k0].evtStat==0 || evtCtrl[k0].evtStat>900) 1520 1358 { 1521 evtCtrl[k0].evtStat = 91; 1522 //gi.evtSkp++; 1523 //gi.evtTot++; 1359 mBufFree(k0); //event written--> free memory 1360 evtCtrl[k0].evtStat = -1; 1361 1362 evtCtrl_frstPtr = (evtCtrl_frstPtr+1) % (MAX_EVT*MAX_RUN); 1524 1363 } 1364 1365 usleep(1); 1525 1366 } 1526 1527 //and clear all buffers (might have to wait until all others are done)1528 // If minclear is 0, an event could be deleted while writing is still ongoing1529 /*1530 int minclear;1531 if (gi_resetR == 1) {1532 minclear = 900;1533 factPrintf(kInfo, -1, "Drain all buffers ...");1534 } else {1535 minclear = 0;1536 factPrintf(kInfo, -1, "Flush all buffers ...");1537 }*/1538 const int minclear = 900;1539 1540 int numclear = 1;1541 while (numclear > 0)1542 {1543 numclear = 0;1544 1545 for (int k0=evtCtrl_frstPtr; k0!=evtCtrl_lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)1546 {1547 if (evtCtrl[k0].evtStat==0 || evtCtrl[k0].evtStat > minclear)1548 {1549 mBufFree(k0); //event written--> free memory1550 evtCtrl[k0].evtStat = -1;1551 1552 if (k0==evtCtrl_frstPtr)1553 evtCtrl_frstPtr = (evtCtrl_frstPtr+1) % MAX_EVT * MAX_RUN;1554 else1555 factPrintf(kError, -1, "Freed a non-first slot");1556 1557 continue;1558 }1559 1560 if (evtCtrl[k0].evtStat > 0)1561 numclear++; //writing is still ongoing...1562 }1563 1564 usleep(1);1565 }1566 1367 } 1567 1368 … … 1586 1387 factPrintf(kInfo, -1, "Exit read Process..."); 1587 1388 1588 factPrintf(kInfo, -1, "%ld Bytes flag ed as in-use.", tgb_inuse);1389 factPrintf(kInfo, -1, "%ld Bytes flagged as in-use.", tgb_inuse); 1589 1390 1590 1391 gi_runStat = -99; … … 2015 1816 } 2016 1817 2017 const int id = k0;//evtCtrl[k0].mBuffer_idx; 2018 2019 const uint32_t irun = evtCtrl[id].runNum; 2020 const int32_t ievt = evtCtrl[id].evNum; 1818 const uint32_t irun = evtCtrl[k0].runNum; 1819 const int32_t ievt = evtCtrl[k0].evNum; 2021 1820 2022 1821 // Find entry in runCtrl which belongs to the event mBuffer[id] … … 2032 1831 if (j>=MAX_RUN) 2033 1832 { 2034 factPrintf(kFatal, 901, "writeEvt: Can not find run %d for event %d in %d", irun, ievt, id);1833 factPrintf(kFatal, 901, "writeEvt: Can not find run %d for event %d in %d", irun, ievt, k0); 2035 1834 // FIXME: What is the right action? 2036 1835 continue; … … 2046 1845 actRun.Version = 1; 2047 1846 actRun.RunType = -1; //to be adapted 2048 actRun.Nroi = evtCtrl[ id].nRoi; //runCtrl[lastRun].roi0;2049 actRun.NroiTM = evtCtrl[ id].nRoiTM; //runCtrl[lastRun].roi8;2050 actRun.RunTime = evtCtrl[ id].pcTime[0];//runCtrl[lastRun].firstTime;2051 actRun.RunUsec = evtCtrl[ id].pcTime[1];//runCtrl[lastRun].firstUsec;1847 actRun.Nroi = evtCtrl[k0].nRoi; //runCtrl[lastRun].roi0; 1848 actRun.NroiTM = evtCtrl[k0].nRoiTM; //runCtrl[lastRun].roi8; 1849 actRun.RunTime = evtCtrl[k0].pcTime[0];//runCtrl[lastRun].firstTime; 1850 actRun.RunUsec = evtCtrl[k0].pcTime[1];//runCtrl[lastRun].firstUsec; 2052 1851 actRun.NBoard = NBOARDS; 2053 1852 actRun.NPix = NPIX; 2054 1853 actRun.NTm = NTMARK; 2055 1854 2056 memcpy(actRun.FADhead, evtCtrl[ id].FADhead, NBOARDS * sizeof (PEVNT_HEADER));1855 memcpy(actRun.FADhead, evtCtrl[k0].FADhead, NBOARDS * sizeof (PEVNT_HEADER)); 2057 1856 2058 1857 runCtrl[lastRun].fileHd = runOpen (irun, &actRun, sizeof (actRun)); … … 2084 1883 if (runCtrl[lastRun].fileId==0) 2085 1884 { 2086 const int rc = runWrite(runCtrl[lastRun].fileHd, evtCtrl[ id].fEvent,2087 sizeof (evtCtrl[id]));1885 const int rc = runWrite(runCtrl[lastRun].fileHd, evtCtrl[k0].fEvent, 1886 0/*sizeof (evtCtrl[k0])*/); 2088 1887 if (rc >= 0) 2089 1888 {
Note:
See TracChangeset
for help on using the changeset viewer.