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