This shows you the differences between two versions of the page.
ep:labs:03:contents:tasks:ex1 [2020/07/30 20:49] gheorghe.petre2608 [04. [20p] Monitor I/O with vmstat and iostat] |
ep:labs:03:contents:tasks:ex1 [2025/02/11 23:27] (current) cezar.craciunoiu |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== 04. [20p] Monitor I/O with vmstat and iostat ==== | + | ==== 01. [30p] Vmstat ==== |
- | We said in the beginning that the disk I/O subsystems are the slowest part of any system. This is why the I/O monitoring is so important, maximizing the performance of the slowest part of a system resulting in an improvement of the performance of the entire system. | + | The **vmstat** utility provides a good low-overhead view of system performance. Since **vmstat** is such a low-overhead tool, it is practical to have it running even on heavily loaded servers when it is needed to monitor the system’s health. |
- | === [10p] Task A - Script === | + | === [10p] Task A - Monitoring stress === |
+ | Run **vmstat** on your machine with a 1 second delay between updates. Notice the CPU utilisation (info about the output columns [[https://medium.com/@damianmyerscough/vmstat-explained-83b3e87493b3|here]]). | ||
- | Write a script that reads the data into memory and generates a text file 500 times larger, by concatenating the contents of the following novel {{:ep:labs:olivertwist.txt|olivertwist.txt}} to itself. | + | In another terminal, use the **stress** command to start N CPU workers, where N is the number of cores on your system. |
+ | Do not pass the number directly. Instead, use command substitution. | ||
+ | |||
+ | Note: if you are trying to solve the lab on //fep// and you don't have **stress** installed, try cloning and compiling [[https://github.com/ColinIanKing/stress-ng|stress-ng]]. | ||
<solution -hidden> | <solution -hidden> | ||
- | <code> | + | **Task A:**\\ |
- | if __name__ == '__main__': | + | <code bash> |
- | text_file1 = open("OliverTwist.txt", "r") | + | $ vmstat -w -n 1 |
- | text_file2 = open("OliverTwistLarge.txt", "w+") | + | $ stress -c $(nproc) |
- | lines_file1 = text_file1.readlines() | + | |
- | for x in range(0, 500): | + | |
- | text_file2.writelines(lines_file1) | + | |
</code> | </code> | ||
</solution> | </solution> | ||
- | === [10p] Task B - Monitoring behaviour === | + | === [10p] Task B - How does it work? === |
+ | Let us look at how **vmstat** works under the hood. We can assume that all these statistics (memory, swap, etc.) can not be normally gathered in userspace. So how does **vmstat** get these values from the kernel? Or rather, how does any process interact with the kernel? Most obvious answer: //**system calls**//. | ||
- | Now we want to analyze what is happening with the I/O subsystem during an expensive operation. Monitor the behavior of the system while running your script using **vmstat** and **iostat**. | + | <code bash> |
+ | $ strace vmstat | ||
+ | </code> | ||
- | <note tip> | + | //"All well and good. But what am I looking at?"// |
- | Understanding vmstat IO section: | + | |
- | * **bi** - column reports the number of blocks received (or “blocks in”) from a disk per second. | + | |
- | * **bi** - column reports the number of blocks sent (“blocks out”) to a disk per second. | + | What you //should// be looking at are the system calls after the two **write**s that display the output header (hint: it has to do with **/proc/** file system). So, what are these files that **vmstat** opens? |
- | </note> | + | |
+ | <code bash> | ||
+ | $ file /proc/meminfo | ||
+ | $ cat /proc/meminfo | ||
+ | $ man 5 proc | ||
+ | </code> | ||
+ | |||
+ | The manual should contain enough information about what these kernel interfaces can provide. However, if you are interested in //how// the kernel generates the statistics in **/proc/meminfo** (for example), a good place to start would be [[https://elixir.bootlin.com/linux/v4.15/source/fs/proc/meminfo.c|meminfo.c]] (but first, [[https://ocw.cs.pub.ro/courses/so2|SO2 wiki]]). | ||
+ | |||
+ | === [10p] Task C - USO flashbacks (1) === | ||
+ | |||
+ | Write a one-liner that uses **vmstat** to report complete **disk statistics** and sort the output in **descending** order based on **total reads** column. | ||
+ | |||
+ | <note tip> | ||
+ | You can eliminate the first two header lines from the **vmstat** output using ''tail -n +3''. | ||
+ | </note> | ||
+ | |||
+ | <solution -hidden> | ||
+ | <code bash> | ||
+ | $ vmstat -wdn | tail -n +3 | sort -nrk 2 | ||
+ | </code> | ||
+ | </solution> |