CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/IgTools/IgProf/plugins/IgProfService.cc

Go to the documentation of this file.
00001 
00002 //
00003 //  Description: FWK service to implement hook for igprof memory profile 
00004 //               dump functionality
00005 //
00006 //  Peter Elmer, Princeton University                        18 Nov, 2008
00007 //
00008 
00009 #include "IgTools/IgProf/plugins/IgProfService.h"
00010 #include "FWCore/Framework/interface/MakerMacros.h"
00011 #include "FWCore/ServiceRegistry/interface/ServiceMaker.h"
00012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00014 #include "FWCore/Framework/interface/LuminosityBlock.h"
00015 #include "FWCore/Framework/interface/FileBlock.h"
00016 #include "FWCore/Framework/interface/Event.h"
00017 #include "FWCore/Framework/interface/Run.h"
00018 #include <string>
00019 #include <dlfcn.h>
00020 #include <cstdio>
00021 #include <cstring>
00022 
00023 using namespace edm::service;
00024 
00025 IgProfService::IgProfService(ParameterSet const& ps, 
00026                              ActivityRegistry&iRegistry)
00027   : dump_(0),
00028     mineventrecord_(1),
00029     prescale_(1),
00030     nrecord_(0),
00031     nevent_(0),
00032     nrun_(0),
00033     nlumi_(0),
00034     nfileopened_(0),
00035     nfileclosed_(0) {
00036 
00037 
00038     // Removing the __extension__ gives a warning which
00039     // is acknowledged as a language problem in the C++ Standard Core 
00040     // Language Defect Report
00041     //
00042     // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#195
00043     //
00044     // since the suggested decision seems to be that the syntax should
00045     // actually be "Conditionally-Supported Behavior" in some 
00046     // future C++ standard I simply silence the warning.
00047     if (void *sym = dlsym(0, "igprof_dump_now")) {
00048       dump_ = __extension__ (void(*)(const char *)) sym;
00049     } else
00050       edm::LogWarning("IgProfModule")
00051         << "IgProfModule requested but application is not"
00052         << " currently being profiled with igprof\n";
00053 
00054   // Get the configuration
00055   prescale_    
00056     = ps.getUntrackedParameter<int>("reportEventInterval", prescale_);
00057   mineventrecord_    
00058     = ps.getUntrackedParameter<int>("reportFirstEvent", mineventrecord_);
00059 
00060   atPostBeginJob_  
00061     = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginJob", atPostBeginJob_);
00062   atPostBeginRun_ 
00063     = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginRun", atPostBeginRun_);
00064   atPostBeginLumi_ 
00065     = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginLumi", atPostBeginLumi_);
00066 
00067   atPreEvent_     
00068     = ps.getUntrackedParameter<std::string>("reportToFileAtPreEvent", atPreEvent_);
00069   atPostEvent_     
00070     = ps.getUntrackedParameter<std::string>("reportToFileAtPostEvent", atPostEvent_);
00071 
00072   atPostEndLumi_ 
00073     = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndLumi", atPostEndLumi_);
00074   atPostEndRun_ 
00075     = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndRun", atPostEndRun_);
00076   atPostEndJob_  
00077     = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndJob", atPostEndJob_);
00078 
00079   atPostOpenFile_ 
00080     = ps.getUntrackedParameter<std::string>("reportToFileAtPostOpenFile", atPostOpenFile_);
00081   atPostCloseFile_ 
00082     = ps.getUntrackedParameter<std::string>("reportToFileAtPostCloseFile", atPostCloseFile_);
00083 
00084 
00085   // Register for the framework signals
00086   iRegistry.watchPostBeginJob(this, &IgProfService::postBeginJob);
00087   iRegistry.watchPostBeginRun(this, &IgProfService::postBeginRun);
00088   iRegistry.watchPostBeginLumi(this, &IgProfService::postBeginLumi);
00089 
00090   iRegistry.watchPreProcessEvent(this, &IgProfService::preEvent);
00091   iRegistry.watchPostProcessEvent(this, &IgProfService::postEvent);
00092 
00093   iRegistry.watchPostEndLumi(this, &IgProfService::postEndLumi);
00094   iRegistry.watchPostEndRun(this, &IgProfService::postEndRun);
00095   iRegistry.watchPostEndJob(this, &IgProfService::postEndJob);
00096 
00097   iRegistry.watchPostOpenFile(this, &IgProfService::postOpenFile);
00098   iRegistry.watchPostCloseFile(this, &IgProfService::postCloseFile);
00099 
00100 }
00101 
00102 void IgProfService::postBeginJob() { 
00103   makeDump(atPostBeginJob_); 
00104 }
00105 
00106 void IgProfService::postBeginRun(const edm::Run &r, const edm::EventSetup &) { 
00107   nrun_ = r.run(); makeDump(atPostBeginRun_); 
00108 }
00109 
00110 void IgProfService::postBeginLumi(const edm::LuminosityBlock &l, 
00111                                   const edm::EventSetup &) { 
00112   nlumi_ = l.luminosityBlock(); makeDump(atPostBeginLumi_); 
00113 }
00114 
00115 void IgProfService::preEvent(const edm::EventID &id, 
00116                              const edm::Timestamp & iTime) {
00117   ++nrecord_; // count before events
00118   nevent_ = id.event();
00119   if ((prescale_ > 0) && 
00120       (nrecord_ >= mineventrecord_) &&
00121       (((nrecord_ - mineventrecord_)% prescale_) == 0)) makeDump(atPreEvent_);
00122 }
00123 
00124 void IgProfService::postEvent(const edm::Event &e, const edm::EventSetup &) {
00125   nevent_ = e.id().event();
00126   if ((prescale_ > 0) && 
00127       (nrecord_ >= mineventrecord_) &&
00128       (((nrecord_ - mineventrecord_)% prescale_) == 0)) makeDump(atPostEvent_);
00129 }
00130 
00131 void IgProfService::postEndLumi(const edm::LuminosityBlock &l, 
00132                                 const edm::EventSetup &) { 
00133   makeDump(atPostEndLumi_); 
00134 }
00135 
00136 void IgProfService::postEndRun(const edm::Run &, const edm::EventSetup &) { 
00137   makeDump(atPostEndRun_); 
00138 }
00139 
00140 void IgProfService::postEndJob() { 
00141   makeDump(atPostEndJob_); 
00142 }
00143 
00144 void IgProfService::postOpenFile () {
00145   ++nfileopened_; 
00146   makeDump(atPostOpenFile_);
00147 }  
00148 
00149 void IgProfService::postCloseFile () {
00150   ++nfileclosed_; 
00151   makeDump(atPostCloseFile_);
00152 }  
00153 
00154 void IgProfService::makeDump(const std::string &format) {
00155   if (! dump_ || format.empty())
00156     return;
00157 
00158   std::string final(format);
00159   final = replace(final, "%I", nrecord_);
00160   final = replace(final, "%E", nevent_);
00161   final = replace(final, "%R", nrun_);
00162   final = replace(final, "%L", nlumi_);
00163   final = replace(final, "%F", nfileopened_);
00164   final = replace(final, "%C", nfileclosed_);
00165   dump_(final.c_str());
00166 }
00167 
00168 std::string 
00169 IgProfService::replace(const std::string &s, const char *pat, int val) {
00170   size_t pos = 0;
00171   size_t patlen = strlen(pat);
00172   std::string result = s;
00173   while ((pos = result.find(pat, pos)) != std::string::npos)
00174   {
00175     char buf[64];
00176     int n = sprintf(buf, "%d", val);
00177     result.replace(pos, patlen, buf);
00178     pos = pos - patlen + n;
00179   }
00180 
00181   return result;
00182 }
00183 
00184 DEFINE_FWK_SERVICE(IgProfService);
00185