CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ResourceEnforcer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Services
4 // Class : ResourceEnforcer
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Chris Jones
10 // Created: Sun May 6 12:16:49 CDT 2012
11 //
12 
13 // system include files
14 
15 // user include files
23 
24 
25 namespace edm {
26  class Event;
27  class EventSetup;
28 
29  namespace service {
31  public:
33 
34  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
35 
36  private:
37  void check();
39 
42 
43  double m_maxVSize;
44  double m_maxRSS;
45  double m_maxTime;
46  unsigned int m_nEventsToSkip;
47  std::atomic<unsigned int> m_eventCount;
48  std::atomic<bool> m_doingCheck;
49  };
50  }
51 }
52 
53 using namespace edm::service;
54 //
55 // constants, enums and typedefs
56 //
57 
58 //
59 // static data member definitions
60 //
61 
62 //
63 // constructors and destructor
64 //
66 m_maxVSize(iConfig.getUntrackedParameter<double>("maxVSize",0)*1000.), //convert to MB
67 m_maxRSS(iConfig.getUntrackedParameter<double>("maxRSS",0)*1000.),
68 m_maxTime(iConfig.getUntrackedParameter<double>("maxTime",0)*60.*60.), //convert from hours to seconds
69 m_nEventsToSkip(0),
70 m_eventCount(0),
71 m_doingCheck(false)
72 {
74  iReg.watchPreallocate([this](edm::service::SystemBounds const& iBounds){
75  //We do not want the frequency of checking to be dependent on
76  // how many parallel streams are running
77  m_nEventsToSkip = iBounds.maxNumberOfStreams()-1;
78  });
79  m_timer.start();
80 
81 }
82 
83 //
84 // member functions
85 //
86 
87 //
88 // const member functions
89 //
90 
91 void
93  //If another thread is already doing a check, we don't need to do another one
94  auto count = ++m_eventCount;
95  if(count> m_nEventsToSkip) {
96  bool expected = false;
97  if( m_doingCheck.compare_exchange_strong(expected,true,std::memory_order_acq_rel) ) {
98  this->check();
99  m_doingCheck.store(false,std::memory_order_release);
100  m_eventCount.store(0,std::memory_order_release);
101  }
102  }
103 }
104 
105 
106 void
108 {
110 
111  if(0!=m_maxVSize && m_maxVSize< pi.vsize) {
112  throw edm::Exception(errors::ExceededResourceVSize)<<"Exceeded maximum allowed VSize of "<<m_maxVSize/1000.<<" GB (VSize is "<<pi.vsize/1000.<<")";
113  }
114 
115  if(0!=m_maxRSS && m_maxRSS< pi.rss) {
116  throw edm::Exception(errors::ExceededResourceRSS)<<"Exceeded maximum allowed RSS of "<<m_maxRSS/1000.<<" GB (VSize is "<<pi.rss/1000.<<")";
117  }
118 
119  if(0!=m_maxTime && m_maxTime < m_timer.realTime()) {
120  throw edm::Exception(errors::ExceededResourceTime)<<"Exceeded maximum allowed time of "<<m_maxTime/60./60.<<" hours";
121  }
122 }
123 
124 
125 //
126 // static member functions
127 //
128 
131  desc.addUntracked<double>("maxVSize", 0.)->setComment("Maximum allowed VSize for the job in GB. Ignored if set to 0.");
132  desc.addUntracked<double>("maxRSS", 0.)->setComment("Maximum allowd RSS for the job in GB. Ignored if set to 0.");
133  desc.addUntracked<double>("maxTime", 0.)->setComment("Maximum allowd wallclock time for the job in hours. Ignored if set to 0.");
134  descriptions.add("ResourceEnforcer", desc);
135 }
136 
void setComment(std::string const &value)
void watchPreallocate(Preallocate::slot_type const &iSlot)
void start()
Definition: CPUTimer.cc:74
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void watchPostEvent(PostEvent::slot_type const &iSlot)
void postEventProcessing(edm::StreamContext const &)
const Double_t pi
unsigned int maxNumberOfStreams() const
Definition: SystemBounds.h:43
ResourceEnforcer(edm::ParameterSet const &iConfig, edm::ActivityRegistry &iAR)
std::atomic< bool > m_doingCheck
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:113
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
volatile std::atomic< bool > shutdown_flag false
double realTime() const
Definition: CPUTimer.cc:150
std::atomic< unsigned int > m_eventCount