InnoDB Plugin  1.0
ut0counter.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 2012, Oracle and/or its affiliates. All Rights Reserved.
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 as published by the Free Software
7 Foundation; version 2 of the License.
8 
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
16 
17 *****************************************************************************/
18 
19 /**************************************************/
27 #ifndef UT0COUNTER_H
28 #define UT0COUNTER_H
29 
30 #include "univ.i"
31 #include <string.h>
32 #include "os0thread.h"
33 
35 #define CACHE_LINE_SIZE 64
36 
38 #define IB_N_SLOTS 64
39 
41 template <typename Type, int N>
46  size_t offset(size_t index) const UNIV_NOTHROW {
47  return(((index % N) + 1) * (CACHE_LINE_SIZE / sizeof(Type)));
48  }
49 };
50 
51 #ifdef HAVE_SCHED_GETCPU
52 #include <utmpx.h>
55 template <typename Type, int N>
56 struct get_sched_indexer_t : public generic_indexer_t<Type, N> {
59  /* @return result from sched_getcpu(), the thread id if it fails. */
60  size_t get_rnd_index() const UNIV_NOTHROW {
61 
62  size_t cpu = sched_getcpu();
63  if (cpu == -1) {
64  cpu = (lint) os_thread_get_curr_id();
65  }
66 
67  return(cpu);
68  }
69 };
70 #endif /* HAVE_SCHED_GETCPU */
71 
73 template <typename Type, int N>
74 struct thread_id_indexer_t : public generic_indexer_t<Type, N> {
77  /* @return a random number, currently we use the thread id. Where
78  thread id is represented as a pointer, it may not work as
79  effectively. */
80  size_t get_rnd_index() const UNIV_NOTHROW {
81  return((lint) os_thread_get_curr_id());
82  }
83 };
84 
86 template <typename Type, int N=1>
91  size_t offset(size_t index) const UNIV_NOTHROW {
92  ut_ad(N == 1);
93  return((CACHE_LINE_SIZE / sizeof(Type)));
94  }
95 
96  /* @return 1 */
97  size_t get_rnd_index() const UNIV_NOTHROW {
98  ut_ad(N == 1);
99  return(1);
100  }
101 };
102 
107 template <
108  typename Type,
109  int N = IB_N_SLOTS,
110  template<typename, int> class Indexer = thread_id_indexer_t>
112 public:
113  ib_counter_t() { memset(m_counter, 0x0, sizeof(m_counter)); }
114 
115  ~ib_counter_t()
116  {
117  ut_ad(validate());
118  }
119 
120  bool validate() UNIV_NOTHROW {
121 #ifdef UNIV_DEBUG
122  size_t n = (CACHE_LINE_SIZE / sizeof(Type));
123 
124  /* Check that we aren't writing outside our defined bounds. */
125  for (size_t i = 0; i < UT_ARR_SIZE(m_counter); i += n) {
126  for (size_t j = 1; j < n - 1; ++j) {
127  ut_ad(m_counter[i + j] == 0);
128  }
129  }
130 #endif /* UNIV_DEBUG */
131  return(true);
132  }
133 
135  void inc() UNIV_NOTHROW { add(1); }
136 
139  void add(Type n) UNIV_NOTHROW {
140  size_t i = m_policy.offset(m_policy.get_rnd_index());
141 
142  ut_ad(i < UT_ARR_SIZE(m_counter));
143 
144  m_counter[i] += n;
145  }
146 
151  void add(size_t index, Type n) UNIV_NOTHROW {
152  size_t i = m_policy.offset(index);
153 
154  ut_ad(i < UT_ARR_SIZE(m_counter));
155 
156  m_counter[i] += n;
157  }
158 
160  void dec() UNIV_NOTHROW { sub(1); }
161 
164  void sub(Type n) UNIV_NOTHROW {
165  size_t i = m_policy.offset(m_policy.get_rnd_index());
166 
167  ut_ad(i < UT_ARR_SIZE(m_counter));
168 
169  m_counter[i] -= n;
170  }
171 
176  void sub(size_t index, Type n) UNIV_NOTHROW {
177  size_t i = m_policy.offset(index);
178 
179  ut_ad(i < UT_ARR_SIZE(m_counter));
180 
181  m_counter[i] -= n;
182  }
183 
184  /* @return total value - not 100% accurate, since it is not atomic. */
185  operator Type() const UNIV_NOTHROW {
186  Type total = 0;
187 
188  for (size_t i = 0; i < N; ++i) {
189  total += m_counter[m_policy.offset(i)];
190  }
191 
192  return(total);
193  }
194 
195 private:
197  Indexer<Type, N>m_policy;
198 
200  Type m_counter[(N + 1) * (CACHE_LINE_SIZE / sizeof(Type))];
201 };
202 
203 #endif /* UT0COUNTER_H */