CMS 3D CMS Logo

IgProfModule.cc
Go to the documentation of this file.
10 
12 #include <string>
13 #include <dlfcn.h>
14 #include <cstdio>
15 #include <cstring>
16 
18 {
19 public:
21  : dump_(nullptr),
22  prescale_(0),
23  nrecord_(0),
24  nevent_(0),
25  nrun_(0),
26  nlumi_(0),
27  nfile_(0)
28  {
29  // Removing the __extension__ gives a warning which
30  // is acknowledged as a language problem in the C++ Standard Core
31  // Language Defect Report
32  //
33  // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#195
34  //
35  // since the suggested decision seems to be that the syntax should
36  // actually be "Conditionally-Supported Behavior" in some
37  // future C++ standard I simply silence the warning.
38  if (void *sym = dlsym(nullptr, "igprof_dump_now"))
39  dump_ = __extension__ (void(*)(const char *)) sym;
40  else
41  edm::LogWarning("IgProfModule")
42  << "IgProfModule requested but application is not"
43  << " currently being profiled with igprof\n";
44 
45  prescale_ = ps.getUntrackedParameter<int>("reportEventInterval", prescale_);
46  atBeginJob_ = ps.getUntrackedParameter<std::string>("reportToFileAtBeginJob", atBeginJob_);
47  atEndJob_ = ps.getUntrackedParameter<std::string>("reportToFileAtEndJob", atEndJob_);
48  atBeginLumi_ = ps.getUntrackedParameter<std::string>("reportToFileAtBeginLumi", atBeginLumi_);
49  atEndLumi_ = ps.getUntrackedParameter<std::string>("reportToFileAtEndLumi", atEndLumi_);
50  atInputFile_ = ps.getUntrackedParameter<std::string>("reportToFileAtInputFile", atInputFile_);
51  atEvent_ = ps.getUntrackedParameter<std::string>("reportToFileAtEvent", atEvent_);
52  }
53 
54  void beginJob() override
55  { makeDump(atBeginJob_); }
56 
57  void endJob(void) override
58  { makeDump(atEndJob_); }
59 
60  void analyze(const edm::Event &e, const edm::EventSetup &) override
61  {
62  nevent_ = e.id().event();
63  if (prescale_ > 0 && (++nrecord_ % prescale_) == 1)
65  }
66 
67  void beginRun(const edm::Run &r, const edm::EventSetup &) override
68  { nrun_ = r.run(); makeDump(atBeginRun_); }
69 
70  void endRun(const edm::Run &, const edm::EventSetup &) override
71  { makeDump(atEndRun_); }
72 
75 
77  { makeDump(atEndLumi_); }
78 
79  void respondToOpenInputFile(const edm::FileBlock &) override
81 
82 private:
84  {
85  if (! dump_ || format.empty())
86  return;
87 
88  std::string final(format);
89  final = replace(final, "%I", nrecord_);
90  final = replaceU64(final, "%E", nevent_);
91  final = replaceU64(final, "%R", nrun_);
92  final = replaceU64(final, "%L", nlumi_);
93  final = replace(final, "%F", nfile_);
94  dump_(final.c_str());
95  }
96 
97  static std::string replace(const std::string &s, const char *pat, int val)
98  {
99  size_t pos = 0;
100  size_t patlen = strlen(pat);
101  std::string result = s;
102  while ((pos = result.find(pat, pos)) != std::string::npos)
103  {
104  char buf[64];
105  int n = sprintf(buf, "%d", val);
106  result.replace(pos, patlen, buf);
107  pos = pos - patlen + n;
108  }
109 
110  return result;
111  }
112 
113  static std::string replaceU64(const std::string &s, const char *pat, unsigned long long val)
114  {
115  size_t pos = 0;
116  size_t patlen = strlen(pat);
117  std::string result = s;
118  while ((pos = result.find(pat, pos)) != std::string::npos)
119  {
120  char buf[64];
121  int n = sprintf(buf, "%llu", val);
122  result.replace(pos, patlen, buf);
123  pos = pos - patlen + n;
124  }
125 
126  return result;
127  }
128 
129  void (*dump_)(const char *);
139  int nrecord_;
143  int nfile_;
144 };
145 
EventNumber_t event() const
Definition: EventID.h:41
T getUntrackedParameter(std::string const &, T const &) const
void beginLuminosityBlock(const edm::LuminosityBlock &l, const edm::EventSetup &) override
Definition: IgProfModule.cc:73
RunNumber_t run() const
Definition: RunBase.h:40
void respondToOpenInputFile(const edm::FileBlock &) override
Definition: IgProfModule.cc:79
void endLuminosityBlock(const edm::LuminosityBlock &l, const edm::EventSetup &) override
Definition: IgProfModule.cc:76
std::string atBeginRun_
std::string atInputFile_
#define nullptr
edm::LuminosityBlockNumber_t nlumi_
unsigned long long EventNumber_t
std::string atEndLumi_
unsigned int LuminosityBlockNumber_t
static std::string replace(const std::string &s, const char *pat, int val)
Definition: IgProfModule.cc:97
void makeDump(const std::string &format)
Definition: IgProfModule.cc:83
std::string atEndRun_
Definition: HeavyIon.h:7
LuminosityBlockNumber_t luminosityBlock() const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EventNumber_t nevent_
std::string atEndJob_
void endRun(const edm::Run &, const edm::EventSetup &) override
Definition: IgProfModule.cc:70
format
Some error handling for the usage.
edm::RunNumber_t nrun_
static std::string replaceU64(const std::string &s, const char *pat, unsigned long long val)
void beginJob() override
Definition: IgProfModule.cc:54
void beginRun(const edm::Run &r, const edm::EventSetup &) override
Definition: IgProfModule.cc:67
void(* dump_)(const char *)
void analyze(const edm::Event &e, const edm::EventSetup &) override
Definition: IgProfModule.cc:60
void endJob(void) override
Definition: IgProfModule.cc:57
edm::EventID id() const
Definition: EventBase.h:59
std::string atEvent_
std::string atBeginJob_
std::string atBeginLumi_
IgProfModule(const edm::ParameterSet &ps)
Definition: IgProfModule.cc:20
unsigned int RunNumber_t
Definition: Run.h:45