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