MySQL 9.2.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
time_based_metric.h
Go to the documentation of this file.
1// Copyright (c) 2024, Oracle and/or its affiliates.
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of MySQL hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License, version 2.0, for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23
24#ifndef CHANGESTREAMS_APPLY_METRICS_TIME_BASED_METRIC_H
25#define CHANGESTREAMS_APPLY_METRICS_TIME_BASED_METRIC_H
26
27#include <atomic>
29
30/// @brief Class that encodes how much time we waited for something
32 public:
33 /// @brief Constructor that allows you to define counting as being manual
34 /// @param manual_counting shall count be automatic on start_timer or not
35 /// (default false)
36 explicit Time_based_metric(bool manual_counting = false);
37
38 /// @brief Copy assignment operator
39 /// @param other the object that is copied
40 /// @return this object
42
43 /// @brief Assignment operator for the interface
44 /// @param other the object that is copied
45 /// @return this object
47
48 /// @brief Deleted copy constructor, move constructor, move assignment
49 /// operator.
53
54 /// @brief Default destuctor.
55 ~Time_based_metric() override = default;
56
57 /// @brief Resets the counter and summed time to 0
58 void reset() override;
59
60 /// @brief Returns the time waited across all executions of the start/stop
61 /// methods
62 /// @return The total time waited
63 int64_t get_time() const override;
64
65 /// @brief Increment the counter.
66 ///
67 /// This is only allowed if the constructor was called using
68 /// manual_counting=1. Otherwise, an assertion is raised.
69 void increment_counter() override;
70
71 /// @brief Returns the number of time we waited on give spot
72 /// @return the number of times waited
73 int64_t get_count() const override;
74
75 protected:
76 /// @brief Starts the timer.
77 void start_timer() override;
78
79 /// @brief Stops the timer.
80 void stop_timer() override;
81
82 private:
83 /// @brief Helper to get current time.
84 /// @return Current time since the epoch for steady_clock, in nanoseconds.
85 static int64_t now();
86
87 /// The total nanoseconds of all completed waits, minus the absolute start
88 /// time of an ongoing wait, if any.
89 ///
90 /// If there is no ongoing wait, this is nonnegative and is the correct
91 /// metric. If there is an ongoing wait, this is negative, and the correct
92 /// value is given by adding the current time to it:
93 /// result = sum_of_completed_waits + current_time - start_of_current_wait
94 std::atomic<int64_t> m_time{0};
95 /// @brief The number of times we waited.
96 std::atomic<int64_t> m_count{0};
97
98 /// If false, the counter is incremented automatically by start_time, and the
99 /// caller must not invoke increment_counter. If true, the counter is not
100 /// incremented by start_time, so the caller has to invoke increment_counter.
101 bool m_manual_counting{false};
102};
103
104#endif /* CHANGESTREAMS_APPLY_METRICS_TIME_BASED_METRIC_H */
Abstract class for time based metrics implementations.
Definition: time_based_metric_interface.h:32
Class that encodes how much time we waited for something.
Definition: time_based_metric.h:31
void start_timer() override
Starts the timer.
Definition: time_based_metric.cc:49
std::atomic< int64_t > m_count
The number of times we waited.
Definition: time_based_metric.h:96
Time_based_metric(Time_based_metric &&)=delete
Time_based_metric & operator=(const Time_based_metric &other)
Copy assignment operator.
Definition: time_based_metric.cc:31
static int64_t now()
Helper to get current time.
Definition: time_based_metric.cc:76
int64_t get_time() const override
Returns the time waited across all executions of the start/stop methods.
Definition: time_based_metric.cc:62
Time_based_metric(const Time_based_metric &)=delete
Deleted copy constructor, move constructor, move assignment operator.
~Time_based_metric() override=default
Default destuctor.
Time_based_metric & operator=(Time_based_metric &&)=delete
bool m_manual_counting
If false, the counter is incremented automatically by start_time, and the caller must not invoke incr...
Definition: time_based_metric.h:101
int64_t get_count() const override
Returns the number of time we waited on give spot.
Definition: time_based_metric.cc:74
Time_based_metric(bool manual_counting=false)
Constructor that allows you to define counting as being manual.
Definition: time_based_metric.cc:28
void reset() override
Resets the counter and summed time to 0.
Definition: time_based_metric.cc:44
void stop_timer() override
Stops the timer.
Definition: time_based_metric.cc:57
void increment_counter() override
Increment the counter.
Definition: time_based_metric.cc:69
std::atomic< int64_t > m_time
The total nanoseconds of all completed waits, minus the absolute start time of an ongoing wait,...
Definition: time_based_metric.h:94