CNIT 17600 - Intro Computer Architecture
Memory Thrashing Lab
Note
The lab manual is written universally referring to the Raspberry Pi. You may be doing this lab on a physical Pi, or on a virtual machine running Raspberry Pi OS. Either way, the instructions remain the same, just change the verbiage for your context.
Activity Overview and Background - Watching R-Pi Virtual Memory
Virtual memory is a way to allow your secondary storage (like hard drive space) to be mapped for CPU use as primary memory (it appears to be RAM). Memory in RAM that is not be- ing used frequently is written to the hard disk, and this makes space for new instructions to be loaded directly into RAM. While we can joke that if your computer is slow you can go to www.downloadmoreram.com, in reality you can create more virtual swap space by using hard drive space as RAM.
There is a utility included in Linux distributions such as Raspbian called vmstat, which stands for ”virtual memory statistics.” This utility is used through the terminal window (also known as the command line) in order to display some data about the system’s memory.
A chunk of virtual memory is called a page. The area on your hard disk that is storing the instructions that were previously in RAM is referred to as a page file. The page frame is the physical chunk in RAM the virtual page is mapped to.
Something called a page fault can occur when the mapping of pages to frames is not correct. If the address needed for the current execution of a program is not in RAM, the CPU will have to swap out the memory that is in RAM out to hard disk to make room for the instructions of the current execution. If the CPU is so busy swapping pages out of RAM to hard disk that it cannot actually do computation, we have created a condition called thrashing.
Thrashing is described by Wikipedia (2017) as the following. “If a process does not have access to a sufficient number of memory pages, a futile, repetitive swapping condition known as ”thrashing” often arises, and the page fault rate typically becomes high. This frequently leads to high, runaway CPU utilization that can grind the system to a halt. In modern computers, thrashing may occur in the paging system (if there is not sufficient physical memory or the disk access time is overly long), or in the I/O communications subsystem (especially in conflicts over internal bus access),
etc.” (para 3).
Thrashing (typically) represents a large number of page faults, or failure to find the address needed. As a result of this excessive swapping without any computation, the OS looks at the CPU that is doing no computations, and assumes it is free, so will load yet another process into memory, and the nasty cycle continues.
<Question 1>
From the description on Thrashing above, describe in your own words the relationship between a page fault and thrashing and why thrashing is likely to occur on your Raspberry Pi given the right conditions.
Procedures
To find out how much memory the computer has, open the command line terminal and use the following command free -h
To find out the size of the page file that your operating system uses, use the command getconf PAGESIZE.
Give a screenshot showing the output of these two commands. Write a quick sentence to show how much swap space your Raspberry pi has available by default.
<Question 2>
The syntax for the vmstat utility’s use is as follows: vmstat [delay] [count]
With the interval and count parameters showing how often to refresh the content and the count giving a number of times to update the content. So vmstat 3 30 will poll the statistics every 3 seconds for a total of 30 times and then the utility will exit. For our purposes, we want to leave it running indefinitely, so we can specify a delay of 2 or 3 seconds and not specify a count. It defaults to infinity at that point.
For the purposes of our tests, run vmstat using the following command vmstat 2
Take a screenshot of the vmstat command after it has polled at least 5 times and include it in your report. This is your baseline operation (normal).
To stop vmstat from running, you can stop it by typing ctrl + C
<Question 3>
Type in the following command:
man vmstat
According to the man page, describe in your own words what each of the columns below mean:
1. r
2. b
3. swpd
4. si
5. so
To exit a man page, you can type the letter q for quit.
<Question 4>
Your goal is to get the Raspberry Pi to “thrash.” Since we want to be able to potentially repeat our results in a later trial, we need to be able to do our test systematically (not randomly). We want our results to be repeatable not only by ourselves, but described with enough detail that someone else could do the same thing we describe later under the same conditions and get the same results.
The general procedure is to use your Raspberry Pi in any manner that you believe will cause it to enter the thrashing state, and give a brief description as to how you caused it to enter that state. To answer this question, document the following kinds of things: What programs were you running, how many of them, how quickly (approximately) were you trying to open the programs, did you need to switch from one program to another or something else? For the sake of repeata- bility, record not only the kind of program and the number of programs but also the rate at which the programs are being started.
Find a method that you can repeat to consistently cause the Raspberry Pi to thrash. Note that you will probably have to hard reboot the Pi after a very successful thrashing. Also note that if the Pi is thrashing, you will not be able to take a screenshot on the Pi itself and may have to take a picture with your phone of the results.
Your evidence for succeeding in causing the machine to thrash will come from vmstat’s r, b, swpd, si, and so columns. When you increase the number of processes running on the system, notice when or if si or so are non-zero. If this happens, you have successfully caused the machine to thrash. It doesn’t matter if it is only a small number, any non-zero value indicates thrashing. Take a picture with your phone (or other camera - since your CPU wont be able to handle the processes to create a screenshot) to document the values (and other programs/processes on your screen) and include the *labeled* pic in your lab report.
The Raspberry Pi 4 can have a substantial amount more RAM than prior versions. If your Pi has more than 1GB of RAM, you may find thrashing to be a real challenge. Despite this, for question 4, document your efforts to thrash with the existing programs and processes and include a description of your attempts in your report as described above.
<Question 5>
It is expected that users with versions of the Pi prior to the 4 would have been successful with prior attempts. All Pis will thrash with this little script - regardless of RAM allocation. This script is a bit of fun playing with a classic denial of service attack from the Linux command line, but with a modification to cause thrashing before the CPU crashes. You will need to monitor the vmstat output in one terminal window, and in a second will run your choice of either top or htop (note, htop will need to be installed with sudo apt install htop). In a third terminal, you will run these commands to create a script file and to run the script.
cd ~
sudo nano fork.ps
In the file, type in the following lines: #!/usr/bin/bash
bar(){
sleep 2s
}
foo(){
foo|bar| foo&
}
foo;
Save the file (ctrl+x) (read prompts at the bottom of the screen). To run the script you will need to make the file executable:
sudo chmod 755 fork.ps
Then, you can use the “dot slash” syntax to run the script directly from the command line: ./fork.ps
This script defines two functions, foo() and bar(). The bar() function is just a sleep function, and the foo() function is a recursive function - that is a function that calls itself. It not only calls itself, but it calls bar, and then it calls itself again. A fork is a process which spawns a new child process. In this “fork bomb” the foo process creates a child process of itself and that child calls itself again, creating a new process that will also create a child process and on and on for infinity...or at least until processing power is consumed. The sleep is the trick to making the Pi thrash before crashing. The CPU will be overwhelmed by the rabbit processes breeding, but the sleep will give a chance for the Pi OS to try to swap out some processes into memory to swap in more processes into the CPU. FUN!
How long (in seconds, as well as in number of child processes spawned) does it take to thrash your Pi? Collect the appropriate information from the OS tools (vmstat and htop/top and system clock/stopwatch, etc.). Include a labeled image of the thrashing Pi’s screen showing the process list, vmstat output, and forkbomb process running. Also include your written description of the results in your lab.
***Troubleshooting tip*** if for some reason, your Pi crashes before thrashing, consider chang- ing the amount of time in the sleep function to 3 seconds and trying again. If that doesn’t work, increment again.
版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。