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  atPostEndRun_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndRun", atPostEndRun_);
70  atPostEndJob_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndJob", atPostEndJob_);
71 
72  atPostOpenFile_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostOpenFile", atPostOpenFile_);
73  atPostCloseFile_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostCloseFile", atPostCloseFile_);
74 
75  // Register for the framework signals
79 
80  iRegistry.watchPreEvent(this, &IgProfService::preEvent);
81  iRegistry.watchPostEvent(this, &IgProfService::postEvent);
82 
83  if (not modules_.empty() or not moduleTypes_.empty()) {
86  }
87 
91 
94 }
95 
97 
99  nrun_ = gc.luminosityBlockID().run();
101 }
102 
106 }
107 
109  ++nrecord_; // count before events
110  nevent_ = iStream.eventID().event();
111  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0))
113 }
114 
116  nevent_ = iStream.eventID().event();
117  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0))
119 }
120 
122  nevent_ = iStream.eventID().event();
123  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0)) {
124  auto const &moduleLabel = mcc.moduleDescription()->moduleLabel();
125  auto const &moduleType = mcc.moduleDescription()->moduleName();
126  if (std::binary_search(modules_.begin(), modules_.end(), moduleLabel) or
127  std::binary_search(moduleTypes_.begin(), moduleTypes_.end(), moduleType)) {
129  }
130  }
131 }
132 
134  nevent_ = iStream.eventID().event();
135  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0)) {
136  auto const &moduleLabel = mcc.moduleDescription()->moduleLabel();
137  auto const &moduleType = mcc.moduleDescription()->moduleName();
138  if (std::binary_search(modules_.begin(), modules_.end(), moduleLabel) or
139  std::binary_search(moduleTypes_.begin(), moduleTypes_.end(), moduleType)) {
141  }
142  }
143 }
144 
148 }
149 
151  nrun_ = gc.luminosityBlockID().run();
153 }
154 
156 
158  ++nfileopened_;
160 }
161 
163  ++nfileclosed_;
165 }
166 
167 void IgProfService::makeDump(const std::string &format, std::string_view moduleLabel) {
168  if (!dump_ || format.empty())
169  return;
170 
171  std::string final(format);
172  final = replace(final, "%I", nrecord_);
173  final = replaceU64(final, "%E", nevent_);
174  final = replaceU64(final, "%R", nrun_);
175  final = replaceU64(final, "%L", nlumi_);
176  final = replace(final, "%F", nfileopened_);
177  final = replace(final, "%C", nfileclosed_);
178  final = replace(final, "%M", moduleLabel);
179  dump_(final.c_str());
180 }
181 
183  size_t pos = 0;
184  size_t patlen = strlen(pat);
185  std::string result = s;
186  while ((pos = result.find(pat, pos)) != std::string::npos) {
187  char buf[64];
188  int n = sprintf(buf, "%d", val);
189  result.replace(pos, patlen, buf);
190  pos = pos - patlen + n;
191  }
192 
193  return result;
194 }
195 
196 std::string IgProfService::replaceU64(const std::string &s, const char *pat, unsigned long long val) {
197  size_t pos = 0;
198  size_t patlen = strlen(pat);
199  std::string result = s;
200  while ((pos = result.find(pat, pos)) != std::string::npos) {
201  char buf[64];
202  int n = sprintf(buf, "%llu", val);
203  result.replace(pos, patlen, buf);
204  pos = pos - patlen + n;
205  }
206 
207  return result;
208 }
209 
210 std::string IgProfService::replace(const std::string &s, const char *pat, std::string_view val) {
211  size_t pos = 0;
212  size_t patlen = strlen(pat);
213  std::string result = s;
214  while ((pos = result.find(pat, pos)) != std::string::npos) {
215  result.replace(pos, patlen, val.data());
216  pos = pos - patlen + val.size();
217  }
218 
219  return result;
220 }
221 
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 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:80
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 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:63
void watchPostGlobalEndLumi(PostGlobalEndLumi::slot_type const &iSlot)
void postCloseFile(std::string const &)
LuminosityBlockID const & luminosityBlockID() const
Definition: GlobalContext.h:60
RunNumber_t run() const
void(* dump_)(const char *)
Definition: IgProfService.h:54
void postBeginLumi(GlobalContext const &gc)
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:97
std::vector< std::string > moduleTypes_
Definition: IgProfService.h:64
void postEndLumi(GlobalContext const &gc)
edm::EventNumber_t nevent_
Definition: IgProfService.h:78
void watchPostGlobalEndRun(PostGlobalEndRun::slot_type const &iSlot)
void watchPostOpenFile(PostOpenFile::slot_type const &iSlot)
EventID const & eventID() const
Definition: StreamContext.h:59
void postModuleEvent(StreamContext const &sc, ModuleCallingContext const &mcc)
void preEvent(StreamContext const &sc)
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)