MySQL 9.1.0
Source Code Documentation
pfs_global.h
Go to the documentation of this file.
1/* Copyright (c) 2008, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef PFS_GLOBAL_H
25#define PFS_GLOBAL_H
26
27#include "my_config.h"
28
29#include <atomic>
30
31#include <stddef.h>
32#ifdef HAVE_SYS_SOCKET_H
33#include <sys/socket.h>
34#endif
35#include <sys/types.h>
36#ifdef WIN32
37#include <ws2tcpip.h> // socklen_t
38#endif
39
40#include "my_compiler.h"
41#include "my_inttypes.h"
42#include "sql/thr_malloc.h"
43
45
46/**
47 @file storage/perfschema/pfs_global.h
48 Miscellaneous global dependencies (declarations).
49*/
50
51/** True when the performance schema is initialized. */
52extern bool pfs_initialized;
53
54#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN) || \
55 defined(HAVE_ALIGNED_MALLOC)
56#define PFS_ALIGNMENT 64
57#define PFS_ALIGNED alignas(PFS_ALIGNMENT)
58#else
59/*
60 Known platforms that do not provide aligned memory:
61 - MacOSX Darwin (osx10.5)
62 For these platforms, compile without the alignment optimization.
63*/
64#define PFS_ALIGNED
65#endif /* HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_ALIGNED_MALLOC */
66
67#ifdef CPU_LEVEL1_DCACHE_LINESIZE
68#define PFS_CACHE_LINE_SIZE CPU_LEVEL1_DCACHE_LINESIZE
69#else
70#define PFS_CACHE_LINE_SIZE 128
71#endif
72
73/**
74 An atomic @c uint32 variable, guaranteed to be alone in a CPU cache line.
75 This is for performance, for variables accessed very frequently.
76*/
78 std::atomic<uint32> m_u32;
79 char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic<uint32>)];
80
82};
83
84/**
85 An atomic @c uint64 variable, guaranteed to be alone in a CPU cache line.
86 This is for performance, for variables accessed very frequently.
87*/
89 std::atomic<uint64> m_u64;
90 char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic<uint64>)];
91
93};
94
95/**
96 An atomic @c size_t variable, guaranteed to be alone in a CPU cache line.
97 This is for performance, for variables accessed very frequently.
98*/
100 std::atomic<size_t> m_size_t;
101 char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic<size_t>)];
102
104};
105
106/**
107 An atomic<T> variable, guaranteed to be alone in a CPU cache line.
108 This is for performance, for variables accessed very frequently.
109*/
110template <class T>
112 std::atomic<T> m_ptr;
113 char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic<T>)];
114
116};
117
119
120/** Memory allocation for the performance schema. */
121void *pfs_malloc(PFS_builtin_memory_class *klass, size_t size, myf flags);
122
123/** Allocate an array of structures with overflow check. */
124void *pfs_malloc_array(PFS_builtin_memory_class *klass, size_t n, size_t size,
125 myf flags);
126
127/**
128 Helper, to allocate an array of structures.
129 @param k memory class
130 @param n number of elements in the array
131 @param s size of array element
132 @param T type of an element
133 @param f flags to use when allocating memory
134*/
135#define PFS_MALLOC_ARRAY(k, n, s, T, f) \
136 reinterpret_cast<T *>(pfs_malloc_array((k), (n), (s), (f)))
137
138/** Free memory allocated with @sa pfs_malloc. */
139void pfs_free(PFS_builtin_memory_class *klass, size_t size, void *ptr);
140
141/** Free memory allocated with @sa pfs_malloc_array. */
142void pfs_free_array(PFS_builtin_memory_class *klass, size_t n, size_t size,
143 void *ptr);
144
145/**
146 Helper, to free an array of structures.
147 @param k memory class
148 @param n number of elements in the array
149 @param s size of array element
150 @param p the array to free
151*/
152#define PFS_FREE_ARRAY(k, n, s, p) pfs_free_array((k), (n), (s), (p))
153
154/** Detect multiplication overflow. */
155bool is_overflow(size_t product, size_t n1, size_t n2);
156
157uint pfs_get_socket_address(char *host, uint host_len, uint *port,
158 const struct sockaddr_storage *src_addr,
159 socklen_t src_len);
160
161/**
162 Helper to allocate an object from mem_root.
163 @param CLASS Class to instantiate
164*/
165#define PFS_NEW(CLASS) (new (*THR_MALLOC) CLASS())
166
167void pfs_print_error(const char *format, ...)
168 MY_ATTRIBUTE((format(printf, 1, 2)));
169
170/**
171 Given an array defined as T ARRAY[MAX],
172 check that an UNSAFE pointer actually points to an element
173 within the array.
174*/
175#define SANITIZE_ARRAY_BODY(T, ARRAY, MAX, UNSAFE) \
176 intptr offset; \
177 if ((&ARRAY[0] <= UNSAFE) && (UNSAFE < &ARRAY[MAX])) { \
178 offset = ((intptr)UNSAFE - (intptr)ARRAY) % sizeof(T); \
179 if (offset == 0) { \
180 return UNSAFE; \
181 } \
182 } \
183 return NULL
184
185#endif
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
static int flags[50]
Definition: hp_test1.cc:40
Header for compiler-dependent features.
Some integer typedefs for easier portability.
int myf
Definition: my_inttypes.h:94
const char * host
Definition: mysqladmin.cc:65
size_t size(const char *const c)
Definition: base64.h:46
uint pfs_get_socket_address(char *host, uint host_len, uint *port, const struct sockaddr_storage *src_addr, socklen_t src_len)
Convert raw ip address into readable format.
Definition: pfs_global.cc:210
void * pfs_malloc_array(PFS_builtin_memory_class *klass, size_t n, size_t size, myf flags)
Allocate an array of structures with overflow check.
Definition: pfs_global.cc:143
void pfs_print_error(const char *format,...)
Definition: pfs_global.cc:193
bool pfs_initialized
True when the performance schema is initialized.
Definition: pfs_global.cc:54
void * pfs_malloc(PFS_builtin_memory_class *klass, size_t size, myf flags)
Memory allocation for the performance schema.
Definition: pfs_global.cc:61
void pfs_free(PFS_builtin_memory_class *klass, size_t size, void *ptr)
Free memory allocated with.
Definition: pfs_global.cc:108
void pfs_free_array(PFS_builtin_memory_class *klass, size_t n, size_t size, void *ptr)
Free memory allocated with.
Definition: pfs_global.cc:171
bool is_overflow(size_t product, size_t n1, size_t n2)
Detect multiplication overflow.
Definition: pfs_global.cc:189
#define PFS_CACHE_LINE_SIZE
Definition: pfs_global.h:68
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
Definition: pfs_builtin_memory.h:39
An atomic<T> variable, guaranteed to be alone in a CPU cache line.
Definition: pfs_global.h:111
char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic< T >)]
Definition: pfs_global.h:113
PFS_cacheline_atomic_ptr()
Definition: pfs_global.h:115
std::atomic< T > m_ptr
Definition: pfs_global.h:112
An atomic size_t variable, guaranteed to be alone in a CPU cache line.
Definition: pfs_global.h:99
std::atomic< size_t > m_size_t
Definition: pfs_global.h:100
PFS_cacheline_atomic_size_t()
Definition: pfs_global.h:103
char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic< size_t >)]
Definition: pfs_global.h:101
An atomic uint32 variable, guaranteed to be alone in a CPU cache line.
Definition: pfs_global.h:77
PFS_cacheline_atomic_uint32()
Definition: pfs_global.h:81
char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic< uint32 >)]
Definition: pfs_global.h:79
std::atomic< uint32 > m_u32
Definition: pfs_global.h:78
An atomic uint64 variable, guaranteed to be alone in a CPU cache line.
Definition: pfs_global.h:88
char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic< uint64 >)]
Definition: pfs_global.h:90
std::atomic< uint64 > m_u64
Definition: pfs_global.h:89
PFS_cacheline_atomic_uint64()
Definition: pfs_global.h:92
int n
Definition: xcom_base.cc:509