performance_schema.setup_actorsの使い方
mysql

performance_schema.setup_actorsの使い方

このエントリーをはてなブックマークに追加

こんにちは、DBAのたなかです。

前回 からの引き続きで相変わらずperformance_schemaのお話です。

ところで、3歳のせがれに「なにかおはなしして?」と言われたのでperformance_schemaについて語ろうとしたところ、「ピーマンじゃないでしょー。もっとたのしいはなしー」と言われたのでしょんぼりしております。パホーマンス・ピーマン。

前回の最後で

さて、これで残るは”threads.instrumented= ‘NO’ を全スレッドに設定しておく”をデフォルトでやる方法を調べればまあまあイケるかな。。

http://tech.gmo-media.jp/post/80860636742/how-to-set-performance-schema-instrument

ということで調べてみたんですが、threads.instrumentedのがYESになるかNOになるかはperformance_schema.setup_actorsに支配されます。デフォルトではHOST, USERとも全てマッチするように設定されています(ROLEは5.6.16現在未使用のカラム)

mysql56> SELECT * FROM performance_schema.setup_actors;
+------+------+------+
| HOST | USER | ROLE |
+------+------+------+
| %    | %    | %    |
+------+------+------+
1 row in set (0.00 sec)

mysql56> SELECT thread_id, name, type, instrumented FROM threads;
+-----------+----------------------------------------+------------+--------------+
| thread_id | name                                   | type       | instrumented |
+-----------+----------------------------------------+------------+--------------+
|         1 | thread/sql/main                        | BACKGROUND | YES          |
|         2 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         3 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         4 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         5 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         6 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         7 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         8 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         9 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|        10 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|        11 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|        13 | thread/innodb/srv_monitor_thread       | BACKGROUND | YES          |
|        14 | thread/innodb/srv_error_monitor_thread | BACKGROUND | YES          |
|        15 | thread/innodb/srv_lock_timeout_thread  | BACKGROUND | YES          |
|        16 | thread/innodb/srv_purge_thread         | BACKGROUND | YES          |
|        17 | thread/innodb/srv_master_thread        | BACKGROUND | YES          |
|        18 | thread/innodb/page_cleaner_thread      | BACKGROUND | YES          |
|        19 | thread/sql/signal_handler              | BACKGROUND | YES          |
|        21 | thread/sql/one_connection              | FOREGROUND | NO           |
+-----------+----------------------------------------+------------+--------------+
19 rows in set (0.00 sec)

新規に作成されたコネクションはsetup_actorsの条件にマッチする場合にのみinstrumentedがYESになるので、setup_actorsのレコードを取り除いてやれば、そこから先に作成されたコネクションは

mysql56> TRUNCATE performance_schema.setup_actors;
Query OK, 0 rows affected (0.00 sec)

mysql56> SELECT thread_id, name, type, instrumented FROM performance_schema.threads;
+-----------+----------------------------------------+------------+--------------+
| thread_id | name                                   | type       | instrumented |
+-----------+----------------------------------------+------------+--------------+
|         1 | thread/sql/main                        | BACKGROUND | YES          |
|         2 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         3 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         4 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         5 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         6 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         7 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         8 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         9 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|        10 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|        11 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|        13 | thread/innodb/srv_monitor_thread       | BACKGROUND | YES          |
|        14 | thread/innodb/srv_error_monitor_thread | BACKGROUND | YES          |
|        15 | thread/innodb/srv_lock_timeout_thread  | BACKGROUND | YES          |
|        16 | thread/innodb/srv_purge_thread         | BACKGROUND | YES          |
|        17 | thread/innodb/srv_master_thread        | BACKGROUND | YES          |
|        18 | thread/innodb/page_cleaner_thread      | BACKGROUND | YES          |
|        19 | thread/sql/signal_handler              | BACKGROUND | YES          |
|        24 | thread/sql/one_connection              | FOREGROUND | YES          |
|        25 | thread/sql/one_connection              | FOREGROUND | NO           |
+-----------+----------------------------------------+------------+--------------+
20 rows in set (0.00 sec)

instrumentedがNOになります。それ以前につないでいたもの(と、バックグラウンドスレッド)はYESのままですね。

というわけでこのperformance_schema.setup_actorsを起動時にどうやって空っぽにするのか、performance-schema-setup-actors的なパラメータはないのか、ソースコードまで読んで調べてみましたが

 そ ん な 設 定 は あ り ま せ ん で し た

残念(´・ω・`)

が、設定がなければできないという道理もないので、思い付いた回避策(?)を紹介しておきます。

1) init-fileを使ってTRUNCATEしてしまう

とても素直な方法です。

$ cat /usr/mysql/5.6.16/setup_actors.sql
TRUNCATE performance_schema.setup_actors;

$ bin/mysqld_safe --init-file=/usr/mysql/5.6.16/setup_actors.sql &

mysql> SELECT * FROM performance_schema.setup_actors;
Empty set (0.00 sec)

mysql> SELECT thread_id, name, type, instrumented FROM performance_schema.threads;
+-----------+----------------------------------------+------------+--------------+
| thread_id | name                                   | type       | instrumented |
+-----------+----------------------------------------+------------+--------------+
|         1 | thread/sql/main                        | BACKGROUND | YES          |
|         2 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         3 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         4 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         5 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         6 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         7 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         8 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|         9 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|        10 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|        11 | thread/innodb/io_handler_thread        | BACKGROUND | YES          |
|        13 | thread/innodb/srv_monitor_thread       | BACKGROUND | YES          |
|        14 | thread/innodb/srv_error_monitor_thread | BACKGROUND | YES          |
|        15 | thread/innodb/srv_lock_timeout_thread  | BACKGROUND | YES          |
|        16 | thread/innodb/srv_purge_thread         | BACKGROUND | YES          |
|        17 | thread/innodb/srv_master_thread        | BACKGROUND | YES          |
|        18 | thread/innodb/page_cleaner_thread      | BACKGROUND | YES          |
|        19 | thread/sql/signal_handler              | BACKGROUND | YES          |
|        21 | thread/sql/one_connection              | FOREGROUND | NO           |
+-----------+----------------------------------------+------------+--------------+
19 rows in set (0.00 sec)

とてもスマートですね :) 問題はinit-fileは1つしか指定できず、ファイルの中に複数ステートメントを記述することもできないので、既に何らかの用途でこのオプションを使っている場合は利用できないこと。あと、my.cnfの他に管理しておかないといけないファイルが1つ増えるのはどうもちょっとしょんぼりします。

2) ソースコードを書き換える

安定の手段ですね。MySQL 5.6.16のコードだと

30 void install_default_setup(PSI_bootstrap *boot)
31 {
..

44 String percent("%", 1, &my_charset_utf8_bin);
45 /* Enable all users on all hosts by default */
46 insert_setup_actor(&percent, &percent, &percent);
..

mysql-5.6.16/storage/perfschema/pfs_defaults.cc

と、こうなっているので、新しくシステム変数を作って、46行目のinsert_setup_actorの手前で判定してやれば良いでしょう。

--performance_schema_setup_actors_fill_default_value=0, --skip-performance_schema_setup_actors_fill_default_valueなどと指定された時はsetup_actorsへのデフォルト値のINSERTをやめるような作りにしてみました。

Feature RequestとしてMySQL Bugsに投稿していますので、気になったら(?)"Affects Me"などしていただけると幸いです。パッチもMySQL Bugsからたどれます。

取り込まれたら嬉しいけれど、取り込まれないだろうからinit-fileで運用しますかね。。次はthread.instrumentedがNOだったらオーバーヘッドがちゃんと削減できるかどうかを調べないとなぁ。。


名無しのエンジニア
ディスプレイ環境が良くなるまでが結構色々あったお話
開発合宿に行ってきたのでレポートするよ!in 日光