CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  atPostEndLumi_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndLumi", atPostEndLumi_);
62  atPostEndRun_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndRun", atPostEndRun_);
63  atPostEndJob_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndJob", atPostEndJob_);
64 
65  atPostOpenFile_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostOpenFile", atPostOpenFile_);
66  atPostCloseFile_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostCloseFile", atPostCloseFile_);
67 
68  // Register for the framework signals
72 
73  iRegistry.watchPreEvent(this, &IgProfService::preEvent);
74  iRegistry.watchPostEvent(this, &IgProfService::postEvent);
75 
79 
82 }
83 
85 
87  nrun_ = gc.luminosityBlockID().run();
89 }
90 
94 }
95 
96 void IgProfService::preEvent(StreamContext const &iStream) {
97  ++nrecord_; // count before events
98  nevent_ = iStream.eventID().event();
99  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0))
101 }
102 
104  nevent_ = iStream.eventID().event();
105  if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0))
107 }
108 
110 
112 
114 
116  ++nfileopened_;
118 }
119 
121  ++nfileclosed_;
123 }
124 
126  if (!dump_ || format.empty())
127  return;
128 
129  std::string final(format);
130  final = replace(final, "%I", nrecord_);
131  final = replaceU64(final, "%E", nevent_);
132  final = replaceU64(final, "%R", nrun_);
133  final = replaceU64(final, "%L", nlumi_);
134  final = replace(final, "%F", nfileopened_);
135  final = replace(final, "%C", nfileclosed_);
136  dump_(final.c_str());
137 }
138 
139 std::string IgProfService::replace(const std::string &s, const char *pat, int val) {
140  size_t pos = 0;
141  size_t patlen = strlen(pat);
142  std::string result = s;
143  while ((pos = result.find(pat, pos)) != std::string::npos) {
144  char buf[64];
145  int n = sprintf(buf, "%d", val);
146  result.replace(pos, patlen, buf);
147  pos = pos - patlen + n;
148  }
149 
150  return result;
151 }
152 
153 std::string IgProfService::replaceU64(const std::string &s, const char *pat, unsigned long long val) {
154  size_t pos = 0;
155  size_t patlen = strlen(pat);
156  std::string result = s;
157  while ((pos = result.find(pat, pos)) != std::string::npos) {
158  char buf[64];
159  int n = sprintf(buf, "%llu", val);
160  result.replace(pos, patlen, buf);
161  pos = pos - patlen + n;
162  }
163 
164  return result;
165 }
166 
EventNumber_t event() const
Definition: EventID.h:40
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:60
void watchPostGlobalBeginLumi(PostGlobalBeginLumi::slot_type const &iSlot)
IgProfService(const ParameterSet &, ActivityRegistry &)
void postEndRun(GlobalContext const &gc)
edm::LuminosityBlockNumber_t nlumi_
Definition: IgProfService.h:71
tuple result
Definition: mps_fire.py:311
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)
void(* dump_)(const char *)
Definition: IgProfService.h:50
void postBeginLumi(GlobalContext const &gc)
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:96
void postEndLumi(GlobalContext const &gc)
edm::EventNumber_t nevent_
Definition: IgProfService.h:69
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
Log< level::Warning, false > LogWarning
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)