54template <
typename Type,
int N>
66template <
typename Type = ul
int,
int N = 1>
85 QueryPerformanceCounter(&cnt);
87 return static_cast<size_t>(cnt.QuadPart);
94template <
typename Type = ul
int,
int N = 1>
102 static_assert(
N == 1);
108 static_assert(
N == 1);
113#define default_indexer_t counter_indexer_t
127 static bool is_fast() {
return (Indexer<Type, N>::fast); }
135 for (
size_t j = 1; j <
n - 1; ++j) {
197 for (
size_t i = 0; i <
N; ++i) {
225using N = std::atomic<Type>;
228 "Atomic counter size > ut::INNODB_CACHE_LINE_SIZE");
246template <
size_t COUNT = 128>
266template <
size_t COUNT>
268 auto &shard_arr = shards.
m_arr;
271 return (shard_arr[
id % shard_arr.size()].m_n.fetch_add(
n, order));
279template <
size_t COUNT>
283 auto &shard_arr = shards.
m_arr;
285 return (shard_arr[
id % shard_arr.size()].m_n.fetch_sub(
n, order));
292template <
size_t COUNT>
294 return (
add(shards,
id, 1));
301template <
size_t COUNT>
303 return (
sub(shards,
id, 1));
310template <
size_t COUNT>
312 auto &shard_arr = shards.m_arr;
313 auto order = shards.m_memory_order;
315 return (shard_arr[
id % shard_arr.size()].m_n.load(order));
322template <
size_t COUNT>
324 for (
const auto &shard : shards.m_arr) {
332template <
size_t COUNT>
343template <
size_t COUNT>
345 for (
auto &shard : shards.m_arr) {
346 shard.m_n.store(0, shards.m_memory_order);
353template <
size_t COUNT>
357 dst.m_arr[i++].m_n.store(
count, dst.m_memory_order);
364template <
size_t COUNT>
368 dst.m_arr[i++].m_n.fetch_add(
count, dst.m_memory_order);
Class for using fuzzy counters.
Definition: ut0counter.h:121
static bool is_fast()
Definition: ut0counter.h:127
void sub(Type n) 1
If you can't use a good index id.
Definition: ut0counter.h:173
void add(Type n) 1
If you can't use a good index id.
Definition: ut0counter.h:148
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:160
ib_counter_t()
Definition: ut0counter.h:123
bool validate() 1
Definition: ut0counter.h:129
void dec() 1
If you can't use a good index id.
Definition: ut0counter.h:169
Type operator[](size_t index) const 1
Definition: ut0counter.h:204
Type m_counter[(N+1) *(ut::INNODB_CACHE_LINE_SIZE/sizeof(Type))]
Slot 0 is unused.
Definition: ut0counter.h:217
Indexer< Type, N > m_policy
Indexer into the array.
Definition: ut0counter.h:214
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:185
~ib_counter_t()
Definition: ut0counter.h:125
void inc() 1
If you can't use a good index id.
Definition: ut0counter.h:144
Multi-platform timer code.
ulonglong my_timer_cycles(void)
A cycle timer.
Definition: my_rdtsc.cc:98
static int count
Definition: myisam_ftdump.cc:43
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
std::function< void(const Type)> Function
Definition: ut0counter.h:241
uint64_t Type
Definition: ut0counter.h:223
Type add(Shards< COUNT > &shards, size_t id, size_t n)
Increment the counter for a shard by n.
Definition: ut0counter.h:267
byte[ut::INNODB_CACHE_LINE_SIZE - sizeof(N)] Pad
Definition: ut0counter.h:230
void copy(Shards< COUNT > &dst, const Shards< COUNT > &src) noexcept
Copy the counters, overwrite destination.
Definition: ut0counter.h:354
constexpr auto Memory_order
Relaxed order by default.
Definition: ut0counter.h:244
Type dec(Shards< COUNT > &shards, size_t id)
Decrement the counter of a shard by 1.
Definition: ut0counter.h:302
Type total(const Shards< COUNT > &shards) noexcept
Get the total value of all shards.
Definition: ut0counter.h:333
Type sub(Shards< COUNT > &shards, size_t id, size_t n)
Decrement the counter for a shard by n.
Definition: ut0counter.h:280
void for_each(const Shards< COUNT > &shards, Function &&f) noexcept
Iterate over the shards.
Definition: ut0counter.h:323
std::atomic< Type > N
Definition: ut0counter.h:225
void clear(Shards< COUNT > &shards) noexcept
Clear the counter - reset to 0.
Definition: ut0counter.h:344
Type get(const Shards< COUNT > &shards, size_t id) noexcept
Get the counter value for a shard.
Definition: ut0counter.h:311
constexpr size_t INNODB_CACHE_LINE_SIZE
CPU cache line size.
Definition: ut0cpu_cache.h:41
const thread_local size_t this_thread_hash
The hash value of the current thread's id.
Definition: os0thread.h:74
The interface to the operating system process and thread control primitives.
Counter shard.
Definition: ut0counter.h:233
N m_n
Sharded counter.
Definition: ut0counter.h:238
Pad m_pad
Separate on cache line.
Definition: ut0counter.h:235
Definition: ut0counter.h:247
void set_order(std::memory_order memory_order)
Override default memory order.
Definition: ut0counter.h:256
std::array< Shard, COUNT > m_arr
Definition: ut0counter.h:249
std::memory_order m_memory_order
Definition: ut0counter.h:252
Use the result of my_timer_cycles(), which mainly uses RDTSC for cycles, to index into the counter ar...
Definition: ut0counter.h:67
static size_t get_rnd_index() 1
Definition: ut0counter.h:73
@ fast
Definition: ut0counter.h:70
Get the offset into the counter array.
Definition: ut0counter.h:55
static size_t offset(size_t index) 1
Default constructor/destructor should be OK.
Definition: ut0counter.h:59
For counters where N=1.
Definition: ut0counter.h:95
static size_t get_rnd_index() 1
Definition: ut0counter.h:107
@ fast
Definition: ut0counter.h:98
static size_t offset(size_t index) 1
Definition: ut0counter.h:101
Version control for database, common definitions, and include files.
#define UNIV_NOTHROW
Definition: univ.i:456
#define UT_ARR_SIZE(a)
Definition: univ.i:524
constexpr uint32_t IB_N_SLOTS
Default number of slots to use in ib_counter_t.
Definition: ut0counter.h:51
#define default_indexer_t
Definition: ut0counter.h:113
Utilities related to CPU cache.
Debug utilities for Innobase.
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:69
int n
Definition: xcom_base.cc:509