00001 #include "IOPool/TFileAdaptor/interface/TStorageFactoryFile.h"
00002 #include "Utilities/StorageFactory/interface/StorageAccount.h"
00003 #include "Utilities/StorageFactory/interface/StorageFactory.h"
00004 #include "FWCore/ServiceRegistry/interface/ServiceMaker.h"
00005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00006 #include "FWCore/ServiceRegistry/interface/Service.h"
00007 #include "FWCore/MessageLogger/interface/JobReport.h"
00008 #include "FWCore/Utilities/interface/EDMException.h"
00009 #include <TROOT.h>
00010 #include <TPluginManager.h>
00011 #include <TFile.h>
00012 #include <sstream>
00013 #include <iostream>
00014 #include <algorithm>
00015 #include <vector>
00016 #include <string>
00017
00018
00019 class TFileAdaptor
00020 {
00021 bool enabled_;
00022 bool doStats_;
00023 std::string cacheHint_;
00024 std::string readHint_;
00025 std::string tempDir_;
00026 std::vector<std::string> native_;
00027
00028 static void addType(TPluginManager *mgr, const char *type)
00029 {
00030 mgr->AddHandler("TFile",
00031 type,
00032 "TStorageFactoryFile",
00033 "IOPoolTFileAdaptor",
00034 "TStorageFactoryFile(const char*,Option_t*,const char*,Int_t)");
00035
00036 mgr->AddHandler("TSystem",
00037 type,
00038 "TStorageFactorySystem",
00039 "IOPoolTFileAdaptor",
00040 "TStorageFactorySystem()");
00041 }
00042
00043 bool native(const char *proto) const
00044 {
00045 return std::find(native_.begin(), native_.end(), "all") != native_.end()
00046 || std::find(native_.begin(), native_.end(), proto) != native_.end();
00047 }
00048
00049 public:
00050 TFileAdaptor(const edm::ParameterSet &p, edm::ActivityRegistry &ar)
00051 : enabled_(true),
00052 doStats_(true),
00053 cacheHint_("application-only"),
00054 readHint_("auto-detect"),
00055 tempDir_(".")
00056 {
00057 if (! (enabled_ = p.getUntrackedParameter<bool> ("enable", enabled_)))
00058 return;
00059
00060 StorageFactory *f = StorageFactory::get();
00061 doStats_ = p.getUntrackedParameter<bool> ("stats", doStats_);
00062 cacheHint_ = p.getUntrackedParameter<std::string> ("cacheHint", cacheHint_);
00063 readHint_ = p.getUntrackedParameter<std::string> ("readHint", readHint_);
00064 tempDir_ = p.getUntrackedParameter<std::string> ("tempDir", tempDir_);
00065 native_ = p.getUntrackedParameter<std::vector<std::string> >("native", native_);
00066 ar.watchPostEndJob(this, &TFileAdaptor::termination);
00067
00068
00069 if (cacheHint_ == "application-only")
00070 f->setCacheHint(StorageFactory::CACHE_HINT_APPLICATION);
00071 else if (cacheHint_ == "storage-only")
00072 f->setCacheHint(StorageFactory::CACHE_HINT_STORAGE);
00073 else if (cacheHint_ == "lazy-download")
00074 f->setCacheHint(StorageFactory::CACHE_HINT_LAZY_DOWNLOAD);
00075 else if (cacheHint_ == "auto-detect")
00076 f->setCacheHint(StorageFactory::CACHE_HINT_AUTO_DETECT);
00077 else
00078 throw cms::Exception("TFileAdaptor")
00079 << "Unrecognised 'cacheHint' value '" << cacheHint_
00080 << "', recognised values are 'application-only',"
00081 << " 'storage-only', 'lazy-download', 'auto-detect'";
00082
00083 if (readHint_ == "direct-unbuffered")
00084 f->setReadHint(StorageFactory::READ_HINT_UNBUFFERED);
00085 else if (readHint_ == "read-ahead-buffered")
00086 f->setReadHint(StorageFactory::READ_HINT_READAHEAD);
00087 else if (readHint_ == "auto-detect")
00088 f->setReadHint(StorageFactory::READ_HINT_AUTO);
00089 else
00090 throw cms::Exception("TFileAdaptor")
00091 << "Unrecognised 'readHint' value '" << readHint_
00092 << "', recognised values are 'direct-unbuffered',"
00093 << " 'read-ahead-buffered', 'auto-detect'";
00094
00095
00096 f->enableAccounting(doStats_);
00097
00098
00099 f->setTempDir(tempDir_);
00100
00101
00102 TPluginManager *mgr = gROOT->GetPluginManager();
00103 mgr->LoadHandlersFromPluginDirs();
00104
00105 if (!native("file")) addType(mgr, "^file:");
00106 if (!native("https")) addType(mgr, "^https:");
00107 if (!native("ftp")) addType(mgr, "^ftp:");
00108 addType(mgr, "^web:");
00109 addType(mgr, "^gsiftp:");
00110 addType(mgr, "^sfn:");
00111 if (!native("rfio")) addType(mgr, "^rfio:");
00112 if (!native("dcache")) addType(mgr, "^dcache:");
00113 if (!native("dcap")) addType(mgr, "^dcap:");
00114 if (!native("gsidcap")) addType(mgr, "^gsidcap:");
00115 if (!native("storm")) addType(mgr, "^storm:");
00116 if (!native("storm-lcg")) addType(mgr, "^storm-lcg:");
00117 }
00118
00119
00120 void termination(void) const
00121 {
00122 std::ostringstream os;
00123 statsXML(os);
00124 if (! os.str().empty())
00125 {
00126 edm::Service<edm::JobReport> jr;
00127 jr->reportStorageStats(os.str());
00128 }
00129 }
00130
00131 void stats(std::ostream &o) const
00132 {
00133 if (! doStats_)
00134 return;
00135
00136 o << "Storage parameters: adaptor: true"
00137 << " Stats:" << (doStats_ ? "true" : "false") << '\n'
00138 << " Cache hint:" << cacheHint_ << '\n'
00139 << " Read hint:" << readHint_ << '\n'
00140 << "Storage statistics: "
00141 << StorageAccount::summaryText()
00142 << "; tfile/read=?/?/" << (TFile::GetFileBytesRead() / 1048576.0) << "MB/?ms/?ms/?ms"
00143 << "; tfile/write=?/?/" << (TFile::GetFileBytesWritten() / 1048576.0) << "MB/?ms/?ms/?ms";
00144 }
00145
00146 void statsXML(std::ostream &o) const
00147 {
00148 if (! doStats_)
00149 return;
00150
00151 o << "<storage-factory-summary>\n"
00152 << " <storage-factory-params>\n"
00153 << " <param name='enabled' value='true' unit='boolean'/>\n"
00154 << " <param name='cache-hint' value='" << cacheHint_ << "' unit='string'/>\n"
00155 << " <param name='read-hint' value='" << readHint_ << "' unit='string'/>\n"
00156 << " <param name='stats' value='" << (doStats_ ? "true" : "false") << "' unit='boolean'/>\n"
00157 << " </storage-factory-params>\n"
00158
00159 << " <storage-factory-stats>\n"
00160 << StorageAccount::summaryXML() << std::endl
00161 << " <storage-root-summary>\n"
00162 << " <counter-value subsystem='tfile' counter-name='read' total-megabytes='"
00163 << (TFile::GetFileBytesRead() / 1048576.0) << "'/>\n"
00164 << " <counter-value subsystem='tfile' counter-name='write' total-megabytes='"
00165 << (TFile::GetFileBytesWritten() / 1048576.0) << "'/>\n"
00166 << " </storage-root-summary>\n"
00167 << " </storage-factory-stats>\n"
00168 << "</storage-factory-summary>";
00169 }
00170 };
00171
00172 typedef TFileAdaptor AdaptorConfig;
00173 DEFINE_FWK_SERVICE(AdaptorConfig);