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 <memory>
13 
14 #include <vector>
15 #include <atomic>
16 #include <chrono>
17 #include <iostream>
18 
27 
28 namespace edm {
29  namespace service {
31  public:
34  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
35 
36  private:
37  void start();
38  void stop();
39 
40  bool trackModule(ModuleCallingContext const& iContext) const;
41  std::unique_ptr<std::atomic<std::chrono::steady_clock::rep>[]> m_timeSums;
42  std::vector<std::string> m_modulesToExclude;
43  std::vector<unsigned int> m_excludedModuleIds;
44  std::chrono::steady_clock::time_point m_time;
45  unsigned int m_nTimeSums = 0;
46  unsigned int m_nModules;
47  unsigned int m_maxNModules = 0;
48  const unsigned int m_padding;
49  std::atomic<bool> m_spinLock;
51  const bool m_excludeSource;
53  };
54  } // namespace service
55 } // namespace edm
56 
57 using namespace edm::service;
58 // system include files
59 
60 // user include files
61 
62 //
63 // constants, enums and typedefs
64 //
65 
66 //
67 // static data member definitions
68 //
69 
70 //
71 // constructors and destructor
72 //
74  : m_modulesToExclude(iConfig.getUntrackedParameter<std::vector<std::string>>("modulesToExclude")),
75  m_time(),
76  m_nModules(0),
77  m_padding(iConfig.getUntrackedParameter<unsigned int>("padding")),
78  m_spinLock{false},
79  m_startedTiming(false),
80  m_excludeSource(iConfig.getUntrackedParameter<bool>("excludeSource")),
81  m_trackGlobalBeginRun(iConfig.getUntrackedParameter<bool>("trackGlobalBeginRun")) {
82  if (not m_modulesToExclude.empty()) {
83  iReg.watchPreModuleConstruction([this](ModuleDescription const& iMod) {
84  for (auto const& name : m_modulesToExclude) {
85  if (iMod.moduleLabel() == name) {
86  m_excludedModuleIds.push_back(iMod.id());
87  break;
88  }
89  }
90  });
91  iReg.watchPreModuleDestruction([this](ModuleDescription const& iMod) {
92  auto found = std::find(m_excludedModuleIds.begin(), m_excludedModuleIds.end(), iMod.id());
93  if (found != m_excludedModuleIds.end()) {
94  m_excludedModuleIds.erase(found);
95  }
96  });
97  iReg.watchPreModuleEvent([this](StreamContext const&, ModuleCallingContext const& iContext) {
98  if (trackModule(iContext)) {
99  start();
100  }
101  });
102  iReg.watchPostModuleEvent([this](StreamContext const&, ModuleCallingContext const& iContext) {
103  if (trackModule(iContext)) {
104  stop();
105  }
106  });
107 
108  iReg.watchPreModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const& iContext) {
109  if (trackModule(iContext)) {
110  if (iContext.state() == ModuleCallingContext::State::kRunning) {
111  stop();
112  }
113  }
114  });
115  iReg.watchPostModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const& iContext) {
116  if (trackModule(iContext)) {
117  if (iContext.state() == ModuleCallingContext::State::kRunning) {
118  start();
119  }
120  }
121  });
122 
123  } else {
124  //apply to all modules so can use faster version
125  iReg.watchPreModuleEvent([this](StreamContext const&, ModuleCallingContext const&) { start(); });
126  iReg.watchPostModuleEvent([this](StreamContext const&, ModuleCallingContext const&) { stop(); });
127 
128  iReg.watchPreModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const& iContext) {
129  if (iContext.state() == ModuleCallingContext::State::kRunning) {
130  stop();
131  }
132  });
133  iReg.watchPostModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const& iContext) {
134  if (iContext.state() == ModuleCallingContext::State::kRunning) {
135  start();
136  }
137  });
138  if (m_trackGlobalBeginRun) {
139  iReg.watchPreModuleGlobalBeginRun([this](GlobalContext const&, ModuleCallingContext const&) {
140  if (not m_startedTiming) {
142  m_startedTiming = true;
143  }
144 
145  start();
146  });
147  iReg.watchPostModuleGlobalBeginRun([this](GlobalContext const&, ModuleCallingContext const&) { stop(); });
148  }
149  }
150 
151  iReg.watchPreallocate([this](edm::service::SystemBounds const& iBounds) {
152  m_nTimeSums = iBounds.maxNumberOfThreads() + 1 + m_padding;
153  m_timeSums = std::make_unique<std::atomic<std::chrono::steady_clock::rep>[]>(m_nTimeSums);
154  for (unsigned int i = 0; i < m_nTimeSums; ++i) {
155  m_timeSums[i] = 0;
156  }
157  });
158 
159  iReg.watchPreSourceEvent([this](StreamID) {
160  if (not m_startedTiming) {
162  m_startedTiming = true;
163  }
164  if (not m_excludeSource) {
165  start();
166  }
167  });
168  if (not m_excludeSource) {
169  iReg.watchPostSourceEvent([this](StreamID) { stop(); });
170  }
171 }
172 
174  std::cout << "Maximum concurrent running modules: " << m_maxNModules << std::endl;
175  std::cout << "Fraction of time running n Modules simultaneously" << std::endl;
176  for (unsigned int i = 0; i < m_nTimeSums; ++i) {
177  std::cout << i << " " << m_timeSums[i] / double(m_timeSums[0]) << " " << m_timeSums[i] << std::endl;
178  }
179 }
180 
181 // ConcurrentModuleTimer::ConcurrentModuleTimer(const ConcurrentModuleTimer& rhs)
182 // {
183 // // do actual copying here;
184 // }
185 
186 //
187 // assignment operators
188 //
189 // const ConcurrentModuleTimer& ConcurrentModuleTimer::operator=(const ConcurrentModuleTimer& rhs)
190 // {
191 // //An exception safe implementation is
192 // ConcurrentModuleTimer temp(rhs);
193 // swap(rhs);
194 //
195 // return *this;
196 // }
197 
198 //
199 // member functions
200 //
202  auto const newTime = std::chrono::steady_clock::now();
203  std::chrono::steady_clock::time_point oldTime;
204  bool expected = false;
205  unsigned int nModules;
206  while (not m_spinLock.compare_exchange_strong(expected, true, std::memory_order_acq_rel)) {
207  expected = false;
208  }
209  {
210  oldTime = m_time;
211  m_time = newTime;
212  nModules = ++m_nModules;
213  if (nModules > m_maxNModules) {
214  m_maxNModules = nModules;
215  }
216  m_spinLock.store(false, std::memory_order_release);
217  }
218  assert(nModules < m_nTimeSums);
219  auto diff = newTime - oldTime;
220  for (unsigned int i = 0; i < nModules; ++i) {
221  m_timeSums[i].fetch_add(diff.count());
222  }
223 }
224 
226  auto const newTime = std::chrono::steady_clock::now();
227  std::chrono::steady_clock::time_point oldTime;
228  bool expected = false;
229  unsigned int nModules;
230  while (not m_spinLock.compare_exchange_weak(expected, true, std::memory_order_acq_rel)) {
231  expected = false;
232  }
233  {
234  oldTime = m_time;
235  m_time = newTime;
236  nModules = m_nModules--;
237  m_spinLock.store(false, std::memory_order_release);
238  }
239  assert(nModules < m_nTimeSums);
240  auto diff = newTime - oldTime;
241  for (unsigned int i = 0; i <= nModules; ++i) {
242  m_timeSums[i].fetch_add(diff.count());
243  }
244 }
245 
246 //
247 // const member functions
248 //
250  auto modId = iContext.moduleDescription()->id();
251  for (auto const id : m_excludedModuleIds) {
252  if (modId == id) {
253  return false;
254  }
255  }
256  return true;
257 }
258 
259 //
260 // static member functions
261 //
264  desc.addUntracked<std::vector<std::string>>("modulesToExclude", std::vector<std::string>{})
265  ->setComment("Module labels to exclude from the timing measurements");
266  desc.addUntracked<bool>("excludeSource", false)->setComment("Exclude the time the source is running");
267  desc.addUntracked<unsigned int>("padding", 0)
268  ->setComment(
269  "[Expert use only] Extra possible concurrent modules beyond thread count.\n Only useful in debugging "
270  "possible framework scheduling problems.");
271  desc.addUntracked<bool>("trackGlobalBeginRun", false)
272  ->setComment("Check for concurrent modules during global begin run");
273  descriptions.add("ConcurrentModuleTimer", desc);
274 }
275 
ModuleDescription const * moduleDescription() const
bool trackModule(ModuleCallingContext const &iContext) const
ConcurrentModuleTimer(edm::ParameterSet const &iConfig, edm::ActivityRegistry &iAR)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
assert(be >=bs)
std::vector< std::string > m_modulesToExclude
std::chrono::steady_clock::time_point m_time
unsigned int id() const
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:97
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::vector< unsigned int > m_excludedModuleIds
unsigned int maxNumberOfThreads() const
Definition: SystemBounds.h:38
HLT enums.
std::unique_ptr< std::atomic< std::chrono::steady_clock::rep >[]> m_timeSums
std::string const & moduleLabel() const