1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*****************************************************************************
Copyright (c) 2012, 2015, Oracle and/or its affiliates. All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file include/ut0counter.h
Counter utility class
Created 2012/04/12 by Sunny Bains
*******************************************************/
#ifndef ut0counter_h
#define ut0counter_h
#include <my_rdtsc.h>
#include "univ.i"
#include "os0thread.h"
/** CPU cache line size */
#ifndef UNIV_HOTBACKUP
# ifdef CPU_LEVEL1_DCACHE_LINESIZE
# define CACHE_LINE_SIZE CPU_LEVEL1_DCACHE_LINESIZE
# else
# error CPU_LEVEL1_DCACHE_LINESIZE is undefined
# endif /* CPU_LEVEL1_DCACHE_LINESIZE */
#else
#ifdef powerpc
#define CACHE_LINE_SIZE 128
#else
# define CACHE_LINE_SIZE 64
#endif /* __powerpc__ */
#endif /* UNIV_HOTBACKUP */
/** Default number of slots to use in ib_counter_t */
#define IB_N_SLOTS 64
/** Get the offset into the counter array. */
template <typename Type, int N>
struct generic_indexer_t {
/** Default constructor/destructor should be OK. */
/** @return offset within m_counter */
static size_t offset(size_t index) UNIV_NOTHROW
{
return(((index % N) + 1) * (CACHE_LINE_SIZE / sizeof(Type)));
}
};
/** Use the result of my_timer_cycles(), which mainly uses RDTSC for cycles,
to index into the counter array. See the comments for my_timer_cycles() */
template <typename Type=ulint, int N=1>
struct counter_indexer_t : public generic_indexer_t<Type, N> {
/** Default constructor/destructor should be OK. */
enum { fast = 1 };
/** @return result from RDTSC or similar functions. */
static size_t get_rnd_index() UNIV_NOTHROW
{
size_t c = static_cast<size_t>(my_timer_cycles());
if (c != 0) {
return(c);
} else {
/* We may go here if my_timer_cycles() returns 0,
so we have to have the plan B for the counter. */
#if !defined(_WIN32)
return(size_t(os_thread_get_curr_id()));
#else
LARGE_INTEGER cnt;
QueryPerformanceCounter(&cnt);
return(static_cast<size_t>(cnt.QuadPart));
#endif /* !_WIN32 */
}
}
};
/** For counters where N=1 */
template <typename Type=ulint, int N=1>
struct single_indexer_t {
/** Default constructor/destructor should are OK. */
enum { fast = 0 };
/** @return offset within m_counter */
static size_t offset(size_t index) UNIV_NOTHROW
{
ut_ad(N == 1);
return((CACHE_LINE_SIZE / sizeof(Type)));
}
/** @return 1 */
static size_t get_rnd_index() UNIV_NOTHROW
{
ut_ad(N == 1);
return(1);
}
};
#define default_indexer_t counter_indexer_t
/** Class for using fuzzy counters. The counter is not protected by any
mutex and the results are not guaranteed to be 100% accurate but close
enough. Creates an array of counters and separates each element by the
CACHE_LINE_SIZE bytes */
template <
typename Type,
int N = IB_N_SLOTS,
template<typename, int> class Indexer = default_indexer_t>
class ib_counter_t {
public:
ib_counter_t() { memset(m_counter, 0x0, sizeof(m_counter)); }
~ib_counter_t()
{
ut_ad(validate());
}
static bool is_fast() { return(Indexer<Type, N>::fast); }
bool validate() UNIV_NOTHROW {
#ifdef UNIV_DEBUG
size_t n = (CACHE_LINE_SIZE / sizeof(Type));
/* Check that we aren't writing outside our defined bounds. */
for (size_t i = 0; i < UT_ARR_SIZE(m_counter); i += n) {
for (size_t j = 1; j < n - 1; ++j) {
ut_ad(m_counter[i + j] == 0);
}
}
#endif /* UNIV_DEBUG */
return(true);
}
/** If you can't use a good index id. Increment by 1. */
void inc() UNIV_NOTHROW { add(1); }
/** If you can't use a good index id.
@param n is the amount to increment */
void add(Type n) UNIV_NOTHROW {
size_t i = m_policy.offset(m_policy.get_rnd_index());
ut_ad(i < UT_ARR_SIZE(m_counter));
m_counter[i] += n;
}
/** Use this if you can use a unique identifier, saves a
call to get_rnd_index().
@param i index into a slot
@param n amount to increment */
void add(size_t index, Type n) UNIV_NOTHROW {
size_t i = m_policy.offset(index);
ut_ad(i < UT_ARR_SIZE(m_counter));
m_counter[i] += n;
}
/** If you can't use a good index id. Decrement by 1. */
void dec() UNIV_NOTHROW { sub(1); }
/** If you can't use a good index id.
@param n the amount to decrement */
void sub(Type n) UNIV_NOTHROW {
size_t i = m_policy.offset(m_policy.get_rnd_index());
ut_ad(i < UT_ARR_SIZE(m_counter));
m_counter[i] -= n;
}
/** Use this if you can use a unique identifier, saves a
call to get_rnd_index().
@param i index into a slot
@param n amount to decrement */
void sub(size_t index, Type n) UNIV_NOTHROW {
size_t i = m_policy.offset(index);
ut_ad(i < UT_ARR_SIZE(m_counter));
m_counter[i] -= n;
}
/* @return total value - not 100% accurate, since it is not atomic. */
operator Type() const UNIV_NOTHROW {
Type total = 0;
for (size_t i = 0; i < N; ++i) {
total += m_counter[m_policy.offset(i)];
}
return(total);
}
private:
/** Indexer into the array */
Indexer<Type, N>m_policy;
/** Slot 0 is unused. */
Type m_counter[(N + 1) * (CACHE_LINE_SIZE / sizeof(Type))];
};
#endif /* ut0counter_h */
|