test
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 
11 
12 #include <sys/statvfs.h>
13 
14 #include "boost/thread/thread.hpp"
15 
16 
17 namespace evf{
19  {
20  public:
21  enum Directory { mInvalid = 0, mBase, mBU, mCOUNT};
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  }
37  //obtain directory to stat on
38  switch(whatToThrottleOn_){
39  case mInvalid:
40  //do nothing
41  break;
42  case mBase:
43  baseDir_ = edm::Service<EvFDaqDirector>()->baseRunDir();
44  break;
45  case mBU:
46  baseDir_ = edm::Service<EvFDaqDirector>()->buBaseRunDir();
47  break;
48  default:
49  baseDir_ = edm::Service<EvFDaqDirector>()->baseRunDir();
50  }
51  start();
52  }
54  }
55 
56  void postEndRun(edm::GlobalContext const& gc){
57  stop();
58  }
60  lock_.lock();
61  lock_.unlock();
62  }
63  bool throttled() const {return throttled_;}
64  private:
65  void dowork(){
67  struct statvfs buf;
68  while(!m_stoprequest){
69  int retval = statvfs(baseDir_.c_str(),&buf);
70  if(retval != 0){
71  std::cout << " building throttle - unable to stat " << baseDir_ << std::endl;
72  m_stoprequest=true;
73  continue;
74  }
75  double fraction = 1.-float(buf.f_bfree*buf.f_bsize)/float(buf.f_blocks*buf.f_frsize);
76  bool highwater_ = fraction>highWaterMark_;
77  bool lowwater_ = fraction<lowWaterMark_;
78  if(highwater_ && !throttled_){ lock_.lock(); throttled_ = true;std::cout << ">>>>throttling on " << std::endl;}
79  if(lowwater_ && throttled_){ lock_.unlock(); throttled_ = false;}
80  std::cout << " building throttle on " << baseDir_ << " is " << fraction*100 << " %full " << std::endl;
81  //edm::Service<EvFDaqDirector>()->writeDiskAndThrottleStat(fraction,highwater_,lowwater_);
82  ::usleep(sleep_*1000);
83  if (edm::shutdown_flag) {
84  std::cout << " Shutdown flag set: stop throttling" << std::endl;
85  break;
86  }
87  }
88  if (throttled_) lock_.unlock();
89  }
90  void start(){
91  assert(!m_thread);
93  m_thread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&EvFBuildingThrottle::dowork,this)));
94  std::cout << "throttle thread started - throttle on " << whatToThrottleOn_ << std::endl;
95  }
96  void stop(){
98  m_stoprequest=true;
99  m_thread->join();
100  }
101 
104  volatile bool m_stoprequest;
105  boost::shared_ptr<boost::thread> m_thread;
111  unsigned int sleep_;
112  };
113 }
114 
115 #endif
116 
static boost::mutex mutex
Definition: LHEProxy.cc:11
assert(m_qm.get())
void watchPreGlobalBeginLumi(PreGlobalBeginLumi::slot_type const &iSlot)
void preBeginLumi(edm::GlobalContext const &gc)
volatile std::atomic< bool > shutdown_flag
ServiceToken presentToken() const
void postBeginRun(edm::GlobalContext const &gc)
static ServiceRegistry & instance()
boost::shared_ptr< boost::thread > m_thread
EvFBuildingThrottle(const edm::ParameterSet &pset, edm::ActivityRegistry &reg)
void watchPreGlobalBeginRun(PreGlobalBeginRun::slot_type const &iSlot)
void watchPostGlobalEndRun(PostGlobalEndRun::slot_type const &iSlot)
void preBeginRun(edm::GlobalContext const &gc)
tuple cout
Definition: gather_cfg.py:145
volatile std::atomic< bool > shutdown_flag false
void postEndRun(edm::GlobalContext const &gc)