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 | int checkRoiConsistency(int i, int roi[]);
|
---|
538 |
|
---|
539 | int mBufEvt (int sk)
|
---|
540 | /*(int evID, uint runID, int nRoi[], int sk, int fadlen, int trgTyp, int trgNum, int fadNum)*/
|
---|
541 | {
|
---|
542 | // generate a new Event into mBuffer:
|
---|
543 | // make sure only complete Event are possible, so 'free' will always work
|
---|
544 | // returns index into mBuffer[], or negative value in case of error
|
---|
545 | // error: <-9000 if roi screwed up (not consistent with run)
|
---|
546 | // <-8000 (not consistent with event)
|
---|
547 | // <-7000 (not consistent with board)
|
---|
548 | // < 0 if no space left
|
---|
549 |
|
---|
550 | // int evFree;
|
---|
551 | // int headmem = 0;
|
---|
552 | // size_t needmem = 0;
|
---|
553 |
|
---|
554 |
|
---|
555 | int nRoi[9];
|
---|
556 | if (!checkRoiConsistency(sk, nRoi))
|
---|
557 | return -9999;
|
---|
558 |
|
---|
559 | //const int b = sk / 7;
|
---|
560 |
|
---|
561 | const int evID = rd[sk].evtID;
|
---|
562 | const uint runID = rd[sk].runID;
|
---|
563 | const int fadlen = rd[sk].fadLen;
|
---|
564 | const int trgTyp = rd[sk].ftmTyp;
|
---|
565 | const int trgNum = rd[sk].ftmID;
|
---|
566 | const int fadNum = rd[sk].evtID;
|
---|
567 |
|
---|
568 | int i = evID % MAX_EVT;
|
---|
569 | int evFree = -1;
|
---|
570 |
|
---|
571 | for (int k = 0; k < MAX_RUN; k++)
|
---|
572 | {
|
---|
573 | //event is already registered;
|
---|
574 | if (mBuffer[i].evNum == evID && mBuffer[i].runNum == runID)
|
---|
575 | {
|
---|
576 | // is it ok ????
|
---|
577 | if (mBuffer[i].nRoi != nRoi[0] || mBuffer[i].nRoiTM != nRoi[8])
|
---|
578 | {
|
---|
579 | factPrintf(kError, 821, "Mismatch of roi within event. Expected roi=%d and roi_tm=%d, got %d and %d.",
|
---|
580 | mBuffer[i].nRoi, mBuffer[i].nRoiTM, nRoi[0], nRoi[8]);
|
---|
581 | return -8201;
|
---|
582 | }
|
---|
583 |
|
---|
584 | // count for inconsistencies
|
---|
585 | if (mBuffer[i].trgNum != trgNum)
|
---|
586 | mBuffer[i].Errors[0]++;
|
---|
587 | if (mBuffer[i].fadNum != fadNum)
|
---|
588 | mBuffer[i].Errors[1]++;
|
---|
589 | if (mBuffer[i].trgTyp != trgTyp)
|
---|
590 | mBuffer[i].Errors[2]++;
|
---|
591 |
|
---|
592 | //everything seems fine so far ==> use this slot ....
|
---|
593 | return i;
|
---|
594 | }
|
---|
595 |
|
---|
596 | if (evFree < 0 && mBuffer[i].evNum < 0)
|
---|
597 | evFree = i;
|
---|
598 |
|
---|
599 | i += MAX_EVT;
|
---|
600 | }
|
---|
601 |
|
---|
602 |
|
---|
603 | //event does not yet exist; create it
|
---|
604 |
|
---|
605 | if (evFree < 0) //no space available in ctrl
|
---|
606 | {
|
---|
607 | factPrintf(kError, 881, "No control slot to keep event %d", evID);
|
---|
608 | return -1;
|
---|
609 | }
|
---|
610 |
|
---|
611 | i = evFree; //found free entry; use it ...
|
---|
612 |
|
---|
613 | // FIXME: This should be the time of the first receiped board
|
---|
614 | struct timeval tv;
|
---|
615 | gettimeofday (&tv, NULL);
|
---|
616 |
|
---|
617 | const uint32_t tsec = tv.tv_sec;
|
---|
618 | const uint32_t tusec = tv.tv_usec;
|
---|
619 |
|
---|
620 | //check if runId already registered in runCtrl
|
---|
621 | evFree = -1;
|
---|
622 |
|
---|
623 | uint oldest = g_actTime + 1000;
|
---|
624 | int jold = -1;
|
---|
625 |
|
---|
626 | int found = 0;
|
---|
627 |
|
---|
628 | for (int k = 0; k < MAX_RUN; k++)
|
---|
629 | {
|
---|
630 | if (runCtrl[k].runId == runID)
|
---|
631 | {
|
---|
632 | if (runCtrl[k].roi0 != nRoi[0] || runCtrl[k].roi8 != nRoi[8])
|
---|
633 | {
|
---|
634 | factPrintf(kError, 931, "Mismatch of roi within run. Expected roi=%d and roi_tm=%d, got %d and %d.",
|
---|
635 | runCtrl[k].roi0, runCtrl[k].roi8, nRoi[0], nRoi[8]);
|
---|
636 | return -9301;
|
---|
637 | }
|
---|
638 |
|
---|
639 | found = 1;
|
---|
640 | break;
|
---|
641 | }
|
---|
642 |
|
---|
643 | if (evFree>=0)
|
---|
644 | continue;
|
---|
645 |
|
---|
646 | //not yet used
|
---|
647 | if (runCtrl[k].fileId < 0)
|
---|
648 | {
|
---|
649 | evFree = k;
|
---|
650 | continue;
|
---|
651 | }
|
---|
652 |
|
---|
653 | //already closed
|
---|
654 | if (runCtrl[k].fileId > 0 && runCtrl[k].closeTime < oldest)
|
---|
655 | {
|
---|
656 | oldest = runCtrl[k].closeTime;
|
---|
657 | jold = k;
|
---|
658 | }
|
---|
659 | }
|
---|
660 |
|
---|
661 | if (!found)
|
---|
662 | {
|
---|
663 | if (evFree < 0 && jold < 0)
|
---|
664 | {
|
---|
665 | factPrintf(kFatal, 883, "Not able to register the new run %d", runID);
|
---|
666 | return -1001;
|
---|
667 | }
|
---|
668 |
|
---|
669 | if (evFree < 0)
|
---|
670 | evFree = jold;
|
---|
671 |
|
---|
672 | factPrintf(kInfo, 503, "New run %d (evFree=%d) registered with roi=%d and roi_tm=%d", runID,
|
---|
673 | evFree, nRoi[0], nRoi[8]);
|
---|
674 |
|
---|
675 | runCtrl[evFree].runId = runID;
|
---|
676 | runCtrl[evFree].roi0 = nRoi[0];
|
---|
677 | runCtrl[evFree].roi8 = nRoi[8];
|
---|
678 | runCtrl[evFree].fileId = -2;
|
---|
679 | runCtrl[evFree].procId = -2;
|
---|
680 | runCtrl[evFree].lastEvt = -1;
|
---|
681 | runCtrl[evFree].nextEvt = 0;
|
---|
682 | runCtrl[evFree].actEvt = 0;
|
---|
683 | runCtrl[evFree].procEvt = 0;
|
---|
684 | runCtrl[evFree].maxEvt = 999999999; //max number events allowed
|
---|
685 | runCtrl[evFree].firstTime = tsec;
|
---|
686 | runCtrl[evFree].firstUsec = tusec;
|
---|
687 | runCtrl[evFree].lastTime = tsec;
|
---|
688 | runCtrl[evFree].closeTime = tsec + 3600 * 24; //max time allowed
|
---|
689 |
|
---|
690 | runTail[evFree].nEventsOk = 0;
|
---|
691 | runTail[evFree].nEventsRej = 0;
|
---|
692 | runTail[evFree].nEventsBad = 0;
|
---|
693 | runTail[evFree].PCtime0 = 0;
|
---|
694 | runTail[evFree].PCtimeX = 0;
|
---|
695 | }
|
---|
696 |
|
---|
697 | // FIXME: Why is that done if FOUND?
|
---|
698 |
|
---|
699 | //flag all boards as unused
|
---|
700 | mBuffer[i].nBoard = 0;
|
---|
701 | for (int k = 0; k < NBOARDS; k++)
|
---|
702 | mBuffer[i].board[k] = -1;
|
---|
703 |
|
---|
704 | mBuffer[i].pcTime[0] = tsec;
|
---|
705 | mBuffer[i].pcTime[1] = tusec;
|
---|
706 | mBuffer[i].nRoi = nRoi[0];
|
---|
707 | mBuffer[i].nRoiTM = nRoi[8];
|
---|
708 | mBuffer[i].evNum = evID;
|
---|
709 | mBuffer[i].runNum = runID;
|
---|
710 | mBuffer[i].fadNum = fadNum;
|
---|
711 | mBuffer[i].trgNum = trgNum;
|
---|
712 | mBuffer[i].trgTyp = trgTyp;
|
---|
713 | mBuffer[i].Errors[0] = 0;
|
---|
714 | mBuffer[i].Errors[1] = 0;
|
---|
715 | mBuffer[i].Errors[2] = 0;
|
---|
716 | mBuffer[i].Errors[3] = 0;
|
---|
717 | mBuffer[i].fEvent = NULL;
|
---|
718 | mBuffer[i].buffer = NULL;
|
---|
719 | mBuffer[i].FADhead = NULL;
|
---|
720 |
|
---|
721 | /*
|
---|
722 | #ifdef ETIENNE_MALLOC
|
---|
723 | mBuffer[i].FADhead = ETI_Malloc(evID, i);
|
---|
724 | mBuffer[i].buffer = NULL;
|
---|
725 | if (mBuffer[i].FADhead != NULL)
|
---|
726 | mBuffer[i].fEvent = (EVENT*)&(((char*)(mBuffer[i].FADhead))[MAX_HEAD_MEM]);
|
---|
727 | else
|
---|
728 | {
|
---|
729 | mBuffer[i].fEvent = NULL;
|
---|
730 | gj.usdMem = 0;
|
---|
731 | for (int k=0;k<numAllocatedChunks;k++)
|
---|
732 | gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE;
|
---|
733 | if (gj.usdMem > gj.maxMem)
|
---|
734 | gj.maxMem = gj.usdMem;
|
---|
735 | else
|
---|
736 | {
|
---|
737 | factPrintf(kDebug, 882, "No memory left to keep event %6d sock %3d", evID, sk);
|
---|
738 | }
|
---|
739 | return -11;
|
---|
740 | }
|
---|
741 | #endif
|
---|
742 |
|
---|
743 | #ifdef STANDARD_MALLOC
|
---|
744 | mBuffer[i].FADhead = malloc (MAX_HEAD_MEM+MAX_EVT_MEM);
|
---|
745 | mBuffer[i].fEvent = NULL;
|
---|
746 | mBuffer[i].buffer = NULL;
|
---|
747 | if (mBuffer[i].FADhead == NULL)
|
---|
748 | {
|
---|
749 | factPrintf(kError, 882, "malloc header failed for event %d", evID);
|
---|
750 | return -12;
|
---|
751 | }
|
---|
752 | mBuffer[i].fEvent = (void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM);
|
---|
753 | #endif
|
---|
754 |
|
---|
755 | #ifdef ETIENNE_MALLOC
|
---|
756 | //ETIENNE
|
---|
757 | //gj.usdMem += needmem + headmem + gi_maxSize;
|
---|
758 | gj.usdMem = 0;
|
---|
759 | for (int k=0;k<numAllocatedChunks;k++)
|
---|
760 | gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE;
|
---|
761 | //END ETIENNE
|
---|
762 | #endif
|
---|
763 | */
|
---|
764 |
|
---|
765 | return i;
|
---|
766 |
|
---|
767 | } /*-----------------------------------------------------------------*/
|
---|
768 |
|
---|
769 | void initEvent(int i)
|
---|
770 | {
|
---|
771 | mBuffer[i].fEvent = (void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM);
|
---|
772 |
|
---|
773 | //flag all pixels as unused
|
---|
774 | for (int k = 0; k < NPIX; k++)
|
---|
775 | mBuffer[i].fEvent->StartPix[k] = -1;
|
---|
776 |
|
---|
777 | //flag all TMark as unused
|
---|
778 | for (int k = 0; k < NTMARK; k++)
|
---|
779 | mBuffer[i].fEvent->StartTM[k] = -1;
|
---|
780 |
|
---|
781 | mBuffer[i].fEvent->NumBoards = 0;
|
---|
782 | mBuffer[i].fEvent->PCTime = mBuffer[i].pcTime[0];
|
---|
783 | mBuffer[i].fEvent->PCUsec = mBuffer[i].pcTime[1];
|
---|
784 | }
|
---|
785 |
|
---|
786 |
|
---|
787 | int
|
---|
788 | mBufFree (int i)
|
---|
789 | {
|
---|
790 | //delete entry [i] from mBuffer:
|
---|
791 | //(and make sure multiple calls do no harm ....)
|
---|
792 |
|
---|
793 | // int headmem = 0;
|
---|
794 | // size_t freemem = 0;
|
---|
795 |
|
---|
796 | #ifdef ETIENNE_MALLOC
|
---|
797 | int evid;
|
---|
798 | evid = mBuffer[i].evNum;
|
---|
799 | ETI_Free(evid, i);
|
---|
800 | #endif
|
---|
801 |
|
---|
802 | // freemem = mBuffer[i].evtLen;
|
---|
803 | //ETIENNE
|
---|
804 | #ifdef THOMAS_MALLOC
|
---|
805 | TGB_free(mBuffer[i].FADhead);
|
---|
806 | #endif
|
---|
807 |
|
---|
808 | #ifdef STANDARD_MALLOC
|
---|
809 | free (mBuffer[i].FADhead);
|
---|
810 | #endif
|
---|
811 |
|
---|
812 | // free (mBuffer[i].fEvent);
|
---|
813 | mBuffer[i].fEvent = NULL;
|
---|
814 |
|
---|
815 | // free (mBuffer[i].FADhead);
|
---|
816 | mBuffer[i].FADhead = NULL;
|
---|
817 |
|
---|
818 | // free (mBuffer[i].buffer);
|
---|
819 | mBuffer[i].buffer = NULL;
|
---|
820 | //END ETIENNE
|
---|
821 | // headmem = NBOARDS * sizeof (PEVNT_HEADER);
|
---|
822 | mBuffer[i].evNum = mBuffer[i].nRoi = -1;
|
---|
823 | mBuffer[i].runNum = 0;
|
---|
824 |
|
---|
825 | #ifdef ETIENNE_MALLOC
|
---|
826 | //ETIENNE
|
---|
827 | // gj.usdMem = gj.usdMem - freemem - headmem - gi_maxSize;
|
---|
828 | gj.usdMem = 0;
|
---|
829 | for (int k=0;k<numAllocatedChunks;k++)
|
---|
830 | {
|
---|
831 | gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE;
|
---|
832 | }
|
---|
833 | //END ETIENNE
|
---|
834 | #endif
|
---|
835 |
|
---|
836 | #ifdef THOMAS_MALLOC
|
---|
837 | gj.usdMem = tgb_inuse;
|
---|
838 | #endif
|
---|
839 |
|
---|
840 | gj.bufTot--;
|
---|
841 |
|
---|
842 | /*if (gi_memStat < 0) {
|
---|
843 | if (gj.usdMem <= 0.75 * gj.maxMem)
|
---|
844 | gi_memStat = +1;
|
---|
845 | }*/
|
---|
846 |
|
---|
847 | return 0;
|
---|
848 |
|
---|
849 | } /*-----------------------------------------------------------------*/
|
---|
850 |
|
---|
851 | /*
|
---|
852 | void
|
---|
853 | resetEvtStat ()
|
---|
854 | {
|
---|
855 | for (int i = 0; i < MAX_SOCK; i++)
|
---|
856 | gi.numRead[i] = 0;
|
---|
857 |
|
---|
858 | for (int i = 0; i < NBOARDS; i++) {
|
---|
859 | gi.gotByte[i] = 0;
|
---|
860 | gi.gotErr[i] = 0;
|
---|
861 |
|
---|
862 | }
|
---|
863 |
|
---|
864 | gi.evtGet = 0; //#new Start of Events read
|
---|
865 | gi.evtTot = 0; //#complete Events read
|
---|
866 | gi.evtErr = 0; //#Events with Errors
|
---|
867 | gi.evtSkp = 0; //#Events incomplete (timeout)
|
---|
868 |
|
---|
869 | gi.procTot = 0; //#Events processed
|
---|
870 | gi.procErr = 0; //#Events showed problem in processing
|
---|
871 | gi.procTrg = 0; //#Events accepted by SW trigger
|
---|
872 | gi.procSkp = 0; //#Events rejected by SW trigger
|
---|
873 |
|
---|
874 | gi.feedTot = 0; //#Events used for feedBack system
|
---|
875 | gi.feedErr = 0; //#Events rejected by feedBack
|
---|
876 |
|
---|
877 | gi.wrtTot = 0; //#Events written to disk
|
---|
878 | gi.wrtErr = 0; //#Events with write-error
|
---|
879 |
|
---|
880 | gi.runOpen = 0; //#Runs opened
|
---|
881 | gi.runClose = 0; //#Runs closed
|
---|
882 | gi.runErr = 0; //#Runs with open/close errors
|
---|
883 |
|
---|
884 | return;
|
---|
885 | }*/ /*-----------------------------------------------------------------*/
|
---|
886 |
|
---|
887 | void reportIncomplete(int k0)
|
---|
888 | {
|
---|
889 | const int id = evtCtrl.evtBuf[k0];
|
---|
890 |
|
---|
891 | factPrintf(kWarn, 601, "%5d skip incomplete evt %8d %8d %2d",
|
---|
892 | mBuffer[id].evNum, evtCtrl.evtBuf[k0], k0, evtCtrl.evtStat[k0]);
|
---|
893 |
|
---|
894 | uint64_t report = 0;
|
---|
895 |
|
---|
896 | char str[1000];
|
---|
897 |
|
---|
898 | int ik=0;
|
---|
899 | for (int ib=0; ib<NBOARDS; ib++)
|
---|
900 | {
|
---|
901 | if (ib%10==0)
|
---|
902 | str[ik++] = '|';
|
---|
903 |
|
---|
904 | const int jb = mBuffer[id].board[ib];
|
---|
905 | if (jb>=0) // data received from that board
|
---|
906 | {
|
---|
907 | str[ik++] = '0'+(jb%10);
|
---|
908 | continue;
|
---|
909 | }
|
---|
910 |
|
---|
911 | // FIXME: Is that really 'b' or should that be 'ib' ?
|
---|
912 | if (gi_NumConnect[ib]<=0) // board not connected
|
---|
913 | {
|
---|
914 | str[ik++] = 'x';
|
---|
915 | continue;
|
---|
916 | }
|
---|
917 |
|
---|
918 | // data from this board lost
|
---|
919 | str[ik++] = '.';
|
---|
920 | report |= ((uint64_t)1)<<ib;
|
---|
921 | }
|
---|
922 |
|
---|
923 | str[ik++] = '|';
|
---|
924 | str[ik] = 0;
|
---|
925 |
|
---|
926 | factOut(kWarn, 601, str);
|
---|
927 |
|
---|
928 | factReportIncomplete(report);
|
---|
929 | }
|
---|
930 |
|
---|
931 | int checkRoiConsistency(int i, int roi[])
|
---|
932 | {
|
---|
933 | int xjr = -1;
|
---|
934 | int xkr = -1;
|
---|
935 |
|
---|
936 | //points to the very first roi
|
---|
937 | int roiPtr = sizeof(PEVNT_HEADER)/2 + 2;
|
---|
938 |
|
---|
939 | roi[0] = ntohs(rd[i].rBuf->S[roiPtr]);
|
---|
940 |
|
---|
941 | for (int jr = 0; jr < 9; jr++)
|
---|
942 | {
|
---|
943 | roi[jr] = ntohs(rd[i].rBuf->S[roiPtr]);
|
---|
944 |
|
---|
945 | if (roi[jr]<0 || roi[jr]>1024)
|
---|
946 | {
|
---|
947 | factPrintf(kError, 999, "Illegal roi in channel %d (allowed: 0<=roi<=1024)", jr, roi[jr]);
|
---|
948 | return 0;
|
---|
949 | }
|
---|
950 |
|
---|
951 | // Check that the roi of pixels jr are compatible with the one of pixel 0
|
---|
952 | if (jr!=8 && roi[jr]!=roi[0])
|
---|
953 | {
|
---|
954 | xjr = jr;
|
---|
955 | break;
|
---|
956 | }
|
---|
957 |
|
---|
958 | // Check that the roi of all other DRS chips on boards are compatible
|
---|
959 | for (int kr = 1; kr < 4; kr++)
|
---|
960 | {
|
---|
961 | const int kroi = ntohs(rd[i].rBuf->S[roiPtr]);
|
---|
962 | if (kroi != roi[jr])
|
---|
963 | {
|
---|
964 | xjr = jr;
|
---|
965 | xkr = kr;
|
---|
966 | break;
|
---|
967 | }
|
---|
968 | roiPtr += kroi+4;
|
---|
969 | }
|
---|
970 | }
|
---|
971 |
|
---|
972 | if (xjr>=0)
|
---|
973 | {
|
---|
974 | if (xkr<0)
|
---|
975 | factPrintf(kFatal, 1, "Inconsistent Roi accross chips [DRS=%d], expected %d, got %d", xjr, roi[0], roi[xjr]);
|
---|
976 | else
|
---|
977 | factPrintf(kFatal, 1, "Inconsistent Roi accross channels [DRS=%d Ch=%d], expected %d, got %d", xjr, xkr, roi[xjr], ntohs(rd[i].rBuf->S[roiPtr]));
|
---|
978 |
|
---|
979 | return 0;
|
---|
980 | }
|
---|
981 |
|
---|
982 | //const int b = i / 7;
|
---|
983 |
|
---|
984 | /*
|
---|
985 | if (roi[0]<0 || roi[0] > 1024)
|
---|
986 | {
|
---|
987 | factPrintf(kError, 999, "Illegal roi in channel 0: %d (allowed: 0<=roi<=1024)", roi[0]);
|
---|
988 | gj.badRoiR++;
|
---|
989 | gj.badRoi[b]++;
|
---|
990 | return 0;
|
---|
991 | }
|
---|
992 | */
|
---|
993 | /*
|
---|
994 | for (int jr = 1; jr < 8; jr++)
|
---|
995 | {
|
---|
996 | if (roi[jr] != roi[0])
|
---|
997 | {
|
---|
998 | factPrintf(kError, 711, "Mismatch of roi (%d) in channel %d with roi (%d) in channel 0.", roi[jr], jr, roi[0]);
|
---|
999 | gj.badRoiB++;
|
---|
1000 | gj.badRoi[b]++;
|
---|
1001 | return 0;
|
---|
1002 | }
|
---|
1003 | }
|
---|
1004 | */
|
---|
1005 | if (roi[8] < roi[0])
|
---|
1006 | {
|
---|
1007 | factPrintf(kError, 712, "Mismatch of roi (%d) in channel 8. Should be larger or equal than the roi (%d) in channel 0.", roi[8], roi[0]);
|
---|
1008 | //gj.badRoiB++;
|
---|
1009 | //gj.badRoi[b]++;
|
---|
1010 | return 0;
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | return 1;
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | void swapEventHeaderBytes(int i)
|
---|
1017 | {
|
---|
1018 | //End of the header. to channels now
|
---|
1019 | int eStart = 36;
|
---|
1020 | for (int ePatchesCount = 0; ePatchesCount<4*9;ePatchesCount++)
|
---|
1021 | {
|
---|
1022 | rd[i].rBuf->S[eStart+0] = ntohs(rd[i].rBuf->S[eStart+0]);//id
|
---|
1023 | rd[i].rBuf->S[eStart+1] = ntohs(rd[i].rBuf->S[eStart+1]);//start_cell
|
---|
1024 | rd[i].rBuf->S[eStart+2] = ntohs(rd[i].rBuf->S[eStart+2]);//roi
|
---|
1025 | rd[i].rBuf->S[eStart+3] = ntohs(rd[i].rBuf->S[eStart+3]);//filling
|
---|
1026 |
|
---|
1027 | eStart += 4+rd[i].rBuf->S[eStart+2];//skip the pixel data
|
---|
1028 | }
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | void copyData(int i, int evID, /*int roi,*/ int boardId)
|
---|
1032 | {
|
---|
1033 | swapEventHeaderBytes(i);
|
---|
1034 |
|
---|
1035 | memcpy(&mBuffer[evID].FADhead[boardId].start_package_flag,
|
---|
1036 | &rd[i].rBuf->S[0], sizeof(PEVNT_HEADER));
|
---|
1037 |
|
---|
1038 | int src = sizeof(PEVNT_HEADER) / 2;
|
---|
1039 |
|
---|
1040 | // consistency of ROIs have been checked already (is it all correct?)
|
---|
1041 | const int roi = rd[i].rBuf->S[src+2];
|
---|
1042 |
|
---|
1043 | // different sort in FAD board.....
|
---|
1044 | for (int px = 0; px < 9; px++)
|
---|
1045 | {
|
---|
1046 | for (int drs = 0; drs < 4; drs++)
|
---|
1047 | {
|
---|
1048 | // pixH = rd[i].rBuf->S[src++]; // ID
|
---|
1049 | src++;
|
---|
1050 |
|
---|
1051 | const int pixC = rd[i].rBuf->S[src++]; // start-cell
|
---|
1052 | const int pixR = rd[i].rBuf->S[src++]; // roi
|
---|
1053 | //here we should check if pixH is correct ....
|
---|
1054 |
|
---|
1055 | const int pixS = boardId * 36 + drs * 9 + px;
|
---|
1056 | src++;
|
---|
1057 |
|
---|
1058 | mBuffer[evID].fEvent->StartPix[pixS] = pixC;
|
---|
1059 |
|
---|
1060 | const int dest1 = pixS * roi;
|
---|
1061 | memcpy(&mBuffer[evID].fEvent->Adc_Data[dest1],
|
---|
1062 | &rd[i].rBuf->S[src], roi * 2);
|
---|
1063 |
|
---|
1064 | src += pixR;
|
---|
1065 |
|
---|
1066 | if (px == 8)
|
---|
1067 | {
|
---|
1068 | const int tmS = boardId * 4 + drs;
|
---|
1069 |
|
---|
1070 | //and we have additional TM info
|
---|
1071 | if (pixR > roi)
|
---|
1072 | {
|
---|
1073 | const int dest2 = tmS * roi + NPIX * roi;
|
---|
1074 |
|
---|
1075 | const int srcT = src - roi;
|
---|
1076 | mBuffer[evID].fEvent->StartTM[tmS] = (pixC + pixR - roi) % 1024;
|
---|
1077 |
|
---|
1078 | memcpy(&mBuffer[evID].fEvent->Adc_Data[dest2],
|
---|
1079 | &rd[i].rBuf->S[srcT], roi * 2);
|
---|
1080 | }
|
---|
1081 | else
|
---|
1082 | {
|
---|
1083 | mBuffer[evID].fEvent->StartTM[tmS] = -1;
|
---|
1084 |
|
---|
1085 | //ETIENNE because the TM channels are always processed during drs calib,
|
---|
1086 | //set them to zero if they are not present
|
---|
1087 | //I suspect that it may be more efficient to set all the allocated mem to
|
---|
1088 | //zero when allocating it
|
---|
1089 | // dest = tmS*roi[0] + NPIX*roi[0];
|
---|
1090 | // bzero(&mBuffer[evID].fEvent->Adc_Data[dest],roi[0]*2);
|
---|
1091 | }
|
---|
1092 | }
|
---|
1093 | }
|
---|
1094 | }
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 |
|
---|
1098 | void
|
---|
1099 | initReadFAD ()
|
---|
1100 | {
|
---|
1101 | return;
|
---|
1102 | } /*-----------------------------------------------------------------*/
|
---|
1103 |
|
---|
1104 | //struct rnd
|
---|
1105 | //{
|
---|
1106 | // int val;
|
---|
1107 | // int idx;
|
---|
1108 | //};
|
---|
1109 | //
|
---|
1110 | //struct rnd random_arr[MAX_SOCK];
|
---|
1111 | //
|
---|
1112 | //int compare(const void *f1, const void *f2)
|
---|
1113 | //{
|
---|
1114 | // struct rnd *r1 = (struct rnd*)f1;
|
---|
1115 | // struct rnd *r2 = (struct rnd*)f2;
|
---|
1116 | // return r1->val - r2->val;
|
---|
1117 | //}
|
---|
1118 |
|
---|
1119 |
|
---|
1120 | void *readFAD (void *ptr)
|
---|
1121 | {
|
---|
1122 | /* *** main loop reading FAD data and sorting them to complete events */
|
---|
1123 |
|
---|
1124 | factPrintf(kInfo, -1, "Start initializing (readFAD)");
|
---|
1125 |
|
---|
1126 | // int cpu = 7; //read thread
|
---|
1127 | // cpu_set_t mask;
|
---|
1128 |
|
---|
1129 | /* CPU_ZERO initializes all the bits in the mask to zero. */
|
---|
1130 | // CPU_ZERO (&mask);
|
---|
1131 | /* CPU_SET sets only the bit corresponding to cpu. */
|
---|
1132 | // cpu = 7;
|
---|
1133 | // CPU_SET (cpu, &mask);
|
---|
1134 |
|
---|
1135 | /* sched_setaffinity returns 0 in success */
|
---|
1136 | // if (sched_setaffinity (0, sizeof (mask), &mask) == -1) {
|
---|
1137 | // snprintf (str, MXSTR, "W ---> can not create affinity to %d", cpu);
|
---|
1138 | // factOut (kWarn, -1, str);
|
---|
1139 | // }
|
---|
1140 |
|
---|
1141 |
|
---|
1142 | const int minLen = sizeof(PEVNT_HEADER); //min #bytes needed to check header: full header for debug
|
---|
1143 |
|
---|
1144 | start.S = 0xFB01;
|
---|
1145 | stop.S = 0x04FE;
|
---|
1146 |
|
---|
1147 | /* initialize run control logics */
|
---|
1148 | for (int i = 0; i < MAX_RUN; i++) {
|
---|
1149 | runCtrl[i].runId = 0;
|
---|
1150 | runCtrl[i].fileId = -2;
|
---|
1151 | runCtrl[i].procId = -2;
|
---|
1152 | }
|
---|
1153 | gi_resetS = gi_resetR = 9;
|
---|
1154 |
|
---|
1155 | int sockDef[NBOARDS]; //internal state of sockets
|
---|
1156 | memset(sockDef, 0, NBOARDS*sizeof(int));
|
---|
1157 |
|
---|
1158 | START:
|
---|
1159 | evtCtrl.frstPtr = 0;
|
---|
1160 | evtCtrl.lastPtr = 0;
|
---|
1161 |
|
---|
1162 | //time in seconds
|
---|
1163 | uint gi_SecTime = time(NULL);;
|
---|
1164 |
|
---|
1165 | const int cntsock = 8 - NUMSOCK ;
|
---|
1166 |
|
---|
1167 | if (gi_resetS > 0) {
|
---|
1168 | //make sure all sockets are preallocated as 'not exist'
|
---|
1169 | for (int i = 0; i < MAX_SOCK; i++) {
|
---|
1170 | rd[i].socket = -1;
|
---|
1171 | rd[i].sockStat = 99;
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | for (int k = 0; k < NBOARDS; k++) {
|
---|
1175 | gi_NumConnect[k] = 0;
|
---|
1176 | //gi.numConn[k] = 0;
|
---|
1177 | gj.numConn[k] = 0;
|
---|
1178 | //gj.errConn[k] = 0;
|
---|
1179 | gj.rateBytes[k] = 0;
|
---|
1180 | gj.totBytes[k] = 0;
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 |
|
---|
1186 | if (gi_resetR > 0) {
|
---|
1187 | //resetEvtStat ();
|
---|
1188 | gj.bufTot = gj.maxEvt = gj.xxxEvt = 0;
|
---|
1189 | gj.usdMem = gj.maxMem = gj.xxxMem = 0;
|
---|
1190 | #ifdef THOMAS_MALLOC
|
---|
1191 | gj.totMem = tgb_memory;
|
---|
1192 | #else
|
---|
1193 | gj.totMem = g_maxMem;
|
---|
1194 | #endif
|
---|
1195 | gj.bufNew = gj.bufEvt = 0;
|
---|
1196 | //gj.badRoiE = gj.badRoiR = gj.badRoiB = 0;
|
---|
1197 | gj.evtSkip = gj.evtWrite = gj.evtErr = 0;
|
---|
1198 |
|
---|
1199 | //for (int b = 0; b < NBOARDS; b++)
|
---|
1200 | // gj.badRoi[b] = 0;
|
---|
1201 |
|
---|
1202 | mBufInit (); //initialize buffers
|
---|
1203 |
|
---|
1204 | factPrintf(kInfo, -1, "End initializing (readFAD)");
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 |
|
---|
1208 | gi_reset = gi_resetR = gi_resetS = gi_resetW = 0;
|
---|
1209 |
|
---|
1210 | //loop until global variable g_runStat claims stop
|
---|
1211 | while (g_runStat >= 0 && g_reset == 0)
|
---|
1212 | {
|
---|
1213 | gi_runStat = g_runStat;
|
---|
1214 | gj.readStat = g_runStat;
|
---|
1215 |
|
---|
1216 | struct timeval tv;
|
---|
1217 | gettimeofday (&tv, NULL);
|
---|
1218 | g_actTime = tv.tv_sec;
|
---|
1219 | g_actUsec = tv.tv_usec;
|
---|
1220 |
|
---|
1221 |
|
---|
1222 | for (int b = 0; b < NBOARDS; b++)
|
---|
1223 | {
|
---|
1224 | // Nothing has changed
|
---|
1225 | if (g_port[b].sockDef == sockDef[b])
|
---|
1226 | continue;
|
---|
1227 |
|
---|
1228 | gi_NumConnect[b] = 0; //must close all connections
|
---|
1229 | //gi.numConn[b] = 0;
|
---|
1230 | gj.numConn[b] = 0;
|
---|
1231 |
|
---|
1232 | // s0 = 0: sockets to be defined and opened
|
---|
1233 | // s0 = -1: sockets to be destroyed
|
---|
1234 | // s0 = +1: sockets to be closed and reopened
|
---|
1235 |
|
---|
1236 | int s0 = 0;
|
---|
1237 | if (sockDef[b] != 0)
|
---|
1238 | s0 = g_port[b].sockDef==0 ? -1 : +1;
|
---|
1239 |
|
---|
1240 | const int p0 = s0==0 ? ntohs (g_port[b].sockAddr.sin_port) : 0;
|
---|
1241 |
|
---|
1242 | int k = b * 7;
|
---|
1243 | for (int p = p0 + 1; p < p0 + 8; p++, k++)
|
---|
1244 | GenSock (s0, k, p, &g_port[b].sockAddr, &rd[k]); //generate address and socket
|
---|
1245 |
|
---|
1246 | sockDef[b] = g_port[b].sockDef;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | // count the number of active boards
|
---|
1250 | int actBoards = 0;
|
---|
1251 | for (int b = 0; b < NBOARDS; b++)
|
---|
1252 | if (sockDef[b] > 0)
|
---|
1253 | actBoards++;
|
---|
1254 |
|
---|
1255 | //count number of succesfull actions
|
---|
1256 | // int numok = 0;
|
---|
1257 |
|
---|
1258 | /*
|
---|
1259 | for (i=0; i<MAX_SOCK/7; i++)
|
---|
1260 | {
|
---|
1261 | random_arr[i].val = rand();
|
---|
1262 | random_arr[i].idx = i;
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | qsort(random_arr, MAX_SOCK/7, sizeof(struct rnd), compare);
|
---|
1266 |
|
---|
1267 | for (int iii = 0; iii < MAX_SOCK/7; iii++) { //check all sockets if something to read
|
---|
1268 |
|
---|
1269 | b = random_arr[iii].idx;
|
---|
1270 | i = b*7;
|
---|
1271 | p = 0;
|
---|
1272 | */
|
---|
1273 |
|
---|
1274 | //check all sockets if something to read
|
---|
1275 | for (int i = 0; i < MAX_SOCK; i+=7)
|
---|
1276 | {
|
---|
1277 | // Do not try to connect this socket
|
---|
1278 | if (rd[i].sockStat > 0)
|
---|
1279 | continue;
|
---|
1280 |
|
---|
1281 | const int board = i / 7 ;
|
---|
1282 | //const int p = i % 7 ;
|
---|
1283 |
|
---|
1284 | if (rd[i].sockStat == -1)
|
---|
1285 | {
|
---|
1286 | //try to connect if not yet done
|
---|
1287 | rd[i].sockStat = connect (rd[i].socket,
|
---|
1288 | (struct sockaddr *) &rd[i].SockAddr,
|
---|
1289 | sizeof (rd[i].SockAddr));
|
---|
1290 | // Failed
|
---|
1291 | if (rd[i].sockStat == -1)
|
---|
1292 | {
|
---|
1293 | rd[i].errCnt++;
|
---|
1294 | usleep(25000);
|
---|
1295 | continue;
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 | // Success (rd[i].sockStat == 0)
|
---|
1299 |
|
---|
1300 | if (sockDef[board] > 0)
|
---|
1301 | {
|
---|
1302 | rd[i].bufTyp = 0; // expect a header
|
---|
1303 | rd[i].bufLen = sizeof(PEVNT_HEADER); // max size to read at begining
|
---|
1304 | }
|
---|
1305 | else
|
---|
1306 | {
|
---|
1307 | rd[i].bufTyp = -1; // full data to be skipped
|
---|
1308 | rd[i].bufLen = MAX_LEN; // huge for skipping
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | rd[i].bufPos = 0; // no byte read so far
|
---|
1312 | rd[i].skip = 0; // start empty
|
---|
1313 |
|
---|
1314 | gi_NumConnect[board] += cntsock;
|
---|
1315 |
|
---|
1316 | //gi.numConn[b]++;
|
---|
1317 | gj.numConn[board]++;
|
---|
1318 |
|
---|
1319 | factPrintf(kInfo, -1, "New connection %d (number of connections: %d)", board, gj.numConn[board]);
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | // Do not read from this socket
|
---|
1323 | if (rd[i].bufLen<0)
|
---|
1324 | continue;
|
---|
1325 |
|
---|
1326 | //numok++;
|
---|
1327 |
|
---|
1328 | if (rd[i].bufLen>0)
|
---|
1329 | {
|
---|
1330 | const int32_t jrd =
|
---|
1331 | recv(rd[i].socket, &rd[i].rBuf->B[rd[i].bufPos],
|
---|
1332 | rd[i].bufLen, MSG_DONTWAIT);
|
---|
1333 |
|
---|
1334 | // recv failed
|
---|
1335 | if (jrd<0)
|
---|
1336 | {
|
---|
1337 | // There was just nothing waiting
|
---|
1338 | if (errno==EWOULDBLOCK || errno==EAGAIN)
|
---|
1339 | {
|
---|
1340 | //numok--;
|
---|
1341 | continue;
|
---|
1342 | }
|
---|
1343 |
|
---|
1344 | factPrintf(kError, 442, "Reading from socket %d failed: %m (recv,rc=%d)", i, errno);
|
---|
1345 | //gi.gotErr[b]++;
|
---|
1346 | continue;
|
---|
1347 | }
|
---|
1348 |
|
---|
1349 | // connection was closed ...
|
---|
1350 | if (jrd==0)
|
---|
1351 | {
|
---|
1352 | factPrintf(kInfo, 441, "Socket %d closed by FAD", i);
|
---|
1353 |
|
---|
1354 | const int s0 = sockDef[board] > 0 ? +1 : -1;
|
---|
1355 | GenSock(s0, i, 0, NULL, &rd[i]);
|
---|
1356 |
|
---|
1357 | //gi.gotErr[b]++;
|
---|
1358 |
|
---|
1359 | gi_NumConnect[board]-= cntsock ;
|
---|
1360 | //gi.numConn[b]--;
|
---|
1361 | gj.numConn[board]--;
|
---|
1362 |
|
---|
1363 | continue;
|
---|
1364 | }
|
---|
1365 | // Success (jrd > 0)
|
---|
1366 |
|
---|
1367 | gj.rateBytes[board] += jrd;
|
---|
1368 |
|
---|
1369 | // are we skipping this board ...
|
---|
1370 | if (rd[i].bufTyp < 0)
|
---|
1371 | continue;
|
---|
1372 |
|
---|
1373 | rd[i].bufPos += jrd; //==> prepare for continuation
|
---|
1374 | rd[i].bufLen -= jrd;
|
---|
1375 |
|
---|
1376 | #ifdef EVTDEBUG
|
---|
1377 | debugRead(i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, rd[i].bufTyp, tv.tv_sec, tv.tv_usec); // i=socket; jrd=#bytes; ievt=eventid; 1=finished event
|
---|
1378 | #endif
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | // are we reading data?
|
---|
1382 | if (rd[i].bufTyp > 0)
|
---|
1383 | {
|
---|
1384 | // not yet all read
|
---|
1385 | if (rd[i].bufLen > 0)
|
---|
1386 | continue;
|
---|
1387 |
|
---|
1388 | if (rd[i].rBuf->B[rd[i].fadLen - 1] != stop.B[0] ||
|
---|
1389 | rd[i].rBuf->B[rd[i].fadLen - 2] != stop.B[1])
|
---|
1390 | {
|
---|
1391 | //gi.evtErr++;
|
---|
1392 | factPrintf(kError, 301, "End-of-event flag wrong on socket %3d for event %4d (len=%5d), expected %3d %3d, got %3d %3d",
|
---|
1393 | i, rd[i].evtID, rd[i].fadLen, stop.B[0], stop.B[1],
|
---|
1394 | rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]);
|
---|
1395 |
|
---|
1396 | // ready to read next header
|
---|
1397 | rd[i].bufTyp = 0;
|
---|
1398 | rd[i].bufLen = sizeof(PEVNT_HEADER);
|
---|
1399 | rd[i].bufPos = 0;
|
---|
1400 |
|
---|
1401 | continue;
|
---|
1402 | }
|
---|
1403 |
|
---|
1404 | // int actid;
|
---|
1405 | // if (g_useFTM > 0)
|
---|
1406 | // actid = rd[i].evtID;
|
---|
1407 | // else
|
---|
1408 | // actid = rd[i].ftmID;
|
---|
1409 |
|
---|
1410 | //get index into mBuffer for this event (create if needed)
|
---|
1411 | const int evID = mBufEvt(i);
|
---|
1412 |
|
---|
1413 | // no free entry in mBuffer, retry later
|
---|
1414 | if (evID == -1)
|
---|
1415 | continue;
|
---|
1416 |
|
---|
1417 | // We have a valid entry, but no memory has yet been allocated
|
---|
1418 | if (evID >= 0 && mBuffer[evID].FADhead == NULL)
|
---|
1419 | {
|
---|
1420 | // Try to get memory from the big buffer
|
---|
1421 | mBuffer[evID].FADhead = TGB_Malloc();
|
---|
1422 | if (mBuffer[evID].FADhead == NULL)
|
---|
1423 | {
|
---|
1424 | // If this works properly, this is a hack which can be removed, or
|
---|
1425 | // replaced by a signal or dim message
|
---|
1426 | if (rd[i].bufTyp==2)
|
---|
1427 | factPrintf(kError, 882, "malloc failed for event %d", evID);
|
---|
1428 | rd[i].bufTyp = 2;
|
---|
1429 | continue;
|
---|
1430 | }
|
---|
1431 |
|
---|
1432 | // Initialice mBuffer[evID]->fEvent
|
---|
1433 | initEvent(evID);
|
---|
1434 |
|
---|
1435 | // Some statistics
|
---|
1436 | gj.usdMem = tgb_inuse;
|
---|
1437 |
|
---|
1438 | if (gj.usdMem > gj.maxMem)
|
---|
1439 | gj.maxMem = gj.usdMem;
|
---|
1440 |
|
---|
1441 | gj.rateNew++;
|
---|
1442 | gj.bufTot++;
|
---|
1443 | if (gj.bufTot > gj.maxEvt)
|
---|
1444 | gj.maxEvt = gj.bufTot;
|
---|
1445 |
|
---|
1446 | //register event in 'active list (reading)'
|
---|
1447 | evtIdx[evID] = evtCtrl.lastPtr;
|
---|
1448 |
|
---|
1449 | evtCtrl.evtBuf[evtCtrl.lastPtr] = evID;
|
---|
1450 | evtCtrl.evtStat[evtCtrl.lastPtr] = 0;
|
---|
1451 | evtCtrl.pcTime[evtCtrl.lastPtr] = g_actTime;
|
---|
1452 |
|
---|
1453 | evtCtrl.lastPtr++;
|
---|
1454 | evtCtrl.lastPtr %= MAX_EVT * MAX_RUN;
|
---|
1455 | }
|
---|
1456 |
|
---|
1457 | // ready to read next header
|
---|
1458 | rd[i].bufTyp = 0;
|
---|
1459 | rd[i].bufLen = sizeof(PEVNT_HEADER);
|
---|
1460 | rd[i].bufPos = 0;
|
---|
1461 |
|
---|
1462 | // Fatal error occured. Event cannot be processed. Skip it. Start reading next header.
|
---|
1463 | if (evID < -1000)
|
---|
1464 | continue;
|
---|
1465 |
|
---|
1466 | //we have a valid entry in mBuffer[]; fill it
|
---|
1467 | const int fadBoard = rd[i].rBuf->S[12];
|
---|
1468 | const int fadCrate = fadBoard>>8;
|
---|
1469 |
|
---|
1470 | if (board != (fadCrate * 10 + (fadBoard&0xff)))
|
---|
1471 | {
|
---|
1472 | factPrintf(kWarn, 301, "Board ID mismatch. Expected %d, got %d (C=%d, B=%d)",
|
---|
1473 | board, fadBoard, fadCrate, fadBoard&0xff);
|
---|
1474 | }
|
---|
1475 |
|
---|
1476 | if (mBuffer[evID].board[board] != -1)
|
---|
1477 | {
|
---|
1478 | factPrintf(kWarn, 501, "Got event %5d from board %3d (i=%3d, len=%5d) twice: Starts with %3d %3d - ends with %3d %3d",
|
---|
1479 | evID, board, i, rd[i].fadLen,
|
---|
1480 | rd[i].rBuf->B[0], rd[i].rBuf->B[1],
|
---|
1481 | rd[i].rBuf->B[rd[i].fadLen - 2],
|
---|
1482 | rd[i].rBuf->B[rd[i].fadLen - 1]);
|
---|
1483 | continue; // Continue reading next header
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | // Copy data from rd[i] to mBuffer[evID]
|
---|
1487 | copyData(i, evID, board);
|
---|
1488 |
|
---|
1489 | // now we have stored a new board contents into Event structure
|
---|
1490 |
|
---|
1491 | const int iDx = evtIdx[evID]; //index into evtCtrl
|
---|
1492 |
|
---|
1493 | evtCtrl.evtStat[iDx]++;
|
---|
1494 | evtCtrl.pcTime[iDx] = g_actTime;
|
---|
1495 |
|
---|
1496 | mBuffer[evID].fEvent->NumBoards++;
|
---|
1497 | mBuffer[evID].board[board] = board;
|
---|
1498 | mBuffer[evID].nBoard++;
|
---|
1499 |
|
---|
1500 | // have we already reported first (partial) event of this run ???
|
---|
1501 | if (mBuffer[evID].nBoard>0 && mBuffer[evID].runNum != actrun)
|
---|
1502 | {
|
---|
1503 | actrun = mBuffer[evID].runNum;
|
---|
1504 |
|
---|
1505 | for (int ir = 0; ir < MAX_RUN; ir++)
|
---|
1506 | {
|
---|
1507 | if (runCtrl[ir].runId == actrun)
|
---|
1508 | {
|
---|
1509 | if (++runCtrl[ir].lastEvt == 0)
|
---|
1510 | {
|
---|
1511 | gotNewRun (actrun, NULL);
|
---|
1512 | factPrintf(kInfo, 1, "gotNewRun called for run %d, event %d",
|
---|
1513 | mBuffer[evID].runNum, mBuffer[evID].evNum);
|
---|
1514 | break;
|
---|
1515 | }
|
---|
1516 | }
|
---|
1517 | }
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | //complete event read ---> flag for next processing
|
---|
1521 | if (mBuffer[evID].nBoard >= actBoards)
|
---|
1522 | {
|
---|
1523 | evtCtrl.evtStat[iDx] = 99;
|
---|
1524 | //gi.evtTot++;
|
---|
1525 | }
|
---|
1526 |
|
---|
1527 | // Continue reading next header
|
---|
1528 | }
|
---|
1529 | else
|
---|
1530 | {
|
---|
1531 | //we are reading event header
|
---|
1532 |
|
---|
1533 | //not yet sufficient data to take action
|
---|
1534 | if (rd[i].bufPos < minLen)
|
---|
1535 | continue;
|
---|
1536 |
|
---|
1537 | //check if startflag correct; else shift block ....
|
---|
1538 | // FIXME: This is not enough... this combination of
|
---|
1539 | // bytes can be anywhere... at least the end bytes
|
---|
1540 | // must be checked somewhere, too.
|
---|
1541 | int k;
|
---|
1542 | for (k = 0; k < rd[i].bufPos - 1; k++)
|
---|
1543 | {
|
---|
1544 | if (rd[i].rBuf->B[k] == start.B[1] && rd[i].rBuf->B[k+1] == start.B[0])
|
---|
1545 | break;
|
---|
1546 | }
|
---|
1547 | rd[i].skip += k;
|
---|
1548 |
|
---|
1549 | //no start of header found
|
---|
1550 | if (k >= rd[i].bufPos - 1)
|
---|
1551 | {
|
---|
1552 | rd[i].bufPos = 0;
|
---|
1553 | rd[i].bufLen = sizeof(PEVNT_HEADER);
|
---|
1554 | continue;
|
---|
1555 | }
|
---|
1556 |
|
---|
1557 | if (k > 0)
|
---|
1558 | {
|
---|
1559 | rd[i].bufPos -= k;
|
---|
1560 | rd[i].bufLen += k;
|
---|
1561 | memmove (&rd[i].rBuf->B[0], &rd[i].rBuf->B[k],
|
---|
1562 | rd[i].bufPos);
|
---|
1563 | }
|
---|
1564 |
|
---|
1565 | if (rd[i].bufPos < minLen)
|
---|
1566 | continue;
|
---|
1567 |
|
---|
1568 | if (rd[i].skip > 0)
|
---|
1569 | {
|
---|
1570 | factPrintf(kInfo, 666, "Skipped %d bytes on port %d", rd[i].skip, i);
|
---|
1571 | rd[i].skip = 0;
|
---|
1572 | }
|
---|
1573 |
|
---|
1574 | // TGB: This needs much more checks than just the first two bytes!
|
---|
1575 |
|
---|
1576 | // Swap everything except start_package_flag.
|
---|
1577 | // It is to difficult to find out where it is used how,
|
---|
1578 | // but it doesn't really matter because it is not really
|
---|
1579 | // used anywehere else
|
---|
1580 | // rd[i].rBuf->S[1] = ntohs(rd[i].rBuf->S[1]); // package_length
|
---|
1581 | rd[i].rBuf->S[2] = ntohs(rd[i].rBuf->S[2]); // version_no
|
---|
1582 | rd[i].rBuf->S[3] = ntohs(rd[i].rBuf->S[3]); // PLLLCK
|
---|
1583 | rd[i].rBuf->S[4] = ntohs(rd[i].rBuf->S[4]); // trigger_crc
|
---|
1584 | rd[i].rBuf->S[5] = ntohs(rd[i].rBuf->S[5]); // trigger_type
|
---|
1585 |
|
---|
1586 | rd[i].rBuf->S[12] = ntohs(rd[i].rBuf->S[12]); // board id
|
---|
1587 | rd[i].rBuf->S[13] = ntohs(rd[i].rBuf->S[13]); // adc_clock_phase_shift
|
---|
1588 | rd[i].rBuf->S[14] = ntohs(rd[i].rBuf->S[14]); // number_of_triggers_to_generate
|
---|
1589 | rd[i].rBuf->S[15] = ntohs(rd[i].rBuf->S[15]); // trigger_generator_prescaler
|
---|
1590 |
|
---|
1591 | rd[i].rBuf->I[3] = ntohl(rd[i].rBuf->I[3]); // trigger_id
|
---|
1592 | rd[i].rBuf->I[4] = ntohl(rd[i].rBuf->I[4]); // fad_evt_counter
|
---|
1593 | rd[i].rBuf->I[5] = ntohl(rd[i].rBuf->I[5]); // REFCLK_frequency
|
---|
1594 |
|
---|
1595 | rd[i].rBuf->I[10] = ntohl(rd[i].rBuf->I[10]); // runnumber;
|
---|
1596 | rd[i].rBuf->I[11] = ntohl(rd[i].rBuf->I[11]); // time;
|
---|
1597 |
|
---|
1598 | for (int s=24;s<24+NTemp+NDAC;s++)
|
---|
1599 | rd[i].rBuf->S[s] = ntohs(rd[i].rBuf->S[s]); // drs_temperature / dac
|
---|
1600 |
|
---|
1601 | rd[i].fadLen = ntohs(rd[i].rBuf->S[1]) * 2;
|
---|
1602 | rd[i].fadVers = rd[i].rBuf->S[2];
|
---|
1603 | rd[i].ftmTyp = rd[i].rBuf->S[5];
|
---|
1604 | rd[i].ftmID = rd[i].rBuf->I[3]; //(FTMevt)
|
---|
1605 | rd[i].evtID = rd[i].rBuf->I[4]; //(FADevt)
|
---|
1606 | rd[i].runID = rd[i].rBuf->I[11];
|
---|
1607 | rd[i].bufTyp = 1; //ready to read full record
|
---|
1608 | rd[i].bufLen = rd[i].fadLen - rd[i].bufPos;
|
---|
1609 |
|
---|
1610 | if (rd[i].runID == 0)
|
---|
1611 | rd[i].runID = g_actTime;
|
---|
1612 |
|
---|
1613 | const int fadBoard = rd[i].rBuf->S[12];
|
---|
1614 | debugHead(i, fadBoard, rd[i].rBuf);
|
---|
1615 |
|
---|
1616 | // Continue reading data
|
---|
1617 |
|
---|
1618 | } // end if data or header
|
---|
1619 |
|
---|
1620 | } // end for loop over all sockets
|
---|
1621 |
|
---|
1622 | g_actTime = time (NULL);
|
---|
1623 | if (g_actTime <= gi_SecTime)
|
---|
1624 | {
|
---|
1625 | usleep(1);
|
---|
1626 | continue;
|
---|
1627 | }
|
---|
1628 |
|
---|
1629 | gi_SecTime = g_actTime;
|
---|
1630 |
|
---|
1631 | gj.bufNew = gj.bufEvt = 0;
|
---|
1632 |
|
---|
1633 | //loop over all active events and flag those older than read-timeout
|
---|
1634 | //delete those that are written to disk ....
|
---|
1635 | for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)
|
---|
1636 | {
|
---|
1637 | if (k0==evtCtrl.frstPtr && evtCtrl.evtStat[k0]<0)
|
---|
1638 | {
|
---|
1639 | evtCtrl.frstPtr++;
|
---|
1640 | evtCtrl.frstPtr %= MAX_EVT * MAX_RUN;
|
---|
1641 |
|
---|
1642 | // Continue because evtCtrl.evtStat[k0] must be <0
|
---|
1643 | continue;
|
---|
1644 | }
|
---|
1645 |
|
---|
1646 | // Check the more likely case first: incomplete events
|
---|
1647 | if (evtCtrl.evtStat[k0]>0 && evtCtrl.evtStat[k0]<92)
|
---|
1648 | {
|
---|
1649 | gj.bufNew++; //incomplete event in Buffer
|
---|
1650 |
|
---|
1651 | // Event has not yet timed out or was reported already
|
---|
1652 | if (evtCtrl.evtStat[k0]>=90 || evtCtrl.pcTime[k0]>=g_actTime - 30)
|
---|
1653 | continue;
|
---|
1654 |
|
---|
1655 | reportIncomplete(k0);
|
---|
1656 |
|
---|
1657 | evtCtrl.evtStat[k0] = 91; //timeout for incomplete events
|
---|
1658 | //gi.evtSkp++;
|
---|
1659 | //gi.evtTot++;
|
---|
1660 | gj.evtSkip++;
|
---|
1661 |
|
---|
1662 | continue;
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | // complete event in Buffer
|
---|
1666 | if (evtCtrl.evtStat[k0] >= 95)
|
---|
1667 | gj.bufEvt++;
|
---|
1668 |
|
---|
1669 | // Check the less likely case: 'useless' or 'delete'
|
---|
1670 | if (evtCtrl.evtStat[k0]==0 || evtCtrl.evtStat[k0] >= 9000)
|
---|
1671 | {
|
---|
1672 | const int id = evtCtrl.evtBuf[k0];
|
---|
1673 | #ifdef EVTDEBUG
|
---|
1674 | factPrintf(kDebug, -1, "%5d free event buffer, nb=%3d", mBuffer[id].evNum, mBuffer[id].nBoard);
|
---|
1675 | #endif
|
---|
1676 | mBufFree (id); //event written--> free memory
|
---|
1677 | evtCtrl.evtStat[k0] = -1;
|
---|
1678 | gj.evtWrite++;
|
---|
1679 | gj.rateWrite++;
|
---|
1680 | }
|
---|
1681 |
|
---|
1682 | }
|
---|
1683 |
|
---|
1684 | gj.deltaT = 1000; //temporary, must be improved
|
---|
1685 |
|
---|
1686 | for (int ib = 0; ib < NBOARDS; ib++)
|
---|
1687 | gj.totBytes[ib] += gj.rateBytes[ib];
|
---|
1688 |
|
---|
1689 | #ifdef THOMAS_MALLOC
|
---|
1690 | gj.totMem = tgb_memory;
|
---|
1691 | #else
|
---|
1692 | gj.totMem = g_maxMem;
|
---|
1693 | #endif
|
---|
1694 |
|
---|
1695 | if (gj.maxMem > gj.xxxMem)
|
---|
1696 | gj.xxxMem = gj.maxMem;
|
---|
1697 | if (gj.maxEvt > gj.xxxEvt)
|
---|
1698 | gj.xxxEvt = gj.maxEvt;
|
---|
1699 |
|
---|
1700 | factStat (gj);
|
---|
1701 | factStatNew (gi);
|
---|
1702 | gj.rateNew = gj.rateWrite = 0;
|
---|
1703 | gj.maxMem = gj.usdMem;
|
---|
1704 | gj.maxEvt = gj.bufTot;
|
---|
1705 | for (int b = 0; b < NBOARDS; b++)
|
---|
1706 | gj.rateBytes[b] = 0;
|
---|
1707 |
|
---|
1708 | } // while (g_runStat >= 0 && g_reset == 0)
|
---|
1709 |
|
---|
1710 | factPrintf(kInfo, -1, "Stop reading ... RESET=%d", g_reset);
|
---|
1711 |
|
---|
1712 | if (g_reset > 0)
|
---|
1713 | {
|
---|
1714 | gi_reset = g_reset;
|
---|
1715 | gi_resetR = gi_reset % 10; //shall we stop reading ?
|
---|
1716 | gi_resetS = (gi_reset / 10) % 10; //shall we close sockets ?
|
---|
1717 | gi_resetW = (gi_reset / 100) % 10; //shall we close files ?
|
---|
1718 | gi_resetX = gi_reset / 1000; //shall we simply wait resetX seconds ?
|
---|
1719 | g_reset = 0;
|
---|
1720 | }
|
---|
1721 | else
|
---|
1722 | {
|
---|
1723 | gi_reset = 0;
|
---|
1724 | gi_resetR = g_runStat == -1 ? 1 : 7;
|
---|
1725 |
|
---|
1726 | gi_resetS = 7; //close all sockets
|
---|
1727 | gi_resetW = 7; //close all files
|
---|
1728 | gi_resetX = 0;
|
---|
1729 |
|
---|
1730 | //inform others we have to quit ....
|
---|
1731 | gi_runStat = -11; //inform all that no update to happen any more
|
---|
1732 | gj.readStat = -11; //inform all that no update to happen any more
|
---|
1733 | }
|
---|
1734 |
|
---|
1735 | if (gi_resetS > 0)
|
---|
1736 | {
|
---|
1737 | //must close all open sockets ...
|
---|
1738 | factPrintf(kInfo, -1, "Close all sockets...");
|
---|
1739 |
|
---|
1740 | for (int i = 0; i < MAX_SOCK; i++)
|
---|
1741 | {
|
---|
1742 | if (rd[i].sockStat != 0)
|
---|
1743 | continue;
|
---|
1744 |
|
---|
1745 | GenSock(-1, i, 0, NULL, &rd[i]); //close and destroy open socket
|
---|
1746 | if (i%7)
|
---|
1747 | continue;
|
---|
1748 |
|
---|
1749 | gi_NumConnect[i / 7]-= cntsock ;
|
---|
1750 | //gi.numConn[i / 7]--;
|
---|
1751 | gj.numConn[i / 7]--;
|
---|
1752 | sockDef[i / 7] = 0; //flag ro recreate the sockets ...
|
---|
1753 | rd[i / 7].sockStat = -1; //and try to open asap
|
---|
1754 | }
|
---|
1755 | }
|
---|
1756 |
|
---|
1757 |
|
---|
1758 | if (gi_resetR > 0)
|
---|
1759 | {
|
---|
1760 | //flag all events as 'read finished'
|
---|
1761 | for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)
|
---|
1762 | {
|
---|
1763 | if (evtCtrl.evtStat[k0] > 0 && evtCtrl.evtStat[k0] < 90)
|
---|
1764 | {
|
---|
1765 | evtCtrl.evtStat[k0] = 91;
|
---|
1766 | //gi.evtSkp++;
|
---|
1767 | //gi.evtTot++;
|
---|
1768 | }
|
---|
1769 | }
|
---|
1770 |
|
---|
1771 | //and clear all buffers (might have to wait until all others are done)
|
---|
1772 | // If minclear is 0, an event could be deleted while writing is still ongoing
|
---|
1773 | /*
|
---|
1774 | int minclear;
|
---|
1775 | if (gi_resetR == 1) {
|
---|
1776 | minclear = 900;
|
---|
1777 | factPrintf(kInfo, -1, "Drain all buffers ...");
|
---|
1778 | } else {
|
---|
1779 | minclear = 0;
|
---|
1780 | factPrintf(kInfo, -1, "Flush all buffers ...");
|
---|
1781 | }*/
|
---|
1782 | const int minclear = 900;
|
---|
1783 |
|
---|
1784 | int numclear = 1;
|
---|
1785 | while (numclear > 0)
|
---|
1786 | {
|
---|
1787 | numclear = 0;
|
---|
1788 |
|
---|
1789 | for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)
|
---|
1790 | {
|
---|
1791 | if (evtCtrl.evtStat[k0] > minclear)
|
---|
1792 | {
|
---|
1793 | const int id = evtCtrl.evtBuf[k0];
|
---|
1794 | #ifdef EVTDEBUG
|
---|
1795 | factPrintf(kDebug, -1, "ev %5d free event buffer, nb=%3d", mBuffer[id].evNum, mBuffer[id].nBoard);
|
---|
1796 | #endif
|
---|
1797 | mBufFree (id); //event written--> free memory
|
---|
1798 | evtCtrl.evtStat[k0] = -1;
|
---|
1799 | }
|
---|
1800 | else
|
---|
1801 | if (evtCtrl.evtStat[k0] > 0)
|
---|
1802 | numclear++; //writing is still ongoing...
|
---|
1803 |
|
---|
1804 | if (k0 == evtCtrl.frstPtr && evtCtrl.evtStat[k0] < 0)
|
---|
1805 | {
|
---|
1806 | evtCtrl.frstPtr++;
|
---|
1807 | evtCtrl.frstPtr %= MAX_EVT * MAX_RUN;
|
---|
1808 | }
|
---|
1809 | }
|
---|
1810 |
|
---|
1811 | usleep(1);
|
---|
1812 | }
|
---|
1813 | }
|
---|
1814 |
|
---|
1815 | if (gi_reset > 0)
|
---|
1816 | {
|
---|
1817 | if (gi_resetW > 0)
|
---|
1818 | CloseRunFile (0, 0, 0); //ask all Runs to be closed
|
---|
1819 |
|
---|
1820 | if (gi_resetX > 0)
|
---|
1821 | {
|
---|
1822 | struct timespec xwait;
|
---|
1823 | xwait.tv_sec = gi_resetX;
|
---|
1824 | xwait.tv_nsec = 0;
|
---|
1825 | nanosleep (&xwait, NULL);
|
---|
1826 | }
|
---|
1827 |
|
---|
1828 | factPrintf(kInfo, -1, "Continue read Process ...");
|
---|
1829 | gi_reset = 0;
|
---|
1830 | goto START;
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | factPrintf(kInfo, -1, "Exit read Process...");
|
---|
1834 |
|
---|
1835 | #ifdef THOMAS_MALLOC
|
---|
1836 | factPrintf(kInfo, -1, "%ld Bytes flaged as in-use.", tgb_inuse);
|
---|
1837 | #endif
|
---|
1838 |
|
---|
1839 | gi_runStat = -99;
|
---|
1840 | gj.readStat = -99;
|
---|
1841 |
|
---|
1842 | factStat (gj);
|
---|
1843 | factStatNew (gi);
|
---|
1844 |
|
---|
1845 | return 0;
|
---|
1846 |
|
---|
1847 | } /*-----------------------------------------------------------------*/
|
---|
1848 |
|
---|
1849 |
|
---|
1850 | void *subProc(void *thrid)
|
---|
1851 | {
|
---|
1852 | const int64_t threadID = (int64_t)thrid;
|
---|
1853 |
|
---|
1854 | factPrintf(kInfo, -1, "Starting sub-process-thread %ld", threadID);
|
---|
1855 |
|
---|
1856 | while (g_runStat > -2) //in case of 'exit' we still must process pending events
|
---|
1857 | {
|
---|
1858 | int numWait = 0;
|
---|
1859 | int numProc = 0;
|
---|
1860 |
|
---|
1861 | for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)
|
---|
1862 | {
|
---|
1863 | if (!(evtCtrl.evtStat[k0] == 1000 + threadID))
|
---|
1864 | {
|
---|
1865 | if (evtCtrl.evtStat[k0] < 1000 + threadID)
|
---|
1866 | numWait++;
|
---|
1867 |
|
---|
1868 | continue;
|
---|
1869 | }
|
---|
1870 |
|
---|
1871 | /*** if (evtCtrl.evtStat[k0] == 1000 + threadID) ****/
|
---|
1872 |
|
---|
1873 | int jret = 9100; // flag to be deleted (gi_resetR>1 : flush buffers asap)
|
---|
1874 |
|
---|
1875 | if (gi_resetR<=1)
|
---|
1876 | {
|
---|
1877 | const int id = evtCtrl.evtBuf[k0];
|
---|
1878 |
|
---|
1879 | jret = subProcEvt(threadID, mBuffer[id].FADhead,
|
---|
1880 | mBuffer[id].fEvent, mBuffer[id].buffer);
|
---|
1881 |
|
---|
1882 |
|
---|
1883 | if (jret <= threadID) {
|
---|
1884 | factPrintf(kError, -1, "Process %ld wants to send event to process %d... not allowed.", threadID, jret);
|
---|
1885 | jret = 5300;
|
---|
1886 | } else if (jret <= 0)
|
---|
1887 | jret = 9200 + threadID; // flag as 'to be deleted'
|
---|
1888 | else if (jret >= gi_maxProc)
|
---|
1889 | jret = 5200 + threadID; // flag as 'to be written'
|
---|
1890 | else
|
---|
1891 | jret = 1000 + jret; // flag for next proces
|
---|
1892 | }
|
---|
1893 |
|
---|
1894 | evtCtrl.evtStat[k0] = jret;
|
---|
1895 | numProc++;
|
---|
1896 | }
|
---|
1897 |
|
---|
1898 | if (gj.readStat < -10 && numWait == 0) { //nothing left to do
|
---|
1899 | factPrintf(kInfo, -1, "Exit subProcessing in process %ld", threadID);
|
---|
1900 | return 0;
|
---|
1901 | }
|
---|
1902 |
|
---|
1903 | //seems we have nothing to do, so sleep a little
|
---|
1904 | if (numProc == 0)
|
---|
1905 | usleep(1);
|
---|
1906 | }
|
---|
1907 |
|
---|
1908 | factPrintf(kInfo, -1, "Ending sub-process-thread %ld", threadID);
|
---|
1909 |
|
---|
1910 | return 0;
|
---|
1911 | }
|
---|
1912 |
|
---|
1913 | /*-----------------------------------------------------------------*/
|
---|
1914 |
|
---|
1915 |
|
---|
1916 | void *
|
---|
1917 | procEvt (void *ptr)
|
---|
1918 | {
|
---|
1919 | /* *** main loop processing file, including SW-trigger */
|
---|
1920 | int status;
|
---|
1921 |
|
---|
1922 | int lastRun = 0; //usually run from last event still valid
|
---|
1923 |
|
---|
1924 | // cpu_set_t mask;
|
---|
1925 | // int cpu = 1; //process thread (will be several in final version)
|
---|
1926 |
|
---|
1927 | factPrintf(kInfo, -1, "Starting process-thread with %d subprocesses", gi_maxProc);
|
---|
1928 |
|
---|
1929 | /* CPU_ZERO initializes all the bits in the mask to zero. */
|
---|
1930 | // CPU_ZERO (&mask);
|
---|
1931 | /* CPU_SET sets only the bit corresponding to cpu. */
|
---|
1932 | // CPU_SET( 0 , &mask ); leave for system
|
---|
1933 | // CPU_SET( 1 , &mask ); used by write process
|
---|
1934 | // CPU_SET (2, &mask);
|
---|
1935 | // CPU_SET (3, &mask);
|
---|
1936 | // CPU_SET (4, &mask);
|
---|
1937 | // CPU_SET (5, &mask);
|
---|
1938 | // CPU_SET (6, &mask);
|
---|
1939 | // CPU_SET( 7 , &mask ); used by read process
|
---|
1940 | /* sched_setaffinity returns 0 in success */
|
---|
1941 | // if (sched_setaffinity (0, sizeof (mask), &mask) == -1) {
|
---|
1942 | // snprintf (str, MXSTR, "P ---> can not create affinity to %d", cpu);
|
---|
1943 | // factOut (kWarn, -1, str);
|
---|
1944 | // }
|
---|
1945 |
|
---|
1946 |
|
---|
1947 | pthread_t thread[100];
|
---|
1948 | // int th_ret[100];
|
---|
1949 |
|
---|
1950 | for (long long k = 0; k < gi_maxProc; k++) {
|
---|
1951 | /*th_ret[k] =*/ pthread_create (&thread[k], NULL, subProc, (void *) k);
|
---|
1952 | }
|
---|
1953 |
|
---|
1954 | // in case of 'exit' we still must process pending events
|
---|
1955 | while (g_runStat > -2)
|
---|
1956 | {
|
---|
1957 | int numWait = 0;
|
---|
1958 | int numProc = 0;
|
---|
1959 |
|
---|
1960 | for (int k0=evtCtrl.frstPtr; k0!=evtCtrl.lastPtr; k0++, k0 %= MAX_EVT*MAX_RUN)
|
---|
1961 | {
|
---|
1962 | if (evtCtrl.evtStat[k0] <= 90 || evtCtrl.evtStat[k0] >= 1000)
|
---|
1963 | {
|
---|
1964 | if (evtCtrl.evtStat[k0] >= 0 && evtCtrl.evtStat[k0] < 90)
|
---|
1965 | numWait++;
|
---|
1966 |
|
---|
1967 | continue;
|
---|
1968 | }
|
---|
1969 |
|
---|
1970 | /*** if (evtCtrl.evtStat[k0] > 90 && evtCtrl.evtStat[k0] < 1000) ***/
|
---|
1971 |
|
---|
1972 | //we are asked to flush buffers asap
|
---|
1973 | if (gi_resetR > 1)
|
---|
1974 | {
|
---|
1975 | evtCtrl.evtStat[k0] = 9991;
|
---|
1976 | continue;
|
---|
1977 | }
|
---|
1978 |
|
---|
1979 | //-------- it is better to open the run already here, so call can be used to initialize
|
---|
1980 | //-------- buffers etc. needed to interprete run (e.g. DRS calibration)
|
---|
1981 | const int id = evtCtrl.evtBuf[k0];
|
---|
1982 |
|
---|
1983 | const uint32_t irun = mBuffer[id].runNum;
|
---|
1984 | const int32_t ievt = mBuffer[id].evNum;
|
---|
1985 |
|
---|
1986 | int j = lastRun;
|
---|
1987 | if (runCtrl[lastRun].runId != irun)
|
---|
1988 | {
|
---|
1989 | //check which fileID to use (or open if needed)
|
---|
1990 | for (j = 0; j < MAX_RUN; j++) {
|
---|
1991 | if (runCtrl[j].runId == irun)
|
---|
1992 | break;
|
---|
1993 | }
|
---|
1994 | if (j >= MAX_RUN) {
|
---|
1995 | factPrintf(kFatal, 901, "procEvt: Can not find run %d for event %d in %d", irun, ievt, id);
|
---|
1996 | // FIXME: The event has to be flagged such that it will be deleted! Although that should never happen
|
---|
1997 | continue;
|
---|
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].firstUsec;
|
---|
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].firstUsec;
|
---|
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
|
---|