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 }
Log< level::Info, true > LogVerbatim
std::unique_ptr< QualityMetricSource > newSource(timespec now)
QualityMetric(timespec now, int default_value=260)
edm::propagate_const< QualityMetric * > m_parent1
Definition: QualityMetric.h:36
QualityMetricUniqueSource & m_parent
Definition: QualityMetric.h:92
void finishWatch(timespec now, int ms)
#define GET_CLOCK_MONOTONIC(ts)
static std::unique_ptr< QualityMetricSource > get(timespec now, const std::string &id)
QualityMetricSource(QualityMetricUniqueSource &parent, timespec now, int default_value)
#define CMS_THREAD_SAFE
void startWatch(QualityMetricWatch &)
void swap(QualityMetricWatch &)
edm::propagate_const< QualityMetric * > m_parent2
Definition: QualityMetric.h:37
tmp
align.sh
Definition: createJobs.py:716
static const unsigned interval_length
Definition: QualityMetric.h:53
static std::string const source
Definition: EdmProvDump.cc:46
static QualityMetricFactory m_instance
Definition: QualityMetric.h:73