00001 #ifndef AUTOBU_BU_H 00002 #define AUTOBU_BU_H 1 00003 00004 00005 #include "EventFilter/AutoBU/interface/BUEvent.h" 00006 00007 #include "EventFilter/Utilities/interface/StateMachine.h" 00008 #include "EventFilter/Utilities/interface/WebGUI.h" 00009 #include "EventFilter/Utilities/interface/Exception.h" 00010 00011 #include "EventFilter/Playback/interface/PlaybackRawDataProvider.h" 00012 00013 #include "xdaq/Application.h" 00014 00015 #include "toolbox/mem/HeapAllocator.h" 00016 #include "toolbox/mem/MemoryPoolFactory.h" 00017 #include "toolbox/net/URN.h" 00018 #include "toolbox/fsm/exception/Exception.h" 00019 00020 #include "xdata/InfoSpace.h" 00021 #include "xdata/UnsignedInteger32.h" 00022 #include "xdata/Double.h" 00023 #include "xdata/Boolean.h" 00024 #include "xdata/String.h" 00025 00026 #include "interface/evb/i2oEVBMsgs.h" 00027 #include "interface/shared/i2oXFunctionCodes.h" 00028 00029 #include "interface/shared/frl_header.h" 00030 #include "interface/shared/fed_header.h" 00031 #include "interface/shared/fed_trailer.h" 00032 00033 #include "i2o/Method.h" 00034 #include "i2o/utils/AddressMap.h" 00035 00036 #include "CLHEP/Random/RandGauss.h" 00037 00038 00039 #include <vector> 00040 #include <queue> 00041 #include <cmath> 00042 #include <semaphore.h> 00043 #include <sys/time.h> 00044 00045 00046 namespace evf { 00047 00048 00049 class BU : public xdaq::Application, 00050 public xdata::ActionListener 00051 { 00052 public: 00053 // 00054 // xdaq instantiator macro 00055 // 00056 XDAQ_INSTANTIATOR(); 00057 00058 00059 // 00060 // construction/destruction 00061 // 00062 BU(xdaq::ApplicationStub *s); 00063 virtual ~BU(); 00064 00065 00066 // 00067 // public member functions 00068 // 00069 00070 // work loop functions to be executed during transitional states (async) 00071 bool configuring(toolbox::task::WorkLoop* wl); 00072 bool enabling(toolbox::task::WorkLoop* wl); 00073 bool stopping(toolbox::task::WorkLoop* wl); 00074 bool halting(toolbox::task::WorkLoop* wl); 00075 00076 // fsm soap command callback 00077 xoap::MessageReference fsmCallback(xoap::MessageReference msg) 00078 throw (xoap::exception::Exception); 00079 00080 // i2o callbacks 00081 void I2O_BU_ALLOCATE_Callback(toolbox::mem::Reference *bufRef); 00082 void I2O_BU_DISCARD_Callback(toolbox::mem::Reference *bufRef); 00083 00084 // xdata::ActionListener callback 00085 void actionPerformed(xdata::Event& e); 00086 00087 // Hyper DAQ web interface [see Utilities/WebGUI] 00088 void webPageRequest(xgi::Input *in,xgi::Output *out) 00089 throw (xgi::exception::Exception); 00090 void customWebPage(xgi::Input*in,xgi::Output*out) 00091 throw (xgi::exception::Exception); 00092 00093 // build events (random or playback) 00094 void startBuildingWorkLoop() throw (evf::Exception); 00095 bool building(toolbox::task::WorkLoop* wl); 00096 00097 // send events to connected FU 00098 void startSendingWorkLoop() throw (evf::Exception); 00099 bool sending(toolbox::task::WorkLoop* wl); 00100 00101 // calculate monitoring information in separate thread 00102 void startMonitoringWorkLoop() throw (evf::Exception); 00103 bool monitoring(toolbox::task::WorkLoop* wl); 00104 00105 00106 private: 00107 // 00108 // private member functions 00109 // 00110 void lock() { sem_wait(&lock_); } 00111 void unlock() { sem_post(&lock_); } 00112 void waitBuild() { sem_wait(&buildSem_); } 00113 void postBuild() { sem_post(&buildSem_); } 00114 void waitSend() { sem_wait(&sendSem_); } 00115 void postSend() { sem_post(&sendSem_); } 00116 void waitRqst() { sem_wait(&rqstSem_); } 00117 void postRqst() { sem_post(&rqstSem_); } 00118 00119 void exportParameters(); 00120 void reset(); 00121 double deltaT(const struct timeval *start,const struct timeval *end); 00122 00123 bool generateEvent(evf::BUEvent* evt); 00124 toolbox::mem::Reference *createMsgChain(evf::BUEvent *evt, 00125 unsigned int fuResourceId); 00126 00127 00128 void dumpFrame(unsigned char* data,unsigned int len); 00129 00130 00131 private: 00132 // 00133 // member data 00134 // 00135 00136 // BU message logger 00137 Logger log_; 00138 00139 // BU application descriptor 00140 xdaq::ApplicationDescriptor *buAppDesc_; 00141 00142 // FU application descriptor 00143 xdaq::ApplicationDescriptor *fuAppDesc_; 00144 00145 // BU application context 00146 xdaq::ApplicationContext *buAppContext_; 00147 00148 // BU state machine 00149 StateMachine fsm_; 00150 00151 // BU web interface 00152 WebGUI *gui_; 00153 00154 // resource management 00155 std::vector<evf::BUEvent*> events_; 00156 std::queue<unsigned int> rqstIds_; 00157 std::queue<unsigned int> freeIds_; 00158 std::queue<unsigned int> builtIds_; 00159 std::set<unsigned int> sentIds_; 00160 unsigned int evtNumber_; 00161 std::vector<unsigned int> validFedIds_; 00162 00163 bool isBuilding_; 00164 bool isSending_; 00165 bool isHalting_; 00166 00167 // workloop / action signature for building events 00168 toolbox::task::WorkLoop *wlBuilding_; 00169 toolbox::task::ActionSignature *asBuilding_; 00170 00171 // workloop / action signature for sending events 00172 toolbox::task::WorkLoop *wlSending_; 00173 toolbox::task::ActionSignature *asSending_; 00174 00175 // workloop / action signature for monitoring 00176 toolbox::task::WorkLoop *wlMonitoring_; 00177 toolbox::task::ActionSignature *asMonitoring_; 00178 00179 00180 std::string sourceId_; 00181 00182 // monitored parameters 00183 xdata::String url_; 00184 xdata::String class_; 00185 xdata::UnsignedInteger32 instance_; 00186 xdata::String hostname_; 00187 xdata::UnsignedInteger32 runNumber_; 00188 xdata::Double memUsedInMB_; 00189 00190 xdata::Double deltaT_; 00191 xdata::UnsignedInteger32 deltaN_; 00192 xdata::Double deltaSumOfSquares_; 00193 xdata::UnsignedInteger32 deltaSumOfSizes_; 00194 00195 xdata::Double throughput_; 00196 xdata::Double average_; 00197 xdata::Double rate_; 00198 xdata::Double rms_; 00199 00200 // monitored counters 00201 xdata::UnsignedInteger32 nbEventsInBU_; 00202 xdata::UnsignedInteger32 nbEventsRequested_; 00203 xdata::UnsignedInteger32 nbEventsBuilt_; 00204 xdata::UnsignedInteger32 nbEventsSent_; 00205 xdata::UnsignedInteger32 nbEventsDiscarded_; 00206 00207 // standard parameters 00208 xdata::String mode_; 00209 xdata::Boolean replay_; 00210 xdata::Boolean crc_; 00211 xdata::Boolean overwriteEvtId_; 00212 xdata::UnsignedInteger32 firstEvent_; 00213 xdata::UnsignedInteger32 queueSize_; 00214 xdata::UnsignedInteger32 eventBufferSize_; 00215 xdata::UnsignedInteger32 msgBufferSize_; 00216 xdata::UnsignedInteger32 fedSizeMax_; 00217 xdata::UnsignedInteger32 fedSizeMean_; 00218 xdata::UnsignedInteger32 fedSizeWidth_; 00219 xdata::Boolean useFixedFedSize_; 00220 xdata::UnsignedInteger32 monSleepSec_; 00221 00222 // gaussian aprameters for randpm fed size generation (log-normal) 00223 double gaussianMean_; 00224 double gaussianWidth_; 00225 00226 // monitoring helpers 00227 struct timeval monStartTime_; 00228 unsigned int monLastN_; 00229 uint64_t monLastSumOfSquares_; 00230 unsigned int monLastSumOfSizes_; 00231 uint64_t sumOfSquares_; 00232 unsigned int sumOfSizes_; 00233 00234 00235 // memory pool for i20 communication 00236 toolbox::mem::Pool* i2oPool_; 00237 00238 // synchronization 00239 sem_t lock_; 00240 sem_t buildSem_; 00241 sem_t sendSem_; 00242 sem_t rqstSem_; 00243 00244 00245 // 00246 // static member data 00247 // 00248 static const int frlHeaderSize_ =sizeof(frlh_t); 00249 static const int fedHeaderSize_ =sizeof(fedh_t); 00250 static const int fedTrailerSize_=sizeof(fedt_t); 00251 00252 }; // class BU 00253 00254 00255 } // namespace evf 00256 00257 00258 #endif