MySQL 26.7.0
Source Code Documentation
sharded_counter.h
Go to the documentation of this file.
1// Copyright (c) 2026, 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 MYSQL_SCHEDULER_SHARDED_COUNTER_H
25#define MYSQL_SCHEDULER_SHARDED_COUNTER_H
26
27#include <atomic>
28#include <chrono>
29#include <unordered_map>
30
31namespace mysql::scheduler {
32
33/// @brief Concurrent counter, relaxing contention on atomic counter when
34/// being updated by different threads.
35/// The caller is able to get a value for specific thread, and store the
36/// value held by a specific thread (`get`, `store`).
37/// In addition to that, the caller can extract
38/// cumulative value for all of the threads (via `get`), atomically increment
39/// the counter by a specific value, or use helper
40/// time counters (`start_timer`, `stop_timer`, `get_timer`).
42 public:
43 /// @brief Obtains value for specific thread id
44 /// @param thread_id Requested thread id
45 long long get(std::size_t thread_id) const;
46 /// @brief Coalescing get operation, available only for integer type
47 long long get() const;
48 /// @brief Stores value for specific thread id
49 /// @param arg Update op argument
50 /// @param thread_id Requested thread id
51 void store(long long arg, std::size_t thread_id = 0);
52 /// @brief Updates value (add operation) for specific thread id
53 /// @param arg Update op argument
54 /// @param thread_id Requested thread id
55 void add(long long arg, std::size_t thread_id = 0);
56 /// @brief Initializes this counter for requested number of threads, starting
57 /// with thread id equal to 0, up to num_threads-1
58 /// @param num_threads Requested number of threads
59 /// @param enabled Specify whether this statistic is enabled
60 void init(std::size_t num_threads, bool enabled = true);
61 /// @brief Special function to handle timer (helper for add/get when builing
62 /// a timer over a concurrent counter). Start timer.
63 /// @param thread_id Requested thread id
64 void start_time(std::size_t thread_id = 0);
65 /// @brief Special function to handle timer (helper for add/get when builing
66 /// a timer over a concurrent counter). Stop timer for the specified thread id
67 /// @param thread_id Requested thread id
68 void stop_time(std::size_t thread_id = 0);
69 /// @brief Special function to handle timer (helper for add/get when builing
70 /// a timer over a concurrent counter). Get timer value for the specified
71 /// thread id
72 /// @param thread_id Requested thread id
73 /// @return Timer value for the specified thread
74 long long get_timer(std::size_t thread_id) const;
75 /// @brief Special function to handle timer (helper for add/get when builing
76 /// a timer over a concurrent counter). Get coalesced timer value.
77 /// @return Coalesced timer value
78 long long get_timer() const;
79 /// @brief Sets the internal value to 0
80 void reset();
81
82 protected:
83 using Map_value_type = std::atomic<long long>;
84 /// @brief Keeps mapping between thread id and value of the counter
85 /// for this thread id
86 std::unordered_map<long long, Map_value_type> m_value;
87 /// Flag specifying whether this statistic is enabled, when false, methods
88 /// are noop and return default values
89 bool m_enabled{true};
90};
91
92} // namespace mysql::scheduler
93
94#endif // MYSQL_SCHEDULER_SHARDED_COUNTER_H
Concurrent counter, relaxing contention on atomic counter when being updated by different threads.
Definition: sharded_counter.h:41
long long get_timer() const
Special function to handle timer (helper for add/get when builing a timer over a concurrent counter).
Definition: sharded_counter.cpp:120
std::atomic< long long > Map_value_type
Definition: sharded_counter.h:83
void init(std::size_t num_threads, bool enabled=true)
Initializes this counter for requested number of threads, starting with thread id equal to 0,...
Definition: sharded_counter.cpp:62
void reset()
Sets the internal value to 0.
Definition: sharded_counter.cpp:76
void add(long long arg, std::size_t thread_id=0)
Updates value (add operation) for specific thread id.
Definition: sharded_counter.cpp:55
long long get() const
Coalescing get operation, available only for integer type.
Definition: sharded_counter.cpp:37
std::unordered_map< long long, Map_value_type > m_value
Keeps mapping between thread id and value of the counter for this thread id.
Definition: sharded_counter.h:86
void store(long long arg, std::size_t thread_id=0)
Stores value for specific thread id.
Definition: sharded_counter.cpp:48
bool m_enabled
Flag specifying whether this statistic is enabled, when false, methods are noop and return default va...
Definition: sharded_counter.h:89
void stop_time(std::size_t thread_id=0)
Special function to handle timer (helper for add/get when builing a timer over a concurrent counter).
Definition: sharded_counter.cpp:101
void start_time(std::size_t thread_id=0)
Special function to handle timer (helper for add/get when builing a timer over a concurrent counter).
Definition: sharded_counter.cpp:93
static my_thread_id thread_id
Definition: my_thr_init.cc:60
Definition: base_dependency_tracker.h:41
required bool enabled
Definition: replication_group_member_actions.proto:33