How to measure IOPS for a command in linux?

后端 未结 3 1676
盖世英雄少女心
盖世英雄少女心 2021-02-20 05:07

I\'m working on a simulation model, where I want to determine when the storage IOPS capacity becomes a bottleneck (e.g. and HDD has ~150 IOPS, while an SSD can have 150,000). So

3条回答
  •  隐瞒了意图╮
    2021-02-20 06:05

    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.

    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
    

    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
    

    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.

    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
    

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

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

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

    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.

提交回复
热议问题