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

Last change on this file since 11525 was 11521, checked in by tbretz, 13 years ago
folgende bug-fixes sind drin: - gotNewRun sollte jetzt immer sofort kommen - usdMem / maxMem sollte jetzt richtig gesetzt werden - open run 0 sollte nicht mehr auftreten (fuer das erste event) Neu implementiert: - falsche ROI werden nicht mehr akzeptiert; die ROI wird jeweils festgelegt durch das erste gelesenen board des ersten events eines neuen runs. Muss gelten: die ersten 8 pixel muessen identische ROI haben, Pixel 9 muss roi >= roi0 sein ( > wenn noch TM info dabei ist) mehrere zaehler werden neu an factStat uebergeben: int32_t badRoiE; //# boards with wrong roi (inconsistent in event) int32_t badRoiR; //# boards with wrong roi (inconsistent in run) int32_t badRoiB; //# boards with wrong roi (inconsistent in board) int32_t badRoi[NBOARDS]; //# wrong roi per board - RUN_HEAD enthaelt neu: uint16_t NroiTM; // <=0 wenn keine TM info da ist // (aber in event-structure ist leere TM drin...) - EVENT enthaelt neu: uint32_t NumBoards ; // number of active boards included // <40 fuer incomplete events
File size: 57.9 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.rateNew++ ;
470
471 //register event in 'active list (reading)'
472
473 evtCtrl.evtBuf[ evtCtrl.lastPtr] = i ;
474 evtCtrl.evtStat[ evtCtrl.lastPtr] = 0 ;
475 evtCtrl.pcTime[ evtCtrl.lastPtr] = g_actTime ;
476 evtIdx[i] = evtCtrl.lastPtr ;
477
478
479 snprintf(str,MXSTR,"%5d %8d start new evt %8d %8d sock %3d len %5d t %10d",
480 evID,runID,i,evtCtrl.lastPtr,sk,fadlen,mBuffer[i].pcTime);
481 factOut(kDebug,-11, str ) ;
482 evtCtrl.lastPtr++ ;
483 if (evtCtrl.lastPtr == MAX_EVT*MAX_RUN ) evtCtrl.lastPtr = 0;
484
485 gi.evtGet++ ;
486
487 return i ;
488
489} /*-----------------------------------------------------------------*/
490
491
492
493
494int mBufFree(int i) {
495//delete entry [i] from mBuffer:
496//(and make sure multiple calls do no harm ....)
497
498 int headmem=0 ;
499 int evid ;
500 size_t freemem = 0 ;
501
502 evid = mBuffer[i].evNum ;
503 freemem = mBuffer[i].evtLen ;
504
505 free(mBuffer[i].fEvent ) ;
506 mBuffer[i].fEvent = NULL ;
507
508 free(mBuffer[i].FADhead ) ;
509 mBuffer[i].FADhead = NULL ;
510
511 headmem = NBOARDS* sizeof(PEVNT_HEADER) ;
512 mBuffer[i].evNum = mBuffer[i].nRoi= -1;
513 mBuffer[i].runNum = 0;
514
515 gj.usdMem = gj.usdMem - freemem - headmem;
516
517 if (gi_memStat < 0 ) {
518 if (gj.usdMem <= 0.75 * gj.maxMem ) gi_memStat = +1 ;
519 }
520
521
522 return 0 ;
523
524} /*-----------------------------------------------------------------*/
525
526
527void resetEvtStat() {
528 int i ;
529
530 for (i=0; i<MAX_SOCK; i++) gi.numRead[i] = 0 ;
531
532 for (i=0; i<NBOARDS; i++ ) {
533 gi.gotByte[i] = 0 ;
534 gi.gotErr[i] = 0 ;
535
536 }
537
538 gi.evtGet = 0 ; //#new Start of Events read
539 gi.evtTot = 0 ; //#complete Events read
540 gi.evtErr = 0 ; //#Events with Errors
541 gi.evtSkp = 0 ; //#Events incomplete (timeout)
542
543 gi.procTot = 0 ; //#Events processed
544 gi.procErr = 0 ; //#Events showed problem in processing
545 gi.procTrg = 0 ; //#Events accepted by SW trigger
546 gi.procSkp = 0 ; //#Events rejected by SW trigger
547
548 gi.feedTot = 0 ; //#Events used for feedBack system
549 gi.feedErr = 0 ; //#Events rejected by feedBack
550
551 gi.wrtTot = 0 ; //#Events written to disk
552 gi.wrtErr = 0 ; //#Events with write-error
553
554 gi.runOpen = 0 ; //#Runs opened
555 gi.runClose= 0 ; //#Runs closed
556 gi.runErr = 0 ; //#Runs with open/close errors
557
558return ;
559} /*-----------------------------------------------------------------*/
560
561
562
563void initReadFAD() {
564return ;
565} /*-----------------------------------------------------------------*/
566
567
568
569void *readFAD( void *ptr ) {
570/* *** main loop reading FAD data and sorting them to complete events */
571 int head_len,frst_len,numok,numok2,numokx,dest,evID,i,k ;
572 int actBoards = 0, minLen ;
573 int32_t jrd ;
574 uint gi_SecTime ; //time in seconds
575 int boardId, roi[8],drs,px,src,pixS,pixH,pixC,pixR,tmS ;
576
577 int goodhed = 0 ;
578 int errcnt0 = 0 ;
579
580 int sockDef[NBOARDS]; //internal state of sockets
581 int jrdx ;
582
583
584 struct timespec xwait ;
585
586
587 struct timeval *tv, atv;
588 tv=&atv;
589 uint32_t tsec, tusec ;
590
591
592 snprintf(str,MXSTR,"start initializing");
593 factOut(kInfo,-1, str ) ;
594
595 int cpu = 7 ; //read thread
596 cpu_set_t mask;
597
598/* CPU_ZERO initializes all the bits in the mask to zero. */
599 CPU_ZERO( &mask );
600/* CPU_SET sets only the bit corresponding to cpu. */
601 cpu = 7 ;
602 CPU_SET( cpu, &mask );
603
604/* sched_setaffinity returns 0 in success */
605 if ( sched_setaffinity( 0, sizeof(mask), &mask ) == -1 ) {
606 snprintf(str,MXSTR,"W ---> can not create affinity to %d",cpu);
607 factOut(kWarn,-1, str ) ;
608 }
609
610 head_len = sizeof(PEVNT_HEADER) ;
611 frst_len = head_len ; //max #bytes to read first: fad_header only, so each event must be longer, even for roi=0
612 minLen = head_len ; //min #bytes needed to check header: full header for debug
613
614 start.S=0xFB01;
615 stop.S= 0x04FE;
616
617/* initialize run control logics */
618 for (i=0; i<MAX_RUN; i++) {
619 runCtrl[i].runId = 0 ;
620 runCtrl[i].fileId = -2 ;
621 }
622 gi_resetS = gi_resetR = 9;
623 for (i=0; i<NBOARDS; i++) sockDef[i]= 0 ;
624
625START:
626 gettimeofday( tv, NULL);
627 g_actTime = tsec = atv.tv_sec ;
628 g_actUsec = tusec= atv.tv_usec ;
629 gi_myRun = g_actTime ;
630 evtCtrl.frstPtr = 0 ;
631 evtCtrl.lastPtr = 0 ;
632
633 gi_SecTime = g_actTime ;
634 gi_runStat = g_runStat ;
635 gj.readStat= g_runStat ;
636 numok = numok2 = 0 ;
637
638 if ( gi_resetS > 0) {
639 //make sure all sockets are preallocated as 'not exist'
640 for (i=0; i<MAX_SOCK; i++) {
641 rd[i].socket = -1 ;
642 rd[i].sockStat = 99 ;
643 }
644
645 for (k=0; k<NBOARDS; k++) {
646 gi_NumConnect[k]=0;
647 gi.numConn[k] =0;
648 gj.numConn[k] =0;
649 gj.errConn[k] =0;
650 gj.rateBytes[k] =0;
651 gj.totBytes[k] =0;
652 }
653
654 }
655
656
657 if ( gi_resetR > 0) {
658 resetEvtStat();
659 gj.usdMem = gj.maxMem = gj.xxxMem = 0 ;
660 gj.totMem = g_maxMem ;
661 gj.bufNew = gj.bufEvt = 0 ;
662 gj.badRoiE = gj.badRoiR = gj.badRoiB =
663 gj.evtSkip = gj.evtWrite = gj.evtErr = 0 ;
664
665 int b ;
666 for (b=0; b<NBOARDS; b++) gj.badRoi[b]=0 ;
667
668 mBufInit() ; //initialize buffers
669
670 snprintf(str,MXSTR,"end initializing");
671 factOut(kInfo,-1, str ) ;
672 }
673
674
675 gi_reset = gi_resetR = gi_resetS = gi_resetW = 0 ;
676
677 while (g_runStat >=0 && g_reset ==0 ) { //loop until global variable g_runStat claims stop
678
679 gi_runStat = g_runStat;
680 gj.readStat= g_runStat;
681 gettimeofday( tv, NULL);
682 g_actTime = tsec = atv.tv_sec ;
683 g_actUsec = tusec= atv.tv_usec ;
684
685
686 int b,p,p0,s0,nch;
687 nch = 0 ;
688 for (b=0; b<NBOARDS; b++ ) {
689 k = b*7 ;
690 if ( g_port[b].sockDef != sockDef[b] ) { //something has changed ...
691 nch++ ;
692 gi_NumConnect[ b ] = 0 ; //must close all connections
693 gi.numConn[ b ] = 0;
694 gj.numConn[ b ] = 0;
695 if ( sockDef[b] == 0) s0= 0 ; //sockets to be defined and opened
696 else if (g_port[b].sockDef == 0) s0= -1 ; //sockets to be destroyed
697 else s0= +1 ; //sockets to be closed and reopened
698
699 if (s0 == 0) p0=ntohs(g_port[b].sockAddr.sin_port);
700 else p0=0 ;
701
702 for (p=p0+1; p<p0+8; p++) {
703 GenSock(s0, k, p, &g_port[b].sockAddr, &rd[k]) ; //generate address and socket
704 k++ ;
705 }
706 sockDef[b] = g_port[b].sockDef ;
707 }
708 }
709
710 if (nch > 0 ) {
711 actBoards = 0 ;
712 for (b=0; b<NBOARDS; b++ ) {
713 if ( sockDef[b] > 0 ) actBoards++ ;
714 }
715 }
716
717
718 jrdx = 0;
719 numokx= 0;
720 numok = 0 ; //count number of succesfull actions
721
722 for (i=0; i<MAX_SOCK; i++) { //check all sockets if something to read
723 b = i / 7 ;
724 if (sockDef[b] > 0) s0= +1 ;
725 else s0= -1 ;
726
727 if (rd[i].sockStat <0 ) { //try to connect if not yet done
728 rd[i].sockStat=connect(rd[i].socket,
729 (struct sockaddr*) &rd[i].SockAddr, sizeof(rd[i].SockAddr)) ;
730 if (rd[i].sockStat ==0 ) { //successfull ==>
731 if (sockDef[b] > 0) {
732 rd[i].bufTyp = 0 ; // expect a header
733 rd[i].bufLen = frst_len ; // max size to read at begining
734 } else {
735 rd[i].bufTyp = -1 ; // full data to be skipped
736 rd[i].bufLen = MAX_LEN ; //huge for skipping
737 }
738 rd[i].bufPos = 0 ; // no byte read so far
739 rd[i].skip = 0 ; // start empty
740 gi_NumConnect[ b ]++ ;
741 gi.numConn[ b ]++ ;
742 gj.numConn[ b ]++ ;
743 snprintf(str,MXSTR,"+++connect %d %d",b,gi.numConn[ b ]);
744 factOut(kInfo,-1, str ) ;
745 }
746 }
747
748 if (rd[i].sockStat ==0 ) { //we have a connection ==> try to read
749 if (rd[i].bufLen > 0) { //might be nothing to read [buffer full]
750 numok++ ;
751 jrd=recv(rd[i].socket,&rd[i].rBuf->B[ rd[i].bufPos], rd[i].bufLen, MSG_DONTWAIT);
752
753 if (jrd >0 ) {
754 debugStream(i,&rd[i].rBuf->B[ rd[i].bufPos],jrd) ;
755 memcpy(&rd[i].xBuf->B[ rd[i].bufPos], &rd[i].rBuf->B[ rd[i].bufPos], jrd) ;
756 snprintf(str,MXSTR,"read sock %3d bytes %5d len %5d first %d %d",i,jrd,rd[i].bufLen,
757 rd[i].rBuf->B[ rd[i].bufPos ],
758 rd[i].rBuf->B[ rd[i].bufPos +1] );
759 factOut(kDebug,301, str ) ;
760 }
761
762 if (jrd == 0) { //connection has closed ...
763 snprintf(str,MXSTR,"Socket %d closed by FAD",i);
764 factOut(kInfo,441, str ) ;
765 GenSock(s0, i, 0,NULL, &rd[i]) ;
766 gi.gotErr[ b ]++ ;
767 gi_NumConnect[ b ]-- ;
768 gi.numConn[ b ]-- ;
769 gj.numConn[ b ]-- ;
770
771 } else if ( jrd<0 ) { //did not read anything
772 if (errno != EAGAIN && errno != EWOULDBLOCK ) {
773 snprintf(str,MXSTR,"Error Reading from %d | %m",i);
774 factOut(kError,442, str ) ;
775 gi.gotErr[ b ]++ ;
776 } else numok-- ; //else nothing waiting to be read
777 jrd = 0 ;
778 }
779 } else {
780 jrd = 0 ; //did read nothing as requested
781 snprintf(str,MXSTR,"do not read from socket %d %d",i,rd[i].bufLen ) ;
782 factOut(kDebug,301, str ) ;
783 }
784
785 gi.gotByte[ b ] += jrd ;
786 gj.rateBytes[b] += jrd ;
787
788 if (jrd>0) {numokx++ ; jrdx+= jrd; }
789
790
791 if ( rd[i].bufTyp <0 ) { // we are skipping this board ...
792// just do nothing
793
794 } else if ( rd[i].bufTyp >0 ) { // we are reading data ...
795 if ( jrd < rd[i].bufLen ) { //not yet all read
796 rd[i].bufPos += jrd ; //==> prepare for continuation
797 rd[i].bufLen -= jrd ;
798 debugRead(i,jrd,rd[i].evtID,rd[i].ftmID,rd[i].runID, 0,tsec,tusec) ; // i=socket; jrd=#bytes; ievt=eventid; 0=reading data
799 } else { //full dataset read
800 rd[i].bufLen = 0 ;
801 rd[i].bufPos = rd[i].fadLen ;
802 if ( rd[i].rBuf->B[ rd[i].bufPos-1] != stop.B[0]
803 || rd[i].rBuf->B[ rd[i].bufPos-2] != stop.B[1]) {
804 gi.evtErr++ ;
805 snprintf(str,MXSTR,"wrong end of buffer found sock %3d ev %4d len %5d %3d %3d - %3d %3d ",
806 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],
807 rd[i].rBuf->B[ rd[i].bufPos-1]);
808 factOut(kError,301, str ) ;
809 goto EndBuf ;
810
811// } else {
812// snprintf(str,MXSTR,"good end of buffer found sock %3d len %5d %d %d : %d %d - %d %d : %d %d",
813// i,rd[i].fadLen,
814// rd[i].rBuf->B[ 0 ], rd[i].rBuf->B[ 1 ], start.B[1],start.B[0],
815// rd[i].rBuf->B[ rd[i].bufPos-2], rd[i].rBuf->B[ rd[i].bufPos-1], stop.B[1], stop.B[0]);
816// factOut(kDebug,301, str ) ;
817 }
818
819 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
820
821 //we have a complete buffer, copy to WORK area
822 int jr ;
823 roi[0] = ntohs(rd[i].rBuf->S[ head_len/2 + 2 ]) ;
824 for (jr=0; jr<9; jr++) {
825 roi[jr] = ntohs(rd[i].rBuf->S[ head_len/2 + 2 + jr*(roi[0]+4) ]) ;
826 }
827 //get index into mBuffer for this event (create if needed)
828 evID = mBufEvt( rd[i].evtID, rd[i].runID, roi, i, rd[i].fadLen ) ;
829
830 if (evID <-1000) {
831 goto EndBuf ; //not usable board/event/run --> skip it
832 }
833 if (evID < 0) { //no space left, retry later
834
835 if ( rd[i].bufLen != 0) {
836 snprintf(str,MXSTR,"something screwed up");
837 factOut(kFatal, 1, str ) ;
838 }
839 xwait.tv_sec = 0;
840 xwait.tv_nsec= 10000000 ; // sleep for ~10 msec
841 nanosleep( &xwait , NULL ) ;
842 goto EndBuf1 ; //hope there is free space next round
843 }
844
845
846 //we have a valid entry in mBuffer[]; fill it
847
848int xchk = memcmp(&rd[i].xBuf->B[0], &rd[i].rBuf->B[0], rd[i].fadLen ) ;
849if (xchk != 0) {
850 snprintf(str,MXSTR,"ERROR OVERWRITE %d %d on port %d",xchk,rd[i].fadLen,i) ;
851 factOut(kFatal, 1, str ) ;
852
853 uint iq;
854 for (iq=0; iq < rd[i].fadLen ; iq++) {
855 if (rd[i].rBuf->B[iq] != rd[i].xBuf->B[iq] ) {
856 snprintf(str,MXSTR,"ERROR %4d %4d %x %x",i,iq,rd[i].rBuf->B[iq], rd[i].xBuf->B[iq]);
857 factOut(kFatal, 1, str ) ;
858 }
859 }
860}
861
862
863 boardId = b ;
864 int fadBoard = ntohs(rd[i].rBuf->S[12] ) ;
865 int fadCrate = fadBoard/256 ;
866 if (boardId != (fadCrate*10 + fadBoard%256) ) {
867 snprintf(str,MXSTR,"wrong Board ID %d %d %d",fadCrate,fadBoard%256,boardId) ;
868 if (errcnt0++ < 99 ) factOut(kWarn,301, str ) ; //print only few times
869 }
870 if ( mBuffer[evID].board[ boardId ] != -1) {
871 snprintf(str,MXSTR,"double board: ev %5d, b %3d, %3d; len %5d %3d %3d - %3d %3d ",
872 evID,boardId,i,rd[i].fadLen, rd[i].rBuf->B[0],
873 rd[i].rBuf->B[1], rd[i].rBuf->B[ rd[i].bufPos-2], rd[i].rBuf->B[ rd[i].bufPos-1]);
874 factOut(kWarn,501, str ) ;
875 goto EndBuf ; //--> skip Board
876 }
877
878 int iDx = evtIdx[evID] ; //index into evtCtrl
879
880 memcpy( &mBuffer[evID].FADhead[boardId].start_package_flag,
881 &rd[i].rBuf->S[0], head_len) ;
882// xxx roi = mBuffer[evID].nRoi ;
883
884 src = head_len/2 ;
885 for ( px=0; px<9; px++ ) { //different sort in FAD board.....
886 for ( drs=0; drs<4; drs++ ) {
887 pixH= ntohs(rd[i].rBuf->S[src++]) ; // ID
888 pixC= ntohs(rd[i].rBuf->S[src++]) ; // start-cell
889 pixR= ntohs(rd[i].rBuf->S[src++]) ; // roi
890//here we should check if pixH is correct ....
891
892 pixS = boardId*36 + drs*9 + px ;
893 src++ ;
894
895// xxx if ( ( px != 8 && pixR == roi )
896// xxx || ( px == 8 && pixR >= roi ) ) { //we have a reasonable roi
897
898 mBuffer[evID].fEvent->StartPix[pixS] =pixC;
899 dest= pixS * roi[0] ;
900 memcpy(
901 &mBuffer[evID].fEvent->Adc_Data[dest],
902 &rd[i].rBuf->S[src], roi[0] * 2) ;
903 src+= pixR ;
904
905 if ( px==8 ) {
906 if ( pixR > roi[0]) { //and we have additional TM info
907 tmS =boardId*4 + drs ;
908 dest= tmS * roi[0] + NPIX* roi[0] ;
909 int srcT= src - roi[0] ;
910 mBuffer[evID].fEvent->StartTM[tmS] = (pixC+pixR-roi[0])%1024 ;
911 memcpy(
912 &mBuffer[evID].fEvent->Adc_Data[dest],
913 &rd[i].rBuf->S[srcT], roi[0] * 2) ;
914 } else {
915 mBuffer[evID].fEvent->StartTM[tmS] = -1 ;
916 }
917 }
918// xxx } else {
919// xxx snprintf(str,MXSTR,"wrong roi %d %d %d %d",px,pixR,roi,src-2);
920// xxx gi.evtErr++ ;
921// xxx factOut(kError,202, str ) ;
922// xxx goto EndBuf ;
923// xxx }
924 }
925 }// now we have stored a new board contents into Event structure
926
927 mBuffer[evID].fEvent->NumBoards++ ;
928 mBuffer[evID].board[ boardId ] = boardId ;
929 evtCtrl.evtStat[ iDx ]++ ;
930 evtCtrl.pcTime[ iDx ] = g_actTime ;
931
932 if (++mBuffer[evID].nBoard >= actBoards ) {
933 snprintf(str,MXSTR,"%5d complete event %8d %8d %2d",mBuffer[evID].evNum,evtCtrl.evtBuf[iDx],iDx,evtCtrl.evtStat[ iDx ]);
934 factOut(kDebug,-1, str ) ;
935
936 if (mBuffer[evID].runNum != actrun ) { // have we already reported first event of this run ???
937 actrun = mBuffer[evID].runNum ;
938 int ir ;
939 for ( ir=0; ir<MAX_RUN; ir++) {
940 if ( runCtrl[ir].runId == actrun) {
941 if ( ++runCtrl[ir].lastEvt ==0 ) {
942 gotNewRun( actrun, mBuffer[evID].FADhead );
943 snprintf(str,MXSTR,"gotNewRun %d (ev %d)",mBuffer[evID].runNum,mBuffer[evID].evNum);
944 factOut(kInfo,1, str ) ;
945 break ;
946 }
947 }
948 }
949 }
950
951 //complete event read ---> flag for next processing
952 evtCtrl.evtStat[ iDx ] = 99;
953 gi.evtTot++ ;
954 }
955
956EndBuf:
957 rd[i].bufTyp = 0 ; //ready to read next header
958 rd[i].bufLen = frst_len ;
959 rd[i].bufPos = 0 ;
960EndBuf1:
961 ;
962 }
963
964 } else { //we are reading event header
965 rd[i].bufPos += jrd ;
966 rd[i].bufLen -= jrd ;
967 if ( rd[i].bufPos >= minLen ){ //sufficient data to take action
968 //check if startflag correct; else shift block ....
969 for (k=0; k<rd[i].bufPos -1 ; k++) {
970 if (rd[i].rBuf->B[k ] == start.B[1]
971 && rd[i].rBuf->B[k+1] == start.B[0] ) break ;
972 }
973 rd[i].skip += k ;
974
975 if (k >= rd[i].bufPos-1 ) { //no start of header found
976 rd[i].bufPos = 0 ;
977 rd[i].bufLen = head_len ;
978 } else if ( k>0 ) {
979 rd[i].bufPos -= k ;
980 rd[i].bufLen += k ;
981 memcpy(&rd[i].rBuf->B[0], &rd[i].rBuf->B[k], rd[i].bufPos ) ;
982 memcpy(&rd[i].xBuf->B[0], &rd[i].xBuf->B[k], rd[i].bufPos ) ;
983 }
984 if ( rd[i].bufPos >= minLen ) {
985 if ( rd[i].skip >0 ) {
986 snprintf(str,MXSTR,"skipped %d bytes on port%d", rd[i].skip, i ) ;
987 factOut(kInfo,666, str ) ;
988 rd[i].skip = 0 ;
989 }
990 goodhed++;
991 rd[i].fadLen = ntohs(rd[i].rBuf->S[1])*2 ;
992 rd[i].fadVers= ntohs(rd[i].rBuf->S[2]) ;
993 rd[i].evtID = ntohl(rd[i].rBuf->I[4]) ; //(FADevt)
994 rd[i].ftmID = ntohl(rd[i].rBuf->I[5]) ; //(FTMevt)
995 rd[i].runID = ntohl(rd[i].rBuf->I[11]) ;
996 rd[i].bufTyp = 1 ; //ready to read full record
997 rd[i].bufLen = rd[i].fadLen - rd[i].bufPos ;
998
999 int fadboard = ntohs(rd[i].rBuf->S[12] ) ;
1000 int fadcrate = fadboard/256 ;
1001 fadboard = (fadcrate*10 + fadboard%256) ;
1002snprintf(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) ;
1003factOut(kDebug,1, str ) ;
1004
1005 if (rd[i].runID ==0 ) rd[i].runID = gi_myRun ;
1006
1007 if (rd[i].bufLen <=head_len || rd[i].bufLen > MAX_LEN ) {
1008 snprintf(str,MXSTR,"illegal event-length on port %d",i) ;
1009 factOut(kFatal,881, str ) ;
1010 rd[i].bufLen = 100000 ; //?
1011 }
1012 int fadBoard = ntohs(rd[i].rBuf->S[12] ) ;
1013 debugHead(i,fadBoard,rd[i].rBuf);
1014 debugRead(i,jrd,rd[i].evtID,rd[i].ftmID,rd[i].runID,-1,tsec,tusec) ; // i=socket; jrd=#bytes; ievt=eventid;-1=start event
1015 } else {
1016 debugRead(i,jrd,0,0,0,-2,tsec,tusec) ; // i=socket; jrd=#bytes; ievt=eventid; -2=start event, unknown id yet
1017 }
1018 } else {
1019 debugRead(i,jrd,0,0,0,-2,tsec,tusec) ; // i=socket; jrd=#bytes; ievt=eventid; -2=start event, unknown id yet
1020 }
1021
1022 } //end interpreting last read
1023 } //end of successful read anything
1024 } //finished trying to read all sockets
1025
1026snprintf(str,MXSTR,"Loop ---- %3d --- %8d",numokx,jrdx);
1027factOut(kDebug,-1, str ) ;
1028 gi.numRead[ numok ] ++ ;
1029
1030 g_actTime = time(NULL) ;
1031 if ( g_actTime > gi_SecTime ) {
1032 gi_SecTime = g_actTime ;
1033
1034
1035 //loop over all active events and flag those older than read-timeout
1036 //delete those that are written to disk ....
1037
1038 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr ;
1039 if ( kd < 0 ) kd+= (MAX_EVT*MAX_RUN) ;
1040
1041 gj.bufNew = gj.bufEvt = 0 ;
1042 int k1=evtCtrl.frstPtr;
1043 for ( k=k1; k<(k1+kd); k++ ) {
1044 int k0 = k % (MAX_EVT*MAX_RUN) ;
1045//would be better to use bitmaps for evtStat (allow '&' instead of multi-if)
1046
1047 if (evtCtrl.evtStat[k0] > 0
1048 && evtCtrl.evtStat[k0] < 92 ) {
1049 gj.bufNew++ ; //incomplete event in Buffer
1050 if ( evtCtrl.evtStat[k0] < 90
1051 && evtCtrl.pcTime[k0] < g_actTime-10 ) {
1052 int id =evtCtrl.evtBuf[k0] ;
1053 snprintf(str,MXSTR,"%5d skip short evt %8d %8d %2d",mBuffer[id].evNum,evtCtrl.evtBuf[k0],k0 ,evtCtrl.evtStat[k0]);
1054 factOut(kWarn,601, str ) ;
1055 evtCtrl.evtStat[k0] = 91 ; //timeout for incomplete events
1056 gi.evtSkp++ ;
1057 gi.evtTot++ ;
1058 gj.evtSkip++;
1059 }
1060 } else if (evtCtrl.evtStat[k0] >= 900 //'delete'
1061 || evtCtrl.evtStat[k0] == 0 ) { //'useless'
1062
1063 int id =evtCtrl.evtBuf[k0] ;
1064 snprintf(str,MXSTR,"%5d free event buffer, nb=%3d", mBuffer[id].evNum, mBuffer[id].nBoard ) ;
1065 factOut(kDebug,-1, str ) ;
1066 mBufFree(id) ; //event written--> free memory
1067 evtCtrl.evtStat[k0] = -1;
1068 gj.evtWrite++ ;
1069 gj.rateWrite++ ;
1070 } else if (evtCtrl.evtStat[k0] >= 95 ) {
1071 gj.bufEvt++ ; //complete event in Buffer
1072 }
1073
1074 if ( k0 == evtCtrl.frstPtr && evtCtrl.evtStat[k0] <0 ) {
1075 evtCtrl.frstPtr = (evtCtrl.frstPtr+1) % (MAX_EVT*MAX_RUN) ;
1076 }
1077 }
1078
1079
1080 gj.deltaT = 1000 ; //temporary, must be improved
1081
1082 int b;
1083 for ( b=0; b<NBOARDS; b++) gj.totBytes[b] +=gj.rateBytes[b] ;
1084 gj.totMem = g_maxMem ;
1085 if (gj.maxMem >= gj.xxxMem) gj.xxxMem = gj.maxMem ;
1086
1087 factStat(gj);
1088 factStatNew(gi) ;
1089 gj.rateNew = gj.rateWrite = 0 ;
1090 gj.maxMem = gj.usdMem ;
1091 for ( b=0; b<NBOARDS; b++) gj.rateBytes[b] =0 ;
1092 }
1093
1094 if (numok > 0 ) numok2=0;
1095 else if (numok2++ > 3) {
1096 if (g_runStat == 1) {
1097 xwait.tv_sec = 1;
1098 xwait.tv_nsec= 0 ; // hibernate for 1 sec
1099 } else {
1100 xwait.tv_sec = 0;
1101 xwait.tv_nsec= 2000000 ; // sleep for ~2 msec
1102 }
1103 nanosleep( &xwait , NULL ) ;
1104 }
1105
1106 } //and do next loop over all sockets ...
1107
1108
1109 snprintf(str,MXSTR,"stop reading ... RESET=%d",g_reset);
1110 factOut(kInfo,-1, str ) ;
1111
1112 if (g_reset >0 ) {
1113 gi_reset = g_reset ;
1114 gi_resetR = gi_reset%10 ; //shall we stop reading ?
1115 gi_resetS = (gi_reset/10)%10 ; //shall we close sockets ?
1116 gi_resetW = (gi_reset/100)%10 ; //shall we close files ?
1117 gi_resetX = gi_reset/1000 ; //shall we simply wait resetX seconds ?
1118 g_reset= 0;
1119 } else {
1120 gi_reset = 0;
1121 if ( g_runStat== -1 ) gi_resetR = 1 ;
1122 else gi_resetR = 7 ;
1123 gi_resetS = 7 ; //close all sockets
1124 gi_resetW = 7 ; //close all files
1125 gi_resetX = 0 ;
1126
1127 //inform others we have to quit ....
1128 gi_runStat = -11 ; //inform all that no update to happen any more
1129 gj.readStat= -11 ; //inform all that no update to happen any more
1130 }
1131
1132 if (gi_resetS > 0) {
1133 //must close all open sockets ...
1134 snprintf(str,MXSTR,"close all sockets ...");
1135 factOut(kInfo,-1, str ) ;
1136 for (i=0; i<MAX_SOCK; i++) {
1137 if (rd[i].sockStat ==0 ) {
1138 GenSock(-1, i, 0, NULL, &rd[i]) ; //close and destroy open socket
1139 if ( i%7 == 0 ) {
1140 gi_NumConnect[ i/7 ]-- ;
1141 gi.numConn[ i/7 ]-- ;
1142 gj.numConn[ i/7 ]-- ;
1143 sockDef[i/7] = 0 ; //flag ro recreate the sockets ...
1144 rd[i/7].sockStat = -1; //and try to open asap
1145 }
1146 }
1147 }
1148 }
1149
1150
1151 if (gi_resetR > 0) {
1152 //flag all events as 'read finished'
1153 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr ;
1154 if ( kd < 0 ) kd+= (MAX_EVT*MAX_RUN) ;
1155
1156 int k1=evtCtrl.frstPtr;
1157 for ( k=k1; k<(k1+kd); k++ ) {
1158 int k0 = k % (MAX_EVT*MAX_RUN) ;
1159 if (evtCtrl.evtStat[k0] > 0
1160 && evtCtrl.evtStat[k0] < 90 ) {
1161 evtCtrl.evtStat[k0] = 91 ;
1162 gi.evtSkp++ ;
1163 gi.evtTot++ ;
1164 }
1165 }
1166
1167 xwait.tv_sec = 0;
1168 xwait.tv_nsec= 2000000 ; // sleep for ~2 msec
1169 nanosleep( &xwait , NULL ) ;
1170
1171 //and clear all buffers (might have to wait until all others are done)
1172 int minclear ;
1173 if (gi_resetR == 1) {
1174 minclear = 900 ;
1175 snprintf(str,MXSTR,"drain all buffers ...");
1176 } else {
1177 minclear = 0 ;
1178 snprintf(str,MXSTR,"flush all buffers ...");
1179 }
1180 factOut(kInfo,-1, str ) ;
1181
1182 int numclear=1 ;
1183 while (numclear > 0 ) {
1184 numclear = 0 ;
1185 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr ;
1186 if ( kd < 0 ) kd+= (MAX_EVT*MAX_RUN) ;
1187
1188 int k1=evtCtrl.frstPtr;
1189 for ( k=k1; k<(k1+kd); k++ ) {
1190 int k0 = k % (MAX_EVT*MAX_RUN) ;
1191 if (evtCtrl.evtStat[k0] > minclear ) {
1192 int id =evtCtrl.evtBuf[k0] ;
1193 snprintf(str,MXSTR,"ev %5d free event buffer, nb=%3d", mBuffer[id].evNum, mBuffer[id].nBoard ) ;
1194 factOut(kDebug,-1, str ) ;
1195 mBufFree(id) ; //event written--> free memory
1196 evtCtrl.evtStat[k0] = -1;
1197 } else if (evtCtrl.evtStat[k0] > 0) numclear++ ; //writing is still ongoing...
1198
1199 if ( k0 == evtCtrl.frstPtr && evtCtrl.evtStat[k0] <0 )
1200 evtCtrl.frstPtr = (evtCtrl.frstPtr+1) % (MAX_EVT*MAX_RUN) ;
1201 }
1202
1203 xwait.tv_sec = 0;
1204 xwait.tv_nsec= 2000000 ; // sleep for ~2 msec
1205 nanosleep( &xwait , NULL ) ;
1206 }
1207 }
1208
1209 if (gi_reset > 0) {
1210 if (gi_resetW > 0) {
1211 CloseRunFile(0,0,0) ; //ask all Runs to be closed
1212 }
1213 if (gi_resetX > 0) {
1214 xwait.tv_sec = gi_resetX;
1215 xwait.tv_nsec= 0 ;
1216 nanosleep( &xwait , NULL ) ;
1217 }
1218
1219 snprintf(str,MXSTR,"Continue read Process ...");
1220 factOut(kInfo,-1, str ) ;
1221 gi_reset = 0 ;
1222 goto START ;
1223 }
1224
1225
1226
1227 snprintf(str,MXSTR,"Exit read Process ...");
1228 factOut(kInfo,-1, str ) ;
1229 gi_runStat = -99 ;
1230 gj.readStat= -99 ;
1231 factStat(gj);
1232 factStatNew(gi) ;
1233 return 0;
1234
1235} /*-----------------------------------------------------------------*/
1236
1237
1238void *procEvt( void *ptr ) {
1239/* *** main loop processing file, including SW-trigger */
1240 int numProc, numWait ;
1241 int k ;
1242 struct timespec xwait ;
1243 char str[MXSTR] ;
1244
1245 cpu_set_t mask;
1246 int cpu = 5 ; //process thread (will be several in final version)
1247
1248 snprintf(str,MXSTR,"Starting process-thread");
1249 factOut(kInfo,-1, str ) ;
1250
1251/* CPU_ZERO initializes all the bits in the mask to zero. */
1252 CPU_ZERO( &mask );
1253/* CPU_SET sets only the bit corresponding to cpu. */
1254 CPU_SET( cpu, &mask );
1255/* sched_setaffinity returns 0 in success */
1256 if ( sched_setaffinity( 0, sizeof(mask), &mask ) == -1 ) {
1257 snprintf(str,MXSTR,"P ---> can not create affinity to %d",cpu);
1258 factOut(kWarn,-1, str ) ;
1259 }
1260
1261
1262 while (g_runStat > -2) { //in case of 'exit' we still must process pending events
1263
1264 numWait = numProc = 0 ;
1265 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr ;
1266 if ( kd < 0 ) kd+= (MAX_EVT*MAX_RUN) ;
1267
1268 int k1=evtCtrl.frstPtr;
1269 for ( k=k1; k<(k1+kd); k++ ) {
1270 int k0 = k % (MAX_EVT*MAX_RUN) ;
1271//would be better to use bitmaps for evtStat (allow '&' instead of multi-if)
1272 if (evtCtrl.evtStat[k0] > 90 && evtCtrl.evtStat[k0] <500) {
1273
1274 if ( gi_resetR > 1 ) { //we are asked to flush buffers asap
1275 evtCtrl.evtStat[k0] = 991 ;
1276 } else {
1277
1278 int id = evtCtrl.evtBuf[k0] ;
1279 int ievt = mBuffer[id].evNum ;
1280 int roi = mBuffer[id].nRoi ;
1281// uint32_t irun = mBuffer[id].runNum ;
1282//snprintf(str,MXSTR,"P processing %d %d %d %d",ievt,k,id,evtCtrl.evtStat[k0]) ;
1283//factOut(kDebug,-1, str ) ;
1284
1285//make sure unused pixels/tmarks are cleared to zero
1286 int ip,it,dest,ib;
1287 for (ip=0; ip<NPIX; ip++) {
1288 if (mBuffer[id].fEvent->StartPix[ip] == -1 ) {
1289 dest= ip*roi ;
1290 bzero( &mBuffer[id].fEvent->Adc_Data[dest], roi*2) ;
1291 }
1292 }
1293 for (it=0; it<NTMARK; it++) {
1294 if (mBuffer[id].fEvent->StartTM[it] == -1 ) {
1295 dest= it*roi + NPIX*roi ;
1296 bzero( &mBuffer[id].fEvent->Adc_Data[dest], roi*2) ;
1297 }
1298 }
1299
1300
1301//and set correct event header ; also check for consistency in event (not yet)
1302 mBuffer[id].fEvent->Roi = roi ;
1303 mBuffer[id].fEvent->EventNum = ievt ;
1304 mBuffer[id].fEvent->TriggerType = 0 ; // TBD
1305 mBuffer[id].fEvent->SoftTrig = 0 ;
1306 for (ib=0; ib<NBOARDS; ib++) {
1307 if (mBuffer[id].board[ib] == -1 ) { //board is not read
1308 mBuffer[id].FADhead[ib].start_package_flag = 0 ;
1309 mBuffer[id].fEvent->BoardTime[ib] = 0 ;
1310 } else {
1311 mBuffer[id].fEvent->BoardTime[ib] =
1312 ntohl(mBuffer[id].FADhead[ib].time) ;
1313 }
1314 }
1315
1316 int i=eventCheck(mBuffer[id].FADhead,mBuffer[id].fEvent) ;
1317// gj.procEvt++ ;
1318 gi.procTot++ ;
1319 numProc++ ;
1320
1321 if (i<0) {
1322 evtCtrl.evtStat[k0] = 999 ; //flag event to be skipped
1323 gi.procErr++ ;
1324 } else {
1325 evtCtrl.evtStat[k0] = 520 ;
1326 }
1327 }
1328 } else if ( evtCtrl.evtStat[k0] >=0 && evtCtrl.evtStat[k0] < 90 ) {
1329 numWait++ ;
1330 }
1331 }
1332
1333 if ( gj.readStat < -10 && numWait == 0) { //nothing left to do
1334 snprintf(str,MXSTR,"Exit Processing Process ...");
1335 factOut(kInfo,-1, str ) ;
1336 gp_runStat = -22 ; //==> we should exit
1337 gj.procStat= -22 ; //==> we should exit
1338 return 0 ;
1339 }
1340
1341 if (numProc == 0) {
1342 //seems we have nothing to do, so sleep a little
1343 xwait.tv_sec = 0;
1344 xwait.tv_nsec= 2000000 ; // sleep for ~2 msec
1345 nanosleep( &xwait , NULL ) ;
1346 }
1347 gp_runStat = gi_runStat ;
1348 gj.procStat= gj.readStat ;
1349
1350 }
1351
1352 //we are asked to abort asap ==> must flag all remaining events
1353 // when gi_runStat claims that all events are in the buffer...
1354
1355 snprintf(str,MXSTR,"Abort Processing Process ...");
1356 factOut(kInfo,-1, str ) ;
1357 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr ;
1358 if ( kd < 0 ) kd+= (MAX_EVT*MAX_RUN) ;
1359
1360 int k1=evtCtrl.frstPtr;
1361 for ( k=k1; k<(k1+kd); k++ ) {
1362 int k0 = k % (MAX_EVT*MAX_RUN) ;
1363 if (evtCtrl.evtStat[k0] >=0 && evtCtrl.evtStat[k0] <500) {
1364 evtCtrl.evtStat[k0] = 555 ; //flag event as 'processed'
1365 }
1366 }
1367
1368 gp_runStat = -99 ;
1369 gj.procStat= -99 ;
1370
1371 return 0;
1372
1373} /*-----------------------------------------------------------------*/
1374
1375int CloseRunFile(uint32_t runId, uint32_t closeTime, uint32_t maxEvt) {
1376/* close run runId (all all runs if runId=0) */
1377/* return: 0=close scheduled / >0 already closed / <0 does not exist */
1378 int j ;
1379
1380
1381 if ( runId == 0 ) {
1382 for ( j=0; j<MAX_RUN; j++) {
1383 if ( runCtrl[j].fileId == 0 ) { //run is open
1384 runCtrl[j].closeTime = closeTime ;
1385 runCtrl[j].maxEvt = maxEvt ;
1386 }
1387 }
1388 return 0;
1389 }
1390
1391 for ( j=0; j<MAX_RUN; j++) {
1392 if ( runCtrl[j].runId == runId ) {
1393 if ( runCtrl[j].fileId == 0 ) { //run is open
1394 runCtrl[j].closeTime = closeTime ;
1395 runCtrl[j].maxEvt = maxEvt ;
1396 return 0;
1397 } else if ( runCtrl[j].fileId <0 ) { //run not yet opened
1398 runCtrl[j].closeTime = closeTime ;
1399 runCtrl[j].maxEvt = maxEvt ;
1400 return +1;
1401 } else { // run already closed
1402 return +2;
1403 }
1404 }
1405 } //we only reach here if the run was never created
1406 return -1;
1407
1408} /*-----------------------------------------------------------------*/
1409
1410
1411void *writeEvt( void *ptr ) {
1412/* *** main loop writing event (including opening and closing run-files */
1413
1414 int numWrite, numWait ;
1415 int k,j,i ;
1416 struct timespec xwait ;
1417 char str[MXSTR] ;
1418
1419 cpu_set_t mask;
1420 int cpu = 3 ; //write thread
1421
1422 snprintf(str,MXSTR,"Starting write-thread");
1423 factOut(kInfo,-1, str ) ;
1424
1425/* CPU_ZERO initializes all the bits in the mask to zero. */
1426 CPU_ZERO( &mask );
1427/* CPU_SET sets only the bit corresponding to cpu. */
1428 CPU_SET( cpu, &mask );
1429/* sched_setaffinity returns 0 in success */
1430 if ( sched_setaffinity( 0, sizeof(mask), &mask ) == -1 ) {
1431 snprintf(str,MXSTR,"W ---> can not create affinity to %d",cpu);
1432 }
1433
1434 int lastRun = 0 ; //usually run from last event still valid
1435
1436 while (g_runStat >-2) {
1437
1438 numWait = numWrite = 0 ;
1439 int kd = evtCtrl.lastPtr - evtCtrl.frstPtr ;
1440 if ( kd < 0 ) kd+= (MAX_EVT*MAX_RUN) ;
1441
1442 int k1=evtCtrl.frstPtr;
1443 for ( k=k1; k<(k1+kd); k++ ) {
1444 int k0 = k % (MAX_EVT*MAX_RUN) ;
1445//would be better to use bitmaps for evtStat (allow '&' instead of multi-if)
1446 if (evtCtrl.evtStat[k0] > 500 && evtCtrl.evtStat[k0] < 900) {
1447
1448 if ( gi_resetR > 1 ) { //we must drain the buffer asap
1449 evtCtrl.evtStat[k0] = 904 ;
1450 } else {
1451
1452
1453 int id = evtCtrl.evtBuf[k0] ;
1454 uint32_t irun = mBuffer[id].runNum ;
1455 int ievt = mBuffer[id].evNum ;
1456
1457 gi.wrtTot++ ;
1458 if (runCtrl[lastRun].runId == irun ) {
1459 j = lastRun ;
1460 } else {
1461 //check which fileID to use (or open if needed)
1462 for ( j=0; j<MAX_RUN; j++) {
1463 if ( runCtrl[j].runId == irun ) break ;
1464 }
1465 if ( j >= MAX_RUN ) {
1466 snprintf(str,MXSTR,"W error: can not find run %d for event %d in %d", irun,ievt,id);
1467 factOut(kFatal,901, str ) ;
1468 gi.wrtErr++ ;
1469 }
1470 lastRun = j ;
1471 }
1472
1473 if (runCtrl[j].fileId < 0 ) {
1474 actRun.Version = 1 ;
1475 actRun.RunType = -1 ; //to be adapted
1476
1477 actRun.Nroi = runCtrl[j].roi0 ;
1478 actRun.NroiTM = runCtrl[j].roi8 ;
1479 if ( actRun.Nroi == actRun.NroiTM ) actRun.NroiTM = 0 ;
1480 actRun.RunTime = runCtrl[j].firstTime ;
1481 actRun.RunUsec = runCtrl[j].firstTime ;
1482 actRun.NBoard = NBOARDS ;
1483 actRun.NPix = NPIX ;
1484 actRun.NTm = NTMARK ;
1485 actRun.Nroi = mBuffer[id].nRoi ;
1486 memcpy(actRun.FADhead, mBuffer[id].FADhead, NBOARDS* sizeof(PEVNT_HEADER) ) ;
1487
1488 runCtrl[j].fileHd = runOpen(irun, &actRun, sizeof(actRun) ) ;
1489 if (runCtrl[j].fileHd == NULL ) {
1490 snprintf(str,MXSTR,"W could not open a file for run %d",irun);
1491 factOut(kError,502, str ) ;
1492 runCtrl[j].fileId = 91 ;
1493 } else {
1494 snprintf(str,MXSTR,"W opened new run_file %d evt %d",irun,ievt) ;
1495 factOut(kInfo,-1, str ) ;
1496 runCtrl[j].fileId = 0 ;
1497 }
1498
1499 }
1500
1501 if (runCtrl[j].fileId != 0 ) {
1502 if (runCtrl[j].fileId < 0 ) {
1503 snprintf(str,MXSTR,"W never opened file for this run %d",irun) ;
1504 factOut(kError,123,str) ;
1505 } else if (runCtrl[j].fileId < 100 ) {
1506 snprintf(str,MXSTR,"W.file for this run is closed %d",irun) ;
1507 factOut(kWarn,123,str) ;
1508 runCtrl[j].fileId += 100 ;
1509 } else {
1510 snprintf(str,MXSTR,"W:file for this run is closed %d",irun) ;
1511 factOut(kDebug,123,str) ;
1512 }
1513 evtCtrl.evtStat[k0] = 903 ;
1514 gi.wrtErr++ ;
1515 } else {
1516 i=runWrite(runCtrl[j].fileHd, mBuffer[id].fEvent, sizeof(mBuffer[id]) );
1517 if ( i>=0 ) {
1518 runCtrl[j].lastTime = g_actTime;
1519 runCtrl[j].actEvt++ ;
1520 evtCtrl.evtStat[k0] = 901 ;
1521 snprintf(str,MXSTR,"%5d successfully wrote for run %d id %5d",ievt,irun,k0);
1522 factOut(kDebug,504, str ) ;
1523// gj.writEvt++ ;
1524 } else {
1525 snprintf(str,MXSTR,"W error writing event for run %d",irun) ;
1526 factOut(kError,503, str ) ;
1527 evtCtrl.evtStat[k0] = 902 ;
1528 gi.wrtErr++ ;
1529 }
1530
1531 if ( i < 0
1532 || runCtrl[j].lastTime < g_actTime-300
1533 || runCtrl[j].closeTime < g_actTime
1534 || runCtrl[j].maxEvt < runCtrl[j].actEvt ) {
1535int ii =0 ;
1536if ( i < 0 ) ii=1 ;
1537else if (runCtrl[j].closeTime < g_actTime ) ii=2 ;
1538else if (runCtrl[j].lastTime < g_actTime-300 ) ii=3 ;
1539else if (runCtrl[j].maxEvt < runCtrl[j].actEvt ) ii=4 ;
1540
1541
1542
1543 //close run for whatever reason
1544 if (runCtrl[j].runId == gi_myRun) gi_myRun = g_actTime ;
1545 i=runClose(runCtrl[j].fileHd, &runTail[j], sizeof(runTail[j]) );
1546 if (i<0) {
1547 snprintf(str,MXSTR,"error closing run %d %d AAA",runCtrl[j].runId,i) ;
1548 factOut(kError,503, str ) ;
1549 runCtrl[j].fileId = 92 ;
1550 } else {
1551 snprintf(str,MXSTR,"W closed run %d AAA %d",irun,ii) ;
1552 factOut(kInfo,503, str ) ;
1553 runCtrl[j].fileId = 93 ;
1554 }
1555 }
1556 }
1557 }
1558 } else if (evtCtrl.evtStat[k0] > 0
1559 && evtCtrl.evtStat[k0] < 900 ) numWait++ ;
1560 }
1561
1562 //check if we should close a run (mainly when no event pending)
1563 for ( j=0; j<MAX_RUN; j++) {
1564 if ( runCtrl[j].fileId==0
1565 && ( runCtrl[j].closeTime < g_actTime
1566 ||runCtrl[j].lastTime < g_actTime-300
1567 ||runCtrl[j].maxEvt < runCtrl[j].actEvt ) ) {
1568 if (runCtrl[j].runId == gi_myRun) gi_myRun = g_actTime ;
1569int ii =0 ;
1570 if (runCtrl[j].closeTime < g_actTime ) ii=2 ;
1571else if (runCtrl[j].lastTime < g_actTime-300 ) ii=3 ;
1572else if (runCtrl[j].maxEvt < runCtrl[j].actEvt ) ii=4 ;
1573
1574
1575 i=runClose(runCtrl[j].fileHd, &runTail[j], sizeof(runTail[j]) );
1576 if (i<0) {
1577 snprintf(str,MXSTR,"error closing run %d %d BBB",runCtrl[j].runId,i) ;
1578 factOut(kError,506, str ) ;
1579 runCtrl[j].fileId = 94 ;
1580 } else {
1581 snprintf(str,MXSTR,"W closed run %d BBB %d",runCtrl[j].runId,ii) ;
1582 factOut(kInfo,507, str ) ;
1583 runCtrl[j].fileId = 95 ;
1584 }
1585 }
1586 }
1587
1588 if (numWrite == 0) {
1589 //seems we have nothing to do, so sleep a little
1590 xwait.tv_sec = 0;
1591 xwait.tv_nsec= 2000000 ; // sleep for ~2 msec
1592 nanosleep( &xwait , NULL ) ;
1593 }
1594
1595 if ( gj.readStat < -10 && numWait == 0) { //nothing left to do
1596 snprintf(str,MXSTR,"Finish Write Process ...");
1597 factOut(kInfo,-1, str ) ;
1598 gw_runStat = -22 ; //==> we should exit
1599 gj.writStat= -22 ; //==> we should exit
1600 goto closerun ;
1601 }
1602 gw_runStat = gi_runStat ;
1603 gj.writStat= gj.readStat ;
1604
1605 }
1606
1607 //must close all open files ....
1608 snprintf(str,MXSTR,"Abort Writing Process ...");
1609 factOut(kInfo,-1, str ) ;
1610
1611closerun:
1612 snprintf(str,MXSTR,"Close all open files ...");
1613 factOut(kInfo,-1, str ) ;
1614 for ( j=0; j<MAX_RUN; j++)
1615 if ( runCtrl[j].fileId ==0 ) {
1616 if (runCtrl[j].runId == gi_myRun) gi_myRun = g_actTime ;
1617 i=runClose(runCtrl[j].fileHd, &runTail[j], sizeof(runTail[j]) );
1618int ii =0 ;
1619 if (runCtrl[j].closeTime < g_actTime ) ii=2 ;
1620else if (runCtrl[j].lastTime < g_actTime-300 ) ii=3 ;
1621else if (runCtrl[j].maxEvt < runCtrl[j].actEvt ) ii=4 ;
1622 if (i<0) {
1623 snprintf(str,MXSTR,"error closing run %d %d CCC",runCtrl[j].runId,i) ;
1624 factOut(kError,506, str ) ;
1625 runCtrl[j].fileId = 96 ;
1626 } else {
1627 snprintf(str,MXSTR,"W closed run %d CCC %d",runCtrl[j].runId,ii) ;
1628 factOut(kInfo,507, str ) ;
1629 runCtrl[j].fileId = 97 ;
1630 }
1631 }
1632
1633 gw_runStat = -99;
1634 gj.writStat= -99;
1635 snprintf(str,MXSTR,"Exit Writing Process ...");
1636 factOut(kInfo,-1, str ) ;
1637 return 0;
1638
1639
1640
1641
1642} /*-----------------------------------------------------------------*/
1643
1644
1645
1646
1647void StartEvtBuild() {
1648
1649 int i,j,imax,status,th_ret[50] ;
1650 pthread_t thread[50] ;
1651 struct timespec xwait ;
1652
1653 gi_runStat = gp_runStat = gw_runStat = 0 ;
1654 gj.readStat= gj.procStat= gj.writStat= 0 ;
1655
1656 snprintf(str,MXSTR,"Starting EventBuilder V15.07 A");
1657 factOut(kInfo,-1, str ) ;
1658
1659//initialize run control logics
1660 for (i=0; i<MAX_RUN; i++) {
1661 runCtrl[i].runId = 0 ;
1662 runCtrl[i].fileId = -2 ;
1663 }
1664
1665//partially initialize event control logics
1666 evtCtrl.frstPtr = 0 ;
1667 evtCtrl.lastPtr = 0 ;
1668
1669//start all threads (more to come) when we are allowed to ....
1670 while (g_runStat == 0 ) {
1671 xwait.tv_sec = 0;
1672 xwait.tv_nsec= 10000000 ; // sleep for ~10 msec
1673 nanosleep( &xwait , NULL ) ;
1674 }
1675
1676 i=0 ;
1677 th_ret[i] = pthread_create( &thread[i], NULL, readFAD, NULL );
1678 i++;
1679 th_ret[i] = pthread_create( &thread[i], NULL, procEvt, NULL );
1680 i++;
1681 th_ret[i] = pthread_create( &thread[i], NULL, writeEvt, NULL );
1682 i++;
1683 imax=i ;
1684
1685
1686#ifdef BILAND
1687 xwait.tv_sec = 30;;
1688 xwait.tv_nsec= 0 ; // sleep for ~20sec
1689 nanosleep( &xwait , NULL ) ;
1690
1691 printf("close all runs in 2 seconds\n");
1692
1693 CloseRunFile( 0, time(NULL)+2, 0) ;
1694
1695 xwait.tv_sec = 1;;
1696 xwait.tv_nsec= 0 ; // sleep for ~20sec
1697 nanosleep( &xwait , NULL ) ;
1698
1699 printf("setting g_runstat to -1\n");
1700
1701 g_runStat = -1 ;
1702#endif
1703
1704
1705//wait for all threads to finish
1706 for (i=0; i<imax; i++) {
1707 j = pthread_join ( thread[i], (void **)&status) ;
1708 }
1709
1710} /*-----------------------------------------------------------------*/
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726 /*-----------------------------------------------------------------*/
1727 /*-----------------------------------------------------------------*/
1728 /*-----------------------------------------------------------------*/
1729 /*-----------------------------------------------------------------*/
1730 /*-----------------------------------------------------------------*/
1731
1732
1733#ifdef BILAND
1734
1735
1736 /*-----------------------------------------------------------------*/
1737 /*-----------------------------------------------------------------*/
1738 /*-----------------------------------------------------------------*/
1739 /*-----------------------------------------------------------------*/
1740 /*-----------------------------------------------------------------*/
1741
1742
1743
1744
1745FileHandle_t runOpen(uint32_t irun, RUN_HEAD *runhd, size_t len )
1746{ return 1; } ;
1747
1748int runWrite(FileHandle_t fileHd , EVENT *event, size_t len )
1749{ return 1; usleep(10000); return 1; }
1750
1751
1752//{ return 1; } ;
1753
1754int runClose(FileHandle_t fileHd , RUN_TAIL *runth, size_t len )
1755{ return 1; } ;
1756
1757
1758
1759int eventCheck( PEVNT_HEADER *fadhd, EVENT *event)
1760{
1761 int i=0;
1762
1763// printf("------------%d\n",ntohl(fadhd[7].fad_evt_counter) );
1764// for (i=0; i<NBOARDS; i++) {
1765// printf("b=%2d,=%5d\n",i,fadhd[i].board_id);
1766// }
1767 return 0;
1768}
1769
1770
1771void factStatNew(EVT_STAT gi) {
1772 int i ;
1773
1774//for (i=0;i<MAX_SOCK;i++) {
1775// printf("%4d",gi.numRead[i]);
1776// if (i%20 == 0 ) printf("\n");
1777//}
1778}
1779
1780void gotNewRun( int runnr, PEVNT_HEADER *headers )
1781{ printf("got new run %d\n",runnr); return; }
1782
1783void factStat(GUI_STAT gj) {
1784// printf("stat: bfr%5lu skp%4lu free%4lu (tot%7lu) mem%12lu rd%12lu %3lu\n",
1785// array[0],array[1],array[2],array[3],array[4],array[5],array[6]);
1786}
1787
1788
1789void debugRead(int isock, int ibyte, int32_t event, int32_t ftmevt, int32_t runnr, int state, uint32_t tsec, uint32_t tusec ) {
1790// printf("%3d %5d %9d %3d %12d\n",isock, ibyte, event, state, tusec) ;
1791}
1792
1793
1794
1795void debugStream(int isock, void *buf, int len) {
1796}
1797
1798void debugHead(int i, int j, void *buf) {
1799}
1800
1801
1802void factOut(int severity, int err, char* message ) {
1803static FILE * fd ;
1804static int file=0 ;
1805
1806 if (file==0) {
1807 printf("open file\n");
1808 fd=fopen("x.out","w+") ;
1809 file=999;
1810 }
1811
1812 fprintf(fd,"%3d %3d | %s \n",severity,err,message) ;
1813
1814 if (severity != kDebug)
1815 printf("%3d %3d | %s\n",severity,err,message) ;
1816}
1817
1818
1819
1820int main() {
1821 int i,b,c,p ;
1822 char ipStr[100] ;
1823 struct in_addr IPaddr ;
1824
1825 g_maxMem = 1024*1024 ; //MBytes
1826//g_maxMem = g_maxMem * 1024 *10 ; //10GBytes
1827 g_maxMem = g_maxMem * 200; //100MBytes
1828
1829
1830 g_runStat = 40 ;
1831
1832 i=0 ;
1833
1834// version for standard crates
1835//for (c=0; c<4,c++) {
1836// for (b=0; b<10; b++) {
1837// sprintf(ipStr,"10.0.%d.%d",128+c,128+b)
1838//
1839// inet_pton(PF_INET, ipStr, &IPaddr) ;
1840//
1841// g_port[i].sockAddr.sin_family = PF_INET;
1842// g_port[i].sockAddr.sin_port = htons(5000) ;
1843// g_port[i].sockAddr.sin_addr = IPaddr ;
1844// g_port[i].sockDef = 1 ;
1845// i++ ;
1846// }
1847//}
1848//
1849//version for PC-test *
1850 for (c=0; c<4; c++) {
1851 for (b=0; b<10; b++) {
1852 sprintf(ipStr,"10.0.%d.11",128+c) ;
1853 if (c<2) sprintf(ipStr,"10.0.%d.11",128) ;
1854 else sprintf(ipStr,"10.0.%d.11",131) ;
1855// if (c==0) sprintf(ipStr,"10.0.100.11") ;
1856
1857 inet_pton(PF_INET, ipStr, &IPaddr) ;
1858 p = 31919+100*c+10*b;
1859
1860
1861 g_port[i].sockAddr.sin_family = PF_INET;
1862 g_port[i].sockAddr.sin_port = htons(p) ;
1863 g_port[i].sockAddr.sin_addr = IPaddr ;
1864 g_port[i].sockDef = 1 ;
1865
1866 i++ ;
1867 }
1868 }
1869
1870
1871//g_port[17].sockDef =-1 ;
1872//g_actBoards-- ;
1873
1874 StartEvtBuild() ;
1875
1876 return 0;
1877
1878}
1879#endif
Note: See TracBrowser for help on using the repository browser.