| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2012, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/ut0counter.h |
| 29 |
|
| 30 |
Counter utility class |
| 31 |
|
| 32 |
Created 2012/04/12 by Sunny Bains |
| 33 |
*******************************************************/ |
| 34 |
|
| 35 |
#ifndef ut0counter_h |
| 36 |
#define ut0counter_h |
| 37 |
|
| 38 |
#include <my_rdtsc.h> |
| 39 |
|
| 40 |
#include "univ.i" |
| 41 |
|
| 42 |
#include "os0thread.h" |
| 43 |
#include "ut0cpu_cache.h" |
| 44 |
#include "ut0dbg.h" |
| 45 |
|
| 46 |
#include <array> |
| 47 |
#include <atomic> |
| 48 |
#include <functional> |
| 49 |
|
| 50 |
/** Default number of slots to use in ib_counter_t */ |
| 51 |
constexpr uint32_t IB_N_SLOTS = 64; |
| 52 |
|
| 53 |
/** Get the offset into the counter array. */ |
| 54 |
template <typename Type, int N> |
| 55 |
struct generic_indexer_t { |
| 56 |
/** Default constructor/destructor should be OK. */ |
| 57 |
|
| 58 |
/** @return offset within m_counter */ |
| 59 |
static size_t offset(size_t index) UNIV_NOTHROW { |
| 60 |
return (((index % N) + 1) * (ut::INNODB_CACHE_LINE_SIZE / sizeof(Type))); |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
/** Use the result of my_timer_cycles(), which mainly uses RDTSC for cycles, |
| 65 |
to index into the counter array. See the comments for my_timer_cycles() */ |
| 66 |
template <typename Type = ulint, int N = 1> |
| 67 |
struct counter_indexer_t : public generic_indexer_t<Type, N> { |
| 68 |
/** Default constructor/destructor should be OK. */ |
| 69 |
|
| 70 |
enum { fast = 1 }; |
| 71 |
|
| 72 |
/** @return result from RDTSC or similar functions. */ |
| 73 |
static size_t get_rnd_index() UNIV_NOTHROW { |
| 74 |
size_t c = static_cast<size_t>(my_timer_cycles()); |
| 75 |
|
| 76 |
if (c != 0) { |
| 77 |
return (c); |
| 78 |
} else { |
| 79 |
/* We may go here if my_timer_cycles() returns 0, |
| 80 |
so we have to have the plan B for the counter. */ |
| 81 |
#if !defined(_WIN32) |
| 82 |
return ut::this_thread_hash; |
| 83 |
#else |
| 84 |
LARGE_INTEGER cnt; |
| 85 |
QueryPerformanceCounter(&cnt); |
| 86 |
|
| 87 |
return static_cast<size_t>(cnt.QuadPart); |
| 88 |
#endif /* !_WIN32 */ |
| 89 |
} |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
/** For counters where N=1 */ |
| 94 |
template <typename Type = ulint, int N = 1> |
| 95 |
struct single_indexer_t { |
| 96 |
/** Default constructor/destructor should are OK. */ |
| 97 |
|
| 98 |
enum { fast = 0 }; |
| 99 |
|
| 100 |
/** @return offset within m_counter */ |
| 101 |
static size_t offset(size_t index [[maybe_unused]]) UNIV_NOTHROW { |
| 102 |
static_assert(N == 1); |
| 103 |
return ((ut::INNODB_CACHE_LINE_SIZE / sizeof(Type))); |
| 104 |
} |
| 105 |
|
| 106 |
/** @return 1 */ |
| 107 |
static size_t get_rnd_index() UNIV_NOTHROW { |
| 108 |
static_assert(N == 1); |
| 109 |
return (1); |
| 110 |
} |
| 111 |
}; |
| 112 |
|
| 113 |
#define default_indexer_t counter_indexer_t |
| 114 |
|
| 115 |
/** Class for using fuzzy counters. The counter is not protected by any |
| 116 |
mutex and the results are not guaranteed to be 100% accurate but close |
| 117 |
enough. Creates an array of counters and separates each element by the |
| 118 |
ut::INNODB_CACHE_LINE_SIZE bytes */ |
| 119 |
template <typename Type, int N = IB_N_SLOTS, |
| 120 |
template <typename, int> class Indexer = default_indexer_t> |
| 121 |
class ib_counter_t { |
| 122 |
public: |
| 123 |
ib_counter_t() { memset(m_counter, 0x0, sizeof(m_counter)); } |
| 124 |
|
| 125 |
~ib_counter_t() { ut_ad(validate()); } |
| 126 |
|
| 127 |
static bool is_fast() { return (Indexer<Type, N>::fast); } |
| 128 |
|
| 129 |
bool validate() UNIV_NOTHROW { |
| 130 |
#ifdef UNIV_DEBUG |
| 131 |
size_t n = (ut::INNODB_CACHE_LINE_SIZE / sizeof(Type)); |
| 132 |
|
| 133 |
/* Check that we aren't writing outside our defined bounds. */ |
| 134 |
for (size_t i = 0; i < UT_ARR_SIZE(m_counter); i += n) { |
| 135 |
for (size_t j = 1; j < n - 1; ++j) { |
| 136 |
ut_ad(m_counter[i + j] == 0); |
| 137 |
} |
| 138 |
} |
| 139 |
#endif /* UNIV_DEBUG */ |
| 140 |
return (true); |
| 141 |
} |
| 142 |
|
| 143 |
/** If you can't use a good index id. Increment by 1. */ |
| 144 |
void inc() UNIV_NOTHROW { add(1); } |
| 145 |
|
| 146 |
/** If you can't use a good index id. |
| 147 |
@param n is the amount to increment */ |
| 148 |
void add(Type n) UNIV_NOTHROW { |
| 149 |
size_t i = m_policy.offset(m_policy.get_rnd_index()); |
| 150 |
|
| 151 |
ut_ad(i < UT_ARR_SIZE(m_counter)); |
| 152 |
|
| 153 |
m_counter[i] += n; |
| 154 |
} |
| 155 |
|
| 156 |
/** Use this if you can use a unique identifier, saves a |
| 157 |
call to get_rnd_index(). |
| 158 |
@param index index into a slot |
| 159 |
@param n amount to increment */ |
| 160 |
void add(size_t index, Type n) UNIV_NOTHROW { |
| 161 |
size_t i = m_policy.offset(index); |
| 162 |
|
| 163 |
ut_ad(i < UT_ARR_SIZE(m_counter)); |
| 164 |
|
| 165 |
m_counter[i] += n; |
| 166 |
} |
| 167 |
|
| 168 |
/** If you can't use a good index id. Decrement by 1. */ |
| 169 |
void dec() UNIV_NOTHROW { sub(1); } |
| 170 |
|
| 171 |
/** If you can't use a good index id. |
| 172 |
@param n the amount to decrement */ |
| 173 |
void sub(Type n) UNIV_NOTHROW { |
| 174 |
size_t i = m_policy.offset(m_policy.get_rnd_index()); |
| 175 |
|
| 176 |
ut_ad(i < UT_ARR_SIZE(m_counter)); |
| 177 |
|
| 178 |
m_counter[i] -= n; |
| 179 |
} |
| 180 |
|
| 181 |
/** Use this if you can use a unique identifier, saves a |
| 182 |
call to get_rnd_index(). |
| 183 |
@param index index into a slot |
| 184 |
@param n amount to decrement */ |
| 185 |
void sub(size_t index, Type n) UNIV_NOTHROW { |
| 186 |
size_t i = m_policy.offset(index); |
| 187 |
|
| 188 |
ut_ad(i < UT_ARR_SIZE(m_counter)); |
| 189 |
|
| 190 |
m_counter[i] -= n; |
| 191 |
} |
| 192 |
|
| 193 |
/* @return total value - not 100% accurate, since it is not atomic. */ |
| 194 |
operator Type() const UNIV_NOTHROW { |
| 195 |
Type total = 0; |
| 196 |
|
| 197 |
for (size_t i = 0; i < N; ++i) { |
| 198 |
total += m_counter[m_policy.offset(i)]; |
| 199 |
} |
| 200 |
|
| 201 |
return total; |
| 202 |
} |
| 203 |
|
| 204 |
Type operator[](size_t index) const UNIV_NOTHROW { |
| 205 |
size_t i = m_policy.offset(index); |
| 206 |
|
| 207 |
ut_ad(i < UT_ARR_SIZE(m_counter)); |
| 208 |
|
| 209 |
return m_counter[i]; |
| 210 |
} |
| 211 |
|
| 212 |
private: |
| 213 |
/** Indexer into the array */ |
| 214 |
Indexer<Type, N> m_policy; |
| 215 |
|
| 216 |
/** Slot 0 is unused. */ |
| 217 |
Type m_counter[(N + 1) * (ut::INNODB_CACHE_LINE_SIZE / sizeof(Type))]; |
| 218 |
}; |
| 219 |
|
| 220 |
/** Sharded atomic counter. */ |
| 221 |
namespace Counter { |
| 222 |
|
| 223 |
using Type = uint64_t; |
| 224 |
|
| 225 |
using N = std::atomic<Type>; |
| 226 |
|
| 227 |
static_assert(ut::INNODB_CACHE_LINE_SIZE >= sizeof(N), |
| 228 |
"Atomic counter size > ut::INNODB_CACHE_LINE_SIZE"); |
| 229 |
|
| 230 |
using Pad = byte[ut::INNODB_CACHE_LINE_SIZE - sizeof(N)]; |
| 231 |
|
| 232 |
/** Counter shard. */ |
| 233 |
struct Shard { |
| 234 |
/** Separate on cache line. */ |
| 235 |
Pad m_pad; |
| 236 |
|
| 237 |
/** Sharded counter. */ |
| 238 |
N m_n{}; |
| 239 |
}; |
| 240 |
|
| 241 |
using Function = std::function<void(const Type)>; |
| 242 |
|
| 243 |
/** Relaxed order by default. */ |
| 244 |
constexpr auto Memory_order = std::memory_order_relaxed; |
| 245 |
|
| 246 |
template <size_t COUNT = 128> |
| 247 |
struct Shards { |
| 248 |
/* Shard array. */ |
| 249 |
std::array<Shard, COUNT> m_arr{}; |
| 250 |
|
| 251 |
/* Memory order for the shards. */ |
| 252 |
std::memory_order m_memory_order{Memory_order}; |
| 253 |
|
| 254 |
/** Override default memory order. |
| 255 |
@param[in] memory_order memory order */ |
| 256 |
void set_order(std::memory_order memory_order) { |
| 257 |
m_memory_order = memory_order; |
| 258 |
} |
| 259 |
}; |
| 260 |
|
| 261 |
/** Increment the counter for a shard by n. |
| 262 |
@param[in,out] shards Sharded counter to increment. |
| 263 |
@param[in] id Shard key. |
| 264 |
@param[in] n Number to add. |
| 265 |
@return previous value. */ |
| 266 |
template <size_t COUNT> |
| 267 |
inline Type add(Shards<COUNT> &shards, size_t id, size_t n) { |
| 268 |
auto &shard_arr = shards.m_arr; |
| 269 |
auto order = shards.m_memory_order; |
| 270 |
|
| 271 |
return (shard_arr[id % shard_arr.size()].m_n.fetch_add(n, order)); |
| 272 |
} |
| 273 |
|
| 274 |
/** Decrement the counter for a shard by n. |
| 275 |
@param[in,out] shards Sharded counter to increment. |
| 276 |
@param[in] id Shard key. |
| 277 |
@param[in] n Number to add. |
| 278 |
@return previous value. */ |
| 279 |
template <size_t COUNT> |
| 280 |
inline Type sub(Shards<COUNT> &shards, size_t id, size_t n) { |
| 281 |
ut_ad(get(shards, id) >= n); |
| 282 |
|
| 283 |
auto &shard_arr = shards.m_arr; |
| 284 |
auto order = shards.m_memory_order; |
| 285 |
return (shard_arr[id % shard_arr.size()].m_n.fetch_sub(n, order)); |
| 286 |
} |
| 287 |
|
| 288 |
/** Increment the counter of a shard by 1. |
| 289 |
@param[in,out] shards Sharded counter to increment. |
| 290 |
@param[in] id Shard key. |
| 291 |
@return previous value. */ |
| 292 |
template <size_t COUNT> |
| 293 |
inline Type inc(Shards<COUNT> &shards, size_t id) { |
| 294 |
return (add(shards, id, 1)); |
| 295 |
} |
| 296 |
|
| 297 |
/** Decrement the counter of a shard by 1. |
| 298 |
@param[in,out] shards Sharded counter to decrement. |
| 299 |
@param[in] id Shard key. |
| 300 |
@return previous value. */ |
| 301 |
template <size_t COUNT> |
| 302 |
inline Type dec(Shards<COUNT> &shards, size_t id) { |
| 303 |
return (sub(shards, id, 1)); |
| 304 |
} |
| 305 |
|
| 306 |
/** Get the counter value for a shard. |
| 307 |
@param[in,out] shards Sharded counter to increment. |
| 308 |
@param[in] id Shard key. |
| 309 |
@return current value. */ |
| 310 |
template <size_t COUNT> |
| 311 |
inline Type get(const Shards<COUNT> &shards, size_t id) noexcept { |
| 312 |
auto &shard_arr = shards.m_arr; |
| 313 |
auto order = shards.m_memory_order; |
| 314 |
|
| 315 |
return (shard_arr[id % shard_arr.size()].m_n.load(order)); |
| 316 |
} |
| 317 |
|
| 318 |
/** Iterate over the shards. |
| 319 |
@param[in] shards Shards to iterate over |
| 320 |
@param[in] f Callback function |
| 321 |
*/ |
| 322 |
template <size_t COUNT> |
| 323 |
inline void for_each(const Shards<COUNT> &shards, Function &&f) noexcept { |
| 324 |
for (const auto &shard : shards.m_arr) { |
| 325 |
f(shard.m_n); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
/** Get the total value of all shards. |
| 330 |
@param[in] shards Shards to sum. |
| 331 |
@return total value. */ |
| 332 |
template <size_t COUNT> |
| 333 |
inline Type total(const Shards<COUNT> &shards) noexcept { |
| 334 |
Type n = 0; |
| 335 |
|
| 336 |
for_each(shards, [&](const Type count) { n += count; }); |
| 337 |
|
| 338 |
return (n); |
| 339 |
} |
| 340 |
|
| 341 |
/** Clear the counter - reset to 0. |
| 342 |
@param[in,out] shards Shards to clear. */ |
| 343 |
template <size_t COUNT> |
| 344 |
inline void clear(Shards<COUNT> &shards) noexcept { |
| 345 |
for (auto &shard : shards.m_arr) { |
| 346 |
shard.m_n.store(0, shards.m_memory_order); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
/** Copy the counters, overwrite destination. |
| 351 |
@param[in,out] dst Destination shard |
| 352 |
@param[in] src Source shard. */ |
| 353 |
template <size_t COUNT> |
| 354 |
inline void copy(Shards<COUNT> &dst, const Shards<COUNT> &src) noexcept { |
| 355 |
size_t i{0}; |
| 356 |
for_each(src, [&](const Type count) { |
| 357 |
dst.m_arr[i++].m_n.store(count, dst.m_memory_order); |
| 358 |
}); |
| 359 |
} |
| 360 |
|
| 361 |
/** Accumulate the counters, add source to destination. |
| 362 |
@param[in,out] dst Destination shard |
| 363 |
@param[in] src Source shard. */ |
| 364 |
template <size_t COUNT> |
| 365 |
inline void add(Shards<COUNT> &dst, const Shards<COUNT> &src) noexcept { |
| 366 |
size_t i{0}; |
| 367 |
for_each(src, [&](const Type count) { |
| 368 |
dst.m_arr[i++].m_n.fetch_add(count, dst.m_memory_order); |
| 369 |
}); |
| 370 |
} |
| 371 |
} // namespace Counter |
| 372 |
|
| 373 |
#endif /* ut0counter_h */ |