File: pfs_server.cc

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (467 lines) | stat: -rw-r--r-- 16,178 bytes parent folder | download
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* Copyright (c) 2008, 2025, Oracle and/or its affiliates.

  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 designed to work 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 either included with
  the program or referenced in the documentation.

  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, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

/**
  @file storage/perfschema/pfs_server.cc
  Private interface for the server (implementation).
*/

#include "storage/perfschema/pfs_server.h"

#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_macros.h"
#include "my_sys.h"
#include "mysys_err.h"
#include "sql/mysqld.h"
#include "storage/perfschema/mysql_server_telemetry_traces_service_imp.h"
#include "storage/perfschema/pfs.h"
#include "storage/perfschema/pfs_account.h"
#include "storage/perfschema/pfs_builtin_memory.h"
#include "storage/perfschema/pfs_defaults.h"
#include "storage/perfschema/pfs_digest.h"
#include "storage/perfschema/pfs_engine_table.h"
#include "storage/perfschema/pfs_error.h"
#include "storage/perfschema/pfs_events_stages.h"
#include "storage/perfschema/pfs_events_statements.h"
#include "storage/perfschema/pfs_events_transactions.h"
#include "storage/perfschema/pfs_events_waits.h"
#include "storage/perfschema/pfs_global.h"
#include "storage/perfschema/pfs_host.h"
#include "storage/perfschema/pfs_instr.h"
#include "storage/perfschema/pfs_instr_class.h"
#include "storage/perfschema/pfs_plugin_table.h"
#include "storage/perfschema/pfs_prepared_stmt.h"
#include "storage/perfschema/pfs_program.h"
#include "storage/perfschema/pfs_setup_actor.h"
#include "storage/perfschema/pfs_setup_object.h"
#include "storage/perfschema/pfs_timer.h"
#include "storage/perfschema/pfs_tls_channel.h"
#include "storage/perfschema/pfs_user.h"
#include "template_utils.h"

PFS_global_param pfs_param;

PFS_table_stat PFS_table_stat::g_reset_template;

static void cleanup_performance_schema();
void cleanup_instrument_config();

void pre_initialize_performance_schema() {
  record_main_thread_id();

  pfs_initialized = false;

  init_all_builtin_memory_class();

  PFS_table_stat::g_reset_template.reset();
  global_idle_stat.reset();
  global_table_io_stat.reset();
  global_table_lock_stat.reset();
  g_histogram_pico_timers.init();
  global_statements_histogram.reset();

  /*
    There is no automatic cleanup. Please either use:
    - my_thread_end()
    - or PSI_server->delete_current_thread()
    in the instrumented code, to explicitly cleanup the instrumentation.
  */
  THR_PFS = nullptr;
}

int initialize_performance_schema(
    PFS_global_param *param, PSI_thread_bootstrap **thread_bootstrap,
    PSI_mutex_bootstrap **mutex_bootstrap,
    PSI_rwlock_bootstrap **rwlock_bootstrap,
    PSI_cond_bootstrap **cond_bootstrap, PSI_file_bootstrap **file_bootstrap,
    PSI_socket_bootstrap **socket_bootstrap,
    PSI_table_bootstrap **table_bootstrap, PSI_mdl_bootstrap **mdl_bootstrap,
    PSI_idle_bootstrap **idle_bootstrap, PSI_stage_bootstrap **stage_bootstrap,
    PSI_statement_bootstrap **statement_bootstrap,
    PSI_transaction_bootstrap **transaction_bootstrap,
    PSI_memory_bootstrap **memory_bootstrap,
    PSI_error_bootstrap **error_bootstrap,
    PSI_data_lock_bootstrap **data_lock_bootstrap,
    PSI_system_bootstrap **system_bootstrap,
    PSI_tls_channel_bootstrap **tls_channel_bootstrap) {
  bool init_failed = false;

  *thread_bootstrap = nullptr;
  *mutex_bootstrap = nullptr;
  *rwlock_bootstrap = nullptr;
  *cond_bootstrap = nullptr;
  *file_bootstrap = nullptr;
  *socket_bootstrap = nullptr;
  *table_bootstrap = nullptr;
  *mdl_bootstrap = nullptr;
  *idle_bootstrap = nullptr;
  *stage_bootstrap = nullptr;
  *statement_bootstrap = nullptr;
  *transaction_bootstrap = nullptr;
  *memory_bootstrap = nullptr;
  *error_bootstrap = nullptr;
  *data_lock_bootstrap = nullptr;
  *system_bootstrap = nullptr;
  *tls_channel_bootstrap = nullptr;

  pfs_enabled = param->m_enabled;

  pfs_automated_sizing(param);
  init_timers();
  init_event_name_sizing(param);
  register_global_classes();

  if (init_sync_class(param->m_mutex_class_sizing, param->m_rwlock_class_sizing,
                      param->m_cond_class_sizing) ||
      init_thread_class(param->m_thread_class_sizing) ||
      init_table_share(param->m_table_share_sizing) ||
      init_table_share_lock_stat(param->m_table_lock_stat_sizing) ||
      init_table_share_index_stat(param->m_index_stat_sizing) ||
      init_file_class(param->m_file_class_sizing) ||
      init_stage_class(param->m_stage_class_sizing) ||
      init_statement_class(param->m_statement_class_sizing) ||
      init_socket_class(param->m_socket_class_sizing) ||
      init_memory_class(param->m_memory_class_sizing) ||
      init_instruments(param) ||
      init_events_waits_history_long(
          param->m_events_waits_history_long_sizing) ||
      init_events_stages_history_long(
          param->m_events_stages_history_long_sizing) ||
      init_events_statements_history_long(
          param->m_events_statements_history_long_sizing) ||
      init_events_transactions_history_long(
          param->m_events_transactions_history_long_sizing) ||
      init_file_hash(param) || init_table_share_hash(param) ||
      init_setup_actor(param) || init_setup_actor_hash(param) ||
      init_setup_object(param) || init_setup_object_hash(param) ||
      init_host(param) || init_host_hash(param) || init_user(param) ||
      init_user_hash(param) || init_account(param) ||
      init_account_hash(param) || init_digest(param) ||
      init_digest_hash(param) || init_program(param) ||
      init_program_hash(param) || init_prepared_stmt(param) ||
      init_error(param)) {
    /*
      The performance schema initialization failed.
      Free the memory used, and disable the instrumentation.
    */
    cleanup_performance_schema();
    init_failed = true;
  }

  if (param->m_enabled && !init_failed) {
    /** Default values for SETUP_CONSUMERS */
    flag_events_stages_current =
        param->m_consumer_events_stages_current_enabled;
    flag_events_stages_history =
        param->m_consumer_events_stages_history_enabled;
    flag_events_stages_history_long =
        param->m_consumer_events_stages_history_long_enabled;
    flag_events_statements_cpu =
        param->m_consumer_events_statements_cpu_enabled;
    flag_events_statements_current =
        param->m_consumer_events_statements_current_enabled;
    flag_events_statements_history =
        param->m_consumer_events_statements_history_enabled;
    flag_events_statements_history_long =
        param->m_consumer_events_statements_history_long_enabled;
    flag_events_transactions_current =
        param->m_consumer_events_transactions_current_enabled;
    flag_events_transactions_history =
        param->m_consumer_events_transactions_history_enabled;
    flag_events_transactions_history_long =
        param->m_consumer_events_transactions_history_long_enabled;
    flag_events_waits_current = param->m_consumer_events_waits_current_enabled;
    flag_events_waits_history = param->m_consumer_events_waits_history_enabled;
    flag_events_waits_history_long =
        param->m_consumer_events_waits_history_long_enabled;
    flag_global_instrumentation =
        param->m_consumer_global_instrumentation_enabled;
    flag_thread_instrumentation =
        param->m_consumer_thread_instrumentation_enabled;
    flag_statements_digest = param->m_consumer_statement_digest_enabled;
  } else {
    flag_events_stages_current = false;
    flag_events_stages_history = false;
    flag_events_stages_history_long = false;
    flag_events_statements_cpu = false;
    flag_events_statements_current = false;
    flag_events_statements_history = false;
    flag_events_statements_history_long = false;
    flag_events_transactions_current = false;
    flag_events_transactions_history = false;
    flag_events_transactions_history_long = false;
    flag_events_waits_current = false;
    flag_events_waits_history = false;
    flag_events_waits_history_long = false;
    flag_global_instrumentation = false;
    flag_thread_instrumentation = false;
    flag_statements_digest = false;
  }

  if (!init_failed) {
    pfs_initialized = true;

    if (param->m_enabled) {
      install_default_setup(&pfs_thread_bootstrap);
      *thread_bootstrap = &pfs_thread_bootstrap;
      *mutex_bootstrap = &pfs_mutex_bootstrap;
      *rwlock_bootstrap = &pfs_rwlock_bootstrap;
      *cond_bootstrap = &pfs_cond_bootstrap;
      *file_bootstrap = &pfs_file_bootstrap;
      *socket_bootstrap = &pfs_socket_bootstrap;
      *table_bootstrap = &pfs_table_bootstrap;
      *mdl_bootstrap = &pfs_mdl_bootstrap;
      *idle_bootstrap = &pfs_idle_bootstrap;
      *stage_bootstrap = &pfs_stage_bootstrap;
      *statement_bootstrap = &pfs_statement_bootstrap;
      *transaction_bootstrap = &pfs_transaction_bootstrap;
      *memory_bootstrap = &pfs_memory_bootstrap;
      *error_bootstrap = &pfs_error_bootstrap;
      *data_lock_bootstrap = &pfs_data_lock_bootstrap;
      *system_bootstrap = &pfs_system_bootstrap;
      *tls_channel_bootstrap = &pfs_tls_channel_bootstrap;
    }
  }

  /*
    Initialize plugin table services.
    This must be done:
    - after the memory allocations for the mutex instrumentation,
      so that mutex LOCK_pfs_share_list gets instrumented
      (if the instrumentation is enabled),
    - in all cases, even when init_failed due to out of memory errors,
      as the plugin table service is independent of
      the main performance schema instrumentation.
  */
  init_pfs_plugin_table();

  /*
     Initialize TLS channel instrumentation data structures
    This must be done:
    - after the memory allocation for rwlock instrumentation,
      so that rwlock LOCK_pfs_tls_channel gets instrumented
      (if the instrumentation is enabled),
    - Even if the RWLOCK LOCK_pfs_tls_channels ends up not instrumented,
       it still needs to be initialized.
  */
  init_pfs_tls_channels_instrumentation();

  /*
     Initialize telemetry tracing service.
    This must be done:
    - after the memory allocation for mutex instrumentation,
      so that mutex LOCK_pfs_tracing_callback gets instrumented
      (if the instrumentation is enabled),
    - Even if the mutex LOCK_pfs_tracing_callback ends up not instrumented,
       it still needs to be initialized.
  */
  initialize_mysql_server_telemetry_traces_service();

  if (init_failed) {
    return 1;
  }

  return 0;
}

static void cleanup_performance_schema() {
  /*
    my.cnf options
  */

  cleanup_instrument_config();

  /*
    All the LF_HASH
  */

  cleanup_setup_actor_hash();
  cleanup_setup_object_hash();
  cleanup_account_hash();
  cleanup_host_hash();
  cleanup_user_hash();
  cleanup_program_hash();
  cleanup_table_share_hash();
  cleanup_file_hash();
  cleanup_digest_hash();

  /*
    Then the lookup tables
  */

  cleanup_setup_actor();
  cleanup_setup_object();

  /*
    Then the history tables
  */

  cleanup_events_waits_history_long();
  cleanup_events_stages_history_long();
  cleanup_events_statements_history_long();
  cleanup_events_transactions_history_long();

  /*
    Then the various aggregations
  */

  cleanup_digest();
  cleanup_account();
  cleanup_host();
  cleanup_user();

  /*
    Then the instrument classes.
    Once a class is cleaned up,
    find_XXX_class(key)
    will return PSI_NOT_INSTRUMENTED
  */
  cleanup_mysql_server_telemetry_traces_service();
  cleanup_pfs_tls_channels_instrumentation();
  cleanup_pfs_plugin_table();
  cleanup_error();
  cleanup_program();
  cleanup_prepared_stmt();
  cleanup_sync_class();
  cleanup_thread_class();
  cleanup_table_share();
  cleanup_table_share_lock_stat();
  cleanup_table_share_index_stat();
  cleanup_file_class();
  cleanup_stage_class();
  cleanup_statement_class();
  cleanup_socket_class();
  cleanup_memory_class();

  cleanup_instruments();
}

void shutdown_performance_schema() {
  pfs_initialized = false;

  /* disable everything, especially for this thread. */
  flag_events_stages_current = false;
  flag_events_stages_history = false;
  flag_events_stages_history_long = false;
  flag_events_statements_cpu = false;
  flag_events_statements_current = false;
  flag_events_statements_history = false;
  flag_events_statements_history_long = false;
  flag_events_transactions_current = false;
  flag_events_transactions_history = false;
  flag_events_transactions_history_long = false;
  flag_events_waits_current = false;
  flag_events_waits_history = false;
  flag_events_waits_history_long = false;
  flag_global_instrumentation = false;
  flag_thread_instrumentation = false;
  flag_statements_digest = false;

  global_table_io_class.m_enabled = false;
  global_table_lock_class.m_enabled = false;
  global_idle_class.m_enabled = false;
  global_metadata_class.m_enabled = false;
  global_error_class.m_enabled = false;
  global_transaction_class.m_enabled = false;

  cleanup_performance_schema();
}

/**
  Initialize the dynamic array used to hold PFS_INSTRUMENT configuration
  options.
*/
void init_pfs_instrument_array() {
  pfs_instr_config_array = new Pfs_instr_config_array(PSI_NOT_INSTRUMENTED);
}

/**
  Deallocate the PFS_INSTRUMENT array.
*/
void cleanup_instrument_config() {
  if (pfs_instr_config_array != nullptr) {
    my_free_container_pointers(*pfs_instr_config_array);
  }
  delete pfs_instr_config_array;
  pfs_instr_config_array = nullptr;
}

/**
  Process one performance_schema_instrument configuration string. Isolate the
  instrument name, evaluate the option value, and store them in a dynamic array.
  Return 'false' for success, 'true' for error.

  @param name    Instrument name
  @param value   Configuration option: 'on', 'off', etc.
  @return 0 for success, non zero for errors
*/

int add_pfs_instr_to_array(const char *name, const char *value) {
  const size_t name_length = strlen(name);
  const size_t value_length = strlen(value);

  /* Allocate structure plus string buffers plus null terminators */
  auto *e = (PFS_instr_config *)my_malloc(
      PSI_NOT_INSTRUMENTED,
      sizeof(PFS_instr_config) + name_length + 1 + value_length + 1,
      MYF(MY_WME));
  if (!e) {
    return 1;
  }

  /* Copy the instrument name */
  e->m_name = (char *)e + sizeof(PFS_instr_config);
  memcpy(e->m_name, name, name_length);
  e->m_name_length = (uint)name_length;
  e->m_name[name_length] = '\0';

  /* Set flags accordingly */
  if (!my_strcasecmp(&my_charset_latin1, value, "counted")) {
    e->m_enabled = true;
    e->m_timed = false;
  } else if (!my_strcasecmp(&my_charset_latin1, value, "true") ||
             !my_strcasecmp(&my_charset_latin1, value, "on") ||
             !my_strcasecmp(&my_charset_latin1, value, "1") ||
             !my_strcasecmp(&my_charset_latin1, value, "yes")) {
    e->m_enabled = true;
    e->m_timed = true;
  } else if (!my_strcasecmp(&my_charset_latin1, value, "false") ||
             !my_strcasecmp(&my_charset_latin1, value, "off") ||
             !my_strcasecmp(&my_charset_latin1, value, "0") ||
             !my_strcasecmp(&my_charset_latin1, value, "no")) {
    e->m_enabled = false;
    e->m_timed = false;
  } else {
    my_free(e);
    return 1;
  }

  /* Add to the array of default startup options */
  if (pfs_instr_config_array->push_back(e)) {
    my_free(e);
    return 1;
  }

  return 0;
}