CMS 3D CMS Logo

IgProfService.cc
Go to the documentation of this file.
1 
2 //
3 // Description: FWK service to implement hook for igprof memory profile
4 // dump functionality
5 //
6 // Peter Elmer, Princeton University 18 Nov, 2008
7 //
8 
18 #include <string>
19 #include <dlfcn.h>
20 #include <cstdio>
21 #include <cstring>
22 
23 using namespace edm::service;
24 
26  : dump_(nullptr),
27  mineventrecord_(1),
28  prescale_(1),
29  nrecord_(0),
30  nevent_(0),
31  nrun_(0),
32  nlumi_(0),
33  nfileopened_(0),
34  nfileclosed_(0) {
35  // Removing the __extension__ gives a warning which
36  // is acknowledged as a language problem in the C++ Standard Core
37  // Language Defect Report
38  //
39  // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#195
40  //
41  // since the suggested decision seems to be that the syntax should
42  // actually be "Conditionally-Supported Behavior" in some
43  // future C++ standard I simply silence the warning.
44  if (void *sym = dlsym(nullptr, "igprof_dump_now")) {
45  dump_ = __extension__(void (*)(const char *)) sym;
46  } else
47  edm::LogWarning("IgProfModule") << "IgProfModule requested but application is not"
48  << " currently being profiled with igprof\n";
49 
50  // Get the configuration
51  prescale_ = ps.getUntrackedParameter<int>("reportEventInterval", prescale_);
52  mineventrecord_ = ps.getUntrackedParameter<int>("reportFirstEvent", mineventrecord_);
53 
54  atPostBeginJob_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginJob", atPostBeginJob_);
55  atPostBeginRun_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginRun", atPostBeginRun_);
56  atPostBeginLumi_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginLumi", atPostBeginLumi_);
57 
58  atPreEvent_ = ps.getUntrackedParameter<std::string>("reportToFileAtPreEvent", atPreEvent_);
59  atPostEvent_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEvent", atPostEvent_);
60 
61  modules_ = ps.getUntrackedParameter<std::vector<std::string>>("reportModules", modules_);
62  moduleTypes_ = ps.getUntrackedParameter<std::vector<std::string>>("reportModuleTypes", moduleTypes_);
63  std::sort(modules_.begin(), modules_.end());
64  std::sort(moduleTypes_.begin(), moduleTypes_.end());
65  atPreModuleEvent_ = ps.getUntrackedParameter<std::string>("reportToFileAtPreModuleEvent", atPreModuleEvent_);
66  atPostModuleEvent_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostModuleEvent", atPostModuleEvent_);
67 
68  atPostEndLumi_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndLumi", atPostEndLumi_);
69  atPreEndRun_ = ps.getUntrackedParameter<std::string>("reportToFileAtPreEndRun", atPreEndRun_);
70  atPostEndRun_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndRun", atPostEndRun_);
72  ps.getUntrackedParameter<std::string>("reportToFileAtPreEndProcessBlock", atPreEndProcessBlock_);
74  ps.getUntrackedParameter<std::string>("reportToFileAtPostEndProcessBlock", atPostEndProcessBlock_);
75  atPreEndJob_ = ps.getUntrackedParameter<std::string>("reportToFileAtPreEndJob", atPreEndJob_);
76  atPostEndJob_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndJob", atPostEndJob_);
77 
78  atPostOpenFile_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostOpenFile", atPostOpenFile_);
79  atPostCloseFile_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostCloseFile", atPostCloseFile_);
80 
81  // Register for the framework signals
85 
86  iRegistry.watchPreEvent(this, &IgProfService::preEvent);
87  iRegistry.watchPostEvent(this, &IgProfService::postEvent);
88 
89  if (not modules_.empty() or not moduleTypes_.empty()) {
92  }
93 
99  iRegistry.watchPreEndJob(this, &IgProfService::preEndJob);
100  iRegistry.watchPostEndJob(this, &IgProfService::postEndJob);
101 
104 }
105 
108 
109  desc.setComment(
110  "All file parameters allow the following replaceable tokens:\n"
111  " %I : record number\n"
112  " %E : event number\n"
113  " %R : run number\n"
114  " %L : lumi number\n"
115  " %F : file open count\n"
116  " %C : file close count\n"
117  " %M : module label");
118  desc.addUntracked<int>("reportEventInterval", 1)->setComment("write a new file every n events");
119  desc.addUntracked<int>("reportFirstEvent", 1)->setComment("first event count to start writing files");
120  ;
121 
122  desc.addUntracked<std::string>("reportToFileAtPostBeginJob", "");
123  desc.addUntracked<std::string>("reportToFileAtPostBeginRun", "");
124  desc.addUntracked<std::string>("reportToFileAtPostBeginLumi", "");
125 
126  desc.addUntracked<std::string>("reportToFileAtPreEvent", "");
127  desc.addUntracked<std::string>("reportToFileAtPostEvent", "");
128 
129  desc.addUntracked<std::vector<std::string>>("reportModules", {});
130  desc.addUntracked<std::vector<std::string>>("reportModuleTypes", {});
131 
132  desc.addUntracked<std::string>("reportToFileAtPreModuleEvent", "");
133  desc.addUntracked<std::string>("reportToFileAtPostModuleEvent", "");
134 
135  desc.addUntracked<std::string>("reportToFileAtPostEndLumi", "");
136  desc.addUntracked<std::string>("reportToFileAtPreEndRun", "");
137  desc.addUntracked<std::string>("reportToFileAtPostEndRun", "");
138  desc.addUntracked<std::string>("reportToFileAtPreEndProcessBlock", "");
139  desc.addUntracked<std::string>("reportToFileAtPostEndProcessBlock", "");
140  desc.addUntracked<std::string>("reportToFileAtPreEndJob", "");
141  desc.addUntracked<std::string>("reportToFileAtPostEndJob", "");
142 
143  desc.addUntracked<std::string>("reportToFileAtPostOpenFile", "");
144  desc.addUntracked<std::string>("reportToFileAtPostCloseFile", "");
145 
146  descriptions.addDefault(desc);
147 }
148 
150 
152  nrun_ = gc.luminosityBlockID().run();
154 }
155 
159 }
160 
162  ++nrecord_; // count before events
163  nevent_ = iStream.eventID().event();
164  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0))
166 }
167 
169  nevent_ = iStream.eventID().event();
170  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0))
172 }
173 
175  nevent_ = iStream.eventID().event();
176  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0)) {
177  auto const &moduleLabel = mcc.moduleDescription()->moduleLabel();
178  auto const &moduleType = mcc.moduleDescription()->moduleName();
179  if (std::binary_search(modules_.begin(), modules_.end(), moduleLabel) or
180  std::binary_search(moduleTypes_.begin(), moduleTypes_.end(), moduleType)) {
182  }
183  }
184 }
185 
187  nevent_ = iStream.eventID().event();
188  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0)) {
189  auto const &moduleLabel = mcc.moduleDescription()->moduleLabel();
190  auto const &moduleType = mcc.moduleDescription()->moduleName();
191  if (std::binary_search(modules_.begin(), modules_.end(), moduleLabel) or
192  std::binary_search(moduleTypes_.begin(), moduleTypes_.end(), moduleType)) {
194  }
195  }
196 }
197 
201 }
202 
204  nrun_ = gc.luminosityBlockID().run();
206 }
207 
209  nrun_ = gc.luminosityBlockID().run();
211 }
212 
214 
216 
218 
220 
222  ++nfileopened_;
224 }
225 
227  ++nfileclosed_;
229 }
230 
231 void IgProfService::makeDump(const std::string &format, std::string_view moduleLabel) {
232  if (!dump_ || format.empty())
233  return;
234 
235  std::string final(format);
236  final = replace(final, "%I", nrecord_);
237  final = replaceU64(final, "%E", nevent_);
238  final = replaceU64(final, "%R", nrun_);
239  final = replaceU64(final, "%L", nlumi_);
240  final = replace(final, "%F", nfileopened_);
241  final = replace(final, "%C", nfileclosed_);
242  final = replace(final, "%M", moduleLabel);
243  dump_(final.c_str());
244 }
245 
247  size_t pos = 0;
248  size_t patlen = strlen(pat);
249  std::string result = s;
250  while ((pos = result.find(pat, pos)) != std::string::npos) {
251  char buf[64];
252  int n = sprintf(buf, "%d", val);
253  result.replace(pos, patlen, buf);
254  pos = pos - patlen + n;
255  }
256 
257  return result;
258 }
259 
260 std::string IgProfService::replaceU64(const std::string &s, const char *pat, unsigned long long val) {
261  size_t pos = 0;
262  size_t patlen = strlen(pat);
263  std::string result = s;
264  while ((pos = result.find(pat, pos)) != std::string::npos) {
265  char buf[64];
266  int n = sprintf(buf, "%llu", val);
267  result.replace(pos, patlen, buf);
268  pos = pos - patlen + n;
269  }
270 
271  return result;
272 }
273 
274 std::string IgProfService::replace(const std::string &s, const char *pat, std::string_view val) {
275  size_t pos = 0;
276  size_t patlen = strlen(pat);
277  std::string result = s;
278  while ((pos = result.find(pat, pos)) != std::string::npos) {
279  result.replace(pos, patlen, val.data());
280  pos = pos - patlen + val.size();
281  }
282 
283  return result;
284 }
285 
void watchPreEvent(PreEvent::slot_type const &iSlot)
LuminosityBlockNumber_t luminosityBlock() const
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
void watchPreModuleEvent(PreModuleEvent::slot_type const &iSlot)
void postOpenFile(std::string const &)
void watchPostEvent(PostEvent::slot_type const &iSlot)
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
IgProfService(const ParameterSet &, ActivityRegistry &)
void postEndRun(GlobalContext const &gc)
edm::LuminosityBlockNumber_t nlumi_
Definition: IgProfService.h:90
void watchPreEndProcessBlock(PreEndProcessBlock::slot_type const &iSlot)
ModuleDescription const * moduleDescription() const noexcept
void preEndRun(GlobalContext const &gc)
void preModuleEvent(StreamContext const &sc, ModuleCallingContext const &mcc)
Definition: HeavyIon.h:7
T getUntrackedParameter(std::string const &, T const &) const
static std::string replaceU64(const std::string &s, const char *pat, unsigned long long val)
void watchPostGlobalBeginRun(PostGlobalBeginRun::slot_type const &iSlot)
void postEvent(StreamContext const &sc)
void watchPostCloseFile(PostCloseFile::slot_type const &iSlot)
void watchPreGlobalEndRun(PreGlobalEndRun::slot_type const &iSlot)
void addDefault(ParameterSetDescription const &psetDescription)
void makeDump(const std::string &format, std::string_view moduleLabel="")
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
std::vector< std::string > modules_
Definition: IgProfService.h:69
std::string atPostEndProcessBlock_
Definition: IgProfService.h:78
void watchPostGlobalEndLumi(PostGlobalEndLumi::slot_type const &iSlot)
void preEndProcessBlock(GlobalContext const &gc)
void postCloseFile(std::string const &)
LuminosityBlockID const & luminosityBlockID() const
Definition: GlobalContext.h:66
RunNumber_t run() const
void(* dump_)(const char *)
Definition: IgProfService.h:60
void postBeginLumi(GlobalContext const &gc)
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:97
std::vector< std::string > moduleTypes_
Definition: IgProfService.h:70
void postEndLumi(GlobalContext const &gc)
edm::EventNumber_t nevent_
Definition: IgProfService.h:88
void watchPostGlobalEndRun(PostGlobalEndRun::slot_type const &iSlot)
void watchPostOpenFile(PostOpenFile::slot_type const &iSlot)
void watchPreEndJob(PreEndJob::slot_type const &iSlot)
EventID const & eventID() const
Definition: StreamContext.h:60
void postModuleEvent(StreamContext const &sc, ModuleCallingContext const &mcc)
void preEvent(StreamContext const &sc)
void postEndProcessBlock(GlobalContext const &gc)
Log< level::Warning, false > LogWarning
std::string const & moduleLabel() const
static std::string replace(const std::string &s, const char *pat, int val)
EventNumber_t event() const
Definition: EventID.h:40
void watchPostBeginJob(PostBeginJob::slot_type const &iSlot)
convenience function for attaching to signal
void postBeginRun(GlobalContext const &gc)