CMS 3D CMS Logo

QualityMetric.cc
Go to the documentation of this file.
1 
2 #include <iostream>
3 
6 
7 #include "QualityMetric.h"
8 
9 #ifdef __MACH__
10 #include <mach/clock.h>
11 #include <mach/mach.h>
12 #define GET_CLOCK_MONOTONIC(ts) \
13  { \
14  clock_serv_t cclock; \
15  mach_timespec_t mts; \
16  host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); \
17  clock_get_time(cclock, &mts); \
18  mach_port_deallocate(mach_task_self(), cclock); \
19  ts.tv_sec = mts.tv_sec; \
20  ts.tv_nsec = mts.tv_nsec; \
21  }
22 #else
23 #define GET_CLOCK_MONOTONIC(ts) clock_gettime(CLOCK_MONOTONIC, &ts);
24 #endif
25 
26 using namespace XrdAdaptor;
27 
29  : m_parent1(parent1), m_parent2(parent2) {
30  // TODO: just assuming success.
32 }
33 
35  if (m_parent1 && m_parent2) {
36  timespec stop;
37  GET_CLOCK_MONOTONIC(stop);
38 
39  int ms = 1000 * (stop.tv_sec - m_start.tv_sec) + (stop.tv_nsec - m_start.tv_nsec) / 1e6;
40  edm::LogVerbatim("XrdAdaptorInternal") << "Finished timer after " << ms << std::endl;
41  m_parent1->finishWatch(stop, ms);
42  m_parent2->finishWatch(stop, ms);
43  }
44 }
45 
47  m_parent1 = that.m_parent1;
48  m_parent2 = that.m_parent2;
49  m_start = that.m_start;
50  that.m_parent1 = nullptr;
51  that.m_parent2 = nullptr;
52  that.m_start = {0, 0};
53 }
54 
57  tmp = that.m_parent1;
58  that.m_parent1 = m_parent1;
59  m_parent1 = tmp;
60  tmp = that.m_parent2;
61  that.m_parent2 = m_parent2;
62  m_parent2 = tmp;
63  timespec tmp2;
64  tmp2 = that.m_start;
65  that.m_start = m_start;
66  m_start = tmp2;
67 }
68 
69 QualityMetric::QualityMetric(timespec now, int default_value)
70  : m_value(default_value),
71  m_interval0_n(0),
72  m_interval0_val(-1),
73  m_interval0_start(now.tv_sec),
74  m_interval1_val(-1),
75  m_interval2_val(-1),
76  m_interval3_val(-1),
77  m_interval4_val(-1) {}
78 
79 void QualityMetric::finishWatch(timespec stop, int ms) {
80  std::unique_lock<std::mutex> sentry(m_mutex);
81 
82  m_value = -1;
83  if (stop.tv_sec > m_interval0_start + interval_length) {
88  m_interval0_n = 1;
89  m_interval0_val = ms;
90  m_interval0_start = stop.tv_sec;
91  } else {
92  int num = m_interval0_val * m_interval0_n + ms;
93  m_interval0_n++;
95  }
96 }
97 
98 unsigned QualityMetric::get() {
99  std::unique_lock<std::mutex> sentry(m_mutex);
100 
101  if (m_value == -1) {
102  unsigned den = 0;
103  m_value = 0;
104  if (m_interval0_val >= 0) {
105  den += 16;
106  m_value = 16 * m_interval0_val;
107  }
108  if (m_interval1_val >= 0) {
109  den += 8;
110  m_value += 8 * m_interval1_val;
111  }
112  if (m_interval2_val >= 0) {
113  den += 4;
114  m_value += 4 * m_interval2_val;
115  }
116  if (m_interval3_val >= 0) {
117  den += 2;
118  m_value += 2 * m_interval3_val;
119  }
120  if (m_interval4_val >= 0) {
121  den += 1;
123  }
124  if (den)
125  m_value /= den;
126  else
127  m_value = 260;
128  }
129  return m_value;
130 }
131 
133 
134 std::unique_ptr<QualityMetricSource> QualityMetricFactory::get(timespec now, const std::string &id) {
135  auto itFound = m_instance.m_sources.find(id);
136  if (itFound == m_instance.m_sources.end()) {
137  // try to make a new one
138  std::unique_ptr<QualityMetricUniqueSource> source(new QualityMetricUniqueSource(now));
139  auto insertResult = m_instance.m_sources.insert(std::make_pair(id, source.get()));
140  itFound = insertResult.first;
141  if (insertResult.second) { // Insert was successful; release our reference.
142  source.release();
143  } // Otherwise, we raced with a different thread and they won; we will delete our new QM source.
144  }
145  return itFound->second->newSource(now);
146 }
147 
149  : QualityMetric(now, default_value), m_parent(parent) {}
150 
153  watch.swap(tmp);
154 }
155 
157 
158 std::unique_ptr<QualityMetricSource> QualityMetricUniqueSource::newSource(timespec now) {
159  std::unique_ptr<QualityMetricSource> child(new QualityMetricSource(*this, now, get()));
160  return child;
161 }
GET_CLOCK_MONOTONIC
#define GET_CLOCK_MONOTONIC(ts)
Definition: QualityMetric.cc:23
MessageLogger.h
XrdAdaptor::QualityMetric::m_interval3_val
int m_interval3_val
Definition: QualityMetric.h:61
submitPVValidationJobs.now
now
Definition: submitPVValidationJobs.py:639
XrdAdaptor::QualityMetricWatch::QualityMetricWatch
QualityMetricWatch()
Definition: QualityMetric.h:24
XrdAdaptor::QualityMetric::m_interval1_val
int m_interval1_val
Definition: QualityMetric.h:59
XrdAdaptor::QualityMetric::m_interval0_val
int m_interval0_val
Definition: QualityMetric.h:57
XrdAdaptor::QualityMetric::get
unsigned get()
Definition: QualityMetric.cc:98
XrdAdaptor::QualityMetricUniqueSource::newSource
std::unique_ptr< QualityMetricSource > newSource(timespec now)
Definition: QualityMetric.cc:158
XrdAdaptor::QualityMetricSource
Definition: QualityMetric.h:83
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
XrdAdaptor::QualityMetricUniqueSource
Definition: QualityMetric.h:98
XrdAdaptor::QualityMetricSource::startWatch
void startWatch(QualityMetricWatch &)
Definition: QualityMetric.cc:151
XrdAdaptor::QualityMetric::interval_length
static const unsigned interval_length
Definition: QualityMetric.h:53
XrdAdaptor::QualityMetricSource::QualityMetricSource
QualityMetricSource(QualityMetricUniqueSource &parent, timespec now, int default_value)
Definition: QualityMetric.cc:148
XrdAdaptor::QualityMetricWatch
Definition: QualityMetric.h:20
XrdAdaptor::QualityMetricSource::m_parent
QualityMetricUniqueSource & m_parent
Definition: QualityMetric.h:92
XrdAdaptor::QualityMetric::QualityMetric
QualityMetric(timespec now, int default_value=260)
Definition: QualityMetric.cc:69
source
static const std::string source
Definition: EdmProvDump.cc:47
XrdAdaptor::QualityMetricWatch::m_parent1
edm::propagate_const< QualityMetric * > m_parent1
Definition: QualityMetric.h:36
XrdAdaptor::QualityMetricUniqueSource::QualityMetricUniqueSource
QualityMetricUniqueSource(timespec now)
Definition: QualityMetric.cc:156
CMS_THREAD_SAFE
#define CMS_THREAD_SAFE
Definition: thread_safety_macros.h:4
XrdAdaptor::QualityMetric::finishWatch
void finishWatch(timespec now, int ms)
Definition: QualityMetric.cc:79
XrdAdaptor::QualityMetricWatch::m_start
timespec m_start
Definition: QualityMetric.h:35
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
XrdAdaptor::QualityMetric::m_interval4_val
int m_interval4_val
Definition: QualityMetric.h:62
thread_safety_macros.h
XrdAdaptor::QualityMetric::m_interval0_n
int m_interval0_n
Definition: QualityMetric.h:56
XrdAdaptor::QualityMetricFactory::m_sources
MetricMap m_sources
Definition: QualityMetric.h:76
XrdAdaptor::QualityMetric::m_value
int m_value
Definition: QualityMetric.h:55
XrdAdaptor::QualityMetricWatch::swap
void swap(QualityMetricWatch &)
Definition: QualityMetric.cc:55
EgammaValidation_cff.num
num
Definition: EgammaValidation_cff.py:34
XrdAdaptor::QualityMetricWatch::~QualityMetricWatch
~QualityMetricWatch()
Definition: QualityMetric.cc:34
XrdAdaptor::QualityMetricFactory
Definition: QualityMetric.h:67
QualityMetric.h
XrdAdaptor::QualityMetricFactory::get
static std::unique_ptr< QualityMetricSource > get(timespec now, const std::string &id)
Definition: QualityMetric.cc:134
submitPVValidationJobs.child
child
Definition: submitPVValidationJobs.py:119
XrdAdaptor::QualityMetricWatch::m_parent2
edm::propagate_const< QualityMetric * > m_parent2
Definition: QualityMetric.h:37
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
XrdAdaptor::QualityMetric::m_interval2_val
int m_interval2_val
Definition: QualityMetric.h:60
XrdAdaptor::QualityMetric
Definition: QualityMetric.h:40
XrdAdaptor::QualityMetric::m_mutex
std::mutex m_mutex
Definition: QualityMetric.h:64
class-composition.parent
parent
Definition: class-composition.py:88
XrdAdaptor
Definition: QualityMetric.h:14
XrdAdaptor::QualityMetric::m_interval0_start
time_t m_interval0_start
Definition: QualityMetric.h:58
XrdAdaptor::QualityMetricFactory::m_instance
static QualityMetricFactory m_instance
Definition: QualityMetric.h:73