XRootD
XrdOssStatsConfig.cc
Go to the documentation of this file.
1 
2 #include "XrdVersion.hh"
3 
4 #include "XrdOssStatsConfig.hh"
6 #include "XrdSys/XrdSysError.hh"
7 
8 #include <sstream>
9 
10 std::string XrdOssStats::detail::LogMaskToString(int mask) {
11  if (mask == LogMask::All) {return "all";}
12 
13  bool has_entry = false;
14  std::stringstream ss;
15  if (mask & LogMask::Debug) {
16  ss << "debug";
17  has_entry = true;
18  }
19  if (mask & LogMask::Info) {
20  ss << (has_entry ? ", " : "") << "info";
21  has_entry = true;
22  }
23  if (mask & LogMask::Warning) {
24  ss << (has_entry ? ", " : "") << "warning";
25  has_entry = true;
26  }
27  if (mask & LogMask::Error) {
28  ss << (has_entry ? ", " : "") << "error";
29  has_entry = true;
30  }
31  return ss.str();
32 }
33 
34 // Parse a string as a timeout value with a unit.
35 //
36 // Example:
37 // 1s500ms
38 bool XrdOssStats::detail::ParseDuration(const std::string &duration, std::chrono::steady_clock::duration &result, std::string &errmsg) {
39 
40  if (duration.empty()) {
41  errmsg = "cannot parse empty string as a time duration";
42  return false;
43  }
44  if (duration == "0") {
45  result = std::chrono::steady_clock::duration(0);
46  return true;
47  }
48  std::chrono::steady_clock::duration dur(0);
49  auto strValue = duration;
50  while (!strValue.empty()) {
51  std::size_t pos;
52  double value;
53  try {
54  value = std::stod(strValue, &pos);
55  } catch (std::invalid_argument const &exc) {
56  errmsg = "Invalid number provided as timeout: " + strValue;
57  return false;
58  } catch (std::out_of_range const &exc) {
59  errmsg = "Provided timeout out of representable range: " + std::string(exc.what());
60  return false;
61  }
62  if (value < 0) {
63  errmsg = "Provided timeout was negative";
64  return false;
65  }
66  strValue = strValue.substr(pos);
67  char unit[3] = {'\0', '\0', '\0'};
68  if (!strValue.empty()) {
69  unit[0] = strValue[0];
70  if (unit[0] >= '0' && unit[0] <= '9') {unit[0] = '\0';}
71  }
72  if (strValue.size() > 1) {
73  unit[1] = strValue[1];
74  if (unit[1] >= '0' && unit[1] <= '9') {unit[1] = '\0';}
75  }
76  if (!strncmp(unit, "ns", 2)) {
77  dur += std::chrono::duration_cast<typeof(dur)>(std::chrono::duration<double, std::nano>(value));
78  } else if (!strncmp(unit, "us", 2)) {
79  dur += std::chrono::duration_cast<typeof(dur)>(std::chrono::duration<double, std::micro>(value));
80  } else if (!strncmp(unit, "ms", 2)) {
81  dur += std::chrono::duration_cast<typeof(dur)>(std::chrono::duration<double, std::milli>(value));
82  } else if (!strncmp(unit, "s", 1)) {
83  dur += std::chrono::duration_cast<typeof(dur)>(std::chrono::duration<double>(value));
84  } else if (!strncmp(unit, "m", 1)) {
85  dur += std::chrono::duration_cast<typeof(dur)>(std::chrono::duration<double, std::ratio<60>>(value));
86  } else if (!strncmp(unit, "h", 1)) {
87  dur += std::chrono::duration_cast<typeof(dur)>(std::chrono::duration<double, std::ratio<3600>>(value));
88  } else if (strlen(unit) > 0) {
89  errmsg = "Unknown unit in duration: " + std::string(unit);
90  return false;
91  } else {
92  errmsg = "Unit missing from duration: " + duration;
93  return false;
94  }
95  strValue = strValue.substr(strlen(unit));
96  }
97  result = dur;
98  return true;
99 }
100 
102 // The following functions export the plugin to the
103 // XRootD framework
104 
105 extern "C" {
106 
108  XrdSysLogger *logger,
109  const char *config_fn,
110  const char *parms,
111  XrdOucEnv *envP)
112 {
113 
114  XrdSysError log(logger, "fsstats_");
115  std::unique_ptr<XrdOssStats::FileSystem> new_oss(new XrdOssStats::FileSystem(curr_oss, logger, config_fn, envP));
116  if (!new_oss) {
117  return nullptr;
118  }
119  std::string errMsg;
120  if (!new_oss->InitSuccessful(errMsg)) {
121  if (errMsg.empty()) { // Initialization failure was non-fatal; just bypass this module.
122  return curr_oss;
123  } else {
124  log.Emsg("Initialize", "Encountered a fatal XrdOssStats initialization failure:", errMsg.c_str());
125  return nullptr;
126  }
127  }
128  return new_oss.release();
129 }
130 
132 
133 }
XrdOss * XrdOssAddStorageSystem2(XrdOss *curr_oss, XrdSysLogger *logger, const char *config_fn, const char *parms, XrdOucEnv *envP)
XrdVERSIONINFO(XrdOssAddStorageSystem2, fsstats)
bool Debug
@ Error
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95
bool ParseDuration(const std::string &duration, std::chrono::steady_clock::duration &result, std::string &errmsg)
std::string LogMaskToString(int mask)
XrdOucEnv * envP
Definition: XrdPss.cc:109