CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/IOPool/TFileAdaptor/src/TFileAdaptor.cc

Go to the documentation of this file.
00001 #include "TFileAdaptor.h"
00002 
00003 #include "FWCore/Catalog/interface/SiteLocalConfig.h"
00004 #include "FWCore/MessageLogger/interface/JobReport.h"
00005 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
00006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00007 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00008 #include "FWCore/ServiceRegistry/interface/Service.h"
00009 #include "FWCore/ServiceRegistry/interface/ServiceMaker.h"
00010 #include "FWCore/Utilities/interface/EDMException.h"
00011 #include "Utilities/StorageFactory/interface/StorageAccount.h"
00012 #include "Utilities/StorageFactory/interface/StorageFactory.h"
00013 
00014 #include <TROOT.h>
00015 #include <TFile.h>
00016 #include <TPluginManager.h>
00017 
00018 #include <boost/shared_ptr.hpp>
00019 
00020 #include <algorithm>
00021 #include <sstream>
00022 
00023 // Driver for configuring ROOT plug-in manager to use TStorageFactoryFile.
00024   void
00025   TFileAdaptor::addType(TPluginManager* mgr, char const* type) {
00026     mgr->AddHandler("TFile",
00027                     type,
00028                     "TStorageFactoryFile",
00029                     "IOPoolTFileAdaptor",
00030                     "TStorageFactoryFile(char const*,Option_t*,char const*,Int_t)");
00031 
00032     mgr->AddHandler("TSystem",
00033                     type,
00034                     "TStorageFactorySystem",
00035                     "IOPoolTFileAdaptor",
00036                     "TStorageFactorySystem()");
00037   }
00038 
00039   bool
00040   TFileAdaptor::native(char const* proto) const {
00041     return std::find(native_.begin(), native_.end(), "all") != native_.end()
00042       || std::find(native_.begin(), native_.end(), proto) != native_.end();
00043   }
00044 
00045   TFileAdaptor::TFileAdaptor(edm::ParameterSet const& p, edm::ActivityRegistry& ar)
00046     : enabled_(true),
00047       doStats_(true),
00048       cacheHint_("application-only"),
00049       readHint_("auto-detect"),
00050       tempDir_(),
00051       minFree_(0),
00052       timeout_(0U),
00053       native_() {
00054     if (!(enabled_ = p.getUntrackedParameter<bool> ("enable", enabled_)))
00055       return;
00056 
00057     StorageFactory* f = StorageFactory::get();
00058     doStats_ = p.getUntrackedParameter<bool> ("stats", doStats_);
00059 
00060     // values set in the site local config or in SiteLocalConfigService override
00061     // any values set here for this service.
00062     // These parameters here are needed only for backward compatibility
00063     // for WMDM tools until we switch to only using the site local config for this info.
00064     cacheHint_ = p.getUntrackedParameter<std::string> ("cacheHint", cacheHint_);
00065     readHint_ = p.getUntrackedParameter<std::string> ("readHint", readHint_);
00066     tempDir_ = p.getUntrackedParameter<std::string> ("tempDir", f->tempPath());
00067     minFree_ = p.getUntrackedParameter<double> ("tempMinFree", f->tempMinFree());
00068     native_ = p.getUntrackedParameter<std::vector<std::string> >("native", native_);
00069 
00070     ar.watchPostEndJob(this, &TFileAdaptor::termination);
00071 
00072     // Retrieve values from SiteLocalConfigService.
00073     // Any such values will override values set above.
00074     edm::Service<edm::SiteLocalConfig> pSLC;
00075     if (pSLC.isAvailable()) {
00076       if (std::string const* p = pSLC->sourceCacheTempDir()) {
00077         tempDir_ = *p;
00078       }
00079       if (double const* p = pSLC->sourceCacheMinFree()) {
00080         minFree_ = *p;
00081       }
00082       if (std::string const* p = pSLC->sourceCacheHint()) {
00083         cacheHint_ = *p;
00084       }
00085       if (std::string const* p = pSLC->sourceReadHint()) {
00086         readHint_ = *p;
00087       }
00088       if (unsigned int const* p = pSLC->sourceTimeout()) {
00089         timeout_ = *p;
00090       }
00091       if (std::vector<std::string> const* p = pSLC->sourceNativeProtocols()) {
00092         native_ = *p;
00093       }
00094     }
00095 
00096     // tell factory how clients should access files
00097     if (cacheHint_ == "application-only")
00098       f->setCacheHint(StorageFactory::CACHE_HINT_APPLICATION);
00099     else if (cacheHint_ == "storage-only")
00100       f->setCacheHint(StorageFactory::CACHE_HINT_STORAGE);
00101     else if (cacheHint_ == "lazy-download")
00102       f->setCacheHint(StorageFactory::CACHE_HINT_LAZY_DOWNLOAD);
00103     else if (cacheHint_ == "auto-detect")
00104       f->setCacheHint(StorageFactory::CACHE_HINT_AUTO_DETECT);
00105     else
00106       throw cms::Exception("TFileAdaptor")
00107         << "Unrecognised 'cacheHint' value '" << cacheHint_
00108         << "', recognised values are 'application-only',"
00109         << " 'storage-only', 'lazy-download', 'auto-detect'";
00110 
00111     if (readHint_ == "direct-unbuffered")
00112       f->setReadHint(StorageFactory::READ_HINT_UNBUFFERED);
00113     else if (readHint_ == "read-ahead-buffered")
00114       f->setReadHint(StorageFactory::READ_HINT_READAHEAD);
00115     else if (readHint_ == "auto-detect")
00116       f->setReadHint(StorageFactory::READ_HINT_AUTO);
00117     else
00118       throw cms::Exception("TFileAdaptor")
00119         << "Unrecognised 'readHint' value '" << readHint_
00120         << "', recognised values are 'direct-unbuffered',"
00121         << " 'read-ahead-buffered', 'auto-detect'";
00122 
00123     f->setTimeout(timeout_);
00124 
00125     // enable file access stats accounting if requested
00126     f->enableAccounting(doStats_);
00127 
00128     // tell where to save files.
00129     f->setTempDir(tempDir_, minFree_);
00130 
00131     // set our own root plugins
00132     TPluginManager* mgr = gROOT->GetPluginManager();
00133     mgr->LoadHandlersFromPluginDirs();
00134 
00135     if (!native("file"))      addType(mgr, "^file:");
00136     if (!native("http"))      addType(mgr, "^http:");
00137     if (!native("ftp"))       addType(mgr, "^ftp:");
00138     /* always */              addType(mgr, "^web:");
00139     /* always */              addType(mgr, "^gsiftp:");
00140     /* always */              addType(mgr, "^sfn:");
00141     if (!native("rfio"))      addType(mgr, "^rfio:");
00142     if (!native("dcache"))    addType(mgr, "^dcache:");
00143     if (!native("dcap"))      addType(mgr, "^dcap:");
00144     if (!native("gsidcap"))   addType(mgr, "^gsidcap:");
00145     if (!native("storm"))     addType(mgr, "^storm:");
00146     if (!native("storm-lcg")) addType(mgr, "^storm-lcg:");
00147     if (!native("lstore"))    addType(mgr, "^lstore:");
00148   }
00149 
00150   void
00151   TFileAdaptor::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
00152     edm::ParameterSetDescription desc;
00153     desc.addOptionalUntracked<bool>("enable");
00154     desc.addOptionalUntracked<bool>("stats");
00155     desc.addOptionalUntracked<std::string>("cacheHint");
00156     desc.addOptionalUntracked<std::string>("readHint");
00157     desc.addOptionalUntracked<std::string>("tempDir");
00158     desc.addOptionalUntracked<double>("tempMinFree");
00159     desc.addOptionalUntracked<std::vector<std::string> >("native");
00160     descriptions.add("AdaptorConfig", desc);
00161   }
00162 
00163   // Write current Storage statistics on a ostream
00164   void
00165   TFileAdaptor::termination(void) const {
00166     std::map<std::string, std::string> data;
00167     statsXML(data);
00168     if (!data.empty()) {
00169       edm::Service<edm::JobReport> reportSvc;
00170       reportSvc->reportPerformanceSummary("StorageStatistics", data);
00171     }
00172   }
00173 
00174   void
00175   TFileAdaptor::stats(std::ostream& o) const {
00176     if (!doStats_) {
00177       return;
00178     }
00179     float const oneMeg = 1048576.0;
00180     o << "Storage parameters: adaptor: true"
00181       << " Stats:" << (doStats_ ? "true" : "false") << '\n'
00182       << " Cache hint:" << cacheHint_ << '\n'
00183       << " Read hint:" << readHint_ << '\n'
00184       << "Storage statistics: "
00185       << StorageAccount::summaryText()
00186       << "; tfile/read=?/?/" << (TFile::GetFileBytesRead() / oneMeg) << "MB/?ms/?ms/?ms"
00187       << "; tfile/write=?/?/" << (TFile::GetFileBytesWritten() / oneMeg) << "MB/?ms/?ms/?ms";
00188   }
00189 
00190   void
00191   TFileAdaptor::statsXML(std::map<std::string, std::string>& data) const {
00192     if (!doStats_) {
00193       return;
00194     }
00195     float const oneMeg = 1048576.0;
00196     data.insert(std::make_pair("Parameter-untracked-bool-enabled", "true"));
00197     data.insert(std::make_pair("Parameter-untracked-bool-stats", (doStats_ ? "true" : "false")));
00198     data.insert(std::make_pair("Parameter-untracked-string-cacheHint", cacheHint_));
00199     data.insert(std::make_pair("Parameter-untracked-string-readHint", readHint_));
00200     StorageAccount::fillSummary(data);
00201     std::ostringstream r;
00202     std::ostringstream w;
00203     r << (TFile::GetFileBytesRead() / oneMeg);
00204     w << (TFile::GetFileBytesWritten() / oneMeg);
00205     data.insert(std::make_pair("ROOT-tfile-read-totalMegabytes", r.str()));
00206     data.insert(std::make_pair("ROOT-tfile-write-totalMegabytes", w.str()));
00207   }
00208 
00209 /*
00210  * wrapper to bind TFileAdaptor to root, python etc
00211  * loading IOPoolTFileAdaptor library and instantiating
00212  * TFileAdaptorUI will make root to use StorageAdaptor for I/O instead
00213  * of its own plugins
00214  */
00215 class TFileAdaptorUI {
00216 public:
00217 
00218   TFileAdaptorUI();
00219   ~TFileAdaptorUI();
00220 
00221   // print current Storage statistics on cout
00222   void stats() const;
00223 
00224 private:
00225   boost::shared_ptr<TFileAdaptor> me;
00226 };
00227 
00228 #include <iostream>
00229 
00230 TFileAdaptorUI::TFileAdaptorUI() {
00231   edm::ActivityRegistry ar;
00232   const edm::ParameterSet param;
00233   me.reset(new TFileAdaptor(param, ar));
00234 }
00235 
00236 TFileAdaptorUI::~TFileAdaptorUI() {}
00237 
00238 void TFileAdaptorUI::stats() const {
00239   me->stats(std::cout); std::cout << std::endl;
00240 }
00241 
00242 typedef TFileAdaptor AdaptorConfig;
00243 
00244 DEFINE_FWK_SERVICE(AdaptorConfig);