Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:53

0001 #ifndef Utilities_XrdAdaptor_QualityMetric_h
0002 #define Utilities_XrdAdaptor_QualityMetric_h
0003 
0004 #include <ctime>
0005 
0006 #include <mutex>
0007 #include <memory>
0008 
0009 #include "oneapi/tbb/concurrent_unordered_map.h"
0010 
0011 #include "FWCore/Utilities/interface/propagate_const.h"
0012 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0013 
0014 namespace XrdAdaptor {
0015 
0016   class QualityMetric;
0017   class QualityMetricSource;
0018   class QualityMetricUniqueSource;
0019 
0020   class QualityMetricWatch {
0021     friend class QualityMetricSource;
0022 
0023   public:
0024     QualityMetricWatch() : m_parent1(nullptr), m_parent2(nullptr) {}
0025     QualityMetricWatch(QualityMetricWatch &&);
0026     ~QualityMetricWatch();
0027 
0028     QualityMetricWatch(const QualityMetricWatch &) = delete;
0029     QualityMetricWatch &operator=(const QualityMetricWatch &) = delete;
0030 
0031     void swap(QualityMetricWatch &);
0032 
0033   private:
0034     QualityMetricWatch(QualityMetric *parent1, QualityMetric *parent2);
0035     timespec m_start;
0036     edm::propagate_const<QualityMetric *> m_parent1;
0037     edm::propagate_const<QualityMetric *> m_parent2;
0038   };
0039 
0040   class QualityMetric {
0041     friend class QualityMetricWatch;
0042 
0043   public:
0044     QualityMetric(timespec now, int default_value = 260);
0045     unsigned get();
0046 
0047     QualityMetric(const QualityMetric &) = delete;
0048     QualityMetric &operator=(const QualityMetric &) = delete;
0049 
0050   private:
0051     void finishWatch(timespec now, int ms);
0052 
0053     static const unsigned interval_length = 60;
0054 
0055     int m_value;
0056     int m_interval0_n;
0057     int m_interval0_val;
0058     time_t m_interval0_start;
0059     int m_interval1_val;
0060     int m_interval2_val;
0061     int m_interval3_val;
0062     int m_interval4_val;
0063 
0064     std::mutex m_mutex;
0065   };
0066 
0067   class QualityMetricFactory {
0068     friend class Source;
0069 
0070   private:
0071     static std::unique_ptr<QualityMetricSource> get(timespec now, const std::string &id);
0072 
0073     CMS_THREAD_SAFE static QualityMetricFactory m_instance;
0074 
0075     typedef oneapi::tbb::concurrent_unordered_map<std::string, QualityMetricUniqueSource *> MetricMap;
0076     MetricMap m_sources;
0077   };
0078 
0079   /**
0080  * This QM implementation is meant to be held by each XrdAdaptor::Source
0081  * instance
0082  */
0083   class QualityMetricSource final : public QualityMetric {
0084     friend class QualityMetricUniqueSource;
0085 
0086   public:
0087     void startWatch(QualityMetricWatch &);
0088 
0089   private:
0090     QualityMetricSource(QualityMetricUniqueSource &parent, timespec now, int default_value);
0091 
0092     QualityMetricUniqueSource &m_parent;
0093   };
0094 
0095   /*
0096  * This quality metric tracks all accesses to a given source ID.
0097  */
0098   class QualityMetricUniqueSource final : public QualityMetric {
0099     friend class QualityMetricFactory;
0100 
0101   private:
0102     QualityMetricUniqueSource(timespec now);
0103     std::unique_ptr<QualityMetricSource> newSource(timespec now);
0104   };
0105 
0106 }  // namespace XrdAdaptor
0107 
0108 #endif  // Utilities_XrdAdaptor_QualityMetric_h