CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/EventFilter/ResourceBroker/interface/IPCMethod.h

Go to the documentation of this file.
00001 
00002 //
00003 // IPCMethod.h
00004 // -------
00005 //
00006 // Contains common functionality for FUResourceTable and FUResourceQueue.
00007 //
00008 //  Created on: Oct 26, 2011
00009 //                                                                              Andrei Spataru : aspataru@cern.ch
00011 
00012 
00013 #ifndef IPCMETHOD_H_
00014 #define IPCMETHOD_H_
00015 
00016 #include "EventFilter/ResourceBroker/interface/FUResource.h"
00017 #include "EventFilter/ResourceBroker/interface/BUProxy.h"
00018 #include "EventFilter/ResourceBroker/interface/SMProxy.h"
00019 #include "EventFilter/ResourceBroker/interface/FUTypes.h"
00020 #include "EventFilter/Utilities/interface/Exception.h"
00021 
00022 #include "log4cplus/logger.h"
00023 
00024 #include <sys/types.h>
00025 #include <string>
00026 #include <vector>
00027 #include <queue>
00028 #include <semaphore.h>
00029 //#include <pthread.h>
00030 #include <mutex>
00031 #include <atomic>
00032 
00033 namespace evf {
00034 
00042 class IPCMethod: public toolbox::lang::Class {
00043 
00044 public:
00045         //
00046         // construction/destruction
00047         //
00048         IPCMethod(bool segmentationMode, UInt_t nbRawCells, UInt_t nbRecoCells,
00049                         UInt_t nbDqmCells, UInt_t rawCellSize, UInt_t recoCellSize,
00050                         UInt_t dqmCellSize, int freeResReq, BUProxy *bu, SMProxy *sm,
00051                         log4cplus::Logger logger, unsigned int timeout,
00052                         EvffedFillerRB *frb, xdaq::Application*app) throw (evf::Exception);
00053 
00054         virtual ~IPCMethod();
00055 
00056         //
00057         // member functions
00058         //
00059 
00060         // set the run number
00061         void setRunNumber(UInt_t runNumber) {
00062                 runNumber_ = runNumber;
00063         }
00064 
00068         virtual bool sendData() = 0;
00072         virtual bool sendDataWhileHalting() = 0;
00076         virtual bool sendDqm() = 0;
00080         virtual bool sendDqmWhileHalting() = 0;
00084         virtual bool discard() = 0;
00088         virtual bool discardWhileHalting(bool sendDiscards) = 0;
00089 
00093         UInt_t allocateResource();
00094 
00098         virtual bool buildResource(MemRef_t* bufRef) = 0;
00099 
00103         virtual bool discardDataEvent(MemRef_t* bufRef) = 0;
00107         virtual bool discardDataEventWhileHalting(MemRef_t* bufRef) = 0;
00108 
00112         virtual bool discardDqmEvent(MemRef_t* bufRef) = 0;
00116         virtual bool discardDqmEventWhileHalting(MemRef_t* bufRef) = 0;
00117 
00121         virtual void postEndOfLumiSection(MemRef_t* bufRef) = 0;
00122 
00126         virtual void dropEvent() = 0;
00127 
00131         virtual bool handleCrashedEP(UInt_t runNumber, pid_t pid) = 0;
00132 
00136         void dumpEvent(evf::FUShmRawCell* cell);
00137 
00141         virtual std::string printStatus();
00142 
00146         virtual void shutDownClients() = 0;
00147 
00151         virtual void clear() = 0;
00152 
00156         virtual void resetCounters() = 0;
00157 
00161         void setDoCrcCheck(UInt_t doCrcCheck) {
00162                 doCrcCheck_ = doCrcCheck;
00163         }
00164 
00168         void setDoDumpEvents(UInt_t doDumpEvents) {
00169                 doDumpEvents_ = doDumpEvents;
00170         }
00171 
00175         bool isActive() const {
00176                 return isActive_;
00177         }
00178 
00179         void setActive(bool activeValue) {
00180                 isActive_ = activeValue;
00181         }
00182 
00186         bool isReadyToShutDown() const {
00187                 return isReadyToShutDown_;
00188         }
00189 
00190         void setReadyToShutDown(bool readyValue) {
00191                 isReadyToShutDown_ = readyValue;
00192         }
00193 
00194         UInt_t shutdownStatus() {
00195                 return shutdownStatus_;
00196         }
00197 
00198         void setStopFlag(bool status) {
00199                 stopFlag_=status;
00200         }
00201 
00202         // various counters
00203         virtual UInt_t nbResources() const = 0; /* Implemented in subclass */
00204         UInt_t nbFreeSlots() const {
00205                 return freeResourceIds_.size();
00206         }
00207         UInt_t nbAllocated() const {
00208                 return nbAllocated_;
00209         }
00210         UInt_t nbPending() const {
00211                 return nbPending_;
00212         }
00213         UInt_t nbCompleted() const {
00214                 return nbCompleted_;
00215         }
00216         UInt_t nbSent() const {
00217                 return nbSent_;
00218         }
00219         UInt_t nbSentError() const {
00220                 return nbSentError_;
00221         }
00222         UInt_t nbSentDqm() const {
00223                 return nbSentDqm_;
00224         }
00225         UInt_t nbPendingSMDiscards() const {
00226                 return nbPendingSMDiscards_;
00227         }
00228         int nbPendingSMDqmDiscards() const {
00229                 return nbPendingSMDqmDiscards_;
00230         }
00231         UInt_t nbDiscarded() const {
00232                 return nbDiscarded_;
00233         }
00234         UInt_t nbLost() const {
00235                 return nbLost_;
00236         }
00237 
00238         // UPDATED
00239         UInt_t nbEolPosted() const {
00240                 return nbEolPosted_;
00241         }
00242         UInt_t nbEolDiscarded() const {
00243                 return nbEolDiscarded_;
00244         }
00245 
00246         UInt_t nbErrors() const {
00247                 return nbErrors_;
00248         }
00249         UInt_t nbCrcErrors() const {
00250                 return nbCrcErrors_;
00251         }
00252         UInt_t nbAllocSent() const {
00253                 return nbAllocSent_;
00254         }
00255 
00256         uint64_t sumOfSquares() const {
00257                 return sumOfSquares_;
00258         }
00259         UInt_t sumOfSizes() const {
00260                 return sumOfSizes_;
00261         }
00262 
00263         // information about (raw) shared memory cells
00264         virtual UInt_t nbClients() const = 0;
00265         virtual std::vector<pid_t> clientPrcIds() const = 0;
00266         virtual std::string clientPrcIdsAsString() const = 0;
00267         virtual std::vector<std::string> cellStates() const = 0;
00268         virtual std::vector<std::string> dqmCellStates() const = 0;
00269         virtual std::vector<UInt_t> cellEvtNumbers() const = 0;
00270         virtual std::vector<pid_t> cellPrcIds() const = 0;
00271         virtual std::vector<time_t> cellTimeStamps() const = 0;
00272 
00273         //
00274         // helpers
00275         //
00276         void sendAllocate();
00278         void resetPendingAllocates();
00280         void releaseResources();
00282         virtual void resetIPC() = 0;
00283 
00284         void sendDiscard(UInt_t buResourceId);
00285 
00286         void sendInitMessage(UInt_t fuResourceId, UInt_t outModId,
00287                         UInt_t fuProcessId, UInt_t fuGuid, UChar_t*data, UInt_t dataSize,
00288                         UInt_t nExpectedEPs);
00289 
00290         void sendDataEvent(UInt_t fuResourceId, UInt_t runNumber, UInt_t evtNumber,
00291                         UInt_t outModId, UInt_t fuProcessId, UInt_t fuGuid, UChar_t*data,
00292                         UInt_t dataSize);
00293 
00294         void sendErrorEvent(UInt_t fuResourceId, UInt_t runNumber,
00295                         UInt_t evtNumber, UInt_t fuProcessId, UInt_t fuGuid, UChar_t*data,
00296                         UInt_t dataSize);
00297 
00298         void sendDqmEvent(UInt_t fuDqmId, UInt_t runNumber, UInt_t evtAtUpdate,
00299                         UInt_t folderId, UInt_t fuProcessId, UInt_t fuGuid, UChar_t*data,
00300                         UInt_t dataSize);
00301 
00302         bool isLastMessageOfEvent(MemRef_t* bufRef);
00303 
00304         void injectCRCError();
00305 
00306         void lock() {
00307                 //lock_.take();
00308                 while (0 != sem_wait(&lock_)) {
00309                         if (errno != EINTR) {
00310                                 LOG4CPLUS_ERROR(log_, "Cannot obtain lock on sem LOCK!");
00311                         }
00312                 }
00313         }
00314         void unlock() {
00315                 //lock_.give();
00316                 sem_post(&lock_);
00317         }
00318         #ifdef linux
00319         std::unique_lock<std::timed_mutex> lockCrashHandler() {
00320                 std::unique_lock<std::timed_mutex> lk(crashHandlerLock_);
00321                 return lk;
00322         }
00323 
00324         std::unique_lock<std::timed_mutex> lockCrashHandlerTimed(unsigned int seconds) {
00325                 std::unique_lock<std::timed_mutex> lk(crashHandlerLock_,std::chrono::seconds(seconds));
00326                 return lk;
00327         }
00328         #endif
00329 
00332         virtual void lastResort() = 0;
00333 
00334 protected:
00335         //
00336         // member data
00337         //
00338 
00339         BUProxy *bu_;
00340         SMProxy *sm_;
00341 
00342         log4cplus::Logger log_;
00343 
00344         UInt_t nbDqmCells_;
00345         UInt_t nbRawCells_;
00346         UInt_t nbRecoCells_;
00347 
00348         std::queue<UInt_t> freeResourceIds_;
00349         // number of free resources required to ask BU for more events
00350         unsigned int freeResRequiredForAllocate_;
00351 
00352         bool *acceptSMDataDiscard_;
00353         int *acceptSMDqmDiscard_;
00354 
00355         UInt_t doCrcCheck_;
00356         UInt_t doDumpEvents_;
00357         unsigned int shutdownTimeout_;
00358 
00359         UInt_t nbAllocated_;
00360         UInt_t nbPending_;
00361         UInt_t nbCompleted_;
00362         UInt_t nbSent_;
00363         UInt_t nbSentError_;
00364         UInt_t nbSentDqm_;
00365         UInt_t nbPendingSMDiscards_;
00366         std::atomic<int> nbPendingSMDqmDiscards_;
00367         UInt_t nbDiscarded_;
00368         UInt_t nbLost_;
00369         // UPDATED
00370         UInt_t nbEolPosted_;
00371         UInt_t nbEolDiscarded_;
00372 
00373         UInt_t nbClientsToShutDown_;
00374         bool isReadyToShutDown_;
00375         bool isActive_;
00376 
00377         UInt_t nbErrors_;
00378         UInt_t nbCrcErrors_;
00379         UInt_t nbAllocSent_;
00380 
00381         uint64_t sumOfSquares_;
00382         UInt_t sumOfSizes_;
00383 
00384         UInt_t runNumber_;
00385 
00386         sem_t lock_;
00387         #ifdef linux
00388         std::timed_mutex crashHandlerLock_;
00389         #endif
00390         EvffedFillerRB *frb_;
00391         xdaq::Application *app_;
00392 
00393         FUResourceVec_t resources_;
00394 
00395         UInt_t shutdownStatus_;
00396         bool stopFlag_;
00397 
00398 };
00399 
00400 }
00401 
00402 #endif /* IPCMETHOD_H_ */