strace介绍及用途

strace是一个用于诊断,分析linux用户态进程的工具

类似的工具pstrace,lsof,gdb,pstrack

strace观察mysqld对my.cnf 配置文件的加载顺序

命令如下:
strace -T -tt -s 100 -o start.log /usr/local/mysql/bin/mysqld

#  cat -n start.log |sed -n '152,165p'
   152  03:43:41.399411 futex(0x7fb060f058b8, FUTEX_WAKE_PRIVATE, 2147483647) = 0 <0.000013>
   153  03:43:41.400839 stat("/etc/my.cnf", {st_mode=S_IFREG|0644, st_size=11064, ...}) = 0 <0.000015>
   154  03:43:41.400951 open("/etc/my.cnf", O_RDONLY) = 3 <0.000024>
   155  03:43:41.401026 fstat(3, {st_mode=S_IFREG|0644, st_size=11064, ...}) = 0 <0.000009>
   156  03:43:41.401068 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb061d94000 <0.000016>
   157  03:43:41.401116 read(3, "[client]\nport            = 3306\n\n[mysql]\nauto-rehash\nprompt=\"\\\\u@\\\\h [\\\\d]>\"\n#pager=\"less -i -n -S\"\n"..., 4096) = 4096 <0.000016>
   158  03:43:41.401211 read(3, " #    0\n#rpl_semi_sync_master_timeout        =1000                          #    1000(1 second)\n#plu"..., 4096) = 4096 <0.000011>
   159  03:43:41.401303 read(3, "       #\ton\ninnodb_change_buffering                         =all                        #\tall\ninnodb"..., 4096) = 2872 <0.000011>
   160  03:43:41.401376 read(3, "", 4096)       = 0 <0.000010>
   161  03:43:41.401423 close(3)                = 0 <0.000052>
   162  03:43:41.401564 munmap(0x7fb061d94000, 4096) = 0 <0.000080>
   163  03:43:41.401777 stat("/etc/mysql/my.cnf", 0x7fff5ec1c170) = -1 ENOENT (No such file or directory) <0.000022>
   164  03:43:41.402070 stat("/usr/local/mysql/etc/my.cnf", 0x7fff5ec1c170) = -1 ENOENT (No such file or directory) <0.000016>
   165  03:43:41.402134 stat("/root/.my.cnf", 0x7fff5ec1c170) = -1 ENOENT (No such file or directory) <0.000012>

MySQL启动后默认会启动多少线程

操作系统查看:

# ps -T `pidof mysqld`
   PID   SPID TTY      STAT   TIME COMMAND
 72371  72371 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72377 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72378 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72379 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72380 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72381 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72382 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72383 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72384 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72385 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72386 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72387 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72388 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72389 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72390 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72391 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72392 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72393 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72394 pts/0    Rl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72395 pts/0    Sl     0:01 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72396 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72397 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72398 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72399 pts/0    Rl     1:45 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72400 pts/0    Rl     0:03 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72401 pts/0    Rl     0:03 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72402 pts/0    Rl     0:03 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72403 pts/0    Rl     0:03 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72404 pts/0    Rl     0:03 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72405 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72406 pts/0    Sl     0:02 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72407 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72408 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72409 pts/0    Sl     0:01 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72410 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72411 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72412 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72413 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72414 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72415 pts/0    Sl     0:01 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72416 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72419 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72420 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72421 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72422 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72423 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72424 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr
 72371  72425 pts/0    Sl     0:00 /usr/local/mysql/bin/mysqld --defaults-file=/app/mysqldata/3306/my.cnf --basedir=/usr

利用pstack
pstack `pidof mysql`  或者pstack `pidof mysqld` 2>&1|tee  /tmp/pstack.log

select thread_id, name from performance_schema.threads;
select name,count(*) from  performance_schema.threads group by name;

mysql> select name,count(*) from performance_schema.threads group by name;
+---------------------------------------------+----------+
| name | count(*) |
+---------------------------------------------+----------+
| thread/sql/main | 1 |
| thread/innodb/io_write_thread | 8 |
| thread/innodb/io_read_thread | 8 |
| thread/innodb/io_ibuf_thread | 1 |
| thread/innodb/io_log_thread | 1 |
| thread/innodb/page_flush_coordinator_thread | 1 |
| thread/innodb/page_flush_thread | 3 |
| thread/innodb/log_checkpointer_thread | 1 |
| thread/innodb/log_closer_thread | 1 |
| thread/innodb/log_writer_thread | 1 |
| thread/innodb/log_flusher_thread | 1 |
| thread/innodb/log_write_notifier_thread | 1 |
| thread/innodb/log_flush_notifier_thread | 1 |
| thread/innodb/srv_lock_timeout_thread | 1 |
| thread/innodb/srv_error_monitor_thread | 1 |
| thread/innodb/srv_monitor_thread | 1 |
| thread/innodb/buf_resize_thread | 1 |
| thread/innodb/srv_master_thread | 1 |
| thread/innodb/buf_dump_thread | 1 |
| thread/innodb/dict_stats_thread | 1 |
| thread/innodb/fts_optimize_thread | 1 |
| thread/mysqlx/worker | 2 |
| thread/mysqlx/acceptor_network | 2 |
| thread/innodb/srv_purge_thread | 2 |
| thread/innodb/srv_worker_thread | 6 |
| thread/sql/signal_handler | 1 |
| thread/sql/event_scheduler | 1 |
| thread/sql/compress_gtid_table | 1 |
| thread/sql/one_connection | 1 |
+---------------------------------------------+----------+
29 rows in set (0.01 sec)

如何匹配conn_id和os_thread_id

MySQL 5.7 & 8.0时代采用的方法:
可以利用sys.processlist 和performance_schema.threads进行关联

mysql> select thd_id, conn_id, thread_os_id, name from sys.processlist a ,performance_schema.threads b where a.thd_id =b.thread_id and conn_id >0;
+--------+---------+--------------+--------------------------------+
| thd_id | conn_id | thread_os_id | name |
+--------+---------+--------------+--------------------------------+
| 55 | 6 | 72425 | thread/sql/compress_gtid_table |
| 60 | 11 | 76698 | thread/sql/one_connection |
| 54 | 4 | 72423 | thread/sql/event_scheduler |
+--------+---------+--------------+--------------------------------+
3 rows in set (0.12 sec)

利用strace观察client的SQL执行

strace -T -tt -e trace=recvfrom,write,access,read,open,pwrite64,pread64 -p 76698

利用strace观察server端执行

MySQL 5.7后可以利用sys.processlist:
select thd_id, conn_id,user, pid,program_name,command,current_statement from processlist where conn_id>0 and pid>0;

strace -o /tmp/test.log -T -tt -ff -p `pidof mysqld`
-ff 参数,在跟踪的线程fork出新的thread时,会产生一个新文件存放

利用获取的线程id在MySQL查询,看看都是干什么的?
select thread_id, thread_os_id,name from threads where thread_os_id in (15871,15872,15874,15901);
 
strace命令常用简单使用方法如下:
strace -T -tt -o /tmp/strace.log CMD
strace -T -tt CMD 2>&1 |tee /tmp/strace.log
strace -T -tt -s 100 -o /tmp/strace.log CMD
strace -T -tt -s 100 -ff -o /tmp/strace.log CMD
strace -T -tt -s 100 -e trace=XXXX -o /tmp/strace.log CMD
strace -T -tt -s 100 -ff -o /root/strace.log /usr/local/mysql/bin/mysqld
strace -T -tt -s 100 -e trace=write -o /root/strace.log /usr/local/mysql/bin/mysqld
perf 是一个调查 Linux 中各种性能问题的有力工具。
# perf --help
 usage: perf [--version] [--help] COMMAND [ARGS]
 The most commonly used perf commands are:
   annotate        Read perf.data (created by perf record) and display annotated code
   archive         Create archive with object files with build-ids found in perf.data file
   bench           General framework for benchmark suites
   buildid-cache   Manage build-id cache.
   buildid-list    List the buildids in a perf.data file
   diff            Read two perf.data files and display the differential profile
   evlist          List the event names in a perf.data file
   inject          Filter to augment the events stream with additional information
   kmem            Tool to trace/measure kernel memory(slab) properties
   kvm             Tool to trace/measure kvm guest os
   list            List all symbolic event types
   lock            Analyze lock events
   record          Run a command and record its profile into perf.data
   report          Read perf.data (created by perf record) and display the profile
   sched           Tool to trace/measure scheduler properties (latencies)
   script          Read perf.data (created by perf record) and display trace output
   stat            Run a command and gather performance counter statistics
   test            Runs sanity tests.
   timechart       Tool to visualize total system behavior during a workload
   top             System profiling tool.
   trace           strace inspired tool
   probe           Define new dynamic tracepoints
 See 'perf help COMMAND' for more information on a specific command.
perf 包含了:perf-stat (perf stat), perf-top (perf top), perf-record (perf record), perf-list (perf list)
perf list 主要是用于列出有哪些可用的event,可以供 perf top -e eventname 来分析。
perf top -e xxx
上面的 perf list [hw|sw...] 可以知道所有的 tracepoint events, 接下来我们就可以使用 perf top -e tracepoint-event 来专门获得指定的tracepoint的信息:
perf top常用选项有:
-e <event>:指明要分析的性能事件。
-p <pid>:Profile events on existing Process ID (comma sperated list). 仅分析目标进程及其创建的线程。
-k <path>:Path to vmlinux. Required for annotation functionality. 带符号表的内核映像所在的路径。
-K:不显示属于内核或模块的符号。
-U:不显示属于用户态程序的符号。
-d <n>:界面的刷新周期,默认为2s,因为perf top默认每2s从mmap的内存区域读取一次性能数据。
-g:得到函数的调用关系图。
 
# perf top -p `pidof mysqld`
Samples: 221  of event 'cycles', Event count (approx.): 10810377, UID: mysql
 10.34%  mysqld            [.] my_strnncollsp_utf8                                                                                           
  5.70%  [kernel]          [k] _spin_lock_irqsave                                                                                            
  5.66%  libc-2.12.so      [.] __memset_sse2                                                                                                 
  5.55%  [kernel]          [k] remove_wait_queue                                                                                             
  5.16%  libc-2.12.so      [.] __GI___strcmp_ssse3                                                                                           
  4.36%  [kernel]          [k] __audit_syscall_exit                                                                                          
  3.39%  [kernel]          [k] lookup_ioctx                                                                                                  
  3.28%  [kernel]          [k] find_next_bit                                                                                                 
  3.22%  mysqld            [.] lex_one_token(YYSTYPE*, THD*)                                                                                 
  2.69%  mysqld            [.] Item::val_bool()                                                                                              
  2.60%  [kernel]          [k] __do_softirq                                                                                                  
  2.53%  mysqld            [.] Protocol::send_result_set_row(List<Item>*)                                                                    
  2.52%  [kernel]          [k] local_bh_enable_ip                                                                                            
  2.51%  mysqld            [.] my_strnncoll_binary                                                                                           
  2.51%  libc-2.12.so      [.] memcpy  
第一列:符号引发的性能事件的比例,默认指占用的cpu周期比例。
第二列:符号所在的DSO(Dynamic Shared Object),可以是应用程序、内核、动态链接库、模块。
第三列:DSO的类型。[.]表示此符号属于用户态的ELF文件,包括可执行文件与动态链接库)。[k]表述此符号属于内核或模块。
第四列:符号名。有些符号不能解析为函数名,只能用地址表示。
 

利用strace & Perf分析MySQL的更多相关文章

  1. 从运维角度来分析mysql数据库优化的一些关键点【转】

    概述 一个成熟的数据库架构并不是一开始设计就具备高可用.高伸缩等特性的,它是随着用户量的增加,基础架构才逐渐完善. 1.数据库表设计 项目立项后,开发部根据产品部需求开发项目,开发工程师工作其中一部分 ...

  2. 利用Eventlog Analyzer分析日志

    利用EventlogAnalyzer分析日志 ManageEngineEventLogAnalyzer是一个基于Web技术.实时的事件监控管理解决方案,能够提高企业网络安全.减少工作站和服务器的宕机事 ...

  3. select加锁分析(Mysql)

    [原创]惊!史上最全的select加锁分析(Mysql) 前言 大家在面试中有没遇到面试官问你下面六句Sql的区别呢 select * from table where id = ? select * ...

  4. zabbix利用percona-toolkit工具监控Mysql主从同步状态

    一.下载percona-toolkit工具包 percona-toolkit是一组高级命令行工具的集合,可以查看当前服务的摘要信息,磁盘检测,分析慢查询日志,查找重复索引,实现表同步等等. [root ...

  5. MySQL性能分析, mysql explain执行计划详解

    MySQL性能分析 MySQL性能分析及explain用法的知识是本文我们主要要介绍的内容,接下来就让我们通过一些实际的例子来介绍这一过程,希望能够对您有所帮助. 1.使用explain语句去查看分析 ...

  6. MYSQL启用日志,查看日志,利用mysqlbinlog工具恢复MySQL数据库【转载】

    转自 MYSQL启用日志,查看日志,利用mysqlbinlog工具恢复MySQL数据库 - _安静 - 博客园http://www.cnblogs.com/xionghui/archive/2012/ ...

  7. 深入浅出分析MySQL索引设计背后的数据结构

    在我们公司的DB规范中,明确规定: 1.建表语句必须明确指定主键 2.无特殊情况,主键必须单调递增 对于这项规定,很多研发小伙伴不理解.本文就来深入简出地分析MySQL索引设计背后的数据结构和算法,从 ...

  8. 分析MySQL中哪些情况下数据库索引会失效

    要想分析MySQL查询语句中的相关信息,如是全表查询还是部分查询,就要用到explain. 一.explain 用法:explain +查询语句. id:查询语句的序列号,上面图片中只有一个selec ...

  9. 在CentOS7中利用yum命令安装mysql

    在CentOS7中利用yum命令安装mysql 原创 2016年08月31日 10:42:33 标签: mysql / centos 4832 一.说明 我们是在VMware虚拟机上安装的mysql, ...

随机推荐

  1. Android根据内网外网连接情况配置服务器访问IP

    新项目的app,可通过内网和外网的服务器ip进行请求访问,但是客户提供了专业终端,终端在wifi情况下走外网内网都可以,但关闭wifi则只能走4G专网,也就是只能走内网. 可前往我的小站查看:Andr ...

  2. spring boot + vue 前后分离实现登录功能(三)

    Spring boot 后台 github 地址 SpringBoot-book-vue-demo 使用tk.mytabis 简化mybatis 开发 使用 durid 连接池 连接Mysql pom ...

  3. currency

    currency 美 ['kʌrənsi]   英 ['kʌrənsi]   n.货币:通货:通用:流行 网络流通:货币型:币种

  4. Windows环境下的安装gcc(c语言环境)

    Windows 具有良好的界面和丰富的工具,所以目前 linux 开发的流程是, windows 下完成编码工作, linux 上实现编译工作. 为了提高工作效率,有必要在 windows 环境下搭建 ...

  5. Python有堆栈/堆,如何管理内存?

    Python有堆栈/堆,如何管理内存? - 代码日志 https://codeday.me/bug/20171016/86264.html

  6. Sizes of integer types 整形字节长度 系统字节

    /usr/include/limits.h /* Copyright (C) 1991, 1992, 1996, 1997, 1998, 1999, 2000, 2005 Free Software ...

  7. 网站安全统一监测平台(WebPecker)

    网站安全统一监测平台(WebPecker)_智恒科技http://www.zhihengit.com/html/pro/chanpinfenlei1/2016/0511/135.html [SQL注入 ...

  8. IDEA优化配置,提高启动和运行速度

    IDEA优化配置,提高启动和运行速度   参考链接:https://blog.csdn.net/riju4713/article/details/83217013,http://www.pc0359. ...

  9. postgresql 利用pgAgent实现定时器任务

    1.安装pgAgent 利用Application Stack Builder安装向导,安装pgAgent. 根据安装向导一步一步安装即可. 安装完成之后,windows服务列表中会增加一个服务:Po ...

  10. 使用meta跳转页面

    <Meta http-equiv="Refresh" Content="3; Url=http://www.baidu.com">