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