Bug #110405 Memory allocation by UT_NEW_THIS_FILE_PSI_KEY at startup cannot be instrumented
Submitted: 17 Mar 2023 4:52 Modified: 20 Mar 2023 5:46
Reporter: Baolin Huang (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S3 (Non-critical)
Version:8.0 OS:Any
Assigned to: CPU Architecture:Any

[17 Mar 2023 4:52] Baolin Huang
Description:
MySQL 8.0 provides UT_NEW_THIS_FILE_PSI_KEY to facilitate memory monitoring without defining pfs key. 

In buf_pool_create, several memory applications use UT_NEW_THIS_FILE_PSI_KEY, including buf_pool->chunks, buf_pool->watch.

These memory allocation cannot be instrumented, because
1. the auto-generated keys have no PSI_FLAG_ONLY_GLOBAL_STAT flag.
2. no pfs_thread created for buf_pool_create thread

How to repeat:
1. 
cat t_pfs_UT_NEW_THIS_FILE_PSI_KEY.test
SELECT * FROM performance_schema.memory_summary_global_by_event_name WHERE event_name LIKE 'memory%buf0buf';
SELECT SLEEP(3600);

$cat t_pfs_UT_NEW_THIS_FILE_PSI_KEY-master.opt 
--performance-schema=ON
--performance-schema-instrument='%=ON'

2. ./mysql-test-run.pl t_pfs_UT_NEW_THIS_FILE_PSI_KEY.test --manual-gdb

3.
gdb> b buf_pool_create

the gdb process is follows

pfs_memory_alloc_vc (key=201, size=56, owner=0x7fffed0ea658) at /home/baolin.hbl/code/upstream/mysql_upstream/storage/perfschema/pfs.cc:7708
7708      PSI_memory_key result_key = key;
(gdb) n
7709      PFS_thread **owner_thread = reinterpret_cast<PFS_thread **>(owner);
(gdb) 
7710      assert(owner_thread != nullptr);
(gdb) 
7712      if (!flag_global_instrumentation) {
(gdb) 
7717      PFS_memory_class *klass = find_memory_class(key);
(gdb) 
7718      if (klass == nullptr) {
(gdb) 
7723      if (!klass->m_enabled) {
(gdb) 
7728      uint index = klass->m_event_name_index;
(gdb) 
7730      if (flag_thread_instrumentation && !klass->is_global()) {
(gdb) 
7731        PFS_thread *pfs_thread = my_thread_get_THR_PFS();
(gdb) 
7732        if (unlikely(pfs_thread == nullptr)) {
(gdb) 
7733          *owner_thread = nullptr;
(gdb) 
7734          return PSI_NOT_INSTRUMENTED;
(gdb) 
7791    }
(gdb) 

#0  pfs_memory_alloc_vc (key=201, size=56, owner=0x7fffed0ea658)
    at /home/baolin.hbl/code/upstream/mysql_upstream/storage/perfschema/pfs.cc:7791
#1  0x0000000004af305f in ut::detail::Alloc_pfs::alloc<true> (size=24, key=201)
    at /home/baolin.hbl/code/upstream/mysql_upstream/storage/innobase/include/detail/ut/alloc.h:282
#2  0x0000000004aed35b in ut::detail::Alloc_<ut::detail::Alloc_pfs>::alloc<true, ut::detail::Alloc_pfs> (size=24, key=201)
    at /home/baolin.hbl/code/upstream/mysql_upstream/storage/innobase/include/detail/ut/alloc.h:438
#3  0x0000000004add8f3 in ut::zalloc_withkey (key=..., size=24)
    at /home/baolin.hbl/code/upstream/mysql_upstream/storage/innobase/include/ut0new.h:633
#4  0x0000000004ec52f5 in buf_pool_create (buf_pool=0x7fffe830c560, buf_pool_size=25165824, instance_no=0, mutex=0x7fffee489100, 
    err=@0x7fffe8012140: DB_SUCCESS) at /home/baolin.hbl/code/upstream/mysql_upstream/storage/innobase/buf/buf0buf.cc:1242

Suggested fix:
Current UT_NEW_THIS_FILE_PSI_KEY
[20 Mar 2023 5:46] MySQL Verification Team
Hello Huang,

Thank you for the report and test case.

regards,
Umesh