![]() |
![]() |
00001 00002 // 00003 // SharedResources.h 00004 // ------- 00005 // 00006 // Resources shared between FSM states. 00007 // 00008 // Created on: Sep 21, 2011 00009 // Andrei Spataru : aspataru@cern.ch 00011 00012 #ifndef RBSHAREDRESOURCES_H_ 00013 #define RBSHAREDRESOURCES_H_ 00014 00015 #include "EventFilter/ResourceBroker/interface/RBStateMachine.h" 00016 #include "EventFilter/ResourceBroker/interface/BUProxy.h" 00017 #include "EventFilter/ResourceBroker/interface/SMProxy.h" 00018 #include "EventFilter/ResourceBroker/interface/IPCManager.h" 00019 #include "EventFilter/ResourceBroker/interface/FUResource.h" 00020 #include "EventFilter/ResourceBroker/interface/CommandQueue.h" 00021 00022 #include "EventFilter/Utilities/interface/IndependentWebGUI.h" 00023 00024 #include "boost/statechart/event_base.hpp" 00025 #include <boost/shared_ptr.hpp> 00026 00027 #include "xdata/InfoSpace.h" 00028 #include "xdata/Integer32.h" 00029 #include "xdata/UnsignedInteger32.h" 00030 #include "xdata/Integer.h" 00031 #include "xdata/Double.h" 00032 #include "xdata/Boolean.h" 00033 #include "xdata/String.h" 00034 00035 #include "toolbox/task/Action.h" 00036 #include "toolbox/task/WorkLoop.h" 00037 #include "toolbox/task/WorkLoopFactory.h" 00038 00039 #include <semaphore.h> 00040 #include <iostream> 00041 #include <queue> 00042 00043 namespace evf { 00044 00045 class FUResourceBroker; 00046 00047 namespace rb_statemachine { 00048 00049 typedef boost::shared_ptr<boost::statechart::event_base> EventPtr; 00050 00051 // forward declaration of state classes 00052 class Halted; 00053 class Configuring; 00054 class Ready; 00055 class Stopped; 00056 class Enabling; 00057 class Enabled; 00058 class Running; 00059 class Stopping; 00060 class Halting; 00061 class Normal; 00062 class Failed; 00063 00071 class SharedResources: public toolbox::lang::Class { 00072 00073 public: 00074 SharedResources(Logger log); 00075 ~SharedResources(); 00076 00077 // in SharedResources because it's called by Configuring and Stopping 00081 void configureResources(xdaq::Application* app); 00082 00083 // in SharedResources because it's called by Configuring and Stopping 00087 void reset(); 00088 void updateGUIExternalState(std::string newState) { 00089 //lock(); 00090 gui_->updateExternalState(newState); 00091 //unlock(); 00092 } 00093 void updateGUIInternalState(std::string newState) { 00094 //lock(); 00095 gui_->updateInternalState(newState); 00096 //unlock(); 00097 } 00098 void setFsmPointer(RBStateMachine* const fsm) { 00099 fsm_ = fsm; 00100 } 00101 00102 void cancelAllWorkloops(); 00103 00104 /* 00105 * CONCURRENT ACCESS 00106 */ 00107 void lock() { 00108 while (0 != sem_wait(&lock_)) { 00109 if (errno != EINTR) { 00110 LOG4CPLUS_ERROR(log_, "Cannot obtain lock on sem LOCK!"); 00111 } 00112 } 00113 } 00114 void unlock() { 00115 sem_post(&lock_); 00116 } 00117 00118 void lockRSAccess() { 00119 while (0 != sem_wait(&accessToResourceStructureLock_)) { 00120 if (errno != EINTR) { 00121 LOG4CPLUS_ERROR(log_, "Cannot obtain lock on sem accessToResourceStructureLock_!"); 00122 } 00123 } 00124 } 00125 00126 int tryLockRSAccess() { 00127 if (sem_trywait(&accessToResourceStructureLock_)) return -1; 00128 return 0; 00129 } 00130 00131 void unlockRSAccess() { 00132 sem_post(&accessToResourceStructureLock_); 00133 } 00134 00135 void printWorkLoopStatus(); 00136 00137 private: 00138 00139 /* 00140 * Workloop starters and dispatch actions. 00141 * Used by friend state classes. 00142 */ 00146 void startMonitoringWorkLoop() throw (evf::Exception); 00150 bool monitoring(toolbox::task::WorkLoop* wl); 00154 void startWatchingWorkLoop() throw (evf::Exception); 00158 bool watching(toolbox::task::WorkLoop* wl); 00159 double deltaT(const struct timeval *start, const struct timeval *end); 00160 00164 void startSendDataWorkLoop() throw (evf::Exception); 00168 bool sendData(toolbox::task::WorkLoop* wl); 00169 00173 void startSendDqmWorkLoop() throw (evf::Exception); 00177 bool sendDqm(toolbox::task::WorkLoop* wl); 00178 00182 void startDiscardWorkLoop() throw (evf::Exception); 00186 bool discard(toolbox::task::WorkLoop* wl); 00187 00191 void goToFailedState(evf::Exception& e); 00192 00193 private: 00194 00195 typedef toolbox::task::WorkLoop WorkLoop_t; 00196 typedef toolbox::task::ActionSignature ActionSignature_t; 00197 00198 /* 00199 * Workloops and action signatures 00200 */ 00201 00202 // workloop / action signature for monitoring 00203 WorkLoop_t *wlMonitoring_; 00204 ActionSignature_t *asMonitoring_; 00205 // workloop / action signature for watching 00206 WorkLoop_t *wlWatching_; 00207 ActionSignature_t *asWatching_; 00208 00209 WorkLoop_t *wlSendData_; 00210 ActionSignature_t *asSendData_; 00211 WorkLoop_t *wlSendDqm_; 00212 ActionSignature_t *asSendDqm_; 00213 WorkLoop_t *wlDiscard_; 00214 ActionSignature_t *asDiscard_; 00215 00216 // 00217 // member data 00218 // 00219 00220 // pointer to FSM 00221 RBStateMachine* fsm_; 00222 00223 // Hyper DAQ Independent web GUI 00224 IndependentWebGUI* gui_; 00225 00226 // Command Queue containing state machine events 00227 CommandQueue commands_; 00228 00229 // application logger 00230 Logger log_; 00231 00232 // BuilderUnit (BU) to receive raw even data from 00233 BUProxy *bu_; 00234 00235 // StorageManager (SM) to send selected events to 00236 SMProxy *sm_; 00237 00238 // memory pool for bu <-> fu comunication messages 00239 toolbox::mem::Pool* i2oPool_; 00240 00241 // managed resources 00242 IPCManager* ipcManager_; 00243 IPCMethod* resourceStructure_; 00244 00245 // application identifier 00246 std::string sourceId_; 00247 00248 // monitored parameters 00249 xdata::UnsignedInteger32 runNumber_; 00250 00251 xdata::Double deltaT_; 00252 xdata::UnsignedInteger32 deltaN_; 00253 xdata::Double deltaSumOfSquares_; 00254 xdata::UnsignedInteger32 deltaSumOfSizes_; 00255 00256 xdata::Double throughput_; 00257 xdata::Double rate_; 00258 xdata::Double average_; 00259 xdata::Double rms_; 00260 00261 // monitored counters 00262 xdata::UnsignedInteger32 nbAllocatedEvents_; 00263 xdata::UnsignedInteger32 nbPendingRequests_; 00264 xdata::UnsignedInteger32 nbReceivedEvents_; 00265 xdata::UnsignedInteger32 nbProcessedEvents_; 00266 xdata::UnsignedInteger32 nbSentEvents_; 00267 xdata::UnsignedInteger32 nbSentDqmEvents_; 00268 xdata::UnsignedInteger32 nbSentErrorEvents_; 00269 xdata::UnsignedInteger32 nbPendingSMDiscards_; 00270 xdata::UnsignedInteger32 nbPendingSMDqmDiscards_; 00271 xdata::UnsignedInteger32 nbDiscardedEvents_; 00272 00273 // UPDATED 00274 xdata::UnsignedInteger32 nbReceivedEol_; 00275 xdata::UnsignedInteger32 highestEolReceived_; 00276 xdata::UnsignedInteger32 nbEolPosted_; 00277 xdata::UnsignedInteger32 nbEolDiscarded_; 00278 00279 xdata::UnsignedInteger32 nbLostEvents_; 00280 xdata::UnsignedInteger32 nbDataErrors_; 00281 xdata::UnsignedInteger32 nbCrcErrors_; 00282 xdata::UnsignedInteger32 nbTimeoutsWithEvent_; 00283 xdata::UnsignedInteger32 nbTimeoutsWithoutEvent_; 00284 xdata::UnsignedInteger32 dataErrorFlag_; 00285 00286 // standard parameters 00287 xdata::Boolean segmentationMode_; 00288 xdata::Boolean useMessageQueueIPC_; 00289 xdata::UnsignedInteger32 nbClients_; 00290 xdata::String clientPrcIds_; 00291 xdata::UnsignedInteger32 nbRawCells_; 00292 xdata::UnsignedInteger32 nbRecoCells_; 00293 xdata::UnsignedInteger32 nbDqmCells_; 00294 xdata::UnsignedInteger32 rawCellSize_; 00295 xdata::UnsignedInteger32 recoCellSize_; 00296 xdata::UnsignedInteger32 dqmCellSize_; 00297 // UPDATE freeResourcesRequiredForAllocate 00298 xdata::Integer freeResRequiredForAllocate_; 00299 00300 xdata::Boolean doDropEvents_; 00301 xdata::Boolean doFedIdCheck_; 00302 xdata::UnsignedInteger32 doCrcCheck_; 00303 xdata::UnsignedInteger32 doDumpEvents_; 00304 00305 xdata::String buClassName_; 00306 xdata::UnsignedInteger32 buInstance_; 00307 xdata::String smClassName_; 00308 xdata::UnsignedInteger32 smInstance_; 00309 00310 xdata::UnsignedInteger32 resourceStructureTimeout_; 00311 xdata::UnsignedInteger32 monSleepSec_; 00312 xdata::UnsignedInteger32 watchSleepSec_; 00313 xdata::UnsignedInteger32 timeOutSec_; 00314 xdata::Boolean processKillerEnabled_; 00315 xdata::Boolean useEvmBoard_; 00316 00317 xdata::String reasonForFailed_; 00318 00319 // debug parameters 00320 xdata::UnsignedInteger32 nbAllocateSent_; 00321 xdata::UnsignedInteger32 nbTakeReceived_; 00322 xdata::UnsignedInteger32 nbDataDiscardReceived_; 00323 xdata::UnsignedInteger32 nbDqmDiscardReceived_; 00324 00325 // helper variables for monitoring 00326 struct timeval monStartTime_; 00327 UInt_t nbSentLast_; 00328 uint64_t sumOfSquaresLast_; 00329 UInt_t sumOfSizesLast_; 00330 00331 // lock 00332 sem_t lock_; 00333 EvffedFillerRB *frb_; 00334 00335 bool shmInconsistent_; 00336 00337 // emergency stop protection against I2O access 00338 sem_t accessToResourceStructureLock_; 00339 bool allowI2ODiscards_; 00340 00341 /* 00342 * FRIENDS 00343 */ 00344 friend class evf::FUResourceBroker; 00345 00346 friend class Halted; 00347 friend class Configuring; 00348 friend class Ready; 00349 friend class Stopped; 00350 friend class Enabling; 00351 friend class Enabled; 00352 friend class Running; 00353 friend class Stopping; 00354 friend class Halting; 00355 friend class Normal; 00356 friend class Failed; 00357 }; 00358 00359 typedef boost::shared_ptr<SharedResources> SharedResourcesPtr_t; 00360 00361 } // end namespace rb_statemachine 00362 00363 } //end namespace evf 00364 00365 #endif /* RBSHAREDRESOURCES_H_ */