1 /* Copyright (c) 2008, 2021, 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 Foundation,
21   51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 #ifndef PFS_GLOBAL_H
24 #define PFS_GLOBAL_H
25 
26 #include "my_global.h"
27 #include "my_compiler.h"
28 
29 /**
30   @file storage/perfschema/pfs_global.h
31   Miscellaneous global dependencies (declarations).
32 */
33 
34 /** True when the performance schema is initialized. */
35 extern bool pfs_initialized;
36 
37 #if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN) || defined(HAVE_ALIGNED_MALLOC)
38 #define PFS_ALIGNEMENT 64
39 #define PFS_ALIGNED MY_ALIGNED(PFS_ALIGNEMENT)
40 #else
41 /*
42   Known platforms that do not provide aligned memory:
43   - MacOSX Darwin (osx10.5)
44   For these platforms, compile without the alignment optimization.
45 */
46 #define PFS_ALIGNED
47 #endif /* HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_ALIGNED_MALLOC */
48 
49 #ifdef CPU_LEVEL1_DCACHE_LINESIZE
50 #define PFS_CACHE_LINE_SIZE CPU_LEVEL1_DCACHE_LINESIZE
51 #else
52 #define PFS_CACHE_LINE_SIZE 128
53 #endif
54 
55 /**
56   A uint32 variable, guaranteed to be alone in a CPU cache line.
57   This is for performance, for variables accessed very frequently.
58 */
59 struct PFS_cacheline_uint32
60 {
61   uint32 m_u32;
62   char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(uint32)];
63 
PFS_cacheline_uint32PFS_cacheline_uint3264   PFS_cacheline_uint32()
65   : m_u32(0)
66   {}
67 };
68 
69 /**
70   A uint64 variable, guaranteed to be alone in a CPU cache line.
71   This is for performance, for variables accessed very frequently.
72 */
73 struct PFS_cacheline_uint64
74 {
75   uint64 m_u64;
76   char m_full_cache_line[PFS_CACHE_LINE_SIZE - sizeof(uint64)];
77 
PFS_cacheline_uint64PFS_cacheline_uint6478   PFS_cacheline_uint64()
79   : m_u64(0)
80   {}
81 };
82 
83 struct PFS_builtin_memory_class;
84 
85 /** Memory allocation for the performance schema. */
86 void *pfs_malloc(PFS_builtin_memory_class *klass, size_t size, myf flags);
87 
88 /** Allocate an array of structures with overflow check. */
89 void *pfs_malloc_array(PFS_builtin_memory_class *klass, size_t n, size_t size, myf flags);
90 
91 /**
92   Helper, to allocate an array of structures.
93   @param k memory class
94   @param n number of elements in the array
95   @param s size of array element
96   @param T type of an element
97   @param f flags to use when allocating memory
98 */
99 #define PFS_MALLOC_ARRAY(k, n, s, T, f) \
100   reinterpret_cast<T*>(pfs_malloc_array((k), (n), (s), (f)))
101 
102 /** Free memory allocated with @sa pfs_malloc. */
103 void pfs_free(PFS_builtin_memory_class *klass, size_t size, void *ptr);
104 
105 /** Free memory allocated with @sa pfs_malloc_array. */
106 void pfs_free_array(PFS_builtin_memory_class *klass, size_t n, size_t size, void *ptr);
107 
108 /**
109   Helper, to free an array of structures.
110   @param k memory class
111   @param n number of elements in the array
112   @param s size of array element
113   @param p the array to free
114 */
115 #define PFS_FREE_ARRAY(k, n, s, p) \
116   pfs_free_array((k), (n), (s), (p))
117 
118 /** Detect multiplication overflow. */
119 bool is_overflow(size_t product, size_t n1, size_t n2);
120 
121 uint pfs_get_socket_address(char *host,
122                             uint host_len,
123                             uint *port,
124                             const struct sockaddr_storage *src_addr,
125                             socklen_t src_len);
126 
127 /**
128   Compute a random index value in an interval.
129   @param ptr seed address
130   @param max_size maximun size of the interval
131   @return a random value in [0, max_size-1]
132 */
randomized_index(const void * ptr,uint max_size)133 inline uint randomized_index(const void *ptr, uint max_size)
134 {
135   static uint seed1= 0;
136   static uint seed2= 0;
137   uint result;
138   intptr value;
139 
140   if (unlikely(max_size == 0))
141     return 0;
142 
143   /*
144     ptr is typically an aligned structure, and can be in an array.
145     - The last bits are not random because of alignment,
146       so we divide by 8.
147     - The high bits are mostly constant, especially with 64 bits architectures,
148       but we keep most of them anyway, by doing computation in intptr.
149       The high bits are significant depending on where the data is
150       stored (the data segment, the stack, the heap, ...).
151     - To spread consecutive cells in an array further, we multiply by
152       a factor A. This factor should not be too high, which would cause
153       an overflow and cause loss of randomness (droping the top high bits).
154       The factor is a prime number, to help spread the distribution.
155     - To add more noise, and to be more robust if the calling code is
156       passing a constant value instead of a random identity,
157       we add the previous results, for hysteresys, with a degree 2 polynom,
158       X^2 + X + 1.
159     - Last, a modulo is applied to be within the [0, max_size - 1] range.
160     Note that seed1 and seed2 are static, and are *not* thread safe,
161     which is even better.
162     Effect with arrays: T array[N]
163     - ptr(i) = & array[i] = & array[0] + i * sizeof(T)
164     - ptr(i+1) = ptr(i) + sizeof(T).
165     What we want here, is to have index(i) and index(i+1) fall into
166     very different areas in [0, max_size - 1], to avoid locality.
167   */
168   value= (reinterpret_cast<intptr> (ptr)) >> 3;
169   value*= 1789;
170   value+= seed2 + seed1 + 1;
171 
172   result= (static_cast<uint> (value)) % max_size;
173 
174   seed2= seed1*seed1;
175   seed1= result;
176 
177   assert(result < max_size);
178   return result;
179 }
180 
181 void pfs_print_error(const char *format, ...);
182 
183 /**
184   Given an array defined as T ARRAY[MAX],
185   check that an UNSAFE pointer actually points to an element
186   within the array.
187 */
188 #define SANITIZE_ARRAY_BODY(T, ARRAY, MAX, UNSAFE)          \
189   intptr offset;                                            \
190   if ((&ARRAY[0] <= UNSAFE) &&                              \
191       (UNSAFE < &ARRAY[MAX]))                               \
192   {                                                         \
193     offset= ((intptr) UNSAFE - (intptr) ARRAY) % sizeof(T); \
194     if (offset == 0)                                        \
195       return UNSAFE;                                        \
196   }                                                         \
197   return NULL
198 
199 #endif
200 
201