1 /* Copyright (c) 2014, 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 #include "my_global.h"
24 #include "pfs_global.h"
25 #include "pfs_lock.h"
26 #include "pfs_account.h"
27 #include "pfs_user.h"
28 #include "pfs_host.h"
29 #include "pfs_buffer_container.h"
30 #include "pfs_builtin_memory.h"
31 
32 PFS_buffer_default_allocator<PFS_mutex> default_mutex_allocator(& builtin_memory_mutex);
33 PFS_mutex_container global_mutex_container(& default_mutex_allocator);
34 
35 PFS_buffer_default_allocator<PFS_rwlock> default_rwlock_allocator(& builtin_memory_rwlock);
36 PFS_rwlock_container global_rwlock_container(& default_rwlock_allocator);
37 
38 PFS_buffer_default_allocator<PFS_cond> default_cond_allocator(& builtin_memory_cond);
39 PFS_cond_container global_cond_container(& default_cond_allocator);
40 
41 PFS_buffer_default_allocator<PFS_file> default_file_allocator(& builtin_memory_file);
42 PFS_file_container global_file_container(& default_file_allocator);
43 
44 PFS_buffer_default_allocator<PFS_socket> default_socket_allocator(& builtin_memory_socket);
45 PFS_socket_container global_socket_container(& default_socket_allocator);
46 
47 PFS_buffer_default_allocator<PFS_metadata_lock> default_mdl_allocator(& builtin_memory_mdl);
48 PFS_mdl_container global_mdl_container(& default_mdl_allocator);
49 
50 PFS_buffer_default_allocator<PFS_setup_actor> default_setup_actor_allocator(& builtin_memory_setup_actor);
51 PFS_setup_actor_container global_setup_actor_container(& default_setup_actor_allocator);
52 
53 PFS_buffer_default_allocator<PFS_setup_object> default_setup_object_allocator(& builtin_memory_setup_object);
54 PFS_setup_object_container global_setup_object_container(& default_setup_object_allocator);
55 
56 PFS_buffer_default_allocator<PFS_table> default_table_allocator(& builtin_memory_table);
57 PFS_table_container global_table_container(& default_table_allocator);
58 
59 PFS_buffer_default_allocator<PFS_table_share> default_table_share_allocator(& builtin_memory_table_share);
60 PFS_table_share_container global_table_share_container(& default_table_share_allocator);
61 
62 PFS_buffer_default_allocator<PFS_table_share_index> default_table_share_index_allocator(& builtin_memory_table_share_index);
63 PFS_table_share_index_container global_table_share_index_container(& default_table_share_index_allocator);
64 
65 PFS_buffer_default_allocator<PFS_table_share_lock> default_table_share_lock_allocator(& builtin_memory_table_share_lock);
66 PFS_table_share_lock_container global_table_share_lock_container(& default_table_share_lock_allocator);
67 
68 PFS_buffer_default_allocator<PFS_program> default_program_allocator(& builtin_memory_program);
69 PFS_program_container global_program_container(& default_program_allocator);
70 
71 PFS_buffer_default_allocator<PFS_prepared_stmt> default_prepared_stmt_allocator(& builtin_memory_prepared_stmt);
72 PFS_prepared_stmt_container global_prepared_stmt_container(& default_prepared_stmt_allocator);
73 
alloc_array(PFS_account_array * array)74 int PFS_account_allocator::alloc_array(PFS_account_array *array)
75 {
76   size_t size= array->m_max;
77   size_t index;
78   size_t waits_sizing= size * wait_class_max;
79   size_t stages_sizing= size * stage_class_max;
80   size_t statements_sizing= size * statement_class_max;
81   size_t transactions_sizing= size * transaction_class_max;
82   size_t memory_sizing= size * memory_class_max;
83 
84   array->m_ptr= NULL;
85   array->m_full= true;
86   array->m_instr_class_waits_array= NULL;
87   array->m_instr_class_stages_array= NULL;
88   array->m_instr_class_statements_array= NULL;
89   array->m_instr_class_transactions_array= NULL;
90   array->m_instr_class_memory_array= NULL;
91 
92   if (size > 0)
93   {
94     array->m_ptr=
95       PFS_MALLOC_ARRAY(& builtin_memory_account,
96                        size, sizeof(PFS_account), PFS_account, MYF(MY_ZEROFILL));
97     if (array->m_ptr == NULL)
98       return 1;
99   }
100 
101   if (waits_sizing > 0)
102   {
103     array->m_instr_class_waits_array=
104       PFS_MALLOC_ARRAY(& builtin_memory_account_waits,
105                        waits_sizing, sizeof(PFS_single_stat), PFS_single_stat, MYF(MY_ZEROFILL));
106     if (array->m_instr_class_waits_array == NULL)
107       return 1;
108 
109     for (index=0; index < waits_sizing; index++)
110       array->m_instr_class_waits_array[index].reset();
111   }
112 
113   if (stages_sizing > 0)
114   {
115     array->m_instr_class_stages_array=
116       PFS_MALLOC_ARRAY(& builtin_memory_account_stages,
117                        stages_sizing, sizeof(PFS_stage_stat), PFS_stage_stat, MYF(MY_ZEROFILL));
118     if (array->m_instr_class_stages_array == NULL)
119       return 1;
120 
121     for (index=0; index < stages_sizing; index++)
122       array->m_instr_class_stages_array[index].reset();
123   }
124 
125   if (statements_sizing > 0)
126   {
127     array->m_instr_class_statements_array=
128       PFS_MALLOC_ARRAY(& builtin_memory_account_statements,
129                        statements_sizing, sizeof(PFS_statement_stat), PFS_statement_stat, MYF(MY_ZEROFILL));
130     if (array->m_instr_class_statements_array == NULL)
131       return 1;
132 
133     for (index=0; index < statements_sizing; index++)
134       array->m_instr_class_statements_array[index].reset();
135   }
136 
137   if (transactions_sizing > 0)
138   {
139     array->m_instr_class_transactions_array=
140       PFS_MALLOC_ARRAY(& builtin_memory_account_transactions,
141                        transactions_sizing, sizeof(PFS_transaction_stat), PFS_transaction_stat, MYF(MY_ZEROFILL));
142     if (array->m_instr_class_transactions_array == NULL)
143       return 1;
144 
145     for (index=0; index < transactions_sizing; index++)
146       array->m_instr_class_transactions_array[index].reset();
147   }
148 
149   if (memory_sizing > 0)
150   {
151     array->m_instr_class_memory_array=
152       PFS_MALLOC_ARRAY(& builtin_memory_account_memory,
153                        memory_sizing, sizeof(PFS_memory_stat), PFS_memory_stat, MYF(MY_ZEROFILL));
154     if (array->m_instr_class_memory_array == NULL)
155       return 1;
156 
157     for (index=0; index < memory_sizing; index++)
158       array->m_instr_class_memory_array[index].reset();
159   }
160 
161   for (index= 0; index < size; index++)
162   {
163     array->m_ptr[index].set_instr_class_waits_stats(
164       & array->m_instr_class_waits_array[index * wait_class_max]);
165     array->m_ptr[index].set_instr_class_stages_stats(
166       & array->m_instr_class_stages_array[index * stage_class_max]);
167     array->m_ptr[index].set_instr_class_statements_stats(
168       & array->m_instr_class_statements_array[index * statement_class_max]);
169     array->m_ptr[index].set_instr_class_transactions_stats(
170       & array->m_instr_class_transactions_array[index * transaction_class_max]);
171     array->m_ptr[index].set_instr_class_memory_stats(
172       & array->m_instr_class_memory_array[index * memory_class_max]);
173   }
174 
175   array->m_full= false;
176   return 0;
177 }
178 
free_array(PFS_account_array * array)179 void PFS_account_allocator::free_array(PFS_account_array *array)
180 {
181   size_t size= array->m_max;
182   size_t waits_sizing= size * wait_class_max;
183   size_t stages_sizing= size * stage_class_max;
184   size_t statements_sizing= size * statement_class_max;
185   size_t transactions_sizing= size * transaction_class_max;
186   size_t memory_sizing= size * memory_class_max;
187 
188   PFS_FREE_ARRAY(& builtin_memory_account,
189                  size, sizeof(PFS_account), array->m_ptr);
190   array->m_ptr= NULL;
191 
192   PFS_FREE_ARRAY(& builtin_memory_account_waits,
193                  waits_sizing, sizeof(PFS_single_stat),
194                  array->m_instr_class_waits_array);
195   array->m_instr_class_waits_array= NULL;
196 
197   PFS_FREE_ARRAY(& builtin_memory_account_stages,
198                  stages_sizing, sizeof(PFS_stage_stat),
199                  array->m_instr_class_stages_array);
200   array->m_instr_class_stages_array= NULL;
201 
202   PFS_FREE_ARRAY(& builtin_memory_account_statements,
203                  statements_sizing, sizeof(PFS_statement_stat),
204                  array->m_instr_class_statements_array);
205   array->m_instr_class_statements_array= NULL;
206 
207   PFS_FREE_ARRAY(& builtin_memory_account_transactions,
208                  transactions_sizing, sizeof(PFS_transaction_stat),
209                  array->m_instr_class_transactions_array);
210   array->m_instr_class_transactions_array= NULL;
211 
212   PFS_FREE_ARRAY(& builtin_memory_account_memory,
213                  memory_sizing, sizeof(PFS_memory_stat),
214                  array->m_instr_class_memory_array);
215   array->m_instr_class_memory_array= NULL;
216 }
217 
218 PFS_account_allocator account_allocator;
219 PFS_account_container global_account_container(& account_allocator);
220 
alloc_array(PFS_host_array * array)221 int PFS_host_allocator::alloc_array(PFS_host_array *array)
222 {
223   size_t size= array->m_max;
224   PFS_host *pfs;
225   size_t index;
226   size_t waits_sizing= size * wait_class_max;
227   size_t stages_sizing= size * stage_class_max;
228   size_t statements_sizing= size * statement_class_max;
229   size_t transactions_sizing= size * transaction_class_max;
230   size_t memory_sizing= size * memory_class_max;
231 
232   array->m_ptr= NULL;
233   array->m_full= true;
234   array->m_instr_class_waits_array= NULL;
235   array->m_instr_class_stages_array= NULL;
236   array->m_instr_class_statements_array= NULL;
237   array->m_instr_class_transactions_array= NULL;
238   array->m_instr_class_memory_array= NULL;
239 
240   if (size > 0)
241   {
242     array->m_ptr=
243       PFS_MALLOC_ARRAY(& builtin_memory_host,
244                        size, sizeof(PFS_host), PFS_host, MYF(MY_ZEROFILL));
245     if (array->m_ptr == NULL)
246       return 1;
247   }
248 
249   if (waits_sizing > 0)
250   {
251     array->m_instr_class_waits_array=
252       PFS_MALLOC_ARRAY(& builtin_memory_host_waits,
253                        waits_sizing, sizeof(PFS_single_stat), PFS_single_stat, MYF(MY_ZEROFILL));
254     if (array->m_instr_class_waits_array == NULL)
255       return 1;
256 
257     for (index=0; index < waits_sizing; index++)
258       array->m_instr_class_waits_array[index].reset();
259   }
260 
261   if (stages_sizing > 0)
262   {
263     array->m_instr_class_stages_array=
264       PFS_MALLOC_ARRAY(& builtin_memory_host_stages,
265                        stages_sizing, sizeof(PFS_stage_stat), PFS_stage_stat, MYF(MY_ZEROFILL));
266     if (array->m_instr_class_stages_array == NULL)
267       return 1;
268 
269     for (index=0; index < stages_sizing; index++)
270       array->m_instr_class_stages_array[index].reset();
271   }
272 
273   if (statements_sizing > 0)
274   {
275     array->m_instr_class_statements_array=
276       PFS_MALLOC_ARRAY(& builtin_memory_host_statements,
277                        statements_sizing, sizeof(PFS_statement_stat), PFS_statement_stat, MYF(MY_ZEROFILL));
278     if (array->m_instr_class_statements_array == NULL)
279       return 1;
280 
281     for (index=0; index < statements_sizing; index++)
282       array->m_instr_class_statements_array[index].reset();
283   }
284 
285   if (transactions_sizing > 0)
286   {
287     array->m_instr_class_transactions_array=
288       PFS_MALLOC_ARRAY(& builtin_memory_host_transactions,
289                        transactions_sizing, sizeof(PFS_transaction_stat), PFS_transaction_stat, MYF(MY_ZEROFILL));
290     if (array->m_instr_class_transactions_array == NULL)
291       return 1;
292 
293     for (index=0; index < transactions_sizing; index++)
294       array->m_instr_class_transactions_array[index].reset();
295   }
296 
297   if (memory_sizing > 0)
298   {
299     array->m_instr_class_memory_array=
300       PFS_MALLOC_ARRAY(& builtin_memory_host_memory,
301                        memory_sizing, sizeof(PFS_memory_stat), PFS_memory_stat, MYF(MY_ZEROFILL));
302     if (array->m_instr_class_memory_array == NULL)
303       return 1;
304 
305     for (index=0; index < memory_sizing; index++)
306       array->m_instr_class_memory_array[index].reset();
307   }
308 
309   for (index= 0; index < size; index++)
310   {
311     pfs= & array->m_ptr[index];
312 
313     pfs->set_instr_class_waits_stats(
314       & array->m_instr_class_waits_array[index * wait_class_max]);
315     pfs->set_instr_class_stages_stats(
316       & array->m_instr_class_stages_array[index * stage_class_max]);
317     pfs->set_instr_class_statements_stats(
318       & array->m_instr_class_statements_array[index * statement_class_max]);
319     pfs->set_instr_class_transactions_stats(
320       & array->m_instr_class_transactions_array[index * transaction_class_max]);
321     pfs->set_instr_class_memory_stats(
322       & array->m_instr_class_memory_array[index * memory_class_max]);
323   }
324 
325   array->m_full= false;
326   return 0;
327 }
328 
free_array(PFS_host_array * array)329 void PFS_host_allocator::free_array(PFS_host_array *array)
330 {
331   size_t size= array->m_max;
332   size_t waits_sizing= size * wait_class_max;
333   size_t stages_sizing= size * stage_class_max;
334   size_t statements_sizing= size * statement_class_max;
335   size_t transactions_sizing= size * transaction_class_max;
336   size_t memory_sizing= size * memory_class_max;
337 
338   PFS_FREE_ARRAY(& builtin_memory_host,
339                  size, sizeof(PFS_host), array->m_ptr);
340   array->m_ptr= NULL;
341 
342   PFS_FREE_ARRAY(& builtin_memory_host_waits,
343                  waits_sizing, sizeof(PFS_single_stat),
344                  array->m_instr_class_waits_array);
345   array->m_instr_class_waits_array= NULL;
346 
347   PFS_FREE_ARRAY(& builtin_memory_host_stages,
348                  stages_sizing, sizeof(PFS_stage_stat),
349                  array->m_instr_class_stages_array);
350   array->m_instr_class_stages_array= NULL;
351 
352   PFS_FREE_ARRAY(& builtin_memory_host_statements,
353                  statements_sizing, sizeof(PFS_statement_stat),
354                  array->m_instr_class_statements_array);
355   array->m_instr_class_statements_array= NULL;
356 
357   PFS_FREE_ARRAY(& builtin_memory_host_transactions,
358                  transactions_sizing, sizeof(PFS_transaction_stat),
359                  array->m_instr_class_transactions_array);
360   array->m_instr_class_transactions_array= NULL;
361 
362   PFS_FREE_ARRAY(& builtin_memory_host_memory,
363                  memory_sizing, sizeof(PFS_memory_stat),
364                  array->m_instr_class_memory_array);
365   array->m_instr_class_memory_array= NULL;
366 }
367 
368 PFS_host_allocator host_allocator;
369 PFS_host_container global_host_container(& host_allocator);
370 
alloc_array(PFS_thread_array * array)371 int PFS_thread_allocator::alloc_array(PFS_thread_array *array)
372 {
373   size_t size= array->m_max;
374   PFS_thread *pfs;
375   PFS_events_statements *pfs_stmt;
376   unsigned char *pfs_tokens;
377 
378   size_t index;
379   size_t waits_sizing= size * wait_class_max;
380   size_t stages_sizing= size * stage_class_max;
381   size_t statements_sizing= size * statement_class_max;
382   size_t transactions_sizing= size * transaction_class_max;
383   size_t memory_sizing= size * memory_class_max;
384 
385   size_t waits_history_sizing= size * events_waits_history_per_thread;
386   size_t stages_history_sizing= size * events_stages_history_per_thread;
387   size_t statements_history_sizing= size * events_statements_history_per_thread;
388   size_t statements_stack_sizing= size * statement_stack_max;
389   size_t transactions_history_sizing= size * events_transactions_history_per_thread;
390   size_t session_connect_attrs_sizing= size * session_connect_attrs_size_per_thread;
391 
392   size_t current_sqltext_sizing= size * pfs_max_sqltext * statement_stack_max;
393   size_t history_sqltext_sizing= size * pfs_max_sqltext * events_statements_history_per_thread;
394   size_t current_digest_tokens_sizing= size * pfs_max_digest_length * statement_stack_max;
395   size_t history_digest_tokens_sizing= size * pfs_max_digest_length * events_statements_history_per_thread;
396 
397   array->m_ptr= NULL;
398   array->m_full= true;
399   array->m_instr_class_waits_array= NULL;
400   array->m_instr_class_stages_array= NULL;
401   array->m_instr_class_statements_array= NULL;
402   array->m_instr_class_transactions_array= NULL;
403   array->m_instr_class_memory_array= NULL;
404 
405   array->m_waits_history_array= NULL;
406   array->m_stages_history_array= NULL;
407   array->m_statements_history_array= NULL;
408   array->m_statements_stack_array= NULL;
409   array->m_transactions_history_array= NULL;
410   array->m_session_connect_attrs_array= NULL;
411 
412   array->m_current_stmts_text_array= NULL;
413   array->m_current_stmts_digest_token_array= NULL;
414   array->m_history_stmts_text_array= NULL;
415   array->m_history_stmts_digest_token_array= NULL;
416 
417   if (size > 0)
418   {
419     array->m_ptr=
420       PFS_MALLOC_ARRAY(& builtin_memory_thread,
421                        size, sizeof(PFS_thread), PFS_thread, MYF(MY_ZEROFILL));
422     if (array->m_ptr == NULL)
423       return 1;
424   }
425 
426   if (waits_sizing > 0)
427   {
428     array->m_instr_class_waits_array=
429       PFS_MALLOC_ARRAY(& builtin_memory_thread_waits,
430                        waits_sizing, sizeof(PFS_single_stat), PFS_single_stat, MYF(MY_ZEROFILL));
431     if (array->m_instr_class_waits_array == NULL)
432       return 1;
433 
434     for (index=0; index < waits_sizing; index++)
435       array->m_instr_class_waits_array[index].reset();
436   }
437 
438   if (stages_sizing > 0)
439   {
440     array->m_instr_class_stages_array=
441       PFS_MALLOC_ARRAY(& builtin_memory_thread_stages,
442                        stages_sizing, sizeof(PFS_stage_stat), PFS_stage_stat, MYF(MY_ZEROFILL));
443     if (array->m_instr_class_stages_array == NULL)
444       return 1;
445 
446     for (index=0; index < stages_sizing; index++)
447       array->m_instr_class_stages_array[index].reset();
448   }
449 
450   if (statements_sizing > 0)
451   {
452     array->m_instr_class_statements_array=
453       PFS_MALLOC_ARRAY(& builtin_memory_thread_statements,
454                        statements_sizing, sizeof(PFS_statement_stat), PFS_statement_stat, MYF(MY_ZEROFILL));
455     if (array->m_instr_class_statements_array == NULL)
456       return 1;
457 
458     for (index=0; index < statements_sizing; index++)
459       array->m_instr_class_statements_array[index].reset();
460   }
461 
462   if (transactions_sizing > 0)
463   {
464     array->m_instr_class_transactions_array=
465       PFS_MALLOC_ARRAY(& builtin_memory_thread_transactions,
466                        transactions_sizing, sizeof(PFS_transaction_stat), PFS_transaction_stat, MYF(MY_ZEROFILL));
467     if (array->m_instr_class_transactions_array == NULL)
468       return 1;
469 
470     for (index=0; index < transactions_sizing; index++)
471       array->m_instr_class_transactions_array[index].reset();
472   }
473 
474   if (memory_sizing > 0)
475   {
476     array->m_instr_class_memory_array=
477       PFS_MALLOC_ARRAY(& builtin_memory_thread_memory,
478                        memory_sizing, sizeof(PFS_memory_stat), PFS_memory_stat, MYF(MY_ZEROFILL));
479     if (array->m_instr_class_memory_array == NULL)
480       return 1;
481 
482     for (index=0; index < memory_sizing; index++)
483       array->m_instr_class_memory_array[index].reset();
484   }
485 
486   if (waits_history_sizing > 0)
487   {
488     array->m_waits_history_array=
489       PFS_MALLOC_ARRAY(& builtin_memory_thread_waits_history,
490                        waits_history_sizing, sizeof(PFS_events_waits), PFS_events_waits, MYF(MY_ZEROFILL));
491     if (unlikely(array->m_waits_history_array == NULL))
492       return 1;
493   }
494 
495   if (stages_history_sizing > 0)
496   {
497     array->m_stages_history_array=
498       PFS_MALLOC_ARRAY(& builtin_memory_thread_stages_history,
499                        stages_history_sizing, sizeof(PFS_events_stages), PFS_events_stages, MYF(MY_ZEROFILL));
500     if (unlikely(array->m_stages_history_array == NULL))
501       return 1;
502   }
503 
504   if (statements_history_sizing > 0)
505   {
506     array->m_statements_history_array=
507       PFS_MALLOC_ARRAY(& builtin_memory_thread_statements_history,
508                        statements_history_sizing, sizeof(PFS_events_statements), PFS_events_statements, MYF(MY_ZEROFILL));
509     if (unlikely(array->m_statements_history_array == NULL))
510       return 1;
511   }
512 
513   if (statements_stack_sizing > 0)
514   {
515     array->m_statements_stack_array=
516       PFS_MALLOC_ARRAY(& builtin_memory_thread_statements_stack,
517                        statements_stack_sizing, sizeof(PFS_events_statements), PFS_events_statements, MYF(MY_ZEROFILL));
518     if (unlikely(array->m_statements_stack_array == NULL))
519       return 1;
520   }
521 
522   if (transactions_history_sizing > 0)
523   {
524     array->m_transactions_history_array=
525       PFS_MALLOC_ARRAY(& builtin_memory_thread_transaction_history,
526                        transactions_history_sizing, sizeof(PFS_events_transactions), PFS_events_transactions, MYF(MY_ZEROFILL));
527     if (unlikely(array->m_transactions_history_array == NULL))
528       return 1;
529   }
530 
531   if (session_connect_attrs_sizing > 0)
532   {
533     array->m_session_connect_attrs_array=
534       (char *)pfs_malloc(& builtin_memory_thread_session_connect_attrs,
535                          session_connect_attrs_sizing, MYF(MY_ZEROFILL));
536     if (unlikely(array->m_session_connect_attrs_array == NULL))
537       return 1;
538   }
539 
540   if (current_sqltext_sizing > 0)
541   {
542     array->m_current_stmts_text_array=
543       (char *)pfs_malloc(& builtin_memory_thread_statements_stack_sqltext,
544                          current_sqltext_sizing, MYF(MY_ZEROFILL));
545     if (unlikely(array->m_current_stmts_text_array == NULL))
546       return 1;
547   }
548 
549   if (history_sqltext_sizing > 0)
550   {
551     array->m_history_stmts_text_array=
552       (char *)pfs_malloc(& builtin_memory_thread_statements_history_sqltext,
553                          history_sqltext_sizing, MYF(MY_ZEROFILL));
554     if (unlikely(array->m_history_stmts_text_array == NULL))
555       return 1;
556   }
557 
558   if (current_digest_tokens_sizing > 0)
559   {
560     array->m_current_stmts_digest_token_array=
561       (unsigned char *)pfs_malloc(& builtin_memory_thread_statements_stack_tokens,
562                                   current_digest_tokens_sizing, MYF(MY_ZEROFILL));
563     if (unlikely(array->m_current_stmts_digest_token_array == NULL))
564       return 1;
565   }
566 
567   if (history_digest_tokens_sizing > 0)
568   {
569     array->m_history_stmts_digest_token_array=
570       (unsigned char *)pfs_malloc(& builtin_memory_thread_statements_history_tokens,
571                                   history_digest_tokens_sizing, MYF(MY_ZEROFILL));
572     if (unlikely(array->m_history_stmts_digest_token_array == NULL))
573       return 1;
574   }
575 
576   for (index= 0; index < size; index++)
577   {
578     pfs= & array->m_ptr[index];
579 
580     pfs->set_instr_class_waits_stats(
581       & array->m_instr_class_waits_array[index * wait_class_max]);
582     pfs->set_instr_class_stages_stats(
583       & array->m_instr_class_stages_array[index * stage_class_max]);
584     pfs->set_instr_class_statements_stats(
585       & array->m_instr_class_statements_array[index * statement_class_max]);
586     pfs->set_instr_class_transactions_stats(
587       & array->m_instr_class_transactions_array[index * transaction_class_max]);
588     pfs->set_instr_class_memory_stats(
589       & array->m_instr_class_memory_array[index * memory_class_max]);
590 
591     pfs->m_waits_history=
592       & array->m_waits_history_array[index * events_waits_history_per_thread];
593     pfs->m_stages_history=
594       & array->m_stages_history_array[index * events_stages_history_per_thread];
595     pfs->m_statements_history=
596       & array->m_statements_history_array[index * events_statements_history_per_thread];
597     pfs->m_statement_stack=
598       & array->m_statements_stack_array[index * statement_stack_max];
599     pfs->m_transactions_history=
600       & array->m_transactions_history_array[index * events_transactions_history_per_thread];
601     pfs->m_session_connect_attrs=
602       & array->m_session_connect_attrs_array[index * session_connect_attrs_size_per_thread];
603   }
604 
605   for (index= 0; index < statements_stack_sizing; index++)
606   {
607     pfs_stmt= & array->m_statements_stack_array[index];
608 
609     pfs_stmt->m_sqltext= & array->m_current_stmts_text_array[index * pfs_max_sqltext];
610 
611     pfs_tokens= & array->m_current_stmts_digest_token_array[index * pfs_max_digest_length];
612     pfs_stmt->m_digest_storage.reset(pfs_tokens, pfs_max_digest_length);
613   }
614 
615   for (index= 0; index < statements_history_sizing; index++)
616   {
617     pfs_stmt= & array->m_statements_history_array[index];
618 
619     pfs_stmt->m_sqltext= & array->m_history_stmts_text_array[index * pfs_max_sqltext];
620 
621     pfs_tokens= & array->m_history_stmts_digest_token_array[index * pfs_max_digest_length];
622     pfs_stmt->m_digest_storage.reset(pfs_tokens, pfs_max_digest_length);
623   }
624 
625   array->m_full= false;
626   return 0;
627 }
628 
free_array(PFS_thread_array * array)629 void PFS_thread_allocator::free_array(PFS_thread_array *array)
630 {
631   size_t size= array->m_max;
632   size_t waits_sizing= size * wait_class_max;
633   size_t stages_sizing= size * stage_class_max;
634   size_t statements_sizing= size * statement_class_max;
635   size_t transactions_sizing= size * transaction_class_max;
636   size_t memory_sizing= size * memory_class_max;
637 
638   size_t waits_history_sizing= size * events_waits_history_per_thread;
639   size_t stages_history_sizing= size * events_stages_history_per_thread;
640   size_t statements_history_sizing= size * events_statements_history_per_thread;
641   size_t statements_stack_sizing= size * statement_stack_max;
642   size_t transactions_history_sizing= size * events_transactions_history_per_thread;
643   size_t session_connect_attrs_sizing= size * session_connect_attrs_size_per_thread;
644 
645   size_t current_sqltext_sizing= size * pfs_max_sqltext * statement_stack_max;
646   size_t history_sqltext_sizing= size * pfs_max_sqltext * events_statements_history_per_thread;
647   size_t current_digest_tokens_sizing= size * pfs_max_digest_length * statement_stack_max;
648   size_t history_digest_tokens_sizing= size * pfs_max_digest_length * events_statements_history_per_thread;
649 
650   PFS_FREE_ARRAY(& builtin_memory_thread,
651                  size, sizeof(PFS_thread), array->m_ptr);
652   array->m_ptr= NULL;
653 
654   PFS_FREE_ARRAY(& builtin_memory_thread_waits,
655                  waits_sizing, sizeof(PFS_single_stat),
656                  array->m_instr_class_waits_array);
657   array->m_instr_class_waits_array= NULL;
658 
659   PFS_FREE_ARRAY(& builtin_memory_thread_stages,
660                  stages_sizing, sizeof(PFS_stage_stat),
661                  array->m_instr_class_stages_array);
662   array->m_instr_class_stages_array= NULL;
663 
664   PFS_FREE_ARRAY(& builtin_memory_thread_statements,
665                  statements_sizing, sizeof(PFS_statement_stat),
666                  array->m_instr_class_statements_array);
667   array->m_instr_class_statements_array= NULL;
668 
669   PFS_FREE_ARRAY(& builtin_memory_thread_transactions,
670                  transactions_sizing, sizeof(PFS_transaction_stat),
671                  array->m_instr_class_transactions_array);
672   array->m_instr_class_transactions_array= NULL;
673 
674   PFS_FREE_ARRAY(& builtin_memory_thread_memory,
675                  memory_sizing, sizeof(PFS_memory_stat),
676                  array->m_instr_class_memory_array);
677   array->m_instr_class_memory_array= NULL;
678 
679 
680   PFS_FREE_ARRAY(& builtin_memory_thread_waits_history,
681                  waits_history_sizing, sizeof(PFS_events_waits),
682                  array->m_waits_history_array);
683   array->m_waits_history_array= NULL;
684 
685   PFS_FREE_ARRAY(& builtin_memory_thread_stages_history,
686                  stages_history_sizing, sizeof(PFS_events_stages),
687                  array->m_stages_history_array);
688   array->m_stages_history_array= NULL;
689 
690   PFS_FREE_ARRAY(& builtin_memory_thread_statements_history,
691                  statements_history_sizing, sizeof(PFS_events_statements),
692                  array->m_statements_history_array);
693   array->m_statements_history_array= NULL;
694 
695   PFS_FREE_ARRAY(& builtin_memory_thread_statements_stack,
696                  statements_stack_sizing, sizeof(PFS_events_statements),
697                  array->m_statements_stack_array);
698   array->m_statements_stack_array= NULL;
699 
700   PFS_FREE_ARRAY(& builtin_memory_thread_transaction_history,
701                  transactions_history_sizing, sizeof(PFS_events_transactions),
702                  array->m_transactions_history_array);
703   array->m_transactions_history_array= NULL;
704 
705   pfs_free(& builtin_memory_thread_session_connect_attrs,
706            session_connect_attrs_sizing,
707            array->m_session_connect_attrs_array);
708   array->m_session_connect_attrs_array= NULL;
709 
710   pfs_free(& builtin_memory_thread_statements_stack_sqltext,
711            current_sqltext_sizing,
712            array->m_current_stmts_text_array);
713   array->m_current_stmts_text_array= NULL;
714 
715   pfs_free(& builtin_memory_thread_statements_history_sqltext,
716            history_sqltext_sizing,
717           array->m_history_stmts_text_array);
718   array->m_history_stmts_text_array= NULL;
719 
720   pfs_free(& builtin_memory_thread_statements_stack_tokens,
721            current_digest_tokens_sizing,
722            array->m_current_stmts_digest_token_array);
723   array->m_current_stmts_digest_token_array= NULL;
724 
725   pfs_free(& builtin_memory_thread_statements_history_tokens,
726            history_digest_tokens_sizing,
727            array->m_history_stmts_digest_token_array);
728   array->m_history_stmts_digest_token_array= NULL;
729 }
730 
731 PFS_thread_allocator thread_allocator;
732 PFS_thread_container global_thread_container(& thread_allocator);
733 
alloc_array(PFS_user_array * array)734 int PFS_user_allocator::alloc_array(PFS_user_array *array)
735 {
736   size_t size= array->m_max;
737   PFS_user *pfs;
738   size_t index;
739   size_t waits_sizing= size * wait_class_max;
740   size_t stages_sizing= size * stage_class_max;
741   size_t statements_sizing= size * statement_class_max;
742   size_t transactions_sizing= size * transaction_class_max;
743   size_t memory_sizing= size * memory_class_max;
744 
745   array->m_ptr= NULL;
746   array->m_full= true;
747   array->m_instr_class_waits_array= NULL;
748   array->m_instr_class_stages_array= NULL;
749   array->m_instr_class_statements_array= NULL;
750   array->m_instr_class_transactions_array= NULL;
751   array->m_instr_class_memory_array= NULL;
752 
753   if (size > 0)
754   {
755     array->m_ptr=
756       PFS_MALLOC_ARRAY(& builtin_memory_user,
757                        size, sizeof(PFS_user), PFS_user, MYF(MY_ZEROFILL));
758     if (array->m_ptr == NULL)
759       return 1;
760   }
761 
762   if (waits_sizing > 0)
763   {
764     array->m_instr_class_waits_array=
765       PFS_MALLOC_ARRAY(& builtin_memory_user_waits,
766                        waits_sizing, sizeof(PFS_single_stat), PFS_single_stat, MYF(MY_ZEROFILL));
767     if (array->m_instr_class_waits_array == NULL)
768       return 1;
769 
770     for (index=0; index < waits_sizing; index++)
771       array->m_instr_class_waits_array[index].reset();
772   }
773 
774   if (stages_sizing > 0)
775   {
776     array->m_instr_class_stages_array=
777       PFS_MALLOC_ARRAY(& builtin_memory_user_stages,
778                        stages_sizing, sizeof(PFS_stage_stat), PFS_stage_stat, MYF(MY_ZEROFILL));
779     if (array->m_instr_class_stages_array == NULL)
780       return 1;
781 
782     for (index=0; index < stages_sizing; index++)
783       array->m_instr_class_stages_array[index].reset();
784   }
785 
786   if (statements_sizing > 0)
787   {
788     array->m_instr_class_statements_array=
789       PFS_MALLOC_ARRAY(& builtin_memory_user_statements,
790                        statements_sizing, sizeof(PFS_statement_stat), PFS_statement_stat, MYF(MY_ZEROFILL));
791     if (array->m_instr_class_statements_array == NULL)
792       return 1;
793 
794     for (index=0; index < statements_sizing; index++)
795       array->m_instr_class_statements_array[index].reset();
796   }
797 
798   if (transactions_sizing > 0)
799   {
800     array->m_instr_class_transactions_array=
801       PFS_MALLOC_ARRAY(& builtin_memory_user_transactions,
802                        transactions_sizing, sizeof(PFS_transaction_stat), PFS_transaction_stat, MYF(MY_ZEROFILL));
803     if (array->m_instr_class_transactions_array == NULL)
804       return 1;
805 
806     for (index=0; index < transactions_sizing; index++)
807       array->m_instr_class_transactions_array[index].reset();
808   }
809 
810   if (memory_sizing > 0)
811   {
812     array->m_instr_class_memory_array=
813       PFS_MALLOC_ARRAY(& builtin_memory_user_memory,
814                        memory_sizing, sizeof(PFS_memory_stat), PFS_memory_stat, MYF(MY_ZEROFILL));
815     if (array->m_instr_class_memory_array == NULL)
816       return 1;
817 
818     for (index=0; index < memory_sizing; index++)
819       array->m_instr_class_memory_array[index].reset();
820   }
821 
822   for (index= 0; index < size; index++)
823   {
824     pfs= & array->m_ptr[index];
825 
826     pfs->set_instr_class_waits_stats(
827       & array->m_instr_class_waits_array[index * wait_class_max]);
828     pfs->set_instr_class_stages_stats(
829       & array->m_instr_class_stages_array[index * stage_class_max]);
830     pfs->set_instr_class_statements_stats(
831       & array->m_instr_class_statements_array[index * statement_class_max]);
832     pfs->set_instr_class_transactions_stats(
833       & array->m_instr_class_transactions_array[index * transaction_class_max]);
834     pfs->set_instr_class_memory_stats(
835       & array->m_instr_class_memory_array[index * memory_class_max]);
836   }
837 
838   array->m_full= false;
839   return 0;
840 }
841 
free_array(PFS_user_array * array)842 void PFS_user_allocator::free_array(PFS_user_array *array)
843 {
844   size_t size= array->m_max;
845   size_t waits_sizing= size * wait_class_max;
846   size_t stages_sizing= size * stage_class_max;
847   size_t statements_sizing= size * statement_class_max;
848   size_t transactions_sizing= size * transaction_class_max;
849   size_t memory_sizing= size * memory_class_max;
850 
851   PFS_FREE_ARRAY(& builtin_memory_user,
852                  size, sizeof(PFS_user), array->m_ptr);
853   array->m_ptr= NULL;
854 
855   PFS_FREE_ARRAY(& builtin_memory_user_waits,
856                  waits_sizing, sizeof(PFS_single_stat),
857                  array->m_instr_class_waits_array);
858   array->m_instr_class_waits_array= NULL;
859 
860   PFS_FREE_ARRAY(& builtin_memory_user_stages,
861                  stages_sizing, sizeof(PFS_stage_stat),
862                  array->m_instr_class_stages_array);
863   array->m_instr_class_stages_array= NULL;
864 
865   PFS_FREE_ARRAY(& builtin_memory_user_statements,
866                  statements_sizing, sizeof(PFS_statement_stat),
867                  array->m_instr_class_statements_array);
868   array->m_instr_class_statements_array= NULL;
869 
870   PFS_FREE_ARRAY(& builtin_memory_user_transactions,
871                  transactions_sizing, sizeof(PFS_transaction_stat),
872                  array->m_instr_class_transactions_array);
873   array->m_instr_class_transactions_array= NULL;
874 
875   PFS_FREE_ARRAY(& builtin_memory_user_memory,
876                  memory_sizing, sizeof(PFS_memory_stat),
877                  array->m_instr_class_memory_array);
878   array->m_instr_class_memory_array= NULL;
879 }
880 
881 PFS_user_allocator user_allocator;
882 PFS_user_container global_user_container(& user_allocator);
883 
884