| 1 | // #define EVTDEBUG
|
|---|
| 2 |
|
|---|
| 3 | #define NUMSOCK 1 //set to 7 for old configuration
|
|---|
| 4 | #define MAXREAD 65536 //64kB wiznet buffer
|
|---|
| 5 |
|
|---|
| 6 | #include <stdlib.h>
|
|---|
| 7 | #include <stdint.h>
|
|---|
| 8 | #include <stdarg.h>
|
|---|
| 9 | #include <unistd.h>
|
|---|
| 10 | #include <stdio.h>
|
|---|
| 11 | #include <sys/time.h>
|
|---|
| 12 | #include <arpa/inet.h>
|
|---|
| 13 | #include <string.h>
|
|---|
| 14 | #include <math.h>
|
|---|
| 15 | #include <error.h>
|
|---|
| 16 | #include <errno.h>
|
|---|
| 17 | #include <unistd.h>
|
|---|
| 18 | #include <sys/types.h>
|
|---|
| 19 | #include <sys/socket.h>
|
|---|
| 20 | #include <netinet/in.h>
|
|---|
| 21 | #include <netinet/tcp.h>
|
|---|
| 22 | #include <pthread.h>
|
|---|
| 23 | #include <sched.h>
|
|---|
| 24 |
|
|---|
| 25 | #include "EventBuilder.h"
|
|---|
| 26 |
|
|---|
| 27 | enum Severity
|
|---|
| 28 | {
|
|---|
| 29 | kMessage = 10, ///< Just a message, usually obsolete
|
|---|
| 30 | kInfo = 20, ///< An info telling something which can be interesting to know
|
|---|
| 31 | kWarn = 30, ///< A warning, things that somehow might result in unexpected or unwanted bahaviour
|
|---|
| 32 | kError = 40, ///< Error, something unexpected happened, but can still be handled by the program
|
|---|
| 33 | kFatal = 50, ///< An error which cannot be handled at all happend, the only solution is program termination
|
|---|
| 34 | kDebug = 99, ///< A message used for debugging only
|
|---|
| 35 | };
|
|---|
| 36 |
|
|---|
| 37 | #define MIN_LEN 32 // min #bytes needed to interpret FADheader
|
|---|
| 38 | #define MAX_LEN 256*1024 // size of read-buffer per socket
|
|---|
| 39 |
|
|---|
| 40 | //#define nanosleep(x,y)
|
|---|
| 41 |
|
|---|
| 42 | extern FileHandle_t runOpen (uint32_t irun, RUN_HEAD * runhd, size_t len);
|
|---|
| 43 | extern int runWrite (FileHandle_t fileHd, EVENT * event, size_t len);
|
|---|
| 44 | extern int runClose (FileHandle_t fileHd, RUN_TAIL * runth, size_t len);
|
|---|
| 45 | //extern int runFinish (uint32_t runnr);
|
|---|
| 46 |
|
|---|
| 47 | extern void factOut (int severity, int err, char *message);
|
|---|
| 48 | extern void factReportIncomplete (uint64_t rep);
|
|---|
| 49 |
|
|---|
| 50 | extern void gotNewRun (int runnr, PEVNT_HEADER * headers);
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | extern void factStat (GUI_STAT gj);
|
|---|
| 54 |
|
|---|
| 55 | extern void factStatNew (EVT_STAT gi);
|
|---|
| 56 |
|
|---|
| 57 | extern int eventCheck (uint32_t runNr, PEVNT_HEADER * fadhd, EVENT * event);
|
|---|
| 58 |
|
|---|
| 59 | extern int subProcEvt (int threadID, PEVNT_HEADER * fadhd, EVENT * event,
|
|---|
| 60 | int8_t * buffer);
|
|---|
| 61 |
|
|---|
| 62 | extern void debugHead (int i, int j, void *buf);
|
|---|
| 63 |
|
|---|
| 64 | extern void debugRead (int isock, int ibyte, int32_t event, int32_t ftmevt,
|
|---|
| 65 | int32_t runnr, int state, uint32_t tsec,
|
|---|
| 66 | uint32_t tusec);
|
|---|
| 67 | extern void debugStream (int isock, void *buf, int len);
|
|---|
| 68 |
|
|---|
| 69 | int CloseRunFile (uint32_t runId, uint32_t closeTime, uint32_t maxEvt);
|
|---|
| 70 |
|
|---|
| 71 | int evtCtrl_frstPtr; // First event in queue
|
|---|
| 72 | int evtCtrl_lastPtr; // pointer to next free slot
|
|---|
| 73 |
|
|---|
| 74 | int g_maxProc;
|
|---|
| 75 | int gi_maxProc;
|
|---|
| 76 |
|
|---|
| 77 | uint g_actTime;
|
|---|
| 78 | int g_runStat;
|
|---|
| 79 | int g_reset;
|
|---|
| 80 |
|
|---|
| 81 | size_t g_maxMem; //maximum memory allowed for buffer
|
|---|
| 82 |
|
|---|
| 83 | FACT_SOCK g_port[NBOARDS]; // .addr=string of IP-addr in dotted-decimal "ddd.ddd.ddd.ddd"
|
|---|
| 84 |
|
|---|
| 85 | uint gi_NumConnect[NBOARDS]; //4 crates * 10 boards
|
|---|
| 86 |
|
|---|
| 87 | //EVT_STAT gi;
|
|---|
| 88 | GUI_STAT gj;
|
|---|
| 89 |
|
|---|
| 90 | #define MAX_EVT 65536 // ( 300s @ 220Hz; 16GB = 5000 evt @ roi=1024 (27s) ; 18000 evt @ roi = 300 )
|
|---|
| 91 | #define MAX_RUN 8 // Number of concurrent runs
|
|---|
| 92 |
|
|---|
| 93 | EVT_CTRL evtCtrl[MAX_EVT]; //control of events during processing
|
|---|
| 94 |
|
|---|
| 95 | void factPrintf(int severity, int id, const char *fmt, ...)
|
|---|
| 96 | {
|
|---|
| 97 | char str[1000];
|
|---|
| 98 |
|
|---|
| 99 | va_list ap;
|
|---|
| 100 | va_start(ap, fmt);
|
|---|
| 101 | vsnprintf(str, 1000, fmt, ap);
|
|---|
| 102 | va_end(ap);
|
|---|
| 103 |
|
|---|
| 104 | factOut(severity, id, str);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 | #define MAX_HEAD_MEM (NBOARDS * sizeof(PEVNT_HEADER))
|
|---|
| 109 | #define MAX_TOT_MEM (sizeof(EVENT) + (NPIX+NTMARK)*1024*2 + MAX_HEAD_MEM)
|
|---|
| 110 | typedef struct TGB_struct
|
|---|
| 111 | {
|
|---|
| 112 | struct TGB_struct *prev;
|
|---|
| 113 | void *mem;
|
|---|
| 114 | } TGB_entry;
|
|---|
| 115 |
|
|---|
| 116 | TGB_entry *tgb_last = NULL;
|
|---|
| 117 | uint64_t tgb_memory = 0;
|
|---|
| 118 | uint64_t tgb_inuse = 0;
|
|---|
| 119 |
|
|---|
| 120 | void *TGB_Malloc()
|
|---|
| 121 | {
|
|---|
| 122 | // No free slot available, next alloc would exceed max memory
|
|---|
| 123 | if (!tgb_last && tgb_memory+MAX_TOT_MEM>g_maxMem)
|
|---|
| 124 | return NULL;
|
|---|
| 125 |
|
|---|
| 126 | // We will return this amount of memory
|
|---|
| 127 | tgb_inuse += MAX_TOT_MEM;
|
|---|
| 128 | gj.bufTot++;
|
|---|
| 129 |
|
|---|
| 130 | // No free slot available, allocate a new one
|
|---|
| 131 | if (!tgb_last)
|
|---|
| 132 | {
|
|---|
| 133 | tgb_memory += MAX_TOT_MEM;
|
|---|
| 134 | return malloc(MAX_TOT_MEM);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | // Get the next free slot from the stack and return it
|
|---|
| 138 | TGB_entry *last = tgb_last;
|
|---|
| 139 |
|
|---|
| 140 | void *mem = last->mem;
|
|---|
| 141 | tgb_last = last->prev;
|
|---|
| 142 |
|
|---|
| 143 | free(last);
|
|---|
| 144 |
|
|---|
| 145 | return mem;
|
|---|
| 146 | };
|
|---|
| 147 |
|
|---|
| 148 | void TGB_free(void *mem)
|
|---|
| 149 | {
|
|---|
| 150 | if (!mem)
|
|---|
| 151 | return;
|
|---|
| 152 |
|
|---|
| 153 | // Add the last free slot to the stack
|
|---|
| 154 | TGB_entry *entry = (TGB_entry*)malloc(sizeof(TGB_entry));
|
|---|
| 155 |
|
|---|
| 156 | // FIXME: Really free memory if memory usuage exceeds g_maxMem
|
|---|
| 157 |
|
|---|
| 158 | entry->prev = tgb_last;
|
|---|
| 159 | entry->mem = mem;
|
|---|
| 160 |
|
|---|
| 161 | tgb_last = entry;
|
|---|
| 162 |
|
|---|
| 163 | // Decrease the amont of memory in use accordingly
|
|---|
| 164 | tgb_inuse -= MAX_TOT_MEM;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | RUN_CTRL runCtrl[MAX_RUN];
|
|---|
| 168 |
|
|---|
| 169 | /*
|
|---|
| 170 | *** Definition of rdBuffer to read in IP packets; keep it global !!!!
|
|---|
| 171 | */
|
|---|
| 172 |
|
|---|
| 173 | typedef union
|
|---|
| 174 | {
|
|---|
| 175 | uint8_t B[MAX_LEN];
|
|---|
| 176 | uint16_t S[MAX_LEN / 2];
|
|---|
| 177 | uint32_t I[MAX_LEN / 4];
|
|---|
| 178 | uint64_t L[MAX_LEN / 8];
|
|---|
| 179 | } CNV_FACT;
|
|---|
| 180 |
|
|---|
| 181 | typedef struct
|
|---|
| 182 | {
|
|---|
| 183 | int bufTyp; //what are we reading at the moment: 0=header 1=data -1=skip ...
|
|---|
| 184 | int32_t bufPos; //next byte to read to the buffer next
|
|---|
| 185 | int32_t bufLen; //number of bytes left to read
|
|---|
| 186 | int32_t skip; //number of bytes skipped before start of event
|
|---|
| 187 |
|
|---|
| 188 | int errCnt; //how often connect failed since last successful
|
|---|
| 189 | int sockStat; //-1 if socket not yet connected , 99 if not exist
|
|---|
| 190 | int socket; //contains the sockets
|
|---|
| 191 |
|
|---|
| 192 | struct sockaddr_in SockAddr; //IP for each socket
|
|---|
| 193 |
|
|---|
| 194 | int evtID; // event ID of event currently read
|
|---|
| 195 | int runID; // run "
|
|---|
| 196 | int ftmID; // event ID from FTM
|
|---|
| 197 | uint fadLen; // FADlength of event currently read
|
|---|
| 198 | int fadVers; // Version of FAD
|
|---|
| 199 | int ftmTyp; // trigger type
|
|---|
| 200 | int Port;
|
|---|
| 201 |
|
|---|
| 202 | CNV_FACT *rBuf;
|
|---|
| 203 |
|
|---|
| 204 | } READ_STRUCT;
|
|---|
| 205 |
|
|---|
| 206 | /*-----------------------------------------------------------------*/
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 | /*-----------------------------------------------------------------*/
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 | int
|
|---|
| 213 | GenSock (int flag, int sid, int port, struct sockaddr_in *sockAddr,
|
|---|
| 214 | READ_STRUCT * rs)
|
|---|
| 215 | {
|
|---|
| 216 | /*
|
|---|
| 217 | *** generate Address, create sockets and allocates readbuffer for it
|
|---|
| 218 | ***
|
|---|
| 219 | *** if flag==0 generate socket and buffer
|
|---|
| 220 | *** <0 destroy socket and buffer
|
|---|
| 221 | *** >0 close and redo socket
|
|---|
| 222 | ***
|
|---|
| 223 | *** sid : board*7 + port id
|
|---|
| 224 | */
|
|---|
| 225 |
|
|---|
| 226 | //close socket if open
|
|---|
| 227 | if (rs->sockStat == 0)
|
|---|
| 228 | {
|
|---|
| 229 | if (close (rs->socket) > 0) {
|
|---|
| 230 | factPrintf(kFatal, 771, "Closing socket %d failed: %m (close,rc=%d)", sid, errno);
|
|---|
| 231 | } else {
|
|---|
| 232 | factPrintf(kInfo, 771, "Succesfully closed socket %d", sid);
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | rs->sockStat = 99;
|
|---|
| 237 |
|
|---|
| 238 | if (flag < 0) {
|
|---|
| 239 | free (rs->rBuf); //and never open again
|
|---|
| 240 | rs->rBuf = NULL;
|
|---|
| 241 | return 0;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 | if (flag == 0) { //generate address and buffer ...
|
|---|
| 246 | rs->Port = port;
|
|---|
| 247 | rs->SockAddr.sin_family = sockAddr->sin_family;
|
|---|
| 248 | rs->SockAddr.sin_port = htons (port);
|
|---|
| 249 | rs->SockAddr.sin_addr = sockAddr->sin_addr;
|
|---|
| 250 |
|
|---|
| 251 | rs->rBuf = (CNV_FACT*)malloc (sizeof (CNV_FACT));
|
|---|
| 252 | if (rs->rBuf == NULL) {
|
|---|
| 253 | factPrintf(kFatal, 774, "Could not create local buffer %d (malloc failed)", sid);
|
|---|
| 254 | rs->sockStat = 77;
|
|---|
| 255 | return -3;
|
|---|
| 256 | }
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 | if ((rs->socket = socket (PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) <= 0) {
|
|---|
| 261 | factPrintf(kFatal, 773, "Generating socket %d failed: %m (socket,rc=%d)", sid, errno);
|
|---|
| 262 | rs->sockStat = 88;
|
|---|
| 263 | return -2;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | int optval = 1;
|
|---|
| 267 | if (setsockopt (rs->socket, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(int)) < 0) {
|
|---|
| 268 | factPrintf(kInfo, 173, "Setting SO_KEEPALIVE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
|
|---|
| 269 | }
|
|---|
| 270 | optval = 10; //start after 10 seconds
|
|---|
| 271 | if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPIDLE, &optval, sizeof(int)) < 0) {
|
|---|
| 272 | factPrintf(kInfo, 173, "Setting TCP_KEEPIDLE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
|
|---|
| 273 | }
|
|---|
| 274 | optval = 10; //do every 10 seconds
|
|---|
| 275 | if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPINTVL, &optval, sizeof(int)) < 0) {
|
|---|
| 276 | factPrintf(kInfo, 173, "Setting TCP_KEEPINTVL for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
|
|---|
| 277 | }
|
|---|
| 278 | optval = 2; //close after 2 unsuccessful tries
|
|---|
| 279 | if (setsockopt (rs->socket, SOL_TCP, TCP_KEEPCNT, &optval, sizeof(int)) < 0) {
|
|---|
| 280 | factPrintf(kInfo, 173, "Setting TCP_KEEPCNT for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | factPrintf(kInfo, 773, "Successfully generated socket %d", sid);
|
|---|
| 284 |
|
|---|
| 285 | rs->sockStat = -1; //try to (re)open socket
|
|---|
| 286 | rs->errCnt = 0;
|
|---|
| 287 | return 0;
|
|---|
| 288 |
|
|---|
| 289 | } /*-----------------------------------------------------------------*/
|
|---|
| 290 |
|
|---|
| 291 | /*-----------------------------------------------------------------*/
|
|---|
| 292 |
|
|---|
| 293 | int checkRoiConsistency(const CNV_FACT *rbuf, int roi[])
|
|---|
| 294 | {
|
|---|
| 295 | int xjr = -1;
|
|---|
| 296 | int xkr = -1;
|
|---|
| 297 |
|
|---|
| 298 | //points to the very first roi
|
|---|
| 299 | int roiPtr = sizeof(PEVNT_HEADER)/2 + 2;
|
|---|
| 300 |
|
|---|
| 301 | roi[0] = ntohs(rbuf->S[roiPtr]);
|
|---|
| 302 |
|
|---|
| 303 | for (int jr = 0; jr < 9; jr++)
|
|---|
| 304 | {
|
|---|
| 305 | roi[jr] = ntohs(rbuf->S[roiPtr]);
|
|---|
| 306 |
|
|---|
| 307 | if (roi[jr]<0 || roi[jr]>1024)
|
|---|
| 308 | {
|
|---|
| 309 | factPrintf(kError, 999, "Illegal roi in channel %d (allowed: 0<=roi<=1024)", jr, roi[jr]);
|
|---|
| 310 | return 0;
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | // Check that the roi of pixels jr are compatible with the one of pixel 0
|
|---|
| 314 | if (jr!=8 && roi[jr]!=roi[0])
|
|---|
| 315 | {
|
|---|
| 316 | xjr = jr;
|
|---|
| 317 | break;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | // Check that the roi of all other DRS chips on boards are compatible
|
|---|
| 321 | for (int kr = 1; kr < 4; kr++)
|
|---|
| 322 | {
|
|---|
| 323 | const int kroi = ntohs(rbuf->S[roiPtr]);
|
|---|
| 324 | if (kroi != roi[jr])
|
|---|
| 325 | {
|
|---|
| 326 | xjr = jr;
|
|---|
| 327 | xkr = kr;
|
|---|
| 328 | break;
|
|---|
| 329 | }
|
|---|
| 330 | roiPtr += kroi+4;
|
|---|
| 331 | }
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | if (xjr>=0)
|
|---|
| 335 | {
|
|---|
| 336 | if (xkr<0)
|
|---|
| 337 | factPrintf(kFatal, 1, "Inconsistent Roi accross chips [DRS=%d], expected %d, got %d", xjr, roi[0], roi[xjr]);
|
|---|
| 338 | else
|
|---|
| 339 | factPrintf(kFatal, 1, "Inconsistent Roi accross channels [DRS=%d Ch=%d], expected %d, got %d", xjr, xkr, roi[xjr], ntohs(rbuf->S[roiPtr]));
|
|---|
| 340 |
|
|---|
| 341 | return 0;
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | if (roi[8] < roi[0])
|
|---|
| 345 | {
|
|---|
| 346 | 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]);
|
|---|
| 347 | //gj.badRoiB++;
|
|---|
| 348 | //gj.badRoi[b]++;
|
|---|
| 349 | return 0;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | return 1;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | int mBufEvt(const READ_STRUCT *rs)
|
|---|
| 356 | {
|
|---|
| 357 | int nRoi[9];
|
|---|
| 358 | if (!checkRoiConsistency(rs->rBuf, nRoi))
|
|---|
| 359 | return -9999;
|
|---|
| 360 |
|
|---|
| 361 | const int evID = rs->evtID;
|
|---|
| 362 | const uint runID = rs->runID;
|
|---|
| 363 | const int trgTyp = rs->ftmTyp;
|
|---|
| 364 | const int trgNum = rs->ftmID;
|
|---|
| 365 | const int fadNum = rs->evtID;
|
|---|
| 366 |
|
|---|
| 367 | const int beg = (evtCtrl_lastPtr + MAX_EVT - 1) % MAX_EVT;
|
|---|
| 368 | const int end = (evtCtrl_frstPtr + MAX_EVT - 1) % MAX_EVT;
|
|---|
| 369 |
|
|---|
| 370 | for (int k=beg; k!=end; k=(k+MAX_EVT-1)%MAX_EVT)
|
|---|
| 371 | {
|
|---|
| 372 | // If the run is different, go on searching.
|
|---|
| 373 | // We cannot stop searching if a lower run-id is found as in
|
|---|
| 374 | // the case of the events, because theoretically, there
|
|---|
| 375 | // can be the same run on two different days.
|
|---|
| 376 | if (runID != evtCtrl[k].runNum)
|
|---|
| 377 | continue;
|
|---|
| 378 |
|
|---|
| 379 | // If the ID of the new event if higher than the last one stored
|
|---|
| 380 | // in that run, we have to assign a new slot (leave the loop)
|
|---|
| 381 | if (evID > evtCtrl[k].evNum/* && runID == evtCtrl[k].runNum*/)
|
|---|
| 382 | break;
|
|---|
| 383 |
|
|---|
| 384 | if (evID != evtCtrl[k].evNum/* || runID != evtCtrl[k].runNum*/)
|
|---|
| 385 | continue;
|
|---|
| 386 |
|
|---|
| 387 | // We have found an entry with the same runID and evtID
|
|---|
| 388 | // Check if ROI is consistent
|
|---|
| 389 | if (evtCtrl[k].nRoi != nRoi[0] || evtCtrl[k].nRoiTM != nRoi[8])
|
|---|
| 390 | {
|
|---|
| 391 | factPrintf(kError, 821, "Mismatch of roi within event. Expected roi=%d and roi_tm=%d, got %d and %d.",
|
|---|
| 392 | evtCtrl[k].nRoi, evtCtrl[k].nRoiTM, nRoi[0], nRoi[8]);
|
|---|
| 393 | return -8201;
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | // count for inconsistencies
|
|---|
| 397 | if (evtCtrl[k].trgNum != trgNum)
|
|---|
| 398 | evtCtrl[k].Errors[0]++;
|
|---|
| 399 | if (evtCtrl[k].fadNum != fadNum)
|
|---|
| 400 | evtCtrl[k].Errors[1]++;
|
|---|
| 401 | if (evtCtrl[k].trgTyp != trgTyp)
|
|---|
| 402 | evtCtrl[k].Errors[2]++;
|
|---|
| 403 |
|
|---|
| 404 | //everything seems fine so far ==> use this slot ....
|
|---|
| 405 | return k;
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | // Check if the control structure still has space left
|
|---|
| 409 | if (end-beg==1 || (end==0 && beg==MAX_EVT-1))
|
|---|
| 410 | {
|
|---|
| 411 | factPrintf(kError, 881, "No control slot to keep event %d (run %d) %d %d", evID, runID, beg, end);
|
|---|
| 412 | return -1;
|
|---|
| 413 | }
|
|---|
| 414 |
|
|---|
| 415 | // If we have already queued at least one event,
|
|---|
| 416 | // check the roi if the previous event
|
|---|
| 417 | // already belongs to the same run.
|
|---|
| 418 |
|
|---|
| 419 | // Get the runCtrl entry of the previous event.
|
|---|
| 420 | // If none is in the queue (we are anyhow super fast)
|
|---|
| 421 | // just get the correct entry from the runCtrl array.
|
|---|
| 422 | int idx = evtCtrl[beg].runCtrl_idx;
|
|---|
| 423 |
|
|---|
| 424 | // If there is an event in the queue and it has the same runID, we can use
|
|---|
| 425 | // that event to check for the roi consistency throughout the run
|
|---|
| 426 | if (evtCtrl_frstPtr!=evtCtrl_lastPtr && runCtrl[idx].runId==runID)
|
|---|
| 427 | {
|
|---|
| 428 | // Check if run already registered (old entries should have runId==-1)
|
|---|
| 429 | if (runCtrl[idx].roi0 != nRoi[0] || runCtrl[idx].roi8 != nRoi[8])
|
|---|
| 430 | {
|
|---|
| 431 | factPrintf(kError, 931, "Mismatch of roi within run. Expected roi=%d and roi_tm=%d, got %d and %d (runID=%d, evID=%d)",
|
|---|
| 432 | runCtrl[idx].roi0, runCtrl[idx].roi8, nRoi[0], nRoi[8], runID, evID);
|
|---|
| 433 | return -9301;
|
|---|
| 434 | }
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | // If there is none in the queue, we have to search for the correct entry
|
|---|
| 438 | if (evtCtrl_frstPtr==evtCtrl_lastPtr)
|
|---|
| 439 | {
|
|---|
| 440 | idx = -1;
|
|---|
| 441 |
|
|---|
| 442 | for (int k=0; k<MAX_RUN; k++)
|
|---|
| 443 | {
|
|---|
| 444 | if (runCtrl[k].runId==runID)
|
|---|
| 445 | {
|
|---|
| 446 | idx = k;
|
|---|
| 447 | break;
|
|---|
| 448 | }
|
|---|
| 449 | }
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | struct timeval tv;
|
|---|
| 453 | gettimeofday(&tv, NULL);
|
|---|
| 454 |
|
|---|
| 455 | const uint32_t tsec = tv.tv_sec;
|
|---|
| 456 | const uint32_t tusec = tv.tv_usec;
|
|---|
| 457 |
|
|---|
| 458 | // Run not yet registered, register run
|
|---|
| 459 | // If we haven't found a corresponding entry in the queue, or the runId has changed
|
|---|
| 460 | // we find the oldest empty entry in the runCtrl array and create a new runCtrl entry
|
|---|
| 461 | if (idx<0 || runCtrl[idx].runId!=runID)
|
|---|
| 462 | {
|
|---|
| 463 | // If there is none in the queue or
|
|---|
| 464 | idx = -1;
|
|---|
| 465 |
|
|---|
| 466 | uint oldest = g_actTime + 1000;
|
|---|
| 467 | for (int k=0; k<MAX_RUN; k++)
|
|---|
| 468 | {
|
|---|
| 469 | // This is just for sanity. We use the oldest free entry (until
|
|---|
| 470 | // we have understood the concept and can use "just" a free entry
|
|---|
| 471 | if (runCtrl[k].runId==0 && runCtrl[k].closeTime < oldest)
|
|---|
| 472 | {
|
|---|
| 473 | oldest = runCtrl[k].closeTime;
|
|---|
| 474 | idx = k;
|
|---|
| 475 | }
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 | if (idx<0)
|
|---|
| 479 | {
|
|---|
| 480 | factPrintf(kFatal, 883, "Not able to register the new run %d", runID);
|
|---|
| 481 | return -1001;
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 | factPrintf(kInfo, 503, "New run %d (evt=%d, idx=%d) registered with roi=%d and roi_tm=%d",
|
|---|
| 485 | runID, evID, idx, nRoi[0], nRoi[8]);
|
|---|
| 486 |
|
|---|
| 487 | runCtrl[idx].runId = runID;
|
|---|
| 488 | runCtrl[idx].roi0 = nRoi[0]; // FIXME: Make obsolete!
|
|---|
| 489 | runCtrl[idx].roi8 = nRoi[8]; // FIXME: Make obsolete!
|
|---|
| 490 | runCtrl[idx].fileId = -2;
|
|---|
| 491 | runCtrl[idx].lastEvt = 1; // Number of events partially started to read
|
|---|
| 492 | runCtrl[idx].actEvt = 0; // Number of written events (write)
|
|---|
| 493 | runCtrl[idx].procEvt = 0; // Number of successfully checked events (checkEvent)
|
|---|
| 494 | runCtrl[idx].maxEvt = 999999999; // max number events allowed
|
|---|
| 495 | runCtrl[idx].lastTime = tsec; // Time when the last event was written
|
|---|
| 496 | runCtrl[idx].closeTime = tsec + 3600 * 24; //max time allowed
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | // The new entry which is populated is the one lastPtr is pointing to
|
|---|
| 500 | const int k = evtCtrl_lastPtr;
|
|---|
| 501 |
|
|---|
| 502 | //flag all boards as unused
|
|---|
| 503 | evtCtrl[k].nBoard = 0;
|
|---|
| 504 | for (int b=0; b<NBOARDS; b++)
|
|---|
| 505 | evtCtrl[k].board[b] = -1;
|
|---|
| 506 |
|
|---|
| 507 | evtCtrl[k].runCtrl_idx = idx;
|
|---|
| 508 | evtCtrl[k].pcTime[0] = tsec;
|
|---|
| 509 | evtCtrl[k].pcTime[1] = tusec;
|
|---|
| 510 | evtCtrl[k].nRoi = nRoi[0];
|
|---|
| 511 | evtCtrl[k].nRoiTM = nRoi[8];
|
|---|
| 512 | evtCtrl[k].evNum = evID;
|
|---|
| 513 | evtCtrl[k].runNum = runID;
|
|---|
| 514 | evtCtrl[k].fadNum = fadNum;
|
|---|
| 515 | evtCtrl[k].trgNum = trgNum;
|
|---|
| 516 | evtCtrl[k].trgTyp = trgTyp;
|
|---|
| 517 | evtCtrl[k].Errors[0] = 0;
|
|---|
| 518 | evtCtrl[k].Errors[1] = 0;
|
|---|
| 519 | evtCtrl[k].Errors[2] = 0;
|
|---|
| 520 | evtCtrl[k].Errors[3] = 0;
|
|---|
| 521 | evtCtrl[k].fEvent = NULL;
|
|---|
| 522 | evtCtrl[k].FADhead = NULL;
|
|---|
| 523 |
|
|---|
| 524 | // -1: kInValid
|
|---|
| 525 | // 0: kValid
|
|---|
| 526 | // 1-40: kIncomplete
|
|---|
| 527 | // 90: kIncompleteReported
|
|---|
| 528 | // 100: kCompleteEventInBuffer
|
|---|
| 529 | // 1000+x: kToBeProcessedByThreadX
|
|---|
| 530 | // 5000: kToBeWritten
|
|---|
| 531 | // 10000: kToBeDeleted
|
|---|
| 532 |
|
|---|
| 533 | evtCtrl[k].evtStat = 0;
|
|---|
| 534 |
|
|---|
| 535 | // This is dangerous, because theoretically, it can result is
|
|---|
| 536 | // acessing invalid memory in another thread if this is split
|
|---|
| 537 | // in two instructions. Must be done only _after_ the contents
|
|---|
| 538 | // have been initialized
|
|---|
| 539 | evtCtrl_lastPtr = (evtCtrl_lastPtr+1) % MAX_EVT;
|
|---|
| 540 |
|
|---|
| 541 | return k;
|
|---|
| 542 |
|
|---|
| 543 | } /*-----------------------------------------------------------------*/
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 | void initEvent(int i)
|
|---|
| 547 | {
|
|---|
| 548 | evtCtrl[i].fEvent = (EVENT*)((char*)evtCtrl[i].FADhead+MAX_HEAD_MEM);
|
|---|
| 549 | memset(evtCtrl[i].fEvent->Adc_Data, 0, (NPIX+NTMARK)*2*evtCtrl[i].nRoi);
|
|---|
| 550 |
|
|---|
| 551 | //flag all pixels as unused
|
|---|
| 552 | for (int k = 0; k < NPIX; k++)
|
|---|
| 553 | evtCtrl[i].fEvent->StartPix[k] = -1;
|
|---|
| 554 |
|
|---|
| 555 | //flag all TMark as unused
|
|---|
| 556 | for (int k = 0; k < NTMARK; k++)
|
|---|
| 557 | evtCtrl[i].fEvent->StartTM[k] = -1;
|
|---|
| 558 |
|
|---|
| 559 | evtCtrl[i].fEvent->NumBoards = 0;
|
|---|
| 560 | evtCtrl[i].fEvent->SoftTrig = 0;
|
|---|
| 561 | evtCtrl[i].fEvent->PCTime = evtCtrl[i].pcTime[0];
|
|---|
| 562 | evtCtrl[i].fEvent->PCUsec = evtCtrl[i].pcTime[1];
|
|---|
| 563 | evtCtrl[i].fEvent->Roi = evtCtrl[i].nRoi;
|
|---|
| 564 | evtCtrl[i].fEvent->RoiTM = evtCtrl[i].nRoiTM;
|
|---|
| 565 | evtCtrl[i].fEvent->EventNum = evtCtrl[i].evNum;
|
|---|
| 566 | evtCtrl[i].fEvent->TriggerNum = evtCtrl[i].trgNum;
|
|---|
| 567 | evtCtrl[i].fEvent->TriggerType = evtCtrl[i].trgTyp;
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 | void mBufFree (int i)
|
|---|
| 572 | {
|
|---|
| 573 | TGB_free(evtCtrl[i].FADhead);
|
|---|
| 574 |
|
|---|
| 575 | evtCtrl[i].fEvent = NULL;
|
|---|
| 576 | evtCtrl[i].FADhead = NULL;
|
|---|
| 577 |
|
|---|
| 578 | evtCtrl[i].evNum = evtCtrl[i].nRoi = -1;
|
|---|
| 579 | evtCtrl[i].runNum = 0;
|
|---|
| 580 |
|
|---|
| 581 | gj.usdMem = tgb_inuse;
|
|---|
| 582 |
|
|---|
| 583 | gj.bufTot--;
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | uint64_t reportIncomplete(int id, const char *txt)
|
|---|
| 587 | {
|
|---|
| 588 | factPrintf(kWarn, 601, "skip incomplete evt (run=%d, evt=%d, %s)",
|
|---|
| 589 | evtCtrl[id].runNum, evtCtrl[id].evNum, txt);
|
|---|
| 590 |
|
|---|
| 591 | uint64_t report = 0;
|
|---|
| 592 |
|
|---|
| 593 | char str[1000];
|
|---|
| 594 |
|
|---|
| 595 | int ik=0;
|
|---|
| 596 | for (int ib=0; ib<NBOARDS; ib++)
|
|---|
| 597 | {
|
|---|
| 598 | if (ib%10==0)
|
|---|
| 599 | str[ik++] = '|';
|
|---|
| 600 |
|
|---|
| 601 | const int jb = evtCtrl[id].board[ib];
|
|---|
| 602 | if (jb>=0) // data received from that board
|
|---|
| 603 | {
|
|---|
| 604 | str[ik++] = '0'+(jb%10);
|
|---|
| 605 | continue;
|
|---|
| 606 | }
|
|---|
| 607 |
|
|---|
| 608 | // FIXME: This is not synchronous... it reports
|
|---|
| 609 | // accoridng to the current connection status, not w.r.t. to the
|
|---|
| 610 | // one when the event was taken.
|
|---|
| 611 | if (gi_NumConnect[ib]<=0) // board not connected
|
|---|
| 612 | {
|
|---|
| 613 | str[ik++] = 'x';
|
|---|
| 614 | continue;
|
|---|
| 615 | }
|
|---|
| 616 |
|
|---|
| 617 | // data from this board lost
|
|---|
| 618 | str[ik++] = '.';
|
|---|
| 619 | report |= ((uint64_t)1)<<ib;
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | str[ik++] = '|';
|
|---|
| 623 | str[ik] = 0;
|
|---|
| 624 |
|
|---|
| 625 | factOut(kWarn, 601, str);
|
|---|
| 626 |
|
|---|
| 627 | return report;
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | // i == board
|
|---|
| 631 | void copyData(CNV_FACT *rbuf, int i, int evID)
|
|---|
| 632 | {
|
|---|
| 633 | // swapEventHeaderBytes: End of the header. to channels now
|
|---|
| 634 | int eStart = 36;
|
|---|
| 635 | for (int ePatchesCount = 0; ePatchesCount<4*9; ePatchesCount++)
|
|---|
| 636 | {
|
|---|
| 637 | rbuf->S[eStart+0] = ntohs(rbuf->S[eStart+0]);//id
|
|---|
| 638 | rbuf->S[eStart+1] = ntohs(rbuf->S[eStart+1]);//start_cell
|
|---|
| 639 | rbuf->S[eStart+2] = ntohs(rbuf->S[eStart+2]);//roi
|
|---|
| 640 | rbuf->S[eStart+3] = ntohs(rbuf->S[eStart+3]);//filling
|
|---|
| 641 |
|
|---|
| 642 | eStart += 4+rbuf->S[eStart+2];//skip the pixel data
|
|---|
| 643 | }
|
|---|
| 644 |
|
|---|
| 645 | memcpy(&evtCtrl[evID].FADhead[i], rbuf, sizeof(PEVNT_HEADER));
|
|---|
| 646 |
|
|---|
| 647 | int src = sizeof(PEVNT_HEADER) / 2;
|
|---|
| 648 |
|
|---|
| 649 | // consistency of ROIs have been checked already (is it all correct?)
|
|---|
| 650 | const int roi = rbuf->S[src+2];
|
|---|
| 651 |
|
|---|
| 652 | // different sort in FAD board.....
|
|---|
| 653 | for (int px = 0; px < 9; px++)
|
|---|
| 654 | {
|
|---|
| 655 | for (int drs = 0; drs < 4; drs++)
|
|---|
| 656 | {
|
|---|
| 657 | // pixH = rd[i].rBuf->S[src++]; // ID
|
|---|
| 658 | src++;
|
|---|
| 659 |
|
|---|
| 660 | const int pixC = rbuf->S[src++]; // start-cell
|
|---|
| 661 | const int pixR = rbuf->S[src++]; // roi
|
|---|
| 662 | //here we should check if pixH is correct ....
|
|---|
| 663 |
|
|---|
| 664 | const int pixS = i * 36 + drs * 9 + px;
|
|---|
| 665 | src++;
|
|---|
| 666 |
|
|---|
| 667 | evtCtrl[evID].fEvent->StartPix[pixS] = pixC;
|
|---|
| 668 |
|
|---|
| 669 | const int dest1 = pixS * roi;
|
|---|
| 670 | memcpy(&evtCtrl[evID].fEvent->Adc_Data[dest1], &rbuf->S[src], roi * 2);
|
|---|
| 671 |
|
|---|
| 672 | src += pixR;
|
|---|
| 673 |
|
|---|
| 674 | if (px == 8)
|
|---|
| 675 | {
|
|---|
| 676 | const int tmS = i * 4 + drs;
|
|---|
| 677 |
|
|---|
| 678 | //and we have additional TM info
|
|---|
| 679 | if (pixR > roi)
|
|---|
| 680 | {
|
|---|
| 681 | const int dest2 = tmS * roi + NPIX * roi;
|
|---|
| 682 |
|
|---|
| 683 | const int srcT = src - roi;
|
|---|
| 684 | evtCtrl[evID].fEvent->StartTM[tmS] = (pixC + pixR - roi) % 1024;
|
|---|
| 685 |
|
|---|
| 686 | memcpy(&evtCtrl[evID].fEvent->Adc_Data[dest2], &rbuf->S[srcT], roi * 2);
|
|---|
| 687 | }
|
|---|
| 688 | else
|
|---|
| 689 | {
|
|---|
| 690 | evtCtrl[evID].fEvent->StartTM[tmS] = -1;
|
|---|
| 691 | }
|
|---|
| 692 | }
|
|---|
| 693 | }
|
|---|
| 694 | }
|
|---|
| 695 | }
|
|---|
| 696 |
|
|---|
| 697 |
|
|---|
| 698 | void *readFAD (void *ptr)
|
|---|
| 699 | {
|
|---|
| 700 | /* *** main loop reading FAD data and sorting them to complete events */
|
|---|
| 701 |
|
|---|
| 702 | factPrintf(kInfo, -1, "Start initializing (readFAD)");
|
|---|
| 703 |
|
|---|
| 704 | READ_STRUCT rd[NBOARDS]; //buffer to read IP and afterwards store in mBuffer
|
|---|
| 705 |
|
|---|
| 706 | uint32_t actrun = 0;
|
|---|
| 707 |
|
|---|
| 708 | const int minLen = sizeof(PEVNT_HEADER); //min #bytes needed to check header: full header for debug
|
|---|
| 709 |
|
|---|
| 710 |
|
|---|
| 711 | /* initialize run control logics */
|
|---|
| 712 | for (int i = 0; i < MAX_RUN; i++) {
|
|---|
| 713 | runCtrl[i].runId = 0;
|
|---|
| 714 | runCtrl[i].fileId = -2;
|
|---|
| 715 | }
|
|---|
| 716 |
|
|---|
| 717 | int gi_reset, gi_resetR, gi_resetS, gi_resetW, gi_resetX;
|
|---|
| 718 | gi_resetS = gi_resetR = 9;
|
|---|
| 719 |
|
|---|
| 720 | int sockDef[NBOARDS]; //internal state of sockets
|
|---|
| 721 | memset(sockDef, 0, NBOARDS*sizeof(int));
|
|---|
| 722 |
|
|---|
| 723 | START:
|
|---|
| 724 | evtCtrl_frstPtr = 0;
|
|---|
| 725 | evtCtrl_lastPtr = 0;
|
|---|
| 726 |
|
|---|
| 727 | //time in seconds
|
|---|
| 728 | uint gi_SecTime = time(NULL);;
|
|---|
| 729 | g_actTime = gi_SecTime;
|
|---|
| 730 |
|
|---|
| 731 | const int cntsock = 8 - NUMSOCK ;
|
|---|
| 732 |
|
|---|
| 733 | if (gi_resetS > 0) {
|
|---|
| 734 | //make sure all sockets are preallocated as 'not exist'
|
|---|
| 735 | for (int i = 0; i < NBOARDS; i++) {
|
|---|
| 736 | rd[i].socket = -1;
|
|---|
| 737 | rd[i].sockStat = 99;
|
|---|
| 738 | }
|
|---|
| 739 |
|
|---|
| 740 | for (int k = 0; k < NBOARDS; k++) {
|
|---|
| 741 | gi_NumConnect[k] = 0;
|
|---|
| 742 | //gi.numConn[k] = 0;
|
|---|
| 743 | gj.numConn[k] = 0;
|
|---|
| 744 | //gj.errConn[k] = 0;
|
|---|
| 745 | gj.rateBytes[k] = 0;
|
|---|
| 746 | gj.totBytes[k] = 0;
|
|---|
| 747 | }
|
|---|
| 748 |
|
|---|
| 749 | }
|
|---|
| 750 |
|
|---|
| 751 | if (gi_resetR > 0)
|
|---|
| 752 | {
|
|---|
| 753 | gj.bufTot = gj.maxEvt = gj.xxxEvt = 0;
|
|---|
| 754 | gj.usdMem = gj.maxMem = gj.xxxMem = 0;
|
|---|
| 755 | gj.totMem = tgb_memory;
|
|---|
| 756 | gj.bufNew = gj.bufEvt = 0;
|
|---|
| 757 | gj.evtSkip = gj.evtWrite = gj.evtErr = 0;
|
|---|
| 758 |
|
|---|
| 759 | // initialize mBuffer (mark all entries as unused\empty)
|
|---|
| 760 | for (int i = 0; i < MAX_EVT; i++)
|
|---|
| 761 | {
|
|---|
| 762 | evtCtrl[i].evNum = evtCtrl[i].nRoi = -1;
|
|---|
| 763 | evtCtrl[i].runNum = 0;
|
|---|
| 764 | evtCtrl[i].evtStat = -1;
|
|---|
| 765 | }
|
|---|
| 766 |
|
|---|
| 767 | evtCtrl_frstPtr = 0;
|
|---|
| 768 | evtCtrl_lastPtr = 0;
|
|---|
| 769 |
|
|---|
| 770 | factPrintf(kInfo, -1, "End initializing (readFAD)");
|
|---|
| 771 | }
|
|---|
| 772 |
|
|---|
| 773 |
|
|---|
| 774 | gi_reset = gi_resetR = gi_resetS = gi_resetW = 0;
|
|---|
| 775 |
|
|---|
| 776 | //loop until global variable g_runStat claims stop
|
|---|
| 777 | while (g_runStat >= 0 && g_reset == 0)
|
|---|
| 778 | {
|
|---|
| 779 | gj.readStat = g_runStat;
|
|---|
| 780 |
|
|---|
| 781 | for (int b = 0; b < NBOARDS; b++)
|
|---|
| 782 | {
|
|---|
| 783 | // Nothing has changed
|
|---|
| 784 | if (g_port[b].sockDef == sockDef[b])
|
|---|
| 785 | continue;
|
|---|
| 786 |
|
|---|
| 787 | gi_NumConnect[b] = 0; //must close all connections
|
|---|
| 788 | gj.numConn[b] = 0;
|
|---|
| 789 |
|
|---|
| 790 | // s0 = 0: sockets to be defined and opened
|
|---|
| 791 | // s0 = -1: sockets to be destroyed
|
|---|
| 792 | // s0 = +1: sockets to be closed and reopened
|
|---|
| 793 |
|
|---|
| 794 | int s0 = 0;
|
|---|
| 795 | if (sockDef[b] != 0)
|
|---|
| 796 | s0 = g_port[b].sockDef==0 ? -1 : +1;
|
|---|
| 797 |
|
|---|
| 798 | const int p0 = s0==0 ? ntohs (g_port[b].sockAddr.sin_port) : 0;
|
|---|
| 799 |
|
|---|
| 800 | GenSock(s0, b, p0+1, &g_port[b].sockAddr, &rd[b]); //generate address and socket
|
|---|
| 801 |
|
|---|
| 802 | sockDef[b] = g_port[b].sockDef;
|
|---|
| 803 | }
|
|---|
| 804 |
|
|---|
| 805 | // count the number of active boards
|
|---|
| 806 | int actBoards = 0;
|
|---|
| 807 | for (int b = 0; b < NBOARDS; b++)
|
|---|
| 808 | if (sockDef[b] > 0)
|
|---|
| 809 | actBoards++;
|
|---|
| 810 |
|
|---|
| 811 | //check all sockets if something to read
|
|---|
| 812 | for (int i = 0; i < NBOARDS; i++)
|
|---|
| 813 | {
|
|---|
| 814 | // Do not try to connect this socket
|
|---|
| 815 | if (rd[i].sockStat > 0)
|
|---|
| 816 | continue;
|
|---|
| 817 |
|
|---|
| 818 | if (rd[i].sockStat == -1)
|
|---|
| 819 | {
|
|---|
| 820 | //try to connect if not yet done
|
|---|
| 821 | rd[i].sockStat = connect (rd[i].socket,
|
|---|
| 822 | (struct sockaddr *) &rd[i].SockAddr,
|
|---|
| 823 | sizeof (rd[i].SockAddr));
|
|---|
| 824 | // Failed
|
|---|
| 825 | if (rd[i].sockStat == -1)
|
|---|
| 826 | {
|
|---|
| 827 | rd[i].errCnt++;
|
|---|
| 828 | usleep(25000);
|
|---|
| 829 | continue;
|
|---|
| 830 | }
|
|---|
| 831 |
|
|---|
| 832 | // Success (rd[i].sockStat == 0)
|
|---|
| 833 |
|
|---|
| 834 | if (sockDef[i] > 0)
|
|---|
| 835 | {
|
|---|
| 836 | rd[i].bufTyp = 0; // expect a header
|
|---|
| 837 | rd[i].bufLen = sizeof(PEVNT_HEADER); // max size to read at begining
|
|---|
| 838 | }
|
|---|
| 839 | else
|
|---|
| 840 | {
|
|---|
| 841 | rd[i].bufTyp = -1; // full data to be skipped
|
|---|
| 842 | rd[i].bufLen = MAX_LEN; // huge for skipping
|
|---|
| 843 | }
|
|---|
| 844 |
|
|---|
| 845 | rd[i].bufPos = 0; // no byte read so far
|
|---|
| 846 | rd[i].skip = 0; // start empty
|
|---|
| 847 |
|
|---|
| 848 | gi_NumConnect[i] += cntsock;
|
|---|
| 849 | gj.numConn[i]++;
|
|---|
| 850 |
|
|---|
| 851 | factPrintf(kInfo, -1, "New connection %d (number of connections: %d)", i, gj.numConn[i]);
|
|---|
| 852 | }
|
|---|
| 853 |
|
|---|
| 854 | // Do not read from this socket
|
|---|
| 855 | if (rd[i].bufLen<0)
|
|---|
| 856 | continue;
|
|---|
| 857 |
|
|---|
| 858 | if (rd[i].bufLen>0)
|
|---|
| 859 | {
|
|---|
| 860 | const int32_t jrd =
|
|---|
| 861 | recv(rd[i].socket, &rd[i].rBuf->B[rd[i].bufPos],
|
|---|
| 862 | rd[i].bufLen, MSG_DONTWAIT);
|
|---|
| 863 |
|
|---|
| 864 | // recv failed
|
|---|
| 865 | if (jrd<0)
|
|---|
| 866 | {
|
|---|
| 867 | // There was just nothing waiting
|
|---|
| 868 | if (errno==EWOULDBLOCK || errno==EAGAIN)
|
|---|
| 869 | continue;
|
|---|
| 870 |
|
|---|
| 871 | factPrintf(kError, 442, "Reading from socket %d failed: %m (recv,rc=%d)", i, errno);
|
|---|
| 872 | continue;
|
|---|
| 873 | }
|
|---|
| 874 |
|
|---|
| 875 | // connection was closed ...
|
|---|
| 876 | if (jrd==0)
|
|---|
| 877 | {
|
|---|
| 878 | factPrintf(kInfo, 441, "Socket %d closed by FAD", i);
|
|---|
| 879 |
|
|---|
| 880 | const int s0 = sockDef[i] > 0 ? +1 : -1;
|
|---|
| 881 | GenSock(s0, i, 0, NULL, &rd[i]);
|
|---|
| 882 |
|
|---|
| 883 | gi_NumConnect[i]-= cntsock ;
|
|---|
| 884 | gj.numConn[i]--;
|
|---|
| 885 |
|
|---|
| 886 | continue;
|
|---|
| 887 | }
|
|---|
| 888 |
|
|---|
| 889 | gj.rateBytes[i] += jrd;
|
|---|
| 890 |
|
|---|
| 891 | // are we skipping this board ...
|
|---|
| 892 | if (rd[i].bufTyp < 0)
|
|---|
| 893 | continue;
|
|---|
| 894 |
|
|---|
| 895 | rd[i].bufPos += jrd; //==> prepare for continuation
|
|---|
| 896 | rd[i].bufLen -= jrd;
|
|---|
| 897 |
|
|---|
| 898 | #ifdef EVTDEBUG
|
|---|
| 899 | debugRead(i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, rd[i].bufTyp, tv.tv_sec, tv.tv_usec); // i=socket; jrd=#bytes; ievt=eventid; 1=finished event
|
|---|
| 900 | #endif
|
|---|
| 901 | }
|
|---|
| 902 |
|
|---|
| 903 | //we are reading event header
|
|---|
| 904 | if (rd[i].bufTyp <= 0)
|
|---|
| 905 | {
|
|---|
| 906 | //not yet sufficient data to take action
|
|---|
| 907 | if (rd[i].bufPos < minLen)
|
|---|
| 908 | continue;
|
|---|
| 909 |
|
|---|
| 910 | //check if startflag correct; else shift block ....
|
|---|
| 911 | // FIXME: This is not enough... this combination of
|
|---|
| 912 | // bytes can be anywhere... at least the end bytes
|
|---|
| 913 | // must be checked somewhere, too.
|
|---|
| 914 | int k;
|
|---|
| 915 | for (k = 0; k < rd[i].bufPos - 1; k++)
|
|---|
| 916 | {
|
|---|
| 917 | //start.S = 0xFB01;
|
|---|
| 918 | if (rd[i].rBuf->B[k] == 0xfb && rd[i].rBuf->B[k+1] == 0x01)
|
|---|
| 919 | break;
|
|---|
| 920 | }
|
|---|
| 921 | rd[i].skip += k;
|
|---|
| 922 |
|
|---|
| 923 | //no start of header found
|
|---|
| 924 | if (k >= rd[i].bufPos - 1)
|
|---|
| 925 | {
|
|---|
| 926 | rd[i].rBuf->B[0] = rd[i].rBuf->B[rd[i].bufPos];
|
|---|
| 927 | rd[i].bufPos = 1;
|
|---|
| 928 | rd[i].bufLen = sizeof(PEVNT_HEADER)-1;
|
|---|
| 929 | continue;
|
|---|
| 930 | }
|
|---|
| 931 |
|
|---|
| 932 | if (k > 0)
|
|---|
| 933 | {
|
|---|
| 934 | rd[i].bufPos -= k;
|
|---|
| 935 | rd[i].bufLen += k;
|
|---|
| 936 | memmove (&rd[i].rBuf->B[0], &rd[i].rBuf->B[k],
|
|---|
| 937 | rd[i].bufPos);
|
|---|
| 938 | }
|
|---|
| 939 |
|
|---|
| 940 | if (rd[i].bufPos < minLen)
|
|---|
| 941 | continue;
|
|---|
| 942 |
|
|---|
| 943 | if (rd[i].skip > 0)
|
|---|
| 944 | {
|
|---|
| 945 | factPrintf(kInfo, 666, "Skipped %d bytes on port %d", rd[i].skip, i);
|
|---|
| 946 | rd[i].skip = 0;
|
|---|
| 947 | }
|
|---|
| 948 |
|
|---|
| 949 | // TGB: This needs much more checks than just the first two bytes!
|
|---|
| 950 |
|
|---|
| 951 | // Swap everything except start_package_flag.
|
|---|
| 952 | // It is to difficult to find out where it is used how,
|
|---|
| 953 | // but it doesn't really matter because it is not really
|
|---|
| 954 | // used anywehere else
|
|---|
| 955 | // rd[i].rBuf->S[1] = ntohs(rd[i].rBuf->S[1]); // package_length
|
|---|
| 956 | rd[i].rBuf->S[2] = ntohs(rd[i].rBuf->S[2]); // version_no
|
|---|
| 957 | rd[i].rBuf->S[3] = ntohs(rd[i].rBuf->S[3]); // PLLLCK
|
|---|
| 958 | rd[i].rBuf->S[4] = ntohs(rd[i].rBuf->S[4]); // trigger_crc
|
|---|
| 959 | rd[i].rBuf->S[5] = ntohs(rd[i].rBuf->S[5]); // trigger_type
|
|---|
| 960 |
|
|---|
| 961 | rd[i].rBuf->S[12] = ntohs(rd[i].rBuf->S[12]); // board id
|
|---|
| 962 | rd[i].rBuf->S[13] = ntohs(rd[i].rBuf->S[13]); // adc_clock_phase_shift
|
|---|
| 963 | rd[i].rBuf->S[14] = ntohs(rd[i].rBuf->S[14]); // number_of_triggers_to_generate
|
|---|
| 964 | rd[i].rBuf->S[15] = ntohs(rd[i].rBuf->S[15]); // trigger_generator_prescaler
|
|---|
| 965 |
|
|---|
| 966 | rd[i].rBuf->I[3] = ntohl(rd[i].rBuf->I[3]); // trigger_id
|
|---|
| 967 | rd[i].rBuf->I[4] = ntohl(rd[i].rBuf->I[4]); // fad_evt_counter
|
|---|
| 968 | rd[i].rBuf->I[5] = ntohl(rd[i].rBuf->I[5]); // REFCLK_frequency
|
|---|
| 969 |
|
|---|
| 970 | rd[i].rBuf->I[10] = ntohl(rd[i].rBuf->I[10]); // runnumber;
|
|---|
| 971 | rd[i].rBuf->I[11] = ntohl(rd[i].rBuf->I[11]); // time;
|
|---|
| 972 |
|
|---|
| 973 | for (int s=24;s<24+NTemp+NDAC;s++)
|
|---|
| 974 | rd[i].rBuf->S[s] = ntohs(rd[i].rBuf->S[s]); // drs_temperature / dac
|
|---|
| 975 |
|
|---|
| 976 | rd[i].fadLen = ntohs(rd[i].rBuf->S[1]) * 2;
|
|---|
| 977 | rd[i].fadVers = rd[i].rBuf->S[2];
|
|---|
| 978 | rd[i].ftmTyp = rd[i].rBuf->S[5];
|
|---|
| 979 | rd[i].ftmID = rd[i].rBuf->I[3]; //(FTMevt)
|
|---|
| 980 | rd[i].evtID = rd[i].rBuf->I[4]; //(FADevt)
|
|---|
| 981 | rd[i].runID = rd[i].rBuf->I[11]==0 ? g_actTime : rd[i].rBuf->I[11];
|
|---|
| 982 | rd[i].bufTyp = 1; //ready to read full record
|
|---|
| 983 | rd[i].bufLen = rd[i].fadLen - rd[i].bufPos;
|
|---|
| 984 |
|
|---|
| 985 | const int fadBoard = rd[i].rBuf->S[12];
|
|---|
| 986 | debugHead(i, fadBoard, rd[i].rBuf);
|
|---|
| 987 |
|
|---|
| 988 | continue;
|
|---|
| 989 | }
|
|---|
| 990 |
|
|---|
| 991 | // are we reading data
|
|---|
| 992 |
|
|---|
| 993 | // not yet all read
|
|---|
| 994 | if (rd[i].bufLen > 0)
|
|---|
| 995 | continue;
|
|---|
| 996 |
|
|---|
| 997 | // stop.S = 0x04FE;
|
|---|
| 998 | if (rd[i].rBuf->B[rd[i].fadLen - 1] != 0xfe ||
|
|---|
| 999 | rd[i].rBuf->B[rd[i].fadLen - 2] != 0x04)
|
|---|
| 1000 | {
|
|---|
| 1001 | factPrintf(kError, 301, "End-of-event flag wrong on socket %3d for event %4d (len=%5d), got %3d %3d",
|
|---|
| 1002 | i, rd[i].evtID, rd[i].fadLen,
|
|---|
| 1003 | rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]);
|
|---|
| 1004 |
|
|---|
| 1005 | // ready to read next header
|
|---|
| 1006 | rd[i].bufTyp = 0;
|
|---|
| 1007 | rd[i].bufLen = sizeof(PEVNT_HEADER);
|
|---|
| 1008 | rd[i].bufPos = 0;
|
|---|
| 1009 |
|
|---|
| 1010 | continue;
|
|---|
| 1011 | }
|
|---|
| 1012 |
|
|---|
| 1013 | // int actid;
|
|---|
| 1014 | // if (g_useFTM > 0)
|
|---|
| 1015 | // actid = rd[i].evtID;
|
|---|
| 1016 | // else
|
|---|
| 1017 | // actid = rd[i].ftmID;
|
|---|
| 1018 |
|
|---|
| 1019 | //get index into mBuffer for this event (create if needed)
|
|---|
| 1020 | const int idx = mBufEvt(&rd[i]);
|
|---|
| 1021 |
|
|---|
| 1022 | // no free entry in mBuffer, retry later
|
|---|
| 1023 | if (idx == -1)
|
|---|
| 1024 | continue;
|
|---|
| 1025 |
|
|---|
| 1026 | // We have a valid entry, but no memory has yet been allocated
|
|---|
| 1027 | if (idx >= 0 && evtCtrl[idx].evtStat==0 && evtCtrl[idx].FADhead==NULL)
|
|---|
| 1028 | {
|
|---|
| 1029 | // Try to get memory from the big buffer
|
|---|
| 1030 | evtCtrl[idx].FADhead = (PEVNT_HEADER*)TGB_Malloc();
|
|---|
| 1031 | if (evtCtrl[idx].FADhead == NULL)
|
|---|
| 1032 | {
|
|---|
| 1033 | // If this works properly, this is a hack which can be removed, or
|
|---|
| 1034 | // replaced by a signal or dim message
|
|---|
| 1035 | if (rd[i].bufTyp==1)
|
|---|
| 1036 | factPrintf(kError, 882, "No free memory left for %d (run=%d)", evtCtrl[idx].evNum, evtCtrl[idx].runNum);
|
|---|
| 1037 | rd[i].bufTyp = 2;
|
|---|
| 1038 | continue;
|
|---|
| 1039 | }
|
|---|
| 1040 |
|
|---|
| 1041 | // Initialise contents of mBuffer[evID]->fEvent
|
|---|
| 1042 | initEvent(idx);
|
|---|
| 1043 |
|
|---|
| 1044 | // Some statistics
|
|---|
| 1045 | gj.usdMem = tgb_inuse;
|
|---|
| 1046 |
|
|---|
| 1047 | if (gj.usdMem > gj.maxMem)
|
|---|
| 1048 | gj.maxMem = gj.usdMem;
|
|---|
| 1049 |
|
|---|
| 1050 | gj.rateNew++;
|
|---|
| 1051 | if (gj.bufTot > gj.maxEvt)
|
|---|
| 1052 | gj.maxEvt = gj.bufTot;
|
|---|
| 1053 | }
|
|---|
| 1054 |
|
|---|
| 1055 | // ready to read next header
|
|---|
| 1056 | rd[i].bufTyp = 0;
|
|---|
| 1057 | rd[i].bufLen = sizeof(PEVNT_HEADER);
|
|---|
| 1058 | rd[i].bufPos = 0;
|
|---|
| 1059 |
|
|---|
| 1060 | // Fatal error occured. Event cannot be processed. Skip it. Start reading next header.
|
|---|
| 1061 | if (idx < -1000)
|
|---|
| 1062 | continue;
|
|---|
| 1063 |
|
|---|
| 1064 | if (evtCtrl[idx].evtStat==-1 || evtCtrl[idx].evtStat>=90)
|
|---|
| 1065 | {
|
|---|
| 1066 | factPrintf(kError, 882, "Received data of event %d [%d] (run=%d) has already been advanced (stat=%d)... skipping", evtCtrl[idx].evNum, i, evtCtrl[idx].runNum, evtCtrl[idx].evtStat);
|
|---|
| 1067 | continue;
|
|---|
| 1068 | }
|
|---|
| 1069 |
|
|---|
| 1070 | //we have a valid entry in mBuffer[]; fill it
|
|---|
| 1071 | const int fadBoard = rd[i].rBuf->S[12];
|
|---|
| 1072 | const int fadCrate = fadBoard>>8;
|
|---|
| 1073 |
|
|---|
| 1074 | if (i != (fadCrate * 10 + (fadBoard&0xff)))
|
|---|
| 1075 | {
|
|---|
| 1076 | factPrintf(kWarn, 301, "Board ID mismatch. Expected %d, got %d (C=%d, B=%d)",
|
|---|
| 1077 | i, fadBoard, fadCrate, fadBoard&0xff);
|
|---|
| 1078 | }
|
|---|
| 1079 |
|
|---|
| 1080 | if (evtCtrl[idx].board[i] != -1)
|
|---|
| 1081 | {
|
|---|
| 1082 | factPrintf(kWarn, 501, "Got event %5d from board %3d (i=%3d, len=%5d) twice: Starts with %3d %3d - ends with %3d %3d",
|
|---|
| 1083 | evtCtrl[idx].evNum, i, i, rd[i].fadLen,
|
|---|
| 1084 | rd[i].rBuf->B[0], rd[i].rBuf->B[1],
|
|---|
| 1085 | rd[i].rBuf->B[rd[i].fadLen - 2],
|
|---|
| 1086 | rd[i].rBuf->B[rd[i].fadLen - 1]);
|
|---|
| 1087 | continue; // Continue reading next header
|
|---|
| 1088 | }
|
|---|
| 1089 |
|
|---|
| 1090 | // Copy data from rd[i] to mBuffer[evID]
|
|---|
| 1091 | copyData(rd[i].rBuf, i, idx);
|
|---|
| 1092 |
|
|---|
| 1093 | // now we have stored a new board contents into Event structure
|
|---|
| 1094 |
|
|---|
| 1095 | evtCtrl[idx].fEvent->NumBoards++;
|
|---|
| 1096 | evtCtrl[idx].board[i] = i;
|
|---|
| 1097 | evtCtrl[idx].nBoard++;
|
|---|
| 1098 | evtCtrl[idx].evtStat = evtCtrl[idx].nBoard;
|
|---|
| 1099 |
|
|---|
| 1100 | // have we already reported first (partial) event of this run ???
|
|---|
| 1101 | if (evtCtrl[idx].nBoard==1 && evtCtrl[idx].runNum != actrun)
|
|---|
| 1102 | {
|
|---|
| 1103 | // Signal the fadctrl that a new run has been started
|
|---|
| 1104 | gotNewRun(evtCtrl[idx].runNum, NULL);
|
|---|
| 1105 |
|
|---|
| 1106 | factPrintf(kInfo, 1, "gotNewRun called, prev run %d, new run %d, event %d",
|
|---|
| 1107 | actrun, evtCtrl[idx].runNum, evtCtrl[idx].evNum);
|
|---|
| 1108 |
|
|---|
| 1109 | for (int j=0; j<MAX_RUN; j++)
|
|---|
| 1110 | {
|
|---|
| 1111 | // Since we have started a new run, we know already when to close the
|
|---|
| 1112 | // previous run in terms of number of events
|
|---|
| 1113 | if (runCtrl[j].runId==actrun)
|
|---|
| 1114 | runCtrl[j].maxEvt = runCtrl[j].lastEvt;
|
|---|
| 1115 |
|
|---|
| 1116 | // We got the first part of this event, so this is
|
|---|
| 1117 | // the number of events we expect for this run
|
|---|
| 1118 | if (runCtrl[j].runId==evtCtrl[idx].runNum)
|
|---|
| 1119 | runCtrl[j].lastEvt++;
|
|---|
| 1120 | }
|
|---|
| 1121 |
|
|---|
| 1122 | // Change 'actrun' the the new runnumber
|
|---|
| 1123 | actrun = evtCtrl[idx].runNum;
|
|---|
| 1124 | }
|
|---|
| 1125 |
|
|---|
| 1126 | // event not yet complete
|
|---|
| 1127 | if (evtCtrl[idx].nBoard < actBoards)
|
|---|
| 1128 | continue;
|
|---|
| 1129 |
|
|---|
| 1130 | // This is a non-ideal hack to lower the probability that
|
|---|
| 1131 | // in mBufEvt the search for correct entry in runCtrl
|
|---|
| 1132 | // will not return a super-old entry. I don't want
|
|---|
| 1133 | // to manipulate that in another thread.
|
|---|
| 1134 | for (int ir=0; ir<MAX_RUN; ir++)
|
|---|
| 1135 | {
|
|---|
| 1136 | if (runCtrl[ir].runId != actrun && runCtrl[ir].fileId>0)
|
|---|
| 1137 | runCtrl[ir].runId = 0;
|
|---|
| 1138 | }
|
|---|
| 1139 |
|
|---|
| 1140 | const int beg = (idx + MAX_EVT - 1) % MAX_EVT;
|
|---|
| 1141 | const int end = (evtCtrl_frstPtr + MAX_EVT - 1) % MAX_EVT;
|
|---|
| 1142 |
|
|---|
| 1143 | // we have just completed an event... so all previous events
|
|---|
| 1144 | // must have been completed already. If they are not, there
|
|---|
| 1145 | // is no need to wait for the timeout, because they will never
|
|---|
| 1146 | // get completed. We can just ensure that if we check for the previous
|
|---|
| 1147 | // event to be complete every time we receive a new complete event.
|
|---|
| 1148 | // If we find an incomplete one, we remove all consecutive
|
|---|
| 1149 | // incomplete ones.
|
|---|
| 1150 | for (int k=beg; k!=end; k=(k+MAX_EVT-1)%MAX_EVT)
|
|---|
| 1151 | {
|
|---|
| 1152 | // We are done if we find a complete or fully processed event
|
|---|
| 1153 | if (evtCtrl[k].evtStat>=100)
|
|---|
| 1154 | break;
|
|---|
| 1155 |
|
|---|
| 1156 | // we do not call factReportIncomplete here, because by starting a new
|
|---|
| 1157 | // run and having received the first complete event from that run
|
|---|
| 1158 | // the user has expressed that the old events are obsolste now
|
|---|
| 1159 | // and the run will be closed anyway
|
|---|
| 1160 | if (evtCtrl[k].evtStat>0 && evtCtrl[k].evtStat<90)
|
|---|
| 1161 | {
|
|---|
| 1162 | reportIncomplete(k, "expired");
|
|---|
| 1163 | evtCtrl[k].evtStat = 90;
|
|---|
| 1164 | }
|
|---|
| 1165 | }
|
|---|
| 1166 |
|
|---|
| 1167 | // Flag that the event is ready for processing
|
|---|
| 1168 | evtCtrl[idx].evtStat = 100;
|
|---|
| 1169 |
|
|---|
| 1170 | } // end for loop over all sockets
|
|---|
| 1171 |
|
|---|
| 1172 | g_actTime = time (NULL);
|
|---|
| 1173 | if (g_actTime <= gi_SecTime)
|
|---|
| 1174 | {
|
|---|
| 1175 | usleep(1);
|
|---|
| 1176 | continue;
|
|---|
| 1177 | }
|
|---|
| 1178 | gi_SecTime = g_actTime;
|
|---|
| 1179 |
|
|---|
| 1180 | gj.bufNew = 0;
|
|---|
| 1181 |
|
|---|
| 1182 | //loop over all active events and flag those older than read-timeout
|
|---|
| 1183 | //delete those that are written to disk ....
|
|---|
| 1184 |
|
|---|
| 1185 | const int count = (evtCtrl_lastPtr-evtCtrl_frstPtr+MAX_EVT)%MAX_EVT;
|
|---|
| 1186 |
|
|---|
| 1187 | // This could be improved having the pointer which separates the queue with
|
|---|
| 1188 | // the incomplete events from the queue with the complete events
|
|---|
| 1189 | for (int k0=evtCtrl_frstPtr; k0!=evtCtrl_lastPtr; k0++, k0 %= MAX_EVT)
|
|---|
| 1190 | {
|
|---|
| 1191 | // Check the more likely case first: incomplete events
|
|---|
| 1192 | if (evtCtrl[k0].evtStat>0 && evtCtrl[k0].evtStat<100)
|
|---|
| 1193 | {
|
|---|
| 1194 | gj.bufNew++; //incomplete event in Buffer
|
|---|
| 1195 |
|
|---|
| 1196 | // Event has not yet timed out or was reported already
|
|---|
| 1197 | if (evtCtrl[k0].evtStat==90 || evtCtrl[k0].pcTime[0]>=g_actTime - 30)
|
|---|
| 1198 | continue;
|
|---|
| 1199 |
|
|---|
| 1200 | // This will result in the emission of a dim service.
|
|---|
| 1201 | // It doesn't matter if that takes comparably long,
|
|---|
| 1202 | // because we have to stop the run anyway.
|
|---|
| 1203 | const uint64_t rep = reportIncomplete(k0, "timeout");
|
|---|
| 1204 | factReportIncomplete(rep);
|
|---|
| 1205 |
|
|---|
| 1206 | //timeout for incomplete events
|
|---|
| 1207 | evtCtrl[k0].evtStat = 90;
|
|---|
| 1208 |
|
|---|
| 1209 | gj.evtSkip++;
|
|---|
| 1210 |
|
|---|
| 1211 | continue;
|
|---|
| 1212 | }
|
|---|
| 1213 |
|
|---|
| 1214 | // Check the less likely case: 'useless' or 'delete'
|
|---|
| 1215 | // evtState==0 can happen if the event was initialized (some data received)
|
|---|
| 1216 | // but the data did not make sense (e.g. inconsistent rois)
|
|---|
| 1217 | if (evtCtrl[k0].evtStat==10000)
|
|---|
| 1218 | {
|
|---|
| 1219 | evtCtrl[k0].evtStat = -1;
|
|---|
| 1220 | mBufFree(k0); //event written--> free memory
|
|---|
| 1221 |
|
|---|
| 1222 | gj.evtWrite++;
|
|---|
| 1223 | gj.rateWrite++;
|
|---|
| 1224 | }
|
|---|
| 1225 |
|
|---|
| 1226 | // Remove leading invalidated slots from queue
|
|---|
| 1227 | if (evtCtrl[evtCtrl_frstPtr].evtStat==-1 && k0==evtCtrl_frstPtr)
|
|---|
| 1228 | {
|
|---|
| 1229 | evtCtrl_frstPtr = (evtCtrl_frstPtr+1) % MAX_EVT;
|
|---|
| 1230 | continue;
|
|---|
| 1231 | }
|
|---|
| 1232 | }
|
|---|
| 1233 |
|
|---|
| 1234 | // The number of complete events in the buffer is the total number of
|
|---|
| 1235 | // events in the buffer minus the number of incomplete events.
|
|---|
| 1236 | gj.bufEvt = count - gj.bufNew;
|
|---|
| 1237 |
|
|---|
| 1238 | gj.deltaT = 1000; //temporary, must be improved
|
|---|
| 1239 |
|
|---|
| 1240 | for (int ib = 0; ib < NBOARDS; ib++)
|
|---|
| 1241 | gj.totBytes[ib] += gj.rateBytes[ib];
|
|---|
| 1242 |
|
|---|
| 1243 | gj.totMem = tgb_memory;
|
|---|
| 1244 |
|
|---|
| 1245 | if (gj.maxMem > gj.xxxMem)
|
|---|
| 1246 | gj.xxxMem = gj.maxMem;
|
|---|
| 1247 | if (gj.maxEvt > gj.xxxEvt)
|
|---|
| 1248 | gj.xxxEvt = gj.maxEvt;
|
|---|
| 1249 |
|
|---|
| 1250 | factStat (gj);
|
|---|
| 1251 | //factStatNew (gi);
|
|---|
| 1252 | gj.rateNew = gj.rateWrite = 0;
|
|---|
| 1253 | gj.maxMem = gj.usdMem;
|
|---|
| 1254 | gj.maxEvt = gj.bufTot;
|
|---|
| 1255 | for (int b = 0; b < NBOARDS; b++)
|
|---|
| 1256 | gj.rateBytes[b] = 0;
|
|---|
| 1257 |
|
|---|
| 1258 | } // while (g_runStat >= 0 && g_reset == 0)
|
|---|
| 1259 |
|
|---|
| 1260 | factPrintf(kInfo, -1, "Stop reading ... RESET=%d", g_reset);
|
|---|
| 1261 |
|
|---|
| 1262 | if (g_reset > 0)
|
|---|
| 1263 | {
|
|---|
| 1264 | gi_reset = g_reset;
|
|---|
| 1265 | gi_resetR = gi_reset % 10; //shall we stop reading ?
|
|---|
| 1266 | gi_resetS = (gi_reset / 10) % 10; //shall we close sockets ?
|
|---|
| 1267 | gi_resetW = (gi_reset / 100) % 10; //shall we close files ?
|
|---|
| 1268 | gi_resetX = gi_reset / 1000; //shall we simply wait resetX seconds ?
|
|---|
| 1269 | g_reset = 0;
|
|---|
| 1270 | }
|
|---|
| 1271 | else
|
|---|
| 1272 | {
|
|---|
| 1273 | gi_reset = 0;
|
|---|
| 1274 | gi_resetR = g_runStat == -1 ? 1 : 7;
|
|---|
| 1275 |
|
|---|
| 1276 | gi_resetS = 7; //close all sockets
|
|---|
| 1277 | gi_resetW = 7; //close all files
|
|---|
| 1278 | gi_resetX = 0;
|
|---|
| 1279 |
|
|---|
| 1280 | //inform others we have to quit ....
|
|---|
| 1281 | gj.readStat = -11; //inform all that no update to happen any more
|
|---|
| 1282 | }
|
|---|
| 1283 |
|
|---|
| 1284 | if (gi_resetS > 0)
|
|---|
| 1285 | {
|
|---|
| 1286 | //must close all open sockets ...
|
|---|
| 1287 | factPrintf(kInfo, -1, "Close all sockets...");
|
|---|
| 1288 |
|
|---|
| 1289 | for (int i = 0; i < NBOARDS; i++)
|
|---|
| 1290 | {
|
|---|
| 1291 | if (rd[i].sockStat != 0)
|
|---|
| 1292 | continue;
|
|---|
| 1293 |
|
|---|
| 1294 | GenSock(-1, i, 0, NULL, &rd[i]); //close and destroy open socket
|
|---|
| 1295 |
|
|---|
| 1296 | gi_NumConnect[i]-= cntsock ;
|
|---|
| 1297 | gj.numConn[i]--;
|
|---|
| 1298 | sockDef[i] = 0; //flag ro recreate the sockets ...
|
|---|
| 1299 | rd[i].sockStat = -1; //and try to open asap
|
|---|
| 1300 | }
|
|---|
| 1301 | }
|
|---|
| 1302 |
|
|---|
| 1303 |
|
|---|
| 1304 | if (gi_resetR > 0)
|
|---|
| 1305 | {
|
|---|
| 1306 | //and clear all buffers (might have to wait until all others are done)
|
|---|
| 1307 | while (evtCtrl_frstPtr!=evtCtrl_lastPtr)
|
|---|
| 1308 | {
|
|---|
| 1309 | const int k0=evtCtrl_frstPtr;
|
|---|
| 1310 |
|
|---|
| 1311 | // flag incomplete events as 'read finished'
|
|---|
| 1312 | // We cannot just detele all events, because some might currently being processed,
|
|---|
| 1313 | // so we have to wait until the processing thread currently processing the event
|
|---|
| 1314 | // signals that the event can be deleted. (Note, that there are currently never
|
|---|
| 1315 | // two threads processing the same event at the same time)
|
|---|
| 1316 | if (evtCtrl[k0].evtStat<90 || evtCtrl[k0].evtStat==10000)
|
|---|
| 1317 | {
|
|---|
| 1318 | evtCtrl[k0].evtStat = -1;
|
|---|
| 1319 | mBufFree(k0); //event written--> free memory
|
|---|
| 1320 | evtCtrl_frstPtr = (evtCtrl_frstPtr+1) % MAX_EVT;
|
|---|
| 1321 | }
|
|---|
| 1322 |
|
|---|
| 1323 | usleep(1);
|
|---|
| 1324 | }
|
|---|
| 1325 | }
|
|---|
| 1326 |
|
|---|
| 1327 | if (gi_reset > 0)
|
|---|
| 1328 | {
|
|---|
| 1329 | if (gi_resetW > 0)
|
|---|
| 1330 | CloseRunFile (0, 0, 0); //ask all Runs to be closed
|
|---|
| 1331 |
|
|---|
| 1332 | if (gi_resetX > 0)
|
|---|
| 1333 | {
|
|---|
| 1334 | struct timespec xwait;
|
|---|
| 1335 | xwait.tv_sec = gi_resetX;
|
|---|
| 1336 | xwait.tv_nsec = 0;
|
|---|
| 1337 | nanosleep (&xwait, NULL);
|
|---|
| 1338 | }
|
|---|
| 1339 |
|
|---|
| 1340 | factPrintf(kInfo, -1, "Continue read Process ...");
|
|---|
| 1341 | gi_reset = 0;
|
|---|
| 1342 | goto START;
|
|---|
| 1343 | }
|
|---|
| 1344 |
|
|---|
| 1345 | factPrintf(kInfo, -1, "Exit read Process...");
|
|---|
| 1346 |
|
|---|
| 1347 | factPrintf(kInfo, -1, "%ld Bytes flagged as in-use.", tgb_inuse);
|
|---|
| 1348 |
|
|---|
| 1349 | gj.readStat = -99;
|
|---|
| 1350 |
|
|---|
| 1351 | factStat (gj);
|
|---|
| 1352 | //factStatNew (gi);
|
|---|
| 1353 |
|
|---|
| 1354 | return 0;
|
|---|
| 1355 |
|
|---|
| 1356 | } /*-----------------------------------------------------------------*/
|
|---|
| 1357 |
|
|---|
| 1358 |
|
|---|
| 1359 | void *subProc(void *thrid)
|
|---|
| 1360 | {
|
|---|
| 1361 | const int64_t threadID = (int64_t)thrid;
|
|---|
| 1362 |
|
|---|
| 1363 | factPrintf(kInfo, -1, "Starting sub-process-thread %ld", threadID);
|
|---|
| 1364 |
|
|---|
| 1365 | while (g_runStat > -2) //in case of 'exit' we still must process pending events
|
|---|
| 1366 | {
|
|---|
| 1367 | int numWait = 0;
|
|---|
| 1368 |
|
|---|
| 1369 | for (int k0=evtCtrl_frstPtr; k0!=evtCtrl_lastPtr; k0++, k0 %= MAX_EVT)
|
|---|
| 1370 | {
|
|---|
| 1371 | // This is a threading issue... the evtStat might have been invalid
|
|---|
| 1372 | // but the frstPtr is not yet updated
|
|---|
| 1373 | if (evtCtrl[k0].evtStat==-1)
|
|---|
| 1374 | continue;
|
|---|
| 1375 |
|
|---|
| 1376 | // If we find the first event still waiting for processing
|
|---|
| 1377 | // there will be only unprocessed events after this one in the queue
|
|---|
| 1378 | if (evtCtrl[k0].evtStat<1000+threadID)
|
|---|
| 1379 | {
|
|---|
| 1380 | numWait = 1;
|
|---|
| 1381 | break;
|
|---|
| 1382 | }
|
|---|
| 1383 |
|
|---|
| 1384 | // If the event was processed already, skip it
|
|---|
| 1385 | // We could replace that to a moving pointer pointing to the first
|
|---|
| 1386 | // non-processed event
|
|---|
| 1387 | if (evtCtrl[k0].evtStat!=1000+threadID)
|
|---|
| 1388 | continue;
|
|---|
| 1389 |
|
|---|
| 1390 | const int jret = subProcEvt(threadID, evtCtrl[k0].FADhead, evtCtrl[k0].fEvent, 0);
|
|---|
| 1391 |
|
|---|
| 1392 | if (jret>0 && jret<=threadID)
|
|---|
| 1393 | factPrintf(kError, -1, "Process %ld wants to send event to process %d... not allowed.", threadID, jret);
|
|---|
| 1394 |
|
|---|
| 1395 | if (jret<=threadID)
|
|---|
| 1396 | {
|
|---|
| 1397 | evtCtrl[k0].evtStat = 10000; // flag as 'to be deleted'
|
|---|
| 1398 | continue;
|
|---|
| 1399 | }
|
|---|
| 1400 |
|
|---|
| 1401 | if (jret>=gi_maxProc)
|
|---|
| 1402 | {
|
|---|
| 1403 | evtCtrl[k0].evtStat = 5000; // flag as 'to be written'
|
|---|
| 1404 | continue;
|
|---|
| 1405 | }
|
|---|
| 1406 |
|
|---|
| 1407 | evtCtrl[k0].evtStat = 1000 + jret; // flag for next proces
|
|---|
| 1408 | }
|
|---|
| 1409 |
|
|---|
| 1410 | if (gj.readStat < -10 && numWait == 0) { //nothing left to do
|
|---|
| 1411 | factPrintf(kInfo, -1, "Exit subProcessing in process %ld", threadID);
|
|---|
| 1412 | return 0;
|
|---|
| 1413 | }
|
|---|
| 1414 |
|
|---|
| 1415 | usleep(1);
|
|---|
| 1416 | }
|
|---|
| 1417 |
|
|---|
| 1418 | factPrintf(kInfo, -1, "Ending sub-process-thread %ld", threadID);
|
|---|
| 1419 |
|
|---|
| 1420 | return 0;
|
|---|
| 1421 | }
|
|---|
| 1422 |
|
|---|
| 1423 | /*-----------------------------------------------------------------*/
|
|---|
| 1424 |
|
|---|
| 1425 |
|
|---|
| 1426 | void *
|
|---|
| 1427 | procEvt (void *ptr)
|
|---|
| 1428 | {
|
|---|
| 1429 | /* *** main loop processing file, including SW-trigger */
|
|---|
| 1430 | int status;
|
|---|
| 1431 |
|
|---|
| 1432 | factPrintf(kInfo, -1, "Starting process-thread with %d subprocesses", gi_maxProc);
|
|---|
| 1433 |
|
|---|
| 1434 | pthread_t thread[100];
|
|---|
| 1435 | // int th_ret[100];
|
|---|
| 1436 |
|
|---|
| 1437 | for (long long k = 0; k < gi_maxProc; k++) {
|
|---|
| 1438 | /*th_ret[k] =*/ pthread_create (&thread[k], NULL, subProc, (void *) k);
|
|---|
| 1439 | }
|
|---|
| 1440 |
|
|---|
| 1441 | // in case of 'exit' we still must process pending events
|
|---|
| 1442 | while (g_runStat > -2)
|
|---|
| 1443 | {
|
|---|
| 1444 | int numWait = 0;
|
|---|
| 1445 |
|
|---|
| 1446 | for (int k0=evtCtrl_frstPtr; k0!=evtCtrl_lastPtr; k0++, k0 %= MAX_EVT)
|
|---|
| 1447 | {
|
|---|
| 1448 | // This is a threading issue... the evtStat might have been invalid
|
|---|
| 1449 | // but the frstPtr is not yet updated
|
|---|
| 1450 | if (evtCtrl[k0].evtStat==-1)
|
|---|
| 1451 | continue;
|
|---|
| 1452 |
|
|---|
| 1453 | // If we find the first incomplete event which is not supposed to
|
|---|
| 1454 | // be processed, there are only more incomplete events in the queue
|
|---|
| 1455 | if (evtCtrl[k0].evtStat<90)
|
|---|
| 1456 | {
|
|---|
| 1457 | numWait = 1;
|
|---|
| 1458 | break;
|
|---|
| 1459 | }
|
|---|
| 1460 |
|
|---|
| 1461 | // If the event was processed already, skip it.
|
|---|
| 1462 | // We could replace that to a moving pointer pointing to the first
|
|---|
| 1463 | // non-processed event
|
|---|
| 1464 | if (evtCtrl[k0].evtStat>=1000)
|
|---|
| 1465 | continue;
|
|---|
| 1466 |
|
|---|
| 1467 | //-------- it is better to open the run already here, so call can be used to initialize
|
|---|
| 1468 | //-------- buffers etc. needed to interprete run (e.g. DRS calibration)
|
|---|
| 1469 | const uint32_t irun = evtCtrl[k0].runNum;
|
|---|
| 1470 | const int32_t ievt = evtCtrl[k0].evNum;
|
|---|
| 1471 |
|
|---|
| 1472 | const int idx = evtCtrl[k0].runCtrl_idx;
|
|---|
| 1473 | if (runCtrl[idx].runId!=irun)
|
|---|
| 1474 | {
|
|---|
| 1475 | //factPrintf(kFatal, 901, "procEvt: runCtrl entry for run %d vanished (evt=%d)", irun, ievt);
|
|---|
| 1476 | // FIXME: What is the right action? (Flag event for deletion?)
|
|---|
| 1477 | continue;
|
|---|
| 1478 | }
|
|---|
| 1479 |
|
|---|
| 1480 | // File not yet open
|
|---|
| 1481 | if (runCtrl[idx].fileId < 0)
|
|---|
| 1482 | {
|
|---|
| 1483 | RUN_HEAD actRun;
|
|---|
| 1484 | actRun.Version = 1;
|
|---|
| 1485 | actRun.RunType = -1; //to be adapted
|
|---|
| 1486 | actRun.Nroi = evtCtrl[k0].nRoi; //runCtrl[lastRun].roi0;
|
|---|
| 1487 | actRun.NroiTM = evtCtrl[k0].nRoiTM; //runCtrl[lastRun].roi8;
|
|---|
| 1488 | actRun.RunTime = evtCtrl[k0].pcTime[0]; //runCtrl[lastRun].firstTime;
|
|---|
| 1489 | actRun.RunUsec = evtCtrl[k0].pcTime[1]; //runCtrl[lastRun].firstUsec;
|
|---|
| 1490 | actRun.NBoard = NBOARDS;
|
|---|
| 1491 | actRun.NPix = NPIX;
|
|---|
| 1492 | actRun.NTm = NTMARK;
|
|---|
| 1493 |
|
|---|
| 1494 | memcpy(actRun.FADhead, evtCtrl[k0].FADhead, NBOARDS*sizeof(PEVNT_HEADER));
|
|---|
| 1495 |
|
|---|
| 1496 | runCtrl[idx].fileHd = runOpen(irun, &actRun, sizeof (actRun));
|
|---|
| 1497 | if (runCtrl[idx].fileHd == NULL)
|
|---|
| 1498 | {
|
|---|
| 1499 | factPrintf(kError, 502, "procEvt: Could not open new file for run %d (idx=%d, evt=%d, runOpen failed)", irun, idx, ievt);
|
|---|
| 1500 | runCtrl[idx].fileId = 91;
|
|---|
| 1501 | // FIXME: What happens to evtStat? Shell we really just try again?
|
|---|
| 1502 | continue;
|
|---|
| 1503 | }
|
|---|
| 1504 |
|
|---|
| 1505 | runCtrl[idx].fileId = 0;
|
|---|
| 1506 |
|
|---|
| 1507 | factPrintf(kInfo, -1, "procEvt: Opened new file for run %d (idx=%d, evt=%d)", irun, idx, ievt);
|
|---|
| 1508 | }
|
|---|
| 1509 |
|
|---|
| 1510 | //--------
|
|---|
| 1511 | //--------
|
|---|
| 1512 |
|
|---|
| 1513 | //and set correct event header ; also check for consistency in event (not yet)
|
|---|
| 1514 | evtCtrl[k0].fEvent->Errors[0] = evtCtrl[k0].Errors[0];
|
|---|
| 1515 | evtCtrl[k0].fEvent->Errors[1] = evtCtrl[k0].Errors[1];
|
|---|
| 1516 | evtCtrl[k0].fEvent->Errors[2] = evtCtrl[k0].Errors[2];
|
|---|
| 1517 | evtCtrl[k0].fEvent->Errors[3] = evtCtrl[k0].Errors[3];
|
|---|
| 1518 |
|
|---|
| 1519 | for (int ib=0; ib<NBOARDS; ib++)
|
|---|
| 1520 | {
|
|---|
| 1521 | // board is not read
|
|---|
| 1522 | if (evtCtrl[k0].board[ib] == -1)
|
|---|
| 1523 | {
|
|---|
| 1524 | evtCtrl[k0].FADhead[ib].start_package_flag = 0;
|
|---|
| 1525 | evtCtrl[k0].fEvent->BoardTime[ib] = 0;
|
|---|
| 1526 | }
|
|---|
| 1527 | else
|
|---|
| 1528 | {
|
|---|
| 1529 | evtCtrl[k0].fEvent->BoardTime[ib] = evtCtrl[k0].FADhead[ib].time;
|
|---|
| 1530 | }
|
|---|
| 1531 | }
|
|---|
| 1532 |
|
|---|
| 1533 | const int rc = eventCheck(evtCtrl[k0].runNum, evtCtrl[k0].FADhead, evtCtrl[k0].fEvent);
|
|---|
| 1534 | if (rc < 0)
|
|---|
| 1535 | {
|
|---|
| 1536 | evtCtrl[k0].evtStat = 10000; // flag event to be deleted
|
|---|
| 1537 | }
|
|---|
| 1538 | else
|
|---|
| 1539 | {
|
|---|
| 1540 | evtCtrl[k0].evtStat = 1000; // flag 'start processing'
|
|---|
| 1541 | runCtrl[idx].procEvt++;
|
|---|
| 1542 | }
|
|---|
| 1543 | }
|
|---|
| 1544 |
|
|---|
| 1545 | if (gj.readStat < -10 && numWait == 0) { //nothing left to do
|
|---|
| 1546 | factPrintf(kInfo, -1, "Exit Processing Process ...");
|
|---|
| 1547 | gj.procStat = -22; //==> we should exit
|
|---|
| 1548 | return 0;
|
|---|
| 1549 | }
|
|---|
| 1550 |
|
|---|
| 1551 | usleep(1);
|
|---|
| 1552 |
|
|---|
| 1553 | gj.procStat = gj.readStat;
|
|---|
| 1554 | }
|
|---|
| 1555 |
|
|---|
| 1556 | //we are asked to abort asap ==> must flag all remaining events
|
|---|
| 1557 | // when gi_runStat claims that all events are in the buffer...
|
|---|
| 1558 |
|
|---|
| 1559 | factPrintf(kInfo, -1, "Abort Processing Process ...");
|
|---|
| 1560 |
|
|---|
| 1561 | for (int k = 0; k < gi_maxProc; k++) {
|
|---|
| 1562 | pthread_join (thread[k], (void **) &status);
|
|---|
| 1563 | }
|
|---|
| 1564 |
|
|---|
| 1565 | gj.procStat = -99;
|
|---|
| 1566 |
|
|---|
| 1567 | return 0;
|
|---|
| 1568 |
|
|---|
| 1569 | } /*-----------------------------------------------------------------*/
|
|---|
| 1570 |
|
|---|
| 1571 | int
|
|---|
| 1572 | CloseRunFile (uint32_t runId, uint32_t closeTime, uint32_t maxEvt)
|
|---|
| 1573 | {
|
|---|
| 1574 | /* close run runId (all all runs if runId=0) */
|
|---|
| 1575 | /* return: 0=close scheduled / >0 already closed / <0 does not exist */
|
|---|
| 1576 | int j;
|
|---|
| 1577 |
|
|---|
| 1578 |
|
|---|
| 1579 | if (runId == 0) {
|
|---|
| 1580 | for (j = 0; j < MAX_RUN; j++) {
|
|---|
| 1581 | if (runCtrl[j].fileId == 0) { //run is open
|
|---|
| 1582 | runCtrl[j].closeTime = closeTime;
|
|---|
| 1583 | runCtrl[j].maxEvt = maxEvt;
|
|---|
| 1584 | }
|
|---|
| 1585 | }
|
|---|
| 1586 | return 0;
|
|---|
| 1587 | }
|
|---|
| 1588 |
|
|---|
| 1589 | for (j = 0; j < MAX_RUN; j++) {
|
|---|
| 1590 | if (runCtrl[j].runId == runId) {
|
|---|
| 1591 | if (runCtrl[j].fileId == 0) { //run is open
|
|---|
| 1592 | runCtrl[j].closeTime = closeTime;
|
|---|
| 1593 | runCtrl[j].maxEvt = maxEvt;
|
|---|
| 1594 | return 0;
|
|---|
| 1595 | } else if (runCtrl[j].fileId < 0) { //run not yet opened
|
|---|
| 1596 | runCtrl[j].closeTime = closeTime;
|
|---|
| 1597 | runCtrl[j].maxEvt = maxEvt;
|
|---|
| 1598 | return +1;
|
|---|
| 1599 | } else { // run already closed
|
|---|
| 1600 | return +2;
|
|---|
| 1601 | }
|
|---|
| 1602 | }
|
|---|
| 1603 | } //we only reach here if the run was never created
|
|---|
| 1604 | return -1;
|
|---|
| 1605 |
|
|---|
| 1606 | }
|
|---|
| 1607 |
|
|---|
| 1608 | void checkAndCloseRun(int j, int cond, int where)
|
|---|
| 1609 | {
|
|---|
| 1610 | if (!cond &&
|
|---|
| 1611 | runCtrl[j].closeTime >= g_actTime &&
|
|---|
| 1612 | runCtrl[j].lastTime >= g_actTime - 300 &&
|
|---|
| 1613 | runCtrl[j].maxEvt > runCtrl[j].actEvt)
|
|---|
| 1614 | return;
|
|---|
| 1615 |
|
|---|
| 1616 | //close run for whatever reason
|
|---|
| 1617 | int ii = 0;
|
|---|
| 1618 | if (cond)
|
|---|
| 1619 | ii = 1;
|
|---|
| 1620 | if (runCtrl[j].closeTime < g_actTime)
|
|---|
| 1621 | ii |= 2; // = 2;
|
|---|
| 1622 | if (runCtrl[j].lastTime < g_actTime - 300)
|
|---|
| 1623 | ii |= 4; // = 3;
|
|---|
| 1624 | if (runCtrl[j].maxEvt <= runCtrl[j].actEvt)
|
|---|
| 1625 | ii |= 8; // = 4;
|
|---|
| 1626 |
|
|---|
| 1627 | runCtrl[j].closeTime = g_actTime - 1;
|
|---|
| 1628 |
|
|---|
| 1629 | const int rc = runClose(runCtrl[j].fileHd, NULL, 0);//&runTail[j], sizeof(runTail[j]));
|
|---|
| 1630 | if (rc<0)
|
|---|
| 1631 | {
|
|---|
| 1632 | factPrintf(kError, 503, "writeEvt-%d: Error closing run %d (runClose,rc=%d)",
|
|---|
| 1633 | where, runCtrl[j].runId, rc);
|
|---|
| 1634 | runCtrl[j].fileId = 92+where*2;
|
|---|
| 1635 | }
|
|---|
| 1636 | else
|
|---|
| 1637 | {
|
|---|
| 1638 | factPrintf(kInfo, 503, "writeEvt-%d: Closed run %d (reason=%d)",
|
|---|
| 1639 | where, runCtrl[j].runId, ii);
|
|---|
| 1640 | runCtrl[j].fileId = 93+where*2;
|
|---|
| 1641 | }
|
|---|
| 1642 | }
|
|---|
| 1643 |
|
|---|
| 1644 | /*-----------------------------------------------------------------*/
|
|---|
| 1645 |
|
|---|
| 1646 |
|
|---|
| 1647 | void *writeEvt (void *ptr)
|
|---|
| 1648 | {
|
|---|
| 1649 | /* *** main loop writing event (including opening and closing run-files */
|
|---|
| 1650 |
|
|---|
| 1651 | factPrintf(kInfo, -1, "Starting write-thread");
|
|---|
| 1652 |
|
|---|
| 1653 | while (g_runStat > -2)
|
|---|
| 1654 | {
|
|---|
| 1655 | //int numWrite = 0;
|
|---|
| 1656 | int numWait = 0;
|
|---|
| 1657 |
|
|---|
| 1658 | // Note that the current loop does not at all gurantee that
|
|---|
| 1659 | // the events are written in the correct order.
|
|---|
| 1660 | for (int k0=evtCtrl_frstPtr; k0!=evtCtrl_lastPtr; k0++, k0 %= MAX_EVT)
|
|---|
| 1661 | {
|
|---|
| 1662 | // This is a threading issue... the evtStat might have been invalid
|
|---|
| 1663 | // but the frstPtr is not yet updated
|
|---|
| 1664 | if (evtCtrl[k0].evtStat==-1)
|
|---|
| 1665 | continue;
|
|---|
| 1666 |
|
|---|
| 1667 | // If we find the first non-written event which is not supposed to
|
|---|
| 1668 | // be written, there are only more incomplete events in the queue
|
|---|
| 1669 | if (evtCtrl[k0].evtStat<5000)
|
|---|
| 1670 | {
|
|---|
| 1671 | numWait = 1;
|
|---|
| 1672 | break;
|
|---|
| 1673 | }
|
|---|
| 1674 |
|
|---|
| 1675 | // If the event was written already already, skip it
|
|---|
| 1676 | // We could replace that to a moving pointer pointing to the first
|
|---|
| 1677 | // non-processed event
|
|---|
| 1678 | if (evtCtrl[k0].evtStat!=5000)
|
|---|
| 1679 | continue;
|
|---|
| 1680 |
|
|---|
| 1681 | const uint32_t irun = evtCtrl[k0].runNum;
|
|---|
| 1682 |
|
|---|
| 1683 | const int idx = evtCtrl[k0].runCtrl_idx;
|
|---|
| 1684 |
|
|---|
| 1685 | if (runCtrl[idx].runId!=irun)
|
|---|
| 1686 | {
|
|---|
| 1687 | //factPrintf(kFatal, 901, "writeEvt: runCtrl entry for run %d vanished (evt=%d)", irun, ievt);
|
|---|
| 1688 | // FIXME: What is the right action? (Flag event for deletion?)
|
|---|
| 1689 | continue;
|
|---|
| 1690 | }
|
|---|
| 1691 |
|
|---|
| 1692 | // File is open
|
|---|
| 1693 | if (runCtrl[idx].fileId==0)
|
|---|
| 1694 | {
|
|---|
| 1695 | const int rc = runWrite(runCtrl[idx].fileHd, evtCtrl[k0].fEvent, 0);
|
|---|
| 1696 | if (rc >= 0)
|
|---|
| 1697 | {
|
|---|
| 1698 | // Sucessfully wrote event
|
|---|
| 1699 | runCtrl[idx].lastTime = g_actTime;
|
|---|
| 1700 | runCtrl[idx].actEvt++;
|
|---|
| 1701 | }
|
|---|
| 1702 | else
|
|---|
| 1703 | factPrintf(kError, 503, "writeEvt: Writing event for run %d failed (runWrite)", irun);
|
|---|
| 1704 |
|
|---|
| 1705 | checkAndCloseRun(idx, rc<0, 1);
|
|---|
| 1706 | }
|
|---|
| 1707 |
|
|---|
| 1708 | evtCtrl[k0].evtStat = 10000; // event written (or has to be discarded) -> delete
|
|---|
| 1709 | }
|
|---|
| 1710 |
|
|---|
| 1711 | // Although the are no pending events, we have to check if a run should be closed (timeout)
|
|---|
| 1712 | for (int j=0; j<MAX_RUN; j++)
|
|---|
| 1713 | {
|
|---|
| 1714 | if (runCtrl[j].fileId == 0)
|
|---|
| 1715 | {
|
|---|
| 1716 | //ETIENNE added the condition at this line. dunno what to do with run 0: skipping it
|
|---|
| 1717 | const int cond = /*runCtrl[j].lastTime < lastStartedTime &&*/ runCtrl[j].runId == 0;
|
|---|
| 1718 | checkAndCloseRun(j, cond, 2);
|
|---|
| 1719 | }
|
|---|
| 1720 | }
|
|---|
| 1721 |
|
|---|
| 1722 | usleep(1);
|
|---|
| 1723 |
|
|---|
| 1724 | //nothing left to do
|
|---|
| 1725 | if (gj.readStat < -10 && numWait == 0)
|
|---|
| 1726 | {
|
|---|
| 1727 | factPrintf(kInfo, -1, "Finish Write Process ...");
|
|---|
| 1728 | gj.writStat = -22; //==> we should exit
|
|---|
| 1729 | break;
|
|---|
| 1730 | }
|
|---|
| 1731 |
|
|---|
| 1732 | gj.writStat = gj.readStat;
|
|---|
| 1733 | }
|
|---|
| 1734 |
|
|---|
| 1735 | factPrintf(kInfo, -1, "Close all open files ...");
|
|---|
| 1736 | for (int j=0; j<MAX_RUN; j++)
|
|---|
| 1737 | {
|
|---|
| 1738 | if (runCtrl[j].fileId == 0)
|
|---|
| 1739 | checkAndCloseRun(j, 1, 3);
|
|---|
| 1740 | }
|
|---|
| 1741 |
|
|---|
| 1742 | gj.writStat = -99;
|
|---|
| 1743 |
|
|---|
| 1744 | factPrintf(kInfo, -1, "Exit Writing Process ...");
|
|---|
| 1745 |
|
|---|
| 1746 | return 0;
|
|---|
| 1747 | } /*-----------------------------------------------------------------*/
|
|---|
| 1748 |
|
|---|
| 1749 |
|
|---|
| 1750 |
|
|---|
| 1751 |
|
|---|
| 1752 | void
|
|---|
| 1753 | StartEvtBuild ()
|
|---|
| 1754 | {
|
|---|
| 1755 |
|
|---|
| 1756 | int i, /*j,*/ imax, status/*, th_ret[50]*/;
|
|---|
| 1757 | pthread_t thread[50];
|
|---|
| 1758 | struct timespec xwait;
|
|---|
| 1759 |
|
|---|
| 1760 | gj.readStat = gj.procStat = gj.writStat = 0;
|
|---|
| 1761 |
|
|---|
| 1762 | factPrintf(kInfo, -1, "Starting EventBuilder V15.07 A");
|
|---|
| 1763 |
|
|---|
| 1764 | //initialize run control logics
|
|---|
| 1765 | for (i = 0; i < MAX_RUN; i++) {
|
|---|
| 1766 | runCtrl[i].runId = 0;
|
|---|
| 1767 | runCtrl[i].fileId = -2;
|
|---|
| 1768 | }
|
|---|
| 1769 |
|
|---|
| 1770 | gi_maxProc = g_maxProc;
|
|---|
| 1771 | if (gi_maxProc <= 0 || gi_maxProc > 90) {
|
|---|
| 1772 | factPrintf(kFatal, 301, "Illegal number of processes %d", gi_maxProc);
|
|---|
| 1773 | gi_maxProc = 1;
|
|---|
| 1774 | }
|
|---|
| 1775 | //partially initialize event control logics
|
|---|
| 1776 | evtCtrl_frstPtr = 0;
|
|---|
| 1777 | evtCtrl_lastPtr = 0;
|
|---|
| 1778 |
|
|---|
| 1779 | //start all threads (more to come) when we are allowed to ....
|
|---|
| 1780 | while (g_runStat == 0) {
|
|---|
| 1781 | xwait.tv_sec = 0;
|
|---|
| 1782 | xwait.tv_nsec = 10000000; // sleep for ~10 msec
|
|---|
| 1783 | nanosleep (&xwait, NULL);
|
|---|
| 1784 | }
|
|---|
| 1785 |
|
|---|
| 1786 | i = 0;
|
|---|
| 1787 | /*th_ret[i] =*/ pthread_create (&thread[i], NULL, readFAD, NULL);
|
|---|
| 1788 | i++;
|
|---|
| 1789 | /*th_ret[i] =*/ pthread_create (&thread[i], NULL, procEvt, NULL);
|
|---|
| 1790 | i++;
|
|---|
| 1791 | /*th_ret[i] =*/ pthread_create (&thread[i], NULL, writeEvt, NULL);
|
|---|
| 1792 | i++;
|
|---|
| 1793 | imax = i;
|
|---|
| 1794 |
|
|---|
| 1795 |
|
|---|
| 1796 | #ifdef BILAND
|
|---|
| 1797 | xwait.tv_sec = 30;;
|
|---|
| 1798 | xwait.tv_nsec = 0; // sleep for ~20sec
|
|---|
| 1799 | nanosleep (&xwait, NULL);
|
|---|
| 1800 |
|
|---|
| 1801 | printf ("close all runs in 2 seconds\n");
|
|---|
| 1802 |
|
|---|
| 1803 | CloseRunFile (0, time (NULL) + 2, 0);
|
|---|
| 1804 |
|
|---|
| 1805 | xwait.tv_sec = 1;;
|
|---|
| 1806 | xwait.tv_nsec = 0; // sleep for ~20sec
|
|---|
| 1807 | nanosleep (&xwait, NULL);
|
|---|
| 1808 |
|
|---|
| 1809 | printf ("setting g_runstat to -1\n");
|
|---|
| 1810 |
|
|---|
| 1811 | g_runStat = -1;
|
|---|
| 1812 | #endif
|
|---|
| 1813 |
|
|---|
| 1814 |
|
|---|
| 1815 | //wait for all threads to finish
|
|---|
| 1816 | for (i = 0; i < imax; i++) {
|
|---|
| 1817 | /*j =*/ pthread_join (thread[i], (void **) &status);
|
|---|
| 1818 | }
|
|---|
| 1819 |
|
|---|
| 1820 | } /*-----------------------------------------------------------------*/
|
|---|
| 1821 |
|
|---|
| 1822 |
|
|---|
| 1823 |
|
|---|
| 1824 |
|
|---|
| 1825 |
|
|---|
| 1826 |
|
|---|
| 1827 |
|
|---|
| 1828 |
|
|---|
| 1829 |
|
|---|
| 1830 |
|
|---|
| 1831 |
|
|---|
| 1832 |
|
|---|
| 1833 |
|
|---|
| 1834 |
|
|---|
| 1835 |
|
|---|
| 1836 | /*-----------------------------------------------------------------*/
|
|---|
| 1837 | /*-----------------------------------------------------------------*/
|
|---|
| 1838 | /*-----------------------------------------------------------------*/
|
|---|
| 1839 | /*-----------------------------------------------------------------*/
|
|---|
| 1840 | /*-----------------------------------------------------------------*/
|
|---|
| 1841 |
|
|---|
| 1842 | #ifdef BILAND
|
|---|
| 1843 |
|
|---|
| 1844 | int
|
|---|
| 1845 | subProcEvt (int threadID, PEVNT_HEADER * fadhd, EVENT * event,
|
|---|
| 1846 | int8_t * buffer)
|
|---|
| 1847 | {
|
|---|
| 1848 | printf ("called subproc %d\n", threadID);
|
|---|
| 1849 | return threadID + 1;
|
|---|
| 1850 | }
|
|---|
| 1851 |
|
|---|
| 1852 |
|
|---|
| 1853 |
|
|---|
| 1854 |
|
|---|
| 1855 | /*-----------------------------------------------------------------*/
|
|---|
| 1856 | /*-----------------------------------------------------------------*/
|
|---|
| 1857 | /*-----------------------------------------------------------------*/
|
|---|
| 1858 | /*-----------------------------------------------------------------*/
|
|---|
| 1859 | /*-----------------------------------------------------------------*/
|
|---|
| 1860 |
|
|---|
| 1861 |
|
|---|
| 1862 |
|
|---|
| 1863 |
|
|---|
| 1864 | FileHandle_t
|
|---|
| 1865 | runOpen (uint32_t irun, RUN_HEAD * runhd, size_t len)
|
|---|
| 1866 | {
|
|---|
| 1867 | return 1;
|
|---|
| 1868 | };
|
|---|
| 1869 |
|
|---|
| 1870 | int
|
|---|
| 1871 | runWrite (FileHandle_t fileHd, EVENT * event, size_t len)
|
|---|
| 1872 | {
|
|---|
| 1873 | return 1;
|
|---|
| 1874 | usleep (10000);
|
|---|
| 1875 | return 1;
|
|---|
| 1876 | }
|
|---|
| 1877 |
|
|---|
| 1878 |
|
|---|
| 1879 | //{ return 1; } ;
|
|---|
| 1880 |
|
|---|
| 1881 | int
|
|---|
| 1882 | runClose (FileHandle_t fileHd, RUN_TAIL * runth, size_t len)
|
|---|
| 1883 | {
|
|---|
| 1884 | return 1;
|
|---|
| 1885 | };
|
|---|
| 1886 |
|
|---|
| 1887 |
|
|---|
| 1888 |
|
|---|
| 1889 |
|
|---|
| 1890 | int
|
|---|
| 1891 | eventCheck (uint32_t runNr, PEVNT_HEADER * fadhd, EVENT * event)
|
|---|
| 1892 | {
|
|---|
| 1893 | int i = 0;
|
|---|
| 1894 |
|
|---|
| 1895 | // printf("------------%d\n",ntohl(fadhd[7].fad_evt_counter) );
|
|---|
| 1896 | // for (i=0; i<NBOARDS; i++) {
|
|---|
| 1897 | // printf("b=%2d,=%5d\n",i,fadhd[i].board_id);
|
|---|
| 1898 | // }
|
|---|
| 1899 | return 0;
|
|---|
| 1900 | }
|
|---|
| 1901 |
|
|---|
| 1902 |
|
|---|
| 1903 | void
|
|---|
| 1904 | factStatNew (EVT_STAT gi)
|
|---|
| 1905 | {
|
|---|
| 1906 | int i;
|
|---|
| 1907 |
|
|---|
| 1908 | //for (i=0;i<MAX_SOCK;i++) {
|
|---|
| 1909 | // printf("%4d",gi.numRead[i]);
|
|---|
| 1910 | // if (i%20 == 0 ) printf("\n");
|
|---|
| 1911 | //}
|
|---|
| 1912 | }
|
|---|
| 1913 |
|
|---|
| 1914 | void
|
|---|
| 1915 | gotNewRun (int runnr, PEVNT_HEADER * headers)
|
|---|
| 1916 | {
|
|---|
| 1917 | printf ("got new run %d\n", runnr);
|
|---|
| 1918 | return;
|
|---|
| 1919 | }
|
|---|
| 1920 |
|
|---|
| 1921 | void
|
|---|
| 1922 | factStat (GUI_STAT gj)
|
|---|
| 1923 | {
|
|---|
| 1924 | // printf("stat: bfr%5lu skp%4lu free%4lu (tot%7lu) mem%12lu rd%12lu %3lu\n",
|
|---|
| 1925 | // array[0],array[1],array[2],array[3],array[4],array[5],array[6]);
|
|---|
| 1926 | }
|
|---|
| 1927 |
|
|---|
| 1928 |
|
|---|
| 1929 | void
|
|---|
| 1930 | debugRead (int isock, int ibyte, int32_t event, int32_t ftmevt, int32_t runnr,
|
|---|
| 1931 | int state, uint32_t tsec, uint32_t tusec)
|
|---|
| 1932 | {
|
|---|
| 1933 | // printf("%3d %5d %9d %3d %12d\n",isock, ibyte, event, state, tusec) ;
|
|---|
| 1934 | }
|
|---|
| 1935 |
|
|---|
| 1936 |
|
|---|
| 1937 |
|
|---|
| 1938 | void
|
|---|
| 1939 | debugStream (int isock, void *buf, int len)
|
|---|
| 1940 | {
|
|---|
| 1941 | }
|
|---|
| 1942 |
|
|---|
| 1943 | void
|
|---|
| 1944 | debugHead (int i, int j, void *buf)
|
|---|
| 1945 | {
|
|---|
| 1946 | }
|
|---|
| 1947 |
|
|---|
| 1948 |
|
|---|
| 1949 | void
|
|---|
| 1950 | factOut (int severity, int err, char *message)
|
|---|
| 1951 | {
|
|---|
| 1952 | static FILE *fd;
|
|---|
| 1953 | static int file = 0;
|
|---|
| 1954 |
|
|---|
| 1955 | if (file == 0) {
|
|---|
| 1956 | printf ("open file\n");
|
|---|
| 1957 | fd = fopen ("x.out", "w+");
|
|---|
| 1958 | file = 999;
|
|---|
| 1959 | }
|
|---|
| 1960 |
|
|---|
| 1961 | fprintf (fd, "%3d %3d | %s \n", severity, err, message);
|
|---|
| 1962 |
|
|---|
| 1963 | if (severity != kDebug)
|
|---|
| 1964 | printf ("%3d %3d | %s\n", severity, err, message);
|
|---|
| 1965 | }
|
|---|
| 1966 |
|
|---|
| 1967 |
|
|---|
| 1968 |
|
|---|
| 1969 | int
|
|---|
| 1970 | main ()
|
|---|
| 1971 | {
|
|---|
| 1972 | int i, b, c, p;
|
|---|
| 1973 | char ipStr[100];
|
|---|
| 1974 | struct in_addr IPaddr;
|
|---|
| 1975 |
|
|---|
| 1976 | g_maxMem = 1024 * 1024; //MBytes
|
|---|
| 1977 | g_maxMem = g_maxMem * 200; //100MBytes
|
|---|
| 1978 |
|
|---|
| 1979 | g_maxProc = 20;
|
|---|
| 1980 |
|
|---|
| 1981 | g_runStat = 40;
|
|---|
| 1982 |
|
|---|
| 1983 | i = 0;
|
|---|
| 1984 |
|
|---|
| 1985 | // version for standard crates
|
|---|
| 1986 | //for (c=0; c<4,c++) {
|
|---|
| 1987 | // for (b=0; b<10; b++) {
|
|---|
| 1988 | // sprintf(ipStr,"10.0.%d.%d",128+c,128+b)
|
|---|
| 1989 | //
|
|---|
| 1990 | // inet_pton(PF_INET, ipStr, &IPaddr) ;
|
|---|
| 1991 | //
|
|---|
| 1992 | // g_port[i].sockAddr.sin_family = PF_INET;
|
|---|
| 1993 | // g_port[i].sockAddr.sin_port = htons(5000) ;
|
|---|
| 1994 | // g_port[i].sockAddr.sin_addr = IPaddr ;
|
|---|
| 1995 | // g_port[i].sockDef = 1 ;
|
|---|
| 1996 | // i++ ;
|
|---|
| 1997 | // }
|
|---|
| 1998 | //}
|
|---|
| 1999 | //
|
|---|
| 2000 | //version for PC-test *
|
|---|
| 2001 | for (c = 0; c < 4; c++) {
|
|---|
| 2002 | for (b = 0; b < 10; b++) {
|
|---|
| 2003 | sprintf (ipStr, "10.0.%d.11", 128 + c);
|
|---|
| 2004 | if (c < 2)
|
|---|
| 2005 | sprintf (ipStr, "10.0.%d.11", 128);
|
|---|
| 2006 | else
|
|---|
| 2007 | sprintf (ipStr, "10.0.%d.11", 131);
|
|---|
| 2008 | // if (c==0) sprintf(ipStr,"10.0.100.11") ;
|
|---|
| 2009 |
|
|---|
| 2010 | inet_pton (PF_INET, ipStr, &IPaddr);
|
|---|
| 2011 | p = 31919 + 100 * c + 10 * b;
|
|---|
| 2012 |
|
|---|
| 2013 |
|
|---|
| 2014 | g_port[i].sockAddr.sin_family = PF_INET;
|
|---|
| 2015 | g_port[i].sockAddr.sin_port = htons (p);
|
|---|
| 2016 | g_port[i].sockAddr.sin_addr = IPaddr;
|
|---|
| 2017 | g_port[i].sockDef = 1;
|
|---|
| 2018 |
|
|---|
| 2019 | i++;
|
|---|
| 2020 | }
|
|---|
| 2021 | }
|
|---|
| 2022 |
|
|---|
| 2023 |
|
|---|
| 2024 | //g_port[17].sockDef =-1 ;
|
|---|
| 2025 | //g_actBoards-- ;
|
|---|
| 2026 |
|
|---|
| 2027 | StartEvtBuild ();
|
|---|
| 2028 |
|
|---|
| 2029 | return 0;
|
|---|
| 2030 |
|
|---|
| 2031 | }
|
|---|
| 2032 | #endif
|
|---|