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 
107 
109  nrun_ = gc.luminosityBlockID().run();
111 }
112 
116 }
117 
119  ++nrecord_; // count before events
120  nevent_ = iStream.eventID().event();
121  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0))
123 }
124 
126  nevent_ = iStream.eventID().event();
127  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0))
129 }
130 
132  nevent_ = iStream.eventID().event();
133  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0)) {
134  auto const &moduleLabel = mcc.moduleDescription()->moduleLabel();
135  auto const &moduleType = mcc.moduleDescription()->moduleName();
136  if (std::binary_search(modules_.begin(), modules_.end(), moduleLabel) or
137  std::binary_search(moduleTypes_.begin(), moduleTypes_.end(), moduleType)) {
139  }
140  }
141 }
142 
144  nevent_ = iStream.eventID().event();
145  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0)) {
146  auto const &moduleLabel = mcc.moduleDescription()->moduleLabel();
147  auto const &moduleType = mcc.moduleDescription()->moduleName();
148  if (std::binary_search(modules_.begin(), modules_.end(), moduleLabel) or
149  std::binary_search(moduleTypes_.begin(), moduleTypes_.end(), moduleType)) {
151  }
152  }
153 }
154 
158 }
159 
161  nrun_ = gc.luminosityBlockID().run();
163 }
164 
166  nrun_ = gc.luminosityBlockID().run();
168 }
169 
171 
173 
175 
177 
179  ++nfileopened_;
181 }
182 
184  ++nfileclosed_;
186 }
187 
188 void IgProfService::makeDump(const std::string &format, std::string_view moduleLabel) {
189  if (!dump_ || format.empty())
190  return;
191 
192  std::string final(format);
193  final = replace(final, "%I", nrecord_);
194  final = replaceU64(final, "%E", nevent_);
195  final = replaceU64(final, "%R", nrun_);
196  final = replaceU64(final, "%L", nlumi_);
197  final = replace(final, "%F", nfileopened_);
198  final = replace(final, "%C", nfileclosed_);
199  final = replace(final, "%M", moduleLabel);
200  dump_(final.c_str());
201 }
202 
204  size_t pos = 0;
205  size_t patlen = strlen(pat);
206  std::string result = s;
207  while ((pos = result.find(pat, pos)) != std::string::npos) {
208  char buf[64];
209  int n = sprintf(buf, "%d", val);
210  result.replace(pos, patlen, buf);
211  pos = pos - patlen + n;
212  }
213 
214  return result;
215 }
216 
217 std::string IgProfService::replaceU64(const std::string &s, const char *pat, unsigned long long val) {
218  size_t pos = 0;
219  size_t patlen = strlen(pat);
220  std::string result = s;
221  while ((pos = result.find(pat, pos)) != std::string::npos) {
222  char buf[64];
223  int n = sprintf(buf, "%llu", val);
224  result.replace(pos, patlen, buf);
225  pos = pos - patlen + n;
226  }
227 
228  return result;
229 }
230 
231 std::string IgProfService::replace(const std::string &s, const char *pat, std::string_view val) {
232  size_t pos = 0;
233  size_t patlen = strlen(pat);
234  std::string result = s;
235  while ((pos = result.find(pat, pos)) != std::string::npos) {
236  result.replace(pos, patlen, val.data());
237  pos = pos - patlen + val.size();
238  }
239 
240  return result;
241 }
242 
void watchPreEvent(PreEvent::slot_type const &iSlot)
ModuleDescription const * moduleDescription() const
LuminosityBlockNumber_t luminosityBlock() const
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:87
void watchPreEndProcessBlock(PreEndProcessBlock::slot_type const &iSlot)
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 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:66
std::string atPostEndProcessBlock_
Definition: IgProfService.h:75
void watchPostGlobalEndLumi(PostGlobalEndLumi::slot_type const &iSlot)
void preEndProcessBlock(GlobalContext const &gc)
void postCloseFile(std::string const &)
LuminosityBlockID const & luminosityBlockID() const
Definition: GlobalContext.h:62
RunNumber_t run() const
void(* dump_)(const char *)
Definition: IgProfService.h:57
void postBeginLumi(GlobalContext const &gc)
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:97
std::vector< std::string > moduleTypes_
Definition: IgProfService.h:67
void postEndLumi(GlobalContext const &gc)
edm::EventNumber_t nevent_
Definition: IgProfService.h:85
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)