MySQL 8.0.29
Source Code Documentation
pfs_global.h
Go to the documentation of this file.
1/* Copyright (c) 2008, 2022, 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
43/**
44 @file storage/perfschema/pfs_global.h
45 Miscellaneous global dependencies (declarations).
46*/
47
48/** True when the performance schema is initialized. */
49extern bool pfs_initialized;
50
51#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN) || \
52 defined(HAVE_ALIGNED_MALLOC)
53#define PFS_ALIGNEMENT 64
54#define PFS_ALIGNED alignas(PFS_ALIGNEMENT)
55#else
56/*
57 Known platforms that do not provide aligned memory:
58 - MacOSX Darwin (osx10.5)
59 For these platforms, compile without the alignment optimization.
60*/
61#define PFS_ALIGNED
62#endif /* HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_ALIGNED_MALLOC */
63
64#ifdef CPU_LEVEL1_DCACHE_LINESIZE
65#define PFS_CACHE_LINE_SIZE CPU_LEVEL1_DCACHE_LINESIZE
66#else
67#define PFS_CACHE_LINE_SIZE 128
68#endif
69
70/**
71 An atomic @c uint32 variable, guaranteed to be alone in a CPU cache line.
72 This is for performance, for variables accessed very frequently.
73*/
75 std::atomic<uint32> m_u32;
76 char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic<uint32>)];
77
79};
80
81/**
82 An atomic @c uint64 variable, guaranteed to be alone in a CPU cache line.
83 This is for performance, for variables accessed very frequently.
84*/
86 std::atomic<uint64> m_u64;
87 char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic<uint64>)];
88
90};
91
92/**
93 An atomic @c size_t variable, guaranteed to be alone in a CPU cache line.
94 This is for performance, for variables accessed very frequently.
95*/
97 std::atomic<size_t> m_size_t;
98 char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic<size_t>)];
99
101};
102
104
105/** Memory allocation for the performance schema. */
106void *pfs_malloc(PFS_builtin_memory_class *klass, size_t size, myf flags);
107
108/** Allocate an array of structures with overflow check. */
109void *pfs_malloc_array(PFS_builtin_memory_class *klass, size_t n, size_t size,
110 myf flags);
111
112/**
113 Helper, to allocate an array of structures.
114 @param k memory class
115 @param n number of elements in the array
116 @param s size of array element
117 @param T type of an element
118 @param f flags to use when allocating memory
119*/
120#define PFS_MALLOC_ARRAY(k, n, s, T, f) \
121 reinterpret_cast<T *>(pfs_malloc_array((k), (n), (s), (f)))
122
123/** Free memory allocated with @sa pfs_malloc. */
124void pfs_free(PFS_builtin_memory_class *klass, size_t size, void *ptr);
125
126/** Free memory allocated with @sa pfs_malloc_array. */
127void pfs_free_array(PFS_builtin_memory_class *klass, size_t n, size_t size,
128 void *ptr);
129
130/**
131 Helper, to free an array of structures.
132 @param k memory class
133 @param n number of elements in the array
134 @param s size of array element
135 @param p the array to free
136*/
137#define PFS_FREE_ARRAY(k, n, s, p) pfs_free_array((k), (n), (s), (p))
138
139/** Detect multiplication overflow. */
140bool is_overflow(size_t product, size_t n1, size_t n2);
141
142uint pfs_get_socket_address(char *host, uint host_len, uint *port,
143 const struct sockaddr_storage *src_addr,
144 socklen_t src_len);
145
146/**
147 Helper to allocate an object from mem_root.
148 @param CLASS Class to instantiate
149*/
150#define PFS_NEW(CLASS) (new (*THR_MALLOC) CLASS())
151
152void pfs_print_error(const char *format, ...)
153 MY_ATTRIBUTE((format(printf, 1, 2)));
154
155/**
156 Given an array defined as T ARRAY[MAX],
157 check that an UNSAFE pointer actually points to an element
158 within the array.
159*/
160#define SANITIZE_ARRAY_BODY(T, ARRAY, MAX, UNSAFE) \
161 intptr offset; \
162 if ((&ARRAY[0] <= UNSAFE) && (UNSAFE < &ARRAY[MAX])) { \
163 offset = ((intptr)UNSAFE - (intptr)ARRAY) % sizeof(T); \
164 if (offset == 0) { \
165 return UNSAFE; \
166 } \
167 } \
168 return NULL
169
170#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:58
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:212
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:141
void pfs_print_error(const char *format,...)
Definition: pfs_global.cc:195
bool pfs_initialized
True when the performance schema is initialized.
Definition: pfs_global.cc:52
void * pfs_malloc(PFS_builtin_memory_class *klass, size_t size, myf flags)
Memory allocation for the performance schema.
Definition: pfs_global.cc:59
void pfs_free(PFS_builtin_memory_class *klass, size_t size, void *ptr)
Free memory allocated with.
Definition: pfs_global.cc:106
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:169
bool is_overflow(size_t product, size_t n1, size_t n2)
Detect multiplication overflow.
Definition: pfs_global.cc:187
#define PFS_CACHE_LINE_SIZE
Definition: pfs_global.h:65
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:96
std::atomic< size_t > m_size_t
Definition: pfs_global.h:97
PFS_cacheline_atomic_size_t()
Definition: pfs_global.h:100
char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic< size_t >)]
Definition: pfs_global.h:98
An atomic uint32 variable, guaranteed to be alone in a CPU cache line.
Definition: pfs_global.h:74
PFS_cacheline_atomic_uint32()
Definition: pfs_global.h:78
char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic< uint32 >)]
Definition: pfs_global.h:76
std::atomic< uint32 > m_u32
Definition: pfs_global.h:75
An atomic uint64 variable, guaranteed to be alone in a CPU cache line.
Definition: pfs_global.h:85
char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(std::atomic< uint64 >)]
Definition: pfs_global.h:87
std::atomic< uint64 > m_u64
Definition: pfs_global.h:86
PFS_cacheline_atomic_uint64()
Definition: pfs_global.h:89
unsigned int uint
Definition: uca-dump.cc:29
int n
Definition: xcom_base.cc:505