本文转载自微信公众号《DBA的杂念录》,作者潇湘隐士。转载本文请联系DBA杂念公众号。iotop简介:iotop是一个类似于top命令的开源免费工具,用于监控磁盘I/O使用情况。iotop可以监控进程I/O信息。它是用Python语言编写的。与iostat工具相比,iostat是系统级的IO监控,而iotop是进程级的IO监控。目前最新版本是iotop0.6。其官网http://guichaz.free.fr/iotop/[1]iotop的安装:注意iotop的安装是有前提条件的(如果操作系统不满足这些条件,iotop是无法正确安装的):1:内核版本为2.6.20或更高版本2:Python2.7或更高版本官方文档详情如下:Linux一直能够显示有多少I/O进行了(vmstat的bi和bo列1command).Iotop是一个Python程序,带有类似top的UI,用于代表哪个进程进行I/O显示。它需要Python≥2.7和Linux内核≥2.6.20以及TASK_DELAY_ACCTCONFIG_TASKSTATS、TASK_IO_ACCOUNTING和CONFIG_VM_EVENT_COUNTERSop.iotop的yum安装yum安装是最简单和最快的。前提是你有配置yum的源码配置。[root@DB-Server~]#python-VPython2.7.5[root@DB-Server~]#uname-aLinuxDB-Server3.10.0-862.el7.x86_64#1SMPFriApr2016:44:24UTC2018x86_64x86_64x86_64GNU/Linux[root@DB-Server~]#[root@DB-Server~]#yum-yinstalliotopiotop的源代码安装wgethttp://guichaz.free.fr/iotop/files/iotop-0.6.tar.bz2tar-xvfiotop-0.6.tar.gzcdiotop-0.6pythonsetup.pybuildpythonsetup.pyinstalliotop的参数#iotop--helpUsage:/usr/local/sbin/iotop[OPTIONS]DISKREADandDISKWRITEaretheblockI/Obandwidthusedduringthesamplingperiod.SWAPINandIOarethepercentagesoftimethethreadspentrespectivelywhileswappinginandwaitingonI/Omoregenerally.PRIOistheI/Opriorityatwhichthethreadisrunning(setusingtheionicecommand).Controls:leftandrightarrowstochangethesortingcolumn,rtoinvertthesortingorder,ototogglethe--onlyoption,ptotogglethe--processesoption,atogglethe--accumulatedoption,itochangeI/Opriority,qtoquit,anyotherkeytoforcearerefresh.Options:--versionshowprogram'sversionnumberandexit-h,--helpshowthishelpmessageandexit-o,--on仅显示实际执行I/O-b的进程或线程,--batchnon-interactivemode-nNUM,--iter=NUMnumberofiterationsbeforeeding[infinite]-dSEC,--delay=SECdelaybetweeniterations[1second]-pPID,--pid=PIDprocesses/threadstomonitor[all]-uUSER,--user=USERuserstomonitor[all]-P,--processesonlyshowprocesses,notallthreads-a,--accumulatedshowaccumulatedI/Oinsteadofbandwidth-k,--kilobytesusekilobytesinsteadofahumanfriendlyunit-t,--timeaddatimestamponeachline(implies--batch)-q,--quietsuppresssomelinesofheaders)iotop快捷键r:反向排序,o:切换到选项--only,p:切换到--processes选项,a:切换到--accumulated选项q:退出i:更改线程优先级iotop示例1:只显示正在产生I/O的进程#iotop-o2:使用非交互方式将iotop命令的输出信息写入日志#nohupiotop-b-o-n10-d5-t>/tmp/iotop.log&3:使用iotop命令找到ConsumeI/O最高的进程,然后找到它所在的SQL语句通过进程执行#iotop-oPPIDPRIOUSERDISKREADDISKWRITESWAPINIO>COMMAND11741be/4oracle4.70M/s0.00B/s0.00%98.60%ora_s004_SCM211739be/4oracle66.99M/s0.00B/s0.00%93.30%ora_s005M_21SCM21.00%93.30%ora_s005M_21SCM21/s0.00%91.78%ora_s006_SCM211743be/4oracle4.87M/s3.78M/s0.00%27.74%ora_s007_SCM211745be/4oracle62.39K/s0.00B/s0.00%2.88%ora_s008_SCM211733be/4oracle39.00K/s0.00B/s0.00%2.08%ora7_sCM/s20016_4oracle0.00B/s1879.61K/s0.00%0.30%ora_dbw0_SCM211699be/4oracle0.00B/s102.36K/s0.00%0.20%ora_lgwr_SCM2找到消耗I/O最多的进程后,找出SQLORACLE进程正在执行StatementSQL>@getsql_by_spid.sqlEntervalueforpid:11741old13:WHEREc.spid='&pid'))new13:WHEREc.spid='11741'))SQL_TEXT------------------------------------------------...........................................(实际环境中,这里会输出SQL语句)getsql_by_spid.sql脚本如下:SELECT/*+ORDERED*/sql_textFROMv$sqltextaWHERE(a.hash_value,a.address)IN(SELECTDECODE(sql_hash_value,0,prev_hash_value,sql_hash_value),解码(sql_hash_value,0,prev_sql_addr,sql_address)FROMv$sessionbWHEREb.paddr=(SELECTaddrFROMv$processcWHEREc.spid='&pid'))ORDERBYpieceASC/这里只是举几个简单的例子,b因为iotop命令很简单,了解了iotop的参数和快捷键后,基本上只需要获取进程的I/O信息或者根据实际需要的信息,剩下的就是参考资料分析判断了[1]iotop:http://guichaz.free。fr/iotop/
