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