使用pt-ioprofile监控数据库io文件读写情况

我们在做IO密集型的应用程序的时候,比如MySQL数据库,通常系统的表现取决于workload的类型。 比如我们要调优,我们就必须非常清楚的知道数据的访问规律,收集到足够的数据,用来做调优的依据。

有很多工具可以收集系统层面的,设备层面的,进程层面的IO数据,但是没有一个现成的工具可以回答我们比如应用打开了多少文件,文件的读和写的比例是多少,调用了多少次sync, 每次的数据大小是多少,调用了多少次,每次用了多少时间, 是顺序操作还是随机操作,是那个线程发起的操作。

pt-ioprofile是percona提供的用于监控进程io和文件读写的一个工具。

pt-ioprofile does two things: 1) get lsof+strace for -s seconds, 2) aggregate the result. If you specify a FILE, then step 1) is not performed.

风险:

WARNING: pt-ioprofile freezes the server and may crash the process, or make it perform badly after detaching, or leave it in a sleeping state! Before using this tool, please:

  • Read the tool’s documentation
  • Review the tool’s known “BUGS”
  • Test the tool on a non-production server
  • Backup your production server and verify the backups
pt-ioprofile should be considered an intrusive tool, and should not be used on production servers unless you understand and accept the risks.

 

由于pt-ioprofile是一种侵入性工具,所以尽量不要在生产上使用,可能会引起进程挂掉。

pt-ioprofile使用的是strace 和lsof来监控进程操作,最后得出操作文件的列表,默认直接运行是监控mysql进程,默认时间是30s。不过也可以用来监控PostgreSQL的服务进程。

下面是几个示例:

使用pgbench进行压测,压测脚本为:

\set id random(1,100000000)    
insert into test (id,info,crt_time) values (:id, md5(random()::text), now()) on conflict (id) do update 
set info=excluded.info, crt_time=excluded.crt_time;

压测命令:

pgbench -M prepared -U swrd swrd  -n -r -P 1 -f ./test.sql -c 32 -j 32 -T 100

 

指定监控pg进程:

# pt-ioprofile  -p 7269

Mon Oct 23 15:38:58 CST 2017
Tracing process ID 7269
     total       read      write       open      close      lseek filename
  0.078474   0.075984   0.000000   0.001795   0.000000   0.000695 base/16401/2619_fsm
  0.049367   0.049318   0.000000   0.000023   0.000000   0.000026 base/16401/2840
  0.004804   0.004764   0.000000   0.000018   0.000011   0.000011 base/16401/2688
  0.003581   0.003302   0.000000   0.000125   0.000154   0.000000 base/16401/pg_internal.init
  0.003091   0.000000   0.000000   0.003078   0.000000   0.000013 base/13269/2601
  0.002482   0.000000   0.000000   0.000018   0.000000   0.002464 base/13269/1259
  0.000829   0.000800   0.000000   0.000016   0.000013   0.000000 base/13269/pg_internal.init
  0.000770   0.000000   0.000000   0.000042   0.000697   0.000031 base/16401/38453
  0.000613   0.000000   0.000000   0.000181   0.000419   0.000013 base/16401/2601
  0.000567   0.000160   0.000000   0.000301   0.000106   0.000000 pg_stat_tmp/global.stat
  0.000448   0.000153   0.000000   0.000154   0.000000   0.000141 base/16401/2696
  0.000385   0.000326   0.000000   0.000035   0.000024   0.000000 global/pg_internal.init
  0.000329   0.000190   0.000000   0.000117   0.000022   0.000000 global/pg_filenode.map
  0.000253   0.000031   0.000028   0.000114   0.000036   0.000044 base/16401/38453_vm
  0.000245   0.000100   0.000000   0.000057   0.000000   0.000088 base/16401/2840_fsm
  0.000194   0.000111   0.000000   0.000044   0.000000   0.000039 base/16401/2654
  0.000164   0.000081   0.000000   0.000050   0.000033   0.000000 pg_stat_tmp/db_0.stat
  0.000161   0.000107   0.000000   0.000031   0.000023   0.000000 pg_stat_tmp/db_16401.stat
  0.000156   0.000000   0.000000   0.000117   0.000000   0.000039 base/16401/2619_vm
  0.000150   0.000000   0.000000   0.000064   0.000046   0.000040 base/16401/38453_fsm
  0.000101   0.000011   0.000000   0.000071   0.000019   0.000000 base/16401/pg_filenode.map
  0.000088   0.000037   0.000000   0.000025   0.000000   0.000026 base/16401/2840_vm
  0.000085   0.000000   0.000000   0.000022   0.000012   0.000051 base/16401/38459
  0.000075   0.000000   0.000000   0.000017   0.000011   0.000047 base/16401/1259
  0.000074   0.000045   0.000000   0.000016   0.000013   0.000000 pg_stat_tmp/db_13269.stat
  0.000071   0.000000   0.000000   0.000036   0.000013   0.000022 global/1262
  0.000058   0.000021   0.000000   0.000025   0.000012   0.000000 base/16401/PG_VERSION
  0.000046   0.000016   0.000000   0.000019   0.000011   0.000000 base/13269/PG_VERSION
  0.000039   0.000012   0.000000   0.000016   0.000011   0.000000 base/13269/pg_filenode.map
  0.000022   0.000000   0.000000   0.000022   0.000000   0.000000 base/16401/38459_fsm

其中,

read:从文件中读出数据。要读取的文件用文件描述符标识,数据读入一个事先定义好的缓冲区。

write:把缓冲区的数据写入文件中。

pread:由于lseek和read调用之间,内核可能会临时挂起进程,所以对同步问题造成了问题,调用pread相当于顺序调用了lseek和read,这两个操作相当于一个捆绑的原子操作。

pwrite:由于lseek和write调用之间,内核可能会临时挂起进程,所以对同步问题造成了问题,调用pwrite相当于顺序调用了lseek 和write,这两个操作相当于一个捆绑的原子操作。

fsync:确保文件所有已修改的内容已经正确同步到硬盘上,该调用会阻塞等待直到设备报告IO完成。

open:打开一个文件,并返回这个文件的描述符。

close:close系统调用用于“关闭”一个文件,close调用终止一个文件描述符以及文件之间的关联。文件描述符被释放,并能够重新使用。

lseek:对文件描述符指定文件的读写指针进行设置,也就是说,它可以设置文件的下一个读写位置。

fcntl:针对(文件)描述符提供控制。

 

看看各参数的作用:

--aggregate

统计的方式,默认是sum,也可指定为avg。

short form: -a; type: string; default: sum

The aggregate function, either sum or avg.

If sum, then each cell will contain the sum of the values in it. If avg, then each cell will contain the average of the values in it.

--cell

显示的单位,默认是times,即IO操作的时间,也可指定为count(IO操作的次数),size(IO操作的大小)

如下所示:

--cell=times

Tracing process ID 7269
     total       read       open      close      lseek filename
  0.000378   0.000351   0.000016   0.000011   0.000000 base/13269/pg_internal.init
  0.000140   0.000053   0.000053   0.000034   0.000000 pg_stat_tmp/global.stat
  0.000105   0.000077   0.000016   0.000012   0.000000 global/pg_internal.init
  0.000067   0.000041   0.000015   0.000011   0.000000 pg_stat_tmp/db_13269.stat
  0.000061   0.000000   0.000016   0.000000   0.000045 base/13269/1259
  0.000051   0.000013   0.000025   0.000013   0.000000 global/pg_filenode.map
  0.000044   0.000016   0.000015   0.000013   0.000000 pg_stat_tmp/db_0.stat
  0.000043   0.000016   0.000016   0.000011   0.000000 base/13269/PG_VERSION
  0.000036   0.000011   0.000015   0.000010   0.000000 base/13269/pg_filenode.map
  0.000030   0.000000   0.000019   0.000000   0.000011 base/13269/2601
  0.000028   0.000000   0.000017   0.000000   0.000011 global/1262

--cell=sizes

Tracing process ID 7269
     total       read       open      close      lseek filename
 667189248          0          0          0  667189248 base/16401/38453
 128270336          0          0          0  128270336 base/16401/38459
    819200          0          0          0     819200 base/16401/1259
    196608          0          0          0     196608 base/16401/2840
    112660     112660          0          0          0 base/16401/pg_internal.init
     32458      32458          0          0          0 pg_stat_tmp/db_16401.stat
     24576          0          0          0      24576 base/16401/2840_fsm
     16664      16664          0          0          0 global/pg_internal.init
      8192          0          0          0       8192 global/1262
      8192          0          0          0       8192 base/16401/38453_vm
      8192          0          0          0       8192 base/16401/2601
      4752       4752          0          0          0 pg_stat_tmp/global.stat
      4742       4742          0          0          0 pg_stat_tmp/db_0.stat
       512        512          0          0          0 global/pg_filenode.map
       512        512          0          0          0 base/16401/pg_filenode.map
         4          4          0          0          0 base/16401/PG_VERSION

--cell=count

Tracing process ID 7269
     total       read       open      close      lseek filename
        31         29          1          1          0 base/16401/pg_internal.init
         9          3          3          3          0 pg_stat_tmp/global.stat
         8          6          1          1          0 global/pg_internal.init
         6          4          1          1          0 pg_stat_tmp/db_16401.stat
         5          0          1          0          4 base/16401/1259
         3          1          1          1          0 pg_stat_tmp/db_0.stat
         3          1          1          1          0 global/pg_filenode.map
         3          1          1          1          0 base/16401/PG_VERSION
         3          1          1          1          0 base/16401/pg_filenode.map
         2          0          1          0          1 global/1262
         2          0          1          0          1 base/16401/2601
short form: -c; type: string; default: times
The cell contents.
Valid values are:
VALUE CELLS CONTAIN
===== =======================
count Count of I/O operations
sizes Sizes of I/O operations
times I/O operation timing

--group-by

分组的单位,默认是filename,即对文件名进行统计,也可指定为all,即对所有操作进行统计,pid,对进程进行统计

short form: -g; type: string; default: filename
The group-by item.
Valid values are:
VALUE GROUPING
===== ======================================
all Summarize into a single line of output
filename One line of output per filename
pid One line of output per process ID

--run-time

执行strace命令的时间,OPT_RUN_TIME就是--run-time指定的值。

--save-samples

将strace和lsof获取的结果保存到指定的文件中

type: string

Filename to save samples in; these can be used for later analysis.

 

参考:

http://www.cnblogs.com/ivictor/p/6013980.html

https://www.percona.com/doc/percona-toolkit/2.2/pt-ioprofile.html#options

posted on 2017-10-23 16:52  Still water run deep  阅读(1170)  评论(0编辑  收藏  举报

导航