Index: /trunk/FACT++/src/EventBuilder.c
===================================================================
--- /trunk/FACT++/src/EventBuilder.c	(revision 15279)
+++ /trunk/FACT++/src/EventBuilder.c	(revision 15280)
@@ -46,4 +46,5 @@
 
 extern void factOut (int severity, int err, char *message);
+extern void factReportIncomplete (uint64_t rep);
 
 extern void gotNewRun (int runnr, PEVNT_HEADER * headers);
@@ -98,5 +99,5 @@
 int gw_runStat;
 
-int gi_memStat = +1;
+//int gi_memStat = +1;
 
 uint32_t gi_myRun = 0;
@@ -128,4 +129,62 @@
 char str[MXSTR];
 
+#define THOMAS_MALLOC
+
+#ifdef THOMAS_MALLOC
+#define MAX_HEAD_MEM (NBOARDS * sizeof(PEVNT_HEADER))
+#define MAX_TOT_MEM (sizeof(EVENT) + (NPIX+NTMARK)*1024*2 + MAX_HEAD_MEM)
+typedef struct TGB_struct
+{
+    struct TGB_struct *prev;
+    void *mem;
+} TGB_entry;
+
+TGB_entry  *tgb_last = NULL;
+uint64_t    tgb_memory = 0;
+uint64_t    tgb_inuse  = 0;
+
+void *TGB_Malloc()
+{
+    // No free slot available, next alloc would exceed max memory
+    if (!tgb_last && tgb_memory+MAX_TOT_MEM>g_maxMem)
+        return NULL;
+
+    // We will return this amount of memory
+    tgb_inuse += MAX_TOT_MEM;
+
+    // No free slot available, allocate a new one
+    if (!tgb_last)
+    {
+        tgb_memory += MAX_TOT_MEM;
+        return malloc(MAX_TOT_MEM);
+    }
+
+    // Get the next free slot from the stack and return it
+    TGB_entry *last = tgb_last;
+
+    TGB_entry *mem = last->mem;
+    tgb_last       = last->prev;
+
+    free(last);
+
+    return mem;
+};
+
+void TGB_free(void *mem)
+{
+    // Add the last free slot to the stack
+    TGB_entry *entry = malloc(sizeof(TGB_entry));
+
+    entry->prev = tgb_last;
+    entry->mem  = mem;
+
+    tgb_last = entry;
+
+    // Decrease the amont of memory in use accordingly
+    tgb_inuse -= MAX_TOT_MEM;
+}
+#endif
+
+#ifdef ETIENNE_MALLOC
 //ETIENNE
 #define MAX_SLOTS_PER_CHUNK 100
@@ -207,5 +266,5 @@
     for (int i=1;i<numNewSlots;i++)
     {
-        EtiMemoryChunks[numAllocatedChunks].pointers[i] = EtiMemoryChunks[numAllocatedChunks].pointers[0] + i*MAX_SLOT_SIZE;// &(((char*)(EtiMemoryChunks[numAllocatedChunks].pointers[i-1]))[MAX_SLOT_SIZE]);
+        EtiMemoryChunks[numAllocatedChunks].pointers[i] = (char*)EtiMemoryChunks[numAllocatedChunks].pointers[0] + i*MAX_SLOT_SIZE;// &(((char*)(EtiMemoryChunks[numAllocatedChunks].pointers[i-1]))[MAX_SLOT_SIZE]);
         EtiMemoryChunks[numAllocatedChunks].events[i] = -1;
     }
@@ -226,5 +285,7 @@
     currentChunk->events[mBufferMapping[evtIndex].slot] = -1;
     currentChunk->nFreeSlots++;
-	
+
+    return; /* TEST */
+
     int chunkIndex = mBufferMapping[evtIndex].chunk;
     if (chunkIndex != numAllocatedChunks-1)
@@ -249,5 +310,5 @@
 }
 //END ETIENNE
-
+#endif
 
 
@@ -465,4 +526,5 @@
       evtCtrl.pcTime[i] = actime;       //initiate to far future
 
+#ifdef ETIENNE_MALLOC
       //ETIENNE
       mBufferMapping[i].chunk = -1;
@@ -470,7 +532,10 @@
       mBufferMapping[i].slot = -1;
       //END ETIENNE
-   }
+#endif
+   }
+#ifdef ETIENNE_MALLOC
    for (int j=0;j<MAX_CHUNKS;j++)
        EtiMemoryChunks[j].pointers[0] = NULL;
+#endif
 
    actRun.FADhead = malloc (NBOARDS * sizeof (PEVNT_HEADER));
@@ -498,6 +563,5 @@
 //        < 0    if no space left
 
-   struct timeval *tv, atv;
-   tv = &atv;
+   struct timeval tv;
    uint32_t tsec, tusec;
    uint oldest;
@@ -506,5 +570,5 @@
    int i, b, evFree;
 //   int headmem = 0;
-   size_t needmem = 0;
+//   size_t needmem = 0;
 
 
@@ -578,7 +642,7 @@
    i = evFree;                  //found free entry; use it ...
 
-   gettimeofday (tv, NULL);
-   tsec = atv.tv_sec;
-   tusec = atv.tv_usec;
+   gettimeofday (&tv, NULL);
+   tsec = tv.tv_sec;
+   tusec = tv.tv_usec;
 
    //check if runId already registered in runCtrl
@@ -665,5 +729,19 @@
       return -11;
    }
-*/
+   */
+
+#ifdef THOMAS_MALLOC
+   mBuffer[i].FADhead = TGB_Malloc();
+   mBuffer[i].fEvent  = NULL;
+   mBuffer[i].buffer  = NULL;
+   if (mBuffer[i].FADhead == NULL) {
+      snprintf (str, MXSTR, "malloc header failed for event %d", evID);
+      factOut (kError, 882, str);
+      return -12;
+   }
+   mBuffer[i].fEvent = (void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM);
+#endif
+
+#ifdef ETIENNE_MALLOC
    mBuffer[i].FADhead = ETI_Malloc(evID, i);
    mBuffer[i].buffer = NULL;
@@ -678,10 +756,10 @@
        if (gj.usdMem > gj.maxMem)
           gj.maxMem = gj.usdMem;
-       if (gi_memStat > 0) {
+       /*if (gi_memStat > 0) {
            gi_memStat = -99;
            snprintf (str, MXSTR, "No memory left to keep event %6d sock %3d",
                      evID, sk);
            factOut (kError, 882, str);
-        }
+        }*/
 #ifdef EVTDEBUG
        else
@@ -694,4 +772,19 @@
        return -11;
    }
+#endif
+
+#ifdef STANDARD_MALLOC
+
+   mBuffer[i].FADhead = malloc (MAX_HEAD_MEM+MAX_EVT_MEM);
+   mBuffer[i].fEvent  = NULL;
+   mBuffer[i].buffer  = NULL;
+   if (mBuffer[i].FADhead == NULL) {
+      snprintf (str, MXSTR, "malloc header failed for event %d", evID);
+      factOut (kError, 882, str);
+      return -12;
+   }
+   mBuffer[i].fEvent = (void*)((char*)mBuffer[i].FADhead+MAX_HEAD_MEM);
+#endif
+
    /*
    mBuffer[i].FADhead = malloc (headmem);
@@ -746,7 +839,9 @@
    mBuffer[i].trgNum = trgNum;
    mBuffer[i].trgTyp = trgTyp;
-   mBuffer[i].evtLen = needmem;
+//   mBuffer[i].evtLen = needmem;
    mBuffer[i].Errors[0] =
-      mBuffer[i].Errors[1] = mBuffer[i].Errors[2] = mBuffer[i].Errors[3] = 0;
+       mBuffer[i].Errors[1] = mBuffer[i].Errors[2] = mBuffer[i].Errors[3] = 0;
+
+#ifdef ETIENNE_MALLOC
 //ETIENNE
    //gj.usdMem += needmem + headmem + gi_maxSize;
@@ -757,4 +852,10 @@
    }
 //END ETIENNE
+#endif
+
+#ifdef THOMAS_MALLOC
+   gj.usdMem = tgb_inuse;
+#endif
+
    if (gj.usdMem > gj.maxMem)
       gj.maxMem = gj.usdMem;
@@ -800,11 +901,22 @@
 
 //   int headmem = 0;
+//   size_t freemem = 0;
+
+#ifdef ETIENNE_MALLOC
    int evid;
-//   size_t freemem = 0;
-
    evid = mBuffer[i].evNum;
+   ETI_Free(evid, i);
+#endif
+
 //   freemem = mBuffer[i].evtLen;
    //ETIENNE
-   ETI_Free(evid, i);
+#ifdef THOMAS_MALLOC
+   TGB_free(mBuffer[i].FADhead);
+#endif
+
+#ifdef STANDARD_MALLOC
+   free (mBuffer[i].FADhead);
+#endif
+
 //   free (mBuffer[i].fEvent);
    mBuffer[i].fEvent = NULL;
@@ -819,4 +931,6 @@
    mBuffer[i].evNum = mBuffer[i].nRoi = -1;
    mBuffer[i].runNum = 0;
+
+#ifdef ETIENNE_MALLOC
 //ETIENNE
 //   gj.usdMem = gj.usdMem - freemem - headmem - gi_maxSize;
@@ -827,11 +941,16 @@
    }
 //END ETIENNE
+#endif
+
+#ifdef THOMAS_MALLOC
+   gj.usdMem = tgb_inuse;
+#endif
+
    gj.bufTot--;
 
-   if (gi_memStat < 0) {
+   /*if (gi_memStat < 0) {
       if (gj.usdMem <= 0.75 * gj.maxMem)
          gi_memStat = +1;
-   }
-
+   }*/
 
    return 0;
@@ -883,4 +1002,18 @@
 } /*-----------------------------------------------------------------*/
 
+//struct rnd
+//{
+//    int val;
+//    int idx;
+//};
+//
+//struct rnd random_arr[MAX_SOCK];
+//
+//int compare(const void *f1, const void *f2)
+//{
+//    struct rnd *r1 = (struct rnd*)f1;
+//    struct rnd *r2 = (struct rnd*)f2;
+//    return r1->val - r2->val;
+//}
 
 
@@ -904,6 +1037,5 @@
 
 
-   struct timeval *tv, atv;
-   tv = &atv;
+   struct timeval tv;
    uint32_t tsec, tusec;
 
@@ -946,7 +1078,7 @@
 
  START:
-   gettimeofday (tv, NULL);
-   g_actTime = tsec = atv.tv_sec;
-   g_actUsec = tusec = atv.tv_usec;
+   gettimeofday (&tv, NULL);
+   g_actTime = tsec = tv.tv_sec;
+   g_actUsec = tusec = tv.tv_usec;
    gi_myRun = g_actTime;
    evtCtrl.frstPtr = 0;
@@ -983,5 +1115,9 @@
       gj.bufTot = gj.maxEvt = gj.xxxEvt = 0;
       gj.usdMem = gj.maxMem = gj.xxxMem = 0;
+#ifdef THOMAS_MALLOC
+      gj.totMem = tgb_memory;
+#else
       gj.totMem = g_maxMem;
+#endif
       gj.bufNew = gj.bufEvt = 0;
       gj.badRoiE = gj.badRoiR = gj.badRoiB =
@@ -1005,7 +1141,7 @@
       gi_runStat = g_runStat;
       gj.readStat = g_runStat;
-      gettimeofday (tv, NULL);
-      g_actTime = tsec = atv.tv_sec;
-      g_actUsec = tusec = atv.tv_usec;
+      gettimeofday (&tv, NULL);
+      g_actTime = tsec = tv.tv_sec;
+      g_actUsec = tusec = tv.tv_usec;
 
 
@@ -1052,10 +1188,27 @@
       numok = 0;                //count number of succesfull actions
 
-      for (i = 0; i < MAX_SOCK; i++) {  //check all sockets if something to read
+/*
+      for (i=0; i<MAX_SOCK/7; i++)
+      {
+          random_arr[i].val = rand();
+          random_arr[i].idx = i;
+      }
+
+      qsort(random_arr, MAX_SOCK/7, sizeof(struct rnd), compare);
+
+      for (int iii = 0; iii < MAX_SOCK/7; iii++) {  //check all sockets if something to read
+
+      b = random_arr[iii].idx;
+      i = b*7;
+      p = 0;
+      */
+
+      for (i = 0; i < MAX_SOCK; i+=7) {  //check all sockets if something to read
+
          b = i / 7 ;
          p = i % 7 ;
 
-if ( p >= NUMSOCK) { ; }
-else {
+/*if ( p >= NUMSOCK) { ; }
+else*/ {
          if (sockDef[b] > 0)
             s0 = +1;
@@ -1070,4 +1223,5 @@
                if (rd[i].sockStat == -1) {
                   rd[i].errCnt++ ;
+                   usleep(25000);
 //                if (rd[i].errCnt < 100) rd[i].sockStat = rd[i].errCnt ;
 //                else if (rd[i].errCnt < 1000) rd[i].sockStat = 1000 ;
@@ -1101,16 +1255,25 @@
 
          if (rd[i].sockStat == 0) {     //we have a connection ==> try to read
-            if (rd[i].bufLen > 0) {     //might be nothing to read [buffer full]
+
+             if (rd[i].bufLen<=0)
+             {
+#ifdef EVTDEBUG
+               snprintf (str, MXSTR, "do not read from socket %d  %d", i,
+                         rd[i].bufLen);
+               factOut (kDebug, 301, str);
+#endif
+                 continue;
+             }
+
+//            if (rd[i].bufLen > 0) {     //might be nothing to read [buffer full]
                numok++;
-               size_t maxread = rd[i].bufLen ;
-               if (maxread > MAXREAD ) maxread=MAXREAD ;
 
                jrd =
                   recv (rd[i].socket, &rd[i].rBuf->B[rd[i].bufPos],
-                        maxread, MSG_DONTWAIT);
-//                      rd[i].bufLen, MSG_DONTWAIT);
+                        rd[i].bufLen, MSG_DONTWAIT);
+
+#ifdef EVTDEBUG
                if (jrd > 0) {
                   debugStream (i, &rd[i].rBuf->B[rd[i].bufPos], jrd);
-#ifdef EVTDEBUG
                   memcpy (&rd[i].xBuf->B[rd[i].bufPos],
                           &rd[i].rBuf->B[rd[i].bufPos], jrd);
@@ -1120,6 +1283,6 @@
                             rd[i].rBuf->B[rd[i].bufPos + 1]);
                   factOut (kDebug, 301, str);
-#endif
                }
+#endif
 
                if (jrd == 0) {  //connection has closed ...
@@ -1140,8 +1303,8 @@
                   } else
                      numok--;   //else nothing waiting to be read
-                  jrd = 0;
+                  //jrd = 0;
                }
-            } else {
-               jrd = 0;         //did read nothing as requested
+/*            } else {
+             //  jrd = 0;         //did read nothing as requested
 #ifdef EVTDEBUG
                snprintf (str, MXSTR, "do not read from socket %d  %d", i,
@@ -1150,12 +1313,15 @@
 #endif
                }
+*/
+            if (jrd<=0)
+                continue;
 
             gi.gotByte[b] += jrd;
             gj.rateBytes[b] += jrd;
 
-            if (jrd > 0) {
+            //if (jrd > 0) {
                numokx++;
                jrdx += jrd;
-            }
+            //}
 
 
@@ -1167,22 +1333,41 @@
                factOut (kInfo, 301, str);
 #endif
-
-            } else if (rd[i].bufTyp > 0) {      // we are reading data ...
-               if (jrd < rd[i].bufLen) {        //not yet all read
-                  rd[i].bufPos += jrd;  //==> prepare for continuation
-                  rd[i].bufLen -= jrd;
+               continue;
+            }
+
+            rd[i].bufPos += jrd;  //==> prepare for continuation
+            rd[i].bufLen -= jrd;
+
+            if (rd[i].bufTyp > 0) {      // we are reading data ...
+
+               if (rd[i].bufLen>0/*jrd < rd[i].bufLen*/) {        //not yet all read
+#ifdef EVTDEBUG
                   debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, 0, tsec, tusec);    // i=socket; jrd=#bytes; ievt=eventid; 0=reading data
-               } else {         //full dataset read
-                  rd[i].bufLen = 0;
-                  rd[i].bufPos = rd[i].fadLen;
-                  if (rd[i].rBuf->B[rd[i].bufPos - 1] != stop.B[0]
-                      || rd[i].rBuf->B[rd[i].bufPos - 2] != stop.B[1]) {
+#endif
+                  continue;
+               }
+
+               //int pos = rdi_bufPos = rd[i].bufPos;
+               //int pos = rdi_bufLen = rd[i].bufLen;
+               //int pos = rdi_bufTyp = rd[i].bufTyp;
+
+               rd[i].bufTyp = 0;     //ready to read next header
+               rd[i].bufLen = frst_len;
+               rd[i].bufPos = 0;
+
+               //{
+               //full dataset read
+                  //rd[i].bufLen = 0;
+                  //rd[i].bufPos = rd[i].fadLen;
+                  if (rd[i].rBuf->B[rd[i].fadLen - 1] != stop.B[0] ||
+                      rd[i].rBuf->B[rd[i].fadLen - 2] != stop.B[1]) {
                      gi.evtErr++;
                      snprintf (str, MXSTR,
                                "End-of-event flag wrong on socket %3d for event %4d (len=%5d), expected %3d %3d, got %3d %3d",
                                i, rd[i].evtID, rd[i].fadLen, stop.B[0], stop.B[1],
-                               rd[i].rBuf->B[rd[i].bufPos - 1], rd[i].rBuf->B[rd[i].bufPos - 2]);
+                               rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]);
                      factOut (kError, 301, str);
-                     goto EndBuf;
+                     //goto EndBuf;
+                     continue;
 
 #ifdef EVTDEBUG
@@ -1192,6 +1377,6 @@
                                i, rd[i].fadLen, rd[i].rBuf->B[0],
                                rd[i].rBuf->B[1], start.B[1], start.B[0],
-                               rd[i].rBuf->B[rd[i].bufPos - 2],
-                               rd[i].rBuf->B[rd[i].bufPos - 1], stop.B[1],
+                               rd[i].rBuf->B[rd[i].fadLen - 2],
+                               rd[i].rBuf->B[rd[i].fadLen - 1], stop.B[1],
                                stop.B[0]);
                      factOut (kDebug, 301, str);
@@ -1199,6 +1384,8 @@
                   }
 
-                  if (jrd > 0)
+#ifdef EVTDEBUG
+                  //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
+#endif
 
 /*                  //we have a complete buffer, copy to WORK area
@@ -1261,5 +1448,6 @@
                           snprintf (str, MXSTR, "Inconsistent Roi accross boards B=%d, expected %d, got %d", kr, checkRoi, roi[0]);
                           factOut (kError, 1, str);
-                          goto EndBuf;
+//                          goto EndBuf;
+                          continue;
                       }
                       roiHopper += checkRoi+4;
@@ -1277,5 +1465,6 @@
                              snprintf (str, MXSTR, "Inconsistent Roi accross patches B=%d P=%d, expected %d, got %d", kr, jr, roi[jr], checkRoi);
                              factOut (kFatal, 1, str);
-                             goto EndBuf;
+//                             goto EndBuf;
+                             continue;
                          }
                      }
@@ -1294,5 +1483,9 @@
 
                   if (evID < -1000) {
-                     goto EndBuf;       //not usable board/event/run --> skip it
+//                     goto EndBuf;       //not usable board/event/run --> skip it
+                  //rd[i].bufTyp = 0;     //ready to read next header
+                  //rd[i].bufLen = frst_len;
+                  //rd[i].bufPos = 0;
+                  continue;
                   }
                   if (evID < 0) {       //no space left, retry later
@@ -1303,8 +1496,13 @@
                      }
 #endif
-                     xwait.tv_sec = 0;
-                     xwait.tv_nsec = 10000000;  // sleep for ~10 msec
-                     nanosleep (&xwait, NULL);
-                     goto EndBuf1;      //hope there is free space next round
+                     rd[i].bufTyp = -1;
+                     rd[i].bufLen = 0;
+                     rd[i].bufPos = rd[i].fadLen;
+
+                     //xwait.tv_sec = 0;
+                     //xwait.tv_nsec = 10000000;  // sleep for ~10 msec
+                     //nanosleep (&xwait, NULL);
+                     continue;
+                     //goto EndBuf1;      //hope there is free space next round
                   }
                   //we have a valid entry in mBuffer[]; fill it
@@ -1342,8 +1540,12 @@
                                evID, boardId, i, rd[i].fadLen,
                                rd[i].rBuf->B[0], rd[i].rBuf->B[1],
-                               rd[i].rBuf->B[rd[i].bufPos - 2],
-                               rd[i].rBuf->B[rd[i].bufPos - 1]);
+                               rd[i].rBuf->B[rd[i].fadLen - 2],
+                               rd[i].rBuf->B[rd[i].fadLen - 1]);
                      factOut (kWarn, 501, str);
-                     goto EndBuf;       //--> skip Board
+//                     goto EndBuf;       //--> skip Board
+                     //rd[i].bufTyp = 0;     //ready to read next header
+                     //rd[i].bufLen = frst_len;
+                     //rd[i].bufPos = 0;
+                  continue;
                   }
 
@@ -1433,16 +1635,23 @@
                   }
 
-                EndBuf:
-                  rd[i].bufTyp = 0;     //ready to read next header
-                  rd[i].bufLen = frst_len;
-                  rd[i].bufPos = 0;
-                EndBuf1:
+//                EndBuf:
+                  //rd[i].bufTyp = 0;     //ready to read next header
+                  //rd[i].bufLen = frst_len;
+                  //rd[i].bufPos = 0;
+                //EndBuf1:
                   ;
-               }
+               //}
 
             } else {            //we are reading event header
-               rd[i].bufPos += jrd;
-               rd[i].bufLen -= jrd;
-               if (rd[i].bufPos >= minLen) {    //sufficient data to take action
+                //rd[i].bufPos += jrd;
+                //rd[i].bufLen -= jrd;
+                if (rd[i].bufPos < minLen) //{    //sufficient data to take action
+                {
+#ifdef EVTDEBUG
+                    debugRead (i, jrd, 0, 0, 0, -2, tsec, tusec);      // i=socket; jrd=#bytes; ievt=eventid; -2=start event, unknown id yet
+#endif
+                    continue;
+                }
+
                   //check if startflag correct; else shift block ....
                   for (k = 0; k < rd[i].bufPos - 1; k++) {
@@ -1456,9 +1665,12 @@
                      rd[i].bufPos = 0;
                      rd[i].bufLen = head_len;
-                  } else if (k > 0) {
+                     continue;
+                  }
+
+                  if (k > 0) {
                      rd[i].bufPos -= k;
                      rd[i].bufLen += k;
-                     memcpy (&rd[i].rBuf->B[0], &rd[i].rBuf->B[k],
-                             rd[i].bufPos);
+                     memmove (&rd[i].rBuf->B[0], &rd[i].rBuf->B[k],
+                              rd[i].bufPos);
 #ifdef EVTDEBUG
                      memcpy (&rd[i].xBuf->B[0], &rd[i].xBuf->B[k],
@@ -1466,5 +1678,14 @@
 #endif
                   }
-                  if (rd[i].bufPos >= minLen) {
+
+                  if (rd[i].bufPos < minLen)
+                  {
+#ifdef EVTDEBUG
+                      debugRead (i, jrd, 0, 0, 0, -2, tsec, tusec);      // i=socket; jrd=#bytes; ievt=eventid; -2=start event, unknown id yet
+#endif
+                      continue;
+                  }
+
+                  //{
                      if (rd[i].skip > 0) {
                         snprintf (str, MXSTR, "Skipped %d bytes on port %d",
@@ -1473,4 +1694,6 @@
                         rd[i].skip = 0;
                      }
+
+                     // TGB: This needs much more checks than just the first two bytes!
                      goodhed++;
 
@@ -1523,21 +1746,14 @@
                         rd[i].runID = gi_myRun;
 
-                     if (rd[i].bufLen <= head_len || rd[i].bufLen > MAX_LEN) {
+                     /*if (rd[i].bufLen <= head_len || rd[i].bufLen > MAX_LEN) {
                         snprintf (str, MXSTR,
                                   "Illegal event-length %d on port %d, %d expected.", rd[i].bufLen, i, head_len);
                         factOut (kFatal, 881, str);
                         rd[i].bufLen = 100000;  //?
-                     }
+                     }*/
 
                      int fadBoard = rd[i].rBuf->S[12];
                      debugHead (i, fadBoard, rd[i].rBuf);
                      debugRead (i, jrd, rd[i].evtID, rd[i].ftmID, rd[i].runID, -1, tsec, tusec);        // i=socket; jrd=#bytes; ievt=eventid;-1=start event
-                  } else {
-                     debugRead (i, jrd, 0, 0, 0, -2, tsec, tusec);      // i=socket; jrd=#bytes; ievt=eventid; -2=start event, unknown id yet
-                  }
-               } else {
-                  debugRead (i, jrd, 0, 0, 0, -2, tsec, tusec); // i=socket; jrd=#bytes; ievt=eventid; -2=start event, unknown id yet
-               }
-
             }                   //end interpreting last read
 }
@@ -1553,5 +1769,9 @@
 
       g_actTime = time (NULL);
-      if (g_actTime > gi_SecTime) {
+      if (g_actTime <= gi_SecTime) {
+          usleep(1);
+          continue;
+      }
+
          gi_SecTime = g_actTime;
 
@@ -1579,25 +1799,28 @@
                   factOut (kWarn, 601, str);
 
+                  uint64_t report = 0;
+
                   int ik,ib,jb;
                   ik=0;
                   for (ib=0; ib<NBOARDS; ib++) {
-                     if (ib%10==0) {
-                        snprintf (&str[ik], MXSTR, "|");
-                        ik++;
+                      if (ib%10==0) {
+                          str[ik++] = '|';
                      }
                      jb = mBuffer[id].board[ib];
                      if ( jb<0 ) {
-                        if (gi_NumConnect[b] >0 ) {
-                           snprintf (&str[ik], MXSTR, ".");
-                        } else {
-                           snprintf (&str[ik], MXSTR, "x");
+                         if (gi_NumConnect[b] >0 ) {
+                             str[ik++] = '.';
+                            report |= 1<<ib;
+                         } else {
+                             str[ik++] = 'x';
                         }
                      } else {
-                        snprintf (&str[ik], MXSTR, "%d",jb%10);
+                         str[ik++] = '0'+(jb%10);
                      }
-                     ik++;
                   }
-                  snprintf (&str[ik], MXSTR, "|");
+                  str[ik] = '|';
                   factOut (kWarn, 601, str);
+
+                  factReportIncomplete(report);
 
                   evtCtrl.evtStat[k0] = 91;     //timeout for incomplete events
@@ -1631,8 +1854,11 @@
          gj.deltaT = 1000;      //temporary, must be improved
 
-         int b;
-         for (b = 0; b < NBOARDS; b++)
-            gj.totBytes[b] += gj.rateBytes[b];
-         gj.totMem = g_maxMem;
+         for (int ib = 0; ib < NBOARDS; ib++)
+            gj.totBytes[ib] += gj.rateBytes[ib];
+#ifdef THOMAS_MALLOC
+      gj.totMem = tgb_memory;
+#else
+      gj.totMem = g_maxMem;
+#endif
          if (gj.maxMem > gj.xxxMem)
             gj.xxxMem = gj.maxMem;
@@ -1647,6 +1873,6 @@
          for (b = 0; b < NBOARDS; b++)
             gj.rateBytes[b] = 0;
-      }
-
+/*
+      }
       if (numok > 0)
          numok2 = 0;
@@ -1661,7 +1887,6 @@
          nanosleep (&xwait, NULL);
       }
-
+      */
    }                            //and do next loop over all sockets ...
-
 
    snprintf (str, MXSTR, "Stop reading ... RESET=%d", g_reset);
@@ -1753,7 +1978,9 @@
             if (evtCtrl.evtStat[k0] > minclear) {
                int id = evtCtrl.evtBuf[k0];
+#ifdef EVTDEBUG
                snprintf (str, MXSTR, "ev %5d free event buffer, nb=%3d",
                          mBuffer[id].evNum, mBuffer[id].nBoard);
                factOut (kDebug, -1, str);
+#endif
                mBufFree (id);   //event written--> free memory
                evtCtrl.evtStat[k0] = -1;
@@ -1789,6 +2016,12 @@
 
 
-   snprintf (str, MXSTR, "Exit read Process ...");
+   snprintf (str, MXSTR, "Exit read Process...");
    factOut (kInfo, -1, str);
+
+#ifdef THOMAS_MALLOC
+   snprintf (str, MXSTR, "%ld Bytes flaged as in-use.", tgb_inuse);
+   factOut (kInfo, -1, str);
+#endif
+
    gi_runStat = -99;
    gj.readStat = -99;
@@ -2022,8 +2255,10 @@
                }
                if (runCtrl[j].procId != 0) {
+#ifdef EVTDEBUG
                   snprintf (str, MXSTR,
                             "procEvt: Skip event %d because no active run %d", ievt,
                             irun);
                   factOut (kDebug, 502, str);
+#endif
                   evtCtrl.evtStat[k0] = 9091;
                } else {
@@ -2295,7 +2530,9 @@
                      runCtrl[j].fileId += 100;
                   } else {
+#ifdef EVTDEBUG
                       snprintf (str, MXSTR, "writeEvt: File for run %d is closed",
                                irun);
                      factOut (kDebug, 123, str);
+#endif
                   }
                   evtCtrl.evtStat[k0] = 9903;
@@ -2310,8 +2547,10 @@
                      runCtrl[j].actEvt++;
                      evtCtrl.evtStat[k0] = 9901;
+#ifdef EVTDEBUG
                      snprintf (str, MXSTR,
                                "%5d successfully wrote for run %d id %5d",
                                ievt, irun, k0);
                      factOut (kDebug, 504, str);
+#endif
 //               gj.writEvt++ ;
                   } else {
