CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/IgTools/IgProf/plugins/IgProfModule.cc

Go to the documentation of this file.
00001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00002 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00003 #include "FWCore/Framework/interface/EDAnalyzer.h"
00004 #include "FWCore/Framework/interface/MakerMacros.h"
00005 #include "FWCore/Framework/interface/LuminosityBlock.h"
00006 #include "FWCore/Framework/interface/FileBlock.h"
00007 #include "FWCore/Framework/interface/Event.h"
00008 #include "FWCore/Framework/interface/Run.h"
00009 
00010 #include "FWCore/ServiceRegistry/interface/Service.h"
00011 #include <string>
00012 #include <dlfcn.h>
00013 #include <cstdio>
00014 #include <cstring>
00015 
00016 class IgProfModule : public edm::EDAnalyzer
00017 {
00018 public:
00019   IgProfModule(const edm::ParameterSet &ps)
00020     : dump_(0),
00021       prescale_(0),
00022       nrecord_(0),
00023       nevent_(0),
00024       nrun_(0),
00025       nlumi_(0),
00026       nfile_(0)
00027     {
00028       // Removing the __extension__ gives a warning which
00029       // is acknowledged as a language problem in the C++ Standard Core 
00030       // Language Defect Report
00031       //
00032       // https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#195
00033       //
00034       // since the suggested decision seems to be that the syntax should
00035       // actually be "Conditionally-Supported Behavior" in some 
00036       // future C++ standard I simply silence the warning.
00037       if (void *sym = dlsym(0, "igprof_dump_now"))
00038         dump_ = __extension__ (void(*)(const char *)) sym;
00039       else
00040         edm::LogWarning("IgProfModule")
00041           << "IgProfModule requested but application is not"
00042           << " currently being profiled with igprof\n";
00043 
00044       prescale_    = ps.getUntrackedParameter<int>("reportEventInterval", prescale_);
00045       atBeginJob_  = ps.getUntrackedParameter<std::string>("reportToFileAtBeginJob", atBeginJob_);
00046       atEndJob_    = ps.getUntrackedParameter<std::string>("reportToFileAtEndJob", atEndJob_);
00047       atBeginLumi_ = ps.getUntrackedParameter<std::string>("reportToFileAtBeginLumi", atBeginLumi_);
00048       atEndLumi_   = ps.getUntrackedParameter<std::string>("reportToFileAtEndLumi", atEndLumi_);
00049       atInputFile_ = ps.getUntrackedParameter<std::string>("reportToFileAtInputFile", atInputFile_);
00050       atEvent_     = ps.getUntrackedParameter<std::string>("reportToFileAtEvent", atEvent_);
00051     }
00052 
00053   virtual void beginJob()
00054     { makeDump(atBeginJob_); }
00055 
00056   virtual void endJob(void)
00057     { makeDump(atEndJob_); }
00058 
00059   virtual void analyze(const edm::Event &e, const edm::EventSetup &)
00060     {
00061       nevent_ = e.id().event();
00062       if (prescale_ > 0 && (++nrecord_ % prescale_) == 1)
00063         makeDump(atEvent_);
00064     }
00065 
00066   virtual void beginRun(const edm::Run &r, const edm::EventSetup &)
00067     { nrun_ = r.run(); makeDump(atBeginRun_); }
00068 
00069   virtual void endRun(const edm::Run &, const edm::EventSetup &)
00070     { makeDump(atEndRun_); }
00071 
00072   virtual void beginLuminosityBlock(const edm::LuminosityBlock &l, const edm::EventSetup &)
00073     { nlumi_ = l.luminosityBlock(); makeDump(atBeginLumi_); }
00074 
00075   virtual void endLuminosityBlock(const edm::LuminosityBlock &l, const edm::EventSetup &)
00076     { makeDump(atEndLumi_); }
00077 
00078   virtual void respondToOpenInputFile(const edm::FileBlock &)
00079     { ++nfile_; makeDump(atInputFile_); }
00080 
00081 private:
00082   void makeDump(const std::string &format)
00083     {
00084       if (! dump_ || format.empty())
00085         return;
00086 
00087       std::string final(format);
00088       final = replace(final, "%I", nrecord_);
00089       final = replace(final, "%E", nevent_);
00090       final = replace(final, "%R", nrun_);
00091       final = replace(final, "%L", nlumi_);
00092       final = replace(final, "%F", nfile_);
00093       dump_(final.c_str());
00094     }
00095 
00096   static std::string replace(const std::string &s, const char *pat, int val)
00097     {
00098       size_t pos = 0;
00099       size_t patlen = strlen(pat);
00100       std::string result = s;
00101       while ((pos = result.find(pat, pos)) != std::string::npos)
00102       {
00103         char buf[64];
00104         int n = sprintf(buf, "%d", val);
00105         result.replace(pos, patlen, buf);
00106         pos = pos - patlen + n;
00107       }
00108 
00109       return result;
00110     }
00111 
00112   void (*dump_)(const char *);
00113   std::string atBeginJob_;
00114   std::string atEndJob_;
00115   std::string atBeginRun_;
00116   std::string atEndRun_;
00117   std::string atBeginLumi_;
00118   std::string atEndLumi_;
00119   std::string atInputFile_;
00120   std::string atEvent_;
00121   int prescale_;
00122   int nrecord_;
00123   int nevent_;
00124   int nrun_;
00125   int nlumi_;
00126   int nfile_;
00127 };
00128 
00129 DEFINE_FWK_MODULE(IgProfModule);