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

Last change on this file since 11545 was 11545, checked in by tbretz, 13 years ago
Added new maxEvt support.
File size: 58.1 KB
Line 
1
2
3
4
5
6#include <stdlib.h>
7#include <stdint.h>
8#include <unistd.h>
9#include <stdio.h>
10#include <sys/time.h>
11#include <arpa/inet.h>
12#include <string.h>
13#include <math.h>
14#include <error.h>
15#include <errno.h>
16#include <unistd.h>
17#include <sys/types.h>
18#include <sys/socket.h>
19#include <pthread.h>
20#include <sched.h>
21
22#include "EventBuilder.h"
23
24enum Severity
25{
26 kMessage = 10, ///< Just a message, usually obsolete
27 kInfo = 20, ///< An info telling something which can be interesting to know
28 kWarn = 30, ///< A warning, things that somehow might result in unexpected or unwanted bahaviour
29 kError = 40, ///< Error, something unexpected happened, but can still be handled by the program
30 kFatal = 50, ///< An error which cannot be handled at all happend, the only solution is program termination
31 kDebug = 99, ///< A message used for debugging only
32};
33
34#define MIN_LEN 32 // min #bytes needed to interpret FADheader
35#define MAX_LEN 256*1024 // size of read-buffer per socket
36
37extern FileHandle_t runOpen(uint32_t irun, RUN_HEAD *runhd, size_t len ) ;
38extern int runWrite(FileHandle_t fileHd , EVENT *event, size_t len ) ;
39extern int runClose(FileHandle_t fileHd , RUN_TAIL *runth, size_t len ) ;
40extern void factOut(int severity, int err, char* message ) ;
41extern void gotNewRun( int runnr, PEVNT_HEADER *headers );
42
43
44extern void factStat(GUI_STAT gj) ;
45
46extern void factStatNew(EVT_STAT gi) ;
47
48extern int eventCheck( PEVNT_HEADER *fadhd, EVENT *event) ;
49
50extern void debugHead(int i, int j, void *buf);
51
52extern void debugRead(int isock, int ibyte, int32_t event,int32_t ftmevt,
53 int32_t runnr, int state, uint32_t tsec, uint32_t tusec ) ;
54extern void debugStream(int isock, void *buf, int len) ;
55
56int CloseRunFile(uint32_t runId, uint32_t closeTime, uint32_t maxEvt);
57
58
59uint g_actTime ;
60uint g_actUsec ;
61int g_runStat ;
62int g_reset ;
63int gi_reset, gi_resetR, gi_resetS, gi_resetW, gi_resetX ;
64size_t g_maxMem ; //maximum memory allowed for buffer
65
66//no longer needed ...
67 int g_maxBoards ; //maximum number of boards to be initialized
68 int g_actBoards ;
69//
70
71FACT_SOCK g_port[NBOARDS] ; // .addr=string of IP-addr in dotted-decimal "ddd.ddd.ddd.ddd"
72
73
74 int gi_runStat ;
75 int gp_runStat ;
76 int gw_runStat ;
77
78 int gi_memStat = +1 ;
79
80 uint32_t gi_myRun = 0 ;
81 uint32_t actrun = 0 ;
82
83
84uint gi_NumConnect[NBOARDS]; //4 crates * 10 boards
85
86//uint gi_EvtStart= 0 ;
87//uint gi_EvtRead = 0 ;
88//uint gi_EvtBad = 0 ;
89//uint gi_EvtTot = 0 ;
90//size_t gi_usedMem = 0 ;
91
92//uint gw_EvtTot = 0 ;
93//uint gp_EvtTot = 0 ;
94
95PIX_MAP g_pixMap[NPIX] ;
96
97EVT_STAT gi ;
98GUI_STAT gj ;
99
100EVT_CTRL evtCtrl ; //control of events during processing
101int evtIdx[MAX_EVT*MAX_RUN] ; //index from mBuffer to evtCtrl
102
103WRK_DATA mBuffer[MAX_EVT*MAX_RUN]; //local working space
104
105
106
107
108RUN_HEAD actRun ;
109
110RUN_CTRL runCtrl[MAX_RUN] ;
111
112RUN_TAIL runTail[MAX_RUN] ;
113
114
115/*
116*** Definition of rdBuffer to read in IP packets; keep it global !!!!
117 */
118
119
120typedef union {
121 int8_t B[MAX_LEN ];
122 int16_t S[MAX_LEN/2];
123 int32_t I[MAX_LEN/4];
124 int64_t L[MAX_LEN/8];
125} CNV_FACT ;
126
127typedef struct {
128 int bufTyp ; //what are we reading at the moment: 0=header 1=data -1=skip ...
129 int32_t bufPos ; //next byte to read to the buffer next
130 int32_t bufLen ; //number of bytes left to read
131 int32_t skip ; //number of bytes skipped before start of event
132
133 int sockStat ; //-1 if socket not yet connected , 99 if not exist
134 int socket ; //contains the sockets
135 struct sockaddr_in SockAddr ; //IP for each socket
136
137 int evtID ; // event ID of event currently read
138 int runID ; // run "
139 int ftmID ; // event ID from FTM
140 uint fadLen ; // FADlength of event currently read
141 int fadVers ; // Version of FAD
142 int board ; // boardID (softwareID: 0..40 )
143 int Port ;
144
145 CNV_FACT *xBuf ; //a copy of rBuf (temporary for debuging)
146 CNV_FACT *rBuf ;
147
148
149} READ_STRUCT ;
150
151
152typedef union {
153 int8_t B[2];
154 int16_t S ;
155} SHORT_BYTE ;
156
157
158
159
160
161#define MXSTR 1000
162char str[MXSTR] ;
163
164SHORT_BYTE start, stop;
165
166READ_STRUCT rd[MAX_SOCK] ; //buffer to read IP and afterwards store in mBuffer
167
168
169
170/*-----------------------------------------------------------------*/
171
172
173/*-----------------------------------------------------------------*/
174
175
176
177int GenSock(int flag, int sid, int port, struct sockaddr_in *sockAddr, READ_STRUCT *rd) {
178/*
179*** generate Address, create sockets and allocates readbuffer for it
180***
181*** if flag==0 generate socket and buffer
182*** <0 destroy socket and buffer
183*** >0 close and redo socket
184***
185*** sid : board*7 + port id
186 */
187
188 int j ;
189
190 if (rd->sockStat ==0 ) { //close socket if open
191 j=close(rd->socket) ;
192 if (j>0) {
193 snprintf(str,MXSTR,"Error closing socket %d | %m",sid);
194 factOut(kFatal,771, str ) ;
195 } else {
196 snprintf(str,MXSTR,"Succesfully closed socket %d",sid);
197 factOut(kInfo,771, str ) ;
198 }
199 }
200
201
202 if (flag < 0) {
203 free(rd->rBuf) ; //and never open again
204 free(rd->xBuf) ; //and never open again
205 rd->rBuf = NULL ;
206 rd->sockStat = 99 ;
207 return 0 ;
208 }
209
210
211 if (flag == 0) { //generate address and buffer ...
212 rd->Port = port ;
213 rd->SockAddr.sin_family = sockAddr->sin_family;
214 rd->SockAddr.sin_port = htons(port) ;
215 rd->SockAddr.sin_addr = sockAddr->sin_addr ;
216
217 rd->xBuf = malloc(sizeof(CNV_FACT) ) ;
218 rd->rBuf = malloc(sizeof(CNV_FACT) ) ;
219 if ( rd->rBuf == NULL ) {
220 snprintf(str,MXSTR,"Could not create local buffer %d",sid);
221 factOut(kFatal,774, str ) ;
222 rd->sockStat = 77 ;
223 return -3 ;
224 }
225 }
226
227
228 if ( (rd->socket = socket (PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) <= 0) {
229 snprintf(str,MXSTR,"Could not generate socket %d | %m",sid);
230 factOut(kFatal,773, str ) ;
231 rd->sockStat = 88 ;
232 return -2 ;
233 }
234
235 snprintf(str,MXSTR,"Successfully generated socket %d ",sid);
236 factOut(kInfo,773, str ) ;
237 rd->sockStat = -1 ; //try to (re)open socket
238 return 0 ;
239
240} /*-----------------------------------------------------------------*/
241
242 /*-----------------------------------------------------------------*/
243
244
245
246
247int mBufInit() {
248// initialize mBuffer (mark all entries as unused\empty)
249
250 int i ;
251 uint32_t actime ;
252
253 actime = g_actTime + 50000000 ;
254
255 for (i=0; i<MAX_EVT*MAX_RUN; i++) {
256 mBuffer[i].evNum = mBuffer[i].nRoi = -1;
257 mBuffer[i].runNum= 0 ;
258
259 evtCtrl.evtBuf[ i] = -1 ;
260 evtCtrl.evtStat[ i] = -1 ;
261 evtCtrl.pcTime[ i] = actime ; //initiate to far future
262 }
263
264
265 actRun.FADhead = malloc( NBOARDS* sizeof(PEVNT_HEADER) ) ;
266
267 evtCtrl.frstPtr = 0 ;
268 evtCtrl.lastPtr = 0 ;
269
270 return 0 ;
271
272} /*-----------------------------------------------------------------*/
273
274
275
276
277int mBufEvt( int evID, uint runID, int nRoi[8], int sk, int fadlen) {
278// generate a new Event into mBuffer:
279// make sure only complete Event are possible, so 'free' will always work
280// returns index into mBuffer[], or negative value in case of error
281// error: <-9000 if roi screwed up (not consistent with run)
282// <-8000 (not consistent with event)
283// <-7000 (not consistent with board)
284// < 0 if no space left
285
286 struct timeval *tv, atv;
287 tv=&atv;
288 uint32_t tsec, tusec ;
289
290 int i, k, jr, b, evFree ;
291 int headmem=0 ;
292 size_t needmem = 0 ;
293
294
295 b = sk/7 ;
296
297 if (nRoi[0] <0 || nRoi[0] > 1024) {
298 snprintf(str,MXSTR,"illegal nRoi[0]: %d",nRoi[0]) ;
299 factOut(kError, 999, str ) ;
300 gj.badRoiR++ ;
301 gj.badRoi[b]++ ;
302 return -9999 ;
303 }
304
305 for (jr=1; jr<8; jr++) {
306 if ( nRoi[jr] != nRoi[0] ) {
307 snprintf(str,MXSTR,"wrong nRoi[%d]: %d %d",jr,nRoi[jr],nRoi[0]) ;
308 factOut(kError,711, str ) ;
309 gj.badRoiB++ ;
310 gj.badRoi[b]++ ;
311 return -7101 ;
312 }
313 }
314 if ( nRoi[8] < nRoi[0] ) {
315 snprintf(str,MXSTR,"wrong nRoi_TM: %d %d",nRoi[8],nRoi[0]) ;
316 factOut(kError,712, str ) ;
317 gj.badRoiB++ ;
318 gj.badRoi[b]++ ;
319 return -7102 ;
320 }
321
322
323 i = evID % MAX_EVT ;
324 evFree = -1 ;
325
326 for ( k=0; k<MAX_RUN; k++) {
327 if ( mBuffer[i].evNum == evID
328 && mBuffer[i].runNum== runID ) { //event is already registered;
329 // is it ok ????
330 if ( mBuffer[i].nRoi != nRoi[0]
331 || mBuffer[i].nRoiTM != nRoi[8] ) {
332 snprintf(str,MXSTR,"illegal evt_roi %d %d ; %d %d",
333 nRoi[0],nRoi[8], mBuffer[i].nRoi, mBuffer[i].nRoiTM );
334 factOut(kError,821, str ) ;
335 gj.badRoiE++ ;
336 gj.badRoi[b]++ ;
337 return -8201 ;
338 }
339 //everything seems fine so far ==> use this slot ....
340 return i ;
341 }
342 if ( evFree < 0 && mBuffer[i].evNum < 0 ) evFree = i ;
343 i += MAX_EVT ;
344 }
345
346
347 //event does not yet exist; create it
348 if (evFree < 0 ) { //no space available in ctrl
349 snprintf(str,MXSTR,"no control slot to keep event %d",evID) ;
350 factOut(kError,881, str ) ;
351 return -1 ;
352 }
353 i = evFree ; //found free entry; use it ...
354
355 gettimeofday( tv, NULL);
356 tsec = atv.tv_sec ;
357 tusec= atv.tv_usec ;
358
359 //check if runId already registered in runCtrl
360 evFree = -1 ;
361 for (k=0; k<MAX_RUN; k++) {
362 if (runCtrl[k].runId == runID ) {
363 if ( runCtrl[k].roi0 != nRoi[0]
364 || runCtrl[k].roi8 != nRoi[8] ) {
365 snprintf(str,MXSTR,"illegal run_roi %d %d ; %d %d",
366 nRoi[0],nRoi[8],runCtrl[k].roi0,runCtrl[k].roi8 );
367 factOut(kError,931, str ) ;
368 gj.badRoiR++ ;
369 gj.badRoi[b]++ ;
370 return -9301 ;
371 }
372 goto RUNFOUND ;
373 }
374 else if (evFree < 0 && runCtrl[k].runId == 0 ) evFree = k ;
375 }
376
377 if (evFree <0 ) {
378 snprintf(str,MXSTR,"not able to register the new run %d",runID);
379 factOut(kFatal,883, str ) ;
380 return -1001 ;
381 } else {
382 snprintf(str,MXSTR,"register new run %d roi: %d %d",runID,nRoi[0],nRoi[8]) ;
383 factOut(kInfo,503, str ) ;
384 runCtrl[evFree].runId = runID ;
385 runCtrl[evFree].roi0 = nRoi[0] ;
386 runCtrl[evFree].roi8 = nRoi[8] ;
387 runCtrl[evFree].fileId = -2 ;
388 runCtrl[evFree].lastEvt= -1 ;
389 runCtrl[evFree].nextEvt= 0;
390 runCtrl[evFree].actEvt = 0;
391 runCtrl[evFree].maxEvt = 999999999 ; //max number events allowed
392 runCtrl[evFree].firstUsec=tusec ;
393 runCtrl[evFree].firstTime=
394 runCtrl[evFree].lastTime=tsec ;
395 runCtrl[evFree].closeTime=tsec + 3600*24 ; //max time allowed
396 runCtrl[evFree].lastTime = 0 ;
397
398 runTail[evFree].nEventsOk =
399 runTail[evFree].nEventsRej =
400 runTail[evFree].nEventsBad =
401 runTail[evFree].PCtime0 =
402 runTail[evFree].PCtimeX = 0 ;
403 }
404
405RUNFOUND:
406
407 needmem = sizeof(EVENT) + NPIX*nRoi[0]*2 + NTMARK*nRoi[0]*2; //
408
409 headmem = NBOARDS* sizeof(PEVNT_HEADER) ;
410
411 if ( gj.usdMem + needmem + headmem > g_maxMem) {
412 gj.maxMem = gj.usdMem + needmem + headmem ;
413 if (gi_memStat>0 ) {
414 gi_memStat = -99 ;
415 snprintf(str,MXSTR,"no memory left to keep event %6d sock %3d",evID,sk) ;
416 factOut(kError,882, str ) ;
417 } else {
418 snprintf(str,MXSTR,"no memory left to keep event %6d sock %3d",evID,sk) ;
419 factOut(kDebug,882, str ) ;
420 }
421 return -11 ;
422 }
423
424 mBuffer[i].FADhead = malloc( headmem ) ;
425 if (mBuffer[i].FADhead == NULL) {
426 snprintf(str,MXSTR,"malloc header failed for event %d",evID) ;
427 factOut(kError,882, str ) ;
428 return -12;
429 }
430
431 mBuffer[i].fEvent = malloc( needmem ) ;
432 if (mBuffer[i].fEvent == NULL) {
433 snprintf(str,MXSTR,"malloc data failed for event %d",evID) ;
434 factOut(kError,882, str ) ;
435 free(mBuffer[i].fEvent) ;
436 mBuffer[i].fEvent = NULL ;
437 return -22;
438 }
439
440
441
442 //flag all boards as unused
443 mBuffer[i].nBoard = 0 ;
444 for (k=0; k<NBOARDS; k++ ) {
445 mBuffer[i].board[k] = -1;
446 }
447 //flag all pixels as unused
448 for (k=0; k<NPIX; k++ ) {
449 mBuffer[i].fEvent->StartPix[k] = -1 ;
450 }
451 //flag all TMark as unused
452 for (k=0; k<NTMARK; k++ ) {
453 mBuffer[i].fEvent->StartTM[k] = -1 ;
454 }
455
456 mBuffer[i].fEvent->NumBoards = 0 ;
457 mBuffer[i].fEvent->PCUsec = tusec ;
458 mBuffer[i].fEvent->PCTime =
459 mBuffer[i].pcTime = tsec ;
460 mBuffer[i].nRoi = nRoi[0] ;
461 mBuffer[i].nRoiTM = nRoi[8] ;
462 mBuffer[i].evNum = evID ;
463 mBuffer[i].runNum = runID ;
464 mBuffer[i].evtLen = needmem ;
465
466 gj.usdMem += needmem + headmem;
467 if (gj.usdMem > gj.maxMem ) gj.maxMem = gj.usdMem ;
468
469 gj.bufTot++ ;
470 if (gj.bufTot > gj.maxEvt ) gj.maxEvt = gj.bufTot ;
471
472 gj.rateNew++ ;
473
474 //register event in 'active list (reading)'
475
476 evtCtrl.evtBuf[ evtCtrl.lastPtr] = i ;
477 evtCtrl.evtStat[ evtCtrl.lastPtr] = 0 ;
478 evtCtrl.pcTime[ evtCtrl.lastPtr] = g_actTime ;
479 evtIdx[i] = evtCtrl.lastPtr ;
480
481
482 snprintf(str,MXSTR,"%5d %8d start new evt %8d %8d sock %3d len %5d t %10d",
483 evID,runID,i,evtCtrl.lastPtr,sk,fadlen,mBuffer[i].pcTime);
484 factOut(kDebug,-11, str ) ;
485 evtCtrl.lastPtr++ ;
486 if (evtCtrl.lastPtr == MAX_EVT*MAX_RUN ) evtCtrl.lastPtr = 0;
487
488 gi.evtGet++ ;
489
490 return i ;
491
492} /*-----------------------------------------------------------------*/
493
494
495
496
497int mBufFree(int i) {
498//delete entry [i] from mBuffer:
499//(and make sure multiple calls do no harm ....)
500
501 int headmem=0 ;
502 int evid ;
503 size_t freemem = 0 ;
504
505 evid = mBuffer[i].evNum ;
506 freemem = mBuffer[i].evtLen ;
507
508 free(mBuffer[i].fEvent ) ;
509 mBuffer[i].fEvent = NULL ;
510
511 free(mBuffer[i].FADhead ) ;
512 mBuffer[i].FADhead = NULL ;
513
514 headmem = NBOARDS* sizeof(PEVNT_HEADER) ;
515 mBuffer[i].evNum = mBuffer[i].nRoi= -1;
516 mBuffer[i].runNum = 0;
517
518 gj.usdMem = gj.usdMem - freemem - headmem;
519 gj.bufTot-- ;
520
521 if (gi_memStat < 0 ) {
522 if (gj.usdMem <= 0.75 * gj.maxMem ) gi_memStat = +1 ;
523 }
524
525
526 return 0 ;
527
528} /*-----------------------------------------------------------------*/
529
530
531void resetEvtStat() {
532 int i ;
533
534 for (i=0; i<MAX_SOCK; i++) gi.numRead[i] = 0 ;
535
536 for (i=0; i<NBOARDS; i++ ) {
537 gi.gotByte[i] = 0 ;
538 gi.gotErr[i] = 0 ;
539
540 }
541
542 gi.evtGet = 0 ; //#new Start of Events read
543 gi.evtTot = 0 ; //#complete Events read
544 gi.evtErr = 0 ; //#Events with Errors
545 gi.evtSkp = 0 ; //#Events incomplete (timeout)
546
547 gi.procTot = 0 ; //#Events processed
548 gi.procErr = 0 ; //#Events showed problem in processing
549 gi.procTrg = 0 ; //#Events accepted by SW trigger
550 gi.procSkp = 0 ; //#Events rejected by SW trigger
551
552 gi.feedTot = 0 ; //#Events used for feedBack system
553 gi.feedErr = 0 ; //#Events rejected by feedBack
554
555 gi.wrtTot = 0 ; //#Events written to disk
556 gi.wrtErr = 0 ; //#Events with write-error
557
558 gi.runOpen = 0 ; //#Runs opened
559 gi.runClose= 0 ; //#Runs closed
560 gi.runErr = 0 ; //#Runs with open/close errors
561
562return ;
563} /*-----------------------------------------------------------------*/
564
565
566
567void initReadFAD() {
568return ;
569} /*-----------------------------------------------------------------*/
570
571
572
573void *readFAD( void *ptr ) {
574/* *** main loop reading FAD data and sorting them to complete events */
575 int head_len,frst_len,numok,numok2,numokx,dest,evID,i,k ;
576 int actBoards = 0, minLen ;
577 int32_t jrd ;
578 uint gi_SecTime ; //time in seconds
579 int boardId, roi[8],drs,px,src,pixS,pixH,pixC,pixR,tmS ;
580
581 int goodhed = 0 ;
582 int errcnt0 = 0 ;
583
584 int sockDef[NBOARDS]; //internal state of sockets
585 int jrdx ;
586
587
588 struct timespec xwait ;
589
590
591 struct timeval *tv, atv;
592 tv=&atv;
593 uint32_t tsec, tusec ;
594
595
596 snprintf(str,MXSTR,"start initializing");
597 factOut(kInfo,-1, str ) ;
598
599 int cpu = 7 ; //read thread
600 cpu_set_t mask;
601
602/* CPU_ZERO initializes all the bits in the mask to zero. */
603 CPU_ZERO( &mask );
604/* CPU_SET sets only the bit corresponding to cpu. */
605 cpu = 7 ;
606 CPU_SET( cpu, &mask );
607
608/* sched_setaffinity returns 0 in success */
609 if ( sched_setaffinity( 0, sizeof(mask), &mask ) == -1 ) {
610 snprintf(str,MXSTR,"W ---> can not create affinity to %d",cpu);
611 factOut(kWarn,-1, str ) ;
612 }
613
614 head_len = sizeof(PEVNT_HEADER) ;
615 frst_len = head_len ; //max #bytes to read first: fad_header only, so each event must be longer, even for roi=0
616 minLen = head_len ; //min #bytes needed to check header: full header for debug
617
618 start.S=0xFB01;
619 stop.S= 0x04FE;
620
621/* initialize run control logics */
622 for (i=0; i<MAX_RUN; i++) {
623 runCtrl[i].runId = 0 ;
624 runCtrl[i].fileId = -2 ;
625 }
626 gi_resetS = gi_resetR = 9;
627 for (i=0; i<NBOARDS; i++) sockDef[i]= 0 ;
628
629START:
630 gettimeofday( tv, NULL);
631 g_actTime = tsec = atv.tv_sec ;
632 g_actUsec = tusec= atv.tv_usec ;
633 gi_myRun = g_actTime ;
634 evtCtrl.frstPtr = 0 ;
635 evtCtrl.lastPtr = 0 ;
636
637 gi_SecTime = g_actTime ;
638 gi_runStat = g_runStat ;
639 gj.readStat= g_runStat ;
640 numok = numok2 = 0 ;
641
642 if ( gi_resetS > 0) {
643 //make sure all sockets are preallocated as 'not exist'
644 for (i=0; i<MAX_SOCK; i++) {
645 rd[i].socket = -1 ;
646 rd[i].sockStat = 99 ;
647 }
648
649 for (k=0; k<NBOARDS; k++) {
650 gi_NumConnect[k]=0;
651 gi.numConn[k] =0;
652 gj.numConn[k] =0;
653 gj.errConn[k] =0;
654 gj.rateBytes[k] =0;
655 gj.totBytes[k] =0;
656 }
657
658 }
659
660
661 if ( gi_resetR > 0) {
662 resetEvtStat();
663 gj.bufTot = gj.maxEvt = gj.xxxEvt = 0 ;
664 gj.usdMem = gj.maxMem = gj.xxxMem = 0 ;
665 gj.totMem = g_maxMem ;
666 gj.bufNew = gj.bufEvt = 0 ;
667 gj.badRoiE = gj.badRoiR = gj.badRoiB =
668 gj.evtSkip = gj.evtWrite = gj.evtErr = 0 ;
669
670 int b ;
671 for (b=0; b<NBOARDS; b++) gj.badRoi[b]=0 ;
672
673 mBufInit() ; //initialize buffers
674
675 snprintf(str,MXSTR,"end initializing");
676 factOut(kInfo,-1, str ) ;
677 }
678
679
680 gi_reset = gi_resetR = gi_resetS = gi_resetW = 0 ;
681
682 while (g_runStat >=0 && g_reset ==0 ) { //loop until global variable g_runStat claims stop
683
684 gi_runStat = g_runStat;
685 gj.readStat= g_runStat;
686 gettimeofday( tv, NULL);
687 g_actTime = tsec = atv.tv_sec ;
688 g_actUsec = tusec= atv.tv_usec ;
689
690
691 int b,p,p0,s0,nch;
692 nch = 0 ;
693 for (b=0; b<NBOARDS; b++ ) {
694 k = b*7 ;
695 if ( g_port[b].sockDef != sockDef[b] ) { //something has changed ...
696 nch++ ;
697 gi_NumConnect[ b ] = 0 ; //must close all connections
698 gi.numConn[ b ] = 0;
699 gj.numConn[ b ] = 0;
700 if ( sockDef[b] == 0) s0= 0 ; //sockets to be defined and opened
701 else if (g_port[b].sockDef == 0) s0= -1 ; //sockets to be destroyed
702 else s0= +1 ; //sockets to be closed and reopened
703
704 if (s0 == 0) p0=ntohs(g_port[b].sockAddr.sin_port);
705 else p0=0 ;
706
707 for (p=p0+1; p<p0+8; p++) {
708 GenSock(s0, k, p, &g_port[b].sockAddr, &rd[k]) ; //generate address and socket
709 k++ ;
710 }
711 sockDef[b] = g_port[b].sockDef ;
712 }
713 }
714
715 if (nch > 0 ) {
716 actBoards = 0 ;
717 for (b=0; b<NBOARDS; b++ ) {
718 if ( sockDef[b] > 0 ) actBoards++ ;
719 }
720 }
721
722
723 jrdx = 0;
724 numokx= 0;
725 numok = 0 ; //count number of succesfull actions
726
727 for (i=0; i<MAX_SOCK; i++) { //check all sockets if something to read
728 b = i / 7 ;
729 if (sockDef[b] > 0) s0= +1 ;
730 else s0= -1 ;
731
732 if (rd[i].sockStat <0 ) { //try to connect if not yet done
733 rd[i].sockStat=connect(rd[i].socket,
734 (struct sockaddr*) &rd[i].SockAddr, sizeof(rd[i].SockAddr)) ;
735 if (rd[i].sockStat ==0 ) { //successfull ==>
736 if (sockDef[b] > 0) {
737 rd[i].bufTyp = 0 ; // expect a header
738 rd[i].bufLen = frst_len ; // max size to read at begining
739 } else {
740 rd[i].bufTyp = -1 ; // full data to be skipped
741 rd[i].bufLen = MAX_LEN ; //huge for skipping
742 }
743 rd[i].bufPos = 0 ; // no byte read so far
744 rd[i].skip = 0 ; // start empty
745 gi_NumConnect[ b ]++ ;
746 gi.numConn[ b ]++ ;
747 gj.numConn[ b ]++ ;
748 snprintf(str,MXSTR,"+++connect %d %d",b,gi.numConn[ b ]);
749 factOut(kInfo,-1, str ) ;
750 }
751 }
752
753 if (rd[i].sockStat ==0 ) { //we have a connection ==> try to read
754 if (rd[i].bufLen > 0) { //might be nothing to read [buffer full]
755 numok++ ;
756 jrd=recv(rd[i].socket,&rd[i].rBuf->B[ rd[i].bufPos], rd[i].bufLen, MSG_DONTWAIT);
757
758 if (jrd >0 ) {
759 debugStream(i,&rd[i].rBuf->B[ rd[i].bufPos],jrd) ;
760 memcpy(&rd[i].xBuf->B[ rd[i].bufPos], &rd[i].rBuf->B[ rd[i].bufPos], jrd) ;
761 snprintf(str,MXSTR,"read sock %3d bytes %5d len %5d first %d %d",i,jrd,rd[i].bufLen,
762 rd[i].rBuf->B[ rd[i].bufPos ],
763 rd[i].rBuf->B[ rd[i].bufPos +1] );
764 factOut(kDebug,301, str ) ;
765 }
766
767 if (jrd == 0) { //connection has closed ...
768 snprintf(str,MXSTR,"Socket %d closed by FAD",i);
769 factOut(kInfo,441, str ) ;
770 GenSock(s0, i, 0,NULL, &rd[i]) ;
771 gi.gotErr[ b ]++ ;
772 gi_NumConnect[ b ]-- ;
773 gi.numConn[ b ]-- ;
774 gj.numConn[ b ]-- ;
775
776 } else if ( jrd<0 ) { //did not read anything
777 if (errno != EAGAIN && errno != EWOULDBLOCK ) {
778 snprintf(str,MXSTR,"Error Reading from %d | %m",i);
779 factOut(kError,442, str ) ;
780 gi.gotErr[ b ]++ ;
781 } else numok-- ; //else nothing waiting to be read
782 jrd = 0 ;
783 }
784 } else {
785 jrd = 0 ; //did read nothing as requested
786 snprintf(str,MXSTR,"do not read from socket %d %d",i,rd[i].bufLen ) ;
787 factOut(kDebug,301, str ) ;
788 }
789
790 gi.gotByte[ b ] += jrd ;
791 gj.rateBytes[b] += jrd ;
792
793 if (jrd>0) {numokx++ ; jrdx+= jrd; }
794
795
796 if ( rd[i].bufTyp <0 ) { // we are skipping this board ...
797// just do nothing
798
799 } else if ( rd[i].bufTyp >0 ) { // we are reading data ...
800 if ( jrd < rd[i].bufLen ) { //not yet all read
801 rd[i].bufPos += jrd ; //==> prepare for continuation
802 rd[i].bufLen -= jrd ;
803 debugRead(i,jrd,rd[i].evtID,rd[i].ftmID,rd[i].runID, 0,tsec,tusec) ; // i=socket; jrd=#bytes; ievt=eventid; 0=reading data
804 } else { //full dataset read
805 rd[i].bufLen = 0 ;
806 rd[i].bufPos = rd[i].fadLen ;
807 if ( rd[i].rBuf->B[ rd[i].bufPos-1] != stop.B[0]
808 || rd[i].rBuf->B[ rd[i].bufPos-2] != stop.B[1]) {
809 gi.evtErr++ ;
810 snprintf(str,MXSTR,"wrong end of buffer found sock %3d ev %4d len %5d %3d %3d - %3d %3d ",
811 i,rd[i].fadLen, rd[i].evtID, rd[i].rBuf->B[0], rd[i].rBuf->B[1], rd[i].rBuf->B[ rd[i].bufPos-2],
812 rd[i].rBuf->B[ rd[i].bufPos-1]);
813 factOut(kError,301, str ) ;
814 goto EndBuf ;
815
816// } else {
817// snprintf(str,MXSTR,"good end of buffer found sock %3d len %5d %d %d : %d %d - %d %d : %d %d",
818// i,rd[i].fadLen,
819// rd[i].rBuf->B[ 0 ], rd[i].rBuf->B[ 1 ], start.B[1],start.B[0],
820// rd[i].rBuf->B[ rd[i].bufPos-2], rd[i].rBuf->B[ rd[i].bufPos-1], stop.B[1], stop.B[0]);
821// factOut(kDebug,301, str ) ;
822 }
823
824 if (jrd>0) debugRead(i,jrd,rd[i].evtID,rd[i].ftmID,rd[i].runID, 1,tsec,tusec) ; // i=socket; jrd=#bytes; ievt=eventid; 1=finished event
825
826 //we have a complete buffer, copy to WORK area
827 int jr ;
828 roi[0] = ntohs(rd[i].rBuf->S[ head_len/2 + 2 ]) ;
829 for (jr=0; jr<9; jr++) {
830 roi[jr] = ntohs(rd[i].rBuf->S[ head_len/2 + 2 + jr*(roi[0]+4) ]) ;
831 }
832 //get index into mBuffer for this event (create if needed)
833 evID = mBufEvt( rd[i].evtID, rd[i].runID, roi, i, rd[i].fadLen ) ;
834
835 if (evID <-1000) {
836 goto EndBuf ; //not usable board/event/run --> skip it
837 }
838 if (evID < 0) { //no space left, retry later
839
840 if ( rd[i].bufLen != 0) {
841 snprintf(str,MXSTR,"something screwed up");
842 factOut(kFatal, 1, str ) ;
843 }
844 xwait.tv_sec = 0;
845 xwait.tv_nsec= 10000000 ; // sleep for ~10 msec
846 nanosleep( &xwait , NULL ) ;
847 goto EndBuf1 ; //hope there is free space next round
848 }
849
850
851 //we have a valid entry in mBuffer[]; fill it
852
853int xchk = memcmp(&rd[i].xBuf->B[0], &rd[i].rBuf->B[0], rd[i].fadLen ) ;
854if (xchk != 0) {
855 snprintf(str,MXSTR,"ERROR OVERWRITE %d %d on port %d",xchk,rd[i].fadLen,i) ;
856 factOut(kFatal, 1, str ) ;
857
858 uint iq;
859 for (iq=0; iq < rd[i].fadLen ; iq++) {
860 if (rd[i].rBuf->B[iq] != rd[i].xBuf->B[iq] ) {
861 snprintf(str,MXSTR,"ERROR %4d %4d %x %x",i,iq,rd[i].rBuf->B[iq], rd[i].xBuf->B[iq]);
862 factOut(kFatal, 1, str ) ;
863 }
864 }
865}
866
867
868 boardId = b ;
869 int fadBoard = ntohs(rd[i].rBuf->S[12] ) ;
870 int fadCrate = fadBoard/256 ;
871 if (boardId != (fadCrate*10 + fadBoard%256) ) {
872 snprintf(str,MXSTR,"wrong Board ID %d %d %d",fadCrate,fadBoard%256,boardId) ;
873 if (errcnt0++ < 99 ) factOut(kWarn,301, str ) ; //print only few times
874 }
875 if ( mBuffer[evID].board[ boardId ] != -1) {
876 snprintf(str,MXSTR,"double board: ev %5d, b %3d, %3d; len %5d %3d %3d - %3d %3d ",
877 evID,boardId,i,rd[i].fadLen, rd[i].rBuf->B[0],
878 rd[i].rBuf->B[1], rd[i].rBuf->B[ rd[i].bufPos-2], rd[i].rBuf->B[ rd[i].bufPos-1]);
879 factOut(kWarn,501, str ) ;
880 goto EndBuf ; //--> skip Board
881 }
882
883 int iDx = evtIdx[evID] ; //index into evtCtrl
884
885 memcpy( &mBuffer[evID].FADhead[boardId].start_package_flag,
886 &rd[i].rBuf->S[0], head_len) ;
887// xxx roi = mBuffer[evID].nRoi ;
888
889 src = head_len/2 ;
890 for ( px=0; px<9; px++ ) { //different sort in FAD board.....
891 for ( drs=0; drs<4; drs++ ) {
892 pixH= ntohs(rd[i].rBuf->S[src++]) ; // ID
893 pixC= ntohs(rd[i].rBuf->S[src++]) ; // start-cell
894 pixR= ntohs(rd[i].rBuf->S[src++]) ; // roi
895//here we should check if pixH is correct ....
896
897 pixS = boardId*36 + drs*9 + px ;
898 src++ ;
899
900// xxx if ( ( px != 8 && pixR == roi )
901// xxx || ( px == 8 && pixR >= roi ) ) { //we have a reasonable roi
902
903 mBuffer[evID].fEvent->StartPix[pixS] =pixC;
904 dest= pixS * roi[0] ;
905 memcpy(
906 &mBuffer[evID].fEvent->Adc_Data[dest],
907 &rd[i].rBuf->S[src], roi[0] * 2) ;
908 src+= pixR ;
909
910 if ( px==8 ) {
911 if ( pixR > roi[0]) { //and we have additional TM info
912 tmS =boardId*4 + drs ;
913 dest= tmS * roi[0] + NPIX* roi[0] ;
914 int srcT= src - roi[0] ;
915 mBuffer[evID].fEvent->StartTM[tmS] = (pixC+pixR-roi[0])%1024 ;
916 memcpy(
917 &mBuffer[evID].fEvent->Adc_Data[dest],
918 &rd[i].rBuf->S[srcT], roi[0] * 2) ;
919 } else {
920 mBuffer[evID].fEvent->StartTM[tmS] = -1 ;
921 }
922 }
923// xxx } else {
924// xxx snprintf(str,MXSTR,"wrong roi %d %d %d %d",px,pixR,roi,src-2);
925// xxx gi.evtErr++ ;
926// xxx factOut(kError,202, str ) ;
927// xxx goto EndBuf ;
928// xxx }
929 }
930 }// now we have stored a new board contents into Event structure
931
932 mBuffer[evID].fEvent->NumBoards++ ;
933 mBuffer[evID].board[ boardId ] = boardId ;
934 evtCtrl.evtStat[ iDx ]++ ;
935 evtCtrl.pcTime[ iDx ] = g_actTime ;
936
937 if (++mBuffer[evID].nBoard >= actBoards ) {
938 snprintf(str,MXSTR,"%5d complete event %8d %8d %2d",mBuffer[evID].evNum,evtCtrl.evtBuf[iDx],iDx,evtCtrl.evtStat[ iDx ]);
939 factOut(kDebug,-1, str ) ;
940
941 if (mBuffer[evID].runNum != actrun ) { // have we already reported first event of this run ???
942 actrun = mBuffer[evID].runNum ;
943 int ir ;
944 for ( ir=0; ir<MAX_RUN; ir++) {
945 if ( runCtrl[ir].runId == actrun) {
946 if ( ++runCtrl[ir].lastEvt ==0 ) {
947 gotNewRun( actrun, mBuffer[evID].FADhead );
948 snprintf(str,MXSTR,"gotNewRun %d (ev %d)",mBuffer[evID].runNum,mBuffer[evID].evNum);
949 factOut(kInfo,1, str ) ;
950 break ;
951 }
952 }
953 }
954 }
955
956 //complete event read ---> flag for next processing
957 evtCtrl.evtStat[ iDx ] = 99;
958 gi.evtTot++ ;
959 }
960
961EndBuf:
962 rd[i].bufTyp = 0 ; //ready to read next header
963 rd[i].bufLen = frst_len ;
964 rd[i].bufPos = 0 ;
965EndBuf1:
966 ;
967 }
968
969 } else { //we are reading event header
970 rd[i].bufPos += jrd ;
971 rd[i].bufLen -= jrd ;
972 if ( rd[i].bufPos >= minLen ){ //sufficient data to take action
973 //check if startflag correct; else shift block ....
974 for (k=0; k<rd[i].bufPos -1 ; k++) {
975 if (rd[i].rBuf->B[k ] == start.B[1]
976 && rd[i].rBuf->B[k+1] == start.B[0] ) break ;
977 }
978 rd[i].skip += k ;
979
980 if (k >= rd[i].bufPos-1 ) { //no start of header found
981 rd[i].bufPos = 0 ;
982 rd[i].bufLen = head_len ;
983 } else if ( k>0 ) {
984 rd[i].bufPos -= k ;
985 rd[i].bufLen += k ;
986 memcpy(&rd[i].rBuf->B[0], &rd[i].rBuf->B[k], rd[i].bufPos ) ;
987 memcpy(&rd[i].xBuf->B[0], &rd[i].xBuf->B[k], rd[i].bufPos ) ;
988 }
989 if ( rd[i].bufPos >= minLen ) {
990 if ( rd[i].skip >0 ) {
991 snprintf(str,MXSTR,"skipped %d bytes on port%d", rd[i].skip, i ) ;
992 factOut(kInfo,666, str ) ;
993 rd[i].skip = 0 ;
994 }
995 goodhed++;
996 rd[i].fadLen = ntohs(rd[i].rBuf->S[1])*2 ;
997 rd[i].fadVers= ntohs(rd[i].rBuf->S[2]) ;
998 rd[i].evtID = ntohl(rd[i].rBuf->I[4]) ; //(FADevt)
999 rd[i].ftmID = ntohl(rd[i].rBuf->I[5]) ; //(FTMevt)
1000 rd[i].runID = ntohl(rd[i].rBuf->I[11]) ;
1001 rd[i].bufTyp = 1 ; //ready to read full record
1002 rd[i].bufLen = rd[i].fadLen - rd[i].bufPos ;
1003
1004 int fadboard = ntohs(rd[i].rBuf->S[12] ) ;
1005 int fadcrate = fadboard/256 ;
1006 fadboard = (fadcrate*10 + fadboard%256) ;
1007snprintf(str,MXSTR,"sk %3d head: %5d %5d %5d %10d %4d %6d",i,rd[i].fadLen,rd[i].evtID,rd[i].ftmID,rd[i].runID,fadboard,jrd) ;
1008factOut(kDebug,1, str ) ;
1009
1010 if (rd[i].runID ==0 ) rd[i].runID = gi_myRun ;
1011
1012 if (rd[i].bufLen <=head_len || rd[i].bufLen > MAX_LEN ) {
1013 snprintf(str,MXSTR,"illegal event-length on port %d",i) ;
1014 factOut(kFatal,881, str ) ;
1015 rd[i].bufLen = 100000 ; //?
1016 }
1017 int fadBoard = ntohs(rd[i].rBuf->S[12] ) ;
1018 debugHead(i,fadBoard,rd[i].rBuf);
1019 debugRead(i,jrd,rd[i].evtID,rd[i].ftmID,rd[i].runID,-1,tsec,tusec) ; // i=socket; jrd=#bytes; ievt=eventid;-1=start event
1020 } else {
1021 debugRead(i,jrd,0,0,0,-2,tsec,tusec) ; // i=socket; jrd=#bytes; ievt=eventid; -2=start event, unknown id yet
1022 }
1023 } else {
1024 debugRead(i,jrd,0,0,0,-2,tsec,tusec) ; // i=socket; jrd=#bytes; ievt=eventid; -2=start event, unknown id yet
1025 }
1026
1027 } //end interpreting last read
1028 } //end of successful read anything
1029 } //finished trying to read all sockets
1030
1031snprintf(str,MXSTR,"Loop ---- %3d --- %8d",numokx,jrdx);
1032factOut(kDebug,-1, str ) ;
1033 gi.numRead[ numok ] ++ ;
1034
1035 g_actTime = time(NULL) ;
1036 if ( g_actTime > gi_SecTime ) {
1037 gi_SecTime = g_actTime ;
1038
1039
1040 //loop over all active events and flag those older than read-timeout
1041 //delete those that are written to disk ....
1042
1043 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr ;
1044 if ( kd < 0 ) kd+= (MAX_EVT*MAX_RUN) ;
1045
1046 gj.bufNew = gj.bufEvt = 0 ;
1047 int k1=evtCtrl.frstPtr;
1048 for ( k=k1; k<(k1+kd); k++ ) {
1049 int k0 = k % (MAX_EVT*MAX_RUN) ;
1050//would be better to use bitmaps for evtStat (allow '&' instead of multi-if)
1051
1052 if (evtCtrl.evtStat[k0] > 0
1053 && evtCtrl.evtStat[k0] < 92 ) {
1054 gj.bufNew++ ; //incomplete event in Buffer
1055 if ( evtCtrl.evtStat[k0] < 90
1056 && evtCtrl.pcTime[k0] < g_actTime-10 ) {
1057 int id =evtCtrl.evtBuf[k0] ;
1058 snprintf(str,MXSTR,"%5d skip short evt %8d %8d %2d",mBuffer[id].evNum,evtCtrl.evtBuf[k0],k0 ,evtCtrl.evtStat[k0]);
1059 factOut(kWarn,601, str ) ;
1060 evtCtrl.evtStat[k0] = 91 ; //timeout for incomplete events
1061 gi.evtSkp++ ;
1062 gi.evtTot++ ;
1063 gj.evtSkip++;
1064 }
1065 } else if (evtCtrl.evtStat[k0] >= 900 //'delete'
1066 || evtCtrl.evtStat[k0] == 0 ) { //'useless'
1067
1068 int id =evtCtrl.evtBuf[k0] ;
1069 snprintf(str,MXSTR,"%5d free event buffer, nb=%3d", mBuffer[id].evNum, mBuffer[id].nBoard ) ;
1070 factOut(kDebug,-1, str ) ;
1071 mBufFree(id) ; //event written--> free memory
1072 evtCtrl.evtStat[k0] = -1;
1073 gj.evtWrite++ ;
1074 gj.rateWrite++ ;
1075 } else if (evtCtrl.evtStat[k0] >= 95 ) {
1076 gj.bufEvt++ ; //complete event in Buffer
1077 }
1078
1079 if ( k0 == evtCtrl.frstPtr && evtCtrl.evtStat[k0] <0 ) {
1080 evtCtrl.frstPtr = (evtCtrl.frstPtr+1) % (MAX_EVT*MAX_RUN) ;
1081 }
1082 }
1083
1084
1085 gj.deltaT = 1000 ; //temporary, must be improved
1086
1087 int b;
1088 for ( b=0; b<NBOARDS; b++) gj.totBytes[b] +=gj.rateBytes[b] ;
1089 gj.totMem = g_maxMem ;
1090 if (gj.maxMem > gj.xxxMem) gj.xxxMem = gj.maxMem ;
1091 if (gj.maxEvt > gj.xxxEvt) gj.xxxEvt = gj.maxEvt ;
1092
1093 factStat(gj);
1094 factStatNew(gi) ;
1095 gj.rateNew = gj.rateWrite = 0 ;
1096 gj.maxMem = gj.usdMem ;
1097 gj.maxEvt = gj.bufTot ;
1098 for ( b=0; b<NBOARDS; b++) gj.rateBytes[b] =0 ;
1099 }
1100
1101 if (numok > 0 ) numok2=0;
1102 else if (numok2++ > 3) {
1103 if (g_runStat == 1) {
1104 xwait.tv_sec = 1;
1105 xwait.tv_nsec= 0 ; // hibernate for 1 sec
1106 } else {
1107 xwait.tv_sec = 0;
1108 xwait.tv_nsec= 2000000 ; // sleep for ~2 msec
1109 }
1110 nanosleep( &xwait , NULL ) ;
1111 }
1112
1113 } //and do next loop over all sockets ...
1114
1115
1116 snprintf(str,MXSTR,"stop reading ... RESET=%d",g_reset);
1117 factOut(kInfo,-1, str ) ;
1118
1119 if (g_reset >0 ) {
1120 gi_reset = g_reset ;
1121 gi_resetR = gi_reset%10 ; //shall we stop reading ?
1122 gi_resetS = (gi_reset/10)%10 ; //shall we close sockets ?
1123 gi_resetW = (gi_reset/100)%10 ; //shall we close files ?
1124 gi_resetX = gi_reset/1000 ; //shall we simply wait resetX seconds ?
1125 g_reset= 0;
1126 } else {
1127 gi_reset = 0;
1128 if ( g_runStat== -1 ) gi_resetR = 1 ;
1129 else gi_resetR = 7 ;
1130 gi_resetS = 7 ; //close all sockets
1131 gi_resetW = 7 ; //close all files
1132 gi_resetX = 0 ;
1133
1134 //inform others we have to quit ....
1135 gi_runStat = -11 ; //inform all that no update to happen any more
1136 gj.readStat= -11 ; //inform all that no update to happen any more
1137 }
1138
1139 if (gi_resetS > 0) {
1140 //must close all open sockets ...
1141 snprintf(str,MXSTR,"close all sockets ...");
1142 factOut(kInfo,-1, str ) ;
1143 for (i=0; i<MAX_SOCK; i++) {
1144 if (rd[i].sockStat ==0 ) {
1145 GenSock(-1, i, 0, NULL, &rd[i]) ; //close and destroy open socket
1146 if ( i%7 == 0 ) {
1147 gi_NumConnect[ i/7 ]-- ;
1148 gi.numConn[ i/7 ]-- ;
1149 gj.numConn[ i/7 ]-- ;
1150 sockDef[i/7] = 0 ; //flag ro recreate the sockets ...
1151 rd[i/7].sockStat = -1; //and try to open asap
1152 }
1153 }
1154 }
1155 }
1156
1157
1158 if (gi_resetR > 0) {
1159 //flag all events as 'read finished'
1160 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr ;
1161 if ( kd < 0 ) kd+= (MAX_EVT*MAX_RUN) ;
1162
1163 int k1=evtCtrl.frstPtr;
1164 for ( k=k1; k<(k1+kd); k++ ) {
1165 int k0 = k % (MAX_EVT*MAX_RUN) ;
1166 if (evtCtrl.evtStat[k0] > 0
1167 && evtCtrl.evtStat[k0] < 90 ) {
1168 evtCtrl.evtStat[k0] = 91 ;
1169 gi.evtSkp++ ;
1170 gi.evtTot++ ;
1171 }
1172 }
1173
1174 xwait.tv_sec = 0;
1175 xwait.tv_nsec= 2000000 ; // sleep for ~2 msec
1176 nanosleep( &xwait , NULL ) ;
1177
1178 //and clear all buffers (might have to wait until all others are done)
1179 int minclear ;
1180 if (gi_resetR == 1) {
1181 minclear = 900 ;
1182 snprintf(str,MXSTR,"drain all buffers ...");
1183 } else {
1184 minclear = 0 ;
1185 snprintf(str,MXSTR,"flush all buffers ...");
1186 }
1187 factOut(kInfo,-1, str ) ;
1188
1189 int numclear=1 ;
1190 while (numclear > 0 ) {
1191 numclear = 0 ;
1192 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr ;
1193 if ( kd < 0 ) kd+= (MAX_EVT*MAX_RUN) ;
1194
1195 int k1=evtCtrl.frstPtr;
1196 for ( k=k1; k<(k1+kd); k++ ) {
1197 int k0 = k % (MAX_EVT*MAX_RUN) ;
1198 if (evtCtrl.evtStat[k0] > minclear ) {
1199 int id =evtCtrl.evtBuf[k0] ;
1200 snprintf(str,MXSTR,"ev %5d free event buffer, nb=%3d", mBuffer[id].evNum, mBuffer[id].nBoard ) ;
1201 factOut(kDebug,-1, str ) ;
1202 mBufFree(id) ; //event written--> free memory
1203 evtCtrl.evtStat[k0] = -1;
1204 } else if (evtCtrl.evtStat[k0] > 0) numclear++ ; //writing is still ongoing...
1205
1206 if ( k0 == evtCtrl.frstPtr && evtCtrl.evtStat[k0] <0 )
1207 evtCtrl.frstPtr = (evtCtrl.frstPtr+1) % (MAX_EVT*MAX_RUN) ;
1208 }
1209
1210 xwait.tv_sec = 0;
1211 xwait.tv_nsec= 2000000 ; // sleep for ~2 msec
1212 nanosleep( &xwait , NULL ) ;
1213 }
1214 }
1215
1216 if (gi_reset > 0) {
1217 if (gi_resetW > 0) {
1218 CloseRunFile(0,0,0) ; //ask all Runs to be closed
1219 }
1220 if (gi_resetX > 0) {
1221 xwait.tv_sec = gi_resetX;
1222 xwait.tv_nsec= 0 ;
1223 nanosleep( &xwait , NULL ) ;
1224 }
1225
1226 snprintf(str,MXSTR,"Continue read Process ...");
1227 factOut(kInfo,-1, str ) ;
1228 gi_reset = 0 ;
1229 goto START ;
1230 }
1231
1232
1233
1234 snprintf(str,MXSTR,"Exit read Process ...");
1235 factOut(kInfo,-1, str ) ;
1236 gi_runStat = -99 ;
1237 gj.readStat= -99 ;
1238 factStat(gj);
1239 factStatNew(gi) ;
1240 return 0;
1241
1242} /*-----------------------------------------------------------------*/
1243
1244
1245void *procEvt( void *ptr ) {
1246/* *** main loop processing file, including SW-trigger */
1247 int numProc, numWait ;
1248 int k ;
1249 struct timespec xwait ;
1250 char str[MXSTR] ;
1251
1252 cpu_set_t mask;
1253 int cpu = 5 ; //process thread (will be several in final version)
1254
1255 snprintf(str,MXSTR,"Starting process-thread");
1256 factOut(kInfo,-1, str ) ;
1257
1258/* CPU_ZERO initializes all the bits in the mask to zero. */
1259 CPU_ZERO( &mask );
1260/* CPU_SET sets only the bit corresponding to cpu. */
1261 CPU_SET( cpu, &mask );
1262/* sched_setaffinity returns 0 in success */
1263 if ( sched_setaffinity( 0, sizeof(mask), &mask ) == -1 ) {
1264 snprintf(str,MXSTR,"P ---> can not create affinity to %d",cpu);
1265 factOut(kWarn,-1, str ) ;
1266 }
1267
1268
1269 while (g_runStat > -2) { //in case of 'exit' we still must process pending events
1270
1271 numWait = numProc = 0 ;
1272 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr ;
1273 if ( kd < 0 ) kd+= (MAX_EVT*MAX_RUN) ;
1274
1275 int k1=evtCtrl.frstPtr;
1276 for ( k=k1; k<(k1+kd); k++ ) {
1277 int k0 = k % (MAX_EVT*MAX_RUN) ;
1278//would be better to use bitmaps for evtStat (allow '&' instead of multi-if)
1279 if (evtCtrl.evtStat[k0] > 90 && evtCtrl.evtStat[k0] <500) {
1280
1281 if ( gi_resetR > 1 ) { //we are asked to flush buffers asap
1282 evtCtrl.evtStat[k0] = 991 ;
1283 } else {
1284
1285 int id = evtCtrl.evtBuf[k0] ;
1286 int ievt = mBuffer[id].evNum ;
1287 int roi = mBuffer[id].nRoi ;
1288// uint32_t irun = mBuffer[id].runNum ;
1289//snprintf(str,MXSTR,"P processing %d %d %d %d",ievt,k,id,evtCtrl.evtStat[k0]) ;
1290//factOut(kDebug,-1, str ) ;
1291
1292//make sure unused pixels/tmarks are cleared to zero
1293 int ip,it,dest,ib;
1294 for (ip=0; ip<NPIX; ip++) {
1295 if (mBuffer[id].fEvent->StartPix[ip] == -1 ) {
1296 dest= ip*roi ;
1297 bzero( &mBuffer[id].fEvent->Adc_Data[dest], roi*2) ;
1298 }
1299 }
1300 for (it=0; it<NTMARK; it++) {
1301 if (mBuffer[id].fEvent->StartTM[it] == -1 ) {
1302 dest= it*roi + NPIX*roi ;
1303 bzero( &mBuffer[id].fEvent->Adc_Data[dest], roi*2) ;
1304 }
1305 }
1306
1307
1308//and set correct event header ; also check for consistency in event (not yet)
1309 mBuffer[id].fEvent->Roi = roi ;
1310 mBuffer[id].fEvent->EventNum = ievt ;
1311 mBuffer[id].fEvent->TriggerType = 0 ; // TBD
1312 mBuffer[id].fEvent->SoftTrig = 0 ;
1313 for (ib=0; ib<NBOARDS; ib++) {
1314 if (mBuffer[id].board[ib] == -1 ) { //board is not read
1315 mBuffer[id].FADhead[ib].start_package_flag = 0 ;
1316 mBuffer[id].fEvent->BoardTime[ib] = 0 ;
1317 } else {
1318 mBuffer[id].fEvent->BoardTime[ib] =
1319 ntohl(mBuffer[id].FADhead[ib].time) ;
1320 }
1321 }
1322
1323 int i=eventCheck(mBuffer[id].FADhead,mBuffer[id].fEvent) ;
1324// gj.procEvt++ ;
1325 gi.procTot++ ;
1326 numProc++ ;
1327
1328 if (i<0) {
1329 evtCtrl.evtStat[k0] = 999 ; //flag event to be skipped
1330 gi.procErr++ ;
1331 } else {
1332 evtCtrl.evtStat[k0] = 520 ;
1333 }
1334 }
1335 } else if ( evtCtrl.evtStat[k0] >=0 && evtCtrl.evtStat[k0] < 90 ) {
1336 numWait++ ;
1337 }
1338 }
1339
1340 if ( gj.readStat < -10 && numWait == 0) { //nothing left to do
1341 snprintf(str,MXSTR,"Exit Processing Process ...");
1342 factOut(kInfo,-1, str ) ;
1343 gp_runStat = -22 ; //==> we should exit
1344 gj.procStat= -22 ; //==> we should exit
1345 return 0 ;
1346 }
1347
1348 if (numProc == 0) {
1349 //seems we have nothing to do, so sleep a little
1350 xwait.tv_sec = 0;
1351 xwait.tv_nsec= 2000000 ; // sleep for ~2 msec
1352 nanosleep( &xwait , NULL ) ;
1353 }
1354 gp_runStat = gi_runStat ;
1355 gj.procStat= gj.readStat ;
1356
1357 }
1358
1359 //we are asked to abort asap ==> must flag all remaining events
1360 // when gi_runStat claims that all events are in the buffer...
1361
1362 snprintf(str,MXSTR,"Abort Processing Process ...");
1363 factOut(kInfo,-1, str ) ;
1364 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr ;
1365 if ( kd < 0 ) kd+= (MAX_EVT*MAX_RUN) ;
1366
1367 int k1=evtCtrl.frstPtr;
1368 for ( k=k1; k<(k1+kd); k++ ) {
1369 int k0 = k % (MAX_EVT*MAX_RUN) ;
1370 if (evtCtrl.evtStat[k0] >=0 && evtCtrl.evtStat[k0] <500) {
1371 evtCtrl.evtStat[k0] = 555 ; //flag event as 'processed'
1372 }
1373 }
1374
1375 gp_runStat = -99 ;
1376 gj.procStat= -99 ;
1377
1378 return 0;
1379
1380} /*-----------------------------------------------------------------*/
1381
1382int CloseRunFile(uint32_t runId, uint32_t closeTime, uint32_t maxEvt) {
1383/* close run runId (all all runs if runId=0) */
1384/* return: 0=close scheduled / >0 already closed / <0 does not exist */
1385 int j ;
1386
1387
1388 if ( runId == 0 ) {
1389 for ( j=0; j<MAX_RUN; j++) {
1390 if ( runCtrl[j].fileId == 0 ) { //run is open
1391 runCtrl[j].closeTime = closeTime ;
1392 runCtrl[j].maxEvt = maxEvt ;
1393 }
1394 }
1395 return 0;
1396 }
1397
1398 for ( j=0; j<MAX_RUN; j++) {
1399 if ( runCtrl[j].runId == runId ) {
1400 if ( runCtrl[j].fileId == 0 ) { //run is open
1401 runCtrl[j].closeTime = closeTime ;
1402 runCtrl[j].maxEvt = maxEvt ;
1403 return 0;
1404 } else if ( runCtrl[j].fileId <0 ) { //run not yet opened
1405 runCtrl[j].closeTime = closeTime ;
1406 runCtrl[j].maxEvt = maxEvt ;
1407 return +1;
1408 } else { // run already closed
1409 return +2;
1410 }
1411 }
1412 } //we only reach here if the run was never created
1413 return -1;
1414
1415} /*-----------------------------------------------------------------*/
1416
1417
1418void *writeEvt( void *ptr ) {
1419/* *** main loop writing event (including opening and closing run-files */
1420
1421 int numWrite, numWait ;
1422 int k,j,i ;
1423 struct timespec xwait ;
1424 char str[MXSTR] ;
1425
1426 cpu_set_t mask;
1427 int cpu = 3 ; //write thread
1428
1429 snprintf(str,MXSTR,"Starting write-thread");
1430 factOut(kInfo,-1, str ) ;
1431
1432/* CPU_ZERO initializes all the bits in the mask to zero. */
1433 CPU_ZERO( &mask );
1434/* CPU_SET sets only the bit corresponding to cpu. */
1435 CPU_SET( cpu, &mask );
1436/* sched_setaffinity returns 0 in success */
1437 if ( sched_setaffinity( 0, sizeof(mask), &mask ) == -1 ) {
1438 snprintf(str,MXSTR,"W ---> can not create affinity to %d",cpu);
1439 }
1440
1441 int lastRun = 0 ; //usually run from last event still valid
1442
1443 while (g_runStat >-2) {
1444
1445 numWait = numWrite = 0 ;
1446 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr ;
1447 if ( kd < 0 ) kd+= (MAX_EVT*MAX_RUN) ;
1448
1449 int k1=evtCtrl.frstPtr;
1450 for ( k=k1; k<(k1+kd); k++ ) {
1451 int k0 = k % (MAX_EVT*MAX_RUN) ;
1452//would be better to use bitmaps for evtStat (allow '&' instead of multi-if)
1453 if (evtCtrl.evtStat[k0] > 500 && evtCtrl.evtStat[k0] < 900) {
1454
1455 if ( gi_resetR > 1 ) { //we must drain the buffer asap
1456 evtCtrl.evtStat[k0] = 904 ;
1457 } else {
1458
1459
1460 int id = evtCtrl.evtBuf[k0] ;
1461 uint32_t irun = mBuffer[id].runNum ;
1462 int ievt = mBuffer[id].evNum ;
1463
1464 gi.wrtTot++ ;
1465 if (runCtrl[lastRun].runId == irun ) {
1466 j = lastRun ;
1467 } else {
1468 //check which fileID to use (or open if needed)
1469 for ( j=0; j<MAX_RUN; j++) {
1470 if ( runCtrl[j].runId == irun ) break ;
1471 }
1472 if ( j >= MAX_RUN ) {
1473 snprintf(str,MXSTR,"W error: can not find run %d for event %d in %d", irun,ievt,id);
1474 factOut(kFatal,901, str ) ;
1475 gi.wrtErr++ ;
1476 }
1477 lastRun = j ;
1478 }
1479
1480 if (runCtrl[j].fileId < 0 ) {
1481 actRun.Version = 1 ;
1482 actRun.RunType = -1 ; //to be adapted
1483
1484 actRun.Nroi = runCtrl[j].roi0 ;
1485 actRun.NroiTM = runCtrl[j].roi8 ;
1486 if ( actRun.Nroi == actRun.NroiTM ) actRun.NroiTM = 0 ;
1487 actRun.RunTime = runCtrl[j].firstTime ;
1488 actRun.RunUsec = runCtrl[j].firstTime ;
1489 actRun.NBoard = NBOARDS ;
1490 actRun.NPix = NPIX ;
1491 actRun.NTm = NTMARK ;
1492 actRun.Nroi = mBuffer[id].nRoi ;
1493 memcpy(actRun.FADhead, mBuffer[id].FADhead, NBOARDS* sizeof(PEVNT_HEADER) ) ;
1494
1495 runCtrl[j].fileHd = runOpen(irun, &actRun, sizeof(actRun) ) ;
1496 if (runCtrl[j].fileHd == NULL ) {
1497 snprintf(str,MXSTR,"W could not open a file for run %d",irun);
1498 factOut(kError,502, str ) ;
1499 runCtrl[j].fileId = 91 ;
1500 } else {
1501 snprintf(str,MXSTR,"W opened new run_file %d evt %d",irun,ievt) ;
1502 factOut(kInfo,-1, str ) ;
1503 runCtrl[j].fileId = 0 ;
1504 }
1505
1506 }
1507
1508 if (runCtrl[j].fileId != 0 ) {
1509 if (runCtrl[j].fileId < 0 ) {
1510 snprintf(str,MXSTR,"W never opened file for this run %d",irun) ;
1511 factOut(kError,123,str) ;
1512 } else if (runCtrl[j].fileId < 100 ) {
1513 snprintf(str,MXSTR,"W.file for this run is closed %d",irun) ;
1514 factOut(kWarn,123,str) ;
1515 runCtrl[j].fileId += 100 ;
1516 } else {
1517 snprintf(str,MXSTR,"W:file for this run is closed %d",irun) ;
1518 factOut(kDebug,123,str) ;
1519 }
1520 evtCtrl.evtStat[k0] = 903 ;
1521 gi.wrtErr++ ;
1522 } else {
1523 i=runWrite(runCtrl[j].fileHd, mBuffer[id].fEvent, sizeof(mBuffer[id]) );
1524 if ( i>=0 ) {
1525 runCtrl[j].lastTime = g_actTime;
1526 runCtrl[j].actEvt++ ;
1527 evtCtrl.evtStat[k0] = 901 ;
1528 snprintf(str,MXSTR,"%5d successfully wrote for run %d id %5d",ievt,irun,k0);
1529 factOut(kDebug,504, str ) ;
1530// gj.writEvt++ ;
1531 } else {
1532 snprintf(str,MXSTR,"W error writing event for run %d",irun) ;
1533 factOut(kError,503, str ) ;
1534 evtCtrl.evtStat[k0] = 902 ;
1535 gi.wrtErr++ ;
1536 }
1537
1538 if ( i < 0
1539 || runCtrl[j].lastTime < g_actTime-300
1540 || runCtrl[j].closeTime < g_actTime
1541 || runCtrl[j].maxEvt < runCtrl[j].actEvt ) {
1542int ii =0 ;
1543if ( i < 0 ) ii=1 ;
1544else if (runCtrl[j].closeTime < g_actTime ) ii=2 ;
1545else if (runCtrl[j].lastTime < g_actTime-300 ) ii=3 ;
1546else if (runCtrl[j].maxEvt < runCtrl[j].actEvt ) ii=4 ;
1547
1548
1549
1550 //close run for whatever reason
1551 if (runCtrl[j].runId == gi_myRun) gi_myRun = g_actTime ;
1552 i=runClose(runCtrl[j].fileHd, &runTail[j], sizeof(runTail[j]) );
1553 if (i<0) {
1554 snprintf(str,MXSTR,"error closing run %d %d AAA",runCtrl[j].runId,i) ;
1555 factOut(kError,503, str ) ;
1556 runCtrl[j].fileId = 92 ;
1557 } else {
1558 snprintf(str,MXSTR,"W closed run %d AAA %d",irun,ii) ;
1559 factOut(kInfo,503, str ) ;
1560 runCtrl[j].fileId = 93 ;
1561 }
1562 }
1563 }
1564 }
1565 } else if (evtCtrl.evtStat[k0] > 0
1566 && evtCtrl.evtStat[k0] < 900 ) numWait++ ;
1567 }
1568
1569 //check if we should close a run (mainly when no event pending)
1570 for ( j=0; j<MAX_RUN; j++) {
1571 if ( runCtrl[j].fileId==0
1572 && ( runCtrl[j].closeTime < g_actTime
1573 ||runCtrl[j].lastTime < g_actTime-300
1574 ||runCtrl[j].maxEvt < runCtrl[j].actEvt ) ) {
1575 if (runCtrl[j].runId == gi_myRun) gi_myRun = g_actTime ;
1576int ii =0 ;
1577 if (runCtrl[j].closeTime < g_actTime ) ii=2 ;
1578else if (runCtrl[j].lastTime < g_actTime-300 ) ii=3 ;
1579else if (runCtrl[j].maxEvt < runCtrl[j].actEvt ) ii=4 ;
1580
1581
1582 i=runClose(runCtrl[j].fileHd, &runTail[j], sizeof(runTail[j]) );
1583 if (i<0) {
1584 snprintf(str,MXSTR,"error closing run %d %d BBB",runCtrl[j].runId,i) ;
1585 factOut(kError,506, str ) ;
1586 runCtrl[j].fileId = 94 ;
1587 } else {
1588 snprintf(str,MXSTR,"W closed run %d BBB %d",runCtrl[j].runId,ii) ;
1589 factOut(kInfo,507, str ) ;
1590 runCtrl[j].fileId = 95 ;
1591 }
1592 }
1593 }
1594
1595 if (numWrite == 0) {
1596 //seems we have nothing to do, so sleep a little
1597 xwait.tv_sec = 0;
1598 xwait.tv_nsec= 2000000 ; // sleep for ~2 msec
1599 nanosleep( &xwait , NULL ) ;
1600 }
1601
1602 if ( gj.readStat < -10 && numWait == 0) { //nothing left to do
1603 snprintf(str,MXSTR,"Finish Write Process ...");
1604 factOut(kInfo,-1, str ) ;
1605 gw_runStat = -22 ; //==> we should exit
1606 gj.writStat= -22 ; //==> we should exit
1607 goto closerun ;
1608 }
1609 gw_runStat = gi_runStat ;
1610 gj.writStat= gj.readStat ;
1611
1612 }
1613
1614 //must close all open files ....
1615 snprintf(str,MXSTR,"Abort Writing Process ...");
1616 factOut(kInfo,-1, str ) ;
1617
1618closerun:
1619 snprintf(str,MXSTR,"Close all open files ...");
1620 factOut(kInfo,-1, str ) ;
1621 for ( j=0; j<MAX_RUN; j++)
1622 if ( runCtrl[j].fileId ==0 ) {
1623 if (runCtrl[j].runId == gi_myRun) gi_myRun = g_actTime ;
1624 i=runClose(runCtrl[j].fileHd, &runTail[j], sizeof(runTail[j]) );
1625int ii =0 ;
1626 if (runCtrl[j].closeTime < g_actTime ) ii=2 ;
1627else if (runCtrl[j].lastTime < g_actTime-300 ) ii=3 ;
1628else if (runCtrl[j].maxEvt < runCtrl[j].actEvt ) ii=4 ;
1629 if (i<0) {
1630 snprintf(str,MXSTR,"error closing run %d %d CCC",runCtrl[j].runId,i) ;
1631 factOut(kError,506, str ) ;
1632 runCtrl[j].fileId = 96 ;
1633 } else {
1634 snprintf(str,MXSTR,"W closed run %d CCC %d",runCtrl[j].runId,ii) ;
1635 factOut(kInfo,507, str ) ;
1636 runCtrl[j].fileId = 97 ;
1637 }
1638 }
1639
1640 gw_runStat = -99;
1641 gj.writStat= -99;
1642 snprintf(str,MXSTR,"Exit Writing Process ...");
1643 factOut(kInfo,-1, str ) ;
1644 return 0;
1645
1646
1647
1648
1649} /*-----------------------------------------------------------------*/
1650
1651
1652
1653
1654void StartEvtBuild() {
1655
1656 int i,j,imax,status,th_ret[50] ;
1657 pthread_t thread[50] ;
1658 struct timespec xwait ;
1659
1660 gi_runStat = gp_runStat = gw_runStat = 0 ;
1661 gj.readStat= gj.procStat= gj.writStat= 0 ;
1662
1663 snprintf(str,MXSTR,"Starting EventBuilder V15.07 A");
1664 factOut(kInfo,-1, str ) ;
1665
1666//initialize run control logics
1667 for (i=0; i<MAX_RUN; i++) {
1668 runCtrl[i].runId = 0 ;
1669 runCtrl[i].fileId = -2 ;
1670 }
1671
1672//partially initialize event control logics
1673 evtCtrl.frstPtr = 0 ;
1674 evtCtrl.lastPtr = 0 ;
1675
1676//start all threads (more to come) when we are allowed to ....
1677 while (g_runStat == 0 ) {
1678 xwait.tv_sec = 0;
1679 xwait.tv_nsec= 10000000 ; // sleep for ~10 msec
1680 nanosleep( &xwait , NULL ) ;
1681 }
1682
1683 i=0 ;
1684 th_ret[i] = pthread_create( &thread[i], NULL, readFAD, NULL );
1685 i++;
1686 th_ret[i] = pthread_create( &thread[i], NULL, procEvt, NULL );
1687 i++;
1688 th_ret[i] = pthread_create( &thread[i], NULL, writeEvt, NULL );
1689 i++;
1690 imax=i ;
1691
1692
1693#ifdef BILAND
1694 xwait.tv_sec = 30;;
1695 xwait.tv_nsec= 0 ; // sleep for ~20sec
1696 nanosleep( &xwait , NULL ) ;
1697
1698 printf("close all runs in 2 seconds\n");
1699
1700 CloseRunFile( 0, time(NULL)+2, 0) ;
1701
1702 xwait.tv_sec = 1;;
1703 xwait.tv_nsec= 0 ; // sleep for ~20sec
1704 nanosleep( &xwait , NULL ) ;
1705
1706 printf("setting g_runstat to -1\n");
1707
1708 g_runStat = -1 ;
1709#endif
1710
1711
1712//wait for all threads to finish
1713 for (i=0; i<imax; i++) {
1714 j = pthread_join ( thread[i], (void **)&status) ;
1715 }
1716
1717} /*-----------------------------------------------------------------*/
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733 /*-----------------------------------------------------------------*/
1734 /*-----------------------------------------------------------------*/
1735 /*-----------------------------------------------------------------*/
1736 /*-----------------------------------------------------------------*/
1737 /*-----------------------------------------------------------------*/
1738
1739
1740#ifdef BILAND
1741
1742
1743 /*-----------------------------------------------------------------*/
1744 /*-----------------------------------------------------------------*/
1745 /*-----------------------------------------------------------------*/
1746 /*-----------------------------------------------------------------*/
1747 /*-----------------------------------------------------------------*/
1748
1749
1750
1751
1752FileHandle_t runOpen(uint32_t irun, RUN_HEAD *runhd, size_t len )
1753{ return 1; } ;
1754
1755int runWrite(FileHandle_t fileHd , EVENT *event, size_t len )
1756{ return 1; usleep(10000); return 1; }
1757
1758
1759//{ return 1; } ;
1760
1761int runClose(FileHandle_t fileHd , RUN_TAIL *runth, size_t len )
1762{ return 1; } ;
1763
1764
1765
1766int eventCheck( PEVNT_HEADER *fadhd, EVENT *event)
1767{
1768 int i=0;
1769
1770// printf("------------%d\n",ntohl(fadhd[7].fad_evt_counter) );
1771// for (i=0; i<NBOARDS; i++) {
1772// printf("b=%2d,=%5d\n",i,fadhd[i].board_id);
1773// }
1774 return 0;
1775}
1776
1777
1778void factStatNew(EVT_STAT gi) {
1779 int i ;
1780
1781//for (i=0;i<MAX_SOCK;i++) {
1782// printf("%4d",gi.numRead[i]);
1783// if (i%20 == 0 ) printf("\n");
1784//}
1785}
1786
1787void gotNewRun( int runnr, PEVNT_HEADER *headers )
1788{ printf("got new run %d\n",runnr); return; }
1789
1790void factStat(GUI_STAT gj) {
1791// printf("stat: bfr%5lu skp%4lu free%4lu (tot%7lu) mem%12lu rd%12lu %3lu\n",
1792// array[0],array[1],array[2],array[3],array[4],array[5],array[6]);
1793}
1794
1795
1796void debugRead(int isock, int ibyte, int32_t event, int32_t ftmevt, int32_t runnr, int state, uint32_t tsec, uint32_t tusec ) {
1797// printf("%3d %5d %9d %3d %12d\n",isock, ibyte, event, state, tusec) ;
1798}
1799
1800
1801
1802void debugStream(int isock, void *buf, int len) {
1803}
1804
1805void debugHead(int i, int j, void *buf) {
1806}
1807
1808
1809void factOut(int severity, int err, char* message ) {
1810static FILE * fd ;
1811static int file=0 ;
1812
1813 if (file==0) {
1814 printf("open file\n");
1815 fd=fopen("x.out","w+") ;
1816 file=999;
1817 }
1818
1819 fprintf(fd,"%3d %3d | %s \n",severity,err,message) ;
1820
1821 if (severity != kDebug)
1822 printf("%3d %3d | %s\n",severity,err,message) ;
1823}
1824
1825
1826
1827int main() {
1828 int i,b,c,p ;
1829 char ipStr[100] ;
1830 struct in_addr IPaddr ;
1831
1832 g_maxMem = 1024*1024 ; //MBytes
1833//g_maxMem = g_maxMem * 1024 *10 ; //10GBytes
1834 g_maxMem = g_maxMem * 200; //100MBytes
1835
1836
1837 g_runStat = 40 ;
1838
1839 i=0 ;
1840
1841// version for standard crates
1842//for (c=0; c<4,c++) {
1843// for (b=0; b<10; b++) {
1844// sprintf(ipStr,"10.0.%d.%d",128+c,128+b)
1845//
1846// inet_pton(PF_INET, ipStr, &IPaddr) ;
1847//
1848// g_port[i].sockAddr.sin_family = PF_INET;
1849// g_port[i].sockAddr.sin_port = htons(5000) ;
1850// g_port[i].sockAddr.sin_addr = IPaddr ;
1851// g_port[i].sockDef = 1 ;
1852// i++ ;
1853// }
1854//}
1855//
1856//version for PC-test *
1857 for (c=0; c<4; c++) {
1858 for (b=0; b<10; b++) {
1859 sprintf(ipStr,"10.0.%d.11",128+c) ;
1860 if (c<2) sprintf(ipStr,"10.0.%d.11",128) ;
1861 else sprintf(ipStr,"10.0.%d.11",131) ;
1862// if (c==0) sprintf(ipStr,"10.0.100.11") ;
1863
1864 inet_pton(PF_INET, ipStr, &IPaddr) ;
1865 p = 31919+100*c+10*b;
1866
1867
1868 g_port[i].sockAddr.sin_family = PF_INET;
1869 g_port[i].sockAddr.sin_port = htons(p) ;
1870 g_port[i].sockAddr.sin_addr = IPaddr ;
1871 g_port[i].sockDef = 1 ;
1872
1873 i++ ;
1874 }
1875 }
1876
1877
1878//g_port[17].sockDef =-1 ;
1879//g_actBoards-- ;
1880
1881 StartEvtBuild() ;
1882
1883 return 0;
1884
1885}
1886#endif
Note: See TracBrowser for help on using the repository browser.