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 namespace edm {
27  namespace service {
29  public:
32  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
33 
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 = 0;
44  unsigned int m_nModules;
45  std::atomic<bool> m_spinLock;
48  };
49  } // namespace service
50 } // namespace edm
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  if (not m_modulesToExclude.empty()) {
76  iReg.watchPreModuleConstruction([this](ModuleDescription const& iMod) {
77  for (auto const& name : m_modulesToExclude) {
78  if (iMod.moduleLabel() == name) {
79  m_excludedModuleIds.push_back(iMod.id());
80  break;
81  }
82  }
83  });
84  iReg.watchPreModuleEvent([this](StreamContext const&, ModuleCallingContext const& iContext) {
85  if (trackModule(iContext)) {
86  start();
87  }
88  });
89  iReg.watchPostModuleEvent([this](StreamContext const&, ModuleCallingContext const& iContext) {
90  if (trackModule(iContext)) {
91  stop();
92  }
93  });
94 
95  iReg.watchPreModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const& iContext) {
96  if (trackModule(iContext)) {
97  if (iContext.state() == ModuleCallingContext::State::kRunning) {
98  stop();
99  }
100  }
101  });
102  iReg.watchPostModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const& iContext) {
103  if (trackModule(iContext)) {
104  if (iContext.state() == ModuleCallingContext::State::kRunning) {
105  start();
106  }
107  }
108  });
109 
110  } else {
111  //apply to all modules so can use faster version
112  iReg.watchPreModuleEvent([this](StreamContext const&, ModuleCallingContext const&) { start(); });
113  iReg.watchPostModuleEvent([this](StreamContext const&, ModuleCallingContext const&) { stop(); });
114 
115  iReg.watchPreModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const& iContext) {
116  if (iContext.state() == ModuleCallingContext::State::kRunning) {
117  stop();
118  }
119  });
120  iReg.watchPostModuleEventDelayedGet([this](StreamContext const&, ModuleCallingContext const& iContext) {
121  if (iContext.state() == ModuleCallingContext::State::kRunning) {
122  start();
123  }
124  });
125  }
126 
127  iReg.watchPreallocate([this](edm::service::SystemBounds const& iBounds) {
128  m_nTimeSums = iBounds.maxNumberOfThreads() + 1;
129  m_timeSums.reset(new std::atomic<std::chrono::high_resolution_clock::rep>[m_nTimeSums]);
130  for (unsigned int i = 0; i < m_nTimeSums; ++i) {
131  m_timeSums[i] = 0;
132  }
133  });
134 
135  iReg.watchPreSourceEvent([this](StreamID) {
136  if (not m_startedTiming) {
138  m_startedTiming = true;
139  }
140  if (not m_excludeSource) {
141  start();
142  }
143  });
144  if (not m_excludeSource) {
145  iReg.watchPostSourceEvent([this](StreamID) { stop(); });
146  }
147 }
148 
150  std::cout << "Fraction of time running n Modules simultaneously" << std::endl;
151  for (unsigned int i = 0; i < m_nTimeSums; ++i) {
152  std::cout << i << " " << m_timeSums[i] / double(m_timeSums[0]) << " " << m_timeSums[i] << std::endl;
153  }
154 }
155 
156 // ConcurrentModuleTimer::ConcurrentModuleTimer(const ConcurrentModuleTimer& rhs)
157 // {
158 // // do actual copying here;
159 // }
160 
161 //
162 // assignment operators
163 //
164 // const ConcurrentModuleTimer& ConcurrentModuleTimer::operator=(const ConcurrentModuleTimer& rhs)
165 // {
166 // //An exception safe implementation is
167 // ConcurrentModuleTimer temp(rhs);
168 // swap(rhs);
169 //
170 // return *this;
171 // }
172 
173 //
174 // member functions
175 //
177  auto const newTime = std::chrono::high_resolution_clock::now();
178  std::chrono::high_resolution_clock::time_point oldTime;
179  bool expected = false;
180  unsigned int nModules;
181  while (not m_spinLock.compare_exchange_strong(expected, true, std::memory_order_acq_rel)) {
182  expected = false;
183  }
184  {
185  oldTime = m_time;
186  m_time = newTime;
187  nModules = ++m_nModules;
188  m_spinLock.store(false, std::memory_order_release);
189  }
190  assert(nModules < m_nTimeSums);
191  auto diff = newTime - oldTime;
192  for (unsigned int i = 0; i < nModules; ++i) {
193  m_timeSums[i].fetch_add(diff.count());
194  }
195 }
196 
198  auto const newTime = std::chrono::high_resolution_clock::now();
199  std::chrono::high_resolution_clock::time_point oldTime;
200  bool expected = false;
201  unsigned int nModules;
202  while (not m_spinLock.compare_exchange_weak(expected, true, std::memory_order_acq_rel)) {
203  expected = false;
204  }
205  {
206  oldTime = m_time;
207  m_time = newTime;
208  nModules = m_nModules--;
209  m_spinLock.store(false, std::memory_order_release);
210  }
211  assert(nModules < m_nTimeSums);
212  auto diff = newTime - oldTime;
213  for (unsigned int i = 0; i <= nModules; ++i) {
214  m_timeSums[i].fetch_add(diff.count());
215  }
216 }
217 
218 //
219 // const member functions
220 //
222  auto modId = iContext.moduleDescription()->id();
223  for (auto const id : m_excludedModuleIds) {
224  if (modId == id) {
225  return false;
226  }
227  }
228  return true;
229 }
230 
231 //
232 // static member functions
233 //
236  desc.addUntracked<std::vector<std::string>>("modulesToExclude", std::vector<std::string>{})
237  ->setComment("Module labels to exclude from the timing measurements");
238  desc.addUntracked<bool>("excludeSource", false)->setComment("Exclude the time the source is running");
239  descriptions.add("ConcurrentModuleTimer", desc);
240 }
241 
unsigned int maxNumberOfThreads() const
Definition: SystemBounds.h:38
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:105
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