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