Skip to content

Commit 231c775

Browse files
authored
Remove mutex from Metric class (#95)
1 parent 4d81470 commit 231c775

File tree

11 files changed

+122
-132
lines changed

11 files changed

+122
-132
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ set(INCLUDE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/include")
6767
set(SRCS
6868
src/Monitoring.cxx
6969
src/Metric.cxx
70+
src/ComplexMetric.cxx
7071
src/Backends/InfluxDB.cxx
7172
src/Backends/Flume.cxx
7273
src/Backends/StdOut.cxx
@@ -145,9 +146,8 @@ set(EXAMPLES
145146
examples/6-Increment.cxx
146147
examples/7-Latency.cxx
147148
examples/8-Multiple.cxx
148-
examples/9-Timer.cxx
149+
examples/9-AutoUpdate.cxx
149150
examples/10-Buffering.cxx
150-
examples/11-AutoUpdate.cxx
151151
)
152152

153153
foreach (example ${EXAMPLES})

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ Each backend has its default verbosity (see backend in [Monitoring instance](#mo
113113
### Customized metrics
114114
Two additional methods can be chained the to `send(Metric&& metric)` in order to __insert custom tags__ or __set custom timestamp__:
115115
+ `addTags(std::vector<Tag>&& tags)`
116-
+ `setTimestamp(std::chrono::time_point<std::chrono::system_clock>& timestamp)`
117116
118117
See how it works in the example: [examples/2-TaggedMetrics.cxx](examples/2-TaggedMetrics.cxx), [examples/3-UserDefinedTimestamp.cxx](examples/3-UserDefinedTimestamp.cxx).
119118
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ using namespace o2::monitoring;
1010
int main() {
1111
auto monitoring = MonitoringFactory::Get("stdout://");
1212

13-
// Enable periodical value pushing (default every 1s)
14-
monitoring->enableAutoPush();
15-
1613
// Get reference to metrics
1714
auto& qcMetric = monitoring->getAutoPushMetric("qcMetric");
1815
auto& qcMetric2 = monitoring->getAutoPushMetric("qcMetric2");

examples/9-Timer.cxx

Lines changed: 0 additions & 21 deletions
This file was deleted.

include/Monitoring/ComplexMetric.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
///
2+
/// \file ComplexMetric.h
3+
/// \author Adam Wegrzynek <[email protected]>
4+
///
5+
6+
#ifndef ALICEO2_MONITORING_CORE_COMPLEXMETRIC_H
7+
#define ALICEO2_MONITORING_CORE_COMPLEXMETRIC_H
8+
9+
#include "Metric.h"
10+
#include <mutex>
11+
12+
namespace o2
13+
{
14+
/// ALICE O2 Monitoring system
15+
namespace monitoring
16+
{
17+
18+
// \brief Extends metric to value setter
19+
class ComplexMetric : public o2::monitoring::Metric
20+
{
21+
public:
22+
/// Integer metric construtor
23+
/// \param value metric value (int)
24+
/// \param name metric name
25+
ComplexMetric(int value, const std::string& name);
26+
27+
/// String metric construtor
28+
/// \param value metric value (string)
29+
/// \param name the metric name
30+
ComplexMetric(std::string value, const std::string& name);
31+
32+
/// Double metric constructor
33+
/// \param value metric value (double)
34+
/// \param name metric name
35+
ComplexMetric(double value, const std::string& name);
36+
37+
/// uint64_t metric constructor
38+
/// \param value metric value (uint64_t)
39+
/// \param name metric name
40+
ComplexMetric(uint64_t value, const std::string& name);
41+
42+
/// boost variant metric constructor, required by derived metrics logic
43+
/// \param value metric value (boost variant)
44+
/// \param name metric name
45+
ComplexMetric(boost::variant< int, std::string, double, uint64_t >, const std::string& name);
46+
47+
/// Default destructor
48+
~ComplexMetric() = default;
49+
50+
// Resets metric's timestamp
51+
void resetTimestamp();
52+
53+
/// Assign operator overload, assignes new values to the metric object
54+
ComplexMetric& operator=(const boost::variant< int, std::string, double, uint64_t >& value);
55+
};
56+
57+
} // namespace monitoring
58+
} // namespace o2
59+
60+
#endif // ALICEO2_MONITORING_CORE_COMPLEXMETRIC_H

include/Monitoring/Metric.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include <string>
1010
#include <chrono>
11-
#include <mutex>
1211
#include <vector>
1312
#include <boost/variant.hpp>
1413
#include "Tag.h"
@@ -58,15 +57,6 @@ class Metric
5857
/// Default destructor
5958
~Metric() = default;
6059

61-
/// Copy initialization
62-
Metric(const Metric& other);
63-
64-
/// Copy assignment
65-
Metric& operator=(Metric const& other);
66-
67-
/// Assign operator overload, assignes new values to the metric object
68-
Metric& operator=(const boost::variant< int, std::string, double, uint64_t >& value);
69-
7060
/// Name getter
7161
/// \return metric name
7262
std::string getName() const;
@@ -96,7 +86,7 @@ class Metric
9686
/// return timestamp as std::chrono::system_clock
9787
static auto getCurrentTimestamp() -> decltype(std::chrono::system_clock::now());
9888

99-
private:
89+
protected:
10090
/// Metric value
10191
boost::variant< int, std::string, double, uint64_t > mValue;
10292

@@ -108,9 +98,6 @@ class Metric
10898

10999
/// Metric tags
110100
std::vector<Tag> tagSet;
111-
112-
/// Mutex for accesing metric value
113-
mutable std::mutex mValueMutex;
114101
};
115102

116103
} // namespace monitoring

include/Monitoring/Monitoring.h

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <vector>
1818
#include <deque>
1919

20+
#include "Monitoring/ComplexMetric.h"
2021
#include "Monitoring/Backend.h"
2122
#include "Monitoring/DerivedMetrics.h"
2223
#include "Monitoring/ProcessMonitor.h"
@@ -76,16 +77,6 @@ class Monitoring
7677
/// \param interval refresh interval
7778
void enableProcessMonitoring(const unsigned int interval = 5);
7879

79-
/// Starts timing
80-
/// Sets a start timestamp and timeout
81-
/// \param name metric name
82-
void startTimer(std::string name);
83-
84-
/// Stops timing
85-
/// Sets stop timestamp, calculates delta and sends value
86-
/// \param name metric name
87-
void stopAndSendTimer(std::string name);
88-
8980
/// Flushes metric buffer (this can also happen when buffer is full)
9081
void flushBuffer();
9182

@@ -101,11 +92,7 @@ class Monitoring
10192
/// Returns a metric which will be periodically sent to backends
10293
/// \param name metric name
10394
/// \return periodically send metric
104-
Metric& getAutoPushMetric(std::string name);
105-
106-
/// Enables periodical push interval
107-
/// \param interval interval in seconds
108-
void enableAutoPush(const unsigned int interval = 1);
95+
ComplexMetric& getAutoPushMetric(std::string name, unsigned int interval = 1);
10996

11097
private:
11198
/// Derived metrics handler
@@ -115,9 +102,6 @@ class Monitoring
115102
/// Vector of backends (where metrics are passed to)
116103
std::vector <std::unique_ptr<Backend>> mBackends;
117104

118-
/// List of timers
119-
std::unordered_map <std::string, std::chrono::time_point<std::chrono::steady_clock>> mTimers;
120-
121105
/// Pushes metric to all backends or to the buffer
122106
void pushToBackends(Metric&& metric);
123107

@@ -143,7 +127,7 @@ class Monitoring
143127
unsigned int mBufferSize;
144128

145129
/// Store for automatically pushed metrics
146-
std::deque<Metric> mPushStore;
130+
std::deque<ComplexMetric> mPushStore;
147131

148132
/// Process monitor interval
149133
std::atomic<unsigned int> mProcessMonitoringInterval;

src/ComplexMetric.cxx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
///
2+
/// \file ComplexMetric.cxx
3+
/// \author Adam Wegrzynek <[email protected]>
4+
///
5+
6+
#include "Monitoring/ComplexMetric.h"
7+
8+
#include <iostream>
9+
#include <chrono>
10+
#include <memory>
11+
12+
namespace o2
13+
{
14+
/// ALICE O2 Monitoring system
15+
namespace monitoring
16+
{
17+
18+
ComplexMetric::ComplexMetric(int value, const std::string& name) :
19+
Metric(value, name)
20+
{}
21+
22+
ComplexMetric::ComplexMetric(std::string value, const std::string& name) :
23+
Metric(value, name)
24+
{}
25+
26+
ComplexMetric::ComplexMetric(double value, const std::string& name) :
27+
Metric(value, name)
28+
{}
29+
30+
ComplexMetric::ComplexMetric(uint64_t value, const std::string& name) :
31+
Metric(value, name)
32+
{}
33+
34+
ComplexMetric::ComplexMetric(boost::variant< int, std::string, double, uint64_t > value, const std::string& name) :
35+
Metric(value, name)
36+
{}
37+
38+
void ComplexMetric::resetTimestamp()
39+
{
40+
mTimestamp = Metric::getCurrentTimestamp();
41+
}
42+
43+
ComplexMetric& ComplexMetric::operator=(const boost::variant< int, std::string, double, uint64_t >& value) {
44+
mValue = value;
45+
return *this;
46+
}
47+
48+
} // namespace monitoring
49+
} // namespace o2

src/Metric.cxx

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,40 +50,11 @@ Metric::Metric(boost::variant< int, std::string, double, uint64_t > value, const
5050
mValue(value), mName(name), mTimestamp(timestamp)
5151
{}
5252

53-
Metric::Metric(const Metric& other)
54-
{
55-
std::lock_guard<std::mutex> lock(other.mValueMutex);
56-
mName = other.mName;
57-
mValue = other.mValue;
58-
mTimestamp = other.mTimestamp;
59-
tagSet = other.tagSet;
60-
}
61-
62-
Metric& Metric::operator=(Metric const& other)
63-
{
64-
if (&other != this) {
65-
std::unique_lock<std::mutex> lockThis(mValueMutex, std::defer_lock);
66-
std::unique_lock<std::mutex> lockOther(other.mValueMutex, std::defer_lock);
67-
std::lock(lockThis, lockOther);
68-
69-
mName = other.mName;
70-
mValue = other.mValue;
71-
mTimestamp = other.mTimestamp;
72-
tagSet = other.tagSet;
73-
}
74-
return *this;
75-
}
76-
7753
boost::variant< int, std::string, double, uint64_t > Metric::getValue() const
7854
{
7955
return mValue;
8056
}
8157

82-
Metric& Metric::operator=(const boost::variant< int, std::string, double, uint64_t >& value) {
83-
mValue = value;
84-
return *this;
85-
}
86-
8758
Metric&& Metric::addTags(std::vector<Tag>&& tags)
8859
{
8960
tagSet = std::move(tags);

src/Monitoring.cxx

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,6 @@ void Monitoring::enableProcessMonitoring(const unsigned int interval) {
6363
#endif
6464
}
6565

66-
void Monitoring::startTimer(std::string name) {
67-
auto search = mTimers.find(name);
68-
if (search == mTimers.end()) {
69-
auto now = std::chrono::steady_clock::now();
70-
mTimers.insert(std::make_pair(name, now));
71-
} else {
72-
MonLogger::Get() << "Monitoring timer : Timer for " << name << " already started" << MonLogger::End();
73-
}
74-
}
75-
76-
void Monitoring::stopAndSendTimer(std::string name) {
77-
auto search = mTimers.find(name);
78-
if (search != mTimers.end()) {
79-
auto now = std::chrono::duration_cast <std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
80-
auto start = std::chrono::duration_cast <std::chrono::milliseconds>(search->second.time_since_epoch()).count();
81-
uint64_t duration = now - start;
82-
send({duration, name});
83-
} else {
84-
MonLogger::Get() << "Monitoring timer : Cannot stop " << name << " timer as it hasn't started" << MonLogger::End();
85-
}
86-
}
87-
8866
void Monitoring::addGlobalTag(std::string name, std::string value)
8967
{
9068
for (auto& backend: mBackends) {
@@ -124,7 +102,8 @@ void Monitoring::pushLoop()
124102

125103
if (mAutoPushInterval != 0 && (loopCount % (mAutoPushInterval*10)) == 0) {
126104
std::vector<Metric> metrics;
127-
for (auto& metric : mPushStore) {
105+
for (auto metric : mPushStore) {
106+
metric.resetTimestamp();
128107
metrics.push_back(metric);
129108
}
130109
send(std::move(metrics));
@@ -134,10 +113,12 @@ void Monitoring::pushLoop()
134113
}
135114
}
136115

137-
Metric& Monitoring::getAutoPushMetric(std::string name)
116+
ComplexMetric& Monitoring::getAutoPushMetric(std::string name, unsigned int interval)
138117
{
139-
if (mAutoPushInterval == 0) {
140-
MonLogger::Get() << "[WARN] AutoPush is not enabled" << MonLogger::End();
118+
if (!mMonitorRunning) {
119+
mMonitorRunning = true;
120+
mMonitorThread = std::thread(&Monitoring::pushLoop, this);
121+
mAutoPushInterval = interval;
141122
}
142123
mPushStore.emplace_back(boost::variant< int, std::string, double, uint64_t > {}, name);
143124
return mPushStore.back();
@@ -166,15 +147,6 @@ void Monitoring::debug(Metric&& metric)
166147
}
167148
}
168149

169-
void Monitoring::enableAutoPush(unsigned int interval)
170-
{
171-
if (!mMonitorRunning) {
172-
mMonitorRunning = true;
173-
mMonitorThread = std::thread(&Monitoring::pushLoop, this);
174-
}
175-
mAutoPushInterval = interval;
176-
}
177-
178150
void Monitoring::pushToBackends(Metric&& metric)
179151
{
180152
if (mBuffering) {

0 commit comments

Comments
 (0)