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