### 15.16.2 使用性能模式监控 InnoDB Mutex 等待

[](<>)[](<>)

互斥锁是代码中使用的一种同步机制，用于强制在给定时间只有一个线程可以访问公共资源。当服务器中执行的两个或多个线程需要访问同一资源时，这些线程会相互竞争。第一个获得互斥锁锁的线程会导致其他线程等待，直到锁被释放。

为了`InnoDB`已检测的互斥锁，可以使用监视互斥锁等待[性能模式](performance-schema.html).例如，在性能模式表中收集的等待事件数据可以帮助识别具有最多等待或最长总等待时间的互斥锁。

以下示例演示如何启用`InnoDB`互斥等待工具，如何启用关联消费者，以及如何查询等待事件数据。

1.  查看可用`InnoDB`mutex 等待工具，查询性能模式[`setup_instruments`](performance-schema-setup-instruments-table.html)桌子。全部`InnoDB`mutex 等待工具默认禁用。

    ```
    mysql> SELECT *
           FROM performance_schema.setup_instruments
           WHERE NAME LIKE '%wait/synch/mutex/innodb%';
    +---------------------------------------------------------+---------+-------+
    | NAME                                                    | ENABLED | TIMED |
    +---------------------------------------------------------+---------+-------+
    | wait/synch/mutex/innodb/commit_cond_mutex               | NO      | NO    |
    | wait/synch/mutex/innodb/innobase_share_mutex            | NO      | NO    |
    | wait/synch/mutex/innodb/autoinc_mutex                   | NO      | NO    |
    | wait/synch/mutex/innodb/autoinc_persisted_mutex         | NO      | NO    |
    | wait/synch/mutex/innodb/buf_pool_flush_state_mutex      | NO      | NO    |
    | wait/synch/mutex/innodb/buf_pool_LRU_list_mutex         | NO      | NO    |
    | wait/synch/mutex/innodb/buf_pool_free_list_mutex        | NO      | NO    |
    | wait/synch/mutex/innodb/buf_pool_zip_free_mutex         | NO      | NO    |
    | wait/synch/mutex/innodb/buf_pool_zip_hash_mutex         | NO      | NO    |
    | wait/synch/mutex/innodb/buf_pool_zip_mutex              | NO      | NO    |
    | wait/synch/mutex/innodb/cache_last_read_mutex           | NO      | NO    |
    | wait/synch/mutex/innodb/dict_foreign_err_mutex          | NO      | NO    |
    | wait/synch/mutex/innodb/dict_persist_dirty_tables_mutex | NO      | NO    |
    | wait/synch/mutex/innodb/dict_sys_mutex                  | NO      | NO    |
    | wait/synch/mutex/innodb/recalc_pool_mutex               | NO      | NO    |
    | wait/synch/mutex/innodb/fil_system_mutex                | NO      | NO    |
    | wait/synch/mutex/innodb/flush_list_mutex                | NO      | NO    |
    | wait/synch/mutex/innodb/fts_bg_threads_mutex            | NO      | NO    |
    | wait/synch/mutex/innodb/fts_delete_mutex                | NO      | NO    |
    | wait/synch/mutex/innodb/fts_optimize_mutex              | NO      | NO    |
    | wait/synch/mutex/innodb/fts_doc_id_mutex                | NO      | NO    |
    | wait/synch/mutex/innodb/log_flush_order_mutex           | NO      | NO    |
    | wait/synch/mutex/innodb/hash_table_mutex                | NO      | NO    |
    | wait/synch/mutex/innodb/ibuf_bitmap_mutex               | NO      | NO    |
    | wait/synch/mutex/innodb/ibuf_mutex                      | NO      | NO    |
    | wait/synch/mutex/innodb/ibuf_pessimistic_insert_mutex   | NO      | NO    |
    | wait/synch/mutex/innodb/log_sys_mutex                   | NO      | NO    |
    | wait/synch/mutex/innodb/log_sys_write_mutex             | NO      | NO    |
    | wait/synch/mutex/innodb/mutex_list_mutex                | NO      | NO    |
    | wait/synch/mutex/innodb/page_zip_stat_per_index_mutex   | NO      | NO    |
    | wait/synch/mutex/innodb/purge_sys_pq_mutex              | NO      | NO    |
    | wait/synch/mutex/innodb/recv_sys_mutex                  | NO      | NO    |
    | wait/synch/mutex/innodb/recv_writer_mutex               | NO      | NO    |
    | wait/synch/mutex/innodb/redo_rseg_mutex                 | NO      | NO    |
    | wait/synch/mutex/innodb/noredo_rseg_mutex               | NO      | NO    |
    | wait/synch/mutex/innodb/rw_lock_list_mutex              | NO      | NO    |
    | wait/synch/mutex/innodb/rw_lock_mutex                   | NO      | NO    |
    | wait/synch/mutex/innodb/srv_dict_tmpfile_mutex          | NO      | NO    |
    | wait/synch/mutex/innodb/srv_innodb_monitor_mutex        | NO      | NO    |
    | wait/synch/mutex/innodb/srv_misc_tmpfile_mutex          | NO      | NO    |
    | wait/synch/mutex/innodb/srv_monitor_file_mutex          | NO      | NO    |
    | wait/synch/mutex/innodb/buf_dblwr_mutex                 | NO      | NO    |
    | wait/synch/mutex/innodb/trx_undo_mutex                  | NO      | NO    |
    | wait/synch/mutex/innodb/trx_pool_mutex                  | NO      | NO    |
    | wait/synch/mutex/innodb/trx_pool_manager_mutex          | NO      | NO    |
    | wait/synch/mutex/innodb/srv_sys_mutex                   | NO      | NO    |
    | wait/synch/mutex/innodb/lock_mutex                      | NO      | NO    |
    | wait/synch/mutex/innodb/lock_wait_mutex                 | NO      | NO    |
    | wait/synch/mutex/innodb/trx_mutex                       | NO      | NO    |
    | wait/synch/mutex/innodb/srv_threads_mutex               | NO      | NO    |
    | wait/synch/mutex/innodb/rtr_active_mutex                | NO      | NO    |
    | wait/synch/mutex/innodb/rtr_match_mutex                 | NO      | NO    |
    | wait/synch/mutex/innodb/rtr_path_mutex                  | NO      | NO    |
    | wait/synch/mutex/innodb/rtr_ssn_mutex                   | NO      | NO    |
    | wait/synch/mutex/innodb/trx_sys_mutex                   | NO      | NO    |
    | wait/synch/mutex/innodb/zip_pad_mutex                   | NO      | NO    |
    | wait/synch/mutex/innodb/master_key_id_mutex             | NO      | NO    |
    +---------------------------------------------------------+---------+-------+
    ```

2.  一些`InnoDB`mutex 实例在服务器启动时创建，并且仅在服务器启动时也启用了关联的仪器时才被检测。为确保所有`InnoDB`mutex 实例已检测并启用，添加以下内容`性能模式工具`规则到您的 MySQL 配置文件：

    ```
    performance-schema-instrument='wait/synch/mutex/innodb/%=ON'
    ```

    如果您不需要所有的等待事件数据`InnoDB`互斥体，您可以通过添加额外的禁用特定仪器`性能模式工具`规则到您的 MySQL 配置文件。例如，禁用`InnoDB`与全文搜索相关的互斥等待事件工具，添加以下规则：

    ```
    performance-schema-instrument='wait/synch/mutex/innodb/fts%=OFF'
    ```

    笔记

    具有较长前缀的规则，例如`等待/同步/互斥/innodb/fts%`优先于具有较短前缀的规则，例如`等待/同步/互斥/innodb/%`.

    添加后`性能模式工具`规则到您的配置文件，重新启动服务器。所有的`InnoDB`启用了除与全文搜索相关的互斥锁之外的互斥锁。要验证，请查询[`setup_instruments`](performance-schema-setup-instruments-table.html)桌子。这`已启用`和`定时`列应设置为`是的`对于您启用的仪器。

    ```
    mysql> SELECT *
           FROM performance_schema.setup_instruments
           WHERE NAME LIKE '%wait/synch/mutex/innodb%';
    +-------------------------------------------------------+---------+-------+
    | NAME                                                  | ENABLED | TIMED |
    +-------------------------------------------------------+---------+-------+
    | wait/synch/mutex/innodb/commit_cond_mutex             | YES     | YES   |
    | wait/synch/mutex/innodb/innobase_share_mutex          | YES     | YES   |
    | wait/synch/mutex/innodb/autoinc_mutex                 | YES     | YES   |
    ...
    | wait/synch/mutex/innodb/master_key_id_mutex           | YES     | YES   |
    +-------------------------------------------------------+---------+-------+
    49 rows in set (0.00 sec)
    ```

3.  通过更新启用等待事件消费者[`setup_consumers`](performance-schema-setup-consumers-table.html)桌子。默认情况下禁用等待事件消费者。

    ```
    mysql> UPDATE performance_schema.setup_consumers
           SET enabled = 'YES'
           WHERE name like 'events_waits%';
    Query OK, 3 rows affected (0.00 sec)
    Rows matched: 3  Changed: 3  Warnings: 0
    ```

    您可以通过查询[`setup_consumers`](performance-schema-setup-consumers-table.html)桌子。这[`events_waits_current`](performance-schema-events-waits-current-table.html),[`events_waits_history`](performance-schema-events-waits-history-table.html)， 和[`events_waits_history_long`](performance-schema-events-waits-history-long-table.html)应启用消费者。

    ```
    mysql> SELECT * FROM performance_schema.setup_consumers;
    +----------------------------------+---------+
    | NAME                             | ENABLED |
    +----------------------------------+---------+
    | events_stages_current            | NO      |
    | events_stages_history            | NO      |
    | events_stages_history_long       | NO      |
    | events_statements_current        | YES     |
    | events_statements_history        | YES     |
    | events_statements_history_long   | NO      |
    | events_transactions_current      | YES     |
    | events_transactions_history      | YES     |
    | events_transactions_history_long | NO      |
    | events_waits_current             | YES     |
    | events_waits_history             | YES     |
    | events_waits_history_long        | YES     |
    | global_instrumentation           | YES     |
    | thread_instrumentation           | YES     |
    | statements_digest                | YES     |
    +----------------------------------+---------+
    15 rows in set (0.00 sec)
    ```

4.  启用仪器和使用者后，运行您要监控的工作负载。在这个例子中，[**mysqlslap**](mysqlslap.html)负载仿真客户端用于模拟工作负载。

    ```
    $> ./mysqlslap --auto-generate-sql --concurrency=100 --iterations=10
           --number-of-queries=1000 --number-char-cols=6 --number-int-cols=6;
    ```

5.  查询等待事件数据。在本例中，等待事件数据是从[`events_waits_summary_global_by_event_name`](performance-schema-wait-summary-tables.html)汇总在[`events_waits_current`](performance-schema-events-waits-current-table.html),[`events_waits_history`](performance-schema-events-waits-history-table.html)， 和[`events_waits_history_long`](performance-schema-events-waits-history-long-table.html)表。数据按事件名称汇总（`EVENT_NAME`)，这是产生事件的仪器的名称。汇总数据包括：

    -   `COUNT_STAR`

        汇总等待事件的数量。

    -   `SUM_TIMER_WAIT`

        汇总的定时等待事件的总等待时间。

    -   `MIN_TIMER_WAIT`

        汇总的定时等待事件的最短等待时间。

    -   `AVG_TIMER_WAIT`

        汇总的定时等待事件的平均等待时间。

    -   `MAX_TIMER_WAIT`

        汇总的定时等待事件的最大等待时间。

        以下查询返回仪器名称 (`EVENT_NAME`），等待事件的数量（`COUNT_STAR`)，以及该工具事件的总等待时间 (`SUM_TIMER_WAIT`）。因为默认情况下等待时间以皮秒（万亿分之一秒）为单位，所以等待时间除以 1000000000 以显示以毫秒为单位的等待时间。数据按汇总等待事件的数量按降序排列（`COUNT_STAR`）。您可以调整`订购方式`子句按总等待时间对数据进行排序。

    ```
    mysql> SELECT EVENT_NAME, COUNT_STAR, SUM_TIMER_WAIT/1000000000 SUM_TIMER_WAIT_MS
           FROM performance_schema.events_waits_summary_global_by_event_name
           WHERE SUM_TIMER_WAIT > 0 AND EVENT_NAME LIKE 'wait/synch/mutex/innodb/%'
           ORDER BY COUNT_STAR DESC;
    +---------------------------------------------------------+------------+-------------------+
    | EVENT_NAME                                              | COUNT_STAR | SUM_TIMER_WAIT_MS |
    +---------------------------------------------------------+------------+-------------------+
    | wait/synch/mutex/innodb/trx_mutex                       |     201111 |           23.4719 |
    | wait/synch/mutex/innodb/fil_system_mutex                |      62244 |            9.6426 |
    | wait/synch/mutex/innodb/redo_rseg_mutex                 |      48238 |            3.1135 |
    | wait/synch/mutex/innodb/log_sys_mutex                   |      46113 |            2.0434 |
    | wait/synch/mutex/innodb/trx_sys_mutex                   |      35134 |         1068.1588 |
    | wait/synch/mutex/innodb/lock_mutex                      |      34872 |         1039.2589 |
    | wait/synch/mutex/innodb/log_sys_write_mutex             |      17805 |         1526.0490 |
    | wait/synch/mutex/innodb/dict_sys_mutex                  |      14912 |         1606.7348 |
    | wait/synch/mutex/innodb/trx_undo_mutex                  |      10634 |            1.1424 |
    | wait/synch/mutex/innodb/rw_lock_list_mutex              |       8538 |            0.1960 |
    | wait/synch/mutex/innodb/buf_pool_free_list_mutex        |       5961 |            0.6473 |
    | wait/synch/mutex/innodb/trx_pool_mutex                  |       4885 |         8821.7496 |
    | wait/synch/mutex/innodb/buf_pool_LRU_list_mutex         |       4364 |            0.2077 |
    | wait/synch/mutex/innodb/innobase_share_mutex            |       3212 |            0.2650 |
    | wait/synch/mutex/innodb/flush_list_mutex                |       3178 |            0.2349 |
    | wait/synch/mutex/innodb/trx_pool_manager_mutex          |       2495 |            0.1310 |
    | wait/synch/mutex/innodb/buf_pool_flush_state_mutex      |       1318 |            0.2161 |
    | wait/synch/mutex/innodb/log_flush_order_mutex           |       1250 |            0.0893 |
    | wait/synch/mutex/innodb/buf_dblwr_mutex                 |        951 |            0.0918 |
    | wait/synch/mutex/innodb/recalc_pool_mutex               |        670 |            0.0942 |
    | wait/synch/mutex/innodb/dict_persist_dirty_tables_mutex |        345 |            0.0414 |
    | wait/synch/mutex/innodb/lock_wait_mutex                 |        303 |            0.1565 |
    | wait/synch/mutex/innodb/autoinc_mutex                   |        196 |            0.0213 |
    | wait/synch/mutex/innodb/autoinc_persisted_mutex         |        196 |            0.0175 |
    | wait/synch/mutex/innodb/purge_sys_pq_mutex              |        117 |            0.0308 |
    | wait/synch/mutex/innodb/srv_sys_mutex                   |         94 |            0.0077 |
    | wait/synch/mutex/innodb/ibuf_mutex                      |         22 |            0.0086 |
    | wait/synch/mutex/innodb/recv_sys_mutex                  |         12 |            0.0008 |
    | wait/synch/mutex/innodb/srv_innodb_monitor_mutex        |          4 |            0.0009 |
    | wait/synch/mutex/innodb/recv_writer_mutex               |          1 |            0.0005 |
    +---------------------------------------------------------+------------+-------------------+
    ```

    笔记

    上述结果集包括启动过程中产生的等待事件数据。要排除此数据，您可以截断[`events_waits_summary_global_by_event_name`](performance-schema-wait-summary-tables.html)在启动后和运行工作负载之前立即创建表。但是，截断操作本身可能会产生可忽略不计的等待事件数据量。

    ```
    mysql> TRUNCATE performance_schema.events_waits_summary_global_by_event_name;
    ```
