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