CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
QualityMetric.cc
Go to the documentation of this file.
1 
2 #include <iostream>
3 
5 
6 #include "QualityMetric.h"
7 
8 #ifdef __MACH__
9 #include <mach/clock.h>
10 #include <mach/mach.h>
11 #define GET_CLOCK_MONOTONIC(ts) \
12 { \
13  clock_serv_t cclock; \
14  mach_timespec_t mts; \
15  host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); \
16  clock_get_time(cclock, &mts); \
17  mach_port_deallocate(mach_task_self(), cclock); \
18  ts.tv_sec = mts.tv_sec; \
19  ts.tv_nsec = mts.tv_nsec; \
20 }
21 #else
22 #define GET_CLOCK_MONOTONIC(ts) \
23  clock_gettime(CLOCK_MONOTONIC, &ts);
24 #endif
25 
26 
27 using namespace XrdAdaptor;
28 
30  : m_parent1(parent1), m_parent2(parent2)
31 {
32  // TODO: just assuming success.
34 }
35 
37 {
38  if (m_parent1 && m_parent2)
39  {
40  timespec stop;
41  GET_CLOCK_MONOTONIC(stop);
42 
43  int ms = 1000*(stop.tv_sec - m_start.tv_sec) + (stop.tv_nsec - m_start.tv_nsec)/1e6;
44  edm::LogVerbatim("XrdAdaptorInternal") << "Finished timer after " << ms << std::endl;
45  m_parent1->finishWatch(stop, ms);
46  m_parent2->finishWatch(stop, ms);
47  }
48 }
49 
51 {
52  m_parent1 = that.m_parent1;
53  m_parent2 = that.m_parent2;
54  m_start = that.m_start;
55  that.m_parent1 = nullptr;
56  that.m_parent2 = nullptr;
57  that.m_start = {0, 0};
58 }
59 
60 void
62 {
64  tmp = that.m_parent1;
65  that.m_parent1 = m_parent1;
66  m_parent1 = tmp;
67  tmp = that.m_parent2;
68  that.m_parent2 = m_parent2;
69  m_parent2 = tmp;
70  timespec tmp2;
71  tmp2 = that.m_start;
72  that.m_start = m_start;
73  m_start = tmp2;
74 }
75 
76 
77 QualityMetric::QualityMetric(timespec now, int default_value)
78  : m_value(default_value),
79  m_interval0_n(0),
80  m_interval0_val(-1),
81  m_interval0_start(now.tv_sec),
82  m_interval1_val(-1),
83  m_interval2_val(-1),
84  m_interval3_val(-1),
85  m_interval4_val(-1)
86 {
87 }
88 
89 void
90 QualityMetric::finishWatch(timespec stop, int ms)
91 {
92  std::unique_lock<std::mutex> sentry(m_mutex);
93 
94  m_value = -1;
95  if (stop.tv_sec > m_interval0_start+interval_length)
96  {
101  m_interval0_n = 1;
103  m_interval0_start = stop.tv_sec;
104  }
105  else
106  {
108  m_interval0_n++;
110  }
111 }
112 
113 unsigned
115 {
116  std::unique_lock<std::mutex> sentry(m_mutex);
117 
118  if (m_value == -1)
119  {
120  unsigned den = 0;
121  m_value = 0;
122  if (m_interval0_val >= 0)
123  {
124  den += 16;
126  }
127  if (m_interval1_val >= 0)
128  {
129  den += 8;
131  }
132  if (m_interval2_val >= 0)
133  {
134  den += 4;
136  }
137  if (m_interval3_val >= 0)
138  {
139  den += 2;
141  }
142  if (m_interval4_val >= 0)
143  {
144  den += 1;
146  }
147  if (den)
148  m_value /= den;
149  else
150  m_value = 260;
151  }
152  return m_value;
153 }
154 
155 
157 
158 
159 std::unique_ptr<QualityMetricSource>
161 {
162  auto itFound = m_instance.m_sources.find(id);
163  if (itFound == m_instance.m_sources.end())
164  {
165  // try to make a new one
166  std::unique_ptr<QualityMetricUniqueSource> source(new QualityMetricUniqueSource(now));
167  auto insertResult = m_instance.m_sources.insert(std::make_pair(id, source.get()));
168  itFound = insertResult.first;
169  if (insertResult.second)
170  { // Insert was successful; release our reference.
171  source.release();
172  } // Otherwise, we raced with a different thread and they won; we will delete our new QM source.
173  }
174  return itFound->second->newSource(now);
175 }
176 
177 
179  : QualityMetric(now, default_value),
180  m_parent(parent)
181 {}
182 
183 void
185 {
187  watch.swap(tmp);
188 }
189 
191  : QualityMetric(now)
192 {}
193 
194 std::unique_ptr<QualityMetricSource>
196 {
197  std::unique_ptr<QualityMetricSource> child(new QualityMetricSource(*this, now, get()));
198  return child;
199 }
200 
edm::propagate_const< QualityMetric * > m_parent2
Definition: QualityMetric.h:34
std::unique_ptr< QualityMetricSource > newSource(timespec now)
QualityMetric(timespec now, int default_value=260)
QualityMetricUniqueSource & m_parent
Definition: QualityMetric.h:89
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)
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:47
static std::string const source
Definition: EdmProvDump.cc:43
static QualityMetricFactory m_instance
Definition: QualityMetric.h:69
edm::propagate_const< QualityMetric * > m_parent1
Definition: QualityMetric.h:33