Skip to content

Commit 0fde16f

Browse files
committed
Thread-safe implementation of the worker performance counter class
1 parent 423d269 commit 0fde16f

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/replica/util/Performance.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,9 @@ ostream& operator<<(ostream& os, Performance const& p) {
7777
WorkerPerformance::WorkerPerformance()
7878
: receive_time(util::TimeUtils::now()), start_time(0), finish_time(0) {}
7979

80-
uint64_t WorkerPerformance::setUpdateStart() {
81-
uint64_t const t = start_time;
82-
start_time = util::TimeUtils::now();
83-
return t;
84-
}
80+
uint64_t WorkerPerformance::setUpdateStart() { return start_time.exchange(util::TimeUtils::now()); }
8581

86-
uint64_t WorkerPerformance::setUpdateFinish() {
87-
uint64_t const t = finish_time;
88-
finish_time = util::TimeUtils::now();
89-
return t;
90-
}
82+
uint64_t WorkerPerformance::setUpdateFinish() { return finish_time.exchange(util::TimeUtils::now()); }
9183

9284
unique_ptr<ProtocolPerformance> WorkerPerformance::info() const {
9385
auto ptr = make_unique<ProtocolPerformance>();
@@ -97,6 +89,12 @@ unique_ptr<ProtocolPerformance> WorkerPerformance::info() const {
9789
return ptr;
9890
}
9991

92+
json WorkerPerformance::toJson() const {
93+
return json::object({{"receive_time", receive_time.load()},
94+
{"start_time", start_time.load()},
95+
{"finish_time", finish_time.load()}});
96+
}
97+
10098
ostream& operator<<(ostream& os, WorkerPerformance const& p) {
10199
os << "WorkerPerformance "
102100
<< " receive:" << p.receive_time << " start:" << p.start_time << " finish:" << p.finish_time

src/replica/util/Performance.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*/
2929

3030
// System headers
31+
#include <atomic>
3132
#include <cstdint>
3233
#include <memory>
3334
#include <ostream>
@@ -116,6 +117,7 @@ std::ostream& operator<<(std::ostream& os, Performance const& p);
116117
*/
117118
class WorkerPerformance {
118119
public:
120+
/// All (but the request receive time) timestamps will be initialized with 0.
119121
WorkerPerformance();
120122
WorkerPerformance(WorkerPerformance const&) = default;
121123
WorkerPerformance& operator=(WorkerPerformance const&) = default;
@@ -126,9 +128,9 @@ class WorkerPerformance {
126128

127129
std::unique_ptr<ProtocolPerformance> info() const;
128130

129-
uint64_t receive_time = 0; /// Received by a worker service
130-
uint64_t start_time = 0; /// Execution started by a worker service
131-
uint64_t finish_time = 0; /// Execution finished by a worker service
131+
std::atomic<uint64_t> receive_time; ///< Received by a worker service
132+
std::atomic<uint64_t> start_time; ///< Execution started by a worker service
133+
std::atomic<uint64_t> finish_time; ///< Execution finished by a worker service
132134
};
133135

134136
std::ostream& operator<<(std::ostream& os, WorkerPerformance const& p);

0 commit comments

Comments
 (0)