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  ActivityRegistry&iRegistry)
27  : dump_(nullptr),
28  mineventrecord_(1),
29  prescale_(1),
30  nrecord_(0),
31  nevent_(0),
32  nrun_(0),
33  nlumi_(0),
34  nfileopened_(0),
35  nfileclosed_(0) {
36 
37 
38  // Removing the __extension__ gives a warning which
39  // is acknowledged as a language problem in the C++ Standard Core
40  // Language Defect Report
41  //
42  // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#195
43  //
44  // since the suggested decision seems to be that the syntax should
45  // actually be "Conditionally-Supported Behavior" in some
46  // future C++ standard I simply silence the warning.
47  if (void *sym = dlsym(nullptr, "igprof_dump_now")) {
48  dump_ = __extension__ (void(*)(const char *)) sym;
49  } else
50  edm::LogWarning("IgProfModule")
51  << "IgProfModule requested but application is not"
52  << " currently being profiled with igprof\n";
53 
54  // Get the configuration
55  prescale_
56  = ps.getUntrackedParameter<int>("reportEventInterval", prescale_);
58  = ps.getUntrackedParameter<int>("reportFirstEvent", mineventrecord_);
59 
61  = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginJob", atPostBeginJob_);
63  = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginRun", atPostBeginRun_);
65  = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginLumi", atPostBeginLumi_);
66 
68  = ps.getUntrackedParameter<std::string>("reportToFileAtPreEvent", atPreEvent_);
70  = ps.getUntrackedParameter<std::string>("reportToFileAtPostEvent", atPostEvent_);
71 
73  = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndLumi", atPostEndLumi_);
75  = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndRun", atPostEndRun_);
77  = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndJob", atPostEndJob_);
78 
80  = ps.getUntrackedParameter<std::string>("reportToFileAtPostOpenFile", atPostOpenFile_);
82  = ps.getUntrackedParameter<std::string>("reportToFileAtPostCloseFile", atPostCloseFile_);
83 
84 
85  // Register for the framework signals
89 
90  iRegistry.watchPreEvent(this, &IgProfService::preEvent);
91  iRegistry.watchPostEvent(this, &IgProfService::postEvent);
92 
96 
99 
100 }
101 
104 }
105 
108 }
109 
112 }
113 
115  ++nrecord_; // count before events
116  nevent_ = iStream.eventID().event();
117  if ((prescale_ > 0) &&
118  (nrecord_ >= mineventrecord_) &&
120 }
121 
123  nevent_ = iStream.eventID().event();
124  if ((prescale_ > 0) &&
125  (nrecord_ >= mineventrecord_) &&
127 }
128 
131 }
132 
135 }
136 
139 }
140 
142  ++nfileopened_;
144 }
145 
147  ++nfileclosed_;
149 }
150 
152  if (! dump_ || format.empty())
153  return;
154 
155  std::string final(format);
156  final = replace(final, "%I", nrecord_);
157  final = replaceU64(final, "%E", nevent_);
158  final = replaceU64(final, "%R", nrun_);
159  final = replaceU64(final, "%L", nlumi_);
160  final = replace(final, "%F", nfileopened_);
161  final = replace(final, "%C", nfileclosed_);
162  dump_(final.c_str());
163 }
164 
166 IgProfService::replace(const std::string &s, const char *pat, int val) {
167  size_t pos = 0;
168  size_t patlen = strlen(pat);
169  std::string result = s;
170  while ((pos = result.find(pat, pos)) != std::string::npos)
171  {
172  char buf[64];
173  int n = sprintf(buf, "%d", val);
174  result.replace(pos, patlen, buf);
175  pos = pos - patlen + n;
176  }
177 
178  return result;
179 }
180 
182 IgProfService::replaceU64(const std::string &s, const char *pat, unsigned long long val) {
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  {
188  char buf[64];
189  int n = sprintf(buf, "%llu", val);
190  result.replace(pos, patlen, buf);
191  pos = pos - patlen + n;
192  }
193 
194  return result;
195 }
196 
198 
EventNumber_t event() const
Definition: EventID.h:41
T getUntrackedParameter(std::string const &, T const &) const
void watchPreEvent(PreEvent::slot_type const &iSlot)
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
void watchPostEvent(PostEvent::slot_type const &iSlot)
void postCloseFile(std::string const &, bool)
LuminosityBlockID const & luminosityBlockID() const
Definition: GlobalContext.h:52
void watchPostGlobalBeginLumi(PostGlobalBeginLumi::slot_type const &iSlot)
IgProfService(const ParameterSet &, ActivityRegistry &)
void postEndRun(GlobalContext const &gc)
edm::LuminosityBlockNumber_t nlumi_
Definition: IgProfService.h:79
#define nullptr
Definition: HeavyIon.h:7
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 postOpenFile(std::string const &, bool)
RunNumber_t run() const
void watchPostGlobalEndLumi(PostGlobalEndLumi::slot_type const &iSlot)
format
Some error handling for the usage.
void(* dump_)(const char *)
Definition: IgProfService.h:58
void postBeginLumi(GlobalContext const &gc)
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:113
void postEndLumi(GlobalContext const &gc)
edm::EventNumber_t nevent_
Definition: IgProfService.h:77
void watchPostGlobalEndRun(PostGlobalEndRun::slot_type const &iSlot)
LuminosityBlockNumber_t luminosityBlock() const
void watchPostOpenFile(PostOpenFile::slot_type const &iSlot)
void preEvent(StreamContext const &sc)
EventID const & eventID() const
Definition: StreamContext.h:59
void makeDump(const std::string &format)
static std::string replace(const std::string &s, const char *pat, int val)
void watchPostBeginJob(PostBeginJob::slot_type const &iSlot)
convenience function for attaching to signal
void postBeginRun(GlobalContext const &gc)