Debian Codegraph @

storage/innobase/include/detail/ut/large_page_alloc.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2021, 2026, Oracle and/or its affiliates.
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, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/detail/ut/large_page_alloc.h
29 Implementation bits and pieces for large (huge) page allocations. */
30
31 #ifndef detail_ut_large_page_alloc_h
32 #define detail_ut_large_page_alloc_h
33
34 #include <cstddef>
35 #include <fstream>
36
37 #include "my_compiler.h"
38 #ifdef _WIN32
39 #include "storage/innobase/include/detail/ut/large_page_alloc-win.h"
40 #elif defined(__APPLE__)
41 #include "storage/innobase/include/detail/ut/large_page_alloc-osx.h"
42 #elif defined(__sun)
43 #include "storage/innobase/include/detail/ut/large_page_alloc-solaris.h"
44 #else
45 #include "storage/innobase/include/detail/ut/large_page_alloc-linux.h"
46 #endif
47 #include "storage/innobase/include/detail/ut/allocator_traits.h"
48 #include "storage/innobase/include/detail/ut/helper.h"
49 #include "storage/innobase/include/detail/ut/page_metadata.h"
50 #include "storage/innobase/include/detail/ut/pfs.h"
51
52 extern const size_t large_page_default_size;
53
54 namespace ut {
55 namespace detail {
56
57 /** Allocation routines which are purposed for allocating memory through the
58 means of what is known as large (huge) pages.
59
60 large_page_aligned_alloc() and large_page_aligned_free() are taking care of
61 OS specific details and Large_page_alloc is a convenience wrapper which only
62 makes the use of large pages more ergonomic so that it serializes the actual
63 size being allocated into the raw memory. This size is then automagically
64 deduced when large page memory is being freed. Otherwise, client code would
65 have been responsible to store and keep that value somewhere until it frees
66 the large page memory segment. Additionally, information on type of page
67 used to back up requested allocation is also serialized into the memory
68 allowing to build higher-kinded abstractions more easily. See
69 ut::malloc_large_page with option to fallback to regular pages through
70 ut::malloc_page.
71
72 Cost associated with this abstraction is the size of a single CPU page. In
73 terms of virtual memory, especially in 64-bit address space, this cost is
74 negligible. In practice this means that for each N huge-page sized
75 allocation request, application code will get to use CPU_PAGE_SIZE bytes
76 less. In other words, for a request that is backed up by three 2MiB
77 huge-pages, application code will get to use 3 * 2MiB - CPU_PAGE_SIZE of
78 total bytes. CPU_PAGE_SIZE is normally 4K but some architectures such as
79 SPARC have it set to 8K. ARM64 can be set to 4K, 8K or 64K.
80
81 Memory layout representation looks like the following:
82
83 -------------------------------------------
84 | PAGE-ALLOC-METADATA | ... DATA ... |
85 -------------------------------------------
86 ^ ^
87 | |
88 | |
89 | ptr (large-page) to be returned to call-site
90 |
91 --------------------------------
92 | DATALEN | PAGE-TYPE | VARLEN |
93 --------------------------------
94 \ \
95 0 \
96 CPU_PAGE_SIZE - 1
97
98 For details on DATALEN, PAGE-TYPE and VARLEN fields see Page_alloc_metadata.
99
100 DATA is an actual page-aligned (!) segment backed by large (huge) page
101 memory that will be returned to the call-site and which the client code will
102 be able to use for the application data.
103 */
104 struct Large_page_alloc : public allocator_traits<false> {
105 using page_allocation_metadata = Page_alloc_metadata;
106
107 /** Allocates memory through large-page support.
108
109 @param[in] size Size of storage (in bytes) requested to be allocated.
110 @return Pointer to the allocated storage. nullptr if allocation failed.
111 */
112 static inline void *alloc(std::size_t size) {
113 auto total_len = round_to_next_multiple(
114 size + page_allocation_metadata::len, large_page_default_size);
115 auto mem = large_page_aligned_alloc(total_len);
116 if (unlikely(!mem)) return nullptr;
117 page_allocation_metadata::datalen(mem, total_len);
118 page_allocation_metadata::page_type(mem, Page_type::large_page);
119 return static_cast<uint8_t *>(mem) + page_allocation_metadata::len;
120 }
121
122 /** Releases storage allocated through
123 Large_page_alloc::alloc().
124
125 @param[in] data Pointer to storage allocated through
126 Large_page_alloc::alloc()
127 @return True if releasing the memory was successful.
128 */
129 static inline bool free(void *data) noexcept {
130 if (unlikely(!data)) return false;
131 ut_ad(page_type(data) == Page_type::large_page);
132 return large_page_aligned_free(deduce(data),
133 page_allocation_metadata::datalen(data));
134 }
135
136 /** Returns the number of bytes that have been allocated.
137
138 @param[in] data Pointer to storage allocated through
139 Large_page_alloc::alloc()
140 @return Number of bytes.
141 */
142 static inline page_allocation_metadata::datalen_t datalen(void *data) {
143 ut_ad(page_type(data) == Page_type::large_page);
144 return page_allocation_metadata::datalen(data) -
145 page_allocation_metadata::len;
146 }
147
148 /** Returns the type of the page.
149
150 @param[in] data Pointer to storage allocated through
151 Large_page_alloc::alloc()
152 @return Page type.
153 */
154 static inline Page_type page_type(void *data) {
155 return page_allocation_metadata::page_type(data);
156 }
157
158 /** Retrieves the pointer and size of the allocation provided by the OS. It is
159 a low level information, and is needed only to call low level
160 memory-related OS functions.
161
162 @param[in] data Pointer to storage allocated through
163 Large_page_alloc::alloc()
164 @return Low level allocation info.
165 */
166 static inline allocation_low_level_info low_level_info(void *data) {
167 ut_ad(page_type(data) == Page_type::large_page);
168 return {deduce(data), page_allocation_metadata::datalen(data)};
169 }
170
171 private:
172 /** Helper function which deduces the original pointer returned by
173 Large_page_alloc from a pointer which is passed to us by the call-site.
174 */
175 static inline void *deduce(void *data) noexcept {
176 ut_ad(page_type(data) == Page_type::large_page);
177 const auto res = reinterpret_cast<void *>(static_cast<uint8_t *>(data) -
178 page_allocation_metadata::len);
179 ut_ad(reinterpret_cast<std::uintptr_t>(res) % large_page_size() == 0);
180 return res;
181 }
182 };
183
184 /** Allocation routines which are purposed for allocating memory through the
185 means of what is known as large (huge) pages. This is a PFS
186 (performance-schema) variant of Large_page_alloc. Implemented in terms
187 of Page_alloc_metadata_pfs.
188
189 large_page_aligned_alloc() and large_page_aligned_free() are taking care of
190 OS specific details and Large_page_alloc is a convenience wrapper which only
191 makes the use of large pages more ergonomic so that it serializes all the
192 relevant PFS details into the raw memory. Otherwise, client code would have
193 been responsible to store and keep those details somewhere until the memory
194 segment is freed. Additionally, information on type of page used to back up
195 requested allocation is also serialized into the memory allowing to build
196 higher-kinded abstractions more easily. See ut::malloc_large_page with
197 option to fallback to regular pages through ut::malloc_page.
198
199 Cost associated with this abstraction is the size of a single CPU page. In
200 terms of virtual memory, especially in 64-bit address space, this cost is
201 negligible. In practice this means that for each N huge-page sized
202 allocation request, application code will get to use CPU_PAGE_SIZE bytes
203 less. In other words, for a request that is backed up by three 2MiB
204 huge-pages, application code will get to use 3 * 2MiB - CPU_PAGE_SIZE of
205 total bytes. CPU_PAGE_SIZE is normally 4K but some architectures such as
206 SPARC have it set to 8K. ARM64 can be set to 4K, 8K or 64K.
207
208 Memory layout representation looks like the following:
209
210 ----------------------------------------------
211 | PAGE-ALLOC-METADATA-PFS | ... DATA ... |
212 ----------------------------------------------
213 ^ ^
214 | |
215 | |
216 | ptr (large-page) to be
217 | returned to call-site
218 |
219 ---------------------------------------------------
220 | PFS-META | PAGE-TYPE | VARLEN | PFS-META-OFFSET |
221 ---------------------------------------------------
222 ^ ^
223 | |
224 | ---------------------------
225 | | OWNER | DATALEN | KEY |
226 | ---------------------------
227 |
228 ptr returned by
229 large_page_aligned_alloc
230
231 For details on PFS-META, PAGE-TYPE, VARLEN and PFS-META-OFFSET fields
232 see Page_alloc_metadata_pfs.
233
234 DATA is an actual page-aligned (!) segment backed by large (huge) page
235 memory that will be returned to the call-site and which the client code will
236 be able to use for the application data.
237 */
238 struct Large_page_alloc_pfs : public allocator_traits<true> {
239 using page_allocation_metadata = Page_alloc_metadata_pfs;
240
241 /** Allocates memory through large-page support.
242
243 @param[in] size Size of storage (in bytes) requested to be allocated.
244 @param[in] key PSI memory key to be used for PFS memory instrumentation.
245 @return Pointer to the allocated storage. nullptr if allocation failed.
246 */
247 static inline void *alloc(
248 std::size_t size,
249 page_allocation_metadata::pfs_metadata::pfs_memory_key_t key) {
250 auto total_len = round_to_next_multiple(
251 size + page_allocation_metadata::len, large_page_default_size);
252 auto mem = large_page_aligned_alloc(total_len);
253 if (unlikely(!mem)) return nullptr;
254
255 #ifdef HAVE_PSI_MEMORY_INTERFACE
256 // The point of this allocator variant is to trace the memory allocations
257 // through PFS (PSI) so do it.
258 page_allocation_metadata::pfs_metadata::pfs_owning_thread_t owner;
259 key = PSI_MEMORY_CALL(memory_alloc)(key, total_len, &owner);
260 // To be able to do the opposite action of tracing when we are releasing the
261 // memory, we need right about the same data we passed to the tracing
262 // memory_alloc function. Let's encode this it into our allocator so we
263 // don't have to carry and keep this data around.
264 page_allocation_metadata::pfs_metadata::pfs_owning_thread(mem, owner);
265 page_allocation_metadata::pfs_metadata::pfs_datalen(mem, total_len);
266 page_allocation_metadata::pfs_metadata::pfs_key(mem, key);
267 page_allocation_metadata::pfs_metadata::pfs_metaoffset(
268 mem, page_allocation_metadata::len);
269 page_allocation_metadata::page_type(mem, Page_type::large_page);
270 #endif
271
272 return static_cast<uint8_t *>(mem) + page_allocation_metadata::len;
273 }
274
275 /** Releases storage allocated through Large_page_alloc_pfs::alloc().
276
277 @param[in] data Pointer to storage allocated through
278 Large_page_alloc_pfs::alloc()
279 @return True if releasing the memory was successful.
280 */
281 static inline bool free(PFS_metadata::data_segment_ptr data) noexcept {
282 if (unlikely(!data)) return false;
283 ut_ad(page_type(data) == Page_type::large_page);
284
285 PFS_metadata::pfs_datalen_t total_len = {};
286 #ifdef HAVE_PSI_MEMORY_INTERFACE
287 // Deduce the PFS data we encoded in Large_page_alloc_pfs::alloc()
288 auto key = page_allocation_metadata::pfs_metadata::pfs_key(data);
289 auto owner =
290 page_allocation_metadata::pfs_metadata::pfs_owning_thread(data);
291 total_len = page_allocation_metadata::pfs_metadata::pfs_datalen(data);
292 // With the deduced PFS data, now trace the memory release action.
293 PSI_MEMORY_CALL(memory_free)
294 (key, total_len, owner);
295 #endif
296
297 return large_page_aligned_free(deduce(data), total_len);
298 }
299
300 /** Returns the number of bytes that have been allocated.
301
302 @param[in] data Pointer to storage allocated through
303 Large_page_alloc_pfs::alloc()
304 @return Number of bytes.
305 */
306 static inline size_t datalen(PFS_metadata::data_segment_ptr data) {
307 ut_ad(page_type(data) == Page_type::large_page);
308 return page_allocation_metadata::pfs_metadata::pfs_datalen(data) -
309 page_allocation_metadata::len;
310 }
311
312 /** Returns the Page_type.
313
314 @param[in] data Pointer to storage allocated through
315 Large_page_alloc_pfs::alloc()
316 @return Page type.
317 */
318 static inline Page_type page_type(PFS_metadata::data_segment_ptr data) {
319 return page_allocation_metadata::page_type(data);
320 }
321
322 /** Retrieves the pointer and size of the allocation provided by the OS. It is
323 a low level information, and is needed only to call low level
324 memory-related OS functions.
325
326 @param[in] data Pointer to storage allocated through
327 Large_page_alloc_pfs::alloc()
328 @return Low level allocation info.
329 */
330 static inline allocation_low_level_info low_level_info(void *data) {
331 ut_ad(page_type(data) == Page_type::large_page);
332 return {deduce(data),
333 page_allocation_metadata::pfs_metadata::pfs_datalen(data)};
334 }
335
336 private:
337 /** Helper function which deduces the original pointer returned by
338 Large_page_alloc_pfs from a pointer which is passed to us by the
339 call-site.
340 */
341 static inline void *deduce(PFS_metadata::data_segment_ptr data) noexcept {
342 ut_ad(page_type(data) == Page_type::large_page);
343 const auto res =
344 page_allocation_metadata::pfs_metadata::deduce_pfs_meta(data);
345 ut_ad(reinterpret_cast<std::uintptr_t>(res) % large_page_size() == 0);
346 return res;
347 }
348 };
349
350 /** Simple utility metafunction which selects appropriate allocator variant
351 (implementation) depending on the input parameter(s).
352 */
353 template <bool Pfs_memory_instrumentation_on>
354 struct select_large_page_alloc_impl {
355 using type =
356 Large_page_alloc; // When PFS is OFF, pick ordinary, non-PFS, variant
357 };
358
359 template <>
360 struct select_large_page_alloc_impl<true> {
361 using type = Large_page_alloc_pfs; // Otherwise, pick PFS variant
362 };
363
364 /** Just a small helper type which saves us some keystrokes. */
365 template <bool Pfs_memory_instrumentation_on>
366 using select_large_page_alloc_impl_t =
367 typename select_large_page_alloc_impl<Pfs_memory_instrumentation_on>::type;
368
369 /** Small wrapper which utilizes SFINAE to dispatch the call to appropriate
370 aligned allocator implementation.
371 */
372 template <typename Impl>
373 struct Large_alloc_ {
374 template <typename T = Impl>
375 static inline typename std::enable_if<T::is_pfs_instrumented_v, void *>::type
376 alloc(size_t size, PSI_memory_key key) {
377 return Impl::alloc(size, key);
378 }
379 template <typename T = Impl>
380 static inline typename std::enable_if<!T::is_pfs_instrumented_v, void *>::type
381 alloc(size_t size, PSI_memory_key /*key*/) {
382 return Impl::alloc(size);
383 }
384 static inline bool free(void *ptr) { return Impl::free(ptr); }
385 static inline size_t datalen(void *ptr) { return Impl::datalen(ptr); }
386 static inline Page_type page_type(void *ptr) { return Impl::page_type(ptr); }
387 static inline allocation_low_level_info low_level_info(void *ptr) {
388 return Impl::low_level_info(ptr);
389 }
390 };
391
392 } // namespace detail
393 } // namespace ut
394
395 #endif