1 /*****************************************************************************
2 
3 Copyright (c) 2012, 2015, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2017, 2019, MariaDB Corporation.
5 
6 This program is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free Software
8 Foundation; version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
17 
18 *****************************************************************************/
19 
20 /**************************************************//**
21 @file include/ut0counter.h
22 
23 Counter utility class
24 
25 Created 2012/04/12 by Sunny Bains
26 *******************************************************/
27 
28 #ifndef ut0counter_h
29 #define ut0counter_h
30 
31 #include "os0thread.h"
32 #include "my_rdtsc.h"
33 
34 /** CPU cache line size */
35 #ifdef CPU_LEVEL1_DCACHE_LINESIZE
36 # define CACHE_LINE_SIZE	CPU_LEVEL1_DCACHE_LINESIZE
37 #else
38 # error CPU_LEVEL1_DCACHE_LINESIZE is undefined
39 #endif /* CPU_LEVEL1_DCACHE_LINESIZE */
40 
41 /** Default number of slots to use in ib_counter_t */
42 #define IB_N_SLOTS		64
43 
44 /** Use the result of my_timer_cycles(), which mainly uses RDTSC for cycles
45 as a random value. See the comments for my_timer_cycles() */
46 /** @return result from RDTSC or similar functions. */
47 static inline size_t
get_rnd_value()48 get_rnd_value()
49 {
50 	size_t c = static_cast<size_t>(my_timer_cycles());
51 
52 	if (c != 0) {
53 		return c;
54 	}
55 
56 	/* We may go here if my_timer_cycles() returns 0,
57 	so we have to have the plan B for the counter. */
58 #if !defined(_WIN32)
59 	return (size_t)os_thread_get_curr_id();
60 #else
61 	LARGE_INTEGER cnt;
62 	QueryPerformanceCounter(&cnt);
63 
64 	return static_cast<size_t>(cnt.QuadPart);
65 #endif /* !_WIN32 */
66 }
67 
68 /** Class for using fuzzy counters. The counter is multi-instance relaxed atomic
69 so the results are not guaranteed to be 100% accurate but close
70 enough. Creates an array of counters and separates each element by the
71 CACHE_LINE_SIZE bytes */
72 template <typename Type, int N = IB_N_SLOTS>
73 struct ib_counter_t {
74 	/** Increment the counter by 1. */
incib_counter_t75 	void inc() { add(1); }
76 
77 	/** Increment the counter by 1.
78 	@param[in]	index	a reasonably thread-unique identifier */
incib_counter_t79 	void inc(size_t index) { add(index, 1); }
80 
81 	/** Add to the counter.
82 	@param[in]	n	amount to be added */
addib_counter_t83 	void add(Type n) { add(get_rnd_value(), n); }
84 
85 	/** Add to the counter.
86 	@param[in]	index	a reasonably thread-unique identifier
87 	@param[in]	n	amount to be added */
addib_counter_t88 	void add(size_t index, Type n) {
89 		index = index % N;
90 
91 		ut_ad(index < UT_ARR_SIZE(m_counter));
92 
93 		m_counter[index].value.fetch_add(n, std::memory_order_relaxed);
94 	}
95 
96 	/* @return total value - not 100% accurate, since it is relaxed atomic*/
Typeib_counter_t97 	operator Type() const {
98 		Type	total = 0;
99 
100 		for (const auto &counter : m_counter) {
101 			total += counter.value.load(std::memory_order_relaxed);
102 		}
103 
104 		return(total);
105 	}
106 
107 private:
108 	/** Atomic which occupies whole CPU cache line.
109 	Note: We rely on the default constructor of std::atomic and
110 	do not explicitly initialize the contents. This works for us,
111 	because ib_counter_t is only intended for usage with global
112 	memory that is allocated from the .bss and thus guaranteed to
113 	be zero-initialized by the run-time environment.
114 	@see srv_stats
115 	@see rw_lock_stats */
116 	struct ib_counter_element_t {
117 		MY_ALIGNED(CACHE_LINE_SIZE) std::atomic<Type> value;
118 	};
119 	static_assert(sizeof(ib_counter_element_t) == CACHE_LINE_SIZE, "");
120 
121 	/** Array of counter elements */
122 	MY_ALIGNED(CACHE_LINE_SIZE) ib_counter_element_t m_counter[N];
123 };
124 
125 #endif /* ut0counter_h */
126