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. Java 面向对象(十五)

    Lambda表达式 1. 函数式编程思想概述 在数学中,函数就是有输入量.输出量的一套计算方案,也就是"拿什么东西做什么事情".相对而言,面向对象过分强调"必须通过对象的 ...

  2. [电脑]拆解DELL 2007FPb液晶显示器

    最近修了不少三星214T显示器,拆卸很方便,多数更换电容就OK了.但有一台出现了花屏,怀疑是数码板出问题了.单位有台显示屏破碎的DELL2007FPb,拆了看看能否借用数码板. 图片:IMG_2063 ...

  3. 手写HashMap实践

    1.什么是HashMap 2.源码分析 3.手写实现 4.不足 一.什么是HashMap hash散列 将一个任意长度通过某种算法(hash函数算法)换成一个固定值 map: 地图x,y 存储 总结: ...

  4. 模型稳定性指标—PSI

    由于模型是以特定时期的样本所开发的,此模型是否适用于开发样本之外的族群,必须经过稳定性测试才能得知.稳定度指标(population stability index ,PSI)可衡量测试样本及模型开发 ...

  5. Dart静态方法、对象操作符和类的继承

    /* Dart中的静态成员: 1.使用static 关键字来实现类级别的变量和函数 2.静态方法不能访问非静态成员,非静态方法可以访问静态成员 */ // class Person { // stat ...

  6. openresty开发系列11--openresty的api入门

    openresty开发系列11--openresty的api入门 1)ngx_lua模块的hello world编辑nginx下conf配置文件nginx.conf# vi nginx.conf在se ...

  7. ansible常用的方法小结

    一.批量安装zabbix客户端 .拷贝sh脚本和.conf到远程服务器(也可以全量拷贝客户端) ansible all -m copy -a "src=/usr/local/zabbix_a ...

  8. Python3基础 输出逐行递增的小星星

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  9. JSOUP 爬虫

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.mavne 依赖: <!--html 解析 : jsoup HTML parser library @ ...

  10. 深入学习c++--多线程编程(一)

    1. 简介 2. 线程使用 2.1 demo #include <iostream> #include <thread> #include <future> usi ...