Debian Codegraph @

storage/innobase/include/detail/ut/pfs.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/pfs.h
29 Implementation bits and pieces for PFS metadata handling. Shared by
30 different allocators.
31 */
32
33 #ifndef detail_ut_pfs_h
34 #define detail_ut_pfs_h
35
36 #include <cassert>
37 #include <cstddef>
38 #include <cstdint>
39 #include <cstdlib>
40 #include <limits>
41 #include <memory>
42 #include <utility>
43
44 #include "mysql/psi/mysql_memory.h"
45
46 namespace ut {
47 namespace detail {
48
49 /** Memory layout representation of PFS metadata segment that is used by
50 the allocator variants which also want to trace the memory consumption
51 through PFS (PSI) interface.
52
53 --------------------------------------------------
54 | PFS-META | VARLEN | PFS-META-OFFSET | DATA |
55 --------------------------------------------------
56 ^ ^ ^
57 | | |
58 | --------------------------- |
59 | | OWNER | DATALEN | KEY | |
60 | --------------------------- |
61 | |
62 ptr returned by |
63 Aligned_alloc_impl |
64 |
65 ptr to be returned to call-site
66 will be pointing here
67
68 PFS-META is a segment that will hold all the necessary details one needs
69 to otherwise carry around in order to exercise the PFS memory tracing.
70 Following data will be serialized into this segment:
71 * Owning thread
72 * Total length of bytes allocated
73 * Key
74
75 VARLEN is the leftover variable-length segment that specialized
76 implementations can further make use of by deducing its size from the
77 following formulae: requested_alignment - sizeof(PFS-META-OFFSET) -
78 sizeof(PFS-META). In code that would be alignment -
79 PFS_metadata::size. Not used by this implementation.
80
81 PFS-META-OFFSET is a field which allows us to recover the pointer to PFS-META
82 segment from a pointer to DATA segment.
83
84 DATA is an actual segment which will keep the user data.
85 */
86 struct PFS_metadata {
87 /** Convenience types that we will be using to serialize necessary details
88 into the Aligned_alloc metadata (allocator and PFS) segments.
89 */
90 using pfs_owning_thread_t = PSI_thread *;
91 using pfs_datalen_t = std::size_t;
92 using pfs_memory_key_t = PSI_memory_key;
93 using pfs_meta_offset_t = std::uint32_t;
94 using data_segment_ptr = void *;
95
96 /** Metadata size */
97 static constexpr auto meta_size = sizeof(pfs_memory_key_t) +
98 sizeof(pfs_owning_thread_t) +
99 sizeof(pfs_datalen_t);
100 static constexpr auto size = meta_size + sizeof(pfs_meta_offset_t);
101
102 /** Helper function which stores the PFS thread info into the OWNER field. */
103 static inline void pfs_owning_thread(data_segment_ptr data,
104 pfs_owning_thread_t thread) noexcept {
105 *ptr_to_pfs_owning_thread(data) = thread;
106 }
107 /** Helper function which stores the PFS datalen info into the DATALEN field.
108 */
109 static inline void pfs_datalen(data_segment_ptr data,
110 size_t datalen) noexcept {
111 assert(datalen <= std::numeric_limits<pfs_datalen_t>::max());
112 *ptr_to_pfs_datalen(data) = datalen;
113 }
114 /** Helper function which stores the PFS key info into the KEY field. */
115 static inline void pfs_key(data_segment_ptr data,
116 pfs_memory_key_t key) noexcept {
117 *ptr_to_pfs_key(data) = key;
118 }
119 /** Helper function which stores the offset to PFS metadata segment into the
120 * PFS-META-OFFSET field.
121 */
122 static inline void pfs_metaoffset(data_segment_ptr data,
123 std::size_t alignment) noexcept {
124 assert(size <= alignment);
125 *ptr_to_pfs_meta_offset(data, alignment) = alignment;
126 }
127 /** Helper function which recovers the information user previously stored in
128 OWNER field.
129 */
130 static inline pfs_owning_thread_t pfs_owning_thread(
131 data_segment_ptr data) noexcept {
132 auto offset = pfs_meta_offset(data);
133 return *reinterpret_cast<pfs_owning_thread_t *>(
134 static_cast<uint8_t *>(data) - offset);
135 }
136 /** Helper function which recovers the information user previously stored in
137 DATALEN field.
138 */
139 static inline pfs_datalen_t pfs_datalen(data_segment_ptr data) noexcept {
140 auto offset = pfs_meta_offset(data);
141 return *reinterpret_cast<pfs_datalen_t *>(
142 static_cast<uint8_t *>(data) - offset + sizeof(pfs_owning_thread_t));
143 }
144 /** Helper function which recovers the information user previously stored in
145 KEY field.
146 */
147 static inline pfs_memory_key_t pfs_key(data_segment_ptr data) noexcept {
148 auto offset = pfs_meta_offset(data);
149 return *reinterpret_cast<pfs_memory_key_t *>(
150 static_cast<uint8_t *>(data) - offset + sizeof(pfs_datalen_t) +
151 sizeof(pfs_owning_thread_t));
152 }
153 /** Helper function which deduces the pointer to the beginning of PFS metadata
154 segment given the pointer to DATA segment.
155 */
156 static inline void *deduce_pfs_meta(data_segment_ptr data) noexcept {
157 auto offset = pfs_meta_offset(data);
158 return reinterpret_cast<void *>(reinterpret_cast<std::uintptr_t>(data) -
159 offset);
160 }
161
162 private:
163 /** Helper accessor function to OWNER metadata. */
164 static inline pfs_owning_thread_t *ptr_to_pfs_owning_thread(
165 data_segment_ptr data) noexcept {
166 return reinterpret_cast<pfs_owning_thread_t *>(data);
167 }
168 /** Helper accessor function to DATALEN metadata. */
169 static inline pfs_datalen_t *ptr_to_pfs_datalen(
170 data_segment_ptr data) noexcept {
171 return reinterpret_cast<pfs_datalen_t *>(ptr_to_pfs_owning_thread(data) +
172 1);
173 }
174 /** Helper accessor function to PFS metadata. */
175 static inline pfs_memory_key_t *ptr_to_pfs_key(
176 data_segment_ptr data) noexcept {
177 return reinterpret_cast<pfs_memory_key_t *>(ptr_to_pfs_datalen(data) + 1);
178 }
179 /** Helper accessor function to PFS-META-OFFSET metadata. */
180 static inline pfs_meta_offset_t *ptr_to_pfs_meta_offset(
181 data_segment_ptr data, std::size_t alignment) noexcept {
182 return reinterpret_cast<pfs_meta_offset_t *>(
183 static_cast<uint8_t *>(data) + alignment - sizeof(pfs_meta_offset_t));
184 }
185 /** Helper function which deduces PFS-META-OFFSET metadata value given the
186 pointer to DATA segment. */
187 static inline pfs_meta_offset_t pfs_meta_offset(
188 data_segment_ptr data) noexcept {
189 return *(reinterpret_cast<pfs_meta_offset_t *>(
190 static_cast<uint8_t *>(data) - sizeof(pfs_meta_offset_t)));
191 }
192 };
193
194 } // namespace detail
195 } // namespace ut
196
197 #endif