CMS 3D CMS Logo

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 #include <iostream>
14 #include <thread>
15 #include <mutex>
16 
17 namespace evf {
19  public:
20  enum Directory { mInvalid = 0, mBase, mBU, mCOUNT };
22  : highWaterMark_(pset.getUntrackedParameter<double>("highWaterMark", 0.8)),
23  lowWaterMark_(pset.getUntrackedParameter<double>("lowWaterMark", 0.5)),
25  whatToThrottleOn_(Directory(pset.getUntrackedParameter<int>("dirCode", mBase))),
27  sleep_(pset.getUntrackedParameter<unsigned int>("sleepmSecs", 1000)) {
31  }
33  void preBeginRun(edm::GlobalContext const& gc) {
34  //obtain directory to stat on
35  switch (whatToThrottleOn_) {
36  case mInvalid:
37  //do nothing
38  break;
39  case mBase:
40  baseDir_ = edm::Service<EvFDaqDirector>()->baseRunDir();
41  break;
42  case mBU:
43  baseDir_ = edm::Service<EvFDaqDirector>()->buBaseRunDir();
44  break;
45  default:
46  baseDir_ = edm::Service<EvFDaqDirector>()->baseRunDir();
47  }
48  start();
49  }
50  void postBeginRun(edm::GlobalContext const& gc) {}
51 
52  void postEndRun(edm::GlobalContext const& gc) { stop(); }
53  void preBeginLumi(edm::GlobalContext const& gc) {
54  lock_.lock();
55  lock_.unlock();
56  }
57  bool throttled() const { return throttled_; }
58 
59  private:
60  void dowork() {
62  struct statvfs buf;
63  while (!m_stoprequest) {
64  int retval = statvfs(baseDir_.c_str(), &buf);
65  if (retval != 0) {
66  std::cout << " building throttle - unable to stat " << baseDir_ << std::endl;
67  m_stoprequest = true;
68  continue;
69  }
70  double fraction = 1. - float(buf.f_bfree * buf.f_bsize) / float(buf.f_blocks * buf.f_frsize);
71  bool highwater_ = fraction > highWaterMark_;
72  bool lowwater_ = fraction < lowWaterMark_;
73  if (highwater_ && !throttled_) {
74  lock_.lock();
75  throttled_ = true;
76  std::cout << ">>>>throttling on " << std::endl;
77  }
78  if (lowwater_ && throttled_) {
79  lock_.unlock();
80  throttled_ = false;
81  }
82  std::cout << " building throttle on " << baseDir_ << " is " << fraction * 100 << " %full " << std::endl;
83  //edm::Service<EvFDaqDirector>()->writeDiskAndThrottleStat(fraction,highwater_,lowwater_);
84  ::usleep(sleep_ * 1000);
85  if (edm::shutdown_flag) {
86  std::cout << " Shutdown flag set: stop throttling" << std::endl;
87  break;
88  }
89  }
90  if (throttled_)
91  lock_.unlock();
92  }
93  void start() {
94  assert(!m_thread);
96  m_thread = std::make_shared<std::thread>(std::bind(&EvFBuildingThrottle::dowork, this));
97  std::cout << "throttle thread started - throttle on " << whatToThrottleOn_ << std::endl;
98  }
99  void stop() {
100  assert(m_thread);
101  m_stoprequest = true;
102  m_thread->join();
103  }
104 
107  std::atomic<bool> m_stoprequest;
108  std::shared_ptr<std::thread> m_thread;
114  unsigned int sleep_;
115  };
116 } // namespace evf
117 
118 #endif
evf::EvFBuildingThrottle::preBeginLumi
void preBeginLumi(edm::GlobalContext const &gc)
Definition: EvFBuildingThrottle.h:53
evf::EvFBuildingThrottle::postEndRun
void postEndRun(edm::GlobalContext const &gc)
Definition: EvFBuildingThrottle.h:52
evf::EvFBuildingThrottle::throttled_
bool throttled_
Definition: EvFBuildingThrottle.h:113
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
funct::false
false
Definition: Factorize.h:29
evf::EvFBuildingThrottle::m_stoprequest
std::atomic< bool > m_stoprequest
Definition: EvFBuildingThrottle.h:107
evf::EvFBuildingThrottle::mBU
Definition: EvFBuildingThrottle.h:20
edm::ActivityRegistry::watchPostGlobalEndRun
void watchPostGlobalEndRun(PostGlobalEndRun::slot_type const &iSlot)
Definition: ActivityRegistry.h:334
evf::EvFBuildingThrottle::postBeginRun
void postBeginRun(edm::GlobalContext const &gc)
Definition: EvFBuildingThrottle.h:50
evf::EvFBuildingThrottle::highWaterMark_
double highWaterMark_
Definition: EvFBuildingThrottle.h:105
gather_cfg.cout
cout
Definition: gather_cfg.py:144
evf::EvFBuildingThrottle::m_thread
std::shared_ptr< std::thread > m_thread
Definition: EvFBuildingThrottle.h:108
evf::EvFBuildingThrottle::lowWaterMark_
double lowWaterMark_
Definition: EvFBuildingThrottle.h:106
evf::EvFBuildingThrottle::throttled
bool throttled() const
Definition: EvFBuildingThrottle.h:57
cms::cuda::assert
assert(be >=bs)
evf::EvFBuildingThrottle::token_
edm::ServiceToken token_
Definition: EvFBuildingThrottle.h:112
evf::EvFBuildingThrottle::baseDir_
std::string baseDir_
Definition: EvFBuildingThrottle.h:110
ActivityRegistry.h
edm::ServiceToken
Definition: ServiceToken.h:42
HLT_FULL_cff.fraction
fraction
Definition: HLT_FULL_cff.py:52806
Service.h
edm::ActivityRegistry
Definition: ActivityRegistry.h:134
evf::EvFBuildingThrottle::~EvFBuildingThrottle
~EvFBuildingThrottle()
Definition: EvFBuildingThrottle.h:32
evf::EvFBuildingThrottle::dowork
void dowork()
Definition: EvFBuildingThrottle.h:60
evf::EvFBuildingThrottle::mInvalid
Definition: EvFBuildingThrottle.h:20
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
UnixSignalHandlers.h
edm::GlobalContext
Definition: GlobalContext.h:29
edm::ParameterSet
Definition: ParameterSet.h:47
Timestamp.h
edm::ServiceRegistry::presentToken
ServiceToken presentToken() const
Definition: ServiceRegistry.cc:63
edm::shutdown_flag
volatile std::atomic< bool > shutdown_flag
Definition: UnixSignalHandlers.cc:22
evf::EvFBuildingThrottle::lock_
std::mutex lock_
Definition: EvFBuildingThrottle.h:109
edm::Service
Definition: Service.h:30
createfilelist.int
int
Definition: createfilelist.py:10
EvFDaqDirector.h
mutex
static std::mutex mutex
Definition: Proxy.cc:8
edm::ServiceRegistry::instance
static ServiceRegistry & instance()
Definition: ServiceRegistry.cc:90
visDQMUpload.buf
buf
Definition: visDQMUpload.py:154
evf::EvFBuildingThrottle
Definition: EvFBuildingThrottle.h:18
edm::ActivityRegistry::watchPreGlobalBeginLumi
void watchPreGlobalBeginLumi(PreGlobalBeginLumi::slot_type const &iSlot)
Definition: ActivityRegistry.h:391
evf::EvFBuildingThrottle::mCOUNT
Definition: EvFBuildingThrottle.h:20
evf::EvFBuildingThrottle::preBeginRun
void preBeginRun(edm::GlobalContext const &gc)
Definition: EvFBuildingThrottle.h:33
evf
Definition: fillJson.h:27
evf::EvFBuildingThrottle::Directory
Directory
Definition: EvFBuildingThrottle.h:20
evf::EvFBuildingThrottle::mBase
Definition: EvFBuildingThrottle.h:20
evf::EvFBuildingThrottle::start
void start()
Definition: EvFBuildingThrottle.h:93
evf::EvFBuildingThrottle::whatToThrottleOn_
Directory whatToThrottleOn_
Definition: EvFBuildingThrottle.h:111
evf::EvFBuildingThrottle::stop
void stop()
Definition: EvFBuildingThrottle.h:99
edm::ActivityRegistry::watchPreGlobalBeginRun
void watchPreGlobalBeginRun(PreGlobalBeginRun::slot_type const &iSlot)
Definition: ActivityRegistry.h:317
ParameterSet.h
evf::EvFBuildingThrottle::sleep_
unsigned int sleep_
Definition: EvFBuildingThrottle.h:114
evf::EvFBuildingThrottle::EvFBuildingThrottle
EvFBuildingThrottle(const edm::ParameterSet &pset, edm::ActivityRegistry &reg)
Definition: EvFBuildingThrottle.h:21
edm::ServiceRegistry::Operate
Definition: ServiceRegistry.h:40
RunID.h
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27