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