CMS 3D CMS Logo

JeProfService.cc
Go to the documentation of this file.
1 
2 //
3 // Description: FWK service to implement hook for jemalloc heap profile
4 // dump functionality
5 //
6 
16 #include <string>
17 #include <dlfcn.h>
18 #include <cstdio>
19 #include <cstring>
20 
21 namespace edm {
22  class GlobalContext;
23  class StreamContext;
24 
25  namespace service {
26  class JeProfService {
27  public:
29 
30  void postBeginJob();
31 
32  void postBeginRun(GlobalContext const &gc);
33 
34  void postBeginLumi(GlobalContext const &gc);
35 
36  void preEvent(StreamContext const &sc);
37  void postEvent(StreamContext const &sc);
38 
39  void preModuleEvent(StreamContext const &sc, ModuleCallingContext const &mcc);
40  void postModuleEvent(StreamContext const &sc, ModuleCallingContext const &mcc);
41 
42  void postEndLumi(GlobalContext const &gc);
43 
44  void preEndRun(GlobalContext const &gc);
45  void postEndRun(GlobalContext const &gc);
46 
47  void preEndProcessBlock(GlobalContext const &gc);
48  void postEndProcessBlock(GlobalContext const &gc);
49 
50  void preEndJob();
51  void postEndJob();
52 
53  void postOpenFile(std::string const &);
54 
55  void postCloseFile(std::string const &);
56 
57  private:
58  void makeDump(const std::string &format, std::string_view moduleLabel = "");
59  static std::string replace(const std::string &s, const char *pat, int val);
60  static std::string replaceU64(const std::string &s, const char *pat, unsigned long long val);
61  static std::string replace(const std::string &s, const char *pat, std::string_view val);
62 
66 
69 
70  std::vector<std::string> modules_;
71  std::vector<std::string> moduleTypes_;
74 
82 
85 
87  int prescale_;
88  int nrecord_; // counter
92  int nfileopened_; // counter of files opened thus far
93  int nfileclosed_; // counter of files closed thus far
94  };
95  inline bool isProcessWideService(JeProfService const *) { return true; }
96  } // namespace service
97 } // namespace edm
98 
99 using namespace edm::service;
100 
102  : mineventrecord_(1), prescale_(1), nrecord_(0), nevent_(0), nrun_(0), nlumi_(0), nfileopened_(0), nfileclosed_(0) {
103  // Get the configuration
104  prescale_ = ps.getUntrackedParameter<int>("reportEventInterval", prescale_);
105  mineventrecord_ = ps.getUntrackedParameter<int>("reportFirstEvent", mineventrecord_);
106 
107  atPostBeginJob_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginJob", atPostBeginJob_);
108  atPostBeginRun_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginRun", atPostBeginRun_);
109  atPostBeginLumi_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginLumi", atPostBeginLumi_);
110 
111  atPreEvent_ = ps.getUntrackedParameter<std::string>("reportToFileAtPreEvent", atPreEvent_);
112  atPostEvent_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEvent", atPostEvent_);
113 
114  modules_ = ps.getUntrackedParameter<std::vector<std::string>>("reportModules", modules_);
115  moduleTypes_ = ps.getUntrackedParameter<std::vector<std::string>>("reportModuleTypes", moduleTypes_);
116  std::sort(modules_.begin(), modules_.end());
117  std::sort(moduleTypes_.begin(), moduleTypes_.end());
118  atPreModuleEvent_ = ps.getUntrackedParameter<std::string>("reportToFileAtPreModuleEvent", atPreModuleEvent_);
119  atPostModuleEvent_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostModuleEvent", atPostModuleEvent_);
120 
121  atPostEndLumi_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndLumi", atPostEndLumi_);
122  atPreEndRun_ = ps.getUntrackedParameter<std::string>("reportToFileAtPreEndRun", atPreEndRun_);
123  atPostEndRun_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndRun", atPostEndRun_);
125  ps.getUntrackedParameter<std::string>("reportToFileAtPreEndProcessBlock", atPreEndProcessBlock_);
127  ps.getUntrackedParameter<std::string>("reportToFileAtPostEndProcessBlock", atPostEndProcessBlock_);
128  atPreEndJob_ = ps.getUntrackedParameter<std::string>("reportToFileAtPreEndJob", atPreEndJob_);
129  atPostEndJob_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndJob", atPostEndJob_);
130 
131  atPostOpenFile_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostOpenFile", atPostOpenFile_);
132  atPostCloseFile_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostCloseFile", atPostCloseFile_);
133 
134  // Register for the framework signals
138 
139  iRegistry.watchPreEvent(this, &JeProfService::preEvent);
140  iRegistry.watchPostEvent(this, &JeProfService::postEvent);
141 
142  if (not modules_.empty() or not moduleTypes_.empty()) {
145  }
146 
152  iRegistry.watchPreEndJob(this, &JeProfService::preEndJob);
153  iRegistry.watchPostEndJob(this, &JeProfService::postEndJob);
154 
157 }
158 
160 
162  nrun_ = gc.luminosityBlockID().run();
164 }
165 
169 }
170 
172  ++nrecord_; // count before events
173  nevent_ = iStream.eventID().event();
174  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0))
176 }
177 
179  nevent_ = iStream.eventID().event();
180  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0))
182 }
183 
185  nevent_ = iStream.eventID().event();
186  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0)) {
187  auto const &moduleLabel = mcc.moduleDescription()->moduleLabel();
188  auto const &moduleType = mcc.moduleDescription()->moduleName();
189  if (std::binary_search(modules_.begin(), modules_.end(), moduleLabel) or
190  std::binary_search(moduleTypes_.begin(), moduleTypes_.end(), moduleType)) {
192  }
193  }
194 }
195 
197  nevent_ = iStream.eventID().event();
198  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0)) {
199  auto const &moduleLabel = mcc.moduleDescription()->moduleLabel();
200  auto const &moduleType = mcc.moduleDescription()->moduleName();
201  if (std::binary_search(modules_.begin(), modules_.end(), moduleLabel) or
202  std::binary_search(moduleTypes_.begin(), moduleTypes_.end(), moduleType)) {
204  }
205  }
206 }
207 
211 }
212 
214  nrun_ = gc.luminosityBlockID().run();
216 }
217 
219  nrun_ = gc.luminosityBlockID().run();
221 }
222 
224 
226 
228 
230 
232  ++nfileopened_;
234 }
235 
237  ++nfileclosed_;
239 }
240 
241 void JeProfService::makeDump(const std::string &format, std::string_view moduleLabel) {
242  std::string final(format);
243  final = replace(final, "%I", nrecord_);
244  final = replaceU64(final, "%E", nevent_);
245  final = replaceU64(final, "%R", nrun_);
246  final = replaceU64(final, "%L", nlumi_);
247  final = replace(final, "%F", nfileopened_);
248  final = replace(final, "%C", nfileclosed_);
249  final = replace(final, "%M", moduleLabel);
250  const char *fileName = final.c_str();
252 }
253 
255  size_t pos = 0;
256  size_t patlen = strlen(pat);
257  std::string result = s;
258  while ((pos = result.find(pat, pos)) != std::string::npos) {
259  char buf[64];
260  int n = sprintf(buf, "%d", val);
261  result.replace(pos, patlen, buf);
262  pos = pos - patlen + n;
263  }
264 
265  return result;
266 }
267 
268 std::string JeProfService::replaceU64(const std::string &s, const char *pat, unsigned long long val) {
269  size_t pos = 0;
270  size_t patlen = strlen(pat);
271  std::string result = s;
272  while ((pos = result.find(pat, pos)) != std::string::npos) {
273  char buf[64];
274  int n = sprintf(buf, "%llu", val);
275  result.replace(pos, patlen, buf);
276  pos = pos - patlen + n;
277  }
278 
279  return result;
280 }
281 
282 std::string JeProfService::replace(const std::string &s, const char *pat, std::string_view val) {
283  size_t pos = 0;
284  size_t patlen = strlen(pat);
285  std::string result = s;
286  while ((pos = result.find(pat, pos)) != std::string::npos) {
287  result.replace(pos, patlen, val.data());
288  pos = pos - patlen + val.size();
289  }
290 
291  return result;
292 }
293 
void preEndProcessBlock(GlobalContext const &gc)
void watchPreEvent(PreEvent::slot_type const &iSlot)
LuminosityBlockNumber_t luminosityBlock() const
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
void postModuleEvent(StreamContext const &sc, ModuleCallingContext const &mcc)
void watchPreModuleEvent(PreModuleEvent::slot_type const &iSlot)
void makeHeapDump(const char *)
Definition: jeprof.cc:33
void watchPostEvent(PostEvent::slot_type const &iSlot)
void preEndRun(GlobalContext const &gc)
bool isProcessWideService(TFileService const *)
Definition: TFileService.h:98
unsigned long long EventNumber_t
void watchPostEndProcessBlock(PostEndProcessBlock::slot_type const &iSlot)
void watchPostModuleEvent(PostModuleEvent::slot_type const &iSlot)
void watchPostGlobalBeginLumi(PostGlobalBeginLumi::slot_type const &iSlot)
std::string const & moduleName() const
void watchPreEndProcessBlock(PreEndProcessBlock::slot_type const &iSlot)
ModuleDescription const * moduleDescription() const noexcept
unsigned int LuminosityBlockNumber_t
Definition: HeavyIon.h:7
T getUntrackedParameter(std::string const &, T const &) const
void watchPostGlobalBeginRun(PostGlobalBeginRun::slot_type const &iSlot)
void watchPostCloseFile(PostCloseFile::slot_type const &iSlot)
void watchPreGlobalEndRun(PreGlobalEndRun::slot_type const &iSlot)
void postBeginRun(GlobalContext const &gc)
static std::string replace(const std::string &s, const char *pat, int val)
void preEvent(StreamContext const &sc)
edm::LuminosityBlockNumber_t nlumi_
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
void postBeginLumi(GlobalContext const &gc)
void watchPostGlobalEndLumi(PostGlobalEndLumi::slot_type const &iSlot)
void preModuleEvent(StreamContext const &sc, ModuleCallingContext const &mcc)
std::vector< std::string > modules_
void postEndLumi(GlobalContext const &gc)
LuminosityBlockID const & luminosityBlockID() const
Definition: GlobalContext.h:66
edm::EventNumber_t nevent_
RunNumber_t run() const
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:97
void watchPostGlobalEndRun(PostGlobalEndRun::slot_type const &iSlot)
void watchPostOpenFile(PostOpenFile::slot_type const &iSlot)
static std::string replaceU64(const std::string &s, const char *pat, unsigned long long val)
void watchPreEndJob(PreEndJob::slot_type const &iSlot)
void postEndRun(GlobalContext const &gc)
std::vector< std::string > moduleTypes_
HLT enums.
EventID const & eventID() const
Definition: StreamContext.h:60
void postEvent(StreamContext const &sc)
void postCloseFile(std::string const &)
void makeDump(const std::string &format, std::string_view moduleLabel="")
JeProfService(const ParameterSet &, ActivityRegistry &)
unsigned int RunNumber_t
void postEndProcessBlock(GlobalContext const &gc)
std::string const & moduleLabel() const
void postOpenFile(std::string const &)
EventNumber_t event() const
Definition: EventID.h:40
void watchPostBeginJob(PostBeginJob::slot_type const &iSlot)
convenience function for attaching to signal