Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 10:06:00

0001 #ifndef __XRD_STATISTICS_SERVICE_H_
0002 #define __XRD_STATISTICS_SERVICE_H_
0003 
0004 #include "Utilities/StorageFactory/interface/IOTypes.h"
0005 #include "Utilities/XrdAdaptor/interface/XrdStatistics.h"
0006 #include "FWCore/Utilities/interface/propagate_const.h"
0007 
0008 #include <atomic>
0009 #include <chrono>
0010 #include <map>
0011 #include <memory>
0012 #include <mutex>
0013 #include <vector>
0014 
0015 namespace edm {
0016   class ParameterSet;
0017   class ActivityRegistry;
0018   class ConfigurationDescriptions;
0019 
0020   namespace service {
0021     class CondorStatusService;
0022   }
0023 }  // namespace edm
0024 
0025 namespace XrdAdaptor {
0026 
0027   class ClientRequest;
0028   class XrdReadStatistics;
0029   class XrdSiteStatistics;
0030 
0031   /* NOTE: All member information is kept in the XrdSiteStatisticsInformation singleton,
0032  * _not_ within the service itself.  This is because we need to be able to use the
0033  * singleton on non-CMSSW-created threads.  Services are only available to threads
0034  * created by CMSSW.
0035  */
0036   class XrdStatisticsService : public xrd_adaptor::XrdStatistics {
0037   public:
0038     XrdStatisticsService(const edm::ParameterSet &iPS, edm::ActivityRegistry &iRegistry);
0039 
0040     void postEndJob();
0041 
0042     static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
0043 
0044     // Provide an update of per-site transfer statistics to the CondorStatusService.
0045     // Returns a mapping of "site name" to transfer statistics.  The "site name" is
0046     // as self-identified by the Xrootd host; may not necessarily match up with the
0047     // "CMS site name".
0048     std::vector<std::pair<std::string, CondorIOStats>> condorUpdate() final;
0049   };
0050 
0051   class XrdSiteStatisticsInformation {
0052     friend class XrdStatisticsService;
0053 
0054   public:
0055     static XrdSiteStatisticsInformation *getInstance();
0056 
0057     std::shared_ptr<XrdSiteStatistics> getStatisticsForSite(std::string const &site);
0058 
0059   private:
0060     static void createInstance();
0061 
0062     static std::atomic<XrdSiteStatisticsInformation *> m_instance;
0063     std::mutex m_mutex;
0064     std::vector<edm::propagate_const<std::shared_ptr<XrdSiteStatistics>>> m_sites;
0065   };
0066 
0067   class XrdSiteStatistics {
0068     friend class XrdReadStatistics;
0069 
0070   public:
0071     XrdSiteStatistics(std::string const &site);
0072     XrdSiteStatistics(const XrdSiteStatistics &) = delete;
0073     XrdSiteStatistics &operator=(const XrdSiteStatistics &) = delete;
0074 
0075     std::string const &site() const { return m_site; }
0076 
0077     // Note that, while this function is thread-safe, the numbers are only consistent if no other
0078     // thread is reading data.
0079     void recomputeProperties(std::map<std::string, std::string> &props);
0080 
0081     static std::shared_ptr<XrdReadStatistics> startRead(std::shared_ptr<XrdSiteStatistics> parent,
0082                                                         std::shared_ptr<ClientRequest> req);
0083 
0084     void finishRead(XrdReadStatistics const &);
0085 
0086     uint64_t getTotalBytes() const { return m_readvSize + m_readSize; }
0087     std::chrono::nanoseconds getTotalReadTime() {
0088       return std::chrono::nanoseconds(m_readvNS) + std::chrono::nanoseconds(m_readNS);
0089     }
0090 
0091   private:
0092     const std::string m_site = "Unknown";
0093 
0094     std::atomic<unsigned> m_readvCount;
0095     std::atomic<unsigned> m_chunkCount;
0096     std::atomic<uint64_t> m_readvSize;
0097     std::atomic<uint64_t> m_readvNS;
0098     std::atomic<unsigned> m_readCount;
0099     std::atomic<uint64_t> m_readSize;
0100     std::atomic<uint64_t> m_readNS;
0101   };
0102 
0103   class XrdReadStatistics {
0104     friend class XrdSiteStatistics;
0105 
0106   public:
0107     ~XrdReadStatistics() { m_parent->finishRead(*this); }
0108     XrdReadStatistics(const XrdReadStatistics &) = delete;
0109     XrdReadStatistics &operator=(const XrdReadStatistics &) = delete;
0110 
0111   private:
0112     XrdReadStatistics(std::shared_ptr<XrdSiteStatistics> parent, edm::storage::IOSize size, size_t count);
0113 
0114     uint64_t elapsedNS() const;
0115     int readCount() const { return m_count; }
0116     int size() const { return m_size; }
0117 
0118     size_t m_size;
0119     edm::storage::IOSize m_count;
0120     edm::propagate_const<std::shared_ptr<XrdSiteStatistics>> m_parent;
0121     std::chrono::time_point<std::chrono::steady_clock> m_start;
0122   };
0123 
0124 }  // namespace XrdAdaptor
0125 
0126 #endif