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 <boost/shared_ptr.hpp>
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  cacheHint_("application-only"),
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.
101  cacheHint_ = pset.getUntrackedParameter<std::string> ("cacheHint", cacheHint_);
102  readHint_ = pset.getUntrackedParameter<std::string> ("readHint", readHint_);
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();
132  }
133 
134  // tell factory how clients should access files
135  if (cacheHint_ == "application-only")
137  else if (cacheHint_ == "storage-only")
139  else if (cacheHint_ == "lazy-download")
141  else if (cacheHint_ == "auto-detect")
143  else
144  throw cms::Exception("TFileAdaptor")
145  << "Unrecognised 'cacheHint' value '" << cacheHint_
146  << "', recognised values are 'application-only',"
147  << " 'storage-only', 'lazy-download', 'auto-detect'";
148 
149  if (readHint_ == "direct-unbuffered")
151  else if (readHint_ == "read-ahead-buffered")
153  else if (readHint_ == "auto-detect")
155  else
156  throw cms::Exception("TFileAdaptor")
157  << "Unrecognised 'readHint' value '" << readHint_
158  << "', recognised values are 'direct-unbuffered',"
159  << " 'read-ahead-buffered', 'auto-detect'";
160 
161  f->setTimeout(timeout_);
163 
164  // enable file access stats accounting if requested
166 
167  // tell where to save files.
168  f->setTempDir(tempDir_, minFree_);
169 
170  // set our own root plugins
171  TPluginManager* mgr = gROOT->GetPluginManager();
172  mgr->LoadHandlersFromPluginDirs();
173 
174  if (!native("file")) addType(mgr, "^file:");
175  if (!native("http")) addType(mgr, "^http:");
176  if (!native("ftp")) addType(mgr, "^ftp:");
177  /* always */ addType(mgr, "^web:");
178  /* always */ addType(mgr, "^gsiftp:");
179  /* always */ addType(mgr, "^sfn:");
180  if (!native("rfio")) addType(mgr, "^rfio:");
181  if (!native("dcache")) addType(mgr, "^dcache:");
182  if (!native("dcap")) addType(mgr, "^dcap:");
183  if (!native("gsidcap")) addType(mgr, "^gsidcap:");
184  if (!native("storm")) addType(mgr, "^storm:");
185  if (!native("storm-lcg")) addType(mgr, "^storm-lcg:");
186  if (!native("lstore")) addType(mgr, "^lstore:");
187  // This is ready to go from a code point-of-view.
188  // Waiting on the validation "OK" from Computing.
189  if (!native("root")) addType(mgr, "^root:", 1); // See comments in addType
190  }
191 
192  void
195  desc.addOptionalUntracked<bool>("enable");
196  desc.addOptionalUntracked<bool>("stats");
197  desc.addOptionalUntracked<std::string>("cacheHint");
198  desc.addOptionalUntracked<std::string>("readHint");
199  desc.addOptionalUntracked<std::string>("tempDir");
200  desc.addOptionalUntracked<double>("tempMinFree");
201  desc.addOptionalUntracked<std::vector<std::string> >("native");
202  descriptions.add("AdaptorConfig", desc);
203  }
204 
205  // Write current Storage statistics on a ostream
206  void
208  std::map<std::string, std::string> data;
209  statsXML(data);
210  if (!data.empty()) {
212  reportSvc->reportPerformanceSummary("StorageStatistics", data);
213  }
214  }
215 
216  void
217  TFileAdaptor::stats(std::ostream& o) const {
218  if (!doStats_) {
219  return;
220  }
221  float const oneMeg = 1048576.0;
222  o << "Storage parameters: adaptor: true"
223  << " Stats:" << (doStats_ ? "true" : "false") << '\n'
224  << " Cache hint:" << cacheHint_ << '\n'
225  << " Read hint:" << readHint_ << '\n'
226  << "Storage statistics: "
228  << "; tfile/read=?/?/" << (TFile::GetFileBytesRead() / oneMeg) << "MB/?ms/?ms/?ms"
229  << "; tfile/write=?/?/" << (TFile::GetFileBytesWritten() / oneMeg) << "MB/?ms/?ms/?ms";
230  }
231 
232  void
233  TFileAdaptor::statsXML(std::map<std::string, std::string>& data) const {
234  if (!doStats_) {
235  return;
236  }
237  float const oneMeg = 1048576.0;
238  data.insert(std::make_pair("Parameter-untracked-bool-enabled", "true"));
239  data.insert(std::make_pair("Parameter-untracked-bool-stats", (doStats_ ? "true" : "false")));
240  data.insert(std::make_pair("Parameter-untracked-string-cacheHint", cacheHint_));
241  data.insert(std::make_pair("Parameter-untracked-string-readHint", readHint_));
243  std::ostringstream r;
244  std::ostringstream w;
245  r << (TFile::GetFileBytesRead() / oneMeg);
246  w << (TFile::GetFileBytesWritten() / oneMeg);
247  data.insert(std::make_pair("ROOT-tfile-read-totalMegabytes", r.str()));
248  data.insert(std::make_pair("ROOT-tfile-write-totalMegabytes", w.str()));
249  }
250 
251 /*
252  * wrapper to bind TFileAdaptor to root, python etc
253  * loading IOPoolTFileAdaptor library and instantiating
254  * TFileAdaptorUI will make root to use StorageAdaptor for I/O instead
255  * of its own plugins
256  */
258 public:
259 
260  TFileAdaptorUI();
261  ~TFileAdaptorUI();
262 
263  // print current Storage statistics on cout
264  void stats() const;
265 
266 private:
267  boost::shared_ptr<TFileAdaptor> me;
268 };
269 
270 #include <iostream>
271 
274  const edm::ParameterSet param;
275  me.reset(new TFileAdaptor(param, ar));
276 }
277 
279 
280 void TFileAdaptorUI::stats() const {
281  me->stats(std::cout); std::cout << std::endl;
282 }
283 
285 
type
Definition: HCALResponse.h:22
T getUntrackedParameter(std::string const &, T const &) const
unsigned int timeout_
Definition: TFileAdaptor.h:40
bool enableAccounting(bool enabled)
static void fillSummary(std::map< std::string, std::string > &summary)
bool native(char const *proto) const
Definition: TFileAdaptor.cc:76
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:36
std::string readHint_
Definition: TFileAdaptor.h:37
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:39
std::string tempDir_
Definition: TFileAdaptor.h:38
static StorageFactory * get(void)
unsigned int debugLevel_
Definition: TFileAdaptor.h:41
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:47
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:42
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
void reportPerformanceSummary(std::string const &metricClass, std::map< std::string, std::string > const &metrics)
Definition: JobReport.cc:844
TFileAdaptor AdaptorConfig
void termination(void) const
T w() const
void statsXML(std::map< std::string, std::string > &data) const