且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Linux中测量命令的IOPS?

更新时间:2023-10-17 16:22:46

在典型的Linux系统上,有多个time(1)命令;默认是内置的bash(1),它有些基本.还有一个/usr/bin/time,您可以通过完全像这样调用它来运行,或者通过在其前面加上反斜杠来告诉bash(1)不要使用别名和内置函数:\time. Debian在默认安装的时间"软件包中提供了该软件包,Ubuntu可能完全相同,其他发行版也非常相似.

There are multiple time(1) commands on a typical Linux system; the default is a bash(1) builtin which is somewhat basic. There is also /usr/bin/time which you can run by either calling it exactly like that, or telling bash(1) to not use aliases and builtins by prefixing it with a backslash thus: \time. Debian has it in the "time" package which is installed by default, Ubuntu is likely identical, and other distributions will be quite similar.

以与内置shell相似的方式调用它已经更加冗长和翔实,尽管它可能更加不透明,除非您已经熟悉数字的真正含义:

Invoking it in a similar fashion to the shell builtin is already more verbose and informative, albeit perhaps more opaque unless you're already familiar with what the numbers really mean:

$ \time df
[output elided]
0.00user 0.00system 0:00.01elapsed 66%CPU (0avgtext+0avgdata 864maxresident)k
0inputs+0outputs (0major+261minor)pagefaults 0swaps

但是,我想提醒您注意手册页,其中列出了-f选项以自定义输出格式,尤其是%w格式,该格式计算进程放弃其CPU的次数I/O的时间片:

However, I'd like to draw your attention to the man page which lists the -f option to customise the output format, and in particular the %w format which counts the number of times the process gave up its CPU timeslice for I/O:

$ \time -f 'ios=%w' du Maildir >/dev/null
ios=184
$ \time -f 'ios=%w' du Maildir >/dev/null
ios=1

请注意,第一次运行停止了I/O 184次,但是第二次运行仅停止了一次.第一个数字是可信的,因为在我的~/Maildir中有124个目录:目录和inode的读取大约为每个目录提供IOPS,少了一点,因为某些inode可能彼此相邻并可以在一个操作中读取,再加上一些额外的内容以映射到du(1)二进制文件,共享库等中.

Note that the first run stopped for I/O 184 times, but the second run stopped just once. The first figure is credible, as there are 124 directories in my ~/Maildir: the reading of the directory and the inode gives roughly two IOPS per directory, less a bit because some inodes were likely next to each other and read in one operation, plus some extra again for mapping in the du(1) binary, shared libraries, and so on.

由于Linux的磁盘缓存,第二个数字当然要低一些.因此,最后一步是刷新缓存. sync(1)是一个熟悉的命令,它会将脏的写刷新到磁盘,但不会刷新读缓存.您可以通过将3写入/proc/sys/vm/drop_caches来刷新该代码. (其他值有时也有用,但在这里您需要3.)作为非root用户,执行此操作的最简单方法是:

The second figure is of course lower due to Linux's disk cache. So the final piece is to flush the cache. sync(1) is a familiar command which flushes dirty writes to disk, but doesn't flush the read cache. You can flush that one by writing 3 to /proc/sys/vm/drop_caches. (Other values are also occasionally useful, but you want 3 here.) As a non-root user, the simplest way to do this is:

echo 3 | sudo tee /proc/sys/vm/drop_caches

将其与/usr/bin/time结合可以使您构建需要对基准命令进行基准测试的脚本.

Combining that with /usr/bin/time should allow you to build the scripts you need to benchmark the commands you're interested in.

除未成年人外,使用tee(1)是因为它不起作用:

As a minor aside, tee(1) is used because this won't work:

sudo echo 3 >/proc/sys/vm/drop_caches

原因?尽管echo(1)以root身份运行,但重定向是使用您的普通用户帐户进行的,该帐户没有对drop_caches的写入权限. tee(1)以root身份有效地进行重定向.

The reason? Although the echo(1) runs as root, the redirection is as your normal user account, which doesn't have write permissions to drop_caches. tee(1) effectively does the redirection as root.