21 #include <my_global.h>
166 static volatile uint64 thread_internal_id_counter= 0;
168 static uint thread_instr_class_waits_sizing;
169 static uint thread_instr_class_stages_sizing;
170 static uint thread_instr_class_statements_sizing;
179 static char *thread_session_connect_attrs_array= NULL;
184 static bool filename_hash_inited=
false;
193 uint thread_waits_history_sizing;
194 uint thread_stages_history_sizing;
195 uint thread_statements_history_sizing;
196 uint thread_statements_stack_sizing;
197 uint thread_session_connect_attrs_sizing;
201 DBUG_ASSERT(wait_class_max != 0);
261 file_handle_array= NULL;
265 thread_waits_history_array= NULL;
266 thread_stages_history_array= NULL;
267 thread_statements_history_array= NULL;
268 thread_statements_stack_array= NULL;
269 thread_instr_class_waits_array= NULL;
270 thread_instr_class_stages_array= NULL;
271 thread_instr_class_statements_array= NULL;
272 thread_internal_id_counter= 0;
277 if (unlikely(mutex_array == NULL))
284 if (unlikely(rwlock_array == NULL))
291 if (unlikely(cond_array == NULL))
298 if (unlikely(file_array == NULL))
305 if (unlikely(file_handle_array == NULL))
312 if (unlikely(table_array == NULL))
319 if (unlikely(socket_array == NULL))
326 if (unlikely(thread_array == NULL))
330 if (thread_waits_history_sizing > 0)
332 thread_waits_history_array=
335 if (unlikely(thread_waits_history_array == NULL))
339 if (thread_instr_class_waits_sizing > 0)
341 thread_instr_class_waits_array=
344 if (unlikely(thread_instr_class_waits_array == NULL))
347 for (index= 0; index < thread_instr_class_waits_sizing; index++)
348 thread_instr_class_waits_array[index].reset();
351 if (thread_stages_history_sizing > 0)
353 thread_stages_history_array=
356 if (unlikely(thread_stages_history_array == NULL))
360 if (thread_instr_class_stages_sizing > 0)
362 thread_instr_class_stages_array=
365 if (unlikely(thread_instr_class_stages_array == NULL))
368 for (index= 0; index < thread_instr_class_stages_sizing; index++)
369 thread_instr_class_stages_array[index].reset();
372 if (thread_statements_history_sizing > 0)
374 thread_statements_history_array=
377 if (unlikely(thread_statements_history_array == NULL))
381 if (thread_statements_stack_sizing > 0)
383 thread_statements_stack_array=
386 if (unlikely(thread_statements_stack_array == NULL))
390 if (thread_instr_class_statements_sizing > 0)
392 thread_instr_class_statements_array=
395 if (unlikely(thread_instr_class_statements_array == NULL))
398 for (index= 0; index < thread_instr_class_statements_sizing; index++)
399 thread_instr_class_statements_array[index].reset();
402 if (thread_session_connect_attrs_sizing > 0)
404 thread_session_connect_attrs_array=
405 (
char *)
pfs_malloc(thread_session_connect_attrs_sizing, MYF(MY_ZEROFILL));
406 if (unlikely(thread_session_connect_attrs_array == NULL))
415 &thread_instr_class_waits_array[index * wait_class_max];
422 thread_array[
index].m_statement_stack=
432 global_instr_class_stages_array=
435 if (unlikely(global_instr_class_stages_array == NULL))
439 global_instr_class_stages_array[index].reset();
444 global_instr_class_statements_array=
447 if (unlikely(global_instr_class_statements_array == NULL))
451 global_instr_class_statements_array[index].reset();
473 file_handle_array= NULL;
484 pfs_free(thread_waits_history_array);
485 thread_waits_history_array= NULL;
486 pfs_free(thread_stages_history_array);
487 thread_stages_history_array= NULL;
488 pfs_free(thread_statements_history_array);
489 thread_statements_history_array= NULL;
490 pfs_free(thread_statements_stack_array);
491 thread_statements_stack_array= NULL;
492 pfs_free(thread_instr_class_waits_array);
493 thread_instr_class_waits_array= NULL;
494 pfs_free(global_instr_class_stages_array);
495 global_instr_class_stages_array= NULL;
496 pfs_free(global_instr_class_statements_array);
497 global_instr_class_statements_array= NULL;
498 pfs_free(thread_session_connect_attrs_array);
499 thread_session_connect_attrs_array=NULL;
504 static uchar *filename_hash_get_key(
const uchar *
entry,
size_t *length,
507 const PFS_file *
const *typed_entry;
510 typed_entry=
reinterpret_cast<const PFS_file*
const *
> (
entry);
511 DBUG_ASSERT(typed_entry != NULL);
513 DBUG_ASSERT(file != NULL);
516 return const_cast<uchar*
> (
reinterpret_cast<const uchar*
> (result));
526 if ((! filename_hash_inited) && (
file_max > 0))
528 lf_hash_init(&filename_hash,
sizeof(
PFS_file*), LF_HASH_UNIQUE,
529 0, 0, filename_hash_get_key, &my_charset_bin);
531 filename_hash_inited=
true;
539 if (filename_hash_inited)
541 lf_hash_destroy(&filename_hash);
542 filename_hash_inited=
false;
557 DBUG_ASSERT(random < max_size);
606 DBUG_ASSERT(m_first[0] < max_size);
607 DBUG_ASSERT(m_first[1] < max_size);
608 DBUG_ASSERT(m_last[1] <= max_size);
609 DBUG_ASSERT(m_last[1] <= max_size);
611 DBUG_ASSERT((m_last[0] - m_first[0]) +
623 static uint PFS_ALIGNED mutex_monotonic_index= 0;
661 pfs= mutex_array +
index;
675 if (klass->is_singleton())
701 DBUG_ASSERT(pfs != NULL);
706 if (klass->is_singleton())
720 static uint PFS_ALIGNED rwlock_monotonic_index= 0;
735 pfs= rwlock_array +
index;
751 if (klass->is_singleton())
769 DBUG_ASSERT(pfs != NULL);
774 if (klass->is_singleton())
788 static uint PFS_ALIGNED cond_monotonic_index= 0;
803 pfs= cond_array +
index;
817 if (klass->is_singleton())
835 DBUG_ASSERT(pfs != NULL);
840 if (klass->is_singleton())
875 ulonglong processlist_id)
877 static uint PFS_ALIGNED thread_monotonic_index= 0;
892 pfs= thread_array +
index;
938 pfs->m_account= NULL;
939 set_thread_account(pfs);
966 child_statement= & pfs->m_statement_stack[
index];
971 child_statement->
m_class= NULL;
1061 DBUG_ASSERT(pfs != NULL);
1063 if (pfs->m_account != NULL)
1065 pfs->m_account->release();
1066 pfs->m_account= NULL;
1067 DBUG_ASSERT(pfs->m_user == NULL);
1068 DBUG_ASSERT(pfs->m_host == NULL);
1072 if (pfs->m_user != NULL)
1074 pfs->m_user->release();
1077 if (pfs->m_host != NULL)
1079 pfs->m_host->release();
1136 if (! filename_hash_inited)
1154 const char *filename, uint len,
bool create)
1158 DBUG_ASSERT(klass != NULL || ! create);
1161 if (unlikely(pins == NULL))
1167 char safe_buffer[FN_REFLEN];
1168 const char *safe_filename;
1170 if (len >= FN_REFLEN)
1180 memcpy(safe_buffer, filename, FN_REFLEN - 1);
1181 safe_buffer[FN_REFLEN - 1]= 0;
1182 safe_filename= safe_buffer;
1185 safe_filename= filename;
1210 char buffer[FN_REFLEN];
1211 char dirbuffer[FN_REFLEN];
1213 const char *normalized_filename;
1214 int normalized_length;
1216 dirlen= dirname_length(safe_filename);
1219 dirbuffer[0]= FN_CURLIB;
1220 dirbuffer[1]= FN_LIBCHAR;
1225 memcpy(dirbuffer, safe_filename, dirlen);
1226 dirbuffer[dirlen]=
'\0';
1229 if (my_realpath(buffer, dirbuffer, MYF(0)) != 0)
1236 char *ptr= buffer + strlen(buffer);
1237 char *buf_end= &buffer[
sizeof(buffer)-1];
1238 if ((buf_end > ptr) && (*(ptr-1) != FN_LIBCHAR))
1241 strncpy(ptr, safe_filename + dirlen, buf_end - ptr);
1244 normalized_filename= buffer;
1245 normalized_length= strlen(normalized_filename);
1248 uint retry_count= 0;
1249 const uint retry_max= 3;
1250 static uint PFS_ALIGNED file_monotonic_index= 0;
1256 entry=
reinterpret_cast<PFS_file**
>
1257 (lf_hash_search(&filename_hash, pins,
1258 normalized_filename, normalized_length));
1259 if (entry && (entry != MY_ERRPTR))
1263 lf_hash_search_unpin(pins);
1267 lf_hash_search_unpin(pins);
1285 pfs= file_array +
index;
1294 strncpy(pfs->
m_filename, normalized_filename, normalized_length);
1304 if (likely(res == 0))
1307 if (klass->is_singleton())
1317 if (++retry_count > retry_max)
1344 DBUG_ASSERT(pfs != NULL);
1355 DBUG_ASSERT(thread != NULL);
1356 DBUG_ASSERT(pfs != NULL);
1363 if (klass->is_singleton())
1367 DBUG_ASSERT(pins != NULL);
1369 lf_hash_delete(&filename_hash, pins,
1371 if (klass->is_singleton())
1385 const void *identity)
1387 static uint PFS_ALIGNED table_monotonic_index= 0;
1402 pfs= table_array +
index;
1418 share->inc_refcount();
1439 if (safe_share != NULL)
1483 DBUG_ASSERT(table_stat != NULL);
1484 DBUG_ASSERT(table_share != NULL);
1486 uint key_count= sanitize_index_count(table_share->
m_key_count);
1489 table_share->
m_table_stat.aggregate(table_stat, key_count);
1490 table_stat->fast_reset();
1496 DBUG_ASSERT(table_stat != NULL);
1497 DBUG_ASSERT(table_share != NULL);
1499 uint key_count= sanitize_index_count(table_share->
m_key_count);
1502 table_share->
m_table_stat.aggregate_io(table_stat, key_count);
1503 table_stat->fast_reset_io();
1509 DBUG_ASSERT(table_stat != NULL);
1510 DBUG_ASSERT(table_share != NULL);
1514 table_stat->fast_reset_lock();
1523 DBUG_ASSERT(pfs != NULL);
1536 const struct sockaddr *addr, socklen_t addr_len)
1538 static uint PFS_ALIGNED socket_monotonic_index= 0;
1550 uint addr_len_used= addr_len;
1555 if (addr_len_used >
sizeof(sockaddr_storage))
1556 addr_len_used=
sizeof(sockaddr_storage);
1561 pfs= socket_array +
index;
1578 if ((addr != NULL) && (addr_len_used > 0))
1590 if (klass->is_singleton())
1608 DBUG_ASSERT(pfs != NULL);
1614 if (klass->is_singleton())
1622 event_name_array= thread->m_instr_class_waits_stats;
1628 event_name_array[
index].aggregate(&stat);
1639 static void reset_mutex_waits_by_instance(
void)
1644 for ( ; pfs < pfs_last; pfs++)
1648 static void reset_rwlock_waits_by_instance(
void)
1653 for ( ; pfs < pfs_last; pfs++)
1657 static void reset_cond_waits_by_instance(
void)
1662 for ( ; pfs < pfs_last; pfs++)
1666 static void reset_file_waits_by_instance(
void)
1671 for ( ; pfs < pfs_last; pfs++)
1675 static void reset_socket_waits_by_instance(
void)
1680 for ( ; pfs < pfs_last; pfs++)
1687 reset_mutex_waits_by_instance();
1688 reset_rwlock_waits_by_instance();
1689 reset_cond_waits_by_instance();
1690 reset_file_waits_by_instance();
1691 reset_socket_waits_by_instance();
1700 for ( ; pfs < pfs_last; pfs++)
1710 for ( ; pfs < pfs_last; pfs++)
1722 from_last= from_array + wait_class_max;
1725 for ( ; from < from_last ; from++, to++)
1729 to->aggregate(from);
1745 from_last= from_array + wait_class_max;
1749 for ( ; from < from_last ; from++, to_1++, to_2++)
1753 to_1->aggregate(from);
1754 to_2->aggregate(from);
1771 for ( ; from < from_last ; from++, to++)
1773 if (from->m_timer1_stat.
m_count > 0)
1775 to->aggregate(from);
1795 for ( ; from < from_last ; from++, to_1++, to_2++)
1797 if (from->m_timer1_stat.
m_count > 0)
1799 to_1->aggregate(from);
1800 to_2->aggregate(from);
1817 for ( ; from < from_last ; from++, to++)
1819 if (from->m_timer1_stat.
m_count > 0)
1821 to->aggregate(from);
1841 for ( ; from < from_last ; from++, to_1++, to_2++)
1843 if (from->m_timer1_stat.
m_count > 0)
1845 to_1->aggregate(from);
1846 to_2->aggregate(from);
1852 void aggregate_thread_stats(
PFS_thread *thread,
1857 if (likely(safe_account != NULL))
1859 safe_account->m_disconnected_count++;
1863 if (safe_user != NULL)
1864 safe_user->m_disconnected_count++;
1866 if (safe_host != NULL)
1867 safe_host->m_disconnected_count++;
1878 aggregate_thread_waits(thread, safe_account, safe_user, safe_host);
1879 aggregate_thread_stages(thread, safe_account, safe_user, safe_host);
1880 aggregate_thread_statements(thread, safe_account, safe_user, safe_host);
1881 aggregate_thread_stats(thread, safe_account, safe_user, safe_host);
1884 void aggregate_thread_waits(
PFS_thread *thread,
1889 if (likely(safe_account != NULL))
1901 if ((safe_user != NULL) && (safe_host != NULL))
1915 if (safe_user != NULL)
1926 if (safe_host != NULL)
1941 void aggregate_thread_stages(
PFS_thread *thread,
1946 if (likely(safe_account != NULL))
1958 if ((safe_user != NULL) && (safe_host != NULL))
1972 if (safe_user != NULL)
1982 global_instr_class_stages_array);
1986 if (safe_host != NULL)
2002 global_instr_class_stages_array);
2005 void aggregate_thread_statements(
PFS_thread *thread,
2010 if (likely(safe_account != NULL))
2022 if ((safe_user != NULL) && (safe_host != NULL))
2036 if (safe_user != NULL)
2046 global_instr_class_statements_array);
2050 if (safe_host != NULL)
2066 global_instr_class_statements_array);
2069 void clear_thread_account(
PFS_thread *thread)
2071 if (thread->m_account != NULL)
2073 thread->m_account->release();
2074 thread->m_account= NULL;
2077 if (thread->m_user != NULL)
2079 thread->m_user->release();
2080 thread->m_user= NULL;
2083 if (thread->m_host != NULL)
2085 thread->m_host->release();
2086 thread->m_host= NULL;
2092 DBUG_ASSERT(thread->m_account == NULL);
2093 DBUG_ASSERT(thread->m_user == NULL);
2094 DBUG_ASSERT(thread->m_host == NULL);
2096 thread->m_account= find_or_create_account(thread,
2103 thread->m_user= find_or_create_user(thread,
2108 thread->m_host= find_or_create_host(thread,
2119 for ( ; pfs < pfs_last; pfs++)
2121 klass= sanitize_mutex_class(pfs->
m_class);
2122 if (likely(klass != NULL))
2141 for ( ; pfs < pfs_last; pfs++)
2143 klass= sanitize_rwlock_class(pfs->
m_class);
2144 if (likely(klass != NULL))
2163 for ( ; pfs < pfs_last; pfs++)
2165 klass= sanitize_cond_class(pfs->
m_class);
2166 if (likely(klass != NULL))
2185 for ( ; pfs < pfs_last; pfs++)
2187 klass= sanitize_file_class(pfs->
m_class);
2188 if (likely(klass != NULL))
2207 for ( ; pfs < pfs_last; pfs++)
2210 if (likely(share != NULL))
2235 for ( ; pfs < pfs_last; pfs++)
2237 klass= sanitize_socket_class(pfs->
m_class);
2238 if (likely(klass != NULL))