MySQL 8.3.0
Source Code Documentation
large_page_alloc-linux.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-linux.h
28 Linux-specific implementation bits and pieces for large (huge) page
29 allocations. Also fallback for other platforms, e.g. FreeBSD. */
30
31#ifndef detail_ut_large_page_alloc_linux_h
32#define detail_ut_large_page_alloc_linux_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 will internally round n_bytes to the multiple of huge-page size if it
53 // is not already
54 int mmap_flags = MAP_PRIVATE | MAP_ANON;
55#ifndef __FreeBSD__
56 mmap_flags |= MAP_HUGETLB;
57#endif
58 void *ptr = mmap(nullptr, n_bytes, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
59 if (unlikely(ptr == (void *)-1)) {
60 ib::log_warn(ER_IB_MSG_856) << "large_page_aligned_alloc mmap(" << n_bytes
61 << " bytes) failed;"
62 " errno "
63 << errno;
64 }
65 return (ptr != (void *)-1) ? ptr : nullptr;
66}
67
68/** Releases memory backed by large (huge) pages.
69
70 @param[in] ptr Pointer to large (huge) page aligned storage.
71 @param[in] n_bytes Size of the storage.
72 @return Returns true if releasing the large (huge) page succeeded.
73 */
74inline bool large_page_aligned_free(void *ptr, size_t n_bytes) {
75 if (unlikely(!ptr)) return false;
76 // Freeing huge-pages require size to be the multiple of huge-page size
77 size_t n_bytes_rounded = pow2_round(n_bytes + (large_page_default_size - 1),
79 auto ret = munmap(ptr, n_bytes_rounded);
80 if (unlikely(ret != 0)) {
81 ib::log_error(ER_IB_MSG_858)
82 << "large_page_aligned_free munmap(" << ptr << ", " << n_bytes_rounded
83 << ") failed;"
84 " errno "
85 << errno;
86 }
87 return ret == 0;
88}
89
90/** Queries the current size of large (huge) pages on running system.
91
92 @return Large (huge) page size in bytes.
93*/
94inline size_t large_page_size() {
95 std::string key;
96 std::ifstream meminfo("/proc/meminfo");
97 if (meminfo) {
98 while (meminfo >> key) {
99 if (key == "Hugepagesize:") {
100 unsigned long long value;
101 meminfo >> value;
102 if (value == 0 && meminfo.fail()) return 0;
103 return value * 1024;
104 }
105 meminfo.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
106 }
107 }
108 return 0;
109}
110
111} // namespace detail
112} // namespace ut
113
114#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
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
required string key
Definition: replication_asynchronous_connection_failover.proto:59
Base of InnoDB utilities.