MySQL 9.1.0
Source Code Documentation
time_based_metric.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
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 CS_MTA_TIME_BASED_METRIC
25#define CS_MTA_TIME_BASED_METRIC
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 Assignment operator
39 /// @param other the object that is copied
40 /// @return this object
42
43 /// @brief Deleted copy constructor, move constructor, move assignment
44 /// operator.
48
49 /// @brief Default destuctor.
50 ~Time_based_metric() override = default;
51
52 /// @brief Resets the counter and summed time to 0
53 void reset() override;
54
55 /// @brief Starts counting time we are waiting on something
56 void start_timer() override;
57
58 /// @brief Stops the timer for the wait.
59 /// Requires start_timer to be called first
60 void stop_timer() override;
61
62 /// @brief Returns the time waited across all executions of the start/stop
63 /// methods
64 /// @return The total time waited
65 int64_t get_sum_time_elapsed() const override;
66
67 /// @brief Increments the waiting counter
68 void increment_counter() override;
69
70 /// @brief Returns the number of time we waited on give spot
71 /// @return the number of times waited
72 int64_t get_count() const override;
73
74 private:
75 /// @brief Helper to get current time.
76 /// @return Current time since the epoch for steady_clock, in nanoseconds.
77 static int64_t now();
78
79 /// The total nanoseconds of all completed waits, minus the absolute start
80 /// time of an ongoing wait, if any.
81 ///
82 /// If there is no ongoing wait, this is nonnegative and is the correct
83 /// metric. If there is an ongoing wait, this is negative, and the correct
84 /// value is given by adding the current time to it:
85 /// result = sum_of_completed_waits + current_time - start_of_current_wait
86 std::atomic<int64_t> m_time{0};
87 /// @brief The number of times we waited.
88 std::atomic<int64_t> m_count{0};
89
90 /// If false, the counter is incremented automatically by start_time, and the
91 /// caller must not invoke increment_counter. If true, the counter is not
92 /// incremented by start_time, so the caller has to invoke increment_counter.
93 bool m_manual_counting{false};
94};
95
96#endif /* CS_MTA_TIME_BASED_METRIC */
Abstract class for time based metrics implementations.
Definition: time_based_metric_interface.h:30
Class that encodes how much time we waited for something.
Definition: time_based_metric.h:31
void start_timer() override
Starts counting time we are waiting on something.
Definition: time_based_metric.cc:43
int64_t get_sum_time_elapsed() const override
Returns the time waited across all executions of the start/stop methods.
Definition: time_based_metric.cc:56
std::atomic< int64_t > m_count
The number of times we waited.
Definition: time_based_metric.h:88
Time_based_metric(Time_based_metric &&)=delete
Time_based_metric & operator=(const Time_based_metric &other)
Assignment operator.
Definition: time_based_metric.cc:31
static int64_t now()
Helper to get current time.
Definition: time_based_metric.cc:70
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:93
int64_t get_count() const override
Returns the number of time we waited on give spot.
Definition: time_based_metric.cc:68
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:38
void stop_timer() override
Stops the timer for the wait.
Definition: time_based_metric.cc:51
void increment_counter() override
Increments the waiting counter.
Definition: time_based_metric.cc:63
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:86