MySQL 8.0.39
Source Code Documentation
atomics_array_index_interleaved.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_INTERLEAVED
25#define CONTAINER_ARRAY_ATOMICS_INDEX_INTERLEAVED
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 interleaves sequentially stored array elements in
42 order to keep them from being pulled to the same cache line, in order to
43 avoid false sharing and cache misses.
44
45 However, false sharing is only avoided for particular access patterns,
46 namely, when it is heuristically unlikely (or impossible) that concurrent
47 threads access array elements that are far apart.
48
49 The array layout is as follows. When each cache line has capacity for C
50 array elements, the array is sliced into C sub-arrays. The sub-arrays are
51 stored in an interleaved manner, such that the i'th sub-array uses the
52 i'th element within each cache line. For instance, if the machine uses
53 128 byte cache lines, and an array has 6 elements, and each elements uses
54 64 bytes, the array will be laid out as follows:
55
56 | byte position | element number | cache line # |
57 | 0 | 0 | 0 |
58 | 64 | 3 | 0 |
59 | 128 | 1 | 1 |
60 | 192 | 4 | 1 |
61 | 256 | 2 | 2 |
62 | 320 | 5 | 2 |
63
64 This class translates element-to-byte indexing as if each consecutive
65 array index has CPU cache line bytes between them, hence, interleaving
66 consecutive array indexes. The CPU cache line size is calculated at
67 runtime.
68 */
69template <typename T>
71 public:
72 using type = std::atomic<T>;
73
74 /**
75 Constructor for the class that takes the number of elements in the
76 array. This value may be increased in order to address CPU cache line
77 alignment.
78 */
79 Interleaved_indexing(size_t max_size);
80 /**
81 Class destructor.
82 */
83 virtual ~Interleaved_indexing() = default;
84 /**
85 The size of the array.
86
87 @return the size of the array
88 */
89 size_t size() const;
90 /**
91 Translates the element index to the byte position in the array.
92
93 @param index the element index to be translated.
94
95 @return the byte position in the byte array.
96 */
97 size_t translate(size_t index) const;
98 /**
99 The array element size, in bytes.
100
101 @return The array element size, in bytes.
102 */
103 static size_t element_size();
104
105 private:
106 /** The array size */
107 size_t m_size{0};
108 /** The size of the CPU cache line */
110 /** The number of array elements that fit a cache line */
111 size_t m_page_size{0};
112 /** The number of cache lines that fit in the byte array */
113 size_t m_pages{0};
114};
115} // namespace container
116
117template <typename T>
119 : m_cacheline_size{memory::minimum_cacheline_for<
121 m_page_size{m_cacheline_size / sizeof(Interleaved_indexing::type)},
122 m_pages{std::max(static_cast<size_t>(1),
123 (size + m_page_size - 1) / m_page_size)} {
124 this->m_size = this->m_pages * this->m_page_size;
125}
126
127template <typename T>
129 return this->m_size;
130}
131
132template <typename T>
134 return (((index % this->m_pages) * this->m_page_size) +
135 (index / this->m_pages)) *
137}
138
139template <typename T>
141 return sizeof(Interleaved_indexing::type);
142}
143
144#endif // CONTAINER_ARRAY_ATOMICS_INDEX_INTERLEAVED
Indexing provider that interleaves sequentially stored array elements in order to keep them from bein...
Definition: atomics_array_index_interleaved.h:70
size_t m_page_size
The number of array elements that fit a cache line.
Definition: atomics_array_index_interleaved.h:111
Interleaved_indexing(size_t max_size)
Constructor for the class that takes the number of elements in the array.
Definition: atomics_array_index_interleaved.h:118
size_t translate(size_t index) const
Translates the element index to the byte position in the array.
Definition: atomics_array_index_interleaved.h:133
size_t m_size
The array size.
Definition: atomics_array_index_interleaved.h:107
size_t m_cacheline_size
The size of the CPU cache line.
Definition: atomics_array_index_interleaved.h:109
size_t m_pages
The number of cache lines that fit in the byte array.
Definition: atomics_array_index_interleaved.h:113
static size_t element_size()
The array element size, in bytes.
Definition: atomics_array_index_interleaved.h:140
std::atomic< T > type
Definition: atomics_array_index_interleaved.h:72
virtual ~Interleaved_indexing()=default
Class destructor.
size_t size() const
The size of the array.
Definition: atomics_array_index_interleaved.h:128
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
Definition: gcs_xcom_synode.h:64