source: trunk/FACT++/src/EventBuilder.c@ 15460

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