MySQL 8.2.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
106
107/** Memory allocation for the performance schema. */
108void *pfs_malloc(PFS_builtin_memory_class *klass, size_t size, myf flags);
109
110/** Allocate an array of structures with overflow check. */
111void *pfs_malloc_array(PFS_builtin_memory_class *klass, size_t n, size_t size,
112 myf flags);
113
114/**
115 Helper, to allocate an array of structures.
116 @param k memory class
117 @param n number of elements in the array
118 @param s size of array element
119 @param T type of an element
120 @param f flags to use when allocating memory
121*/
122#define PFS_MALLOC_ARRAY(k, n, s, T, f) \
123 reinterpret_cast<T *>(pfs_malloc_array((k), (n), (s), (f)))
124
125/** Free memory allocated with @sa pfs_malloc. */
126void pfs_free(PFS_builtin_memory_class *klass, size_t size, void *ptr);
127
128/** Free memory allocated with @sa pfs_malloc_array. */
129void pfs_free_array(PFS_builtin_memory_class *klass, size_t n, size_t size,
130 void *ptr);
131
132/**
133 Helper, to free an array of structures.
134 @param k memory class
135 @param n number of elements in the array
136 @param s size of array element
137 @param p the array to free
138*/
139#define PFS_FREE_ARRAY(k, n, s, p) pfs_free_array((k), (n), (s), (p))
140
141/** Detect multiplication overflow. */
142bool is_overflow(size_t product, size_t n1, size_t n2);
143
144uint pfs_get_socket_address(char *host, uint host_len, uint *port,
145 const struct sockaddr_storage *src_addr,
146 socklen_t src_len);
147
148/**
149 Helper to allocate an object from mem_root.
150 @param CLASS Class to instantiate
151*/
152#define PFS_NEW(CLASS) (new (*THR_MALLOC) CLASS())
153
154void pfs_print_error(const char *format, ...)
155 MY_ATTRIBUTE((format(printf, 1, 2)));
156
157/**
158 Given an array defined as T ARRAY[MAX],
159 check that an UNSAFE pointer actually points to an element
160 within the array.
161*/
162#define SANITIZE_ARRAY_BODY(T, ARRAY, MAX, UNSAFE) \
163 intptr offset; \
164 if ((&ARRAY[0] <= UNSAFE) && (UNSAFE < &ARRAY[MAX])) { \
165 offset = ((intptr)UNSAFE - (intptr)ARRAY) % sizeof(T); \
166 if (offset == 0) { \
167 return UNSAFE; \
168 } \
169 } \
170 return NULL
171
172#endif
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 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