1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/* Copyright (c) 2008, 2023, Oracle and/or its affiliates.
Copyright (c) 2022, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
/**
@file storage/perfschema/pfs_digest.h
Statement Digest data structures (implementation).
*/
/*
This code needs extra visibility in the lexer structures
*/
#define MYSQL_LEX 1
#include "my_global.h"
#include "my_sys.h"
#include "pfs_instr.h"
#include "pfs_digest.h"
#include "pfs_global.h"
#include "pfs_builtin_memory.h"
#include "table_helper.h"
#include "sql_lex.h"
#include "sql_signal.h"
#include "sql_get_diagnostics.h"
#include "sql_string.h"
#include <string.h>
size_t digest_max= 0;
ulong digest_lost= 0;
/** EVENTS_STATEMENTS_HISTORY_LONG circular buffer. */
PFS_statements_digest_stat *statements_digest_stat_array= NULL;
static unsigned char *statements_digest_token_array= NULL;
/** Consumer flag for table EVENTS_STATEMENTS_SUMMARY_BY_DIGEST. */
bool flag_statements_digest= true;
/**
Current index in Stat array where new record is to be inserted.
index 0 is reserved for "all else" case when entire array is full.
*/
PFS_ALIGNED static PFS_cacheline_uint32 digest_monotonic_index;
bool digest_full= false;
LF_HASH digest_hash;
static bool digest_hash_inited= false;
/**
Initialize table EVENTS_STATEMENTS_SUMMARY_BY_DIGEST.
@param param performance schema sizing
*/
int init_digest(const PFS_global_param *param)
{
/*
Allocate memory for statements_digest_stat_array based on
performance_schema_digests_size values
*/
digest_max= param->m_digest_sizing;
digest_lost= 0;
PFS_atomic::store_u32(& digest_monotonic_index.m_u32, 1);
digest_full= false;
if (digest_max == 0)
return 0;
statements_digest_stat_array=
PFS_MALLOC_ARRAY(& builtin_memory_digest,
digest_max,
sizeof(PFS_statements_digest_stat), PFS_statements_digest_stat,
MYF(MY_ZEROFILL));
if (unlikely(statements_digest_stat_array == NULL))
{
cleanup_digest();
return 1;
}
if (pfs_max_digest_length > 0)
{
/* Size of each digest array. */
size_t digest_memory_size= pfs_max_digest_length * sizeof(unsigned char);
statements_digest_token_array=
PFS_MALLOC_ARRAY(& builtin_memory_digest_tokens,
digest_max,
digest_memory_size,
unsigned char,
MYF(MY_ZEROFILL));
if (unlikely(statements_digest_token_array == NULL))
{
cleanup_digest();
return 1;
}
}
for (size_t index= 0; index < digest_max; index++)
{
statements_digest_stat_array[index].reset_data(statements_digest_token_array
+ index * pfs_max_digest_length, pfs_max_digest_length);
}
/* Set record[0] as allocated. */
statements_digest_stat_array[0].m_lock.set_allocated();
/* Set record[0] as allocated. */
statements_digest_stat_array[0].m_lock.set_allocated();
return 0;
}
/** Cleanup table EVENTS_STATEMENTS_SUMMARY_BY_DIGEST. */
void cleanup_digest(void)
{
PFS_FREE_ARRAY(& builtin_memory_digest,
digest_max,
sizeof(PFS_statements_digest_stat),
statements_digest_stat_array);
PFS_FREE_ARRAY(& builtin_memory_digest_tokens,
digest_max,
(pfs_max_digest_length * sizeof(unsigned char)),
statements_digest_token_array);
statements_digest_stat_array= NULL;
statements_digest_token_array= NULL;
}
C_MODE_START
static uchar *digest_hash_get_key(const uchar *entry, size_t *length,
my_bool)
{
const PFS_statements_digest_stat * const *typed_entry;
const PFS_statements_digest_stat *digest;
const void *result;
typed_entry= reinterpret_cast<const PFS_statements_digest_stat*const*>(entry);
assert(typed_entry != NULL);
digest= *typed_entry;
assert(digest != NULL);
*length= sizeof (PFS_digest_key);
result= & digest->m_digest_key;
return const_cast<uchar*> (reinterpret_cast<const uchar*> (result));
}
C_MODE_END
/**
Initialize the digest hash.
@return 0 on success
*/
int init_digest_hash(const PFS_global_param *param)
{
if ((! digest_hash_inited) && (param->m_digest_sizing != 0))
{
lf_hash_init(&digest_hash, sizeof(PFS_statements_digest_stat*),
LF_HASH_UNIQUE, 0, 0, digest_hash_get_key,
&my_charset_bin);
digest_hash_inited= true;
}
return 0;
}
void cleanup_digest_hash(void)
{
if (digest_hash_inited)
{
lf_hash_destroy(&digest_hash);
digest_hash_inited= false;
}
}
static LF_PINS* get_digest_hash_pins(PFS_thread *thread)
{
if (unlikely(thread->m_digest_hash_pins == NULL))
{
if (!digest_hash_inited)
return NULL;
thread->m_digest_hash_pins= lf_hash_get_pins(&digest_hash);
}
return thread->m_digest_hash_pins;
}
PFS_statement_stat*
find_or_create_digest(PFS_thread *thread,
const sql_digest_storage *digest_storage,
const char *schema_name,
uint schema_name_length)
{
assert(digest_storage != NULL);
if (statements_digest_stat_array == NULL)
return NULL;
if (digest_storage->m_byte_count <= 0)
return NULL;
LF_PINS *pins= get_digest_hash_pins(thread);
if (unlikely(pins == NULL))
return NULL;
/*
Note: the LF_HASH key is a block of memory,
make sure to clean unused bytes,
so that memcmp() can compare keys.
*/
PFS_digest_key hash_key;
memset(& hash_key, 0, sizeof(hash_key));
/* Compute MD5 Hash of the tokens received. */
compute_digest_md5(digest_storage, hash_key.m_md5);
memcpy((void*)& digest_storage->m_md5, &hash_key.m_md5, MD5_HASH_SIZE);
/* Add the current schema to the key */
hash_key.m_schema_name_length= schema_name_length;
if (schema_name_length > 0)
memcpy(hash_key.m_schema_name, schema_name, schema_name_length);
int res;
uint retry_count= 0;
const uint retry_max= 3;
size_t safe_index;
size_t attempts= 0;
PFS_statements_digest_stat **entry;
PFS_statements_digest_stat *pfs= NULL;
pfs_dirty_state dirty_state;
ulonglong now= my_hrtime().val;
search:
/* Lookup LF_HASH using this new key. */
entry= reinterpret_cast<PFS_statements_digest_stat**>
(lf_hash_search(&digest_hash, pins,
&hash_key, sizeof(PFS_digest_key)));
if (entry && (entry != MY_ERRPTR))
{
/* If digest already exists, update stats and return. */
pfs= *entry;
pfs->m_last_seen= now;
lf_hash_search_unpin(pins);
return & pfs->m_stat;
}
lf_hash_search_unpin(pins);
if (digest_full)
{
/* digest_stat array is full. Add stat at index 0 and return. */
pfs= &statements_digest_stat_array[0];
digest_lost++;
if (pfs->m_first_seen == 0)
pfs->m_first_seen= now;
pfs->m_last_seen= now;
return & pfs->m_stat;
}
while (++attempts <= digest_max)
{
safe_index= PFS_atomic::add_u32(& digest_monotonic_index.m_u32, 1) % digest_max;
if (safe_index == 0)
{
/* Record [0] is reserved. */
continue;
}
/* Add a new record in digest stat array. */
assert(safe_index < digest_max);
pfs= &statements_digest_stat_array[safe_index];
if (pfs->m_lock.is_free())
{
if (pfs->m_lock.free_to_dirty(& dirty_state))
{
/* Copy digest hash/LF Hash search key. */
memcpy(& pfs->m_digest_key, &hash_key, sizeof(PFS_digest_key));
/*
Copy digest storage to statement_digest_stat_array so that it could be
used later to generate digest text.
*/
pfs->m_digest_storage.copy(digest_storage);
pfs->m_first_seen= now;
pfs->m_last_seen= now;
res= lf_hash_insert(&digest_hash, pins, &pfs);
if (likely(res == 0))
{
pfs->m_lock.dirty_to_allocated(& dirty_state);
return & pfs->m_stat;
}
pfs->m_lock.dirty_to_free(& dirty_state);
if (res > 0)
{
/* Duplicate insert by another thread */
if (++retry_count > retry_max)
{
/* Avoid infinite loops */
digest_lost++;
return NULL;
}
goto search;
}
/* OOM in lf_hash_insert */
digest_lost++;
return NULL;
}
}
}
/* The digest array is now full. */
digest_full= true;
pfs= &statements_digest_stat_array[0];
if (pfs->m_first_seen == 0)
pfs->m_first_seen= now;
pfs->m_last_seen= now;
return & pfs->m_stat;
}
void purge_digest(PFS_thread* thread, PFS_digest_key *hash_key)
{
LF_PINS *pins= get_digest_hash_pins(thread);
if (unlikely(pins == NULL))
return;
PFS_statements_digest_stat **entry;
/* Lookup LF_HASH using this new key. */
entry= reinterpret_cast<PFS_statements_digest_stat**>
(lf_hash_search(&digest_hash, pins,
hash_key, sizeof(PFS_digest_key)));
if (entry && (entry != MY_ERRPTR))
{
lf_hash_delete(&digest_hash, pins,
hash_key, sizeof(PFS_digest_key));
}
lf_hash_search_unpin(pins);
return;
}
void PFS_statements_digest_stat::reset_data(unsigned char *token_array, size_t length)
{
pfs_dirty_state dirty_state;
m_lock.set_dirty(& dirty_state);
m_digest_storage.reset(token_array, length);
m_stat.reset();
m_first_seen= 0;
m_last_seen= 0;
m_lock.dirty_to_free(& dirty_state);
}
void PFS_statements_digest_stat::reset_index(PFS_thread *thread)
{
/* Only remove entries that exists in the HASH index. */
if (m_digest_storage.m_byte_count > 0)
{
purge_digest(thread, & m_digest_key);
}
}
void reset_esms_by_digest()
{
uint index;
if (statements_digest_stat_array == NULL)
return;
PFS_thread *thread= PFS_thread::get_current_thread();
if (unlikely(thread == NULL))
return;
/* Reset statements_digest_stat_array. */
for (index= 0; index < digest_max; index++)
{
statements_digest_stat_array[index].reset_index(thread);
statements_digest_stat_array[index].reset_data(statements_digest_token_array + index * pfs_max_digest_length, pfs_max_digest_length);
}
/* Mark record[0] as allocated again. */
statements_digest_stat_array[0].m_lock.set_allocated();
/*
Reset index which indicates where the next calculated digest information
to be inserted in statements_digest_stat_array.
*/
PFS_atomic::store_u32(& digest_monotonic_index.m_u32, 1);
digest_full= false;
}
|