Changeset 15460 for trunk/FACT++


Ignore:
Timestamp:
04/30/13 15:46:40 (11 years ago)
Author:
tbretz
Message:
Made RUN_HEAD local; first process 'header' and then 'data' so that the logically last action in the loop is to flag an event as complete; evtCtrl.evtStat[iDx] must contain the number of boards to detect incomplete events (this was removed yesterday); added some casts required if compiled as C++; replaced rd by rs in GenSock because rd is already globally used
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/EventBuilder.c

    r15459 r15460  
    176176    TGB_entry *last = tgb_last;
    177177
    178     TGB_entry *mem = last->mem;
    179     tgb_last       = last->prev;
     178    void *mem = last->mem;
     179    tgb_last  = last->prev;
    180180
    181181    free(last);
     
    187187{
    188188    // Add the last free slot to the stack
    189     TGB_entry *entry = malloc(sizeof(TGB_entry));
     189    TGB_entry *entry = (TGB_entry*)malloc(sizeof(TGB_entry));
    190190
    191191    entry->prev = tgb_last;
     
    321321
    322322
    323 RUN_HEAD actRun;
    324 
    325323RUN_CTRL runCtrl[MAX_RUN];
    326 
    327324RUN_TAIL runTail[MAX_RUN];
    328325
     
    406403int
    407404GenSock (int flag, int sid, int port, struct sockaddr_in *sockAddr,
    408          READ_STRUCT * rd)
     405         READ_STRUCT * rs)
    409406{
    410407/*
     
    425422   if (sid % 7 >= NUMSOCK) {
    426423      //this is a not used socket, so do nothing ...
    427       rd->sockStat = 77;
    428       rd->rBuf = NULL ;
     424      rs->sockStat = 77;
     425      rs->rBuf = NULL ;
    429426      return 0;
    430427   }
    431428
    432    if (rd->sockStat == 0) {     //close socket if open
    433       j = close (rd->socket);
     429   if (rs->sockStat == 0) {     //close socket if open
     430      j = close (rs->socket);
    434431      if (j > 0) {
    435432          factPrintf(kFatal, 771, "Closing socket %d failed: %m (close,rc=%d)", sid, errno);
     
    439436   }
    440437
    441    rd->sockStat = 99;
     438   rs->sockStat = 99;
    442439
    443440   if (flag < 0) {
    444       free (rd->rBuf);          //and never open again
    445       rd->rBuf = NULL;
     441      free (rs->rBuf);          //and never open again
     442      rs->rBuf = NULL;
    446443      return 0;
    447444   }
     
    449446
    450447   if (flag == 0) {             //generate address and buffer ...
    451       rd->Port = port;
    452       rd->SockAddr.sin_family = sockAddr->sin_family;
    453       rd->SockAddr.sin_port = htons (port);
    454       rd->SockAddr.sin_addr = sockAddr->sin_addr;
    455 
    456       rd->rBuf = malloc (sizeof (CNV_FACT));
    457       if (rd->rBuf == NULL) {
     448      rs->Port = port;
     449      rs->SockAddr.sin_family = sockAddr->sin_family;
     450      rs->SockAddr.sin_port = htons (port);
     451      rs->SockAddr.sin_addr = sockAddr->sin_addr;
     452
     453      rs->rBuf = (CNV_FACT*)malloc (sizeof (CNV_FACT));
     454      if (rs->rBuf == NULL) {
    458455         factPrintf(kFatal, 774, "Could not create local buffer %d (malloc failed)", sid);
    459          rd->sockStat = 77;
     456         rs->sockStat = 77;
    460457         return -3;
    461458      }
     
    463460
    464461
    465    if ((rd->socket = socket (PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) <= 0) {
     462   if ((rs->socket = socket (PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) <= 0) {
    466463       factPrintf(kFatal, 773, "Generating socket %d failed: %m (socket,rc=%d)", sid, errno);
    467464      rd->sockStat = 88;
     
    469466   }
    470467   optval = 1;
    471    if (setsockopt (rd->socket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
     468   if (setsockopt (rs->socket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
    472469       factPrintf(kInfo, 173, "Setting SO_KEEPALIVE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
    473470   }
    474471   optval = 10;                 //start after 10 seconds
    475    if (setsockopt (rd->socket, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) {
     472   if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) {
    476473       factPrintf(kInfo, 173, "Setting TCP_KEEPIDLE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
    477474   }
    478475   optval = 10;                 //do every 10 seconds
    479    if (setsockopt (rd->socket, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) {
     476   if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) {
    480477      factPrintf(kInfo, 173, "Setting TCP_KEEPINTVL for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
    481478   }
    482479   optval = 2;                  //close after 2 unsuccessful tries
    483    if (setsockopt (rd->socket, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) {
     480   if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) {
    484481      factPrintf(kInfo, 173, "Setting TCP_KEEPCNT for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
    485482   }
     
    487484   factPrintf(kInfo, 773, "Successfully generated socket %d", sid);
    488485
    489    rd->sockStat = -1;           //try to (re)open socket
    490    rd->errCnt = 0;
     486   rs->sockStat = -1;           //try to (re)open socket
     487   rs->errCnt = 0;
    491488   return 0;
    492489
     
    526523#endif
    527524
    528    actRun.FADhead = malloc (NBOARDS * sizeof (PEVNT_HEADER));
     525   //actRun.FADhead = malloc (NBOARDS * sizeof (PEVNT_HEADER));
    529526
    530527   evtCtrl.frstPtr = 0;
     
    789786void initEvent(int i)
    790787{
    791     mBuffer[i].fEvent = (void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM);
     788    mBuffer[i].fEvent = (EVENT*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM);
    792789
    793790    //flag all pixels as unused
     
    13991396          }
    14001397
    1401           // are we reading data?
    1402           if (rd[i].bufTyp > 0)
     1398          //we are reading event header
     1399          if (rd[i].bufTyp <= 0)
    14031400          {
    1404               // not yet all read
    1405               if (rd[i].bufLen > 0)
    1406                   continue;
    1407 
    1408               if (rd[i].rBuf->B[rd[i].fadLen - 1] != stop.B[0] ||
    1409                   rd[i].rBuf->B[rd[i].fadLen - 2] != stop.B[1])
    1410               {
    1411                   //gi.evtErr++;
    1412                   factPrintf(kError, 301, "End-of-event flag wrong on socket %3d for event %4d (len=%5d), expected %3d %3d, got %3d %3d",
    1413                              i, rd[i].evtID, rd[i].fadLen, stop.B[0], stop.B[1],
    1414                              rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]);
    1415 
    1416                   // ready to read next header
    1417                   rd[i].bufTyp = 0;
    1418                   rd[i].bufLen = sizeof(PEVNT_HEADER);
    1419                   rd[i].bufPos = 0;
    1420 
    1421                   continue;
    1422               }
    1423 
    1424               //  int actid;
    1425               //  if (g_useFTM > 0)
    1426               //     actid = rd[i].evtID;
    1427               //  else
    1428               //     actid = rd[i].ftmID;
    1429 
    1430               //get index into mBuffer for this event (create if needed)
    1431               const int evID = mBufEvt(i);
    1432 
    1433               // no free entry in mBuffer, retry later
    1434               if (evID == -1)
    1435                   continue;
    1436 
    1437               // We have a valid entry, but no memory has yet been allocated
    1438               if (evID >= 0 && mBuffer[evID].FADhead == NULL)
    1439               {
    1440                   // Try to get memory from the big buffer
    1441                   mBuffer[evID].FADhead = TGB_Malloc();
    1442                   if (mBuffer[evID].FADhead == NULL)
    1443                   {
    1444                       // If this works properly, this is a hack which can be removed, or
    1445                       // replaced by a signal or dim message
    1446                       if (rd[i].bufTyp==2)
    1447                           factPrintf(kError, 882, "malloc failed for event %d", evID);
    1448                       rd[i].bufTyp = 2;
    1449                       continue;
    1450                   }
    1451 
    1452                   // Initialice mBuffer[evID]->fEvent
    1453                   initEvent(evID);
    1454 
    1455                   // Some statistics
    1456                   gj.usdMem = tgb_inuse;
    1457 
    1458                   if (gj.usdMem > gj.maxMem)
    1459                       gj.maxMem = gj.usdMem;
    1460 
    1461                   gj.rateNew++;
    1462                   gj.bufTot++;
    1463                   if (gj.bufTot > gj.maxEvt)
    1464                       gj.maxEvt = gj.bufTot;
    1465 
    1466                   //register event in 'active list (reading)'
    1467                   evtIdx[evID] = evtCtrl.lastPtr;
    1468 
    1469                   evtCtrl.evtBuf[evtCtrl.lastPtr]  = evID;
    1470                   evtCtrl.evtStat[evtCtrl.lastPtr] = 0;
    1471                   evtCtrl.pcTime[evtCtrl.lastPtr]  = g_actTime;
    1472 
    1473                   evtCtrl.lastPtr++;
    1474                   evtCtrl.lastPtr %= MAX_EVT * MAX_RUN;
    1475               }
    1476 
    1477               // ready to read next header
    1478               rd[i].bufTyp = 0;
    1479               rd[i].bufLen = sizeof(PEVNT_HEADER);
    1480               rd[i].bufPos = 0;
    1481 
    1482               // Fatal error occured. Event cannot be processed. Skip it. Start reading next header.
    1483               if (evID < -1000)
    1484                   continue;
    1485 
    1486               //we have a valid entry in mBuffer[]; fill it
    1487               const int fadBoard = rd[i].rBuf->S[12];
    1488               const int fadCrate = fadBoard>>8;
    1489 
    1490               if (board != (fadCrate * 10 + (fadBoard&0xff)))
    1491               {
    1492                   factPrintf(kWarn, 301, "Board ID mismatch. Expected %d, got %d (C=%d, B=%d)",
    1493                              board, fadBoard, fadCrate, fadBoard&0xff);
    1494               }
    1495 
    1496               if (mBuffer[evID].board[board] != -1)
    1497               {
    1498                   factPrintf(kWarn, 501, "Got event %5d from board %3d (i=%3d, len=%5d) twice: Starts with %3d %3d - ends with %3d %3d",
    1499                              evID, board, i, rd[i].fadLen,
    1500                              rd[i].rBuf->B[0], rd[i].rBuf->B[1],
    1501                              rd[i].rBuf->B[rd[i].fadLen - 2],
    1502                              rd[i].rBuf->B[rd[i].fadLen - 1]);
    1503                   continue; // Continue reading next header
    1504               }
    1505 
    1506               // Copy data from rd[i] to mBuffer[evID]
    1507               copyData(i, evID, board);
    1508 
    1509               // now we have stored a new board contents into Event structure
    1510 
    1511               const int iDx = evtIdx[evID];       //index into evtCtrl
    1512 
    1513               //evtCtrl.evtStat[iDx]++;
    1514               evtCtrl.pcTime[iDx] = g_actTime;
    1515 
    1516               mBuffer[evID].fEvent->NumBoards++;
    1517               mBuffer[evID].board[board] = board;
    1518               mBuffer[evID].nBoard++;
    1519 
    1520               // have we already reported first (partial) event of this run ???
    1521               if (mBuffer[evID].nBoard==1 && mBuffer[evID].runNum != actrun)
    1522               {
    1523                   // Signal the fadctrl that a new run has been started
    1524                   gotNewRun(mBuffer[evID].runNum, NULL);
    1525 
    1526                   factPrintf(kInfo, 1, "gotNewRun called, prev run %d, new run %d, event %d",
    1527                              actrun, mBuffer[evID].runNum, mBuffer[evID].evNum);
    1528 
    1529                   for (int j=0; j<MAX_RUN; j++)
    1530                   {
    1531                       // Since we have started a new run, we know already when to close the
    1532                       // previous run in terms of number of events
    1533                       if (runCtrl[j].runId==actrun)
    1534                           runCtrl[j].maxEvt = runCtrl[j].lastEvt;
    1535 
    1536                       // We got the first part of this event, so this is
    1537                       // the number of events we expect for this run
    1538                       if (runCtrl[j].runId==mBuffer[evID].runNum)
    1539                           runCtrl[j].lastEvt++;
    1540                   }
    1541 
    1542                   // Change 'actrun' the the new runnumber
    1543                   actrun = mBuffer[evID].runNum;
    1544               }
    1545 
    1546               //complete event read ---> flag for next processing
    1547               if (mBuffer[evID].nBoard >= actBoards)
    1548               {
    1549                   // This is a non-ideal hack to lower the probability that
    1550                   // in mBufEvt the search for correct entry in runCtrl
    1551                   // will not return a super-old entry
    1552                   for (int ir=0; ir<MAX_RUN; ir++)
    1553                   {
    1554                       if (runCtrl[ir].runId != actrun && runCtrl[ir].fileId>0)
    1555                           runCtrl[ir].runId = 0;
    1556                   }
    1557 
    1558                   // Flag that the event is ready for processing
    1559                   evtCtrl.evtStat[iDx] = 99;
    1560               }
    1561 
    1562               // Continue reading next header
    1563           }
    1564           else
    1565           {
    1566               //we are reading event header
    1567 
    15681401              //not yet sufficient data to take action
    15691402              if (rd[i].bufPos < minLen)
     
    16461479              debugHead(i, fadBoard, rd[i].rBuf);
    16471480
    1648               // Continue reading data
    1649 
    1650           } // end if data or header
     1481              continue;
     1482          }
     1483
     1484          // are we reading data
     1485
     1486          // not yet all read
     1487          if (rd[i].bufLen > 0)
     1488              continue;
     1489
     1490          if (rd[i].rBuf->B[rd[i].fadLen - 1] != stop.B[0] ||
     1491              rd[i].rBuf->B[rd[i].fadLen - 2] != stop.B[1])
     1492          {
     1493              //gi.evtErr++;
     1494              factPrintf(kError, 301, "End-of-event flag wrong on socket %3d for event %4d (len=%5d), expected %3d %3d, got %3d %3d",
     1495                         i, rd[i].evtID, rd[i].fadLen, stop.B[0], stop.B[1],
     1496                         rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]);
     1497
     1498              // ready to read next header
     1499              rd[i].bufTyp = 0;
     1500              rd[i].bufLen = sizeof(PEVNT_HEADER);
     1501              rd[i].bufPos = 0;
     1502
     1503              continue;
     1504          }
     1505
     1506          //  int actid;
     1507          //  if (g_useFTM > 0)
     1508          //     actid = rd[i].evtID;
     1509          //  else
     1510          //     actid = rd[i].ftmID;
     1511
     1512          //get index into mBuffer for this event (create if needed)
     1513          const int evID = mBufEvt(i);
     1514
     1515          // no free entry in mBuffer, retry later
     1516          if (evID == -1)
     1517              continue;
     1518
     1519          // We have a valid entry, but no memory has yet been allocated
     1520          if (evID >= 0 && mBuffer[evID].FADhead == NULL)
     1521          {
     1522              // Try to get memory from the big buffer
     1523              mBuffer[evID].FADhead = (PEVT_HEADER*)TGB_Malloc();
     1524              if (mBuffer[evID].FADhead == NULL)
     1525              {
     1526                  // If this works properly, this is a hack which can be removed, or
     1527                  // replaced by a signal or dim message
     1528                  if (rd[i].bufTyp==2)
     1529                      factPrintf(kError, 882, "malloc failed for event %d", evID);
     1530                  rd[i].bufTyp = 2;
     1531                  continue;
     1532              }
     1533
     1534              // Initialice mBuffer[evID]->fEvent
     1535              initEvent(evID);
     1536
     1537              // Some statistics
     1538              gj.usdMem = tgb_inuse;
     1539
     1540              if (gj.usdMem > gj.maxMem)
     1541                  gj.maxMem = gj.usdMem;
     1542
     1543              gj.rateNew++;
     1544              gj.bufTot++;
     1545              if (gj.bufTot > gj.maxEvt)
     1546                  gj.maxEvt = gj.bufTot;
     1547
     1548              //register event in 'active list (reading)'
     1549              evtIdx[evID] = evtCtrl.lastPtr;
     1550
     1551              evtCtrl.evtBuf[evtCtrl.lastPtr]  = evID;
     1552              evtCtrl.evtStat[evtCtrl.lastPtr] = 0;
     1553              evtCtrl.pcTime[evtCtrl.lastPtr]  = g_actTime;
     1554
     1555              evtCtrl.lastPtr++;
     1556              evtCtrl.lastPtr %= MAX_EVT * MAX_RUN;
     1557          }
     1558
     1559          // ready to read next header
     1560          rd[i].bufTyp = 0;
     1561          rd[i].bufLen = sizeof(PEVNT_HEADER);
     1562          rd[i].bufPos = 0;
     1563
     1564          // Fatal error occured. Event cannot be processed. Skip it. Start reading next header.
     1565          if (evID < -1000)
     1566              continue;
     1567
     1568          //we have a valid entry in mBuffer[]; fill it
     1569          const int fadBoard = rd[i].rBuf->S[12];
     1570          const int fadCrate = fadBoard>>8;
     1571
     1572          if (board != (fadCrate * 10 + (fadBoard&0xff)))
     1573          {
     1574              factPrintf(kWarn, 301, "Board ID mismatch. Expected %d, got %d (C=%d, B=%d)",
     1575                         board, fadBoard, fadCrate, fadBoard&0xff);
     1576          }
     1577
     1578          if (mBuffer[evID].board[board] != -1)
     1579          {
     1580              factPrintf(kWarn, 501, "Got event %5d from board %3d (i=%3d, len=%5d) twice: Starts with %3d %3d - ends with %3d %3d",
     1581                         evID, board, i, rd[i].fadLen,
     1582                         rd[i].rBuf->B[0], rd[i].rBuf->B[1],
     1583                         rd[i].rBuf->B[rd[i].fadLen - 2],
     1584                         rd[i].rBuf->B[rd[i].fadLen - 1]);
     1585              continue; // Continue reading next header
     1586          }
     1587
     1588          // Copy data from rd[i] to mBuffer[evID]
     1589          copyData(i, evID, board);
     1590
     1591          // now we have stored a new board contents into Event structure
     1592
     1593          const int iDx = evtIdx[evID];       //index into evtCtrl
     1594
     1595          mBuffer[evID].fEvent->NumBoards++;
     1596          mBuffer[evID].board[board] = board;
     1597          mBuffer[evID].nBoard++;
     1598
     1599          evtCtrl.evtStat[iDx] = mBuffer[evID].nBoard;
     1600          evtCtrl.pcTime[iDx]  = g_actTime;
     1601
     1602          // have we already reported first (partial) event of this run ???
     1603          if (mBuffer[evID].nBoard==1 && mBuffer[evID].runNum != actrun)
     1604          {
     1605              // Signal the fadctrl that a new run has been started
     1606              gotNewRun(mBuffer[evID].runNum, NULL);
     1607
     1608              factPrintf(kInfo, 1, "gotNewRun called, prev run %d, new run %d, event %d",
     1609                         actrun, mBuffer[evID].runNum, mBuffer[evID].evNum);
     1610
     1611              for (int j=0; j<MAX_RUN; j++)
     1612              {
     1613                  // Since we have started a new run, we know already when to close the
     1614                  // previous run in terms of number of events
     1615                  if (runCtrl[j].runId==actrun)
     1616                      runCtrl[j].maxEvt = runCtrl[j].lastEvt;
     1617
     1618                  // We got the first part of this event, so this is
     1619                  // the number of events we expect for this run
     1620                  if (runCtrl[j].runId==mBuffer[evID].runNum)
     1621                      runCtrl[j].lastEvt++;
     1622              }
     1623
     1624              // Change 'actrun' the the new runnumber
     1625              actrun = mBuffer[evID].runNum;
     1626          }
     1627
     1628          // event not yet complete
     1629          if (mBuffer[evID].nBoard < actBoards)
     1630              continue;
     1631
     1632          // This is a non-ideal hack to lower the probability that
     1633          // in mBufEvt the search for correct entry in runCtrl
     1634          // will not return a super-old entry
     1635          for (int ir=0; ir<MAX_RUN; ir++)
     1636          {
     1637              if (runCtrl[ir].runId != actrun && runCtrl[ir].fileId>0)
     1638                  runCtrl[ir].runId = 0;
     1639          }
     1640
     1641          // Flag that the event is ready for processing
     1642          evtCtrl.evtStat[iDx] = 99;
    16511643
    16521644      } // end for loop over all sockets
     
    20512043               }
    20522044
     2045               RUN_HEAD actRun;
     2046
    20532047               actRun.Version = 1;
    20542048               actRun.RunType = -1;  //to be adapted
     
    23652359           if (runCtrl[lastRun].fileId < 0)
    23662360           {
     2361               RUN_HEAD actRun;
     2362
    23672363               actRun.Version = 1;
    23682364               actRun.RunType = -1;  //to be adapted
Note: See TracChangeset for help on using the changeset viewer.