MySQL 8.3.0
Source Code Documentation
large_page_alloc-solaris.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-solaris.h
28 Solaris-specific implementation bits and pieces for large (huge) page
29 allocations. */
30
31#ifndef detail_ut_large_page_alloc_solaris_h
32#define detail_ut_large_page_alloc_solaris_h
33
34#include <sys/mman.h>
35#include <sys/types.h>
36
37#include "mysqld_error.h"
40
41extern const size_t large_page_default_size;
42
43namespace ut {
44namespace detail {
45
46/** Allocates memory backed by large (huge) pages.
47
48 @param[in] n_bytes Size of storage (in bytes) requested to be allocated.
49 @return Pointer to the allocated storage. nullptr if allocation failed.
50*/
51inline void *large_page_aligned_alloc(size_t n_bytes) {
52 // mmap on Solaris requires for n_bytes to be a multiple of large-page size
53 size_t n_bytes_rounded = pow2_round(n_bytes + (large_page_default_size - 1),
55 void *ptr = mmap(nullptr, n_bytes_rounded, PROT_READ | PROT_WRITE,
56 MAP_PRIVATE | MAP_ANON, -1, 0);
57 if (unlikely(ptr == (void *)-1)) {
58 ib::log_warn(ER_IB_MSG_856)
59 << "large_page_aligned_alloc mmap(" << n_bytes_rounded
60 << " bytes) failed;"
61 " errno "
62 << errno;
63 }
64 // We also must do additional step to make it happen
65 struct memcntl_mha m = {};
66 m.mha_cmd = MHA_MAPSIZE_VA;
67 m.mha_pagesize = large_page_default_size;
68 int ret = memcntl(ptr, n_bytes_rounded, MC_HAT_ADVISE, (caddr_t)&m, 0, 0);
69 if (unlikely(ret == -1)) {
70 ib::log_warn(ER_IB_MSG_856)
71 << "large_page_aligned_alloc memcntl(ptr, " << n_bytes_rounded
72 << " bytes) failed;"
73 " errno "
74 << errno;
75 return nullptr;
76 }
77 return (ptr != (void *)-1) ? ptr : nullptr;
78}
79
80/** Releases memory backed by large (huge) pages.
81
82 @param[in] ptr Pointer to large (huge) page aligned storage.
83 @param[in] n_bytes Size of the storage.
84 @return Returns true if releasing the large (huge) page succeeded.
85 */
86inline bool large_page_aligned_free(void *ptr, size_t n_bytes) {
87 if (unlikely(!ptr)) return false;
88 // Freeing huge-pages require size to be the multiple of huge-page size
89 size_t n_bytes_rounded = pow2_round(n_bytes + (large_page_default_size - 1),
91 auto ret = munmap(ptr, n_bytes_rounded);
92 if (unlikely(ret != 0)) {
93 ib::log_error(ER_IB_MSG_858)
94 << "large_page_aligned_free munmap(" << ptr << ", " << n_bytes_rounded
95 << ") failed;"
96 " errno "
97 << errno;
98 }
99 return ret == 0;
100}
101
102/** Queries all possible page-sizes. Solaris allows picking one at _runtime_
103 which is contrary to how Linux, Windows and OSX does.
104
105 @return std::vector of supported page sizes (in bytes).
106*/
107inline std::vector<size_t> large_page_all_supported_sizes() {
108 int nr_of_pages = getpagesizes(NULL, 0);
109 std::vector<size_t> supported_page_sizes(nr_of_pages);
110 if (nr_of_pages > 0) {
111 getpagesizes(supported_page_sizes.data(), nr_of_pages);
112 }
113 return supported_page_sizes;
114}
115
116/** Queries the page-size that is next to the minimum supported page-size
117 Lowest supported page size is usually 4K on x86_64 whereas it's 8K on SPARC.
118
119 @return Minimum supported large (huge) page size in bytes.
120*/
121inline size_t large_page_size() { return large_page_all_supported_sizes()[1]; }
122
123} // namespace detail
124} // namespace ut
125
126#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
std::vector< size_t > large_page_all_supported_sizes()
Queries all possible page-sizes.
Definition: large_page_alloc-solaris.h:107
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
#define NULL
Definition: types.h:54
__caddr_t caddr_t
Definition: types.h:81
Base of InnoDB utilities.