- Timestamp:
- 04/30/13 15:46:40 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/EventBuilder.c
r15459 r15460 176 176 TGB_entry *last = tgb_last; 177 177 178 TGB_entry*mem = last->mem;179 tgb_last 178 void *mem = last->mem; 179 tgb_last = last->prev; 180 180 181 181 free(last); … … 187 187 { 188 188 // 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)); 190 190 191 191 entry->prev = tgb_last; … … 321 321 322 322 323 RUN_HEAD actRun;324 325 323 RUN_CTRL runCtrl[MAX_RUN]; 326 327 324 RUN_TAIL runTail[MAX_RUN]; 328 325 … … 406 403 int 407 404 GenSock (int flag, int sid, int port, struct sockaddr_in *sockAddr, 408 READ_STRUCT * r d)405 READ_STRUCT * rs) 409 406 { 410 407 /* … … 425 422 if (sid % 7 >= NUMSOCK) { 426 423 //this is a not used socket, so do nothing ... 427 r d->sockStat = 77;428 r d->rBuf = NULL ;424 rs->sockStat = 77; 425 rs->rBuf = NULL ; 429 426 return 0; 430 427 } 431 428 432 if (r d->sockStat == 0) { //close socket if open433 j = close (r d->socket);429 if (rs->sockStat == 0) { //close socket if open 430 j = close (rs->socket); 434 431 if (j > 0) { 435 432 factPrintf(kFatal, 771, "Closing socket %d failed: %m (close,rc=%d)", sid, errno); … … 439 436 } 440 437 441 r d->sockStat = 99;438 rs->sockStat = 99; 442 439 443 440 if (flag < 0) { 444 free (r d->rBuf); //and never open again445 r d->rBuf = NULL;441 free (rs->rBuf); //and never open again 442 rs->rBuf = NULL; 446 443 return 0; 447 444 } … … 449 446 450 447 if (flag == 0) { //generate address and buffer ... 451 r d->Port = port;452 r d->SockAddr.sin_family = sockAddr->sin_family;453 r d->SockAddr.sin_port = htons (port);454 r d->SockAddr.sin_addr = sockAddr->sin_addr;455 456 r d->rBuf =malloc (sizeof (CNV_FACT));457 if (r d->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) { 458 455 factPrintf(kFatal, 774, "Could not create local buffer %d (malloc failed)", sid); 459 r d->sockStat = 77;456 rs->sockStat = 77; 460 457 return -3; 461 458 } … … 463 460 464 461 465 if ((r d->socket = socket (PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) <= 0) {462 if ((rs->socket = socket (PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) <= 0) { 466 463 factPrintf(kFatal, 773, "Generating socket %d failed: %m (socket,rc=%d)", sid, errno); 467 464 rd->sockStat = 88; … … 469 466 } 470 467 optval = 1; 471 if (setsockopt (r d->socket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {468 if (setsockopt (rs->socket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) { 472 469 factPrintf(kInfo, 173, "Setting SO_KEEPALIVE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno); 473 470 } 474 471 optval = 10; //start after 10 seconds 475 if (setsockopt (r d->socket, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) {472 if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) { 476 473 factPrintf(kInfo, 173, "Setting TCP_KEEPIDLE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno); 477 474 } 478 475 optval = 10; //do every 10 seconds 479 if (setsockopt (r d->socket, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) {476 if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) { 480 477 factPrintf(kInfo, 173, "Setting TCP_KEEPINTVL for socket %d failed: %m (setsockopt,rc=%d)", sid, errno); 481 478 } 482 479 optval = 2; //close after 2 unsuccessful tries 483 if (setsockopt (r d->socket, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) {480 if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) { 484 481 factPrintf(kInfo, 173, "Setting TCP_KEEPCNT for socket %d failed: %m (setsockopt,rc=%d)", sid, errno); 485 482 } … … 487 484 factPrintf(kInfo, 773, "Successfully generated socket %d", sid); 488 485 489 r d->sockStat = -1; //try to (re)open socket490 r d->errCnt = 0;486 rs->sockStat = -1; //try to (re)open socket 487 rs->errCnt = 0; 491 488 return 0; 492 489 … … 526 523 #endif 527 524 528 actRun.FADhead = malloc (NBOARDS * sizeof (PEVNT_HEADER));525 //actRun.FADhead = malloc (NBOARDS * sizeof (PEVNT_HEADER)); 529 526 530 527 evtCtrl.frstPtr = 0; … … 789 786 void initEvent(int i) 790 787 { 791 mBuffer[i].fEvent = ( void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM);788 mBuffer[i].fEvent = (EVENT*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM); 792 789 793 790 //flag all pixels as unused … … 1399 1396 } 1400 1397 1401 // are we reading data?1402 if (rd[i].bufTyp >0)1398 //we are reading event header 1399 if (rd[i].bufTyp <= 0) 1403 1400 { 1404 // not yet all read1405 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 header1417 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 // else1428 // 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 later1434 if (evID == -1)1435 continue;1436 1437 // We have a valid entry, but no memory has yet been allocated1438 if (evID >= 0 && mBuffer[evID].FADhead == NULL)1439 {1440 // Try to get memory from the big buffer1441 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, or1445 // replaced by a signal or dim message1446 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]->fEvent1453 initEvent(evID);1454 1455 // Some statistics1456 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 header1478 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 it1487 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 header1504 }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 structure1510 1511 const int iDx = evtIdx[evID]; //index into evtCtrl1512 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 started1524 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 the1532 // previous run in terms of number of events1533 if (runCtrl[j].runId==actrun)1534 runCtrl[j].maxEvt = runCtrl[j].lastEvt;1535 1536 // We got the first part of this event, so this is1537 // the number of events we expect for this run1538 if (runCtrl[j].runId==mBuffer[evID].runNum)1539 runCtrl[j].lastEvt++;1540 }1541 1542 // Change 'actrun' the the new runnumber1543 actrun = mBuffer[evID].runNum;1544 }1545 1546 //complete event read ---> flag for next processing1547 if (mBuffer[evID].nBoard >= actBoards)1548 {1549 // This is a non-ideal hack to lower the probability that1550 // in mBufEvt the search for correct entry in runCtrl1551 // will not return a super-old entry1552 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 processing1559 evtCtrl.evtStat[iDx] = 99;1560 }1561 1562 // Continue reading next header1563 }1564 else1565 {1566 //we are reading event header1567 1568 1401 //not yet sufficient data to take action 1569 1402 if (rd[i].bufPos < minLen) … … 1646 1479 debugHead(i, fadBoard, rd[i].rBuf); 1647 1480 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; 1651 1643 1652 1644 } // end for loop over all sockets … … 2051 2043 } 2052 2044 2045 RUN_HEAD actRun; 2046 2053 2047 actRun.Version = 1; 2054 2048 actRun.RunType = -1; //to be adapted … … 2365 2359 if (runCtrl[lastRun].fileId < 0) 2366 2360 { 2361 RUN_HEAD actRun; 2362 2367 2363 actRun.Version = 1; 2368 2364 actRun.RunType = -1; //to be adapted
Note:
See TracChangeset
for help on using the changeset viewer.