03 吞吐和时延 - IO 设备第一要义
什么是 IO
- IO 是 Input 和 Output 的缩写,即为输入/输出;
- 在 Linux 系统中,IO 包括内存 IO,网络 IO 和磁盘 IO 三种;
- 在通常的描述中,IO 指的是网络 IO 和磁盘 IO,在大多数场景下,尤指磁盘 IO;
网络 IO
使用 ifconfig 工具
ifconfig是一个网络配置和管理工具,用于查看和配置网络接口。- 运行
ifconfig -a可以显示系统中所有网络接口的详细信息,包括活跃和非活跃的接口的接口状态、IP 地址、子网掩码、MAC 地址、传输队列长度、接收和发送的数据包及错误统计信息。 - 通过固定时间内的收发字节数可计算出该段时间内的网络流量。
# 安装 ifconfig 工具
➜ ~ sudo yum install net-tools -y
# 显示所有网卡的 MAC 地址、IP、收发包数量等信息
➜ ~ ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.16.3 netmask 255.255.252.0 broadcast 10.0.19.255
inet6 fe80::5054:ff:fe3e:4720 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:3e:47:20 txqueuelen 1000 (Ethernet)
RX packets 3405888 bytes 1652192200 (1.5 GiB) # 接收到的网络包数量及总字节数
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2530819 bytes 406101797 (387.2 MiB) # 发送出的网络包数量及总字节数
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0eth0 接口
eth0 是系统中的第一个以太网接口,典型用于连接局域网。
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.16.3 netmask 255.255.252.0 broadcast 10.0.19.255
inet6 fe80::5054:ff:fe3e:4720 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:3e:47:20 txqueuelen 1000 (Ethernet)
RX packets 3405888 bytes 1652192200 (1.5 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2530819 bytes 406101797 (387.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0flags=4163<UP,BROADCAST,RUNNING,MULTICAST>:网络接口的状态标志。UP:接口已启用。BROADCAST:支持广播通信。RUNNING:接口正在运行。MULTICAST:支持多播。
mtu 1500:最大传输单元(MTU),表示可以传输的最大数据包大小为 1500 字节。inet 10.0.16.3:IPv4 地址。netmask 255.255.252.0:子网掩码,用于区分网络地址和主机地址。broadcast 10.0.19.255:广播地址。inet6 fe80::5054:ff:fe3e:4720:链路本地的 IPv6 地址。prefixlen 64:IPv6 地址的前缀长度。scopeid 0x20<link>:IPv6 地址的作用域 ID,表示该地址仅在本地链路上有效。ether 52:54:00:3e:47:20:MAC 地址(物理地址)。txqueuelen 1000:传输队列长度。RX packets 3405888 bytes 1652192200 (1.5 GiB):接收到的数据包和字节数。RX errors 0 dropped 0 overruns 0 frame 0:接收数据包的错误、丢弃、超载和帧错误数。TX packets 2530819 bytes 406101797 (387.2 MiB):发送的数据包和字节数。TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0:发送数据包的错误、丢弃、超载、载波错误和冲突数。
lo 接口
lo 是环回接口,用于网络软件之间的本地通信,不涉及物理网络硬件。
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0flags=73<UP,LOOPBACK,RUNNING>`:网络接口的状态标志。
UP:接口已启用。LOOPBACK:环回接口。RUNNING:接口正在运行。
使用 sar 工具
sar -n DEV 2 -p 3
# 或者简写为
sar -n DEV 2 3-n DEV:指定要报告的网络设备活动统计信息。2:每 2 秒采集一次数据。-p 3:总共采集 3 次。
➜ ~ sudo yum install sysstat -y
➜ ~ sar -n DEV 2 -p 3
Linux 5.14.0-467.el9.x86_64 (VM-16-3-centos) 07/29/2024 _x86_64_ (4 CPU)
08:29:22 PM rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil IFACE
08:29:24 PM 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 lo
08:29:24 PM 16.00 17.00 1.48 2.65 0.00 0.00 0.00 0.00 eth0
08:29:24 PM rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil IFACE
08:29:26 PM 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 lo
08:29:26 PM 5.50 7.00 0.40 0.76 0.00 0.00 0.00 0.00 eth0
08:29:26 PM rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil IFACE
08:29:28 PM 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 lo
08:29:28 PM 2.50 5.50 0.13 0.89 0.00 0.00 0.00 0.00 eth0
Average: rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil IFACE
Average: 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 lo
Average: 8.00 9.83 0.67 1.43 0.00 0.00 0.00 0.00 eth0系统信息
Linux 5.14.0-467.el9.x86_64 (VM-16-3-centos) 07/29/2024 _x86_64_ (4 CPU)Linux 5.14.0-467.el9.x86_64:内核版本。VM-16-3-centos:主机名。07/29/2024:日期。x86_64:系统架构。(4 CPU):CPU 核心数。
采样数据
08:29:22 PM rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil IFACE
08:29:24 PM 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 lo
08:29:24 PM 16.00 17.00 1.48 2.65 0.00 0.00 0.00 0.00 eth008:29:22 PM:采样时间。rxpck/s:每秒接收的数据包数。txpck/s:每秒发送的数据包数。rxkB/s:每秒接收的字节数(KB)。txkB/s:每秒发送的字节数(KB)。rxcmp/s:每秒接收的压缩数据包数。txcmp/s:每秒发送的压缩数据包数。rxmcst/s:每秒接收的多播数据包数。%ifutil:接口利用率(百分比)。IFACE:网络接口名称。lo:环回接口,通常用于本地网络通信。eth0:第一个以太网接口,用于外部网络通信。
当进行数据传输(如下载代码等)时,将会看到网络流量明显增加
磁盘 IO
整个系统
-b:显示整个系统的 I/O 和传输速率。
➜ ~ sar -b 2 2
Linux 5.14.0-467.el9.x86_64 (VM-16-3-centos) 07/31/2024 _x86_64_ (4 CPU)
09:45:21 AM tps rtps wtps dtps bread/s bwrtn/s bdscd/s
09:45:23 AM 7.50 0.00 7.50 0.00 0.00 80.00 0.00
09:45:25 AM 20.50 0.00 20.50 0.00 0.00 532.00 0.00
Average: 14.00 0.00 14.00 0.00 0.00 306.00 0.00tps(Transactions Per Second):每秒执行的 I/O 事务数量。一次事务是指对磁盘设备进行的一次读或写操作。这里总是等于rtps(每秒读事务数)加上wtps(每秒写事务数)。rtps(Read Transactions Per Second):每秒读请求数。wtps(Write Transactions Per Second):每秒写请求数。dtps(Discard Transactions Per Second):每秒丢弃事务的数量(在这个输出中为 0.00,表示没有丢弃事务)。bread/s(Blocks Read Per Second):每秒从磁盘读取的块数(通常块大小为 512 字节)。bwrtn/s(Blocks Written Per Second):每秒写入磁盘的块数。bdscd/s(Blocks Discarded Per Second):每秒丢弃的块数。
每个磁盘设备
-d:显示每个磁盘设备的 I/O 活动细节。-p:带人类可读的设备名称显示(与-d结合使用)。
➜ ~ sar -d 2 2
Linux 5.14.0-467.el9.x86_64 (VM-16-3-centos) 07/31/2024 _x86_64_ (4 CPU)
09:45:30 AM DEV tps rkB/s wkB/s dkB/s areq-sz aqu-sz await %util
09:45:32 AM vda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
09:45:32 AM sr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
09:45:32 AM DEV tps rkB/s wkB/s dkB/s areq-sz aqu-sz await %util
09:45:34 AM vda 14.00 0.00 178.00 0.00 12.71 0.01 1.07 1.50
09:45:34 AM sr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: DEV tps rkB/s wkB/s dkB/s areq-sz aqu-sz await %util
Average: vda 7.00 0.00 89.00 0.00 12.71 0.01 1.07 0.75
Average: sr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
➜ ~ sar -dp 2 2
Linux 5.14.0-467.el9.x86_64 (VM-16-3-centos) 07/31/2024 _x86_64_ (4 CPU)
09:45:40 AM tps rkB/s wkB/s dkB/s areq-sz aqu-sz await %util DEV
09:45:42 AM 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 vda
09:45:42 AM 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 sr0
09:45:42 AM tps rkB/s wkB/s dkB/s areq-sz aqu-sz await %util DEV
09:45:44 AM 14.00 0.00 178.00 0.00 12.71 0.03 2.25 2.75 vda
09:45:44 AM 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 sr0
Average: tps rkB/s wkB/s dkB/s areq-sz aqu-sz await %util DEV
Average: 7.00 0.00 89.00 0.00 12.71 0.02 2.25 1.38 vda
Average: 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 sr0DEV:设备名称,如vda和sr0。tps(Transactions Per Second):每秒执行的 I/O 事务数量。rkB/s(Read KB per Second):每秒从设备读取的千字节数。wkB/s(Write KB per Second):每秒写入设备的千字节数。dkB/s(Discard KB per Second):每秒从设备丢弃的千字节数。areq-sz(Average Request Size):平均请求大小(以块为单位,通常 512 字节)。aqu-sz(Average Queue Size):平均请求队列长度。await:I/O 请求的平均等待时间(以毫秒为单位)。%util(Utilization):设备忙碌的百分比时间。值越高,设备负载越重。
sar 工具
sar(System Activity Reporter)是一个强大的 Linux/Unix 工具,用于收集、报告和保存系统活动性能指标。它是
sysstat工具包的一部分,广泛用于系统监控和性能分析。sar能够收集 CPU、内存、I/O、网络、设备负载等各个方面的性能数据,并以详细的报表形式展示,帮助系统管理员识别瓶颈和优化系统性能。sar的主要功能包括:- 历史性能数据收集:能够定期收集和存储系统的各种性能数据。
- 多种性能指标:包括 CPU 使用率、内存使用率、设备 I/O、网络流量、进程活动、文件系统活动等。
- 实时监控:可以实时查看系统性能指标,帮助识别和解决系统瓶颈。
- 长期分析:通过对长期收集的数据进行分析,帮助进行容量规划和性能趋势分析。
- 丰富的输出选项:支持多种格式的数据输出,便于导入其他工具进行分析。
使用方法:
bashsar [options] [interval] [count]options:指定要监控的资源类型或输出格式。interval:采样间隔时间(秒)。count:采样次数。
一些常用的
sar选项及其功能:-u:报告 CPU 使用情况。-r:报告内存使用情况。-b:报告 I/O 和传输速率。-n:网络相关统计信息。-n DEV:显示网络接口的传输和接收数据。-n EDEV:显示详细网络错误统计。-n SOCK:显示 socket 使用情况。-n ALL:显示所有网络统计信息。
-d:报告块设备的活动。-q:报告队列长度和负载平均值。-S:报告交换空间的使用。-W:报告交换统计信息。-P:报告特定处理器的统计信息。-P ALL:显示所有 CPU 的信息。
-A:报告所有活动统计信息。
查看 CPU 使用情况
➜ ~ sar -u 2 2
Linux 5.14.0-467.el9.x86_64 (VM-16-3-centos) 07/31/2024 _x86_64_ (4 CPU)
11:21:20 AM CPU %user %nice %system %iowait %steal %idle
11:21:22 AM all 0.50 0.00 1.25 0.00 0.00 98.25
11:21:24 AM all 0.25 0.00 0.38 1.25 0.00 98.12
Average: all 0.38 0.00 0.81 0.63 0.00 98.1911:21:20[时间戳]:数据采集的时间。CPU:指定的 CPU(在此输出中为所有 CPU 的平均值)。%user:用户空间进程使用的 CPU 百分比(不包括通过nice调整优先级的进程)。%nice:通过nice命令调整优先级的用户空间进程使用的 CPU 百分比。%system:内核空间进程使用的 CPU 百分比。%iowait:CPU 等待 I/O 操作完成的时间百分比。%steal:虚拟机管理程序从虚拟机偷取的时间百分比(常见于虚拟化环境中)。%idle:CPU 空闲时间的百分比。
查看内存使用情况
➜ ~ sar -r 2 2
Linux 5.14.0-467.el9.x86_64 (VM-16-3-centos) 07/31/2024 _x86_64_ (4 CPU)
11:22:13 AM kbmemfree kbavail kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
11:22:15 AM 537280 3035488 280280 7.48 97548 2552292 868144 23.17 441188 2388468 328
11:22:17 AM 537280 3035492 280276 7.48 97548 2552296 868144 23.17 441276 2388472 332
Average: 537280 3035490 280278 7.48 97548 2552294 868144 23.17 441232 2388470 33011:22:13[时间戳]:数据采集的时间。kbmemfree:系统中当前可用的物理内存量(以千字节为单位)。- kbava`il:可用内存量,包括部分被缓存和缓冲区使用的内存,可以立即用于新进程。
kbmemused:已用物理内存量(以千字节为单位),包括内核、应用程序及缓存和缓冲区的内存使用。%memused:已用物理内存的百分比。kbbuffers:用于缓冲区的内存量(以千字节为单位),通常由块设备使用。kbcached:用于缓存的内存量(以千字节为单位),用于缓冲文件系统数据,以加快文件的读取和写入操作。kbcommit:承诺给正在运行的进程的虚拟内存总量(以千字节为单位),包括内存中的数据和 swap 交换分区中的数据。%commit:kbcommit相对于总内存加交换空间的百分比。kbactive:最近使用的内存量(以千字节为单位),这部分内存不会很快被换出。kbinact:较久未使用的内存量(以千字节为单位),这部分内存可能会被换出。kbdirty:脏数据量(以千字节为单位),尚未写入磁盘的内存。
yum provides 查找软件包
yum provides是一个yum(Yellowdog Updater, Modified) 工具的子命令,用于在基于 RPM 的 Linux 发行版(如 CentOS、Red Hat Enterprise Linux 和 Fedora)上查找包含特定文件或功能的包。
➜ ~ yum provides sar
Repository baseos is listed more than once in the configuration
Repository baseos-source is listed more than once in the configuration
Repository appstream is listed more than once in the configuration
Repository appstream-source is listed more than once in the configuration
Repository rt is listed more than once in the configuration
Repository rt-source is listed more than once in the configuration
Repository resilientstorage is listed more than once in the configuration
Repository resilientstorage-source is listed more than once in the configuration
Last metadata expiration check: 0:56:19 ago on Wed 31 Jul 2024 09:05:43 AM CST.
sysstat-12.5.4-3.el9.x86_64 : Collection of performance monitoring tools for Linux
Repo : appstream
Matched from:
Filename : /usr/bin/sar功能
- 查找特定文件的包:帮助用户找到安装特定文件所需的包。
- 解决依赖问题:在遇到缺少特定文件的依赖问题时,提供解决方案。
- 系统管理:帮助系统管理员更好地管理和维护系统包和依赖关系。
用法
yum provides <file_path>yum provides命令会查询本地的 Yum 缓存以及远程仓库,以找到所有包含指定文件或库的包。- 文件路径可以使用通配符(如
*和?),以便更灵活地查找相关包。
常见用法:
查找提供
/usr/bin/wget文件的包:bashyum provides /usr/bin/wget查找包含某个库文件(例如
libssl.so.1.1)的包:bashyum provides '*/libssl.so.1.1'
注意事项
- 由于
yum provides需要查询大量的包信息,有时运行速度可能会较慢。 - 确保你的系统已更新本地的 Yum 缓存,以获得准确的搜索结果。可以使用
yum makecache命令来更新缓存。