MySQL 8.3.0
Source Code Documentation
large_page_alloc-osx.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2021, 2023, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/** @file include/detail/ut/large_page_alloc-osx.h
28 OSX-specific implementation bits and pieces for large (huge) page
29 allocations. */
30
31#ifndef detail_ut_large_page_alloc_osx_h
32#define detail_ut_large_page_alloc_osx_h
33
34#include <mach/vm_statistics.h>
35#include <sys/mman.h>
36#include <sys/types.h>
37
38#include "mysqld_error.h"
41
42extern const size_t large_page_default_size;
43
44namespace ut {
45namespace detail {
46
47/** Superpage size to be used (2MB). */
48static constexpr auto SUPER_PAGE_SIZE = VM_FLAGS_SUPERPAGE_SIZE_2MB;
49
50/** Allocates memory backed by large (huge) pages.
51
52 @param[in] n_bytes Size of storage (in bytes) requested to be allocated.
53 @return Pointer to the allocated storage. nullptr if allocation failed.
54*/
55inline void *large_page_aligned_alloc(size_t n_bytes) {
56 // mmap on OSX requires for n_bytes to be a multiple of large-page size
57 size_t n_bytes_rounded = pow2_round(n_bytes + (large_page_default_size - 1),
59 void *ptr = mmap(nullptr, n_bytes_rounded, PROT_READ | PROT_WRITE,
60 MAP_PRIVATE | MAP_ANON, SUPER_PAGE_SIZE, 0);
61 if (unlikely(ptr == (void *)-1)) {
62 ib::log_warn(ER_IB_MSG_856)
63 << "large_page_aligned_alloc mmap(" << n_bytes_rounded
64 << " bytes) failed;"
65 " errno "
66 << errno;
67 }
68 return (ptr != (void *)-1) ? ptr : nullptr;
69}
70
71/** Releases memory backed by large (huge) pages.
72
73 @param[in] ptr Pointer to large (huge) page aligned storage.
74 @param[in] n_bytes Size of the storage.
75 @return Returns true if releasing the large (huge) page succeeded.
76 */
77inline bool large_page_aligned_free(void *ptr, size_t n_bytes) {
78 if (unlikely(!ptr)) return false;
79 // Freeing huge-pages require size to be the multiple of huge-page size
80 size_t n_bytes_rounded = pow2_round(n_bytes + (large_page_default_size - 1),
82 auto ret = munmap(ptr, n_bytes_rounded);
83 if (unlikely(ret != 0)) {
84 ib::log_error(ER_IB_MSG_858)
85 << "large_page_aligned_free munmap(" << ptr << ", " << n_bytes_rounded
86 << ") failed;"
87 " errno "
88 << errno;
89 }
90 return ret == 0;
91}
92
93/** Queries the current size of large (huge) pages on running system.
94
95 @return Large (huge) page size in bytes.
96*/
97inline size_t large_page_size() {
98 // Return value of this function is hard-coded because of the way how
99 // large_page_aligned_alloc is allocating superpages
100 // (VM_FLAGS_SUPERPAGE_SIZE_2MB).
101 //
102 // Nonetheless we can make a compile-time check to detect situations when and
103 // if that value may change, in which case we can at least provide a
104 // guideline through static_assert message.
105 static_assert(
106 SUPER_PAGE_SIZE == VM_FLAGS_SUPERPAGE_SIZE_2MB,
107 "superpage size is not the one that has been expected (2MB). In case this \
108 is a wanted change, please tweak this static_assert _and_ modify this function \
109 to return appropriate new superpage size value.");
110 return 2 * 1024 * 1024;
111}
112
113} // namespace detail
114} // namespace ut
115
116#endif
Small helper functions.
const size_t large_page_default_size
System-default huge (large) page setting.
Definition: ut0new.cc:39
constexpr bool unlikely(bool expr)
Definition: my_compiler.h:57
Definition: ut0tuple.h:56
static auto log_warn()
Definition: ut0log.h:451
static auto log_error()
Definition: ut0log.h:452
constexpr size_t pow2_round(size_t n, size_t m)
Calculates the biggest multiple of m that is not bigger than n when m is a power of two.
Definition: helper.h:56
void * large_page_aligned_alloc(size_t n_bytes)
Allocates memory backed by large (huge) pages.
Definition: large_page_alloc-linux.h:51
static constexpr auto SUPER_PAGE_SIZE
Superpage size to be used (2MB).
Definition: large_page_alloc-osx.h:48
bool large_page_aligned_free(void *ptr, size_t n_bytes)
Releases memory backed by large (huge) pages.
Definition: large_page_alloc-linux.h:74
size_t large_page_size()
Queries the current size of large (huge) pages on running system.
Definition: large_page_alloc-linux.h:94
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:47
Base of InnoDB utilities.