MySQL 8.0.39
Source Code Documentation
pfs.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/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
45
46namespace ut {
47namespace 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 */
87 /** Convenience types that we will be using to serialize necessary details
88 into the Aligned_alloc metadata (allocator and PFS) segments.
89 */
91 using pfs_datalen_t = std::size_t;
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 */
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. */
165 data_segment_ptr data) noexcept {
166 return reinterpret_cast<pfs_owning_thread_t *>(data);
167 }
168 /** Helper accessor function to DATALEN metadata. */
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. */
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. */
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. */
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
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
struct PSI_thread PSI_thread
Definition: psi_thread_bits.h:82
Instrumentation helpers for memory allocation.
Definition: ut0tuple.h:57
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:48
required string key
Definition: replication_asynchronous_connection_failover.proto:60
Memory layout representation of PFS metadata segment that is used by the allocator variants which als...
Definition: pfs.h:86
void * data_segment_ptr
Definition: pfs.h:94
std::size_t pfs_datalen_t
Definition: pfs.h:91
static pfs_datalen_t * ptr_to_pfs_datalen(data_segment_ptr data) noexcept
Helper accessor function to DATALEN metadata.
Definition: pfs.h:169
static pfs_owning_thread_t pfs_owning_thread(data_segment_ptr data) noexcept
Helper function which recovers the information user previously stored in OWNER field.
Definition: pfs.h:130
static pfs_memory_key_t * ptr_to_pfs_key(data_segment_ptr data) noexcept
Helper accessor function to PFS metadata.
Definition: pfs.h:175
static void pfs_datalen(data_segment_ptr data, size_t datalen) noexcept
Helper function which stores the PFS datalen info into the DATALEN field.
Definition: pfs.h:109
static void pfs_key(data_segment_ptr data, pfs_memory_key_t key) noexcept
Helper function which stores the PFS key info into the KEY field.
Definition: pfs.h:115
static constexpr auto meta_size
Metadata size.
Definition: pfs.h:97
PSI_thread * pfs_owning_thread_t
Convenience types that we will be using to serialize necessary details into the Aligned_alloc metadat...
Definition: pfs.h:90
static constexpr auto size
Definition: pfs.h:100
static void pfs_owning_thread(data_segment_ptr data, pfs_owning_thread_t thread) noexcept
Helper function which stores the PFS thread info into the OWNER field.
Definition: pfs.h:103
static pfs_owning_thread_t * ptr_to_pfs_owning_thread(data_segment_ptr data) noexcept
Helper accessor function to OWNER metadata.
Definition: pfs.h:164
static pfs_memory_key_t pfs_key(data_segment_ptr data) noexcept
Helper function which recovers the information user previously stored in KEY field.
Definition: pfs.h:147
static void pfs_metaoffset(data_segment_ptr data, std::size_t alignment) noexcept
Helper function which stores the offset to PFS metadata segment into the PFS-META-OFFSET field.
Definition: pfs.h:122
PSI_memory_key pfs_memory_key_t
Definition: pfs.h:92
static void * deduce_pfs_meta(data_segment_ptr data) noexcept
Helper function which deduces the pointer to the beginning of PFS metadata segment given the pointer ...
Definition: pfs.h:156
static pfs_datalen_t pfs_datalen(data_segment_ptr data) noexcept
Helper function which recovers the information user previously stored in DATALEN field.
Definition: pfs.h:139
static pfs_meta_offset_t * ptr_to_pfs_meta_offset(data_segment_ptr data, std::size_t alignment) noexcept
Helper accessor function to PFS-META-OFFSET metadata.
Definition: pfs.h:180
static pfs_meta_offset_t pfs_meta_offset(data_segment_ptr data) noexcept
Helper function which deduces PFS-META-OFFSET metadata value given the pointer to DATA segment.
Definition: pfs.h:187
std::uint32_t pfs_meta_offset_t
Definition: pfs.h:93