| 1 |
|
|---|
| 2 | // // // #define EVTDEBUG
|
|---|
| 3 |
|
|---|
| 4 | #define NUMSOCK 1 //set to 7 for old configuration
|
|---|
| 5 | #define MAXREAD 65536 //64kB wiznet buffer
|
|---|
| 6 |
|
|---|
| 7 | #include <stdlib.h>
|
|---|
| 8 | #include <stdint.h>
|
|---|
| 9 | #include <stdarg.h>
|
|---|
| 10 | #include <unistd.h>
|
|---|
| 11 | #include <stdio.h>
|
|---|
| 12 | #include <sys/time.h>
|
|---|
| 13 | #include <arpa/inet.h>
|
|---|
| 14 | #include <string.h>
|
|---|
| 15 | #include <math.h>
|
|---|
| 16 | #include <error.h>
|
|---|
| 17 | #include <errno.h>
|
|---|
| 18 | #include <unistd.h>
|
|---|
| 19 | #include <sys/types.h>
|
|---|
| 20 | #include <sys/socket.h>
|
|---|
| 21 | #include <netinet/in.h>
|
|---|
| 22 | #include <netinet/tcp.h>
|
|---|
| 23 | #include <pthread.h>
|
|---|
| 24 | #include <sched.h>
|
|---|
| 25 |
|
|---|
| 26 | #include "EventBuilder.h"
|
|---|
| 27 |
|
|---|
| 28 | enum Severity
|
|---|
| 29 | {
|
|---|
| 30 | kMessage = 10, ///< Just a message, usually obsolete
|
|---|
| 31 | kInfo = 20, ///< An info telling something which can be interesting to know
|
|---|
| 32 | kWarn = 30, ///< A warning, things that somehow might result in unexpected or unwanted bahaviour
|
|---|
| 33 | kError = 40, ///< Error, something unexpected happened, but can still be handled by the program
|
|---|
| 34 | kFatal = 50, ///< An error which cannot be handled at all happend, the only solution is program termination
|
|---|
| 35 | kDebug = 99, ///< A message used for debugging only
|
|---|
| 36 | };
|
|---|
| 37 |
|
|---|
| 38 | #define MIN_LEN 32 // min #bytes needed to interpret FADheader
|
|---|
| 39 | #define MAX_LEN 256*1024 // size of read-buffer per socket
|
|---|
| 40 |
|
|---|
| 41 | //#define nanosleep(x,y)
|
|---|
| 42 |
|
|---|
| 43 | extern FileHandle_t runOpen (uint32_t irun, RUN_HEAD * runhd, size_t len);
|
|---|
| 44 | extern int runWrite (FileHandle_t fileHd, EVENT * event, size_t len);
|
|---|
| 45 | extern int runClose (FileHandle_t fileHd, RUN_TAIL * runth, size_t len);
|
|---|
| 46 | //extern int runFinish (uint32_t runnr);
|
|---|
| 47 |
|
|---|
| 48 | extern void factOut (int severity, int err, char *message);
|
|---|
| 49 | extern void factReportIncomplete (uint64_t rep);
|
|---|
| 50 |
|
|---|
| 51 | extern void gotNewRun (int runnr, PEVNT_HEADER * headers);
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | extern void factStat (GUI_STAT gj);
|
|---|
| 55 |
|
|---|
| 56 | extern void factStatNew (EVT_STAT gi);
|
|---|
| 57 |
|
|---|
| 58 | extern int eventCheck (uint32_t runNr, PEVNT_HEADER * fadhd, EVENT * event);
|
|---|
| 59 |
|
|---|
| 60 | extern int subProcEvt (int threadID, PEVNT_HEADER * fadhd, EVENT * event,
|
|---|
| 61 | int8_t * buffer);
|
|---|
| 62 |
|
|---|
| 63 | extern void debugHead (int i, int j, void *buf);
|
|---|
| 64 |
|
|---|
| 65 | extern void debugRead (int isock, int ibyte, int32_t event, int32_t ftmevt,
|
|---|
| 66 | int32_t runnr, int state, uint32_t tsec,
|
|---|
| 67 | uint32_t tusec);
|
|---|
| 68 | extern void debugStream (int isock, void *buf, int len);
|
|---|
| 69 |
|
|---|
| 70 | int CloseRunFile (uint32_t runId, uint32_t closeTime, uint32_t maxEvt);
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 | int g_maxProc;
|
|---|
| 77 | int g_maxSize;
|
|---|
| 78 | int gi_maxSize;
|
|---|
| 79 | int gi_maxProc;
|
|---|
| 80 |
|
|---|
| 81 | uint g_actTime;
|
|---|
| 82 | uint g_actUsec;
|
|---|
| 83 | int g_runStat;
|
|---|
| 84 | int g_reset;
|
|---|
| 85 | int g_useFTM;
|
|---|
| 86 |
|
|---|
| 87 | int gi_reset, gi_resetR, gi_resetS, gi_resetW, gi_resetX;
|
|---|
| 88 | size_t g_maxMem; //maximum memory allowed for buffer
|
|---|
| 89 |
|
|---|
| 90 | //no longer needed ...
|
|---|
| 91 | int g_maxBoards; //maximum number of boards to be initialized
|
|---|
| 92 | int g_actBoards;
|
|---|
| 93 | //
|
|---|
| 94 |
|
|---|
| 95 | FACT_SOCK g_port[NBOARDS]; // .addr=string of IP-addr in dotted-decimal "ddd.ddd.ddd.ddd"
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 | int gi_runStat;
|
|---|
| 99 | int gp_runStat;
|
|---|
| 100 | int gw_runStat;
|
|---|
| 101 |
|
|---|
| 102 | //int gi_memStat = +1;
|
|---|
| 103 |
|
|---|
| 104 | uint32_t actrun = 0;
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 | uint gi_NumConnect[NBOARDS]; //4 crates * 10 boards
|
|---|
| 108 |
|
|---|
| 109 | //uint gi_EvtStart= 0 ;
|
|---|
| 110 | //uint gi_EvtRead = 0 ;
|
|---|
| 111 | //uint gi_EvtBad = 0 ;
|
|---|
| 112 | //uint gi_EvtTot = 0 ;
|
|---|
| 113 | //size_t gi_usedMem = 0 ;
|
|---|
| 114 |
|
|---|
| 115 | //uint gw_EvtTot = 0 ;
|
|---|
| 116 | //uint gp_EvtTot = 0 ;
|
|---|
| 117 |
|
|---|
| 118 | PIX_MAP g_pixMap[NPIX];
|
|---|
| 119 |
|
|---|
| 120 | EVT_STAT gi;
|
|---|
| 121 | GUI_STAT gj;
|
|---|
| 122 |
|
|---|
| 123 | EVT_CTRL evtCtrl; //control of events during processing
|
|---|
| 124 | int evtIdx[MAX_EVT * MAX_RUN]; //index from mBuffer to evtCtrl
|
|---|
| 125 |
|
|---|
| 126 | WRK_DATA mBuffer[MAX_EVT * MAX_RUN]; //local working space
|
|---|
| 127 |
|
|---|
| 128 | //#define MXSTR 1000
|
|---|
| 129 | //char str[MXSTR];
|
|---|
| 130 |
|
|---|
| 131 | void factPrintf(int severity, int id, const char *fmt, ...)
|
|---|
| 132 | {
|
|---|
| 133 | char str[1000];
|
|---|
| 134 |
|
|---|
| 135 | va_list ap;
|
|---|
| 136 | va_start(ap, fmt);
|
|---|
| 137 | vsnprintf(str, 1000, fmt, ap);
|
|---|
| 138 | va_end(ap);
|
|---|
| 139 |
|
|---|
| 140 | factOut(severity, id, str);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 | #define THOMAS_MALLOC
|
|---|
| 145 |
|
|---|
| 146 | #ifdef THOMAS_MALLOC
|
|---|
| 147 | #define MAX_HEAD_MEM (NBOARDS * sizeof(PEVNT_HEADER))
|
|---|
| 148 | #define MAX_TOT_MEM (sizeof(EVENT) + (NPIX+NTMARK)*1024*2 + MAX_HEAD_MEM)
|
|---|
| 149 | typedef struct TGB_struct
|
|---|
| 150 | {
|
|---|
| 151 | struct TGB_struct *prev;
|
|---|
| 152 | void *mem;
|
|---|
| 153 | } TGB_entry;
|
|---|
| 154 |
|
|---|
| 155 | TGB_entry *tgb_last = NULL;
|
|---|
| 156 | uint64_t tgb_memory = 0;
|
|---|
| 157 | uint64_t tgb_inuse = 0;
|
|---|
| 158 |
|
|---|
| 159 | void *TGB_Malloc()
|
|---|
| 160 | {
|
|---|
| 161 | // No free slot available, next alloc would exceed max memory
|
|---|
| 162 | if (!tgb_last && tgb_memory+MAX_TOT_MEM>g_maxMem)
|
|---|
| 163 | return NULL;
|
|---|
| 164 |
|
|---|
| 165 | // We will return this amount of memory
|
|---|
| 166 | tgb_inuse += MAX_TOT_MEM;
|
|---|
| 167 |
|
|---|
| 168 | // No free slot available, allocate a new one
|
|---|
| 169 | if (!tgb_last)
|
|---|
| 170 | {
|
|---|
| 171 | tgb_memory += MAX_TOT_MEM;
|
|---|
| 172 | return malloc(MAX_TOT_MEM);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | // Get the next free slot from the stack and return it
|
|---|
| 176 | TGB_entry *last = tgb_last;
|
|---|
| 177 |
|
|---|
| 178 | TGB_entry *mem = last->mem;
|
|---|
| 179 | tgb_last = last->prev;
|
|---|
| 180 |
|
|---|
| 181 | free(last);
|
|---|
| 182 |
|
|---|
| 183 | return mem;
|
|---|
| 184 | };
|
|---|
| 185 |
|
|---|
| 186 | void TGB_free(void *mem)
|
|---|
| 187 | {
|
|---|
| 188 | // Add the last free slot to the stack
|
|---|
| 189 | TGB_entry *entry = malloc(sizeof(TGB_entry));
|
|---|
| 190 |
|
|---|
| 191 | entry->prev = tgb_last;
|
|---|
| 192 | entry->mem = mem;
|
|---|
| 193 |
|
|---|
| 194 | tgb_last = entry;
|
|---|
| 195 |
|
|---|
| 196 | // Decrease the amont of memory in use accordingly
|
|---|
| 197 | tgb_inuse -= MAX_TOT_MEM;
|
|---|
| 198 | }
|
|---|
| 199 | #endif
|
|---|
| 200 |
|
|---|
| 201 | #ifdef ETIENNE_MALLOC
|
|---|
| 202 | //ETIENNE
|
|---|
| 203 | #define MAX_SLOTS_PER_CHUNK 100
|
|---|
| 204 |
|
|---|
| 205 | typedef struct {
|
|---|
| 206 | int32_t eventNumber;
|
|---|
| 207 | int32_t chunk;
|
|---|
| 208 | int32_t slot;
|
|---|
| 209 | } CHUNK_MAPPING;
|
|---|
| 210 |
|
|---|
| 211 | CHUNK_MAPPING mBufferMapping[MAX_EVT * MAX_RUN];
|
|---|
| 212 |
|
|---|
| 213 | #define MAX_EVT_MEM (sizeof(EVENT) + NPIX*1024*2 + NTMARK*1024*2)
|
|---|
| 214 | #define MAX_HEAD_MEM (NBOARDS * sizeof(PEVNT_HEADER))
|
|---|
| 215 | #define MAX_SLOT_SIZE (MAX_EVT_MEM + MAX_HEAD_MEM)
|
|---|
| 216 | #define MAX_CHUNK_SIZE (MAX_SLOT_SIZE*MAX_SLOTS_PER_CHUNK)
|
|---|
| 217 |
|
|---|
| 218 | typedef struct {
|
|---|
| 219 | void * pointers[MAX_SLOTS_PER_CHUNK];
|
|---|
| 220 | int32_t events[MAX_SLOTS_PER_CHUNK];
|
|---|
| 221 | int32_t nFreeSlots;
|
|---|
| 222 | int32_t nSlots;
|
|---|
| 223 | } ETI_CHUNK;
|
|---|
| 224 |
|
|---|
| 225 | int32_t numAllocatedChunks = 0;
|
|---|
| 226 |
|
|---|
| 227 | #define MAX_CHUNKS 8096
|
|---|
| 228 | ETI_CHUNK EtiMemoryChunks[MAX_CHUNKS];
|
|---|
| 229 |
|
|---|
| 230 | void* ETI_Malloc(int evtId, int evtIndex)
|
|---|
| 231 | {
|
|---|
| 232 | for (int i=0;i<numAllocatedChunks;i++) {
|
|---|
| 233 | if (EtiMemoryChunks[i].nFreeSlots > 0) {
|
|---|
| 234 | for (int j=0;j<EtiMemoryChunks[i].nSlots;j++)
|
|---|
| 235 | {
|
|---|
| 236 | if (EtiMemoryChunks[i].events[j] == -1)
|
|---|
| 237 | {
|
|---|
| 238 | EtiMemoryChunks[i].events[j] = evtId;
|
|---|
| 239 | EtiMemoryChunks[i].nFreeSlots--;
|
|---|
| 240 | if (EtiMemoryChunks[i].nFreeSlots < 0)
|
|---|
| 241 | {
|
|---|
| 242 | factPrintf(kError, 0, "Number of free slot in chunk %d went below zero (%d) slot: %d", i, EtiMemoryChunks[i].nFreeSlots, j);
|
|---|
| 243 | return NULL;
|
|---|
| 244 | }
|
|---|
| 245 | mBufferMapping[evtIndex].eventNumber = evtId;
|
|---|
| 246 | mBufferMapping[evtIndex].chunk = i;
|
|---|
| 247 | mBufferMapping[evtIndex].slot = j;
|
|---|
| 248 | return EtiMemoryChunks[i].pointers[j];
|
|---|
| 249 | }
|
|---|
| 250 | }
|
|---|
| 251 | //If I reach this point then we have a problem because it should have found
|
|---|
| 252 | //a free spot just above.
|
|---|
| 253 | factPrintf(kError, 0, "Could not find a free slot in a chunk that's supposed to have some. chunk=%d", i);
|
|---|
| 254 | return NULL;
|
|---|
| 255 | }
|
|---|
| 256 | }
|
|---|
| 257 | //If we reach this point this means that we should allocate more memory
|
|---|
| 258 | int32_t numNewSlots = MAX_SLOTS_PER_CHUNK;
|
|---|
| 259 | if ((numAllocatedChunks + 1)*MAX_CHUNK_SIZE >= g_maxMem)
|
|---|
| 260 | return NULL;
|
|---|
| 261 |
|
|---|
| 262 | EtiMemoryChunks[numAllocatedChunks].pointers[0] = malloc(MAX_SLOT_SIZE*MAX_SLOTS_PER_CHUNK);
|
|---|
| 263 | if (EtiMemoryChunks[numAllocatedChunks].pointers[0] == NULL)
|
|---|
| 264 | {
|
|---|
| 265 | factPrintf(kError, 0, "Allocation of %lu bytes failed. %d chunks are currently allocated (max allowed %lu bytes)", MAX_CHUNK_SIZE, numAllocatedChunks, g_maxMem);
|
|---|
| 266 | return NULL;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | EtiMemoryChunks[numAllocatedChunks].nSlots = numNewSlots;
|
|---|
| 270 | EtiMemoryChunks[numAllocatedChunks].events[0] = evtId;
|
|---|
| 271 | EtiMemoryChunks[numAllocatedChunks].nFreeSlots = numNewSlots-1;
|
|---|
| 272 | mBufferMapping[evtIndex].eventNumber = evtId;
|
|---|
| 273 | mBufferMapping[evtIndex].chunk = numAllocatedChunks;
|
|---|
| 274 | mBufferMapping[evtIndex].slot = 0;
|
|---|
| 275 |
|
|---|
| 276 | for (int i=1;i<numNewSlots;i++)
|
|---|
| 277 | {
|
|---|
| 278 | EtiMemoryChunks[numAllocatedChunks].pointers[i] = (char*)EtiMemoryChunks[numAllocatedChunks].pointers[0] + i*MAX_SLOT_SIZE;// &(((char*)(EtiMemoryChunks[numAllocatedChunks].pointers[i-1]))[MAX_SLOT_SIZE]);
|
|---|
| 279 | EtiMemoryChunks[numAllocatedChunks].events[i] = -1;
|
|---|
| 280 | }
|
|---|
| 281 | numAllocatedChunks++;
|
|---|
| 282 |
|
|---|
| 283 | return EtiMemoryChunks[numAllocatedChunks-1].pointers[0];
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | void ETI_Free(int evtId, int evtIndex)
|
|---|
| 287 | {
|
|---|
| 288 | ETI_CHUNK* currentChunk = &EtiMemoryChunks[mBufferMapping[evtIndex].chunk];
|
|---|
| 289 | if (currentChunk->events[mBufferMapping[evtIndex].slot] != evtId)
|
|---|
| 290 | {
|
|---|
| 291 | factPrintf(kError, 0, "Mismatch in chunk mapping table. Expected evtId %d. Got %d. No memory was freed.", evtId, currentChunk->events[mBufferMapping[evtIndex].slot]);
|
|---|
| 292 | return;
|
|---|
| 293 | }
|
|---|
| 294 | currentChunk->events[mBufferMapping[evtIndex].slot] = -1;
|
|---|
| 295 | currentChunk->nFreeSlots++;
|
|---|
| 296 |
|
|---|
| 297 | return; /* TEST */
|
|---|
| 298 |
|
|---|
| 299 | int chunkIndex = mBufferMapping[evtIndex].chunk;
|
|---|
| 300 | if (chunkIndex != numAllocatedChunks-1)
|
|---|
| 301 | return;
|
|---|
| 302 |
|
|---|
| 303 | while (EtiMemoryChunks[chunkIndex].nFreeSlots == EtiMemoryChunks[chunkIndex].nSlots)
|
|---|
| 304 | {//free this chunk
|
|---|
| 305 | if (EtiMemoryChunks[chunkIndex].pointers[0] == NULL)
|
|---|
| 306 | {
|
|---|
| 307 | factPrintf(kError, 0, "Chunk %d not allocated as it ought to be. Skipping memory release.", chunkIndex);
|
|---|
| 308 | return;
|
|---|
| 309 | }
|
|---|
| 310 | free(EtiMemoryChunks[chunkIndex].pointers[0]);
|
|---|
| 311 | EtiMemoryChunks[chunkIndex].pointers[0] = NULL;
|
|---|
| 312 | EtiMemoryChunks[chunkIndex].nSlots = 0;
|
|---|
| 313 | numAllocatedChunks--;
|
|---|
| 314 | chunkIndex--;
|
|---|
| 315 | if (numAllocatedChunks == 0)
|
|---|
| 316 | break;
|
|---|
| 317 | }
|
|---|
| 318 | }
|
|---|
| 319 | //END ETIENNE
|
|---|
| 320 | #endif
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 | RUN_HEAD actRun;
|
|---|
| 324 |
|
|---|
| 325 | RUN_CTRL runCtrl[MAX_RUN];
|
|---|
| 326 |
|
|---|
| 327 | RUN_TAIL runTail[MAX_RUN];
|
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 | /*
|
|---|
| 331 | *** Definition of rdBuffer to read in IP packets; keep it global !!!!
|
|---|
| 332 | */
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 | typedef union
|
|---|
| 336 | {
|
|---|
| 337 | int8_t B[MAX_LEN];
|
|---|
| 338 | int16_t S[MAX_LEN / 2];
|
|---|
| 339 | int32_t I[MAX_LEN / 4];
|
|---|
| 340 | int64_t L[MAX_LEN / 8];
|
|---|
| 341 | } CNV_FACT;
|
|---|
| 342 |
|
|---|
| 343 | typedef struct
|
|---|
| 344 | {
|
|---|
| 345 | int bufTyp; //what are we reading at the moment: 0=header 1=data -1=skip ...
|
|---|
| 346 | int32_t bufPos; //next byte to read to the buffer next
|
|---|
| 347 | int32_t bufLen; //number of bytes left to read
|
|---|
| 348 | // size_t bufLen; //number of bytes left to read size_t might be better
|
|---|
| 349 | int32_t skip; //number of bytes skipped before start of event
|
|---|
| 350 |
|
|---|
| 351 | int errCnt; //how often connect failed since last successful
|
|---|
| 352 | int sockStat; //-1 if socket not yet connected , 99 if not exist
|
|---|
| 353 | int socket; //contains the sockets
|
|---|
| 354 | struct sockaddr_in SockAddr; //IP for each socket
|
|---|
| 355 |
|
|---|
| 356 | int evtID; // event ID of event currently read
|
|---|
| 357 | int runID; // run "
|
|---|
| 358 | int ftmID; // event ID from FTM
|
|---|
| 359 | uint fadLen; // FADlength of event currently read
|
|---|
| 360 | int fadVers; // Version of FAD
|
|---|
| 361 | int ftmTyp; // trigger type
|
|---|
| 362 | int board; // boardID (softwareID: 0..40 )
|
|---|
| 363 | int Port;
|
|---|
| 364 |
|
|---|
| 365 | CNV_FACT *rBuf;
|
|---|
| 366 |
|
|---|
| 367 | } READ_STRUCT;
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 | typedef union
|
|---|
| 371 | {
|
|---|
| 372 | int8_t B[2];
|
|---|
| 373 | int16_t S;
|
|---|
| 374 | } SHORT_BYTE;
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 | SHORT_BYTE start, stop;
|
|---|
| 381 |
|
|---|
| 382 | READ_STRUCT rd[MAX_SOCK]; //buffer to read IP and afterwards store in mBuffer
|
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 | /*-----------------------------------------------------------------*/
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 | /*-----------------------------------------------------------------*/
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 | int
|
|---|
| 394 | runFinish1 (uint32_t runnr)
|
|---|
| 395 | {
|
|---|
| 396 | factPrintf(kInfo, 173, "Should finish(1) run %d (but not yet possible)", runnr);
|
|---|
| 397 | return 0;
|
|---|
| 398 | }
|
|---|
| 399 | int
|
|---|
| 400 | runFinish (uint32_t runnr)
|
|---|
| 401 | {
|
|---|
| 402 | factPrintf(kInfo, 173, "Should finish run %d (but not yet possible)", runnr);
|
|---|
| 403 | return 0;
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | int
|
|---|
| 407 | GenSock (int flag, int sid, int port, struct sockaddr_in *sockAddr,
|
|---|
| 408 | READ_STRUCT * rd)
|
|---|
| 409 | {
|
|---|
| 410 | /*
|
|---|
| 411 | *** generate Address, create sockets and allocates readbuffer for it
|
|---|
| 412 | ***
|
|---|
| 413 | *** if flag==0 generate socket and buffer
|
|---|
| 414 | *** <0 destroy socket and buffer
|
|---|
| 415 | *** >0 close and redo socket
|
|---|
| 416 | ***
|
|---|
| 417 | *** sid : board*7 + port id
|
|---|
| 418 | */
|
|---|
| 419 |
|
|---|
| 420 | int j;
|
|---|
| 421 | int optval = 1; //activate keepalive
|
|---|
| 422 | socklen_t optlen = sizeof (optval);
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 | if (sid % 7 >= NUMSOCK) {
|
|---|
| 426 | //this is a not used socket, so do nothing ...
|
|---|
| 427 | rd->sockStat = 77;
|
|---|
| 428 | rd->rBuf = NULL ;
|
|---|
| 429 | return 0;
|
|---|
| 430 | }
|
|---|
| 431 |
|
|---|
| 432 | if (rd->sockStat == 0) { //close socket if open
|
|---|
| 433 | j = close (rd->socket);
|
|---|
| 434 | if (j > 0) {
|
|---|
| 435 | factPrintf(kFatal, 771, "Closing socket %d failed: %m (close,rc=%d)", sid, errno);
|
|---|
| 436 | } else {
|
|---|
| 437 | factPrintf(kInfo, 771, "Succesfully closed socket %d", sid);
|
|---|
| 438 | }
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | rd->sockStat = 99;
|
|---|
| 442 |
|
|---|
| 443 | if (flag < 0) {
|
|---|
| 444 | free (rd->rBuf); //and never open again
|
|---|
| 445 | rd->rBuf = NULL;
|
|---|
| 446 | return 0;
|
|---|
| 447 | }
|
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 | if (flag == 0) { //generate address and buffer ...
|
|---|
| 451 | rd->Port = port;
|
|---|
| 452 | rd->SockAddr.sin_family = sockAddr->sin_family;
|
|---|
| 453 | rd->SockAddr.sin_port = htons (port);
|
|---|
| 454 | rd->SockAddr.sin_addr = sockAddr->sin_addr;
|
|---|
| 455 |
|
|---|
| 456 | rd->rBuf = malloc (sizeof (CNV_FACT));
|
|---|
| 457 | if (rd->rBuf == NULL) {
|
|---|
| 458 | factPrintf(kFatal, 774, "Could not create local buffer %d (malloc failed)", sid);
|
|---|
| 459 | rd->sockStat = 77;
|
|---|
| 460 | return -3;
|
|---|
| 461 | }
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 | if ((rd->socket = socket (PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) <= 0) {
|
|---|
| 466 | factPrintf(kFatal, 773, "Generating socket %d failed: %m (socket,rc=%d)", sid, errno);
|
|---|
| 467 | rd->sockStat = 88;
|
|---|
| 468 | return -2;
|
|---|
| 469 | }
|
|---|
| 470 | optval = 1;
|
|---|
| 471 | if (setsockopt (rd->socket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
|
|---|
| 472 | factPrintf(kInfo, 173, "Setting SO_KEEPALIVE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
|
|---|
| 473 | }
|
|---|
| 474 | optval = 10; //start after 10 seconds
|
|---|
| 475 | if (setsockopt (rd->socket, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) {
|
|---|
| 476 | factPrintf(kInfo, 173, "Setting TCP_KEEPIDLE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
|
|---|
| 477 | }
|
|---|
| 478 | optval = 10; //do every 10 seconds
|
|---|
| 479 | if (setsockopt (rd->socket, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) {
|
|---|
| 480 | factPrintf(kInfo, 173, "Setting TCP_KEEPINTVL for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
|
|---|
| 481 | }
|
|---|
| 482 | optval = 2; //close after 2 unsuccessful tries
|
|---|
| 483 | if (setsockopt (rd->socket, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) {
|
|---|
| 484 | factPrintf(kInfo, 173, "Setting TCP_KEEPCNT for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | factPrintf(kInfo, 773, "Successfully generated socket %d", sid);
|
|---|
| 488 |
|
|---|
| 489 | rd->sockStat = -1; //try to (re)open socket
|
|---|
| 490 | rd->errCnt = 0;
|
|---|
| 491 | return 0;
|
|---|
| 492 |
|
|---|
| 493 | } /*-----------------------------------------------------------------*/
|
|---|
| 494 |
|
|---|
| 495 | /*-----------------------------------------------------------------*/
|
|---|
| 496 |
|
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 | int
|
|---|
| 501 | mBufInit ()
|
|---|
| 502 | {
|
|---|
| 503 | // initialize mBuffer (mark all entries as unused\empty)
|
|---|
| 504 |
|
|---|
| 505 | uint32_t actime = g_actTime + 50000000;
|
|---|
| 506 |
|
|---|
| 507 | for (int i = 0; i < MAX_EVT * MAX_RUN; i++) {
|
|---|
| 508 | mBuffer[i].evNum = mBuffer[i].nRoi = -1;
|
|---|
| 509 | mBuffer[i].runNum = 0;
|
|---|
| 510 |
|
|---|
| 511 | evtCtrl.evtBuf[i] = -1;
|
|---|
| 512 | evtCtrl.evtStat[i] = -1;
|
|---|
| 513 | evtCtrl.pcTime[i] = actime; //initiate to far future
|
|---|
| 514 |
|
|---|
| 515 | #ifdef ETIENNE_MALLOC
|
|---|
| 516 | //ETIENNE
|
|---|
| 517 | mBufferMapping[i].chunk = -1;
|
|---|
| 518 | mBufferMapping[i].eventNumber = -1;
|
|---|
| 519 | mBufferMapping[i].slot = -1;
|
|---|
| 520 | //END ETIENNE
|
|---|
| 521 | #endif
|
|---|
| 522 | }
|
|---|
| 523 | #ifdef ETIENNE_MALLOC
|
|---|
| 524 | for (int j=0;j<MAX_CHUNKS;j++)
|
|---|
| 525 | EtiMemoryChunks[j].pointers[0] = NULL;
|
|---|
| 526 | #endif
|
|---|
| 527 |
|
|---|
| 528 | actRun.FADhead = malloc (NBOARDS * sizeof (PEVNT_HEADER));
|
|---|
| 529 |
|
|---|
| 530 | evtCtrl.frstPtr = 0;
|
|---|
| 531 | evtCtrl.lastPtr = 0;
|
|---|
| 532 |
|
|---|
| 533 | return 0;
|
|---|
| 534 |
|
|---|
| 535 | } /*-----------------------------------------------------------------*/
|
|---|
| 536 |
|
|---|
| 537 | void swapEventHeaderBytes(int i);
|
|---|
| 538 | int checkRoiConsistency(int i, int roi[]);
|
|---|
| 539 |
|
|---|
| 540 | int mBufEvt (int sk)
|
|---|
| 541 | /*(int evID, uint runID, int nRoi[], int sk, int fadlen, int trgTyp, int trgNum, int fadNum)*/
|
|---|
| 542 | {
|
|---|
| 543 | // generate a new Event into mBuffer:
|
|---|
| 544 | // make sure only complete Event are possible, so 'free' will always work
|
|---|
| 545 | // returns index into mBuffer[], or negative value in case of error
|
|---|
| 546 | // error: <-9000 if roi screwed up (not consistent with run)
|
|---|
| 547 | // <-8000 (not consistent with event)
|
|---|
| 548 | // <-7000 (not consistent with board)
|
|---|
| 549 | // < 0 if no space left
|
|---|
| 550 |
|
|---|
| 551 | // int evFree;
|
|---|
| 552 | // int headmem = 0;
|
|---|
| 553 | // size_t needmem = 0;
|
|---|
| 554 |
|
|---|
| 555 | swapEventHeaderBytes(sk);
|
|---|
| 556 |
|
|---|
| 557 | int nRoi[9];
|
|---|
| 558 | if (!checkRoiConsistency(sk, nRoi))
|
|---|
| 559 | return -9999;
|
|---|
| 560 |
|
|---|
| 561 | const int b = sk / 7;
|
|---|
| 562 |
|
|---|
| 563 | if (nRoi[0] < 0 || nRoi[0] > 1024) {
|
|---|
| 564 | factPrintf(kError, 999, "Illegal roi in channel 0: %d (allowed: 0<=roi<=1024)", nRoi[0]);
|
|---|
| 565 | gj.badRoiR++;
|
|---|
| 566 | gj.badRoi[b]++;
|
|---|
| 567 | return -9999;
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 | for (int jr = 1; jr < 8; jr++) {
|
|---|
| 571 | if (nRoi[jr] != nRoi[0]) {
|
|---|
| 572 | factPrintf(kError, 711, "Mismatch of roi (%d) in channel %d with roi (%d) in channel 0.", nRoi[jr], jr, nRoi[0]);
|
|---|
| 573 | gj.badRoiB++;
|
|---|
| 574 | gj.badRoi[b]++;
|
|---|
| 575 | return -7101;
|
|---|
| 576 | }
|
|---|
| 577 | }
|
|---|
| 578 | if (nRoi[8] < nRoi[0]) {
|
|---|
| 579 | factPrintf(kError, 712, "Mismatch of roi (%d) in channel 8. Should be larger or equal than the roi (%d) in channel 0.", nRoi[8], nRoi[0]);
|
|---|
| 580 | gj.badRoiB++;
|
|---|
| 581 | gj.badRoi[b]++;
|
|---|
| 582 | return -7102;
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | const int evID = rd[sk].evtID;
|
|---|
| 586 | const uint runID = rd[sk].runID;
|
|---|
| 587 | const int fadlen = rd[sk].fadLen;
|
|---|
| 588 | const int trgTyp = rd[sk].ftmTyp;
|
|---|
| 589 | const int trgNum = rd[sk].ftmID;
|
|---|
| 590 | const int fadNum = rd[sk].evtID;
|
|---|
| 591 |
|
|---|
| 592 | int i = evID % MAX_EVT;
|
|---|
| 593 | int evFree = -1;
|
|---|
| 594 |
|
|---|
| 595 | for (int k = 0; k < MAX_RUN; k++) {
|
|---|
| 596 | if (mBuffer[i].evNum == evID && mBuffer[i].runNum == runID) { //event is already registered;
|
|---|
| 597 | // is it ok ????
|
|---|
| 598 | if (mBuffer[i].nRoi != nRoi[0]
|
|---|
| 599 | || mBuffer[i].nRoiTM != nRoi[8]) {
|
|---|
| 600 | factPrintf(kError, 821, "Mismatch of roi within event. Expected roi=%d and roi_tm=%d, got %d and %d.",
|
|---|
| 601 | mBuffer[i].nRoi, mBuffer[i].nRoiTM, nRoi[0], nRoi[8]);
|
|---|
| 602 | gj.badRoiE++;
|
|---|
| 603 | gj.badRoi[b]++;
|
|---|
| 604 | return -8201;
|
|---|
| 605 | }
|
|---|
| 606 | // count for inconsistencies
|
|---|
| 607 |
|
|---|
| 608 | if (mBuffer[i].trgNum != trgNum)
|
|---|
| 609 | mBuffer[i].Errors[0]++;
|
|---|
| 610 | if (mBuffer[i].fadNum != fadNum)
|
|---|
| 611 | mBuffer[i].Errors[1]++;
|
|---|
| 612 | if (mBuffer[i].trgTyp != trgTyp)
|
|---|
| 613 | mBuffer[i].Errors[2]++;
|
|---|
| 614 |
|
|---|
| 615 | //everything seems fine so far ==> use this slot ....
|
|---|
| 616 | return i;
|
|---|
| 617 | }
|
|---|
| 618 | if (evFree < 0 && mBuffer[i].evNum < 0)
|
|---|
| 619 | evFree = i;
|
|---|
| 620 | i += MAX_EVT;
|
|---|
| 621 | }
|
|---|
| 622 |
|
|---|
| 623 |
|
|---|
| 624 | //event does not yet exist; create it
|
|---|
| 625 | if (evFree < 0) { //no space available in ctrl
|
|---|
| 626 | factPrintf(kError, 881, "No control slot to keep event %d", evID);
|
|---|
| 627 | return -1;
|
|---|
| 628 | }
|
|---|
| 629 | i = evFree; //found free entry; use it ...
|
|---|
| 630 |
|
|---|
| 631 | struct timeval tv;
|
|---|
| 632 | gettimeofday (&tv, NULL);
|
|---|
| 633 |
|
|---|
| 634 | const uint32_t tsec = tv.tv_sec;
|
|---|
| 635 | const uint32_t tusec = tv.tv_usec;
|
|---|
| 636 |
|
|---|
| 637 | //check if runId already registered in runCtrl
|
|---|
| 638 | evFree = -1;
|
|---|
| 639 |
|
|---|
| 640 | uint oldest = g_actTime + 1000;
|
|---|
| 641 | int jold = -1;
|
|---|
| 642 |
|
|---|
| 643 | for (int k = 0; k < MAX_RUN; k++) {
|
|---|
| 644 | if (runCtrl[k].runId == runID) {
|
|---|
| 645 | // if (runCtrl[k].procId > 0) { //run is closed -> reject
|
|---|
| 646 | // snprintf (str, MXSTR, "skip event since run %d finished", runID);
|
|---|
| 647 | // factOut (kInfo, 931, str);
|
|---|
| 648 | // return -21;
|
|---|
| 649 | // }
|
|---|
| 650 |
|
|---|
| 651 | if (runCtrl[k].roi0 != nRoi[0]
|
|---|
| 652 | || runCtrl[k].roi8 != nRoi[8]) {
|
|---|
| 653 | factPrintf(kError, 931, "Mismatch of roi within run. Expected roi=%d and roi_tm=%d, got %d and %d.",
|
|---|
| 654 | runCtrl[k].roi0, runCtrl[k].roi8, nRoi[0], nRoi[8]);
|
|---|
| 655 | gj.badRoiR++;
|
|---|
| 656 | gj.badRoi[b]++;
|
|---|
| 657 | return -9301;
|
|---|
| 658 | }
|
|---|
| 659 | goto RUNFOUND;
|
|---|
| 660 | } else if (evFree < 0 && runCtrl[k].fileId < 0) { //not yet used
|
|---|
| 661 | evFree = k;
|
|---|
| 662 | } else if (evFree < 0 && runCtrl[k].fileId > 0) { //already closed
|
|---|
| 663 | if (runCtrl[k].closeTime < oldest) {
|
|---|
| 664 | oldest = runCtrl[k].closeTime;
|
|---|
| 665 | jold = k;
|
|---|
| 666 | }
|
|---|
| 667 | }
|
|---|
| 668 | }
|
|---|
| 669 |
|
|---|
| 670 | if (evFree < 0 && jold < 0) {
|
|---|
| 671 | factPrintf(kFatal, 883, "Not able to register the new run %d", runID);
|
|---|
| 672 | return -1001;
|
|---|
| 673 | }
|
|---|
| 674 |
|
|---|
| 675 | if (evFree < 0)
|
|---|
| 676 | evFree = jold;
|
|---|
| 677 | factPrintf(kInfo, 503, "New run %d (evFree=%d) registered with roi=%d and roi_tm=%d", runID,
|
|---|
| 678 | evFree, nRoi[0], nRoi[8]);
|
|---|
| 679 | runCtrl[evFree].runId = runID;
|
|---|
| 680 | runCtrl[evFree].roi0 = nRoi[0];
|
|---|
| 681 | runCtrl[evFree].roi8 = nRoi[8];
|
|---|
| 682 | runCtrl[evFree].fileId = -2;
|
|---|
| 683 | runCtrl[evFree].procId = -2;
|
|---|
| 684 | runCtrl[evFree].lastEvt = -1;
|
|---|
| 685 | runCtrl[evFree].nextEvt = 0;
|
|---|
| 686 | runCtrl[evFree].actEvt = 0;
|
|---|
| 687 | runCtrl[evFree].procEvt = 0;
|
|---|
| 688 | runCtrl[evFree].maxEvt = 999999999; //max number events allowed
|
|---|
| 689 | runCtrl[evFree].firstUsec = tusec;
|
|---|
| 690 | runCtrl[evFree].firstTime = runCtrl[evFree].lastTime = tsec;
|
|---|
| 691 | runCtrl[evFree].closeTime = tsec + 3600 * 24; //max time allowed
|
|---|
| 692 | // runCtrl[evFree].lastTime = 0;
|
|---|
| 693 |
|
|---|
| 694 | runTail[evFree].nEventsOk =
|
|---|
| 695 | runTail[evFree].nEventsRej =
|
|---|
| 696 | runTail[evFree].nEventsBad =
|
|---|
| 697 | runTail[evFree].PCtime0 = runTail[evFree].PCtimeX = 0;
|
|---|
| 698 |
|
|---|
| 699 | RUNFOUND:
|
|---|
| 700 | //ETIENNE
|
|---|
| 701 | /* needmem = sizeof (EVENT) + NPIX * nRoi[0] * 2 + NTMARK * nRoi[0] * 2; //
|
|---|
| 702 |
|
|---|
| 703 | headmem = NBOARDS * sizeof (PEVNT_HEADER);
|
|---|
| 704 |
|
|---|
| 705 | if (gj.usdMem + needmem + headmem + gi_maxSize > g_maxMem) {
|
|---|
| 706 | gj.maxMem = gj.usdMem + needmem + headmem + gi_maxSize;
|
|---|
| 707 | if (gi_memStat > 0) {
|
|---|
| 708 | gi_memStat = -99;
|
|---|
| 709 | snprintf (str, MXSTR, "no memory left to keep event %6d sock %3d",
|
|---|
| 710 | evID, sk);
|
|---|
| 711 | factOut (kError, 882, str);
|
|---|
| 712 | } else {
|
|---|
| 713 | snprintf (str, MXSTR, "no memory left to keep event %6d sock %3d",
|
|---|
| 714 | evID, sk);
|
|---|
| 715 | factOut (kDebug, 882, str);
|
|---|
| 716 | }
|
|---|
| 717 | return -11;
|
|---|
| 718 | }
|
|---|
| 719 | */
|
|---|
| 720 |
|
|---|
| 721 | #ifdef THOMAS_MALLOC
|
|---|
| 722 | mBuffer[i].FADhead = TGB_Malloc();
|
|---|
| 723 | mBuffer[i].fEvent = NULL;
|
|---|
| 724 | mBuffer[i].buffer = NULL;
|
|---|
| 725 | if (mBuffer[i].FADhead == NULL) {
|
|---|
| 726 | factPrintf(kError, 882, "malloc header failed for event %d", evID);
|
|---|
| 727 | return -12;
|
|---|
| 728 | }
|
|---|
| 729 | mBuffer[i].fEvent = (void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM);
|
|---|
| 730 | #endif
|
|---|
| 731 |
|
|---|
| 732 | #ifdef ETIENNE_MALLOC
|
|---|
| 733 | mBuffer[i].FADhead = ETI_Malloc(evID, i);
|
|---|
| 734 | mBuffer[i].buffer = NULL;
|
|---|
| 735 | if (mBuffer[i].FADhead != NULL)
|
|---|
| 736 | mBuffer[i].fEvent = (EVENT*)&(((char*)(mBuffer[i].FADhead))[MAX_HEAD_MEM]);
|
|---|
| 737 | else
|
|---|
| 738 | {
|
|---|
| 739 | mBuffer[i].fEvent = NULL;
|
|---|
| 740 | gj.usdMem = 0;
|
|---|
| 741 | for (int k=0;k<numAllocatedChunks;k++)
|
|---|
| 742 | gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE;
|
|---|
| 743 | if (gj.usdMem > gj.maxMem)
|
|---|
| 744 | gj.maxMem = gj.usdMem;
|
|---|
| 745 | /*if (gi_memStat > 0) {
|
|---|
| 746 | gi_memStat = -99;
|
|---|
| 747 | snprintf (str, MXSTR, "No memory left to keep event %6d sock %3d",
|
|---|
| 748 | evID, sk);
|
|---|
| 749 | factOut (kError, 882, str);
|
|---|
| 750 | }*/
|
|---|
| 751 | #ifdef EVTDEBUG
|
|---|
| 752 | else
|
|---|
| 753 | {
|
|---|
| 754 | factPrintf(kDebug, 882, "No memory left to keep event %6d sock %3d", evID, sk);
|
|---|
| 755 | }
|
|---|
| 756 | #endif
|
|---|
| 757 | return -11;
|
|---|
| 758 | }
|
|---|
| 759 | #endif
|
|---|
| 760 |
|
|---|
| 761 | #ifdef STANDARD_MALLOC
|
|---|
| 762 |
|
|---|
| 763 | mBuffer[i].FADhead = malloc (MAX_HEAD_MEM+MAX_EVT_MEM);
|
|---|
| 764 | mBuffer[i].fEvent = NULL;
|
|---|
| 765 | mBuffer[i].buffer = NULL;
|
|---|
| 766 | if (mBuffer[i].FADhead == NULL) {
|
|---|
| 767 | factPrintf(kError, 882, "malloc header failed for event %d", evID);
|
|---|
| 768 | return -12;
|
|---|
| 769 | }
|
|---|
| 770 | mBuffer[i].fEvent = (void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM);
|
|---|
| 771 | #endif
|
|---|
| 772 |
|
|---|
| 773 | /*
|
|---|
| 774 | mBuffer[i].FADhead = malloc (headmem);
|
|---|
| 775 | if (mBuffer[i].FADhead == NULL) {
|
|---|
| 776 | snprintf (str, MXSTR, "malloc header failed for event %d", evID);
|
|---|
| 777 | factOut (kError, 882, str);
|
|---|
| 778 | return -12;
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | mBuffer[i].fEvent = malloc (needmem);
|
|---|
| 782 | if (mBuffer[i].fEvent == NULL) {
|
|---|
| 783 | snprintf (str, MXSTR, "malloc data failed for event %d", evID);
|
|---|
| 784 | factOut (kError, 882, str);
|
|---|
| 785 | free (mBuffer[i].FADhead);
|
|---|
| 786 | mBuffer[i].FADhead = NULL;
|
|---|
| 787 | return -22;
|
|---|
| 788 | }
|
|---|
| 789 |
|
|---|
| 790 | mBuffer[i].buffer = malloc (gi_maxSize);
|
|---|
| 791 | if (mBuffer[i].buffer == NULL) {
|
|---|
| 792 | snprintf (str, MXSTR, "malloc buffer failed for event %d", evID);
|
|---|
| 793 | factOut (kError, 882, str);
|
|---|
| 794 | free (mBuffer[i].FADhead);
|
|---|
| 795 | mBuffer[i].FADhead = NULL;
|
|---|
| 796 | free (mBuffer[i].fEvent);
|
|---|
| 797 | mBuffer[i].fEvent = NULL;
|
|---|
| 798 | return -32;
|
|---|
| 799 | }*/
|
|---|
| 800 | //END ETIENNE
|
|---|
| 801 | //flag all boards as unused
|
|---|
| 802 | mBuffer[i].nBoard = 0;
|
|---|
| 803 | for (int k = 0; k < NBOARDS; k++) {
|
|---|
| 804 | mBuffer[i].board[k] = -1;
|
|---|
| 805 | }
|
|---|
| 806 | //flag all pixels as unused
|
|---|
| 807 | for (int k = 0; k < NPIX; k++) {
|
|---|
| 808 | mBuffer[i].fEvent->StartPix[k] = -1;
|
|---|
| 809 | }
|
|---|
| 810 | //flag all TMark as unused
|
|---|
| 811 | for (int k = 0; k < NTMARK; k++) {
|
|---|
| 812 | mBuffer[i].fEvent->StartTM[k] = -1;
|
|---|
| 813 | }
|
|---|
| 814 |
|
|---|
| 815 | mBuffer[i].fEvent->NumBoards = 0;
|
|---|
| 816 | mBuffer[i].fEvent->PCUsec = tusec;
|
|---|
| 817 | mBuffer[i].fEvent->PCTime = mBuffer[i].pcTime = tsec;
|
|---|
| 818 | mBuffer[i].nRoi = nRoi[0];
|
|---|
| 819 | mBuffer[i].nRoiTM = nRoi[8];
|
|---|
| 820 | mBuffer[i].evNum = evID;
|
|---|
| 821 | mBuffer[i].runNum = runID;
|
|---|
| 822 | mBuffer[i].fadNum = fadNum;
|
|---|
| 823 | mBuffer[i].trgNum = trgNum;
|
|---|
| 824 | mBuffer[i].trgTyp = trgTyp;
|
|---|
| 825 | // mBuffer[i].evtLen = needmem;
|
|---|
| 826 | mBuffer[i].Errors[0] =
|
|---|
| 827 | mBuffer[i].Errors[1] = mBuffer[i].Errors[2] = mBuffer[i].Errors[3] = 0;
|
|---|
| 828 |
|
|---|
| 829 | #ifdef ETIENNE_MALLOC
|
|---|
| 830 | //ETIENNE
|
|---|
| 831 | //gj.usdMem += needmem + headmem + gi_maxSize;
|
|---|
| 832 | gj.usdMem = 0;
|
|---|
| 833 | for (int k=0;k<numAllocatedChunks;k++)
|
|---|
| 834 | {
|
|---|
| 835 | gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE;
|
|---|
| 836 | }
|
|---|
| 837 | //END ETIENNE
|
|---|
| 838 | #endif
|
|---|
| 839 |
|
|---|
| 840 | #ifdef THOMAS_MALLOC
|
|---|
| 841 | gj.usdMem = tgb_inuse;
|
|---|
| 842 | #endif
|
|---|
| 843 |
|
|---|
| 844 | if (gj.usdMem > gj.maxMem)
|
|---|
| 845 | gj.maxMem = gj.usdMem;
|
|---|
| 846 |
|
|---|
| 847 | gj.bufTot++;
|
|---|
| 848 | if (gj.bufTot > gj.maxEvt)
|
|---|
| 849 | gj.maxEvt = gj.bufTot;
|
|---|
| 850 |
|
|---|
| 851 | gj.rateNew++;
|
|---|
| 852 |
|
|---|
| 853 | //register event in 'active list (reading)'
|
|---|
| 854 |
|
|---|
| 855 | evtCtrl.evtBuf[evtCtrl.lastPtr] = i;
|
|---|
| 856 | evtCtrl.evtStat[evtCtrl.lastPtr] = 0;
|
|---|
| 857 | evtCtrl.pcTime[evtCtrl.lastPtr] = g_actTime;
|
|---|
| 858 | evtIdx[i] = evtCtrl.lastPtr;
|
|---|
| 859 |
|
|---|
| 860 |
|
|---|
| 861 | #ifdef EVTDEBUG
|
|---|
| 862 | factPrintf(kDebug, -11, "%5d %8d start new evt %8d %8d sock %3d len %5d t %10d",
|
|---|
| 863 | evID, runID, i, evtCtrl.lastPtr, sk, fadlen, mBuffer[i].pcTime);
|
|---|
| 864 | #endif
|
|---|
| 865 | evtCtrl.lastPtr++;
|
|---|
| 866 | evtCtrl.lastPtr %= MAX_EVT * MAX_RUN;
|
|---|
| 867 |
|
|---|
| 868 | //gi.evtGet++;
|
|---|
| 869 |
|
|---|
| 870 | return i;
|
|---|
| 871 |
|
|---|
| 872 | } /*-----------------------------------------------------------------*/
|
|---|
| 873 |
|
|---|
| 874 |
|
|---|
| 875 |
|
|---|
| 876 |
|
|---|
| 877 | int
|
|---|
| 878 | mBufFree (int i)
|
|---|
| 879 | {
|
|---|
| 880 | //delete entry [i] from mBuffer:
|
|---|
| 881 | //(and make sure multiple calls do no harm ....)
|
|---|
| 882 |
|
|---|
| 883 | // int headmem = 0;
|
|---|
| 884 | // size_t freemem = 0;
|
|---|
| 885 |
|
|---|
| 886 | #ifdef ETIENNE_MALLOC
|
|---|
| 887 | int evid;
|
|---|
| 888 | evid = mBuffer[i].evNum;
|
|---|
| 889 | ETI_Free(evid, i);
|
|---|
| 890 | #endif
|
|---|
| 891 |
|
|---|
| 892 | // freemem = mBuffer[i].evtLen;
|
|---|
| 893 | //ETIENNE
|
|---|
| 894 | #ifdef THOMAS_MALLOC
|
|---|
| 895 | TGB_free(mBuffer[i].FADhead);
|
|---|
| 896 | #endif
|
|---|
| 897 |
|
|---|
| 898 | #ifdef STANDARD_MALLOC
|
|---|
| 899 | free (mBuffer[i].FADhead);
|
|---|
| 900 | #endif
|
|---|
| 901 |
|
|---|
| 902 | // free (mBuffer[i].fEvent);
|
|---|
| 903 | mBuffer[i].fEvent = NULL;
|
|---|
| 904 |
|
|---|
| 905 | // free (mBuffer[i].FADhead);
|
|---|
| 906 | mBuffer[i].FADhead = NULL;
|
|---|
| 907 |
|
|---|
| 908 | // free (mBuffer[i].buffer);
|
|---|
| 909 | mBuffer[i].buffer = NULL;
|
|---|
| 910 | //END ETIENNE
|
|---|
| 911 | // headmem = NBOARDS * sizeof (PEVNT_HEADER);
|
|---|
| 912 | mBuffer[i].evNum = mBuffer[i].nRoi = -1;
|
|---|
| 913 | mBuffer[i].runNum = 0;
|
|---|
| 914 |
|
|---|
| 915 | #ifdef ETIENNE_MALLOC
|
|---|
| 916 | //ETIENNE
|
|---|
| 917 | // gj.usdMem = gj.usdMem - freemem - headmem - gi_maxSize;
|
|---|
| 918 | gj.usdMem = 0;
|
|---|
| 919 | for (int k=0;k<numAllocatedChunks;k++)
|
|---|
| 920 | {
|
|---|
| 921 | gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE;
|
|---|
| 922 | }
|
|---|
| 923 | //END ETIENNE
|
|---|
| 924 | #endif
|
|---|
| 925 |
|
|---|
| 926 | #ifdef THOMAS_MALLOC
|
|---|
| 927 | gj.usdMem = tgb_inuse;
|
|---|
| 928 | #endif
|
|---|
| 929 |
|
|---|
| 930 | gj.bufTot--;
|
|---|
| 931 |
|
|---|
| 932 | /*if (gi_memStat < 0) {
|
|---|
| 933 | if (gj.usdMem <= 0.75 * gj.maxMem)
|
|---|
| 934 | gi_memStat = +1;
|
|---|
| 935 | }*/
|
|---|
| 936 |
|
|---|
| 937 | return 0;
|
|---|
| 938 |
|
|---|
| 939 | } /*-----------------------------------------------------------------*/
|
|---|
| 940 |
|
|---|
| 941 | /*
|
|---|
| 942 | void
|
|---|
| 943 | resetEvtStat ()
|
|---|
| 944 | {
|
|---|
| 945 | for (int i = 0; i < MAX_SOCK; i++)
|
|---|
| 946 | gi.numRead[i] = 0;
|
|---|
| 947 |
|
|---|
| 948 | for (int i = 0; i < NBOARDS; i++) {
|
|---|
| 949 | gi.gotByte[i] = 0;
|
|---|
| 950 | gi.gotErr[i] = 0;
|
|---|
| 951 |
|
|---|
| 952 | }
|
|---|
| 953 |
|
|---|
| 954 | gi.evtGet = 0; //#new Start of Events read
|
|---|
| 955 | gi.evtTot = 0; //#complete Events read
|
|---|
| 956 | gi.evtErr = 0; //#Events with Errors
|
|---|
| 957 | gi.evtSkp = 0; //#Events incomplete (timeout)
|
|---|
| 958 |
|
|---|
| 959 | gi.procTot = 0; //#Events processed
|
|---|
| 960 | gi.procErr = 0; //#Events showed problem in processing
|
|---|
| 961 | gi.procTrg = 0; //#Events accepted by SW trigger
|
|---|
| 962 | gi.procSkp = 0; //#Events rejected by SW trigger
|
|---|
| 963 |
|
|---|
| 964 | gi.feedTot = 0; //#Events used for feedBack system
|
|---|
| 965 | gi.feedErr = 0; //#Events rejected by feedBack
|
|---|
| 966 |
|
|---|
| 967 | gi.wrtTot = 0; //#Events written to disk
|
|---|
| 968 | gi.wrtErr = 0; //#Events with write-error
|
|---|
| 969 |
|
|---|
| 970 | gi.runOpen = 0; //#Runs opened
|
|---|
| 971 | gi.runClose = 0; //#Runs closed
|
|---|
| 972 | gi.runErr = 0; //#Runs with open/close errors
|
|---|
| 973 |
|
|---|
| 974 | return;
|
|---|
| 975 | }*/ /*-----------------------------------------------------------------*/
|
|---|
| 976 |
|
|---|
| 977 | void reportIncomplete(int k0)
|
|---|
| 978 | {
|
|---|
| 979 | const int id = evtCtrl.evtBuf[k0];
|
|---|
| 980 |
|
|---|
| 981 | factPrintf(kWarn, 601, "%5d skip incomplete evt %8d %8d %2d",
|
|---|
| 982 | mBuffer[id].evNum, evtCtrl.evtBuf[k0], k0, evtCtrl.evtStat[k0]);
|
|---|
| 983 |
|
|---|
| 984 | uint64_t report = 0;
|
|---|
| 985 |
|
|---|
| 986 | char str[1000];
|
|---|
| 987 |
|
|---|
| 988 | int ik=0;
|
|---|
| 989 | for (int ib=0; ib<NBOARDS; ib++)
|
|---|
| 990 | {
|
|---|
| 991 | if (ib%10==0)
|
|---|
| 992 | str[ik++] = '|';
|
|---|
| 993 |
|
|---|
| 994 | const int jb = mBuffer[id].board[ib];
|
|---|
| 995 | if (jb>=0) // data received from that board
|
|---|
| 996 | {
|
|---|
| 997 | str[ik++] = '0'+(jb%10);
|
|---|
| 998 | continue;
|
|---|
| 999 | }
|
|---|
| 1000 |
|
|---|
| 1001 | // FIXME: Is that really 'b' or should that be 'ib' ?
|
|---|
| 1002 | if (gi_NumConnect[ib]<=0) // board not connected
|
|---|
| 1003 | {
|
|---|
| 1004 | str[ik++] = 'x';
|
|---|
| 1005 | continue;
|
|---|
| 1006 | }
|
|---|
| 1007 |
|
|---|
| 1008 | // data from this board lost
|
|---|
| 1009 | str[ik++] = '.';
|
|---|
| 1010 | report |= ((uint64_t)1)<<ib;
|
|---|
| 1011 | }
|
|---|
| 1012 |
|
|---|
| 1013 | str[ik++] = '|';
|
|---|
| 1014 | str[ik] = 0;
|
|---|
| 1015 |
|
|---|
| 1016 | factOut(kWarn, 601, str);
|
|---|
| 1017 |
|
|---|
| 1018 | factReportIncomplete(report);
|
|---|
| 1019 | }
|
|---|
| 1020 |
|
|---|
| 1021 | int checkRoiConsistency(int i, int roi[])
|
|---|
| 1022 | {
|
|---|
| 1023 | int xjr = -1;
|
|---|
| 1024 | int xkr = -1;
|
|---|
| 1025 |
|
|---|
| 1026 | //points to the very first roi
|
|---|
| 1027 | int roiPtr = sizeof(PEVNT_HEADER)/2 + 2;
|
|---|
| 1028 |
|
|---|
| 1029 | roi[0] = rd[i].rBuf->S[roiPtr];
|
|---|
| 1030 |
|
|---|
| 1031 | for (int jr = 0; jr < 9; jr++)
|
|---|
| 1032 | {
|
|---|
| 1033 | roi[jr] = rd[i].rBuf->S[roiPtr];
|
|---|
| 1034 |
|
|---|
| 1035 | if (roi[jr]!=roi[0])
|
|---|
| 1036 | {
|
|---|
| 1037 | xjr = jr;
|
|---|
| 1038 | break;
|
|---|
| 1039 | }
|
|---|
| 1040 |
|
|---|
| 1041 | for (int kr = 1; kr < 4; kr++)
|
|---|
| 1042 | {
|
|---|
| 1043 | const int kroi = rd[i].rBuf->S[roiPtr];
|
|---|
| 1044 | if (kroi != roi[jr])
|
|---|
| 1045 | {
|
|---|
| 1046 | xjr = jr;
|
|---|
| 1047 | xkr = kr;
|
|---|
| 1048 | break;
|
|---|
| 1049 | }
|
|---|
| 1050 | roiPtr += kroi+4;
|
|---|
| 1051 | }
|
|---|
| 1052 | }
|
|---|
| 1053 |
|
|---|
| 1054 | if (xjr<0)
|
|---|
| 1055 | return 1;
|
|---|
| 1056 |
|
|---|
| 1057 | if (xkr<0)
|
|---|
| 1058 | factPrintf(kFatal, 1, "Inconsistent Roi accross boards B=%d, expected %d, got %d", xjr, roi[xjr], roi[0]);
|
|---|
| 1059 | else
|
|---|
| 1060 | factPrintf(kFatal, 1, "Inconsistent Roi accross patches B=%d P=%d, expected %d, got %d", xjr, xkr, roi[xjr], rd[i].rBuf->S[roiPtr]);
|
|---|
| 1061 |
|
|---|
| 1062 | return 0;
|
|---|
| 1063 | }
|
|---|
| 1064 |
|
|---|
| 1065 | void swapEventHeaderBytes(int i)
|
|---|
| 1066 | {
|
|---|
| 1067 | //End of the header. to channels now
|
|---|
| 1068 | int eStart = 36;
|
|---|
| 1069 | for (int ePatchesCount = 0; ePatchesCount<4*9;ePatchesCount++)
|
|---|
| 1070 | {
|
|---|
| 1071 | rd[i].rBuf->S[eStart+0] = ntohs(rd[i].rBuf->S[eStart+0]);//id
|
|---|
| 1072 | rd[i].rBuf->S[eStart+1] = ntohs(rd[i].rBuf->S[eStart+1]);//start_cell
|
|---|
| 1073 | rd[i].rBuf->S[eStart+2] = ntohs(rd[i].rBuf->S[eStart+2]);//roi
|
|---|
| 1074 | rd[i].rBuf->S[eStart+3] = ntohs(rd[i].rBuf->S[eStart+3]);//filling
|
|---|
| 1075 |
|
|---|
| 1076 | eStart += 4+rd[i].rBuf->S[eStart+2];//skip the pixel data
|
|---|
| 1077 | }
|
|---|
| 1078 | }
|
|---|
| 1079 |
|
|---|
| 1080 | void copyData(int i, int evID, /*int roi,*/ int boardId)
|
|---|
| 1081 | {
|
|---|
| 1082 | memcpy(&mBuffer[evID].FADhead[boardId].start_package_flag,
|
|---|
| 1083 | &rd[i].rBuf->S[0], sizeof(PEVNT_HEADER));
|
|---|
| 1084 |
|
|---|
| 1085 | int src = sizeof(PEVNT_HEADER) / 2;
|
|---|
| 1086 |
|
|---|
| 1087 | // consistency of ROIs have been checked already (is it all correct?)
|
|---|
| 1088 | const int roi = rd[i].rBuf->S[src+2];
|
|---|
| 1089 |
|
|---|
| 1090 | // different sort in FAD board.....
|
|---|
| 1091 | for (int px = 0; px < 9; px++)
|
|---|
| 1092 | {
|
|---|
| 1093 | for (int drs = 0; drs < 4; drs++)
|
|---|
| 1094 | {
|
|---|
| 1095 | // pixH = rd[i].rBuf->S[src++]; // ID
|
|---|
| 1096 | src++;
|
|---|
| 1097 |
|
|---|
| 1098 | const int pixC = rd[i].rBuf->S[src++]; // start-cell
|
|---|
| 1099 | const int pixR = rd[i].rBuf->S[src++]; // roi
|
|---|
| 1100 | //here we should check if pixH is correct ....
|
|---|
| 1101 |
|
|---|
| 1102 | const int pixS = boardId * 36 + drs * 9 + px;
|
|---|
| 1103 | src++;
|
|---|
| 1104 |
|
|---|
| 1105 | mBuffer[evID].fEvent->StartPix[pixS] = pixC;
|
|---|
| 1106 |
|
|---|
| 1107 | const int dest1 = pixS * roi;
|
|---|
| 1108 | memcpy(&mBuffer[evID].fEvent->Adc_Data[dest1],
|
|---|
| 1109 | &rd[i].rBuf->S[src], roi * 2);
|
|---|
| 1110 |
|
|---|
| 1111 | src += pixR;
|
|---|
| 1112 |
|
|---|
| 1113 | if (px == 8)
|
|---|
| 1114 | {
|
|---|
| 1115 | const int tmS = boardId * 4 + drs;
|
|---|
| 1116 |
|
|---|
| 1117 | //and we have additional TM info
|
|---|
| 1118 | if (pixR > roi)
|
|---|
| 1119 | {
|
|---|
| 1120 | const int dest2 = tmS * roi + NPIX * roi;
|
|---|
| 1121 |
|
|---|
| 1122 | const int srcT = src - roi;
|
|---|
| 1123 | mBuffer[evID].fEvent->StartTM[tmS] = (pixC + pixR - roi) % 1024;
|
|---|
| 1124 |
|
|---|
| 1125 | memcpy(&mBuffer[evID].fEvent->Adc_Data[dest2],
|
|---|
| 1126 | &rd[i].rBuf->S[srcT], roi * 2);
|
|---|
| 1127 | }
|
|---|
| 1128 | else
|
|---|
| 1129 | {
|
|---|
| 1130 | mBuffer[evID].fEvent->StartTM[tmS] = -1;
|
|---|
| 1131 |
|
|---|
| 1132 | //ETIENNE because the TM channels are always processed during drs calib,
|
|---|
| 1133 | //set them to zero if they are not present
|
|---|
| 1134 | //I suspect that it may be more efficient to set all the allocated mem to
|
|---|
| 1135 | //zero when allocating it
|
|---|
| 1136 | // dest = tmS*roi[0] + NPIX*roi[0];
|
|---|
| 1137 | // bzero(&mBuffer[evID].fEvent->Adc_Data[dest],roi[0]*2);
|
|---|
| 1138 | }
|
|---|
| 1139 | }
|
|---|
| 1140 | }
|
|---|
| 1141 | }
|
|---|
| 1142 | }
|
|---|
| 1143 |
|
|---|
| 1144 |
|
|---|
| 1145 | void
|
|---|
| 1146 | initReadFAD ()
|
|---|
| 1147 | {
|
|---|
| 1148 | return;
|
|---|
| 1149 | } /*-----------------------------------------------------------------*/
|
|---|
| 1150 |
|
|---|
| 1151 | //struct rnd
|
|---|
| 1152 | //{
|
|---|
| 1153 | // int val;
|
|---|
| 1154 | // int idx;
|
|---|
| 1155 | //};
|
|---|
| 1156 | //
|
|---|
| 1157 | //struct rnd random_arr[MAX_SOCK];
|
|---|
| 1158 | //
|
|---|
| 1159 | //int compare(const void *f1, const void *f2)
|
|---|
| 1160 | //{
|
|---|
| 1161 | // struct rnd *r1 = (struct rnd*)f1;
|
|---|
| 1162 | // struct rnd *r2 = (struct rnd*)f2;
|
|---|
| 1163 | // return r1->val - r2->val;
|
|---|
| 1164 | //}
|
|---|
| 1165 |
|
|---|
| 1166 |
|
|---|
| 1167 | void *readFAD (void *ptr)
|
|---|
| 1168 | {
|
|---|
| 1169 | /* *** main loop reading FAD data and sorting them to complete events */
|
|---|
| 1170 |
|
|---|
| 1171 | factPrintf(kInfo, -1, "Start initializing (readFAD)");
|
|---|
| 1172 |
|
|---|
| 1173 | // int cpu = 7; //read thread
|
|---|
| 1174 | // cpu_set_t mask;
|
|---|
| 1175 |
|
|---|
| 1176 | /* CPU_ZERO initializes all the bits in the mask to zero. */
|
|---|
| 1177 | // CPU_ZERO (&mask);
|
|---|
| 1178 | /* CPU_SET sets only the bit corresponding to cpu. */
|
|---|
| 1179 | // cpu = 7;
|
|---|
| 1180 | // CPU_SET (cpu, &mask);
|
|---|
| 1181 |
|
|---|
| 1182 | /* sched_setaffinity returns 0 in success */
|
|---|
| 1183 | // if (sched_setaffinity (0, sizeof (mask), &mask) == -1) {
|
|---|
| 1184 | // snprintf (str, MXSTR, "W ---> can not create affinity to %d", cpu);
|
|---|
| 1185 | // factOut (kWarn, -1, str);
|
|---|
| 1186 | // }
|
|---|
| 1187 |
|
|---|
| 1188 |
|
|---|
| 1189 | const int minLen = sizeof(PEVNT_HEADER); //min #bytes needed to check header: full header for debug
|
|---|
| 1190 |
|
|---|
| 1191 | int frst_len = sizeof(PEVNT_HEADER); //max #bytes to read first: fad_header only, so each event must be longer, even for roi=0
|
|---|
| 1192 |
|
|---|
| 1193 | start.S = 0xFB01;
|
|---|
| 1194 | stop.S = 0x04FE;
|
|---|
| 1195 |
|
|---|
| 1196 | /* initialize run control logics */
|
|---|
| 1197 | for (int i = 0; i < MAX_RUN; i++) {
|
|---|
| 1198 | runCtrl[i].runId = 0;
|
|---|
| 1199 | runCtrl[i].fileId = -2;
|
|---|
| 1200 | runCtrl[i].procId = -2;
|
|---|
| 1201 | }
|
|---|
| 1202 | gi_resetS = gi_resetR = 9;
|
|---|
| 1203 |
|
|---|
| 1204 | int sockDef[NBOARDS]; //internal state of sockets
|
|---|
| 1205 | memset(sockDef, 0, NBOARDS*sizeof(int));
|
|---|
| 1206 |
|
|---|
| 1207 | START:
|
|---|
| 1208 | evtCtrl.frstPtr = 0;
|
|---|
| 1209 | evtCtrl.lastPtr = 0;
|
|---|
| 1210 |
|
|---|
| 1211 | //time in seconds
|
|---|
| 1212 | uint gi_SecTime = time(NULL);;
|
|---|
| 1213 |
|
|---|
| 1214 | const int cntsock = 8 - NUMSOCK ;
|
|---|
| 1215 |
|
|---|
| 1216 | if (gi_resetS > 0) {
|
|---|
| 1217 | //make sure all sockets are preallocated as 'not exist'
|
|---|
| 1218 | for (int i = 0; i < MAX_SOCK; i++) {
|
|---|
| 1219 | rd[i].socket = -1;
|
|---|
| 1220 | rd[i].sockStat = 99;
|
|---|
| 1221 | }
|
|---|
| 1222 |
|
|---|
| 1223 | for (int k = 0; k < NBOARDS; k++) {
|
|---|
| 1224 | gi_NumConnect[k] = 0;
|
|---|
| 1225 | //gi.numConn[k] = 0;
|
|---|
| 1226 | gj.numConn[k] = 0;
|
|---|
| 1227 | //gj.errConn[k] = 0;
|
|---|
| 1228 | gj.rateBytes[k] = 0;
|
|---|
| 1229 | gj.totBytes[k] = 0;
|
|---|
| 1230 | }
|
|---|
| 1231 |
|
|---|
| 1232 | }
|
|---|
| 1233 |
|
|---|
| 1234 |
|
|---|
| 1235 | if (gi_resetR > 0) {
|
|---|
| 1236 | //resetEvtStat ();
|
|---|
| 1237 | gj.bufTot = gj.maxEvt = gj.xxxEvt = 0;
|
|---|
| 1238 | gj.usdMem = gj.maxMem = gj.xxxMem = 0;
|
|---|
| 1239 | #ifdef THOMAS_MALLOC
|
|---|
| 1240 | gj.totMem = tgb_memory;
|
|---|
| 1241 | #else
|
|---|
| 1242 | gj.totMem = g_maxMem;
|
|---|
| 1243 | #endif
|
|---|
| 1244 | gj.bufNew = gj.bufEvt = 0;
|
|---|
| 1245 | gj.badRoiE = gj.badRoiR = gj.badRoiB =
|
|---|
| 1246 | gj.evtSkip = gj.evtWrite = gj.evtErr = 0;
|
|---|
| 1247 |
|
|---|
| 1248 | for (int b = 0; b < NBOARDS; b++)
|
|---|
| 1249 | gj.badRoi[b] = 0;
|
|---|
| 1250 |
|
|---|
| 1251 | mBufInit (); //initialize buffers
|
|---|
| 1252 |
|
|---|
| 1253 | factPrintf(kInfo, -1, "End initializing (readFAD)");
|
|---|
| 1254 | }
|
|---|
| 1255 |
|
|---|
| 1256 |
|
|---|
| 1257 | gi_reset = gi_resetR = gi_resetS = gi_resetW = 0;
|
|---|
| 1258 |
|
|---|
| 1259 | //loop until global variable g_runStat claims stop
|
|---|
| 1260 | while (g_runStat >= 0 && g_reset == 0)
|
|---|
| 1261 | {
|
|---|
| 1262 | gi_runStat = g_runStat;
|
|---|
| 1263 | gj.readStat = g_runStat;
|
|---|
| 1264 |
|
|---|
| 1265 | struct timeval tv;
|
|---|
| 1266 | gettimeofday (&tv, NULL);
|
|---|
| 1267 | g_actTime = tv.tv_sec;
|
|---|
| 1268 | g_actUsec = tv.tv_usec;
|
|---|
| 1269 |
|
|---|
| 1270 |
|
|---|
| 1271 | for (int b = 0; b < NBOARDS; b++)
|
|---|
| 1272 | {
|
|---|
| 1273 | // Nothing has changed
|
|---|
| 1274 | if (g_port[b].sockDef == sockDef[b])
|
|---|
| 1275 | continue;
|
|---|
| 1276 |
|
|---|
| 1277 | gi_NumConnect[b] = 0; //must close all connections
|
|---|
| 1278 | //gi.numConn[b] = 0;
|
|---|
| 1279 | gj.numConn[b] = 0;
|
|---|
| 1280 |
|
|---|
| 1281 | // s0 = 0: sockets to be defined and opened
|
|---|
| 1282 | // s0 = -1: sockets to be destroyed
|
|---|
| 1283 | // s0 = +1: sockets to be closed and reopened
|
|---|
| 1284 |
|
|---|
| 1285 | int s0 = 0;
|
|---|
| 1286 | if (sockDef[b] != 0)
|
|---|
| 1287 | s0 = g_port[b].sockDef==0 ? -1 : +1;
|
|---|
| 1288 |
|
|---|
| 1289 | const int p0 = s0==0 ? ntohs (g_port[b].sockAddr.sin_port) : 0;
|
|---|
| 1290 |
|
|---|
| 1291 | int k = b * 7;
|
|---|
| 1292 | for (int p = p0 + 1; p < p0 + 8; p++, k++)
|
|---|
| 1293 | GenSock (s0, k, p, &g_port[b].sockAddr, &rd[k]); //generate address and socket
|
|---|
| 1294 |
|
|---|
| 1295 | sockDef[b] = g_port[b].sockDef;
|
|---|
| 1296 | }
|
|---|
| 1297 |
|
|---|
| 1298 | // count the number of active boards
|
|---|
| 1299 | int actBoards = 0;
|
|---|
| 1300 | for (int b = 0; b < NBOARDS; b++)
|
|---|
| 1301 | if (sockDef[b] > 0)
|
|---|
| 1302 | actBoards++;
|
|---|
| 1303 |
|
|---|
| 1304 | //count number of succesfull actions
|
|---|
| 1305 | // int numok = 0;
|
|---|
| 1306 |
|
|---|
| 1307 | /*
|
|---|
| 1308 | for (i=0; i<MAX_SOCK/7; i++)
|
|---|
| 1309 | {
|
|---|
| 1310 | random_arr[i].val = rand();
|
|---|
| 1311 | random_arr[i].idx = i;
|
|---|
| 1312 | }
|
|---|
| 1313 |
|
|---|
| 1314 | qsort(random_arr, MAX_SOCK/7, sizeof(struct rnd), compare);
|
|---|
| 1315 |
|
|---|
| 1316 | for (int iii = 0; iii < MAX_SOCK/7; iii++) { //check all sockets if something to read
|
|---|
| 1317 |
|
|---|
| 1318 | b = random_arr[iii].idx;
|
|---|
| 1319 | i = b*7;
|
|---|
| 1320 | p = 0;
|
|---|
| 1321 | */
|
|---|
| 1322 |
|
|---|
| 1323 | //check all sockets if something to read
|
|---|
| 1324 | for (int i = 0; i < MAX_SOCK; i+=7)
|
|---|
| 1325 | {
|
|---|
| 1326 | // Do not try to connect this socket
|
|---|
| 1327 | if (rd[i].sockStat > 0)
|
|---|
| 1328 | continue;
|
|---|
| 1329 |
|
|---|
| 1330 | const int b = i / 7 ;
|
|---|
| 1331 | //const int p = i % 7 ;
|
|---|
| 1332 |
|
|---|
| 1333 | if (rd[i].sockStat == -1)
|
|---|
| 1334 | {
|
|---|
| 1335 | //try to connect if not yet done
|
|---|
| 1336 | rd[i].sockStat = connect (rd[i].socket,
|
|---|
| 1337 | (struct sockaddr *) &rd[i].SockAddr,
|
|---|
| 1338 | sizeof (rd[i].SockAddr));
|
|---|
| 1339 | // Failed
|
|---|
| 1340 | if (rd[i].sockStat == -1)
|
|---|
| 1341 | {
|
|---|
| 1342 | rd[i].errCnt++;
|
|---|
| 1343 | usleep(25000);
|
|---|
| 1344 | continue;
|
|---|
| 1345 | }
|
|---|
| 1346 |
|
|---|
| 1347 | // Success (rd[i].sockStat == 0)
|
|---|
| 1348 |
|
|---|
| 1349 | if (sockDef[b] > 0)
|
|---|
| 1350 | {
|
|---|
| 1351 | rd[i].bufTyp = 0; // expect a header
|
|---|
| 1352 | rd[i].bufLen = frst_len; // max size to read at begining
|
|---|
| 1353 | }
|
|---|
| 1354 | else
|
|---|
| 1355 | {
|
|---|
| 1356 | rd[i].bufTyp = -1; // full data to be skipped
|
|---|
| 1357 | rd[i].bufLen = MAX_LEN; // huge for skipping
|
|---|
| 1358 | }
|
|---|
| 1359 |
|
|---|
| 1360 | rd[i].bufPos = 0; // no byte read so far
|
|---|
| 1361 | rd[i].skip = 0; // start empty
|
|---|
| 1362 |
|
|---|
| 1363 | gi_NumConnect[b] += cntsock;
|
|---|
| 1364 |
|
|---|
| 1365 | //gi.numConn[b]++;
|
|---|
| 1366 | gj.numConn[b]++;
|
|---|
| 1367 |
|
|---|
| 1368 | factPrintf(kInfo, -1, "New connection %d (number of connections: %d)", b, gj.numConn[b]);
|
|---|
| 1369 | }
|
|---|
| 1370 |
|
|---|
| 1371 | // Do not read from this socket
|
|---|
| 1372 | if (rd[i].bufLen<=0)
|
|---|
| 1373 | continue;
|
|---|
| 1374 |
|
|---|
| 1375 | //numok++;
|
|---|
| 1376 |
|
|---|
| 1377 | const int32_t jrd =
|
|---|
| 1378 | recv(rd[i].socket, &rd[i].rBuf->B[rd[i].bufPos],
|
|---|
| 1379 | rd[i].bufLen, MSG_DONTWAIT);
|
|---|
| 1380 |
|
|---|
| 1381 | // recv failed
|
|---|
| 1382 | if (jrd<0)
|
|---|
| 1383 | {
|
|---|
| 1384 | // There was just nothing waiting
|
|---|
| 1385 | if (errno==EWOULDBLOCK || errno==EAGAIN)
|
|---|
| 1386 | {
|
|---|
| 1387 | //numok--;
|
|---|
| 1388 | continue;
|
|---|
| 1389 | }
|
|---|
| 1390 |
|
|---|
| 1391 | factPrintf(kError, 442, "Reading from socket %d failed: %m (recv,rc=%d)", i, errno);
|
|---|
| 1392 | //gi.gotErr[b]++;
|
|---|
| 1393 | continue;
|
|---|
| 1394 | }
|
|---|
| 1395 |
|
|---|
| 1396 | // connection was closed ...
|
|---|
| 1397 | if (jrd==0)
|
|---|
| 1398 | {
|
|---|
| 1399 | factPrintf(kInfo, 441, "Socket %d closed by FAD", i);
|
|---|
| 1400 |
|
|---|
| 1401 | const int s0 = sockDef[b] > 0 ? +1 : -1;
|
|---|
| 1402 | GenSock(s0, i, 0, NULL, &rd[i]);
|
|---|
| 1403 |
|
|---|
| 1404 | //gi.gotErr[b]++;
|
|---|
| 1405 |
|
|---|
| 1406 | gi_NumConnect[b]-= cntsock ;
|
|---|
| 1407 | //gi.numConn[b]--;
|
|---|
| 1408 | gj.numConn[b]--;
|
|---|
| 1409 |
|
|---|
| 1410 | continue;
|
|---|
| 1411 | }
|
|---|
| 1412 |
|
|---|
| 1413 | // Success (jrd > 0)
|
|---|
| 1414 |
|
|---|
| 1415 | //gi.gotByte[b] += jrd;
|
|---|
| 1416 | gj.rateBytes[b] += jrd;
|
|---|
| 1417 |
|
|---|
| 1418 | // are we skipping this board ...
|
|---|
| 1419 | if (rd[i].bufTyp < 0)
|
|---|
| 1420 | continue;
|
|---|
| 1421 |
|
|---|
| 1422 | rd[i].bufPos += jrd; //==> prepare for continuation
|
|---|
| 1423 | rd[i].bufLen -= jrd;
|
|---|
| 1424 |
|
|---|
| 1425 | // are we reading data?
|
|---|
| 1426 | if (rd[i].bufTyp > 0)
|
|---|
| 1427 | {
|
|---|
| 1428 | // not yet all read
|
|---|
| 1429 | if (rd[i].bufLen > 0)
|
|---|
| 1430 | continue;
|
|---|
| 1431 |
|
|---|
| 1432 | // ready to read next header
|
|---|
| 1433 | rd[i].bufTyp = 0;
|
|---|
| 1434 | rd[i].bufLen = frst_len;
|
|---|
| 1435 | rd[i].bufPos = 0;
|
|---|
| 1436 |
|
|---|
| 1437 | if (rd[i].rBuf->B[rd[i].fadLen - 1] != stop.B[0] ||
|
|---|
| 1438 | rd[i].rBuf->B[rd[i].fadLen - 2] != stop.B[1])
|
|---|
| 1439 | {
|
|---|
| 1440 | //gi.evtErr++;
|
|---|
| 1441 | factPrintf(kError, 301, "End-of-event flag wrong on socket %3d for event %4d (len=%5d), expected %3d %3d, got %3d %3d",
|
|---|
| 1442 | i, rd[i].evtID, rd[i].fadLen, stop.B[0], stop.B[1],
|
|---|
| 1443 | rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]);
|
|---|
| 1444 | continue;
|
|---|
| 1445 | }
|
|---|
| 1446 |
|
|---|
| 1447 | #ifdef EVTDEBUG
|
|---|
| 1448 | debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, 1, tv.tv_sec, tv.tv_usec); // i=socket; jrd=#bytes; ievt=eventid; 1=finished event
|
|---|
| 1449 | #endif
|
|---|
| 1450 |
|
|---|
| 1451 | // int actid;
|
|---|
| 1452 | // if (g_useFTM > 0)
|
|---|
| 1453 | // actid = rd[i].evtID;
|
|---|
| 1454 | // else
|
|---|
| 1455 | // actid = rd[i].ftmID;
|
|---|
| 1456 |
|
|---|
| 1457 | //get index into mBuffer for this event (create if needed)
|
|---|
| 1458 | const int evID = mBufEvt(i);
|
|---|
| 1459 |
|
|---|
| 1460 | if (evID < -1000)
|
|---|
| 1461 | continue;
|
|---|
| 1462 |
|
|---|
| 1463 | // no space left, retry later
|
|---|
| 1464 | if (evID < 0)
|
|---|
| 1465 | {
|
|---|
| 1466 | rd[i].bufTyp = -1;
|
|---|
| 1467 | rd[i].bufLen = 0;
|
|---|
| 1468 | rd[i].bufPos = rd[i].fadLen;
|
|---|
| 1469 | continue;
|
|---|
| 1470 | }
|
|---|
| 1471 |
|
|---|
| 1472 | //we have a valid entry in mBuffer[]; fill it
|
|---|
| 1473 |
|
|---|
| 1474 | const int boardId = b;
|
|---|
| 1475 | const int fadBoard = rd[i].rBuf->S[12];
|
|---|
| 1476 | const int fadCrate = fadBoard / 256;
|
|---|
| 1477 |
|
|---|
| 1478 | if (boardId != (fadCrate * 10 + fadBoard % 256))
|
|---|
| 1479 | {
|
|---|
| 1480 | factPrintf(kWarn, 301, "Board ID mismatch. Expected %d, got %d (C=%d, B=%d)",
|
|---|
| 1481 | boardId, fadBoard, fadCrate, fadBoard % 256);
|
|---|
| 1482 | }
|
|---|
| 1483 |
|
|---|
| 1484 | if (mBuffer[evID].board[boardId] != -1)
|
|---|
| 1485 | {
|
|---|
| 1486 | factPrintf(kWarn, 501, "Got event %5d from board %3d (i=%3d, len=%5d) twice: Starts with %3d %3d - ends with %3d %3d",
|
|---|
| 1487 | evID, boardId, i, rd[i].fadLen,
|
|---|
| 1488 | rd[i].rBuf->B[0], rd[i].rBuf->B[1],
|
|---|
| 1489 | rd[i].rBuf->B[rd[i].fadLen - 2],
|
|---|
| 1490 | rd[i].rBuf->B[rd[i].fadLen - 1]);
|
|---|
| 1491 | // FIXME: Is that action the right one?
|
|---|
| 1492 | continue;
|
|---|
| 1493 | }
|
|---|
| 1494 |
|
|---|
| 1495 | // Copy data from rd[i] to mBuffer[evID]
|
|---|
| 1496 | copyData(i, evID, boardId);
|
|---|
| 1497 |
|
|---|
| 1498 | // now we have stored a new board contents into Event structure
|
|---|
| 1499 |
|
|---|
| 1500 | const int iDx = evtIdx[evID]; //index into evtCtrl
|
|---|
| 1501 |
|
|---|
| 1502 | evtCtrl.evtStat[iDx]++;
|
|---|
| 1503 | evtCtrl.pcTime[iDx] = g_actTime;
|
|---|
| 1504 |
|
|---|
| 1505 | mBuffer[evID].fEvent->NumBoards++;
|
|---|
| 1506 | mBuffer[evID].board[boardId] = boardId;
|
|---|
| 1507 | mBuffer[evID].nBoard++;
|
|---|
| 1508 |
|
|---|
| 1509 | // have we already reported first (partial) event of this run ???
|
|---|
| 1510 | if (mBuffer[evID].nBoard>0 && mBuffer[evID].runNum != actrun)
|
|---|
| 1511 | {
|
|---|
| 1512 | actrun = mBuffer[evID].runNum;
|
|---|
| 1513 |
|
|---|
| 1514 | for (int ir = 0; ir < MAX_RUN; ir++)
|
|---|
| 1515 | {
|
|---|
| 1516 | if (runCtrl[ir].runId == actrun)
|
|---|
| 1517 | {
|
|---|
| 1518 | if (++runCtrl[ir].lastEvt == 0)
|
|---|
| 1519 | {
|
|---|
| 1520 | gotNewRun (actrun, NULL);
|
|---|
| 1521 | factPrintf(kInfo, 1, "gotNewRun called for run %d, event %d",
|
|---|
| 1522 | mBuffer[evID].runNum, mBuffer[evID].evNum);
|
|---|
| 1523 | break;
|
|---|
| 1524 | }
|
|---|
| 1525 | }
|
|---|
| 1526 | }
|
|---|
| 1527 | }
|
|---|
| 1528 |
|
|---|
| 1529 | //complete event read ---> flag for next processing
|
|---|
| 1530 | if (mBuffer[evID].nBoard >= actBoards)
|
|---|
| 1531 | {
|
|---|
| 1532 | evtCtrl.evtStat[iDx] = 99;
|
|---|
| 1533 | //gi.evtTot++;
|
|---|
| 1534 | }
|
|---|
| 1535 | }
|
|---|
| 1536 | else
|
|---|
| 1537 | {
|
|---|
| 1538 | //we are reading event header
|
|---|
| 1539 |
|
|---|
| 1540 | //not yet sufficient data to take action
|
|---|
| 1541 | if (rd[i].bufPos < minLen)
|
|---|
| 1542 | continue;
|
|---|
| 1543 |
|
|---|
| 1544 | //check if startflag correct; else shift block ....
|
|---|
| 1545 | // FIXME: This is not enough... this combination of
|
|---|
| 1546 | // bytes can be anywhere... at least the end bytes
|
|---|
| 1547 | // must be checked somewhere, too.
|
|---|
| 1548 | int k;
|
|---|
| 1549 | for (k = 0; k < rd[i].bufPos - 1; k++)
|
|---|
| 1550 | {
|
|---|
| 1551 | if (rd[i].rBuf->B[k] == start.B[1] && rd[i].rBuf->B[k+1] == start.B[0])
|
|---|
| 1552 | break;
|
|---|
| 1553 | }
|
|---|
| 1554 | rd[i].skip += k;
|
|---|
| 1555 |
|
|---|
| 1556 | //no start of header found
|
|---|
| 1557 | if (k >= rd[i].bufPos - 1)
|
|---|
| 1558 | {
|
|---|
| 1559 | rd[i].bufPos = 0;
|
|---|
| 1560 | rd[i].bufLen = sizeof(PEVNT_HEADER);
|
|---|
| 1561 | continue;
|
|---|
| 1562 | }
|
|---|
| 1563 |
|
|---|
| 1564 | if (k > 0)
|
|---|
| 1565 | {
|
|---|
| 1566 | rd[i].bufPos -= k;
|
|---|
| 1567 | rd[i].bufLen += k;
|
|---|
| 1568 | memmove (&rd[i].rBuf->B[0], &rd[i].rBuf->B[k],
|
|---|
| 1569 | rd[i].bufPos);
|
|---|
| 1570 | }
|
|---|
| 1571 |
|
|---|
| 1572 | if (rd[i].bufPos < minLen)
|
|---|
| 1573 | continue;
|
|---|
| 1574 |
|
|---|
| 1575 | if (rd[i].skip > 0)
|
|---|
| 1576 | {
|
|---|
| 1577 | factPrintf(kInfo, 666, "Skipped %d bytes on port %d", rd[i].skip, i);
|
|---|
| 1578 | rd[i].skip = 0;
|
|---|
| 1579 | }
|
|---|
| 1580 |
|
|---|
| 1581 | // TGB: This needs much more checks than just the first two bytes!
|
|---|
| 1582 |
|
|---|
| 1583 | // Swap everything except start_package_flag.
|
|---|
| 1584 | // It is to difficult to find out where it is used how,
|
|---|
| 1585 | // but it doesn't really matter because it is not really
|
|---|
| 1586 | // used anywehere else
|
|---|
| 1587 | // rd[i].rBuf->S[1] = ntohs(rd[i].rBuf->S[1]); // package_length
|
|---|
| 1588 | rd[i].rBuf->S[2] = ntohs(rd[i].rBuf->S[2]); // version_no
|
|---|
| 1589 | rd[i].rBuf->S[3] = ntohs(rd[i].rBuf->S[3]); // PLLLCK
|
|---|
| 1590 | rd[i].rBuf->S[4] = ntohs(rd[i].rBuf->S[4]); // trigger_crc
|
|---|
| 1591 | rd[i].rBuf->S[5] = ntohs(rd[i].rBuf->S[5]); // trigger_type
|
|---|
| 1592 |
|
|---|
| 1593 | rd[i].rBuf->S[12] = ntohs(rd[i].rBuf->S[12]); // board id
|
|---|
| 1594 | rd[i].rBuf->S[13] = ntohs(rd[i].rBuf->S[13]); // adc_clock_phase_shift
|
|---|
| 1595 | rd[i].rBuf->S[14] = ntohs(rd[i].rBuf->S[14]); // number_of_triggers_to_generate
|
|---|
| 1596 | rd[i].rBuf->S[15] = ntohs(rd[i].rBuf->S[15]); // trigger_generator_prescaler
|
|---|
| 1597 |
|
|---|
| 1598 | rd[i].rBuf->I[3] = ntohl(rd[i].rBuf->I[3]); // trigger_id
|
|---|
| 1599 | rd[i].rBuf->I[4] = ntohl(rd[i].rBuf->I[4]); // fad_evt_counter
|
|---|
| 1600 | rd[i].rBuf->I[5] = ntohl(rd[i].rBuf->I[5]); // REFCLK_frequency
|
|---|
| 1601 |
|
|---|
| 1602 | rd[i].rBuf->I[10] = ntohl(rd[i].rBuf->I[10]); // runnumber;
|
|---|
| 1603 | rd[i].rBuf->I[11] = ntohl(rd[i].rBuf->I[11]); // time;
|
|---|
| 1604 |
|
|---|
| 1605 | for (int s=24;s<24+NTemp+NDAC;s++)
|
|---|
| 1606 | rd[i].rBuf->S[s] = ntohs(rd[i].rBuf->S[s]); // drs_temperature / dac
|
|---|
| 1607 |
|
|---|
| 1608 | rd[i].fadLen = ntohs(rd[i].rBuf->S[1]) * 2;
|
|---|
| 1609 | rd[i].fadVers = rd[i].rBuf->S[2];
|
|---|
| 1610 | rd[i].ftmTyp = rd[i].rBuf->S[5];
|
|---|
| 1611 | rd[i].ftmID = rd[i].rBuf->I[3]; //(FTMevt)
|
|---|
| 1612 | rd[i].evtID = rd[i].rBuf->I[4]; //(FADevt)
|
|---|
| 1613 | rd[i].runID = rd[i].rBuf->I[11];
|
|---|
| 1614 | rd[i].bufTyp = 1; //ready to read full record
|
|---|
| 1615 | rd[i].bufLen = rd[i].fadLen - rd[i].bufPos;
|
|---|
| 1616 |
|
|---|
| 1617 | if (rd[i].runID == 0)
|
|---|
| 1618 | rd[i].runID = g_actTime;
|
|---|
| 1619 |
|
|---|
| 1620 | const int fadBoard = rd[i].rBuf->S[12];
|
|---|
| 1621 | debugHead (i, fadBoard, rd[i].rBuf);
|
|---|
| 1622 | debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, -1, tv.tv_sec, tv.tv_usec); // i=socket; jrd=#bytes; ievt=eventid;-1=start event
|
|---|
| 1623 | } // end if data or header
|
|---|
| 1624 | } // end for loop over all sockets
|
|---|
| 1625 |
|
|---|
| 1626 | //gi.numRead[numok]++;
|
|---|
| 1627 |
|
|---|
| 1628 | g_actTime = time (NULL);
|
|---|
| 1629 | if (g_actTime <= gi_SecTime) {
|
|---|
| 1630 | usleep(1);
|
|---|
| 1631 | continue;
|
|---|
| 1632 | }
|
|---|
| 1633 |
|
|---|
| 1634 | gi_SecTime = g_actTime;
|
|---|
| 1635 |
|
|---|
| 1636 | gj.bufNew = gj.bufEvt = 0;
|
|---|
| 1637 |
|
|---|
| 1638 | //loop over all active events and flag those older than read-timeout
|
|---|
| 1639 | //delete those that are written to disk ....
|
|---|
| 1640 | for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)
|
|---|
| 1641 | {
|
|---|
| 1642 | if (k0==evtCtrl.frstPtr && evtCtrl.evtStat[k0]<0)
|
|---|
| 1643 | {
|
|---|
| 1644 | evtCtrl.frstPtr++;
|
|---|
| 1645 | evtCtrl.frstPtr %= MAX_EVT * MAX_RUN;
|
|---|
| 1646 |
|
|---|
| 1647 | // Continue because evtCtrl.evtStat[k0] must be <0
|
|---|
| 1648 | continue;
|
|---|
| 1649 | }
|
|---|
| 1650 |
|
|---|
| 1651 | // Check the more likely case first: incomplete events
|
|---|
| 1652 | if (evtCtrl.evtStat[k0]>0 && evtCtrl.evtStat[k0]<92)
|
|---|
| 1653 | {
|
|---|
| 1654 | gj.bufNew++; //incomplete event in Buffer
|
|---|
| 1655 |
|
|---|
| 1656 | // Event has not yet timed out or was reported already
|
|---|
| 1657 | if (evtCtrl.evtStat[k0]>=90 || evtCtrl.pcTime[k0]>=g_actTime - 30)
|
|---|
| 1658 | continue;
|
|---|
| 1659 |
|
|---|
| 1660 | reportIncomplete(k0);
|
|---|
| 1661 |
|
|---|
| 1662 | evtCtrl.evtStat[k0] = 91; //timeout for incomplete events
|
|---|
| 1663 | //gi.evtSkp++;
|
|---|
| 1664 | //gi.evtTot++;
|
|---|
| 1665 | gj.evtSkip++;
|
|---|
| 1666 |
|
|---|
| 1667 | continue;
|
|---|
| 1668 | }
|
|---|
| 1669 |
|
|---|
| 1670 | // complete event in Buffer
|
|---|
| 1671 | if (evtCtrl.evtStat[k0] >= 95)
|
|---|
| 1672 | gj.bufEvt++;
|
|---|
| 1673 |
|
|---|
| 1674 | // Check the less likely case: 'useless' or 'delete'
|
|---|
| 1675 | if (evtCtrl.evtStat[k0]==0 || evtCtrl.evtStat[k0] >= 9000)
|
|---|
| 1676 | {
|
|---|
| 1677 | const int id = evtCtrl.evtBuf[k0];
|
|---|
| 1678 | #ifdef EVTDEBUG
|
|---|
| 1679 | factPrintf(kDebug, -1, "%5d free event buffer, nb=%3d", mBuffer[id].evNum, mBuffer[id].nBoard);
|
|---|
| 1680 | #endif
|
|---|
| 1681 | mBufFree (id); //event written--> free memory
|
|---|
| 1682 | evtCtrl.evtStat[k0] = -1;
|
|---|
| 1683 | gj.evtWrite++;
|
|---|
| 1684 | gj.rateWrite++;
|
|---|
| 1685 | }
|
|---|
| 1686 |
|
|---|
| 1687 | }
|
|---|
| 1688 |
|
|---|
| 1689 | gj.deltaT = 1000; //temporary, must be improved
|
|---|
| 1690 |
|
|---|
| 1691 | for (int ib = 0; ib < NBOARDS; ib++)
|
|---|
| 1692 | gj.totBytes[ib] += gj.rateBytes[ib];
|
|---|
| 1693 |
|
|---|
| 1694 | #ifdef THOMAS_MALLOC
|
|---|
| 1695 | gj.totMem = tgb_memory;
|
|---|
| 1696 | #else
|
|---|
| 1697 | gj.totMem = g_maxMem;
|
|---|
| 1698 | #endif
|
|---|
| 1699 |
|
|---|
| 1700 | if (gj.maxMem > gj.xxxMem)
|
|---|
| 1701 | gj.xxxMem = gj.maxMem;
|
|---|
| 1702 | if (gj.maxEvt > gj.xxxEvt)
|
|---|
| 1703 | gj.xxxEvt = gj.maxEvt;
|
|---|
| 1704 |
|
|---|
| 1705 | factStat (gj);
|
|---|
| 1706 | factStatNew (gi);
|
|---|
| 1707 | gj.rateNew = gj.rateWrite = 0;
|
|---|
| 1708 | gj.maxMem = gj.usdMem;
|
|---|
| 1709 | gj.maxEvt = gj.bufTot;
|
|---|
| 1710 | for (int b = 0; b < NBOARDS; b++)
|
|---|
| 1711 | gj.rateBytes[b] = 0;
|
|---|
| 1712 |
|
|---|
| 1713 | } // while (g_runStat >= 0 && g_reset == 0)
|
|---|
| 1714 |
|
|---|
| 1715 | factPrintf(kInfo, -1, "Stop reading ... RESET=%d", g_reset);
|
|---|
| 1716 |
|
|---|
| 1717 | if (g_reset > 0)
|
|---|
| 1718 | {
|
|---|
| 1719 | gi_reset = g_reset;
|
|---|
| 1720 | gi_resetR = gi_reset % 10; //shall we stop reading ?
|
|---|
| 1721 | gi_resetS = (gi_reset / 10) % 10; //shall we close sockets ?
|
|---|
| 1722 | gi_resetW = (gi_reset / 100) % 10; //shall we close files ?
|
|---|
| 1723 | gi_resetX = gi_reset / 1000; //shall we simply wait resetX seconds ?
|
|---|
| 1724 | g_reset = 0;
|
|---|
| 1725 | }
|
|---|
| 1726 | else
|
|---|
| 1727 | {
|
|---|
| 1728 | gi_reset = 0;
|
|---|
| 1729 | gi_resetR = g_runStat == -1 ? 1 : 7;
|
|---|
| 1730 |
|
|---|
| 1731 | gi_resetS = 7; //close all sockets
|
|---|
| 1732 | gi_resetW = 7; //close all files
|
|---|
| 1733 | gi_resetX = 0;
|
|---|
| 1734 |
|
|---|
| 1735 | //inform others we have to quit ....
|
|---|
| 1736 | gi_runStat = -11; //inform all that no update to happen any more
|
|---|
| 1737 | gj.readStat = -11; //inform all that no update to happen any more
|
|---|
| 1738 | }
|
|---|
| 1739 |
|
|---|
| 1740 | if (gi_resetS > 0)
|
|---|
| 1741 | {
|
|---|
| 1742 | //must close all open sockets ...
|
|---|
| 1743 | factPrintf(kInfo, -1, "Close all sockets...");
|
|---|
| 1744 |
|
|---|
| 1745 | for (int i = 0; i < MAX_SOCK; i++)
|
|---|
| 1746 | {
|
|---|
| 1747 | if (rd[i].sockStat != 0)
|
|---|
| 1748 | continue;
|
|---|
| 1749 |
|
|---|
| 1750 | GenSock(-1, i, 0, NULL, &rd[i]); //close and destroy open socket
|
|---|
| 1751 | if (i%7)
|
|---|
| 1752 | continue;
|
|---|
| 1753 |
|
|---|
| 1754 | gi_NumConnect[i / 7]-= cntsock ;
|
|---|
| 1755 | //gi.numConn[i / 7]--;
|
|---|
| 1756 | gj.numConn[i / 7]--;
|
|---|
| 1757 | sockDef[i / 7] = 0; //flag ro recreate the sockets ...
|
|---|
| 1758 | rd[i / 7].sockStat = -1; //and try to open asap
|
|---|
| 1759 | }
|
|---|
| 1760 | }
|
|---|
| 1761 |
|
|---|
| 1762 |
|
|---|
| 1763 | if (gi_resetR > 0)
|
|---|
| 1764 | {
|
|---|
| 1765 | //flag all events as 'read finished'
|
|---|
| 1766 | for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)
|
|---|
| 1767 | {
|
|---|
| 1768 | if (evtCtrl.evtStat[k0] > 0 && evtCtrl.evtStat[k0] < 90)
|
|---|
| 1769 | {
|
|---|
| 1770 | evtCtrl.evtStat[k0] = 91;
|
|---|
| 1771 | //gi.evtSkp++;
|
|---|
| 1772 | //gi.evtTot++;
|
|---|
| 1773 | }
|
|---|
| 1774 | }
|
|---|
| 1775 |
|
|---|
| 1776 | //and clear all buffers (might have to wait until all others are done)
|
|---|
| 1777 | int minclear;
|
|---|
| 1778 | if (gi_resetR == 1) {
|
|---|
| 1779 | minclear = 900;
|
|---|
| 1780 | factPrintf(kInfo, -1, "Drain all buffers ...");
|
|---|
| 1781 | } else {
|
|---|
| 1782 | minclear = 0;
|
|---|
| 1783 | factPrintf(kInfo, -1, "Flush all buffers ...");
|
|---|
| 1784 | }
|
|---|
| 1785 |
|
|---|
| 1786 | int numclear = 1;
|
|---|
| 1787 | while (numclear > 0)
|
|---|
| 1788 | {
|
|---|
| 1789 | numclear = 0;
|
|---|
| 1790 |
|
|---|
| 1791 | for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)
|
|---|
| 1792 | {
|
|---|
| 1793 | if (evtCtrl.evtStat[k0] > minclear)
|
|---|
| 1794 | {
|
|---|
| 1795 | const int id = evtCtrl.evtBuf[k0];
|
|---|
| 1796 | #ifdef EVTDEBUG
|
|---|
| 1797 | factPrintf(kDebug, -1, "ev %5d free event buffer, nb=%3d", mBuffer[id].evNum, mBuffer[id].nBoard);
|
|---|
| 1798 | #endif
|
|---|
| 1799 | mBufFree (id); //event written--> free memory
|
|---|
| 1800 | evtCtrl.evtStat[k0] = -1;
|
|---|
| 1801 | }
|
|---|
| 1802 | else
|
|---|
| 1803 | if (evtCtrl.evtStat[k0] > 0)
|
|---|
| 1804 | numclear++; //writing is still ongoing...
|
|---|
| 1805 |
|
|---|
| 1806 | if (k0 == evtCtrl.frstPtr && evtCtrl.evtStat[k0] < 0)
|
|---|
| 1807 | {
|
|---|
| 1808 | evtCtrl.frstPtr++;
|
|---|
| 1809 | evtCtrl.frstPtr %= MAX_EVT * MAX_RUN;
|
|---|
| 1810 | }
|
|---|
| 1811 | }
|
|---|
| 1812 |
|
|---|
| 1813 | usleep(1);
|
|---|
| 1814 | }
|
|---|
| 1815 | }
|
|---|
| 1816 |
|
|---|
| 1817 | if (gi_reset > 0)
|
|---|
| 1818 | {
|
|---|
| 1819 | if (gi_resetW > 0)
|
|---|
| 1820 | CloseRunFile (0, 0, 0); //ask all Runs to be closed
|
|---|
| 1821 |
|
|---|
| 1822 | if (gi_resetX > 0)
|
|---|
| 1823 | {
|
|---|
| 1824 | struct timespec xwait;
|
|---|
| 1825 | xwait.tv_sec = gi_resetX;
|
|---|
| 1826 | xwait.tv_nsec = 0;
|
|---|
| 1827 | nanosleep (&xwait, NULL);
|
|---|
| 1828 | }
|
|---|
| 1829 |
|
|---|
| 1830 | factPrintf(kInfo, -1, "Continue read Process ...");
|
|---|
| 1831 | gi_reset = 0;
|
|---|
| 1832 | goto START;
|
|---|
| 1833 | }
|
|---|
| 1834 |
|
|---|
| 1835 | factPrintf(kInfo, -1, "Exit read Process...");
|
|---|
| 1836 |
|
|---|
| 1837 | #ifdef THOMAS_MALLOC
|
|---|
| 1838 | factPrintf(kInfo, -1, "%ld Bytes flaged as in-use.", tgb_inuse);
|
|---|
| 1839 | #endif
|
|---|
| 1840 |
|
|---|
| 1841 | gi_runStat = -99;
|
|---|
| 1842 | gj.readStat = -99;
|
|---|
| 1843 |
|
|---|
| 1844 | factStat (gj);
|
|---|
| 1845 | factStatNew (gi);
|
|---|
| 1846 |
|
|---|
| 1847 | return 0;
|
|---|
| 1848 |
|
|---|
| 1849 | } /*-----------------------------------------------------------------*/
|
|---|
| 1850 |
|
|---|
| 1851 |
|
|---|
| 1852 | void *subProc(void *thrid)
|
|---|
| 1853 | {
|
|---|
| 1854 | const int64_t threadID = (int64_t)thrid;
|
|---|
| 1855 |
|
|---|
| 1856 | factPrintf(kInfo, -1, "Starting sub-process-thread %ld", threadID);
|
|---|
| 1857 |
|
|---|
| 1858 | while (g_runStat > -2) //in case of 'exit' we still must process pending events
|
|---|
| 1859 | {
|
|---|
| 1860 | int numWait = 0;
|
|---|
| 1861 | int numProc = 0;
|
|---|
| 1862 |
|
|---|
| 1863 | for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)
|
|---|
| 1864 | {
|
|---|
| 1865 | if (!(evtCtrl.evtStat[k0] == 1000 + threadID))
|
|---|
| 1866 | {
|
|---|
| 1867 | if (evtCtrl.evtStat[k0] < 1000 + threadID)
|
|---|
| 1868 | numWait++;
|
|---|
| 1869 |
|
|---|
| 1870 | continue;
|
|---|
| 1871 | }
|
|---|
| 1872 |
|
|---|
| 1873 | /*** if (evtCtrl.evtStat[k0] == 1000 + threadID) ****/
|
|---|
| 1874 |
|
|---|
| 1875 | int jret = 9100; // flag to be deleted (gi_resetR>1 : flush buffers asap)
|
|---|
| 1876 |
|
|---|
| 1877 | if (gi_resetR<=1)
|
|---|
| 1878 | {
|
|---|
| 1879 | const int id = evtCtrl.evtBuf[k0];
|
|---|
| 1880 |
|
|---|
| 1881 | jret = subProcEvt(threadID, mBuffer[id].FADhead,
|
|---|
| 1882 | mBuffer[id].fEvent, mBuffer[id].buffer);
|
|---|
| 1883 |
|
|---|
| 1884 |
|
|---|
| 1885 | if (jret <= threadID) {
|
|---|
| 1886 | factPrintf(kError, -1, "Process %ld wants to send event to process %d... not allowed.", threadID, jret);
|
|---|
| 1887 | jret = 5300;
|
|---|
| 1888 | } else if (jret <= 0)
|
|---|
| 1889 | jret = 9200 + threadID; // flag as 'to be deleted'
|
|---|
| 1890 | else if (jret >= gi_maxProc)
|
|---|
| 1891 | jret = 5200 + threadID; // flag as 'to be written'
|
|---|
| 1892 | else
|
|---|
| 1893 | jret = 1000 + jret; // flag for next proces
|
|---|
| 1894 | }
|
|---|
| 1895 |
|
|---|
| 1896 | evtCtrl.evtStat[k0] = jret;
|
|---|
| 1897 | numProc++;
|
|---|
| 1898 | }
|
|---|
| 1899 |
|
|---|
| 1900 | if (gj.readStat < -10 && numWait == 0) { //nothing left to do
|
|---|
| 1901 | factPrintf(kInfo, -1, "Exit subProcessing in process %ld", threadID);
|
|---|
| 1902 | return 0;
|
|---|
| 1903 | }
|
|---|
| 1904 |
|
|---|
| 1905 | //seems we have nothing to do, so sleep a little
|
|---|
| 1906 | if (numProc == 0)
|
|---|
| 1907 | usleep(1);
|
|---|
| 1908 | }
|
|---|
| 1909 |
|
|---|
| 1910 | factPrintf(kInfo, -1, "Ending sub-process-thread %ld", threadID);
|
|---|
| 1911 |
|
|---|
| 1912 | return 0;
|
|---|
| 1913 | }
|
|---|
| 1914 |
|
|---|
| 1915 | /*-----------------------------------------------------------------*/
|
|---|
| 1916 |
|
|---|
| 1917 |
|
|---|
| 1918 | void *
|
|---|
| 1919 | procEvt (void *ptr)
|
|---|
| 1920 | {
|
|---|
| 1921 | /* *** main loop processing file, including SW-trigger */
|
|---|
| 1922 | int status;
|
|---|
| 1923 |
|
|---|
| 1924 | int lastRun = 0; //usually run from last event still valid
|
|---|
| 1925 |
|
|---|
| 1926 | // cpu_set_t mask;
|
|---|
| 1927 | // int cpu = 1; //process thread (will be several in final version)
|
|---|
| 1928 |
|
|---|
| 1929 | factPrintf(kInfo, -1, "Starting process-thread with %d subprocesses", gi_maxProc);
|
|---|
| 1930 |
|
|---|
| 1931 | /* CPU_ZERO initializes all the bits in the mask to zero. */
|
|---|
| 1932 | // CPU_ZERO (&mask);
|
|---|
| 1933 | /* CPU_SET sets only the bit corresponding to cpu. */
|
|---|
| 1934 | // CPU_SET( 0 , &mask ); leave for system
|
|---|
| 1935 | // CPU_SET( 1 , &mask ); used by write process
|
|---|
| 1936 | // CPU_SET (2, &mask);
|
|---|
| 1937 | // CPU_SET (3, &mask);
|
|---|
| 1938 | // CPU_SET (4, &mask);
|
|---|
| 1939 | // CPU_SET (5, &mask);
|
|---|
| 1940 | // CPU_SET (6, &mask);
|
|---|
| 1941 | // CPU_SET( 7 , &mask ); used by read process
|
|---|
| 1942 | /* sched_setaffinity returns 0 in success */
|
|---|
| 1943 | // if (sched_setaffinity (0, sizeof (mask), &mask) == -1) {
|
|---|
| 1944 | // snprintf (str, MXSTR, "P ---> can not create affinity to %d", cpu);
|
|---|
| 1945 | // factOut (kWarn, -1, str);
|
|---|
| 1946 | // }
|
|---|
| 1947 |
|
|---|
| 1948 |
|
|---|
| 1949 | pthread_t thread[100];
|
|---|
| 1950 | // int th_ret[100];
|
|---|
| 1951 |
|
|---|
| 1952 | for (long long k = 0; k < gi_maxProc; k++) {
|
|---|
| 1953 | /*th_ret[k] =*/ pthread_create (&thread[k], NULL, subProc, (void *) k);
|
|---|
| 1954 | }
|
|---|
| 1955 |
|
|---|
| 1956 | // in case of 'exit' we still must process pending events
|
|---|
| 1957 | while (g_runStat > -2)
|
|---|
| 1958 | {
|
|---|
| 1959 | int numWait = 0;
|
|---|
| 1960 | int numProc = 0;
|
|---|
| 1961 |
|
|---|
| 1962 | for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)
|
|---|
| 1963 | {
|
|---|
| 1964 | if (evtCtrl.evtStat[k0] <= 90 || evtCtrl.evtStat[k0] >= 1000)
|
|---|
| 1965 | {
|
|---|
| 1966 | if (evtCtrl.evtStat[k0] >= 0 && evtCtrl.evtStat[k0] < 90)
|
|---|
| 1967 | numWait++;
|
|---|
| 1968 |
|
|---|
| 1969 | continue;
|
|---|
| 1970 | }
|
|---|
| 1971 |
|
|---|
| 1972 | /*** if (evtCtrl.evtStat[k0] > 90 && evtCtrl.evtStat[k0] < 1000) ***/
|
|---|
| 1973 |
|
|---|
| 1974 | //we are asked to flush buffers asap
|
|---|
| 1975 | if (gi_resetR > 1)
|
|---|
| 1976 | {
|
|---|
| 1977 | evtCtrl.evtStat[k0] = 9991;
|
|---|
| 1978 | continue;
|
|---|
| 1979 | }
|
|---|
| 1980 |
|
|---|
| 1981 | //-------- it is better to open the run already here, so call can be used to initialize
|
|---|
| 1982 | //-------- buffers etc. needed to interprete run (e.g. DRS calibration)
|
|---|
| 1983 | const int id = evtCtrl.evtBuf[k0];
|
|---|
| 1984 |
|
|---|
| 1985 | const uint32_t irun = mBuffer[id].runNum;
|
|---|
| 1986 | const int32_t ievt = mBuffer[id].evNum;
|
|---|
| 1987 |
|
|---|
| 1988 | int j = lastRun;
|
|---|
| 1989 | if (runCtrl[lastRun].runId != irun)
|
|---|
| 1990 | {
|
|---|
| 1991 | //check which fileID to use (or open if needed)
|
|---|
| 1992 | for (j = 0; j < MAX_RUN; j++) {
|
|---|
| 1993 | if (runCtrl[j].runId == irun)
|
|---|
| 1994 | break;
|
|---|
| 1995 | }
|
|---|
| 1996 | if (j >= MAX_RUN) {
|
|---|
| 1997 | factPrintf(kFatal, 901, "procEvt: Can not find run %d for event %d in %d", irun, ievt, id);
|
|---|
| 1998 | }
|
|---|
| 1999 | lastRun = j;
|
|---|
| 2000 | }
|
|---|
| 2001 |
|
|---|
| 2002 | if (runCtrl[j].fileId < 0)
|
|---|
| 2003 | {
|
|---|
| 2004 | //---- we need to open a new run ==> make sure all older runs are
|
|---|
| 2005 | //---- finished and marked to be closed ....
|
|---|
| 2006 | int j1;
|
|---|
| 2007 | for (j1 = 0; j1 < MAX_RUN; j1++) {
|
|---|
| 2008 | if (runCtrl[j1].fileId == 0) {
|
|---|
| 2009 | runCtrl[j1].procId = 2; //--> do no longer accept events for processing
|
|---|
| 2010 | //---- problem: processing still going on ==> must wait for closing ....
|
|---|
| 2011 | factPrintf(kInfo, -1, "procEvt: Finished run since new one opened %d", runCtrl[j1].runId);
|
|---|
| 2012 | runFinish1(runCtrl[j1].runId);
|
|---|
| 2013 | }
|
|---|
| 2014 | }
|
|---|
| 2015 |
|
|---|
| 2016 | actRun.Version = 1;
|
|---|
| 2017 | actRun.RunType = -1; //to be adapted
|
|---|
| 2018 |
|
|---|
| 2019 | actRun.Nroi = runCtrl[j].roi0;
|
|---|
| 2020 | actRun.NroiTM = runCtrl[j].roi8;
|
|---|
| 2021 | //ETIENNE don't reset it to zero as it is taken care of in DataWriteFits
|
|---|
| 2022 | // if (actRun.Nroi == actRun.NroiTM)
|
|---|
| 2023 | // actRun.NroiTM = 0;
|
|---|
| 2024 | actRun.RunTime = runCtrl[j].firstTime;
|
|---|
| 2025 | actRun.RunUsec = runCtrl[j].firstTime;
|
|---|
| 2026 | actRun.NBoard = NBOARDS;
|
|---|
| 2027 | actRun.NPix = NPIX;
|
|---|
| 2028 | actRun.NTm = NTMARK;
|
|---|
| 2029 | actRun.Nroi = mBuffer[id].nRoi;
|
|---|
| 2030 |
|
|---|
| 2031 | memcpy(actRun.FADhead, mBuffer[id].FADhead, NBOARDS*sizeof(PEVNT_HEADER));
|
|---|
| 2032 |
|
|---|
| 2033 | runCtrl[j].fileHd = runOpen (irun, &actRun, sizeof (actRun));
|
|---|
| 2034 | if (runCtrl[j].fileHd == NULL)
|
|---|
| 2035 | {
|
|---|
| 2036 | factPrintf(kError, 502, "procEvt: Could not open a file for run %d (runOpen failed)", irun);
|
|---|
| 2037 | runCtrl[j].fileId = 91;
|
|---|
| 2038 | runCtrl[j].procId = 91;
|
|---|
| 2039 | }
|
|---|
| 2040 | else
|
|---|
| 2041 | {
|
|---|
| 2042 | factPrintf(kInfo, -1, "procEvt: Opened new file for run %d (evt=%d)", irun, ievt);
|
|---|
| 2043 | runCtrl[j].fileId = 0;
|
|---|
| 2044 | runCtrl[j].procId = 0;
|
|---|
| 2045 | }
|
|---|
| 2046 | }
|
|---|
| 2047 |
|
|---|
| 2048 | //-------- also check if run shall be closed (==> skip event, but do not close the file !!! )
|
|---|
| 2049 | if (runCtrl[j].procId == 0)
|
|---|
| 2050 | {
|
|---|
| 2051 | if (runCtrl[j].closeTime < g_actTime ||
|
|---|
| 2052 | runCtrl[j].lastTime < g_actTime - 300 ||
|
|---|
| 2053 | runCtrl[j].maxEvt <= runCtrl[j].procEvt)
|
|---|
| 2054 | {
|
|---|
| 2055 | factPrintf(kInfo, 502, "procEvt: Reached end of run condition for run %d", irun);
|
|---|
| 2056 | runFinish1 (runCtrl[j].runId);
|
|---|
| 2057 | runCtrl[j].procId = 1;
|
|---|
| 2058 | }
|
|---|
| 2059 | }
|
|---|
| 2060 | if (runCtrl[j].procId != 0) {
|
|---|
| 2061 | #ifdef EVTDEBUG
|
|---|
| 2062 | factPrintf(kDebug, 502, "procEvt: Skip event %d because no active run %d", ievt, irun);
|
|---|
| 2063 | #endif
|
|---|
| 2064 | evtCtrl.evtStat[k0] = 9091;
|
|---|
| 2065 | continue;
|
|---|
| 2066 | }
|
|---|
| 2067 |
|
|---|
| 2068 | //--------
|
|---|
| 2069 | //--------
|
|---|
| 2070 |
|
|---|
| 2071 | const int roi = mBuffer[id].nRoi;
|
|---|
| 2072 | //const int roiTM = mBuffer[id].nRoiTM;
|
|---|
| 2073 |
|
|---|
| 2074 | //make sure unused pixels/tmarks are cleared to zero
|
|---|
| 2075 | //ETIENNE don't reset it to zero as it is taken care of in DataWriteFits
|
|---|
| 2076 | // if (roiTM == roi)
|
|---|
| 2077 | // roiTM = 0;
|
|---|
| 2078 | for (int ip=0; ip<NPIX; ip++)
|
|---|
| 2079 | {
|
|---|
| 2080 | if (mBuffer[id].fEvent->StartPix[ip] == -1)
|
|---|
| 2081 | {
|
|---|
| 2082 | const int dest = ip * roi;
|
|---|
| 2083 | memset(&mBuffer[id].fEvent->Adc_Data[dest], 0, roi * 2);
|
|---|
| 2084 | }
|
|---|
| 2085 | }
|
|---|
| 2086 |
|
|---|
| 2087 | for (int it=0; it<NTMARK; it++)
|
|---|
| 2088 | {
|
|---|
| 2089 | if (mBuffer[id].fEvent->StartTM[it] == -1)
|
|---|
| 2090 | {
|
|---|
| 2091 | const int dest = it * roi + NPIX * roi;
|
|---|
| 2092 | memset(&mBuffer[id].fEvent->Adc_Data[dest], 0, roi * 2);
|
|---|
| 2093 | }
|
|---|
| 2094 | }
|
|---|
| 2095 |
|
|---|
| 2096 | //and set correct event header ; also check for consistency in event (not yet)
|
|---|
| 2097 | mBuffer[id].fEvent->Roi = mBuffer[id].nRoi;
|
|---|
| 2098 | mBuffer[id].fEvent->RoiTM = mBuffer[id].nRoiTM;
|
|---|
| 2099 | mBuffer[id].fEvent->EventNum = mBuffer[id].evNum;
|
|---|
| 2100 | mBuffer[id].fEvent->TriggerNum = mBuffer[id].trgNum;
|
|---|
| 2101 | mBuffer[id].fEvent->TriggerType = mBuffer[id].trgTyp;
|
|---|
| 2102 | mBuffer[id].fEvent->Errors[0] = mBuffer[id].Errors[0];
|
|---|
| 2103 | mBuffer[id].fEvent->Errors[1] = mBuffer[id].Errors[1];
|
|---|
| 2104 | mBuffer[id].fEvent->Errors[2] = mBuffer[id].Errors[2];
|
|---|
| 2105 | mBuffer[id].fEvent->Errors[3] = mBuffer[id].Errors[3];
|
|---|
| 2106 | mBuffer[id].fEvent->SoftTrig = 0;
|
|---|
| 2107 |
|
|---|
| 2108 |
|
|---|
| 2109 | for (int ib=0; ib<NBOARDS; ib++)
|
|---|
| 2110 | {
|
|---|
| 2111 | // board is not read
|
|---|
| 2112 | if (mBuffer[id].board[ib] == -1)
|
|---|
| 2113 | {
|
|---|
| 2114 | mBuffer[id].FADhead[ib].start_package_flag = 0;
|
|---|
| 2115 | mBuffer[id].fEvent->BoardTime[ib] = 0;
|
|---|
| 2116 | }
|
|---|
| 2117 | else
|
|---|
| 2118 | {
|
|---|
| 2119 | mBuffer[id].fEvent->BoardTime[ib] = mBuffer[id].FADhead[ib].time;
|
|---|
| 2120 | }
|
|---|
| 2121 | }
|
|---|
| 2122 |
|
|---|
| 2123 | const int rc = eventCheck(mBuffer[id].runNum, mBuffer[id].FADhead,
|
|---|
| 2124 | mBuffer[id].fEvent);
|
|---|
| 2125 | //gi.procTot++;
|
|---|
| 2126 | numProc++;
|
|---|
| 2127 |
|
|---|
| 2128 | if (rc < 0)
|
|---|
| 2129 | {
|
|---|
| 2130 | evtCtrl.evtStat[k0] = 9999; //flag event to be skipped
|
|---|
| 2131 | //gi.procErr++;
|
|---|
| 2132 | }
|
|---|
| 2133 | else
|
|---|
| 2134 | {
|
|---|
| 2135 | evtCtrl.evtStat[k0] = 1000;
|
|---|
| 2136 | runCtrl[j].procEvt++;
|
|---|
| 2137 | }
|
|---|
| 2138 | }
|
|---|
| 2139 |
|
|---|
| 2140 | if (gj.readStat < -10 && numWait == 0) { //nothing left to do
|
|---|
| 2141 | factPrintf(kInfo, -1, "Exit Processing Process ...");
|
|---|
| 2142 | gp_runStat = -22; //==> we should exit
|
|---|
| 2143 | gj.procStat = -22; //==> we should exit
|
|---|
| 2144 | return 0;
|
|---|
| 2145 | }
|
|---|
| 2146 |
|
|---|
| 2147 | //seems we have nothing to do, so sleep a little
|
|---|
| 2148 | if (numProc == 0)
|
|---|
| 2149 | usleep(1);
|
|---|
| 2150 |
|
|---|
| 2151 | gp_runStat = gi_runStat;
|
|---|
| 2152 | gj.procStat = gj.readStat;
|
|---|
| 2153 |
|
|---|
| 2154 | }
|
|---|
| 2155 |
|
|---|
| 2156 | //we are asked to abort asap ==> must flag all remaining events
|
|---|
| 2157 | // when gi_runStat claims that all events are in the buffer...
|
|---|
| 2158 |
|
|---|
| 2159 | factPrintf(kInfo, -1, "Abort Processing Process ...");
|
|---|
| 2160 |
|
|---|
| 2161 | for (int k = 0; k < gi_maxProc; k++) {
|
|---|
| 2162 | pthread_join (thread[k], (void **) &status);
|
|---|
| 2163 | }
|
|---|
| 2164 |
|
|---|
| 2165 | for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)
|
|---|
| 2166 | {
|
|---|
| 2167 | if (evtCtrl.evtStat[k0] >= 0 && evtCtrl.evtStat[k0] < 1000)
|
|---|
| 2168 | evtCtrl.evtStat[k0] = 9800; //flag event as 'processed'
|
|---|
| 2169 | }
|
|---|
| 2170 |
|
|---|
| 2171 | gp_runStat = -99;
|
|---|
| 2172 | gj.procStat = -99;
|
|---|
| 2173 |
|
|---|
| 2174 | return 0;
|
|---|
| 2175 |
|
|---|
| 2176 | } /*-----------------------------------------------------------------*/
|
|---|
| 2177 |
|
|---|
| 2178 | int
|
|---|
| 2179 | CloseRunFile (uint32_t runId, uint32_t closeTime, uint32_t maxEvt)
|
|---|
| 2180 | {
|
|---|
| 2181 | /* close run runId (all all runs if runId=0) */
|
|---|
| 2182 | /* return: 0=close scheduled / >0 already closed / <0 does not exist */
|
|---|
| 2183 | int j;
|
|---|
| 2184 |
|
|---|
| 2185 |
|
|---|
| 2186 | if (runId == 0) {
|
|---|
| 2187 | for (j = 0; j < MAX_RUN; j++) {
|
|---|
| 2188 | if (runCtrl[j].fileId == 0) { //run is open
|
|---|
| 2189 | runCtrl[j].closeTime = closeTime;
|
|---|
| 2190 | runCtrl[j].maxEvt = maxEvt;
|
|---|
| 2191 | }
|
|---|
| 2192 | }
|
|---|
| 2193 | return 0;
|
|---|
| 2194 | }
|
|---|
| 2195 |
|
|---|
| 2196 | for (j = 0; j < MAX_RUN; j++) {
|
|---|
| 2197 | if (runCtrl[j].runId == runId) {
|
|---|
| 2198 | if (runCtrl[j].fileId == 0) { //run is open
|
|---|
| 2199 | runCtrl[j].closeTime = closeTime;
|
|---|
| 2200 | runCtrl[j].maxEvt = maxEvt;
|
|---|
| 2201 | return 0;
|
|---|
| 2202 | } else if (runCtrl[j].fileId < 0) { //run not yet opened
|
|---|
| 2203 | runCtrl[j].closeTime = closeTime;
|
|---|
| 2204 | runCtrl[j].maxEvt = maxEvt;
|
|---|
| 2205 | return +1;
|
|---|
| 2206 | } else { // run already closed
|
|---|
| 2207 | return +2;
|
|---|
| 2208 | }
|
|---|
| 2209 | }
|
|---|
| 2210 | } //we only reach here if the run was never created
|
|---|
| 2211 | return -1;
|
|---|
| 2212 |
|
|---|
| 2213 | }
|
|---|
| 2214 |
|
|---|
| 2215 | void checkAndCloseRun(int j, int irun, int cond, int where)
|
|---|
| 2216 | {
|
|---|
| 2217 | if (!cond &&
|
|---|
| 2218 | runCtrl[j].closeTime >= g_actTime &&
|
|---|
| 2219 | runCtrl[j].lastTime >= g_actTime - 300 &&
|
|---|
| 2220 | runCtrl[j].maxEvt > runCtrl[j].actEvt)
|
|---|
| 2221 | return;
|
|---|
| 2222 |
|
|---|
| 2223 | //close run for whatever reason
|
|---|
| 2224 | int ii = 0;
|
|---|
| 2225 | if (cond)
|
|---|
| 2226 | ii = 1;
|
|---|
| 2227 | if (runCtrl[j].closeTime < g_actTime)
|
|---|
| 2228 | ii |= 2; // = 2;
|
|---|
| 2229 | if (runCtrl[j].lastTime < g_actTime - 300)
|
|---|
| 2230 | ii |= 4; // = 3;
|
|---|
| 2231 | if (runCtrl[j].maxEvt <= runCtrl[j].actEvt)
|
|---|
| 2232 | ii |= 8; // = 4;
|
|---|
| 2233 |
|
|---|
| 2234 | if (runCtrl[j].procId == 0)
|
|---|
| 2235 | {
|
|---|
| 2236 | runFinish1(runCtrl[j].runId);
|
|---|
| 2237 | runCtrl[j].procId = 92;
|
|---|
| 2238 | }
|
|---|
| 2239 |
|
|---|
| 2240 | runCtrl[j].closeTime = g_actTime - 1;
|
|---|
| 2241 |
|
|---|
| 2242 | const int rc = runClose(runCtrl[j].fileHd, &runTail[j], sizeof(runTail[j]));
|
|---|
| 2243 | if (rc<0)
|
|---|
| 2244 | {
|
|---|
| 2245 | factPrintf(kError, 503, "writeEvt-%d: Error closing run %d (runClose,rc=%d)",
|
|---|
| 2246 | where, runCtrl[j].runId, rc);
|
|---|
| 2247 | runCtrl[j].fileId = 92+where*2;
|
|---|
| 2248 | }
|
|---|
| 2249 | else
|
|---|
| 2250 | {
|
|---|
| 2251 | factPrintf(kInfo, 503, "writeEvt-%d: Closed run %d (reason=%d)",
|
|---|
| 2252 | where, irun, ii);
|
|---|
| 2253 | runCtrl[j].fileId = 93+where*2;
|
|---|
| 2254 | }
|
|---|
| 2255 | }
|
|---|
| 2256 |
|
|---|
| 2257 | /*-----------------------------------------------------------------*/
|
|---|
| 2258 |
|
|---|
| 2259 |
|
|---|
| 2260 | void *writeEvt (void *ptr)
|
|---|
| 2261 | {
|
|---|
| 2262 | /* *** main loop writing event (including opening and closing run-files */
|
|---|
| 2263 |
|
|---|
| 2264 | // cpu_set_t mask;
|
|---|
| 2265 | // int cpu = 1; //write thread
|
|---|
| 2266 |
|
|---|
| 2267 | factPrintf(kInfo, -1, "Starting write-thread");
|
|---|
| 2268 |
|
|---|
| 2269 | /* CPU_ZERO initializes all the bits in the mask to zero. */
|
|---|
| 2270 | // CPU_ZERO (&mask);
|
|---|
| 2271 | /* CPU_SET sets only the bit corresponding to cpu. */
|
|---|
| 2272 | // CPU_SET (cpu, &mask);
|
|---|
| 2273 | /* sched_setaffinity returns 0 in success */
|
|---|
| 2274 | // if (sched_setaffinity (0, sizeof (mask), &mask) == -1) {
|
|---|
| 2275 | // snprintf (str, MXSTR, "W ---> can not create affinity to %d", cpu);
|
|---|
| 2276 | // }
|
|---|
| 2277 |
|
|---|
| 2278 | int lastRun = 0; //usually run from last event still valid
|
|---|
| 2279 |
|
|---|
| 2280 | while (g_runStat > -2)
|
|---|
| 2281 | {
|
|---|
| 2282 | int numWrite = 0;
|
|---|
| 2283 | int numWait = 0;
|
|---|
| 2284 |
|
|---|
| 2285 | for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)
|
|---|
| 2286 | {
|
|---|
| 2287 | if (evtCtrl.evtStat[k0] <= 5000 || evtCtrl.evtStat[k0] >= 9000)
|
|---|
| 2288 | {
|
|---|
| 2289 | if (evtCtrl.evtStat[k0] > 0 && evtCtrl.evtStat[k0] < 9000)
|
|---|
| 2290 | numWait++;
|
|---|
| 2291 |
|
|---|
| 2292 | continue;
|
|---|
| 2293 | }
|
|---|
| 2294 |
|
|---|
| 2295 | /*** if (evtCtrl.evtStat[k0] > 5000 && evtCtrl.evtStat[k0] < 9000) ***/
|
|---|
| 2296 |
|
|---|
| 2297 | //we must drain the buffer asap
|
|---|
| 2298 | if (gi_resetR > 1)
|
|---|
| 2299 | {
|
|---|
| 2300 | evtCtrl.evtStat[k0] = 9904;
|
|---|
| 2301 | continue;
|
|---|
| 2302 | }
|
|---|
| 2303 |
|
|---|
| 2304 | const int id = evtCtrl.evtBuf[k0];
|
|---|
| 2305 |
|
|---|
| 2306 | const uint32_t irun = mBuffer[id].runNum;
|
|---|
| 2307 | const int32_t ievt = mBuffer[id].evNum;
|
|---|
| 2308 |
|
|---|
| 2309 | //gi.wrtTot++;
|
|---|
| 2310 |
|
|---|
| 2311 | int j = lastRun;
|
|---|
| 2312 |
|
|---|
| 2313 | if (runCtrl[lastRun].runId != irun)
|
|---|
| 2314 | {
|
|---|
| 2315 | //check which fileID to use (or open if needed)
|
|---|
| 2316 | for (j = 0; j < MAX_RUN; j++)
|
|---|
| 2317 | {
|
|---|
| 2318 | if (runCtrl[j].runId == irun)
|
|---|
| 2319 | break;
|
|---|
| 2320 | }
|
|---|
| 2321 |
|
|---|
| 2322 | if (j >= MAX_RUN)
|
|---|
| 2323 | {
|
|---|
| 2324 | factPrintf(kFatal, 901, "writeEvt: Can not find run %d for event %d in %d", irun, ievt, id);
|
|---|
| 2325 | //gi.wrtErr++;
|
|---|
| 2326 | }
|
|---|
| 2327 |
|
|---|
| 2328 | lastRun = j;
|
|---|
| 2329 | }
|
|---|
| 2330 |
|
|---|
| 2331 | if (runCtrl[j].fileId < 0) {
|
|---|
| 2332 | actRun.Version = 1;
|
|---|
| 2333 | actRun.RunType = -1; //to be adapted
|
|---|
| 2334 |
|
|---|
| 2335 | actRun.Nroi = runCtrl[j].roi0;
|
|---|
| 2336 | actRun.NroiTM = runCtrl[j].roi8;
|
|---|
| 2337 | //ETIENNE don't reset it to zero as it is taken care of in DataWriteFits
|
|---|
| 2338 | // if (actRun.Nroi == actRun.NroiTM)
|
|---|
| 2339 | // actRun.NroiTM = 0;
|
|---|
| 2340 | actRun.RunTime = runCtrl[j].firstTime;
|
|---|
| 2341 | actRun.RunUsec = runCtrl[j].firstTime;
|
|---|
| 2342 | actRun.NBoard = NBOARDS;
|
|---|
| 2343 | actRun.NPix = NPIX;
|
|---|
| 2344 | actRun.NTm = NTMARK;
|
|---|
| 2345 | actRun.Nroi = mBuffer[id].nRoi;
|
|---|
| 2346 | memcpy (actRun.FADhead, mBuffer[id].FADhead,
|
|---|
| 2347 | NBOARDS * sizeof (PEVNT_HEADER));
|
|---|
| 2348 |
|
|---|
| 2349 | runCtrl[j].fileHd =
|
|---|
| 2350 | runOpen (irun, &actRun, sizeof (actRun));
|
|---|
| 2351 | if (runCtrl[j].fileHd == NULL) {
|
|---|
| 2352 | factPrintf(kError, 502, "writeEvt: Could not open a file for run %d (runOpen failed)", irun);
|
|---|
| 2353 | runCtrl[j].fileId = 91;
|
|---|
| 2354 | } else {
|
|---|
| 2355 | factPrintf(kInfo, -1, "writeEvt: Opened new file for run %d (evt %d)", irun, ievt);
|
|---|
| 2356 | runCtrl[j].fileId = 0;
|
|---|
| 2357 | }
|
|---|
| 2358 |
|
|---|
| 2359 | }
|
|---|
| 2360 |
|
|---|
| 2361 | if (runCtrl[j].fileId != 0) {
|
|---|
| 2362 | if (runCtrl[j].fileId < 0) {
|
|---|
| 2363 | factPrintf(kError, 123, "writeEvt: Never opened file for run %d", irun);
|
|---|
| 2364 | } else if (runCtrl[j].fileId < 100) {
|
|---|
| 2365 | factPrintf(kWarn, 123, "writeEvt: File for run %d is closed", irun);
|
|---|
| 2366 | runCtrl[j].fileId += 100;
|
|---|
| 2367 | } else {
|
|---|
| 2368 | #ifdef EVTDEBUG
|
|---|
| 2369 | factPrintf(kDebug, 123, "writeEvt: File for run %d is closed", irun);
|
|---|
| 2370 | #endif
|
|---|
| 2371 | }
|
|---|
| 2372 | evtCtrl.evtStat[k0] = 9903;
|
|---|
| 2373 | //gi.wrtErr++;
|
|---|
| 2374 | } else {
|
|---|
| 2375 | // snprintf (str, MXSTR,"write event %d size %d",ievt,sizeof (mBuffer[id]));
|
|---|
| 2376 | // factOut (kInfo, 504, str);
|
|---|
| 2377 | const int rc = runWrite (runCtrl[j].fileHd, mBuffer[id].fEvent,
|
|---|
| 2378 | sizeof (mBuffer[id]));
|
|---|
| 2379 | if (rc >= 0) {
|
|---|
| 2380 | runCtrl[j].lastTime = g_actTime;
|
|---|
| 2381 | runCtrl[j].actEvt++;
|
|---|
| 2382 | evtCtrl.evtStat[k0] = 9901;
|
|---|
| 2383 | #ifdef EVTDEBUG
|
|---|
| 2384 | factPrintf(kDebug, 504, "%5d successfully wrote for run %d id %5d", ievt, irun, k0);
|
|---|
| 2385 | #endif
|
|---|
| 2386 | // gj.writEvt++ ;
|
|---|
| 2387 | } else {
|
|---|
| 2388 | factPrintf(kError, 503, "writeEvt: Writing event for run %d failed (runWrite)", irun);
|
|---|
| 2389 | evtCtrl.evtStat[k0] = 9902;
|
|---|
| 2390 | //gi.wrtErr++;
|
|---|
| 2391 | }
|
|---|
| 2392 |
|
|---|
| 2393 | checkAndCloseRun(j, irun, rc<0, 1);
|
|---|
| 2394 | }
|
|---|
| 2395 | }
|
|---|
| 2396 |
|
|---|
| 2397 | //check if we should close a run (mainly when no event pending)
|
|---|
| 2398 | //ETIENNE but first figure out which one is the latest run with a complete event.
|
|---|
| 2399 | //i.e. max run Id and lastEvt >= 0
|
|---|
| 2400 | //this condition is sufficient because all pending events were written already in the loop just above
|
|---|
| 2401 | //actrun
|
|---|
| 2402 | uint32_t lastStartedTime = 0;
|
|---|
| 2403 | uint32_t runIdFound = 0;
|
|---|
| 2404 | if (actrun != 0)
|
|---|
| 2405 | {//If we have an active run, look for its start time
|
|---|
| 2406 | for (int j=0;j<MAX_RUN;j++)
|
|---|
| 2407 | {
|
|---|
| 2408 | if (runCtrl[j].runId == actrun)
|
|---|
| 2409 | {
|
|---|
| 2410 | lastStartedTime = runCtrl[j].lastTime;
|
|---|
| 2411 | runIdFound = 1;
|
|---|
| 2412 | }
|
|---|
| 2413 | }
|
|---|
| 2414 | }
|
|---|
| 2415 | else
|
|---|
| 2416 | runIdFound = 1;
|
|---|
| 2417 |
|
|---|
| 2418 | if (runIdFound == 0)
|
|---|
| 2419 | {
|
|---|
| 2420 | factPrintf(kInfo, 0, "An Active run (number %u) has been registered, but it could not be found in the runs list", actrun);
|
|---|
| 2421 | }
|
|---|
| 2422 | // snprintf(str, MXSTR, "Maximum runId: %d", maxStartedRun);
|
|---|
| 2423 | // factOut(kInfo, 000, str);
|
|---|
| 2424 | //Also check if some files will never be opened
|
|---|
| 2425 | //EDIT: this is completely useless, because as run Numbers are taken from FADs board,
|
|---|
| 2426 | //I will never get run numbers for which no file is to be opened
|
|---|
| 2427 | for (int j=0;j<MAX_RUN;j++)
|
|---|
| 2428 | {
|
|---|
| 2429 | if ((runCtrl[j].fileId < 0) &&
|
|---|
| 2430 | (runCtrl[j].lastTime < lastStartedTime) &&
|
|---|
| 2431 | (runCtrl[j].runId != 0))
|
|---|
| 2432 | {
|
|---|
| 2433 | factPrintf(kInfo, 0, "writeEvt: No file will be opened for run %u. Last run: %u (started)", runCtrl[j].runId, actrun);
|
|---|
| 2434 | ;//TODO notify that this run will never be opened
|
|---|
| 2435 | }
|
|---|
| 2436 | }
|
|---|
| 2437 | for (int j=0; j<MAX_RUN; j++)
|
|---|
| 2438 | {
|
|---|
| 2439 | if (runCtrl[j].fileId == 0)
|
|---|
| 2440 | {
|
|---|
| 2441 | //ETIENNE added the condition at this line. dunno what to do with run 0: skipping it
|
|---|
| 2442 | const int cond = runCtrl[j].lastTime < lastStartedTime && runCtrl[j].runId != 0;
|
|---|
| 2443 | checkAndCloseRun(j, runCtrl[j].runId, cond, 2);
|
|---|
| 2444 | }
|
|---|
| 2445 | }
|
|---|
| 2446 |
|
|---|
| 2447 | //seems we have nothing to do, so sleep a little
|
|---|
| 2448 | if (numWrite == 0)
|
|---|
| 2449 | usleep(1);
|
|---|
| 2450 |
|
|---|
| 2451 | if (gj.readStat < -10 && numWait == 0) { //nothing left to do
|
|---|
| 2452 | factPrintf(kInfo, -1, "Finish Write Process ...");
|
|---|
| 2453 | gw_runStat = -22; //==> we should exit
|
|---|
| 2454 | gj.writStat = -22; //==> we should exit
|
|---|
| 2455 | goto closerun;
|
|---|
| 2456 | }
|
|---|
| 2457 | gw_runStat = gi_runStat;
|
|---|
| 2458 | gj.writStat = gj.readStat;
|
|---|
| 2459 |
|
|---|
| 2460 | }
|
|---|
| 2461 |
|
|---|
| 2462 | //must close all open files ....
|
|---|
| 2463 | factPrintf(kInfo, -1, "Abort Writing Process ...");
|
|---|
| 2464 |
|
|---|
| 2465 | closerun:
|
|---|
| 2466 | factPrintf(kInfo, -1, "Close all open files ...");
|
|---|
| 2467 | for (int j=0; j<MAX_RUN; j++)
|
|---|
| 2468 | {
|
|---|
| 2469 | if (runCtrl[j].fileId == 0)
|
|---|
| 2470 | checkAndCloseRun(j, runCtrl[j].runId, 1, 3);
|
|---|
| 2471 | }
|
|---|
| 2472 |
|
|---|
| 2473 | gw_runStat = -99;
|
|---|
| 2474 | gj.writStat = -99;
|
|---|
| 2475 | factPrintf(kInfo, -1, "Exit Writing Process ...");
|
|---|
| 2476 | return 0;
|
|---|
| 2477 |
|
|---|
| 2478 |
|
|---|
| 2479 |
|
|---|
| 2480 |
|
|---|
| 2481 | } /*-----------------------------------------------------------------*/
|
|---|
| 2482 |
|
|---|
| 2483 |
|
|---|
| 2484 |
|
|---|
| 2485 |
|
|---|
| 2486 | void
|
|---|
| 2487 | StartEvtBuild ()
|
|---|
| 2488 | {
|
|---|
| 2489 |
|
|---|
| 2490 | int i, /*j,*/ imax, status/*, th_ret[50]*/;
|
|---|
| 2491 | pthread_t thread[50];
|
|---|
| 2492 | struct timespec xwait;
|
|---|
| 2493 |
|
|---|
| 2494 | gi_runStat = gp_runStat = gw_runStat = 0;
|
|---|
| 2495 | gj.readStat = gj.procStat = gj.writStat = 0;
|
|---|
| 2496 |
|
|---|
| 2497 | factPrintf(kInfo, -1, "Starting EventBuilder V15.07 A");
|
|---|
| 2498 |
|
|---|
| 2499 | //initialize run control logics
|
|---|
| 2500 | for (i = 0; i < MAX_RUN; i++) {
|
|---|
| 2501 | runCtrl[i].runId = 0;
|
|---|
| 2502 | runCtrl[i].fileId = -2;
|
|---|
| 2503 | }
|
|---|
| 2504 |
|
|---|
| 2505 | //prepare for subProcesses
|
|---|
| 2506 | gi_maxSize = g_maxSize;
|
|---|
| 2507 | if (gi_maxSize <= 0)
|
|---|
| 2508 | gi_maxSize = 1;
|
|---|
| 2509 |
|
|---|
| 2510 | gi_maxProc = g_maxProc;
|
|---|
| 2511 | if (gi_maxProc <= 0 || gi_maxProc > 90) {
|
|---|
| 2512 | factPrintf(kFatal, 301, "Illegal number of processes %d", gi_maxProc);
|
|---|
| 2513 | gi_maxProc = 1;
|
|---|
| 2514 | }
|
|---|
| 2515 | //partially initialize event control logics
|
|---|
| 2516 | evtCtrl.frstPtr = 0;
|
|---|
| 2517 | evtCtrl.lastPtr = 0;
|
|---|
| 2518 |
|
|---|
| 2519 | //start all threads (more to come) when we are allowed to ....
|
|---|
| 2520 | while (g_runStat == 0) {
|
|---|
| 2521 | xwait.tv_sec = 0;
|
|---|
| 2522 | xwait.tv_nsec = 10000000; // sleep for ~10 msec
|
|---|
| 2523 | nanosleep (&xwait, NULL);
|
|---|
| 2524 | }
|
|---|
| 2525 |
|
|---|
| 2526 | i = 0;
|
|---|
| 2527 | /*th_ret[i] =*/ pthread_create (&thread[i], NULL, readFAD, NULL);
|
|---|
| 2528 | i++;
|
|---|
| 2529 | /*th_ret[i] =*/ pthread_create (&thread[i], NULL, procEvt, NULL);
|
|---|
| 2530 | i++;
|
|---|
| 2531 | /*th_ret[i] =*/ pthread_create (&thread[i], NULL, writeEvt, NULL);
|
|---|
| 2532 | i++;
|
|---|
| 2533 | imax = i;
|
|---|
| 2534 |
|
|---|
| 2535 |
|
|---|
| 2536 | #ifdef BILAND
|
|---|
| 2537 | xwait.tv_sec = 30;;
|
|---|
| 2538 | xwait.tv_nsec = 0; // sleep for ~20sec
|
|---|
| 2539 | nanosleep (&xwait, NULL);
|
|---|
| 2540 |
|
|---|
| 2541 | printf ("close all runs in 2 seconds\n");
|
|---|
| 2542 |
|
|---|
| 2543 | CloseRunFile (0, time (NULL) + 2, 0);
|
|---|
| 2544 |
|
|---|
| 2545 | xwait.tv_sec = 1;;
|
|---|
| 2546 | xwait.tv_nsec = 0; // sleep for ~20sec
|
|---|
| 2547 | nanosleep (&xwait, NULL);
|
|---|
| 2548 |
|
|---|
| 2549 | printf ("setting g_runstat to -1\n");
|
|---|
| 2550 |
|
|---|
| 2551 | g_runStat = -1;
|
|---|
| 2552 | #endif
|
|---|
| 2553 |
|
|---|
| 2554 |
|
|---|
| 2555 | //wait for all threads to finish
|
|---|
| 2556 | for (i = 0; i < imax; i++) {
|
|---|
| 2557 | /*j =*/ pthread_join (thread[i], (void **) &status);
|
|---|
| 2558 | }
|
|---|
| 2559 |
|
|---|
| 2560 | } /*-----------------------------------------------------------------*/
|
|---|
| 2561 |
|
|---|
| 2562 |
|
|---|
| 2563 |
|
|---|
| 2564 |
|
|---|
| 2565 |
|
|---|
| 2566 |
|
|---|
| 2567 |
|
|---|
| 2568 |
|
|---|
| 2569 |
|
|---|
| 2570 |
|
|---|
| 2571 |
|
|---|
| 2572 |
|
|---|
| 2573 |
|
|---|
| 2574 |
|
|---|
| 2575 |
|
|---|
| 2576 | /*-----------------------------------------------------------------*/
|
|---|
| 2577 | /*-----------------------------------------------------------------*/
|
|---|
| 2578 | /*-----------------------------------------------------------------*/
|
|---|
| 2579 | /*-----------------------------------------------------------------*/
|
|---|
| 2580 | /*-----------------------------------------------------------------*/
|
|---|
| 2581 |
|
|---|
| 2582 | #ifdef BILAND
|
|---|
| 2583 |
|
|---|
| 2584 | int
|
|---|
| 2585 | subProcEvt (int threadID, PEVNT_HEADER * fadhd, EVENT * event,
|
|---|
| 2586 | int8_t * buffer)
|
|---|
| 2587 | {
|
|---|
| 2588 | printf ("called subproc %d\n", threadID);
|
|---|
| 2589 | return threadID + 1;
|
|---|
| 2590 | }
|
|---|
| 2591 |
|
|---|
| 2592 |
|
|---|
| 2593 |
|
|---|
| 2594 |
|
|---|
| 2595 | /*-----------------------------------------------------------------*/
|
|---|
| 2596 | /*-----------------------------------------------------------------*/
|
|---|
| 2597 | /*-----------------------------------------------------------------*/
|
|---|
| 2598 | /*-----------------------------------------------------------------*/
|
|---|
| 2599 | /*-----------------------------------------------------------------*/
|
|---|
| 2600 |
|
|---|
| 2601 |
|
|---|
| 2602 |
|
|---|
| 2603 |
|
|---|
| 2604 | FileHandle_t
|
|---|
| 2605 | runOpen (uint32_t irun, RUN_HEAD * runhd, size_t len)
|
|---|
| 2606 | {
|
|---|
| 2607 | return 1;
|
|---|
| 2608 | };
|
|---|
| 2609 |
|
|---|
| 2610 | int
|
|---|
| 2611 | runWrite (FileHandle_t fileHd, EVENT * event, size_t len)
|
|---|
| 2612 | {
|
|---|
| 2613 | return 1;
|
|---|
| 2614 | usleep (10000);
|
|---|
| 2615 | return 1;
|
|---|
| 2616 | }
|
|---|
| 2617 |
|
|---|
| 2618 |
|
|---|
| 2619 | //{ return 1; } ;
|
|---|
| 2620 |
|
|---|
| 2621 | int
|
|---|
| 2622 | runClose (FileHandle_t fileHd, RUN_TAIL * runth, size_t len)
|
|---|
| 2623 | {
|
|---|
| 2624 | return 1;
|
|---|
| 2625 | };
|
|---|
| 2626 |
|
|---|
| 2627 |
|
|---|
| 2628 |
|
|---|
| 2629 |
|
|---|
| 2630 | int
|
|---|
| 2631 | eventCheck (uint32_t runNr, PEVNT_HEADER * fadhd, EVENT * event)
|
|---|
| 2632 | {
|
|---|
| 2633 | int i = 0;
|
|---|
| 2634 |
|
|---|
| 2635 | // printf("------------%d\n",ntohl(fadhd[7].fad_evt_counter) );
|
|---|
| 2636 | // for (i=0; i<NBOARDS; i++) {
|
|---|
| 2637 | // printf("b=%2d,=%5d\n",i,fadhd[i].board_id);
|
|---|
| 2638 | // }
|
|---|
| 2639 | return 0;
|
|---|
| 2640 | }
|
|---|
| 2641 |
|
|---|
| 2642 |
|
|---|
| 2643 | void
|
|---|
| 2644 | factStatNew (EVT_STAT gi)
|
|---|
| 2645 | {
|
|---|
| 2646 | int i;
|
|---|
| 2647 |
|
|---|
| 2648 | //for (i=0;i<MAX_SOCK;i++) {
|
|---|
| 2649 | // printf("%4d",gi.numRead[i]);
|
|---|
| 2650 | // if (i%20 == 0 ) printf("\n");
|
|---|
| 2651 | //}
|
|---|
| 2652 | }
|
|---|
| 2653 |
|
|---|
| 2654 | void
|
|---|
| 2655 | gotNewRun (int runnr, PEVNT_HEADER * headers)
|
|---|
| 2656 | {
|
|---|
| 2657 | printf ("got new run %d\n", runnr);
|
|---|
| 2658 | return;
|
|---|
| 2659 | }
|
|---|
| 2660 |
|
|---|
| 2661 | void
|
|---|
| 2662 | factStat (GUI_STAT gj)
|
|---|
| 2663 | {
|
|---|
| 2664 | // printf("stat: bfr%5lu skp%4lu free%4lu (tot%7lu) mem%12lu rd%12lu %3lu\n",
|
|---|
| 2665 | // array[0],array[1],array[2],array[3],array[4],array[5],array[6]);
|
|---|
| 2666 | }
|
|---|
| 2667 |
|
|---|
| 2668 |
|
|---|
| 2669 | void
|
|---|
| 2670 | debugRead (int isock, int ibyte, int32_t event, int32_t ftmevt, int32_t runnr,
|
|---|
| 2671 | int state, uint32_t tsec, uint32_t tusec)
|
|---|
| 2672 | {
|
|---|
| 2673 | // printf("%3d %5d %9d %3d %12d\n",isock, ibyte, event, state, tusec) ;
|
|---|
| 2674 | }
|
|---|
| 2675 |
|
|---|
| 2676 |
|
|---|
| 2677 |
|
|---|
| 2678 | void
|
|---|
| 2679 | debugStream (int isock, void *buf, int len)
|
|---|
| 2680 | {
|
|---|
| 2681 | }
|
|---|
| 2682 |
|
|---|
| 2683 | void
|
|---|
| 2684 | debugHead (int i, int j, void *buf)
|
|---|
| 2685 | {
|
|---|
| 2686 | }
|
|---|
| 2687 |
|
|---|
| 2688 |
|
|---|
| 2689 | void
|
|---|
| 2690 | factOut (int severity, int err, char *message)
|
|---|
| 2691 | {
|
|---|
| 2692 | static FILE *fd;
|
|---|
| 2693 | static int file = 0;
|
|---|
| 2694 |
|
|---|
| 2695 | if (file == 0) {
|
|---|
| 2696 | printf ("open file\n");
|
|---|
| 2697 | fd = fopen ("x.out", "w+");
|
|---|
| 2698 | file = 999;
|
|---|
| 2699 | }
|
|---|
| 2700 |
|
|---|
| 2701 | fprintf (fd, "%3d %3d | %s \n", severity, err, message);
|
|---|
| 2702 |
|
|---|
| 2703 | if (severity != kDebug)
|
|---|
| 2704 | printf ("%3d %3d | %s\n", severity, err, message);
|
|---|
| 2705 | }
|
|---|
| 2706 |
|
|---|
| 2707 |
|
|---|
| 2708 |
|
|---|
| 2709 | int
|
|---|
| 2710 | main ()
|
|---|
| 2711 | {
|
|---|
| 2712 | int i, b, c, p;
|
|---|
| 2713 | char ipStr[100];
|
|---|
| 2714 | struct in_addr IPaddr;
|
|---|
| 2715 |
|
|---|
| 2716 | g_maxMem = 1024 * 1024; //MBytes
|
|---|
| 2717 | //g_maxMem = g_maxMem * 1024 *10 ; //10GBytes
|
|---|
| 2718 | g_maxMem = g_maxMem * 200; //100MBytes
|
|---|
| 2719 |
|
|---|
| 2720 | g_maxProc = 20;
|
|---|
| 2721 | g_maxSize = 30000;
|
|---|
| 2722 |
|
|---|
| 2723 | g_runStat = 40;
|
|---|
| 2724 |
|
|---|
| 2725 | i = 0;
|
|---|
| 2726 |
|
|---|
| 2727 | // version for standard crates
|
|---|
| 2728 | //for (c=0; c<4,c++) {
|
|---|
| 2729 | // for (b=0; b<10; b++) {
|
|---|
| 2730 | // sprintf(ipStr,"10.0.%d.%d",128+c,128+b)
|
|---|
| 2731 | //
|
|---|
| 2732 | // inet_pton(PF_INET, ipStr, &IPaddr) ;
|
|---|
| 2733 | //
|
|---|
| 2734 | // g_port[i].sockAddr.sin_family = PF_INET;
|
|---|
| 2735 | // g_port[i].sockAddr.sin_port = htons(5000) ;
|
|---|
| 2736 | // g_port[i].sockAddr.sin_addr = IPaddr ;
|
|---|
| 2737 | // g_port[i].sockDef = 1 ;
|
|---|
| 2738 | // i++ ;
|
|---|
| 2739 | // }
|
|---|
| 2740 | //}
|
|---|
| 2741 | //
|
|---|
| 2742 | //version for PC-test *
|
|---|
| 2743 | for (c = 0; c < 4; c++) {
|
|---|
| 2744 | for (b = 0; b < 10; b++) {
|
|---|
| 2745 | sprintf (ipStr, "10.0.%d.11", 128 + c);
|
|---|
| 2746 | if (c < 2)
|
|---|
| 2747 | sprintf (ipStr, "10.0.%d.11", 128);
|
|---|
| 2748 | else
|
|---|
| 2749 | sprintf (ipStr, "10.0.%d.11", 131);
|
|---|
| 2750 | // if (c==0) sprintf(ipStr,"10.0.100.11") ;
|
|---|
| 2751 |
|
|---|
| 2752 | inet_pton (PF_INET, ipStr, &IPaddr);
|
|---|
| 2753 | p = 31919 + 100 * c + 10 * b;
|
|---|
| 2754 |
|
|---|
| 2755 |
|
|---|
| 2756 | g_port[i].sockAddr.sin_family = PF_INET;
|
|---|
| 2757 | g_port[i].sockAddr.sin_port = htons (p);
|
|---|
| 2758 | g_port[i].sockAddr.sin_addr = IPaddr;
|
|---|
| 2759 | g_port[i].sockDef = 1;
|
|---|
| 2760 |
|
|---|
| 2761 | i++;
|
|---|
| 2762 | }
|
|---|
| 2763 | }
|
|---|
| 2764 |
|
|---|
| 2765 |
|
|---|
| 2766 | //g_port[17].sockDef =-1 ;
|
|---|
| 2767 | //g_actBoards-- ;
|
|---|
| 2768 |
|
|---|
| 2769 | StartEvtBuild ();
|
|---|
| 2770 |
|
|---|
| 2771 | return 0;
|
|---|
| 2772 |
|
|---|
| 2773 | }
|
|---|
| 2774 | #endif
|
|---|