How to measure IOPS for a command in linux?

后端 未结 3 1660
盖世英雄少女心
盖世英雄少女心 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 05:45

    The iotop command collects I/O usage information about processes on Linux. By default, it is an interactive command but you can run it in batch mode with -b / --batch. Also, you can a list of processes with -p / --pid. Thus, you can monitor the activity of a git command with:

    $ sudo iotop -p $(pidof git) -b
    

    You can change the delay with -d / --delay.

    0 讨论(0)
  • 2021-02-20 05:47

    You can use pidstat:
    pidstat -d 2
    More specifically pidstat -d 2 | grep COMMAND or pidstat -C COMMANDNAME -d 2

    The pidstat command is used for monitoring individual tasks currently being managed by the Linux kernel. It writes to standard output activities for every task selected with option -p or for every task managed by the Linux kernel if option -p ALL has been used. Not selecting any tasks is equivalent to specifying -p ALL but only active tasks (tasks with non-zero statistics values) will appear in the report. The pidstat command can also be used for monitoring the child processes of selected tasks.

    -C commDisplay only tasks whose command name includes the stringcomm. This string can be a regular expression.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题