Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-01 22:41:15

0001 
0002 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0004 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0005 #include "FWCore/MessageLogger/interface/JobReport.h"
0006 #include "FWCore/ServiceRegistry/interface/Service.h"
0007 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
0008 
0009 #include "XrdRequest.h"
0010 #include "XrdStatistics.h"
0011 
0012 #include <chrono>
0013 
0014 using namespace edm::storage;
0015 
0016 namespace xrd_adaptor {
0017   XrdStatistics::~XrdStatistics() {}
0018 }  // namespace xrd_adaptor
0019 
0020 using namespace XrdAdaptor;
0021 
0022 std::atomic<XrdSiteStatisticsInformation *> XrdSiteStatisticsInformation::m_instance;
0023 
0024 XrdStatisticsService::XrdStatisticsService(const edm::ParameterSet &iPS, edm::ActivityRegistry &iRegistry) {
0025   XrdSiteStatisticsInformation::createInstance();
0026 
0027   if (iPS.getUntrackedParameter<bool>("reportToFJR", false)) {
0028     iRegistry.watchPostEndJob(this, &XrdStatisticsService::postEndJob);
0029   }
0030 }
0031 
0032 void XrdStatisticsService::postEndJob() {
0033   edm::Service<edm::JobReport> reportSvc;
0034   if (!reportSvc.isAvailable()) {
0035     return;
0036   }
0037 
0038   XrdSiteStatisticsInformation *instance = XrdSiteStatisticsInformation::getInstance();
0039   if (!instance) {
0040     return;
0041   }
0042 
0043   std::map<std::string, std::string> props;
0044   for (auto &stats : instance->m_sites) {
0045     stats->recomputeProperties(props);
0046     reportSvc->reportPerformanceForModule(stats->site(), "XrdSiteStatistics", props);
0047   }
0048 }
0049 
0050 std::vector<std::pair<std::string, XrdStatisticsService::CondorIOStats>> XrdStatisticsService::condorUpdate() {
0051   std::vector<std::pair<std::string, XrdStatisticsService::CondorIOStats>> result;
0052   XrdSiteStatisticsInformation *instance = XrdSiteStatisticsInformation::getInstance();
0053   if (!instance) {
0054     return result;
0055   }
0056 
0057   std::lock_guard<std::mutex> lock(instance->m_mutex);
0058   result.reserve(instance->m_sites.size());
0059   for (auto &stats : instance->m_sites) {
0060     CondorIOStats cs;
0061     std::shared_ptr<XrdSiteStatistics> ss = get_underlying_safe(stats);
0062     if (!ss)
0063       continue;
0064     cs.bytesRead = ss->getTotalBytes();
0065     cs.transferTime = ss->getTotalReadTime();
0066     result.emplace_back(ss->site(), cs);
0067   }
0068   return result;
0069 }
0070 
0071 std::shared_ptr<XrdSiteStatistics> XrdSiteStatisticsInformation::getStatisticsForSite(std::string const &site) {
0072   std::lock_guard<std::mutex> lock(m_mutex);
0073   for (auto &stats : m_sites) {
0074     if (stats->site() == site) {
0075       return get_underlying_safe(stats);
0076     }
0077   }
0078   m_sites.emplace_back(new XrdSiteStatistics(site));
0079   return get_underlying_safe(m_sites.back());
0080 }
0081 
0082 void XrdSiteStatisticsInformation::createInstance() {
0083   if (!m_instance) {
0084     std::unique_ptr<XrdSiteStatisticsInformation> tmp{new XrdSiteStatisticsInformation()};
0085     XrdSiteStatisticsInformation *expected = nullptr;
0086     if (m_instance.compare_exchange_strong(expected, tmp.get())) {
0087       tmp.release();
0088     }
0089   }
0090 }
0091 
0092 XrdSiteStatisticsInformation *XrdSiteStatisticsInformation::getInstance() {
0093   return m_instance.load(std::memory_order_relaxed);
0094 }
0095 
0096 void XrdStatisticsService::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0097   edm::ParameterSetDescription desc;
0098   desc.setComment("Report Xrootd-related statistics centrally.");
0099   desc.addUntracked<bool>("reportToFJR", true)
0100       ->setComment(
0101           "True: Add per-site Xrootd statistics to the framework job report.\n"
0102           "False: Collect no site-specific statistics.\n");
0103   descriptions.add("XrdStatisticsService", desc);
0104 }
0105 
0106 XrdSiteStatistics::XrdSiteStatistics(std::string const &site)
0107     : m_site(site),
0108       m_readvCount(0),
0109       m_chunkCount(0),
0110       m_readvSize(0),
0111       m_readvNS(0.0),
0112       m_readCount(0),
0113       m_readSize(0),
0114       m_readNS(0) {}
0115 
0116 std::shared_ptr<XrdReadStatistics> XrdSiteStatistics::startRead(std::shared_ptr<XrdSiteStatistics> parent,
0117                                                                 std::shared_ptr<ClientRequest> req) {
0118   std::shared_ptr<XrdReadStatistics> readStats(new XrdReadStatistics(parent, req->getSize(), req->getCount()));
0119   return readStats;
0120 }
0121 
0122 static std::string i2str(int input) {
0123   std::ostringstream formatter;
0124   formatter << input;
0125   return formatter.str();
0126 }
0127 
0128 static std::string d2str(double input) {
0129   std::ostringstream formatter;
0130   formatter << std::setw(4) << input;
0131   return formatter.str();
0132 }
0133 
0134 void XrdSiteStatistics::recomputeProperties(std::map<std::string, std::string> &props) {
0135   props.clear();
0136 
0137   props["readv-numOperations"] = i2str(m_readvCount);
0138   props["readv-numChunks"] = i2str(m_chunkCount);
0139   props["readv-totalMegabytes"] = d2str(static_cast<float>(m_readvSize) / (1024.0 * 1024.0));
0140   props["readv-totalMsecs"] = d2str(m_readvNS / 1e6);
0141 
0142   props["read-numOperations"] = i2str(m_readCount);
0143   props["read-totalMegabytes"] = d2str(static_cast<float>(m_readSize) / (1024.0 * 1024.0));
0144   props["read-totalMsecs"] = d2str(static_cast<float>(m_readNS) / 1e6);
0145 }
0146 
0147 void XrdSiteStatistics::finishRead(XrdReadStatistics const &readStats) {
0148   if (readStats.readCount() > 1) {
0149     m_readvCount++;
0150     m_chunkCount += readStats.readCount();
0151     m_readvSize += readStats.size();
0152     m_readvNS += readStats.elapsedNS();
0153   } else {
0154     m_readCount++;
0155     m_readSize += readStats.size();
0156     m_readNS += readStats.elapsedNS();
0157   }
0158 }
0159 
0160 XrdReadStatistics::XrdReadStatistics(std::shared_ptr<XrdSiteStatistics> parent, IOSize size, size_t count)
0161     : m_size(size), m_count(count), m_parent(parent), m_start(std::chrono::high_resolution_clock::now()) {}
0162 
0163 uint64_t XrdReadStatistics::elapsedNS() const {
0164   std::chrono::time_point<std::chrono::high_resolution_clock> end = std::chrono::high_resolution_clock::now();
0165   return std::chrono::duration_cast<std::chrono::nanoseconds>(end - m_start).count();
0166 }