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) \
24  clock_gettime(CLOCK_MONOTONIC, &ts);
25 #endif
26 
27 
28 using namespace XrdAdaptor;
29 
31  : m_parent1(parent1), m_parent2(parent2)
32 {
33  // TODO: just assuming success.
35 }
36 
38 {
39  if (m_parent1 && m_parent2)
40  {
41  timespec stop;
42  GET_CLOCK_MONOTONIC(stop);
43 
44  int ms = 1000*(stop.tv_sec - m_start.tv_sec) + (stop.tv_nsec - m_start.tv_nsec)/1e6;
45  edm::LogVerbatim("XrdAdaptorInternal") << "Finished timer after " << ms << std::endl;
46  m_parent1->finishWatch(stop, ms);
47  m_parent2->finishWatch(stop, ms);
48  }
49 }
50 
52 {
53  m_parent1 = that.m_parent1;
54  m_parent2 = that.m_parent2;
55  m_start = that.m_start;
56  that.m_parent1 = nullptr;
57  that.m_parent2 = nullptr;
58  that.m_start = {0, 0};
59 }
60 
61 void
63 {
65  tmp = that.m_parent1;
66  that.m_parent1 = m_parent1;
67  m_parent1 = tmp;
68  tmp = that.m_parent2;
69  that.m_parent2 = m_parent2;
70  m_parent2 = tmp;
71  timespec tmp2;
72  tmp2 = that.m_start;
73  that.m_start = m_start;
74  m_start = tmp2;
75 }
76 
77 
78 QualityMetric::QualityMetric(timespec now, int default_value)
79  : m_value(default_value),
80  m_interval0_n(0),
81  m_interval0_val(-1),
82  m_interval0_start(now.tv_sec),
83  m_interval1_val(-1),
84  m_interval2_val(-1),
85  m_interval3_val(-1),
86  m_interval4_val(-1)
87 {
88 }
89 
90 void
91 QualityMetric::finishWatch(timespec stop, int ms)
92 {
93  std::unique_lock<std::mutex> sentry(m_mutex);
94 
95  m_value = -1;
96  if (stop.tv_sec > m_interval0_start+interval_length)
97  {
102  m_interval0_n = 1;
104  m_interval0_start = stop.tv_sec;
105  }
106  else
107  {
109  m_interval0_n++;
111  }
112 }
113 
114 unsigned
116 {
117  std::unique_lock<std::mutex> sentry(m_mutex);
118 
119  if (m_value == -1)
120  {
121  unsigned den = 0;
122  m_value = 0;
123  if (m_interval0_val >= 0)
124  {
125  den += 16;
127  }
128  if (m_interval1_val >= 0)
129  {
130  den += 8;
132  }
133  if (m_interval2_val >= 0)
134  {
135  den += 4;
137  }
138  if (m_interval3_val >= 0)
139  {
140  den += 2;
142  }
143  if (m_interval4_val >= 0)
144  {
145  den += 1;
147  }
148  if (den)
149  m_value /= den;
150  else
151  m_value = 260;
152  }
153  return m_value;
154 }
155 
156 
158 
159 
160 std::unique_ptr<QualityMetricSource>
162 {
163  auto itFound = m_instance.m_sources.find(id);
164  if (itFound == m_instance.m_sources.end())
165  {
166  // try to make a new one
167  std::unique_ptr<QualityMetricUniqueSource> source(new QualityMetricUniqueSource(now));
168  auto insertResult = m_instance.m_sources.insert(std::make_pair(id, source.get()));
169  itFound = insertResult.first;
170  if (insertResult.second)
171  { // Insert was successful; release our reference.
172  source.release();
173  } // Otherwise, we raced with a different thread and they won; we will delete our new QM source.
174  }
175  return itFound->second->newSource(now);
176 }
177 
178 
180  : QualityMetric(now, default_value),
181  m_parent(parent)
182 {}
183 
184 void
186 {
188  watch.swap(tmp);
189 }
190 
192  : QualityMetric(now)
193 {}
194 
195 std::unique_ptr<QualityMetricSource>
197 {
198  std::unique_ptr<QualityMetricSource> child(new QualityMetricSource(*this, now, get()));
199  return child;
200 }
201 
edm::propagate_const< QualityMetric * > m_parent2
Definition: QualityMetric.h:35
std::unique_ptr< QualityMetricSource > newSource(timespec now)
QualityMetric(timespec now, int default_value=260)
QualityMetricUniqueSource & m_parent
Definition: QualityMetric.h:90
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
susybsm::MuonSegment ms
Definition: classes.h:31
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
void startWatch(QualityMetricWatch &)
void swap(QualityMetricWatch &)
static const unsigned interval_length
Definition: QualityMetric.h:48
static std::string const source
Definition: EdmProvDump.cc:43
static QualityMetricFactory m_instance
Definition: QualityMetric.h:70
edm::propagate_const< QualityMetric * > m_parent1
Definition: QualityMetric.h:34