MySQL 8.0.33
Source Code Documentation
atomics_array_index_padding.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2023, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef CONTAINER_ARRAY_ATOMICS_INDEX_PADDING
24#define CONTAINER_ARRAY_ATOMICS_INDEX_PADDING
25
26#include <algorithm>
27#include <atomic>
28#include <cmath>
29#include <map>
30#include <memory>
31#include <sstream>
32#include <thread>
33#include <tuple>
34
35#include "sql/memory/aligned_atomic.h" // memory::minimum_cacheline_for
36
37namespace container {
38
39/**
40 Indexing provider that pads each of the array elements to the size of the
41 CPU cache line, in order to avoid false sharing and cache misses.
42
43 In terms of the array size, this indexing provider will use force the use
44 of more memory than the needed to store the array elements of type
45 `T`. If the array is of size `n`, then instead of the allocated memory
46 being `n * sizeof(std::atomic<T>)`, it will be of size `n *
47 cache_line_size`. Since, typically, in modern systems, the cache line
48 size is 64 or 128 bytes, that would be an increase in the allocated
49 memory.
50
51 This class translates element-to-byte indexing as if each element is
52 aligned with the size of the CPU cache line. The CPU cache line size is
53 calculated at runtime.
54 */
55template <typename T>
57 public:
58 using type = std::atomic<T>;
59
60 /**
61 Constructor for the class that takes the maximum allowed number of
62 elements in the array.
63 */
64 Padded_indexing(size_t max_size);
65 /**
66 Class destructor.
67 */
68 virtual ~Padded_indexing() = default;
69 /**
70 The size of the array.
71
72 @return the size of the array
73 */
74 size_t size() const;
75 /**
76 Translates the element index to the byte position in the array.
77
78 @param index the element index to be translated.
79
80 @return the byte position in the byte array.
81 */
82 size_t translate(size_t index) const;
83 /**
84 The array element size, in bytes.
85
86 @return The array element size, in bytes.
87 */
88 static size_t element_size();
89
90 private:
91 /** The size of the array */
92 size_t m_size{0};
93 /** The size of the CPU cache line */
95};
96} // namespace container
97
98template <typename T>
100 : m_size{size},
101 m_cacheline_size{memory::minimum_cacheline_for<Padded_indexing::type>()} {
102}
103
104template <typename T>
106 return this->m_size;
107}
108
109template <typename T>
111 return index * this->m_cacheline_size;
112}
113
114template <typename T>
116 return memory::minimum_cacheline_for<Padded_indexing::type>();
117}
118
119#endif // CONTAINER_ARRAY_ATOMICS_INDEX_PADDING
Indexing provider that pads each of the array elements to the size of the CPU cache line,...
Definition: atomics_array_index_padding.h:56
size_t translate(size_t index) const
Translates the element index to the byte position in the array.
Definition: atomics_array_index_padding.h:110
Padded_indexing(size_t max_size)
Constructor for the class that takes the maximum allowed number of elements in the array.
Definition: atomics_array_index_padding.h:99
size_t size() const
The size of the array.
Definition: atomics_array_index_padding.h:105
size_t m_size
The size of the array.
Definition: atomics_array_index_padding.h:92
std::atomic< T > type
Definition: atomics_array_index_padding.h:58
static size_t element_size()
The array element size, in bytes.
Definition: atomics_array_index_padding.h:115
virtual ~Padded_indexing()=default
Class destructor.
size_t m_cacheline_size
The size of the CPU cache line.
Definition: atomics_array_index_padding.h:94
Definition: atomics_array.h:38
Definition: aligned_atomic.h:43
static size_t minimum_cacheline_for()
Retrieves the amount of bytes, multiple of the current cacheline size, needed to store an element of ...
Definition: aligned_atomic.h:132