]> jspc29.x-matter.uni-frankfurt.de Git - daqdata.git/commitdiff
Join with new-file-handling branch, auto reopen of file after
authorhadaq <hadaq>
Mon, 18 Nov 2002 14:05:24 +0000 (14:05 +0000)
committerhadaq <hadaq>
Mon, 18 Nov 2002 14:05:24 +0000 (14:05 +0000)
2 GB -- mm

hadaq/evtbuild.c

index f5bbfdde184d8723c8f1021340d41a9ef51704ba..f345daf7b526e633f2916a575094e09e750531f0 100644 (file)
@@ -1,4 +1,4 @@
-static char *rcsId = "$Header: /misc/hadesprojects/daq/cvsroot/eventbuilder/hadaq/evtbuild.c,v 6.52 2002-11-18 09:47:21 hadaq Exp $";
+static char *rcsId = "$Header: /misc/hadesprojects/daq/cvsroot/eventbuilder/hadaq/evtbuild.c,v 6.53 2002-11-18 14:05:24 hadaq Exp $";
 
 
 #define _POSIX_C_SOURCE 199309L
@@ -33,6 +33,8 @@ static char *rcsId = "$Header: /misc/hadesprojects/daq/cvsroot/eventbuilder/hada
 #define NTRIGTYPES 64UL                        /* must be 2^n */
 #define NTRIGTYPES_IN_FILE 16  /* must be 2^n */
 
+static FILE *outFile;
+static AnsiTape *outTape;
 static time_t ourTime;
 
 typedef struct TheArgsS {
@@ -114,8 +116,8 @@ static void *appendFile(void *my, const char *path)
 static void usage(const char *progName)
 {
        syslog(LOG_ERR, "Usage: %s [-x expId]", progName);
-       syslog(LOG_ERR, "Usage: [-m nrOfMsgs] [-r runNr]");
-       syslog(LOG_ERR, "Usage: [-f slowCtrlFile ...] [-o outPath] [-d null|tape|file]");
+       syslog(LOG_ERR, "Usage: [-m nrOfMsgs] [-f slowCtrlFile ...]");
+       syslog(LOG_ERR, "Usage: [-o outPath] [-d null|tape|file|stdout]");
        syslog(LOG_ERR, "Usage: [-a (agent)] [-p priority]");
 }
 
@@ -148,7 +150,6 @@ static void argsDefault(TheArgs * my)
        my->slowCtrlFileCnt = 0;
        strcpy(my->outPath, "");
        strcpy(my->outDev, "null");
-       my->runNr = ourTime;
        strcpy(my->expId, "xx");
        my->priority = 0;
        my->isStandalone = 1;
@@ -169,9 +170,6 @@ static int argsFromCL(TheArgs * my, int argc, char *argv[])
                case 'f':
                        strcpy(my->slowCtrlFiles[my->slowCtrlFileCnt++], optarg);
                        break;
-               case 'r':
-                       my->runNr = strtoul(optarg, NULL, 0);
-                       break;
                case 's':
                        syslog(LOG_WARNING,
                                   "-s option obsolete, will be removed in a future version");
@@ -217,7 +215,6 @@ static int argsFromParam(TheArgs * my, int argc, char *argv[])
                                PARAM_MAX_ARRAY_LEN, &my->slowCtrlFileCnt, my->slowCtrlFiles);
        Param_getString(param, argv[0], "outpath", &paramWasFound, my->outPath);
        Param_getString(param, argv[0], "outdev", &paramWasFound, my->outDev);
-       Param_getInt(param, argv[0], "runnr", &paramWasFound, &my->runNr);
        Param_getString(param, argv[0], "expid", &paramWasFound, my->expId);
        Param_getInt(param, argv[0], "stndln", &paramWasFound, &my->isStandalone);
        Param_getInt(param, argv[0], "prio", &paramWasFound, &my->priority);
@@ -354,6 +351,86 @@ static void storeInfoStop(const char *n, time_t t, Worker *w)
        desParam(p);
 }
 
+static int openFile(TheArgs *theArgs) {
+       char fileName[_POSIX_PATH_MAX];
+       static char outPath[_POSIX_PATH_MAX];
+       static once = 1;
+
+       theArgs->runNr = ourTime;
+
+       if (once) {
+               strcpy(outPath, theArgs->outPath);
+               once = 0;
+       } else {
+               strcpy(theArgs->outPath, outPath);
+       }
+
+       /* construct a default filename */
+       strcpy(fileName, theArgs->expId);
+       strftime(fileName + strlen(fileName), 18, "%y%j%H%M%S.hld", localtime(&ourTime));
+
+       outTape = NULL;
+       outFile = NULL;
+       if (strcmp(theArgs->outDev, "null") == 0) {
+               outFile = NULL;
+       } else if (strcmp(theArgs->outDev, "stdout") == 0) {
+               outFile = stdout;
+       } else if (strcmp(theArgs->outDev, "file") == 0) {
+               if (strcmp(theArgs->outPath, "") == 0) {
+                       strcpy(theArgs->outPath, fileName);
+               } else {
+                       struct stat bufS, *buf = &bufS;
+
+                       stat(theArgs->outPath, buf);
+                       if (S_ISDIR(buf->st_mode)) {
+                               strcat(theArgs->outPath, "/");
+                               strcat(theArgs->outPath, fileName);
+                       }
+               }
+
+               if (NULL == (outFile = fopen(theArgs->outPath, "wb"))) {
+                       syslog(LOG_ERR, "opening file %s: %s", theArgs->outPath, strerror(errno));
+                       outFile = NULL;
+                       return -1;
+               }
+       } else if (strcmp(theArgs->outDev, "tape") == 0) {
+               if (strcmp(theArgs->outPath, "") == 0) {
+                       strcpy(theArgs->outPath, fileName);
+               }
+               if (NULL == (outTape = openAnsiTape(theArgs->outPath, "/dev/tape"))) {
+                       syslog(LOG_ERR, "opening tape %s: %s", theArgs->outPath, strerror(errno));
+                       outFile = NULL;
+                       return -1;
+               }
+       } else {
+               syslog(LOG_ERR, "unknown outputDev \"%s\"", theArgs->outDev);
+               return -1;
+       }
+       return 0;
+}
+
+static int writeFile(void *evt) {
+       int writeFileR;
+
+       if (outFile != NULL) {
+               writeFileR = fwrite(evt, 1, Evt_paddedSize(evt), outFile);
+       } else if (outTape != NULL) {
+               writeFileR = writeAnsiTape(outTape, evt, Evt_paddedSize(evt));
+       }
+       return writeFileR;
+}
+
+static int closeFile() {
+       int closeFileR;
+
+       if (outFile != NULL) {
+               closeFileR = fclose(outFile);
+       } else if (outTape != NULL) {
+               closeFileR = closeAnsiTape(outTape);
+       }
+       return closeFileR;
+}
+
 /* BUGBUG bailOut not proper yet */
 int main(int argc, char *argv[])
 {
@@ -363,24 +440,16 @@ int main(int argc, char *argv[])
        ShmTrans **shmTrans;
        HadTuQueue **hadTuQueue;
        Worker *worker;
-       char fileName[_POSIX_PATH_MAX];
-       FILE *outFile;
-       AnsiTape *outTape;
-       void *evt;
-       void *subEvt;
+       off_t maxFileSize;
 
        int scanWasSuccessful;
        uint32_t currTrigNr;
        uint32_t currTrigTag;
        uint32_t currId;
 
-       ourTime = time(NULL);
-
        openlog(argv[0], LOG_PID | LOG_PERROR, LOG_LOCAL0);
        setlogmask(LOG_UPTO(LOG_INFO));
 
-
-
        argsDefault(theArgs);
        argsFromParam(theArgs, argc, argv);
        if (0 > argsFromCL(theArgs, argc, argv)) {
@@ -401,50 +470,10 @@ int main(int argc, char *argv[])
        theArgs->expId[1] = tolower(theArgs->expId[1]);
        theArgs->expId[2] = '\0';
 
-       /* construct a default filename */
-       strcpy(fileName, theArgs->expId);
-       strftime(fileName + strlen(fileName), 18, "%y%j%H%M%S.hld", localtime(&ourTime));
-
-       /* construct the output path */
-       if (strcmp(theArgs->outPath, "") == 0) {
-               strcpy(theArgs->outPath, fileName);
-       } else {
-               struct stat bufS, *buf = &bufS;
-
-               stat(theArgs->outPath, buf);
-               if (S_ISDIR(buf->st_mode)) {
-                       strcat(theArgs->outPath, "/");
-                       strcat(theArgs->outPath, fileName);
-               }
-       }
-
        if (NULL == (worker = Worker_initBegin(argv[0], sigHandler, theArgs->priority, theArgs->isStandalone))) {
                syslog(LOG_ERR, "%s, %d: %s", __FILE__, __LINE__, strerror(errno));
                exit(EXIT_FAILURE);
        }
-       outTape = NULL;
-       outFile = NULL;
-       if (strcmp(theArgs->outDev, "null") == 0) {
-               outFile = NULL;
-       } else if (strcmp(theArgs->outDev, "stdout") == 0) {
-               outFile = stdout;
-       } else if (strcmp(theArgs->outDev, "file") == 0) {
-               if (NULL == (outFile = fopen(theArgs->outPath, "wb"))) {
-                       syslog(LOG_ERR, "%s, %d: %s", __FILE__, __LINE__, strerror(errno));
-                       exit(EXIT_FAILURE);
-               }
-       } else if (strcmp(theArgs->outDev, "tape") == 0) {
-               if (NULL == (outTape = openAnsiTape(theArgs->outPath, "/dev/tape"))) {
-                       syslog(LOG_NOTICE, "Opening of tape failed (no tape available?): Writing to /dev/null.");
-                       strcpy(theArgs->outDev, "null");
-                       outFile = NULL;
-               }
-       } else {
-               syslog(LOG_ERR, "unknown outputDev \"%s\"", theArgs->outDev);
-               usage(argv[0]);
-               exit(EXIT_FAILURE);
-       }
-
        if (-1 == initOnline()) {
                syslog(LOG_WARNING, "unable to initialize online service");
        }
@@ -494,29 +523,38 @@ int main(int argc, char *argv[])
        }
 
        argsDump(theArgs);
-       storeInfoStart(argv[0], ourTime, theArgs);
 
        Worker_initEnd(worker);
 
-       evt = newEvt(EvtDecoding_64bitAligned, EvtId_runStart, theArgs->runNr, theArgs->expId);
-       for (i = 0; i < theArgs->slowCtrlFileCnt; i++) {
-               evt = appendFile(evt, theArgs->slowCtrlFiles[i]);
-       }
-       if (outFile != NULL) {
-               fwrite(evt, 1, Evt_paddedSize(evt), outFile);
-       } else if (outTape != NULL) {
-               writeAnsiTape(outTape, evt, Evt_paddedSize(evt));
-       }
-       deleteEvt(evt);
-
        currId = 0;
+       maxFileSize = (2 * 1024 * 1024 * 1024UL) - theArgs->queueSize;
        while (setjmp(terminateJmp) == 0) {
+               void *evt;
+               void *subEvt;
                int step;
                int evtIsBroken = 0;
                int dataError = 0;
                int tagError = 0;
 
                statsDump(theArgs, theStats, 1);
+
+               if (*theStats->bytesWritten == 0) {
+                       ourTime = time(NULL);
+                       if (-1 == openFile(theArgs)) {
+                               syslog(LOG_ERR, "error opening output file, exiting");
+                               exit(EXIT_FAILURE);
+                       }
+
+                       storeInfoStart(argv[0], ourTime, theArgs);
+
+                       evt = newEvt(EvtDecoding_64bitAligned, EvtId_runStart, theArgs->runNr, theArgs->expId);
+                       for (i = 0; i < theArgs->slowCtrlFileCnt; i++) {
+                               evt = appendFile(evt, theArgs->slowCtrlFiles[i]);
+                       }
+                       writeFile(evt);
+                       deleteEvt(evt);
+               }
+
                evt = newEvt(EvtDecoding_64bitAligned, EvtId_data, theArgs->runNr, theArgs->expId);
                for (i = 0; i < theArgs->nrOfMsgs && !evtIsBroken; i += step) {
                        uint32_t trigNr;
@@ -530,11 +568,9 @@ int main(int argc, char *argv[])
                                conHadTuQueue_voidP(hadTuQueue[i], storage);
                        }
                        subEvt = HadTuQueue_front(hadTuQueue[i]);
-#ifndef NDEBUG
                        syslog(LOG_DEBUG,
                                   "hadTuQueue[%d]: %p = subEvt: %s",
                                   i, subEvt, SubEvt_2charP(subEvt));
-#endif
 
                        (*theStats->trigNr[i]) = SubEvt_trigNr(subEvt);
 
@@ -542,10 +578,8 @@ int main(int argc, char *argv[])
                                currTrigNr = SubEvt_trigNr(subEvt) >> 8;
                                currTrigTag = SubEvt_trigNr(subEvt) & 0xff;
                                currId = SubEvt_pureId(subEvt);
-#ifndef NDEBUG
                                syslog(LOG_DEBUG,
                                           "currTrigNr: 0x%06x, currTrigTag 0x%02x, currId 0x%08x", currTrigNr, currTrigTag, currId);
-#endif
                        }
                        trigNr = SubEvt_trigNr(subEvt) >> 8;
                        trigTag = SubEvt_trigNr(subEvt) & 0xff;
@@ -591,41 +625,29 @@ int main(int argc, char *argv[])
                        Evt_setId(evt, currId & (NTRIGTYPES_IN_FILE - 1));
 
                        (*theStats->bytesWritten) += Evt_size(evt);
-                       if (outFile != NULL) {
-                               fwrite(evt, 1, Evt_paddedSize(evt), outFile);
-                       } else if (outTape != NULL) {
-                               writeAnsiTape(outTape, evt, Evt_paddedSize(evt));
-                       }
+                       writeFile(evt);
                        Evt_online(evt);
                } else {
                        (*theStats->evtsDiscarded)++;
                }
                deleteEvt(evt);
-       }
-       statsDump(theArgs, theStats, 0);
-
-       ourTime = time(NULL);
-       storeInfoStop(argv[0], ourTime, worker);
+               if (*theStats->bytesWritten >= maxFileSize) {
+                       evt = newEvt(EvtDecoding_64bitAligned, EvtId_runStop, theArgs->runNr, theArgs->expId);
+                       for (i = 0; i < theArgs->slowCtrlFileCnt; i++) {
+                               evt = appendFile(evt, theArgs->slowCtrlFiles[i]);
+                       }
+                       writeFile(evt);
+                       deleteEvt(evt);
 
+                       ourTime = time(NULL);
+                       closeFile();
+                       storeInfoStop(argv[0], ourTime, worker);
 
-       evt = newEvt(EvtDecoding_64bitAligned, EvtId_runStop, theArgs->runNr, theArgs->expId);
-       for (i = 0; i < theArgs->slowCtrlFileCnt; i++) {
-               evt = appendFile(evt, theArgs->slowCtrlFiles[i]);
-       }
-       if (outFile != NULL) {
-               fwrite(evt, 1, Evt_paddedSize(evt), outFile);
-       } else if (outTape != NULL) {
-               writeAnsiTape(outTape, evt, Evt_paddedSize(evt));
-       }
-       deleteEvt(evt);
-
-       if (outFile != NULL) {
-               fclose(outFile);
-       } else if (outTape != NULL) {
-               if (-1 == closeAnsiTape(outTape)) {
-                       syslog(LOG_ERR, "%s, %d: %s", __FILE__, __LINE__, strerror(errno));
+                       (*theStats->bytesWritten) = 0;
                }
        }
+       statsDump(theArgs, theStats, 0);
+
        for (i = 0; i < theArgs->nrOfMsgs; i++) {
                ShmTrans_remove(shmTrans[i]);
        }