CMS 3D CMS Logo

ConcurrentModuleTimer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Subsystem/Package
4 // Class : ConcurrentModuleTimer
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Chris Jones
10 // Created: Tue, 10 Dec 2013 21:16:00 GMT
11 //
12 #include <vector>
13 #include <atomic>
14 #include <chrono>
15 #include <iostream>
16 
25 
26 
27 namespace edm {
28  namespace service {
30  public:
33  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
34  private:
35  void start();
36  void stop();
37 
38  bool trackModule(ModuleCallingContext const& iContext) const;
39  std::unique_ptr<std::atomic<std::chrono::high_resolution_clock::rep>[]> m_timeSums;
40  std::vector<std::string> m_modulesToExclude;
41  std::vector<unsigned int> m_excludedModuleIds;
42  std::chrono::high_resolution_clock::time_point m_time;
43  unsigned int m_nTimeSums;
44  unsigned int m_nModules;
45  std::atomic<bool> m_spinLock;
48  };
49  }
50 }
51 
52 using namespace edm::service;
53 // system include files
54 
55 // user include files
56 
57 //
58 // constants, enums and typedefs
59 //
60 
61 //
62 // static data member definitions
63 //
64 
65 //
66 // constructors and destructor
67 //
69 m_modulesToExclude(iConfig.getUntrackedParameter<std::vector<std::string>>("modulesToExclude")),
70 m_time(),
71 m_nModules(0),
72 m_spinLock{false},
73 m_startedTiming(false),
74 m_excludeSource(iConfig.getUntrackedParameter<bool>("excludeSource"))
75 {
76  if(not m_modulesToExclude.empty()) {
77  iReg.watchPreModuleConstruction( [this](ModuleDescription const& iMod) {
78  for(auto const& name: m_modulesToExclude) {
79  if( iMod.moduleLabel() == name) {
80  m_excludedModuleIds.push_back(iMod.id());
81  break;
82  }
83  }
84  });
85  iReg.watchPreModuleEvent([this](StreamContext const&, ModuleCallingContext const& iContext){
86  if(trackModule(iContext)) {
87  start();
88  }
89  });
90  iReg.watchPostModuleEvent([this](StreamContext const&, ModuleCallingContext const& iContext){
91  if(trackModule(iContext)) {
92  stop();
93  }
94  });
95 
96  iReg.watchPreModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const& iContext){
97  if(trackModule(iContext)) {
99  stop();
100  }
101  }
102  });
103  iReg.watchPostModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const& iContext){
104  if(trackModule(iContext)) {
105  if(iContext.state() == ModuleCallingContext::State::kRunning) {
106  start();
107  }
108  }
109  });
110 
111  } else {
112  //apply to all modules so can use faster version
113  iReg.watchPreModuleEvent([this](StreamContext const&, ModuleCallingContext const&){
114  start();
115  });
116  iReg.watchPostModuleEvent([this](StreamContext const&, ModuleCallingContext const&){
117  stop();
118  });
119 
120  iReg.watchPreModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const& iContext){
121  if(iContext.state() == ModuleCallingContext::State::kRunning) {
122  stop();
123  }
124  });
125  iReg.watchPostModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const& iContext){
126  if(iContext.state() == ModuleCallingContext::State::kRunning) {
127  start();
128  }
129  });
130  }
131 
132  iReg.watchPreallocate([this](edm::service::SystemBounds const& iBounds){
133  m_nTimeSums =iBounds.maxNumberOfThreads()+1;
134  m_timeSums.reset(new std::atomic<std::chrono::high_resolution_clock::rep>[m_nTimeSums]);
135  for(unsigned int i=0; i<m_nTimeSums;++i) {
136  m_timeSums[i]=0;
137  }
138  });
139 
140  iReg.watchPreSourceEvent([this](StreamID){
141  if(not m_startedTiming) {
143  m_startedTiming=true;
144  }
145  if(not m_excludeSource) {
146  start();
147  }
148  });
149  if(not m_excludeSource) {
150  iReg.watchPostSourceEvent([this](StreamID){
151  stop();
152  });
153  }
154 }
155 
157 
158  std::cout <<"Fraction of time running n Modules simultaneously"<<std::endl;
159  for (unsigned int i=0; i<m_nTimeSums; ++i) {
160  std::cout <<i<<" "<<m_timeSums[i]/double(m_timeSums[0])<<" "<<m_timeSums[i]<<std::endl;
161  }
162 
163 }
164 
165 // ConcurrentModuleTimer::ConcurrentModuleTimer(const ConcurrentModuleTimer& rhs)
166 // {
167 // // do actual copying here;
168 // }
169 
170 //
171 // assignment operators
172 //
173 // const ConcurrentModuleTimer& ConcurrentModuleTimer::operator=(const ConcurrentModuleTimer& rhs)
174 // {
175 // //An exception safe implementation is
176 // ConcurrentModuleTimer temp(rhs);
177 // swap(rhs);
178 //
179 // return *this;
180 // }
181 
182 //
183 // member functions
184 //
185 void
187 {
188  auto const newTime =std::chrono::high_resolution_clock::now();
189  std::chrono::high_resolution_clock::time_point oldTime;
190  bool expected = false;
191  unsigned int nModules;
192  while (not m_spinLock.compare_exchange_strong(expected,true,std::memory_order_acq_rel)){
193  expected = false;
194  }
195  {
196  oldTime = m_time;
197  m_time = newTime;
198  nModules = ++m_nModules;
199  m_spinLock.store(false,std::memory_order_release);
200  }
201  assert(nModules <m_nTimeSums);
202  auto diff = newTime - oldTime;
203  for(unsigned int i=0;i<nModules;++i) {
204  m_timeSums[i].fetch_add(diff.count());
205  }
206 }
207 
208 void
210 {
211  auto const newTime =std::chrono::high_resolution_clock::now();
212  std::chrono::high_resolution_clock::time_point oldTime;
213  bool expected = false;
214  unsigned int nModules;
215  while (not m_spinLock.compare_exchange_weak(expected,true,std::memory_order_acq_rel)){
216  expected = false;
217  }
218  {
219  oldTime = m_time;
220  m_time = newTime;
221  nModules = m_nModules--;
222  m_spinLock.store(false,std::memory_order_release);
223  }
224  assert(nModules <m_nTimeSums);
225  auto diff = newTime - oldTime;
226  for(unsigned int i=0;i<=nModules;++i) {
227  m_timeSums[i].fetch_add(diff.count());
228  }
229 }
230 
231 //
232 // const member functions
233 //
234 bool
236 {
237  auto modId = iContext.moduleDescription()->id();
238  for(auto const id: m_excludedModuleIds) {
239  if(modId == id) {
240  return false;
241  }
242  }
243  return true;
244 }
245 
246 //
247 // static member functions
248 //
249 void
251 {
253  desc.addUntracked<std::vector<std::string> >("modulesToExclude", std::vector<std::string>{})->setComment("Module labels to exclude from the timing measurements");
254  desc.addUntracked<bool>("excludeSource",false)->setComment("Exclude the time the source is running");
255  descriptions.add("ConcurrentModuleTimer", desc);
256 }
257 
259 
unsigned int maxNumberOfThreads() const
Definition: SystemBounds.h:46
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
ConcurrentModuleTimer(edm::ParameterSet const &iConfig, edm::ActivityRegistry &iAR)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::string const & moduleLabel() const
std::vector< std::string > m_modulesToExclude
std::chrono::high_resolution_clock::time_point m_time
ModuleDescription const * moduleDescription() const
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:113
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::vector< unsigned int > m_excludedModuleIds
HLT enums.
bool trackModule(ModuleCallingContext const &iContext) const
std::unique_ptr< std::atomic< std::chrono::high_resolution_clock::rep >[]> m_timeSums
unsigned int id() const