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. Linux静态路由配置

    配置了多块网卡后,需要指定数据包由哪块网卡发送,否则可能无法访问内网,这就要用到静态路由了. 配置静态路由有多种方式: 1.修改 /etc/rc.local 文件,这样每次重启后就会自动添加,如: e ...

  2. CAT中实现异步请求的调用链查看

    CAT简介 CAT(Central Application Tracking),是美团点评基于 Java 开发的一套开源的分布式实时监控系统.美团点评基础架构部希望在基础存储.高性能通信.大规模在线访 ...

  3. 服务端高并发分布式架构 ESB 企业服务总线

    服务端高并发分布式架构演进之路 - 个人文章 - SegmentFault 思否 https://segmentfault.com/a/1190000018626163 ESB 企业服务总线讲解 ht ...

  4. iptables nat表配置

  5. 【转载】 linux dig 命令使用方法

    原文地址: https://www.imooc.com/article/26971?block_id=tuijian_wz 作者:ibeautiful来源:慕课网 ------------------ ...

  6. Java 有双引号的字符串处理

    public class Test{ public static void main(String[] args){ String str1 = "\"name\"&qu ...

  7. zookeeper使用过程的错误

    一.zookeeper启动成功,dubbo服务也注册成功,但是服务消费者调用失败 报错如下: [myid:] - INFO [SyncThread:0:ZooKeeperServer@645] - E ...

  8. DataWorks2.0——DataStudio简单对比使用上手

    1.原先的数据管理去哪里了? 悬停在此图标上即可:  2.项目模式有何不同?

  9. RabbitMQ 入门教程(PHP版) 使用rabbitmq-delayed-message-exchange插件实现延迟功能

    延迟任务应用场景 场景一:物联网系统经常会遇到向终端下发命令,如果命令一段时间没有应答,就需要设置成超时. 场景二:订单下单之后30分钟后,如果用户没有付钱,则系统自动取消订单. 场景三:过1分钟给新 ...

  10. python flask框架学习(三)——豆瓣微信小程序案例(二)整理封装block,模板的继承

    我们所要实现的效果: 点击电影的更多,跳转到更多的电影页面:点击电视剧的更多,跳转到更多的电视剧页面. 三个页面的风格相同,可以设置一个模板,三个页面都继承这个模板 1.在指定模板之前,把css放在一 ...