Sometimes we need to check the growth rate of a file, such as a log file, to get a feel for the load on the system, because in general, the faster the logs are written, the heavier the load on the system.
In this article, we will introduce several ways to view the growth rate of logs in Linux, as follows.
Using dd
The first thing to introduce is dd, as the dd command comes with almost all major distributions and requires no additional installation, as follows.
As above, use tail -F
to get the newly written data, then pipe the data to dd, which copies it to /dev/null
, where status=progress
is used to show the copy speed, as you can see, the write speed of our log is 471 kB/s
.
Using pv
The pv command can be thought of as cp with progress, as follows.
The principle is similar to dd, but the command is changed to pv.
Using the cv command
Since log data are written by programs (such as java), and in the following pseudo-file, which holds information about files opened by programs, as follows.
/proc/<pid>/fd
: holds the open file descriptor/proc/<pid>/fdinfo
: holds the offset to which the file descriptor is written
So reading this offset periodically will tell you how fast the file is being written, which is how the cv command is implemented, as follows.
|
|
The new version of the cv command has been renamed to progress. When you can’t install the cv package, you can try to install the progress package.
Write a small script
By writing a small script to periodically observe the file size and also view the file write speed, as follows.
A further look through the watch command also gives a rough idea of the speed, as follows.
|
|
Think of it this way
In fact, if we output the log in a function, and then filter out this log by grep, then we just count the number of lines of log output per second, is this not the QPS of the function execution!
Here the QPS can be calculated from 151/15
to be 10, and since it does not exceed 1000, it is seen as 0.0kB/s. It would be simpler to use the pv command as follows.