zoukankan      html  css  js  c++  java
  • DTrace Probes In MySQL 自定义探针

    Inserting user-defined DTrace probes into MySQL source code is very useful to help user identify the performance problems in the application level and the database server, In addition, the cost of the USDT probe is basically neglectable. Each probes inserted into the src can be enabled by adding the code like:

     If (PROVIDER_PROBE_ENABLED()

    {

                    PROVIDER_PROBE(arg0,…);

    }

    The steps to add DTrace probes into MySQL is very straightforward.

    Step 1: Figure out what probes are needed to insert into the source code

    This is the difficult part that requires you understand the MySQL implementation details. Generally, it is good to insert probes to clarify the DB response time distribution including processing query, waiting on locks and latches, doing disk I/O, receiving/sending back data. You can certainly define more probes deep into each of the MySQL engines (such as: define probes to measure the cost of innodb sync spin wait)

    Step 2: Define Provider and probes

    Create a mysqlprovider.d file as:

    provider mysql {

                    probe query__execute__start(int);

                   probe query__execute__finish(int);

                …

    };

    It is required to define the probes with easy to understand name. The two underscore(__) is translated to hyphen(-) in the D script file, so the above two probes are called query-execute-startand query-execute-finish

    Step 3: Define header file for probes

    Create mysqlprovider.h file as:

    #ifndef _MYSQLPROVIDER_H

    #define _MYSQLPROVIDER_H

     

    #ifdef ENABLE_DTRACE

     

    #define MYSQL_QUERY_EXECUTE_START(arg0) \

                 __dtrace_mysql__query_execute__start(arg0)

    #define MYSQL_QUERY_EXECUTE_START_ENABLED() \

                 __dtraceenabled_mysql__query_execute__start()

    #define MYSQL_QUERY_EXECUTE_FINISH(arg0) \

                 __dtrace_mysql__query_execute__finish(arg0)

    #define MYSQL_QUERY_EXECUTE_FINISH_ENABLED() \

                 __dtraceenabled_mysql__query_execute__finish()

    extern void __ dtrace_mysql__query_execute__start(int)

    extern int __ dtraceenabled_mysql__query_execute__start(void)

    extern void __ dtrace_mysql__query_execute__finish(int)

    extern int __ dtraceenabled_mysql__query_execute__finish(void)

     

    #else

    /*

    *Unless DTrace is explicitly enabled with –enable-dtrace, the MYSQL macros will expand to no-ops.

    */

     

    #define MYSQL_QUERY_EXECUTE_START(arg0) \

                 __dtrace_mysql__query_execute__start(arg0)

    #define MYSQL_QUERY_EXECUTE_START_ENABLED() \

                 __dtraceenabled_mysql__query_execute__start()

    #define MYSQL_QUERY_EXECUTE_FINISH(arg0) \

                 __dtrace_mysql__query_execute__finish(arg0)

    #define MYSQL_QUERY_EXECUTE_FINISH_ENABLED()

     

    #endif

    #endif  /* _MYSQLPROVIDER_H */

     

    Step 4: Insert the probes into source code

    You need to include the header file created for DTrace probes before inserting the probe macro. And in order to monitor the server behavior as expected, it requires the knowledge of the MySQLsource code to add the probe macro into the right place.

    #include <mysqlprovider.h>

    mysql_parse {

    bool

    mysql_execute_command(THD *thd)

    {

     

        MYSQL_QUERY_EXECUTE_START(thd->thread_id);

    case SQLCOM_EXECUTE:

    {

       mysql_sql_stmt_execute(thd);

      

       MYSQL_QUERY_EXECUTE_FINISH(thd->thread_id);

       Break;

    }

    ….

     

    }

     

    Step 5: Build MySQL with DTrace

    You will need to specify the “—enable-dtrace” as the configure option to make the DTrace probes available in MySQL on Solaris 10 and above. On the other operating system without the DTracefacility, the DTrace probes are disabled as default.

    In the Makefile, you can compile the 64-bit MySQL with DTrace probes as bellow:

    mysqlproviders.o: mysqlproviders.d $(mysqld_OBJECTS)

    dtrace -G -64 -s mysqlproviders.d $(mysqld_OBJECTS)

     

    Now, at this point, you have completed inserting the DTrace probes into MySQL, and the probes are ready to use. For example, to use the query-execute-start and query-execute-stop probes, you can write a simple D script(query-execute.d) to measure the time spending on the query execution for each session.

    #!/usr/sbin/dtrace –qs

     

    mysql*:::query-execute-start

    {

                    self->init = timestamp;

    }

     

    mysql*:::query-execute-finish

    /self->init/

    {

                    @inittime[args[0]] = sum(timestamp – self->init);

                    self->init = 0;

    }

     

    profile:::tick-5s

    {

    printf("--------------------------------------------------\n");

            printf("Date: %Y\n", walltimestamp);

            printf("Query execution time\n");

            printa(@inittime);

            printf("--------------------------------------------------\n");

    }

    Now, you can execute the script to get the data for query execution:

    #./query_execute.d

    --------------------------------------------------

    Date: 2007 May 25 19:18:59

    Query execution time

     

          149       4542802785

          146       4577178817

          148       4586742308

          147       4602289846

    --------------------------------------------------

    Please let me know if you find this is useful, any suggestions on which/where probes would be useful in the MySQL server and client application. You can contact me by email:Luojia.chen@sun.com, or comment on this blog.

    Resources:

     
     
  • 相关阅读:
    安防视频云服务EasyCVR视频上云网关如何通过wireshark将发送的rtp流数据保存成文件?
    安防视频监控系统视频上云解决方案EasyCVR语音转发功能音频数据打包发送流程介绍
    安防视频监控系统视频上云解决方案EasyCVR音频基础知识介绍
    如何通过RTSP协议视频平台EasyNVR建立一套外网可访问的4S店远程监控系统?
    IP摄像机RTSP协议视频平台EasyNVR点击程序启动后闪退问题排查及解决
    5G时代RTC技术是直播互动的最终选择,EasyRTC视频会议系统将赋能VR/电商直播等更多新场景
    视频会议软件EasyRTC-SFU之mediasoup-demo在 Windows上的编译安装
    视频会议软件/音视频通话软件EasyRTC-SFU开发中如何使用TortoiseGit将代码推送到两个代码仓库?
    SFU架构的云视频会议系统如何取代硬件视频会议系统,成为5G时代的视频会议新宠?
    云架构视频会议系统EasyRTC企业远程会议MCU版与SFU版在行业应用场景上有什么区别?
  • 原文地址:https://www.cnblogs.com/zengkefu/p/5762249.html
Copyright © 2011-2022 走看看