53template <
typename Type,
int N>
65template <
typename Type = ul
int,
int N = 1>
84 QueryPerformanceCounter(&cnt);
86 return static_cast<size_t>(cnt.QuadPart);
93template <
typename Type = ul
int,
int N = 1>
101 static_assert(
N == 1);
107 static_assert(
N == 1);
112#define default_indexer_t counter_indexer_t
126 static bool is_fast() {
return (Indexer<Type, N>::fast); }
134 for (
size_t j = 1; j <
n - 1; ++j) {
196 for (
size_t i = 0; i <
N; ++i) {
224using N = std::atomic<Type>;
227 "Atomic counter size > ut::INNODB_CACHE_LINE_SIZE");
245template <
size_t COUNT = 128>
265template <
size_t COUNT>
267 auto &shard_arr = shards.
m_arr;
270 return (shard_arr[
id % shard_arr.size()].m_n.fetch_add(
n, order));
278template <
size_t COUNT>
282 auto &shard_arr = shards.
m_arr;
284 return (shard_arr[
id % shard_arr.size()].m_n.fetch_sub(
n, order));
291template <
size_t COUNT>
293 return (
add(shards,
id, 1));
300template <
size_t COUNT>
302 return (
sub(shards,
id, 1));
309template <
size_t COUNT>
311 auto &shard_arr = shards.m_arr;
312 auto order = shards.m_memory_order;
314 return (shard_arr[
id % shard_arr.size()].m_n.load(order));
321template <
size_t COUNT>
323 for (
const auto &shard : shards.m_arr) {
331template <
size_t COUNT>
342template <
size_t COUNT>
344 for (
auto &shard : shards.m_arr) {
345 shard.m_n.store(0, shards.m_memory_order);
352template <
size_t COUNT>
356 dst.m_arr[i++].m_n.store(
count, dst.m_memory_order);
363template <
size_t COUNT>
367 dst.m_arr[i++].m_n.fetch_add(
count, dst.m_memory_order);
Class for using fuzzy counters.
Definition: ut0counter.h:120
static bool is_fast()
Definition: ut0counter.h:126
void sub(Type n) 1
If you can't use a good index id.
Definition: ut0counter.h:172
void add(Type n) 1
If you can't use a good index id.
Definition: ut0counter.h:147
void add(size_t index, Type n) 1
Use this if you can use a unique identifier, saves a call to get_rnd_index().
Definition: ut0counter.h:159
ib_counter_t()
Definition: ut0counter.h:122
bool validate() 1
Definition: ut0counter.h:128
void dec() 1
If you can't use a good index id.
Definition: ut0counter.h:168
Type operator[](size_t index) const 1
Definition: ut0counter.h:203
Type m_counter[(N+1) *(ut::INNODB_CACHE_LINE_SIZE/sizeof(Type))]
Slot 0 is unused.
Definition: ut0counter.h:216
Indexer< Type, N > m_policy
Indexer into the array.
Definition: ut0counter.h:213
void sub(size_t index, Type n) 1
Use this if you can use a unique identifier, saves a call to get_rnd_index().
Definition: ut0counter.h:184
~ib_counter_t()
Definition: ut0counter.h:124
void inc() 1
If you can't use a good index id.
Definition: ut0counter.h:143
Multi-platform timer code.
ulonglong my_timer_cycles(void)
A cycle timer.
Definition: my_rdtsc.cc:97
static int count
Definition: myisam_ftdump.cc:44
Sharded atomic counter.
Definition: ut0counter.h:220
Type inc(Shards< COUNT > &shards, size_t id)
Increment the counter of a shard by 1.
Definition: ut0counter.h:292
std::function< void(const Type)> Function
Definition: ut0counter.h:240
uint64_t Type
Definition: ut0counter.h:222
Type add(Shards< COUNT > &shards, size_t id, size_t n)
Increment the counter for a shard by n.
Definition: ut0counter.h:266
byte[ut::INNODB_CACHE_LINE_SIZE - sizeof(N)] Pad
Definition: ut0counter.h:229
void copy(Shards< COUNT > &dst, const Shards< COUNT > &src) noexcept
Copy the counters, overwrite destination.
Definition: ut0counter.h:353
constexpr auto Memory_order
Relaxed order by default.
Definition: ut0counter.h:243
Type dec(Shards< COUNT > &shards, size_t id)
Decrement the counter of a shard by 1.
Definition: ut0counter.h:301
Type total(const Shards< COUNT > &shards) noexcept
Get the total value of all shards.
Definition: ut0counter.h:332
Type sub(Shards< COUNT > &shards, size_t id, size_t n)
Decrement the counter for a shard by n.
Definition: ut0counter.h:279
void for_each(const Shards< COUNT > &shards, Function &&f) noexcept
Iterate over the shards.
Definition: ut0counter.h:322
std::atomic< Type > N
Definition: ut0counter.h:224
void clear(Shards< COUNT > &shards) noexcept
Clear the counter - reset to 0.
Definition: ut0counter.h:343
Type get(const Shards< COUNT > &shards, size_t id) noexcept
Get the counter value for a shard.
Definition: ut0counter.h:310
constexpr size_t INNODB_CACHE_LINE_SIZE
CPU cache line size.
Definition: ut0cpu_cache.h:40
const thread_local size_t this_thread_hash
The hash value of the current thread's id.
Definition: os0thread.h:72
The interface to the operating system process and thread control primitives.
Counter shard.
Definition: ut0counter.h:232
N m_n
Sharded counter.
Definition: ut0counter.h:237
Pad m_pad
Separate on cache line.
Definition: ut0counter.h:234
Definition: ut0counter.h:246
void set_order(std::memory_order memory_order)
Override default memory order.
Definition: ut0counter.h:255
std::array< Shard, COUNT > m_arr
Definition: ut0counter.h:248
std::memory_order m_memory_order
Definition: ut0counter.h:251
Use the result of my_timer_cycles(), which mainly uses RDTSC for cycles, to index into the counter ar...
Definition: ut0counter.h:66
static size_t get_rnd_index() 1
Definition: ut0counter.h:72
@ fast
Definition: ut0counter.h:69
Get the offset into the counter array.
Definition: ut0counter.h:54
static size_t offset(size_t index) 1
Default constructor/destructor should be OK.
Definition: ut0counter.h:58
For counters where N=1.
Definition: ut0counter.h:94
static size_t get_rnd_index() 1
Definition: ut0counter.h:106
@ fast
Definition: ut0counter.h:97
static size_t offset(size_t index) 1
Definition: ut0counter.h:100
Version control for database, common definitions, and include files.
#define UNIV_NOTHROW
Definition: univ.i:455
#define UT_ARR_SIZE(a)
Definition: univ.i:523
constexpr uint32_t IB_N_SLOTS
Default number of slots to use in ib_counter_t.
Definition: ut0counter.h:50
#define default_indexer_t
Definition: ut0counter.h:112
Utilities related to CPU cache.
Debug utilities for Innobase.
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:68
int n
Definition: xcom_base.cc:508