how to use vmstat
作者:互联网
https://www.howtogeek.com/424334/how-to-use-the-vmstat-command-on-linux/
What Is Virtual Memory?
Your computer is fitted with a finite amount of physical memory called random access memory (RAM). This RAM needs to be managed by the kernel and shared between the operating system and whatever applications happen to be running. If these combined demands are asking for more memory than is physically installed in your computer, what can the kernel do?
Linux and Unix-like operating systems such as macOS can use space on your hard disk to help them manage memory demands. A reserved area of hard drive space called “swap space” can be used as though it were an extension of RAM. This is virtual memory.
The Linux kernel can write the contents of a block of memory into swap space, and free up that region of RAM for use by another process. The swapped out—also called “paged” out—memory can be retrieved from the swap space and restored to RAM when it is required.
Of course, the speed of access for paged out memory is slower than that of memory held in RAM. And that’s not the only trade-off. Whilst virtual memory does provide a way for Linux to manage its memory demands, using virtual memory places increased burdens elsewhere on the computer.
Your hard drive must perform more reads and writes. The kernel—and hence, the CPU—must do more work as it swaps memory out, swaps memory in, and keeps all the plates spinning to satisfy the memory needs of the different processes.
Linux provides a way for you to monitor all of this activity in the shape of the vmstat command, which reports on virtual memory statistics.
The vmstat Command
If you type vmstat as a command with no parameters, it will show you a set of values. These values are the averages for each of the statistics since your computer was last rebooted. These figures are not a snapshot of the values “right now.”
vmstat
A short table of values is displayed.
ADVERTISEMENT
There are columns headed Procs, Memory, Swap, IO, System, and CPU. The final column (most right-hand column) contains the data relating to the CPU.
Here is a list of the data items in each column.
THE BEST TECH NEWSLETTER ANYWHERE
Join 425,000 subscribers and get a daily digest of features, articles, news, and trivia.
e-mail address
Sign Me Up!
By submitting your email, you agree to the Terms of Use and Privacy Policy.
Proc
r: The number of runnable processes. These are processes that have been launched and are either running or are waiting for their next time-sliced burst of CPU cycles.
b: The number of processes in uninterruptible sleep. The process isn’t sleeping, it is performing a blocking system call, and it cannot be interrupted until it has completed its current action. Typically the process is a device driver waiting for some resource to come free. Any queued interrupts for that process are handled when the process resumes its usual activity.
Memory
swpd: the amount of virtual memory used. In other words, how much memory has been swapped out.,
free: the amount of idle (currently unused) memory.
buff: the amount of memory used as buffers.
cache: the amount of memory used as cache.
Swap
si: Amount of virtual memory swapped in from swap space.
so: Amount of virtual memory swapped out to swap space.
IO
bi: Blocks received from a block device. The number of data blocks used to swap virtual memory back into RAM.
bo: Blocks sent to a block device. The number of data blocks used to swap virtual memory out of RAM and into swap space.
System
in: The number of interrupts per second, including the clock.
cs: The number of context switches per second. A context switch is when the kernel swaps from system mode processing into user mode processing.
CPU
These values are all percentages of the total CPU time.
us: Time spent running non-kernel code. That is, how much time is spent in user time processing and in nice time processing.
sy: Time spent running kernel code.
id: Time spent idle.
wa: Time spent waiting for input or output.
st: Time stolen from a virtual machine. This is the time a virtual machine has to wait for the hypervisor to finish servicing other virtual machines before it can come back and attend to this virtual machine.
Using a Time Interval
We can have vmstat provide regular updates to these figures by using a delay value. The delay value is provided in seconds. To have the statistics updated every five seconds, we’d use the following command:
vmstat 5
ADVERTISEMENT
Every five seconds vmstat will add another line of data to the table. You’ll need to hit Ctrl+C to stop this.
Using a Count Value
Using too low a delay value will put additional strain on your system. If you need to have rapid updates to try to diagnose a problem, it is recommended that you use a count value as well as a delay value.
The count value tells vmstat how many updates to perform before it exits and returns you to the command prompt. If you do not provide a count value, vmstat will run until it is stopped by Ctrl+C.
ADVERTISEMENT
To have vmstat provide an update every five seconds—but only for four updates—use the following command:
vmstat 5 4
After four updates vmstat stops of its own accord.
Changing the Units
You can choose to have the memory and swap statistics displayed in kilobytes or megabytes using the -S (unit-character) option. This must be followed by one of k , K , m, or M. These represent:
k:1000 bytes
K: 1024 bytes
m: 1000000 bytes
M: 1048576 bytes
To have the statistics updated every 10 seconds with the memory and swap statistics displayed in megabytes, use the following command:
vmstat 10 -S M
The memory and swap statistics are now shown in megabytes. Note that the -S option does not affect the IO block statistics. These are always displayed in blocks.
Active and Inactive Memory
If you use the -a (active) option the buff and cache memory columns are replaced by the “inact” and “active” columns. As they would suggest, these show the amount of inactive and active memory.
ADVERTISEMENT
To see these two columns instead of the buff and cache columns, include the -a option, as shown:
vmstat 5 -a -S M
The inact and active columns are affected by the -S (unit-character) option.
Forks
The -f switch displays the number of forks that have happened since the computer was booted up.
ADVERTISEMENT
In other words, this shows the number of tasks that have been launched (and, for the bulk of them, closed again) since the system was booted. Every process launched from the command line would increase this figure. Each time a task or process spawns or clones a new task, this figure will increase.
vmstat -f
The forks display does not update.
Displaying Slabinfo
The kernel has its own memory management to worry about as well as the memory management for the operating system and all of the applications.
As you might imagine the kernel is allocating and deallocating memory over and over for the many different types of data object that it must handle. To make this as efficient as possible, it uses a system called slabs. This is a form of caching.
Memory allocated, used, and no longer required for a specific type of kernel data object can be re-used for another data object of the same type without the memory being deallocated and reallocated. Think of slabs as pre-allocated, made to measure, segments of RAM for the kernel’s own needs.
ADVERTISEMENT
To see the statistics for the slabs, use the -m (slabs) option. You will need to use sudo, and you will be prompted for your password. As the output can be quite lengthy, we are piping it through less.
sudo vmstat -m | less
The output has five columns. These are:
Cache: Name of the cache.
num: The number of currently active objects in this cache.
total: The total number of available objects in this cache.
size: The size of each object in the cache.
pages: The total number of memory pages that have (at least) one object currently associated with this cache.
Press q to leave less.
Displaying Event Counters and Memory Statistics
To display a page of event counters and memory statistics, use the -s (stats) option. Note that’s a lowercase “s.”
vmstat -s
Although the statistics that are reported are largely the same as the information that makes up the default vmstat output, some of them are split out in more detail.
For example, the default output combines both the nice and the non-nice user CPU time into the “us” column. The -s (stats) display lists these statistics separately.
output from vmstat -s in aterminal window
Displaying Disk Statistics
You can obtain a similar listing of disk statistics using the -d (disk) option.
vmstat -d | less
ADVERTISEMENT
For each disk, three columns are displayed, these are Reads, Writes, and IO.
IO is the rightmost column. Note that the sec column in IO is measured in seconds but the time-based statistics in the read and write columns are measured in milliseconds.
This is what the columns mean:
Reads
total: The total count of disk reads.
merged: The total count of grouped reads.
sectors: The total count of sectors that have been read in.
ms: Total count of time in milliseconds that were used reading data from the disk.
writes
total: The total count of disk writes.
merged: The total count of grouped writes.
sectors: The total count of sectors written to.
ms = Total count of time in milliseconds that were used writing data to the disk.
IO
cur: Number of current disk reads or writes.
sec: Time spent in seconds for any in-progress reads or writes.
Displaying Summary Disk Statistics
To see a quick display of summary statistics for your disk activity, use the -D (disk-sum) option. Note the uppercase “D.”
vmstat -D
ADVERTISEMENT
The number of disks might look abnormally high. The computer used to research this article is running Ubuntu. With Ubuntu, each time you install an application from a Snap, a squashfs pseudo-filesystem is created which is attached to a /dev/loop device.
Annoyingly these device entries are counted as hard drive devices by many of the Linux commands and utilities.
Displaying Partition Statistics
To see statistics related to a specific partition, use the -p (partition) option and provide the partition identifier as a command line parameter.
Here we are going to look at the partition sda1. The digit one indicates this is the first partition on device sda, which is the main hard drive for this computer.
vmstat -p sda1
The information returned shows the total count of disk reads and disk writes to and from that partition, and the number of sectors included in disk read and disk write actions.
A Peek Under The Hood
It’s always good to know how to lift the hood and see what’s going on underneath. Sometimes you’ll be trying to problem solve, sometimes it’ll be out of interest because you want to know how your computer ticks.
ADVERTISEMENT
vmstat can provide you with a ton of useful information. Now you know how to access it and what it means. And forewarned is forearmed—when you do need to roll your sleeves up and do some diagnostics, you’ll know you’ve got vmstat on your side.
READ NEXT
› How to Change the Watch Face on a Samsung Galaxy Watch
› How to Embed Files and Calendar Events in Google Docs
› How to Turn Off the Xbox Series X|S
› How to Leave a Google Review
› What Does “RGB” Mean, and Why Is It All Over Tech?
Dave McKayDAVE MCKAY
Dave McKay first used computers when punched paper tape was in vogue, and he has been programming ever since. After over 30 years in the IT industry, he is now a full-time technology journalist. During his career, he has worked as a freelance programmer, manager of an international software development team, an IT services project manager, and, most recently, as a Data Protection Officer. Dave is a Linux evangelist and open source advocate. READ FULL BIO »
JOIN GEEK TALK ON FACEBOOK
The above article may contain affiliate links, which help support How-To Geek.
How-To Geek is where you turn when you want experts to explain technology. Since we launched in 2006, our articles have been read more than 1 billion times. Want to know more?
How-To Geek
Facebook
Instagram
Twitter
LinkedIn
The Best Free Tech Newsletter Anywhere
Enter Your Email
By submitting your email, you agree to the Terms of Use and Privacy Policy.
About Us
Contact Us
Join Our Team
Advertising
Privacy Policy
Terms of Use
Accessibility
Do not sell my info
Toggle Dark Mode
© 2021 LifeSavvy Media. All Rights Reserved
标签:use,statistics,how,vmstat,memory,total,disk 来源: https://blog.csdn.net/weixin_53000630/article/details/118680571