[转帖]SystemTap Beginners Guide -disk

systemtap,beginners,guide,disk · 浏览次数 : 0

小编点评

**SystemTap Beginners Guide** **5.2.1. Summarizing Disk Read/Write Traffic** This section describes how to identify which processes are performing the heaviest disk reads/writes to the system. **Key Points:** * Use the `stap` command to monitor disk and I/O activity. * The script outputs the top ten processes responsible for the heaviest reads/writes to disk. * The output includes the following data per listed process: * UID * PID * PPID * CMD * Device * Action (W or R) * Bytes read/written * The script uses `ctime()` and `gettimeofday_s()` to get the current time and date. **Example Output:** ``` Mon Sep 29 03:38:28 2008 , Average: 19Kb/sec, Read: 7Kb, Write: 89KbUID PID PPID CMD DEVICE T BYTES0 26319 26294 firefox sda5 W 902290 2758 2757 pam_timestamp_c sda5 R 80640 2885 1 cupsd sda5 W 1678 ``` **Benefits of Using the Script:** * Provides insights into resource consumption for heavy disk operations. * Helps identify processes that are heavily accessing the system's storage. * Allows you to optimize system performance by targeting high-performing processes.

正文

  • SystemTap Beginners Guide

⁠5.2. Disk

The following sections showcase scripts that monitor disk and I/O activity.

⁠5.2.1. Summarizing Disk Read/Write Traffic

This section describes how to identify which processes are performing the heaviest disk reads/writes to the system.
disktop.stp
#!/usr/bin/env stap 
#
# Copyright (C) 2007 Oracle Corp.
#
# Get the status of reading/writing disk every 5 seconds,
# output top ten entries 
#
# This is free software,GNU General Public License (GPL);
# either version 2, or (at your option) any later version.
#
# Usage:
#  ./disktop.stp
#

global io_stat,device
global read_bytes,write_bytes

probe vfs.read.return {
  if (returnval()>0) {
    if (devname!="N/A") {/*skip read from cache*/
      io_stat[pid(),execname(),uid(),ppid(),"R"] += returnval()
      device[pid(),execname(),uid(),ppid(),"R"] = devname
      read_bytes += returnval()
    }
  }
}

probe vfs.write.return {
  if (returnval()>0) {
    if (devname!="N/A") { /*skip update cache*/
      io_stat[pid(),execname(),uid(),ppid(),"W"] += returnval()
      device[pid(),execname(),uid(),ppid(),"W"] = devname
      write_bytes += returnval()
    }
  }
}

probe timer.ms(5000) {
  /* skip non-read/write disk */
  if (read_bytes+write_bytes) {

    printf("\n%-25s, %-8s%4dKb/sec, %-7s%6dKb, %-7s%6dKb\n\n",
           ctime(gettimeofday_s()),
           "Average:", ((read_bytes+write_bytes)/1024)/5,
           "Read:",read_bytes/1024,
           "Write:",write_bytes/1024)

    /* print header */
    printf("%8s %8s %8s %25s %8s %4s %12s\n",
           "UID","PID","PPID","CMD","DEVICE","T","BYTES")
  }
  /* print top ten I/O */
  foreach ([process,cmd,userid,parent,action] in io_stat- limit 10)
    printf("%8d %8d %8d %25s %8s %4s %12d\n",
           userid,process,parent,cmd,
           device[process,cmd,userid,parent,action],
           action,io_stat[process,cmd,userid,parent,action])

  /* clear data */
  delete io_stat
  delete device
  read_bytes = 0
  write_bytes = 0  
}

probe end{
  delete io_stat
  delete device
  delete read_bytes
  delete write_bytes
}
disktop.stp outputs the top ten processes responsible for the heaviest reads/writes to disk. Example 5.6, “disktop.stp Sample Output” displays a sample output for this script, and includes the following data per listed process:
  • UID — user ID. A user ID of 0 refers to the root user.
  • PID — the ID of the listed process.
  • PPID — the process ID of the listed process's parent process.
  • CMD — the name of the listed process.
  • DEVICE — which storage device the listed process is reading from or writing to.
  • T — the type of action performed by the listed process; W refers to write, while R refers to read.
  • BYTES — the amount of data read to or written from disk.
The time and date in the output of disktop.stp is returned by the functions ctime() and gettimeofday_s()ctime() derives calendar time in terms of seconds passed since the Unix epoch (January 1, 1970). gettimeofday_s() counts the actual number of seconds since Unix epoch, which gives a fairly accurate human-readable timestamp for the output.
In this script, the $return is a local variable that stores the actual number of bytes each process reads or writes from the virtual file system. $return can only be used in return probes (for example, vfs.read.return and vfs.read.return).

Example 5.6. disktop.stp Sample Output

[...]
Mon Sep 29 03:38:28 2008 , Average:  19Kb/sec, Read: 7Kb, Write: 89Kb

UID      PID     PPID                       CMD   DEVICE    T    BYTES
0    26319    26294                   firefox     sda5    W        90229
0     2758     2757           pam_timestamp_c     sda5    R         8064
0     2885        1                     cupsd     sda5    W         1678

Mon Sep 29 03:38:38 2008 , Average:   1Kb/sec, Read: 7Kb, Write: 1Kb

UID      PID     PPID                       CMD   DEVICE    T    BYTES
0     2758     2757           pam_timestamp_c     sda5    R         8064
0     2885        1                     cupsd     sda5    W         1678

与[转帖]SystemTap Beginners Guide -disk相似的内容:

[转帖] SystemTap Beginners Guide -network

SystemTap Beginners Guide Next ⁠Chapter 5. Useful SystemTap Scripts 5.1. Network5.1.1. Network Profiling5.1.2. Tracing Functions Called in Network Soc

[转帖]SystemTap Beginners Guide -profiling

Next ⁠5.3. Profiling The following sections showcase scripts that profile kernel activity by monitoring function calls. ⁠5.3.1. Counting Function Call

[转帖]Systemtap的另类用法

November 10th, 2010Yu Feng 原创文章,转载请注明: 转载自系统技术非业余研究 本文链接地址: Systemtap的另类用法 通常我们在做内核编程的时候,会用到内核的数据结构,比如说textsearch提供了几种算法用于支付串查找。在用于正式的项目前,我们会希望考察下他的用法

[转帖]systemtap - perf - 火焰图

https://www.cnblogs.com/hixiaowei/p/15580662.html 0. 火焰图生成框架 1. Capture stacks 2. Fold stacks 3. flamegraph.pl 1. perf (13条消息) 性能分析之profiling及火焰图_巷中人的

[转帖]Systemtap 用法

https://www.jianshu.com/p/fb4dde8baff4 霸爷博客,干货满满。有两篇文章现在还记得,《Linux下如何知道文件被哪个进程写》和《巧用Systemtap注入延迟模拟IO设备抖动》,周末突然想起来,发现能看懂了:) 什么是 systemtap Systemtap is

[转帖]Systemtap系列之语法专辑

1. 常用技巧 systemtap可以实现交叉编译: 编译可执行模块如下: stap -r kernel_version script -m module_name 运行命令如下: staprun module_name.ko stap命令会读取脚本的指令,并翻译成C代码,编译成内核模块加载到内核。

[转帖]systemtap 之 基础命令

https://phpor.net/blog/post/3661 示例1: 通过 -e 选项直接执行probe,如: 1 stap -ve 'probe kernel.function("do_fork") { printf("%-25s %-10d 0x%-x\n", execname(), pi

[转帖]巧用Systemtap注入延迟模拟IO设备抖动

http://blog.yufeng.info/archives/2935 原创文章,转载请注明: 转载自系统技术非业余研究 本文链接地址: 巧用Systemtap注入延迟模拟IO设备抖动 当我们的IO密集型的应用怀疑设备的IO抖动,比如说一段时间的wait时间过长导致性能或其他疑难问题的时候,这个

[转帖]s-systemtap工具使用图谱(持续更新)

整体的学习思维导图如下,后续持续更新完善文章目录​​安装​​​​简介​​​​执行流程​​​​执行方式​​​​stap脚本语法​​​​探针语法​​​​API函数​​​​探针举例​​​​变量使用​​​​基本应用​​​​1. 定位函数位置​​​​2. 查看文件能够添加探针的位置​​​​3. 打印函数参数(

[转帖]s-systemtap工具使用图谱(持续更新)

整体的学习思维导图如下,后续持续更新完善文章目录​​安装​​​​简介​​​​执行流程​​​​执行方式​​​​stap脚本语法​​​​探针语法​​​​API函数​​​​探针举例​​​​变量使用​​​​基本应用​​​​1. 定位函数位置​​​​2. 查看文件能够添加探针的位置​​​​3. 打印函数参数(