Debian Codegraph @

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