CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EvFBuildingThrottle.h
Go to the documentation of this file.
1 #ifndef EVENTFILTER_UTILTIES_PLUGINS_EVFBuildingThrottle
2 #define EVENTFILTER_UTILTIES_PLUGINS_EVFBuildingThrottle
3 
9 #include "EvFDaqDirector.h"
10 
11 #include <sys/statvfs.h>
12 
13 #include "boost/thread/thread.hpp"
14 
15 
16 namespace evf{
18  {
19  public:
20  enum Directory { mInvalid = 0, mBase, mBU, mSM, mMon, mWhat,
21  mCOUNT};
22  explicit EvFBuildingThrottle( const edm::ParameterSet &pset,
23  edm::ActivityRegistry& reg )
24  : highWaterMark_(pset.getUntrackedParameter<double>("highWaterMark",0.8))
25  , lowWaterMark_(pset.getUntrackedParameter<double>("lowWaterMark",0.5))
27  , whatToThrottleOn_(Directory(pset.getUntrackedParameter<int>("dirCode",mBase)))
28  , throttled_(false)
29  , sleep_( pset.getUntrackedParameter<unsigned int>("sleepmSecs",1000))
30  {
34  }
36  void preBeginRun(edm::RunID const& id, edm::Timestamp const& ts){
37  //obtain directory to stat on
38  switch(whatToThrottleOn_){
39  case mInvalid:
40  //do nothing
41  break;
42  case mBase:
44  break;
45  case mBU:
46  baseDir_ = edm::Service<EvFDaqDirector>()->buBaseDir();
47  break;
48  case mSM:
49  baseDir_ = edm::Service<EvFDaqDirector>()->smBaseDir();
50  break;
51  case mMon:
52  baseDir_ = edm::Service<EvFDaqDirector>()->monitorBaseDir();
53  break;
54  default:
56  }
57  start();
58  }
59  void postBeginRun(edm::RunID const& id, edm::Timestamp const& ts){
60 
61  }
62 
63  void postEndRun(edm::Run const& run, edm::EventSetup const& es){
64  stop();
65  }
66  void preBeginLumi(edm::LuminosityBlockID const& iID, edm::Timestamp const& iTime){
67  lock_.lock();
68  lock_.unlock();
69  }
70  bool throttled() const {return throttled_;}
71  private:
72  void dowork(){
74  struct statvfs buf;
75  while(!m_stoprequest){
76  int retval = statvfs(baseDir_.c_str(),&buf);
77  if(retval != 0){
78  std::cout << " building throttle - unable to stat " << baseDir_ << std::endl;
79  m_stoprequest=true;
80  continue;
81  }
82  double fraction = 1.-float(buf.f_bfree*buf.f_bsize)/float(buf.f_blocks*buf.f_frsize);
83  bool highwater_ = fraction>highWaterMark_;
84  bool lowwater_ = fraction<lowWaterMark_;
85  if(highwater_ && !throttled_){ lock_.lock(); throttled_ = true;std::cout << ">>>>throttling on " << std::endl;}
86  if(lowwater_ && throttled_){ lock_.unlock(); throttled_ = false;}
87  std::cout << " building throttle on " << baseDir_ << " is " << fraction*100 << " %full " << std::endl;
88  edm::Service<EvFDaqDirector>()->writeDiskAndThrottleStat(fraction,highwater_,lowwater_);
89  ::usleep(sleep_*1000);
90  }
91  }
92  void start(){
93  assert(!m_thread);
95  m_thread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&EvFBuildingThrottle::dowork,this)));
96  std::cout << "throttle thread started - throttle on " << whatToThrottleOn_ << std::endl;
97  }
98  void stop(){
99  assert(m_thread);
100  m_stoprequest=true;
101  m_thread->join();
102  }
103 
106  volatile bool m_stoprequest;
107  boost::shared_ptr<boost::thread> m_thread;
113  unsigned int sleep_;
114  };
115 }
116 
117 #endif
118 
void preBeginLumi(edm::LuminosityBlockID const &iID, edm::Timestamp const &iTime)
void postBeginRun(edm::RunID const &id, edm::Timestamp const &ts)
static boost::mutex mutex
Definition: LHEProxy.cc:11
void postEndRun(edm::Run const &run, edm::EventSetup const &es)
ServiceToken presentToken() const
static ServiceRegistry & instance()
boost::shared_ptr< boost::thread > m_thread
EvFBuildingThrottle(const edm::ParameterSet &pset, edm::ActivityRegistry &reg)
void watchPreBeginLumi(PreBeginLumi::slot_type const &iSlot)
tuple cout
Definition: gather_cfg.py:121
void watchPostEndRun(PostEndRun::slot_type const &iSlot)
volatile std::atomic< bool > shutdown_flag false
Definition: Run.h:41
void preBeginRun(edm::RunID const &id, edm::Timestamp const &ts)
void watchPreBeginRun(PreBeginRun::slot_type const &iSlot)