Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-08-16 22:25:54

0001 
0002 //
0003 //  Description: FWK service to implement hook for igprof memory profile
0004 //               dump functionality
0005 //
0006 //  Peter Elmer, Princeton University                        18 Nov, 2008
0007 //
0008 
0009 #include "IgTools/IgProf/plugins/IgProfService.h"
0010 #include "FWCore/Framework/interface/MakerMacros.h"
0011 #include "FWCore/ServiceRegistry/interface/ServiceMaker.h"
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "FWCore/Framework/interface/LuminosityBlock.h"
0015 #include "FWCore/Framework/interface/FileBlock.h"
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "FWCore/Framework/interface/Run.h"
0018 #include <string>
0019 #include <dlfcn.h>
0020 #include <cstdio>
0021 #include <cstring>
0022 
0023 using namespace edm::service;
0024 
0025 IgProfService::IgProfService(ParameterSet const &ps, ActivityRegistry &iRegistry)
0026     : dump_(nullptr),
0027       mineventrecord_(1),
0028       prescale_(1),
0029       nrecord_(0),
0030       nevent_(0),
0031       nrun_(0),
0032       nlumi_(0),
0033       nfileopened_(0),
0034       nfileclosed_(0) {
0035   // Removing the __extension__ gives a warning which
0036   // is acknowledged as a language problem in the C++ Standard Core
0037   // Language Defect Report
0038   //
0039   // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#195
0040   //
0041   // since the suggested decision seems to be that the syntax should
0042   // actually be "Conditionally-Supported Behavior" in some
0043   // future C++ standard I simply silence the warning.
0044   if (void *sym = dlsym(nullptr, "igprof_dump_now")) {
0045     dump_ = __extension__(void (*)(const char *)) sym;
0046   } else
0047     edm::LogWarning("IgProfModule") << "IgProfModule requested but application is not"
0048                                     << " currently being profiled with igprof\n";
0049 
0050   // Get the configuration
0051   prescale_ = ps.getUntrackedParameter<int>("reportEventInterval", prescale_);
0052   mineventrecord_ = ps.getUntrackedParameter<int>("reportFirstEvent", mineventrecord_);
0053 
0054   atPostBeginJob_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginJob", atPostBeginJob_);
0055   atPostBeginRun_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginRun", atPostBeginRun_);
0056   atPostBeginLumi_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostBeginLumi", atPostBeginLumi_);
0057 
0058   atPreEvent_ = ps.getUntrackedParameter<std::string>("reportToFileAtPreEvent", atPreEvent_);
0059   atPostEvent_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEvent", atPostEvent_);
0060 
0061   modules_ = ps.getUntrackedParameter<std::vector<std::string>>("reportModules", modules_);
0062   moduleTypes_ = ps.getUntrackedParameter<std::vector<std::string>>("reportModuleTypes", moduleTypes_);
0063   std::sort(modules_.begin(), modules_.end());
0064   std::sort(moduleTypes_.begin(), moduleTypes_.end());
0065   atPreModuleEvent_ = ps.getUntrackedParameter<std::string>("reportToFileAtPreModuleEvent", atPreModuleEvent_);
0066   atPostModuleEvent_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostModuleEvent", atPostModuleEvent_);
0067 
0068   atPostEndLumi_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndLumi", atPostEndLumi_);
0069   atPreEndRun_ = ps.getUntrackedParameter<std::string>("reportToFileAtPreEndRun", atPreEndRun_);
0070   atPostEndRun_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndRun", atPostEndRun_);
0071   atPreEndProcessBlock_ =
0072       ps.getUntrackedParameter<std::string>("reportToFileAtPreEndProcessBlock", atPreEndProcessBlock_);
0073   atPostEndProcessBlock_ =
0074       ps.getUntrackedParameter<std::string>("reportToFileAtPostEndProcessBlock", atPostEndProcessBlock_);
0075   atPreEndJob_ = ps.getUntrackedParameter<std::string>("reportToFileAtPreEndJob", atPreEndJob_);
0076   atPostEndJob_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostEndJob", atPostEndJob_);
0077 
0078   atPostOpenFile_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostOpenFile", atPostOpenFile_);
0079   atPostCloseFile_ = ps.getUntrackedParameter<std::string>("reportToFileAtPostCloseFile", atPostCloseFile_);
0080 
0081   // Register for the framework signals
0082   iRegistry.watchPostBeginJob(this, &IgProfService::postBeginJob);
0083   iRegistry.watchPostGlobalBeginRun(this, &IgProfService::postBeginRun);
0084   iRegistry.watchPostGlobalBeginLumi(this, &IgProfService::postBeginLumi);
0085 
0086   iRegistry.watchPreEvent(this, &IgProfService::preEvent);
0087   iRegistry.watchPostEvent(this, &IgProfService::postEvent);
0088 
0089   if (not modules_.empty() or not moduleTypes_.empty()) {
0090     iRegistry.watchPreModuleEvent(this, &IgProfService::preModuleEvent);
0091     iRegistry.watchPostModuleEvent(this, &IgProfService::postModuleEvent);
0092   }
0093 
0094   iRegistry.watchPostGlobalEndLumi(this, &IgProfService::postEndLumi);
0095   iRegistry.watchPreGlobalEndRun(this, &IgProfService::preEndRun);
0096   iRegistry.watchPostGlobalEndRun(this, &IgProfService::postEndRun);
0097   iRegistry.watchPreEndProcessBlock(this, &IgProfService::preEndProcessBlock);
0098   iRegistry.watchPostEndProcessBlock(this, &IgProfService::postEndProcessBlock);
0099   iRegistry.watchPreEndJob(this, &IgProfService::preEndJob);
0100   iRegistry.watchPostEndJob(this, &IgProfService::postEndJob);
0101 
0102   iRegistry.watchPostOpenFile(this, &IgProfService::postOpenFile);
0103   iRegistry.watchPostCloseFile(this, &IgProfService::postCloseFile);
0104 }
0105 
0106 void IgProfService::postBeginJob() { makeDump(atPostBeginJob_); }
0107 
0108 void IgProfService::postBeginRun(GlobalContext const &gc) {
0109   nrun_ = gc.luminosityBlockID().run();
0110   makeDump(atPostBeginRun_);
0111 }
0112 
0113 void IgProfService::postBeginLumi(GlobalContext const &gc) {
0114   nlumi_ = gc.luminosityBlockID().luminosityBlock();
0115   makeDump(atPostBeginLumi_);
0116 }
0117 
0118 void IgProfService::preEvent(StreamContext const &iStream) {
0119   ++nrecord_;  // count before events
0120   nevent_ = iStream.eventID().event();
0121   if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0))
0122     makeDump(atPreEvent_);
0123 }
0124 
0125 void IgProfService::postEvent(StreamContext const &iStream) {
0126   nevent_ = iStream.eventID().event();
0127   if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0))
0128     makeDump(atPostEvent_);
0129 }
0130 
0131 void IgProfService::preModuleEvent(StreamContext const &iStream, ModuleCallingContext const &mcc) {
0132   nevent_ = iStream.eventID().event();
0133   if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0)) {
0134     auto const &moduleLabel = mcc.moduleDescription()->moduleLabel();
0135     auto const &moduleType = mcc.moduleDescription()->moduleName();
0136     if (std::binary_search(modules_.begin(), modules_.end(), moduleLabel) or
0137         std::binary_search(moduleTypes_.begin(), moduleTypes_.end(), moduleType)) {
0138       makeDump(atPreModuleEvent_, moduleLabel);
0139     }
0140   }
0141 }
0142 
0143 void IgProfService::postModuleEvent(StreamContext const &iStream, ModuleCallingContext const &mcc) {
0144   nevent_ = iStream.eventID().event();
0145   if ((prescale_ > 0) && (nrecord_ >= mineventrecord_) && (((nrecord_ - mineventrecord_) % prescale_) == 0)) {
0146     auto const &moduleLabel = mcc.moduleDescription()->moduleLabel();
0147     auto const &moduleType = mcc.moduleDescription()->moduleName();
0148     if (std::binary_search(modules_.begin(), modules_.end(), moduleLabel) or
0149         std::binary_search(moduleTypes_.begin(), moduleTypes_.end(), moduleType)) {
0150       makeDump(atPostModuleEvent_, moduleLabel);
0151     }
0152   }
0153 }
0154 
0155 void IgProfService::postEndLumi(GlobalContext const &gc) {
0156   nlumi_ = gc.luminosityBlockID().luminosityBlock();
0157   makeDump(atPostEndLumi_);
0158 }
0159 
0160 void IgProfService::preEndRun(GlobalContext const &gc) {
0161   nrun_ = gc.luminosityBlockID().run();
0162   makeDump(atPreEndRun_);
0163 }
0164 
0165 void IgProfService::postEndRun(GlobalContext const &gc) {
0166   nrun_ = gc.luminosityBlockID().run();
0167   makeDump(atPostEndRun_);
0168 }
0169 
0170 void IgProfService::preEndProcessBlock(GlobalContext const &gc) { makeDump(atPreEndProcessBlock_); }
0171 
0172 void IgProfService::postEndProcessBlock(GlobalContext const &gc) { makeDump(atPostEndProcessBlock_); }
0173 
0174 void IgProfService::preEndJob() { makeDump(atPreEndJob_); }
0175 
0176 void IgProfService::postEndJob() { makeDump(atPostEndJob_); }
0177 
0178 void IgProfService::postOpenFile(std::string const &) {
0179   ++nfileopened_;
0180   makeDump(atPostOpenFile_);
0181 }
0182 
0183 void IgProfService::postCloseFile(std::string const &) {
0184   ++nfileclosed_;
0185   makeDump(atPostCloseFile_);
0186 }
0187 
0188 void IgProfService::makeDump(const std::string &format, std::string_view moduleLabel) {
0189   if (!dump_ || format.empty())
0190     return;
0191 
0192   std::string final(format);
0193   final = replace(final, "%I", nrecord_);
0194   final = replaceU64(final, "%E", nevent_);
0195   final = replaceU64(final, "%R", nrun_);
0196   final = replaceU64(final, "%L", nlumi_);
0197   final = replace(final, "%F", nfileopened_);
0198   final = replace(final, "%C", nfileclosed_);
0199   final = replace(final, "%M", moduleLabel);
0200   dump_(final.c_str());
0201 }
0202 
0203 std::string IgProfService::replace(const std::string &s, const char *pat, int val) {
0204   size_t pos = 0;
0205   size_t patlen = strlen(pat);
0206   std::string result = s;
0207   while ((pos = result.find(pat, pos)) != std::string::npos) {
0208     char buf[64];
0209     int n = sprintf(buf, "%d", val);
0210     result.replace(pos, patlen, buf);
0211     pos = pos - patlen + n;
0212   }
0213 
0214   return result;
0215 }
0216 
0217 std::string IgProfService::replaceU64(const std::string &s, const char *pat, unsigned long long val) {
0218   size_t pos = 0;
0219   size_t patlen = strlen(pat);
0220   std::string result = s;
0221   while ((pos = result.find(pat, pos)) != std::string::npos) {
0222     char buf[64];
0223     int n = sprintf(buf, "%llu", val);
0224     result.replace(pos, patlen, buf);
0225     pos = pos - patlen + n;
0226   }
0227 
0228   return result;
0229 }
0230 
0231 std::string IgProfService::replace(const std::string &s, const char *pat, std::string_view val) {
0232   size_t pos = 0;
0233   size_t patlen = strlen(pat);
0234   std::string result = s;
0235   while ((pos = result.find(pat, pos)) != std::string::npos) {
0236     result.replace(pos, patlen, val.data());
0237     pos = pos - patlen + val.size();
0238   }
0239 
0240   return result;
0241 }
0242 
0243 DEFINE_FWK_SERVICE(IgProfService);