CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
40  TFileAdaptor::addType(TPluginManager* mgr, char const* type, int altType /*=0*/) {
41 
42  // HACK:
43  // The ROOT plug-in manager does not understand loading plugins with different
44  // signatures. So, because TXNetSystem is registered with a different constructor
45  // than all the other plugins, we must match its interface in order to override
46  // it.
47  if (altType == 0) {
48  mgr->AddHandler("TFile",
49  type,
50  "TStorageFactoryFile",
51  "IOPoolTFileAdaptor",
52  "TStorageFactoryFile(char const*,Option_t*,char const*,Int_t)");
53 
54  mgr->AddHandler("TSystem",
55  type,
56  "TStorageFactorySystem",
57  "IOPoolTFileAdaptor",
58  "TStorageFactorySystem()");
59  } else if (altType == 1) {
60  mgr->AddHandler("TFile",
61  type,
62  "TStorageFactoryFile",
63  "IOPoolTFileAdaptor",
64  "TStorageFactoryFile(char const*,Option_t*,char const*,Int_t, Int_t, Bool_t)");
65 
66  mgr->AddHandler("TSystem",
67  type,
68  "TStorageFactorySystem",
69  "IOPoolTFileAdaptor",
70  "TStorageFactorySystem(const char *,Bool_t)");
71  }
72 
73  }
74 
75  bool
76  TFileAdaptor::native(char const* proto) const {
77  return std::find(native_.begin(), native_.end(), "all") != native_.end()
78  || std::find(native_.begin(), native_.end(), proto) != native_.end();
79  }
80 
82  : enabled_(true),
83  doStats_(true),
84  enablePrefetching_(false),
85  cacheHint_("auto-detect"),
86  readHint_("auto-detect"),
87  tempDir_(),
88  minFree_(0),
89  timeout_(0U),
90  debugLevel_(0U),
91  native_() {
92  if (!(enabled_ = pset.getUntrackedParameter<bool> ("enable", enabled_)))
93  return;
94 
96  doStats_ = pset.getUntrackedParameter<bool> ("stats", doStats_);
97 
98  // values set in the site local config or in SiteLocalConfigService override
99  // any values set here for this service.
100  // These parameters here are needed only for backward compatibility
101  // for WMDM tools until we switch to only using the site local config for this info.
104  tempDir_ = pset.getUntrackedParameter<std::string> ("tempDir", f->tempPath());
105  minFree_ = pset.getUntrackedParameter<double> ("tempMinFree", f->tempMinFree());
106  native_ = pset.getUntrackedParameter<std::vector<std::string> >("native", native_);
107 
109 
110  // Retrieve values from SiteLocalConfigService.
111  // Any such values will override values set above.
113  if (pSLC.isAvailable()) {
114  if (std::string const* p = pSLC->sourceCacheTempDir()) {
115  tempDir_ = *p;
116  }
117  if (double const* p = pSLC->sourceCacheMinFree()) {
118  minFree_ = *p;
119  }
120  if (std::string const* p = pSLC->sourceCacheHint()) {
121  cacheHint_ = *p;
122  }
123  if (std::string const* p = pSLC->sourceReadHint()) {
124  readHint_ = *p;
125  }
126  if (unsigned int const* p = pSLC->sourceTimeout()) {
127  timeout_ = *p;
128  }
129  if (std::vector<std::string> const* p = pSLC->sourceNativeProtocols()) {
130  native_ = *p;
131  }
132  debugLevel_ = pSLC->debugLevel();
134  }
135 
136  // Prefetching does not work with storage-only; forcibly disable it.
137  if ((enablePrefetching_) && ((cacheHint_ == "storage-only") || (cacheHint_ == "auto-detect")))
138  cacheHint_ = "application-only";
139 
140  // tell factory how clients should access files
141  if (cacheHint_ == "application-only")
143  else if (cacheHint_ == "storage-only")
145  else if (cacheHint_ == "lazy-download")
147  else if (cacheHint_ == "auto-detect")
149  else
150  throw cms::Exception("TFileAdaptor")
151  << "Unrecognised 'cacheHint' value '" << cacheHint_
152  << "', recognised values are 'application-only',"
153  << " 'storage-only', 'lazy-download', 'auto-detect'";
154 
155  if (readHint_ == "direct-unbuffered")
157  else if (readHint_ == "read-ahead-buffered")
159  else if (readHint_ == "auto-detect")
161  else
162  throw cms::Exception("TFileAdaptor")
163  << "Unrecognised 'readHint' value '" << readHint_
164  << "', recognised values are 'direct-unbuffered',"
165  << " 'read-ahead-buffered', 'auto-detect'";
166 
167  f->setTimeout(timeout_);
169 
170  // enable file access stats accounting if requested
172 
173  // tell where to save files.
174  f->setTempDir(tempDir_, minFree_);
175 
176  // set our own root plugins
177  TPluginManager* mgr = gROOT->GetPluginManager();
178 
179  if (!native("file")) addType(mgr, "^file:");
180  if (!native("http")) addType(mgr, "^http:");
181  if (!native("ftp")) addType(mgr, "^ftp:");
182  /* always */ addType(mgr, "^web:");
183  /* always */ addType(mgr, "^gsiftp:");
184  /* always */ addType(mgr, "^sfn:");
185  if (!native("rfio")) addType(mgr, "^rfio:");
186  if (!native("dcache")) addType(mgr, "^dcache:");
187  if (!native("dcap")) addType(mgr, "^dcap:");
188  if (!native("gsidcap")) addType(mgr, "^gsidcap:");
189  if (!native("storm")) addType(mgr, "^storm:");
190  if (!native("storm-lcg")) addType(mgr, "^storm-lcg:");
191  if (!native("lstore")) addType(mgr, "^lstore:");
192  // This is ready to go from a code point-of-view.
193  // Waiting on the validation "OK" from Computing.
194  if (!native("root")) addType(mgr, "^root:", 1); // See comments in addType
195  }
196 
197  void
200  desc.addOptionalUntracked<bool>("enable");
201  desc.addOptionalUntracked<bool>("stats");
202  desc.addOptionalUntracked<std::string>("cacheHint");
203  desc.addOptionalUntracked<std::string>("readHint");
204  desc.addOptionalUntracked<std::string>("tempDir");
205  desc.addOptionalUntracked<double>("tempMinFree");
206  desc.addOptionalUntracked<std::vector<std::string> >("native");
207  descriptions.add("AdaptorConfig", desc);
208  }
209 
210  // Write current Storage statistics on a ostream
211  void
213  std::map<std::string, std::string> data;
214  statsXML(data);
215  if (!data.empty()) {
217  reportSvc->reportPerformanceSummary("StorageStatistics", data);
218  }
219  }
220 
221  void
222  TFileAdaptor::stats(std::ostream& o) const {
223  if (!doStats_) {
224  return;
225  }
226  float const oneMeg = 1048576.0;
227  o << "Storage parameters: adaptor: true"
228  << " Stats:" << (doStats_ ? "true" : "false") << '\n'
229  << " Prefetching:" << (enablePrefetching_ ? "true" : "false") << '\n'
230  << " Cache hint:" << cacheHint_ << '\n'
231  << " Read hint:" << readHint_ << '\n'
232  << "Storage statistics: "
234  << "; tfile/read=?/?/" << (TFile::GetFileBytesRead() / oneMeg) << "MB/?ms/?ms/?ms"
235  << "; tfile/write=?/?/" << (TFile::GetFileBytesWritten() / oneMeg) << "MB/?ms/?ms/?ms";
236  }
237 
238  void
239  TFileAdaptor::statsXML(std::map<std::string, std::string>& data) const {
240  if (!doStats_) {
241  return;
242  }
243  float const oneMeg = 1048576.0;
244  data.insert(std::make_pair("Parameter-untracked-bool-enabled", "true"));
245  data.insert(std::make_pair("Parameter-untracked-bool-stats", (doStats_ ? "true" : "false")));
246  data.insert(std::make_pair("Parameter-untracked-bool-prefetching", (enablePrefetching_ ? "true" : "false")));
247  data.insert(std::make_pair("Parameter-untracked-string-cacheHint", cacheHint_));
248  data.insert(std::make_pair("Parameter-untracked-string-readHint", readHint_));
250  std::ostringstream r;
251  std::ostringstream w;
252  r << (TFile::GetFileBytesRead() / oneMeg);
253  w << (TFile::GetFileBytesWritten() / oneMeg);
254  data.insert(std::make_pair("ROOT-tfile-read-totalMegabytes", r.str()));
255  data.insert(std::make_pair("ROOT-tfile-write-totalMegabytes", w.str()));
256  }
257 
258 /*
259  * wrapper to bind TFileAdaptor to root, python etc
260  * loading IOPoolTFileAdaptor library and instantiating
261  * TFileAdaptorUI will make root to use StorageAdaptor for I/O instead
262  * of its own plugins
263  */
265 public:
266 
267  TFileAdaptorUI();
268  ~TFileAdaptorUI();
269 
270  // print current Storage statistics on cout
271  void stats() const;
272 
273 private:
274  boost::shared_ptr<TFileAdaptor> me;
275 };
276 
277 #include <iostream>
278 
281  const edm::ParameterSet param;
282  me.reset(new TFileAdaptor(param, ar));
283 }
284 
286 
287 void TFileAdaptorUI::stats() const {
288  me->stats(std::cout); std::cout << std::endl;
289 }
290 
292 
type
Definition: HCALResponse.h:21
T getUntrackedParameter(std::string const &, T const &) const
unsigned int timeout_
Definition: TFileAdaptor.h:43
bool enableAccounting(bool enabled)
static void fillSummary(std::map< std::string, std::string > &summary)
bool native(char const *proto) const
Definition: TFileAdaptor.cc:76
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:39
std::string readHint_
Definition: TFileAdaptor.h:40
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:7
double tempMinFree(void) const
double minFree_
Definition: TFileAdaptor.h:42
std::string tempDir_
Definition: TFileAdaptor.h:41
virtual bool enablePrefetching() const =0
static StorageFactory * get(void)
unsigned int debugLevel_
Definition: TFileAdaptor.h:44
bool enablePrefetching_
Definition: TFileAdaptor.h:38
virtual std::string const * sourceCacheTempDir() const =0
boost::shared_ptr< TFileAdaptor > me
virtual unsigned int const * sourceTimeout() 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:733
double f[11][100]
void setDebugLevel(unsigned int level)
void stats() const
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:113
virtual unsigned int debugLevel() const =0
std::vector< std::string > native_
Definition: TFileAdaptor.h:45
static void addType(TPluginManager *mgr, char const *type, int altType=0)
Definition: TFileAdaptor.cc:40
void add(std::string const &label, ParameterSetDescription const &psetDescription)
virtual std::vector< std::string > const * sourceNativeProtocols() const =0
void setTempDir(const std::string &s, double minFreeSpace)
TFileAdaptor(edm::ParameterSet const &pset, edm::ActivityRegistry &ar)
Definition: TFileAdaptor.cc:81
void setCacheHint(CacheHint value)
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
virtual double const * sourceCacheMinFree() const =0
virtual std::string const * sourceCacheHint() const =0
static std::string summaryText(bool banner=false)
tuple cout
Definition: gather_cfg.py:121
ParameterDescriptionBase * addOptionalUntracked(U const &iLabel, T const &value)
virtual std::string const * sourceReadHint() const =0
volatile std::atomic< bool > shutdown_flag false
TFileAdaptor AdaptorConfig
void termination(void) const
void statsXML(std::map< std::string, std::string > &data) const