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::high_resolution_clock::rep>[]> m_timeSums;
42  std::vector<std::string> m_modulesToExclude;
43  std::vector<unsigned int> m_excludedModuleIds;
44  std::chrono::high_resolution_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::high_resolution_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::high_resolution_clock::now();
203  std::chrono::high_resolution_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::high_resolution_clock::now();
227  std::chrono::high_resolution_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 
ConfigurationDescriptions.h
change_name.diff
diff
Definition: change_name.py:13
edm::ModuleDescription::moduleLabel
std::string const & moduleLabel() const
Definition: ModuleDescription.h:43
service
Definition: service.py:1
ModuleCallingContext.h
mps_fire.i
i
Definition: mps_fire.py:428
edm::service::ConcurrentModuleTimer::m_modulesToExclude
std::vector< std::string > m_modulesToExclude
Definition: ConcurrentModuleTimer.cc:42
edm::service::ConcurrentModuleTimer::m_nModules
unsigned int m_nModules
Definition: ConcurrentModuleTimer.cc:46
submitPVValidationJobs.now
now
Definition: submitPVValidationJobs.py:639
edm::service::ConcurrentModuleTimer::m_trackGlobalBeginRun
const bool m_trackGlobalBeginRun
Definition: ConcurrentModuleTimer.cc:52
edm
HLT enums.
Definition: AlignableModifier.h:19
gather_cfg.cout
cout
Definition: gather_cfg.py:144
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
DEFINE_FWK_SERVICE
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:96
edm::service::ConcurrentModuleTimer::m_timeSums
std::unique_ptr< std::atomic< std::chrono::high_resolution_clock::rep >[]> m_timeSums
Definition: ConcurrentModuleTimer.cc:41
cms::cuda::assert
assert(be >=bs)
edm::service::ConcurrentModuleTimer::m_excludedModuleIds
std::vector< unsigned int > m_excludedModuleIds
Definition: ConcurrentModuleTimer.cc:43
edm::ModuleCallingContext::moduleDescription
ModuleDescription const * moduleDescription() const
Definition: ModuleCallingContext.h:50
edm::service::ConcurrentModuleTimer::m_maxNModules
unsigned int m_maxNModules
Definition: ConcurrentModuleTimer.cc:47
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
edm::service::ConcurrentModuleTimer::m_spinLock
std::atomic< bool > m_spinLock
Definition: ConcurrentModuleTimer.cc:49
newFWLiteAna.found
found
Definition: newFWLiteAna.py:118
edm::ModuleDescription
Definition: ModuleDescription.h:21
ModuleDescription.h
ActivityRegistry.h
edm::service::ConcurrentModuleTimer::m_nTimeSums
unsigned int m_nTimeSums
Definition: ConcurrentModuleTimer.cc:45
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
edm::service::ConcurrentModuleTimer::m_padding
const unsigned int m_padding
Definition: ConcurrentModuleTimer.cc:48
edm::service::ConcurrentModuleTimer::m_time
std::chrono::high_resolution_clock::time_point m_time
Definition: ConcurrentModuleTimer.cc:44
edm::ActivityRegistry
Definition: ActivityRegistry.h:134
edm::service::ConcurrentModuleTimer::stop
void stop()
Definition: ConcurrentModuleTimer.cc:225
ServiceMaker.h
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
edm::service::SystemBounds
Definition: SystemBounds.h:29
edm::ParameterSet
Definition: ParameterSet.h:47
edm::service::SystemBounds::maxNumberOfThreads
unsigned int maxNumberOfThreads() const
Definition: SystemBounds.h:38
edm::service::ConcurrentModuleTimer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: ConcurrentModuleTimer.cc:262
createfilelist.int
int
Definition: createfilelist.py:10
edm::service::ConcurrentModuleTimer
Definition: ConcurrentModuleTimer.cc:30
edm::service::ConcurrentModuleTimer::~ConcurrentModuleTimer
~ConcurrentModuleTimer()
Definition: ConcurrentModuleTimer.cc:173
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
edm::service
Definition: TFileService.h:95
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::service::ConcurrentModuleTimer::start
void start()
Definition: ConcurrentModuleTimer.cc:201
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
std
Definition: JetResolutionObject.h:76
edm::service::ConcurrentModuleTimer::m_startedTiming
bool m_startedTiming
Definition: ConcurrentModuleTimer.cc:50
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
edm::service::ConcurrentModuleTimer::ConcurrentModuleTimer
ConcurrentModuleTimer(edm::ParameterSet const &iConfig, edm::ActivityRegistry &iAR)
Definition: ConcurrentModuleTimer.cc:73
command_line.start
start
Definition: command_line.py:167
CPUTimer.h
ParameterSet.h
SystemBounds.h
edm::service::ConcurrentModuleTimer::m_excludeSource
const bool m_excludeSource
Definition: ConcurrentModuleTimer.cc:51
edm::ModuleDescription::id
unsigned int id() const
Definition: ModuleDescription.h:46
edm::ModuleCallingContext::State::kRunning
edm::ModuleCallingContext
Definition: ModuleCallingContext.h:29
edm::service::ConcurrentModuleTimer::trackModule
bool trackModule(ModuleCallingContext const &iContext) const
Definition: ConcurrentModuleTimer.cc:249