CMS 3D CMS Logo

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