Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ep:labs:04:contents:tasks:ex2 [2021/09/27 13:36]
radu.mantu [02. [20p] Traffic monitoring - Tcpdump]
ep:labs:04:contents:tasks:ex2 [2025/02/11 23:36] (current)
cezar.craciunoiu
Line 1: Line 1:
-==== 02. [??piperf3+==== 02. [20pSwap space ====
  
-TODO+<note warning>​ 
 +Before starting this task, call the assistant to show him your progress. If you manage to freeze your PC, it might prove tricky to do so afterwards. 
 +</​note>​
  
 +=== [10p] Task A - Swap File ===
 +
 +First, let us check what swap devices we have enabled. Check the //NAME// and //SIZE// columns of the following command:
 +<​code>​
 +$ swapon --show
 +</​code>​
 +No output means that there are no swap devices available.
 +
 +If you ever installed a Linux distro, you may remember creating a separate //swap partition//​. This, however, is only one method of creating swap space. The other is by adding a //swap file//. Run the following commands:
 +<​code>​
 +$ sudo swapoff -a
 +$ sudo dd if=/​dev/​zero of=/​swapfile bs=1024 count=$((4 * 1024 * 1024))
 +$ sudo chmod 600 /swapfile
 +$ sudo mkswap /swapfile
 +$ sudo swapon /swapfile
 +
 +$ swapon --show
 +</​code>​
 +
 +Just to clarify what we did:
 +  * disabled all swap devices
 +  * created a 4Gb zero-initialized file
 +  * set the permission to the file so only //root// can edit it
 +  * created a swap area from the file using **mkswap** (works on devices too)
 +  * activated the swap area
 +
 +The new swap area is temporary and will not survive a reboot. To make it permanent, we need to register it in [[https://​en.wikipedia.org/​wiki/​Fstab|/​etc/​fstab]] by adding a line such as this:
 +
 +<​code>​
 +/swapfile swap swap defaults 0 0
 +</​code>​
 +
 +=== [10p] Task B - Does it work? ===
 +
 +In one terminal run **vmstat** and look at the //swpd// and //free// columns.
 +<​code>​
 +$ vmstat -w 1
 +</​code>​
 +
 +In another terminal, open a python shell and allocate a bit more memory than the available RAM. Identify the moment when the newly created swap space is being used.
 +
 +One thing you might notice is that the value in **vmstat**'​s //free// column is lower than before. This does not mean that you have less available RAM after creating the swap file. Remember using the **dd** command to create a 4GB file? A big chunk of RAM was used to buffer the data that was written to disk. If //free// drops to unacceptable levels, the kernel will make sure to reclaim some of this buffer/​cache memory. To get a clear view of how much available memory you actually have, try running the following command:
 +
 +<code bash>
 +$ free -h
 +</​code>​
 +
 +Observe that once you close the python shell and the memory is freed, //swpd// still displays a non-zero value. Why? There simply isn't a reason to clear the data from the swap area. If you really want to clean up the used swap space, try the following:
 +<​code>​
 +$ vmstat
 +$ sudo swapoff -a && sudo swapon -a
 +$ vmstat
 +</​code>​
 +
 +<​solution -hidden>
 +Output here:
 +
 +{{ :​ep:​labs:​ep2017_l2_ex05.png?​550 |}}
 +
 +Free memory goes down, swap usage goes up.
 +</​solution>​
 +
 +Create two swap files. Set their priorities to 10 and 20, respectively. \\
 +Include the commands (copy+paste) or a screenshot of the terminal. \\
 +Also add 2 advantages and disadvantages when using a //swap file// comparing with a //swap partition//​.
ep/labs/04/contents/tasks/ex2.1632738999.txt.gz · Last modified: 2021/09/27 13:36 by radu.mantu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0