CMS 3D CMS Logo

TFileAdaptor.cc
Go to the documentation of this file.
1 #include "TFileAdaptor.h"
2 
12 
13 #include <TROOT.h>
14 #include <TFile.h>
15 #include <TPluginManager.h>
16 
17 #include <memory>
18 
19 #include <algorithm>
20 #include <sstream>
21 
22 // Driver for configuring ROOT plug-in manager to use TStorageFactoryFile.
23 
38 void TFileAdaptor::addType(TPluginManager* mgr, char const* type, int altType /*=0*/) {
39  // HACK:
40  // The ROOT plug-in manager does not understand loading plugins with different
41  // signatures. So, because TXNetSystem is registered with a different constructor
42  // than all the other plugins, we must match its interface in order to override
43  // it.
44  if (altType == 0) {
45  mgr->AddHandler("TFile",
46  type,
47  "TStorageFactoryFile",
48  "IOPoolTFileAdaptor",
49  "TStorageFactoryFile(char const*,Option_t*,char const*,Int_t)");
50 
51  mgr->AddHandler("TSystem", type, "TStorageFactorySystem", "IOPoolTFileAdaptor", "TStorageFactorySystem()");
52  } else if (altType == 1) {
53  mgr->AddHandler("TFile",
54  type,
55  "TStorageFactoryFile",
56  "IOPoolTFileAdaptor",
57  "TStorageFactoryFile(char const*,Option_t*,char const*,Int_t, Int_t, Bool_t)");
58 
59  mgr->AddHandler(
60  "TSystem", type, "TStorageFactorySystem", "IOPoolTFileAdaptor", "TStorageFactorySystem(const char *,Bool_t)");
61  }
62 }
63 
64 bool TFileAdaptor::native(char const* proto) const {
65  return std::find(native_.begin(), native_.end(), "all") != native_.end() ||
66  std::find(native_.begin(), native_.end(), proto) != native_.end();
67 }
68 
70  : enabled_(true),
71  doStats_(true),
72  enablePrefetching_(false),
73  cacheHint_("auto-detect"),
74  readHint_("auto-detect"),
75  tempDir_(),
76  minFree_(0),
77  timeout_(0U),
78  debugLevel_(0U),
79  native_() {
80  if (!(enabled_ = pset.getUntrackedParameter<bool>("enable", enabled_)))
81  return;
82 
83  using namespace edm::storage;
84  StorageFactory* f = StorageFactory::getToModify();
85  doStats_ = pset.getUntrackedParameter<bool>("stats", doStats_);
86 
87  // values set in the site local config or in SiteLocalConfigService override
88  // any values set here for this service.
89  // These parameters here are needed only for backward compatibility
90  // for WMDM tools until we switch to only using the site local config for this info.
91  cacheHint_ = pset.getUntrackedParameter<std::string>("cacheHint", cacheHint_);
92  readHint_ = pset.getUntrackedParameter<std::string>("readHint", readHint_);
93  tempDir_ = pset.getUntrackedParameter<std::string>("tempDir", f->tempPath());
94  minFree_ = pset.getUntrackedParameter<double>("tempMinFree", f->tempMinFree());
95  native_ = pset.getUntrackedParameter<std::vector<std::string> >("native", native_);
96 
98 
99  // Retrieve values from SiteLocalConfigService.
100  // Any such values will override values set above.
102  if (pSLC.isAvailable()) {
103  if (std::string const* p = pSLC->sourceCacheTempDir()) {
104  tempDir_ = *p;
105  }
106  if (double const* p = pSLC->sourceCacheMinFree()) {
107  minFree_ = *p;
108  }
109  if (std::string const* p = pSLC->sourceCacheHint()) {
110  cacheHint_ = *p;
111  }
112  if (std::string const* p = pSLC->sourceReadHint()) {
113  readHint_ = *p;
114  }
115  if (unsigned int const* p = pSLC->sourceTimeout()) {
116  timeout_ = *p;
117  }
118  if (std::vector<std::string> const* p = pSLC->sourceNativeProtocols()) {
119  native_ = *p;
120  }
121  debugLevel_ = pSLC->debugLevel();
123  }
124 
125  // Prefetching does not work with storage-only; forcibly disable it.
126  if ((enablePrefetching_) && ((cacheHint_ == "storage-only") || (cacheHint_ == "auto-detect")))
127  cacheHint_ = "application-only";
128 
129  // tell factory how clients should access files
130  if (cacheHint_ == "application-only")
131  f->setCacheHint(StorageFactory::CACHE_HINT_APPLICATION);
132  else if (cacheHint_ == "storage-only")
133  f->setCacheHint(StorageFactory::CACHE_HINT_STORAGE);
134  else if (cacheHint_ == "lazy-download")
135  f->setCacheHint(StorageFactory::CACHE_HINT_LAZY_DOWNLOAD);
136  else if (cacheHint_ == "auto-detect")
137  f->setCacheHint(StorageFactory::CACHE_HINT_AUTO_DETECT);
138  else
139  throw cms::Exception("TFileAdaptor") << "Unrecognised 'cacheHint' value '" << cacheHint_
140  << "', recognised values are 'application-only',"
141  << " 'storage-only', 'lazy-download', 'auto-detect'";
142 
143  if (readHint_ == "direct-unbuffered")
144  f->setReadHint(StorageFactory::READ_HINT_UNBUFFERED);
145  else if (readHint_ == "read-ahead-buffered")
146  f->setReadHint(StorageFactory::READ_HINT_READAHEAD);
147  else if (readHint_ == "auto-detect")
148  f->setReadHint(StorageFactory::READ_HINT_AUTO);
149  else
150  throw cms::Exception("TFileAdaptor") << "Unrecognised 'readHint' value '" << readHint_
151  << "', recognised values are 'direct-unbuffered',"
152  << " 'read-ahead-buffered', 'auto-detect'";
153 
154  f->setTimeout(timeout_);
155  f->setDebugLevel(debugLevel_);
156 
157  // enable file access stats accounting if requested
158  f->enableAccounting(doStats_);
159 
160  // tell where to save files.
161  f->setTempDir(tempDir_, minFree_);
162 
163  // set our own root plugins
164  TPluginManager* mgr = gROOT->GetPluginManager();
165 
166  // Make sure ROOT parses system directories first.
167  mgr->LoadHandlersFromPluginDirs("TFile");
168  mgr->LoadHandlersFromPluginDirs("TSystem");
169 
170  if (!native("file"))
171  addType(mgr, "^file:");
172  if (!native("http"))
173  addType(mgr, "^http:");
174  if (!native("http"))
175  addType(mgr, "^http[s]?:");
176  if (!native("ftp"))
177  addType(mgr, "^ftp:");
178  /* always */ addType(mgr, "^web:");
179  /* always */ addType(mgr, "^gsiftp:");
180  /* always */ addType(mgr, "^sfn:");
181  if (!native("rfio"))
182  addType(mgr, "^rfio:");
183  if (!native("dcache"))
184  addType(mgr, "^dcache:");
185  if (!native("dcap"))
186  addType(mgr, "^dcap:");
187  if (!native("gsidcap"))
188  addType(mgr, "^gsidcap:");
189  if (!native("storm"))
190  addType(mgr, "^storm:");
191  if (!native("storm-lcg"))
192  addType(mgr, "^storm-lcg:");
193  if (!native("lstore"))
194  addType(mgr, "^lstore:");
195  if (!native("root"))
196  addType(mgr, "^root:", 1); // See comments in addType
197  if (!native("root"))
198  addType(mgr, "^[x]?root:", 1); // See comments in addType
199 }
200 
203  desc.addOptionalUntracked<bool>("enable");
204  desc.addOptionalUntracked<bool>("stats");
205  desc.addOptionalUntracked<std::string>("cacheHint");
206  desc.addOptionalUntracked<std::string>("readHint");
207  desc.addOptionalUntracked<std::string>("tempDir");
208  desc.addOptionalUntracked<double>("tempMinFree");
209  desc.addOptionalUntracked<std::vector<std::string> >("native");
210  descriptions.add("AdaptorConfig", desc);
211 }
212 
213 // Write current Storage statistics on a ostream
214 void TFileAdaptor::termination(void) const {
215  std::map<std::string, std::string> data;
216  statsXML(data);
217  if (!data.empty()) {
219  reportSvc->reportPerformanceSummary("StorageStatistics", data);
220  }
221 }
222 
223 void TFileAdaptor::stats(std::ostream& o) const {
224  if (!doStats_) {
225  return;
226  }
227  float const oneMeg = 1048576.0;
228  o << "Storage parameters: adaptor: true"
229  << " Stats:" << (doStats_ ? "true" : "false") << '\n'
230  << " Prefetching:" << (enablePrefetching_ ? "true" : "false") << '\n'
231  << " Cache hint:" << cacheHint_ << '\n'
232  << " Read hint:" << readHint_ << '\n'
233  << "Storage statistics: " << edm::storage::StorageAccount::summaryText() << "; tfile/read=?/?/"
234  << (TFile::GetFileBytesRead() / oneMeg) << "MB/?ms/?ms/?ms"
235  << "; tfile/write=?/?/" << (TFile::GetFileBytesWritten() / oneMeg) << "MB/?ms/?ms/?ms";
236 }
237 
238 void TFileAdaptor::statsXML(std::map<std::string, std::string>& data) const {
239  if (!doStats_) {
240  return;
241  }
242  float const oneMeg = 1048576.0;
243  data.insert(std::make_pair("Parameter-untracked-bool-enabled", "true"));
244  data.insert(std::make_pair("Parameter-untracked-bool-stats", (doStats_ ? "true" : "false")));
245  data.insert(std::make_pair("Parameter-untracked-bool-prefetching", (enablePrefetching_ ? "true" : "false")));
246  data.insert(std::make_pair("Parameter-untracked-string-cacheHint", cacheHint_));
247  data.insert(std::make_pair("Parameter-untracked-string-readHint", readHint_));
249  std::ostringstream r;
250  std::ostringstream w;
251  r << (TFile::GetFileBytesRead() / oneMeg);
252  w << (TFile::GetFileBytesWritten() / oneMeg);
253  data.insert(std::make_pair("ROOT-tfile-read-totalMegabytes", r.str()));
254  data.insert(std::make_pair("ROOT-tfile-write-totalMegabytes", w.str()));
255 }
256 
257 #include <iostream>
258 
261  const edm::ParameterSet param;
262  me = std::make_shared<TFileAdaptor>(param, ar); // propagate_const<T> has no reset() function
263 }
264 
266 
267 void TFileAdaptorUI::stats() const {
268  me->stats(std::cout);
269  std::cout << std::endl;
270 }
unsigned int timeout_
Definition: TFileAdaptor.h:47
static std::string summaryText(bool banner=false)
bool native(char const *proto) const
Definition: TFileAdaptor.cc:64
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
T w() const
std::string cacheHint_
Definition: TFileAdaptor.h:43
std::string readHint_
Definition: TFileAdaptor.h:44
void termination(void) const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
double minFree_
Definition: TFileAdaptor.h:46
static void fillSummary(std::map< std::string, std::string > &summary)
std::string tempDir_
Definition: TFileAdaptor.h:45
virtual bool enablePrefetching() const =0
unsigned int debugLevel_
Definition: TFileAdaptor.h:48
bool enablePrefetching_
Definition: TFileAdaptor.h:42
virtual std::string const * sourceCacheTempDir() const =0
virtual unsigned int const * sourceTimeout() const =0
void reportPerformanceSummary(std::string const &metricClass, std::map< std::string, std::string > const &metrics)
Definition: JobReport.cc:655
double f[11][100]
void stats(std::ostream &o) const
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
virtual unsigned int debugLevel() const =0
std::vector< std::string > native_
Definition: TFileAdaptor.h:49
static void addType(TPluginManager *mgr, char const *type, int altType=0)
Definition: TFileAdaptor.cc:38
void add(std::string const &label, ParameterSetDescription const &psetDescription)
virtual std::vector< std::string > const * sourceNativeProtocols() const =0
TFileAdaptor(edm::ParameterSet const &pset, edm::ActivityRegistry &ar)
Definition: TFileAdaptor.cc:69
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
virtual double const * sourceCacheMinFree() const =0
virtual std::string const * sourceCacheHint() const =0
bool isAvailable() const
Definition: Service.h:40
virtual std::string const * sourceReadHint() const =0
void statsXML(std::map< std::string, std::string > &data) const
void stats() const