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