CMS 3D CMS Logo

XrdStatistics.cc
Go to the documentation of this file.
1 
8 
9 #include "XrdRequest.h"
10 #include "XrdStatistics.h"
11 
12 #include <chrono>
13 
14 using namespace edm::storage;
15 
16 namespace xrd_adaptor {
17  XrdStatistics::~XrdStatistics() {}
18 } // namespace xrd_adaptor
19 
20 using namespace XrdAdaptor;
21 
22 std::atomic<XrdSiteStatisticsInformation *> XrdSiteStatisticsInformation::m_instance;
23 
24 XrdStatisticsService::XrdStatisticsService(const edm::ParameterSet &iPS, edm::ActivityRegistry &iRegistry) {
25  XrdSiteStatisticsInformation::createInstance();
26 
27  if (iPS.getUntrackedParameter<bool>("reportToFJR", false)) {
28  iRegistry.watchPostEndJob(this, &XrdStatisticsService::postEndJob);
29  }
30 }
31 
32 void XrdStatisticsService::postEndJob() {
34  if (!reportSvc.isAvailable()) {
35  return;
36  }
37 
38  XrdSiteStatisticsInformation *instance = XrdSiteStatisticsInformation::getInstance();
39  if (!instance) {
40  return;
41  }
42 
43  std::map<std::string, std::string> props;
44  for (auto &stats : instance->m_sites) {
45  stats->recomputeProperties(props);
46  reportSvc->reportPerformanceForModule(stats->site(), "XrdSiteStatistics", props);
47  }
48 }
49 
50 std::vector<std::pair<std::string, XrdStatisticsService::CondorIOStats>> XrdStatisticsService::condorUpdate() {
51  std::vector<std::pair<std::string, XrdStatisticsService::CondorIOStats>> result;
52  XrdSiteStatisticsInformation *instance = XrdSiteStatisticsInformation::getInstance();
53  if (!instance) {
54  return result;
55  }
56 
57  std::lock_guard<std::mutex> lock(instance->m_mutex);
58  result.reserve(instance->m_sites.size());
59  for (auto &stats : instance->m_sites) {
61  std::shared_ptr<XrdSiteStatistics> ss = get_underlying_safe(stats);
62  if (!ss)
63  continue;
64  cs.bytesRead = ss->getTotalBytes();
65  cs.transferTime = ss->getTotalReadTime();
66  result.emplace_back(ss->site(), cs);
67  }
68  return result;
69 }
70 
71 std::shared_ptr<XrdSiteStatistics> XrdSiteStatisticsInformation::getStatisticsForSite(std::string const &site) {
72  std::lock_guard<std::mutex> lock(m_mutex);
73  for (auto &stats : m_sites) {
74  if (stats->site() == site) {
75  return get_underlying_safe(stats);
76  }
77  }
78  m_sites.emplace_back(new XrdSiteStatistics(site));
79  return get_underlying_safe(m_sites.back());
80 }
81 
82 void XrdSiteStatisticsInformation::createInstance() {
83  if (!m_instance) {
84  std::unique_ptr<XrdSiteStatisticsInformation> tmp{new XrdSiteStatisticsInformation()};
85  XrdSiteStatisticsInformation *expected = nullptr;
86  if (m_instance.compare_exchange_strong(expected, tmp.get())) {
87  tmp.release();
88  }
89  }
90 }
91 
92 XrdSiteStatisticsInformation *XrdSiteStatisticsInformation::getInstance() {
93  return m_instance.load(std::memory_order_relaxed);
94 }
95 
98  desc.setComment("Report Xrootd-related statistics centrally.");
99  desc.addUntracked<bool>("reportToFJR", true)
100  ->setComment(
101  "True: Add per-site Xrootd statistics to the framework job report.\n"
102  "False: Collect no site-specific statistics.\n");
103  descriptions.add("XrdStatisticsService", desc);
104 }
105 
106 XrdSiteStatistics::XrdSiteStatistics(std::string const &site)
107  : m_site(site),
108  m_readvCount(0),
109  m_chunkCount(0),
110  m_readvSize(0),
111  m_readvNS(0.0),
112  m_readCount(0),
113  m_readSize(0),
114  m_readNS(0) {}
115 
116 std::shared_ptr<XrdReadStatistics> XrdSiteStatistics::startRead(std::shared_ptr<XrdSiteStatistics> parent,
117  std::shared_ptr<ClientRequest> req) {
118  std::shared_ptr<XrdReadStatistics> readStats(new XrdReadStatistics(parent, req->getSize(), req->getCount()));
119  return readStats;
120 }
121 
122 static std::string i2str(int input) {
123  std::ostringstream formatter;
124  formatter << input;
125  return formatter.str();
126 }
127 
128 static std::string d2str(double input) {
129  std::ostringstream formatter;
130  formatter << std::setw(4) << input;
131  return formatter.str();
132 }
133 
134 void XrdSiteStatistics::recomputeProperties(std::map<std::string, std::string> &props) {
135  props.clear();
136 
137  props["readv-numOperations"] = i2str(m_readvCount);
138  props["readv-numChunks"] = i2str(m_chunkCount);
139  props["readv-totalMegabytes"] = d2str(static_cast<float>(m_readvSize) / (1024.0 * 1024.0));
140  props["readv-totalMsecs"] = d2str(m_readvNS / 1e6);
141 
142  props["read-numOperations"] = i2str(m_readCount);
143  props["read-totalMegabytes"] = d2str(static_cast<float>(m_readSize) / (1024.0 * 1024.0));
144  props["read-totalMsecs"] = d2str(static_cast<float>(m_readNS) / 1e6);
145 }
146 
148  if (readStats.readCount() > 1) {
149  m_readvCount++;
150  m_chunkCount += readStats.readCount();
151  m_readvSize += readStats.size();
152  m_readvNS += readStats.elapsedNS();
153  } else {
154  m_readCount++;
155  m_readSize += readStats.size();
156  m_readNS += readStats.elapsedNS();
157  }
158 }
159 
160 XrdReadStatistics::XrdReadStatistics(std::shared_ptr<XrdSiteStatistics> parent, IOSize size, size_t count)
161  : m_size(size), m_count(count), m_parent(parent), m_start(std::chrono::steady_clock::now()) {}
162 
164  std::chrono::time_point<std::chrono::steady_clock> end = std::chrono::steady_clock::now();
165  return std::chrono::duration_cast<std::chrono::nanoseconds>(end - m_start).count();
166 }
size
Write out results.
XrdReadStatistics(const XrdReadStatistics &)=delete
boost::date_time::subsecond_duration< boost::posix_time::time_duration, 1000000000 > nanoseconds
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
std::atomic< unsigned > m_readCount
Definition: XrdStatistics.h:98
static PFTauRenderPlugin instance
void finishRead(XrdReadStatistics const &)
constexpr std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
std::atomic< uint64_t > m_readvSize
Definition: XrdStatistics.h:96
static std::string d2str(double input)
static std::string const input
Definition: EdmProvDump.cc:50
void reportPerformanceForModule(std::string const &metricClass, std::string const &moduleName, std::map< std::string, std::string > const &metrics)
Definition: JobReport.cc:722
T getUntrackedParameter(std::string const &, T const &) const
void recomputeProperties(std::map< std::string, std::string > &props)
std::chrono::time_point< std::chrono::steady_clock > m_start
static std::shared_ptr< XrdReadStatistics > startRead(std::shared_ptr< XrdSiteStatistics > parent, std::shared_ptr< ClientRequest > req)
std::atomic< unsigned > m_readvCount
Definition: XrdStatistics.h:94
std::atomic< uint64_t > m_readvNS
Definition: XrdStatistics.h:97
std::atomic< uint64_t > m_readNS
size_t IOSize
Definition: IOTypes.h:15
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
unsigned long long uint64_t
Definition: Time.h:13
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static std::string i2str(int input)
std::atomic< uint64_t > m_readSize
Definition: XrdStatistics.h:99
bool isAvailable() const
Definition: Service.h:40
tmp
align.sh
Definition: createJobs.py:716
std::atomic< unsigned > m_chunkCount
Definition: XrdStatistics.h:95