MySQL 9.3.0
Source Code Documentation
entity.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2023, 2025, 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 designed to work 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 either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_OBSERVABILITY_ENTITY_H_
27#define ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_OBSERVABILITY_ENTITY_H_
28
29#include <atomic>
30#include <cassert>
31#include <shared_mutex>
32
33namespace mrs {
34namespace observability {
35
36class Common {
37 public:
38 static inline std::shared_mutex mutex;
39};
40
41class Entity : public Common {
42 public:
43 virtual ~Entity() = default;
44
45 virtual uint64_t get_id() const = 0;
46 virtual std::string get_name() const = 0;
47 virtual uint64_t get_value_and_reset() = 0;
48};
49
50template <uint64_t counter_id>
51class EntityWithId : public Entity {
52 public:
53 EntityWithId() { assert(!registred_.test_and_set()); }
54
55 protected:
56#ifndef NDEBUG
57 static inline std::atomic_flag registred_;
58#endif
59};
60
61template <uint64_t counter_id>
62class EntityCounter : public EntityWithId<counter_id> {
63 public:
64 static void increment(int32_t inc = 1) {
65 auto lock = std::shared_lock<std::shared_mutex>(Common::mutex);
66 value_ += inc;
67 }
68
69 EntityCounter(const std::string &name) : name_{name} {}
70
71 uint64_t get_id() const override { return counter_id; }
72 std::string get_name() const override { return name_; }
73 uint64_t get_value_and_reset() override { return value_.exchange(0); }
74
75 protected:
76 std::string name_;
77 static inline std::atomic<uint64_t> value_;
78};
79
80template <uint64_t counter_id>
81class EntityCounterNotResetable : public EntityCounter<counter_id> {
82 public:
85 uint64_t get_value_and_reset() override { return Parent::value_.load(); }
86};
87
88template <uint64_t counter_id>
89class EntityAverageInt : public EntityCounter<counter_id> {
90 public:
93
94 static void increment(int32_t inc = 1) {
95 auto lock = std::shared_lock<std::shared_mutex>(Common::mutex);
97 ++count_;
98 }
99
100 uint64_t get_value_and_reset() override {
101 return Parent::value_.exchange(0) / count_.exchange(0);
102 }
103
104 protected:
105 static inline std::atomic<uint64_t> count_;
106};
107
108} // namespace observability
109
110template <uint64_t counter_id>
112
113template <uint64_t counter_id>
115
116} // namespace mrs
117
118#endif // ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_OBSERVABILITY_ENTITY_H_
Definition: entity.h:36
static std::shared_mutex mutex
Definition: entity.h:38
static std::atomic< uint64_t > count_
Definition: entity.h:105
static void increment(int32_t inc=1)
Definition: entity.h:94
uint64_t get_value_and_reset() override
Definition: entity.h:100
uint64_t get_value_and_reset() override
Definition: entity.h:85
Definition: entity.h:62
uint64_t get_value_and_reset() override
Definition: entity.h:73
static std::atomic< uint64_t > value_
Definition: entity.h:77
std::string get_name() const override
Definition: entity.h:72
static void increment(int32_t inc=1)
Definition: entity.h:64
EntityCounter(const std::string &name)
Definition: entity.h:69
uint64_t get_id() const override
Definition: entity.h:71
std::string name_
Definition: entity.h:76
Definition: entity.h:51
EntityWithId()
Definition: entity.h:53
static std::atomic_flag registred_
Definition: entity.h:57
Definition: entity.h:41
virtual uint64_t get_value_and_reset()=0
virtual ~Entity()=default
virtual std::string get_name() const =0
virtual uint64_t get_id() const =0
Sharded atomic counter.
Definition: ut0counter.h:221
Type inc(Shards< COUNT > &shards, size_t id)
Increment the counter of a shard by 1.
Definition: ut0counter.h:293
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
Definition: authorize_manager.h:48
case opt name
Definition: sslopt-case.h:29