Differences

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

Link to this comparison view

ep:labs:02:contents:tasks:ex3 [2020/08/11 15:32]
radu.mantu
ep:labs:02:contents:tasks:ex3 [2022/09/13 11:58] (current)
radu.mantu [03. [30p] Kernel Samepage Merging]
Line 1: Line 1:
-==== 03. [??p] Kernel Samepage Merging ====+==== 03. [30p] Kernel Samepage Merging ====
  
 [[https://​www.kernel.org/​doc/​html/​latest/​admin-guide/​mm/​ksm.html|KSM]] is a page de-duplication strategy introduced in kernel version 2.6.32. In case you are wondering, it's not the same thing as the file page cache. KSM was originally developed in tandem with KVM in order to detect data pages with //exactly// the same content and make their page table entries point to the same physical address (marked Copy-On-Write.) The end goal was to allow more VMs to run on the same host. Since each page must be scanned for identical content, this solution had no chance of scaling well with the available quantity of RAM. So, the developers compromised to scan only with the private anonymous pages that were marked as likely candidates via ''​madvise(addr,​ length, MADV_MERGEABLE)''​. [[https://​www.kernel.org/​doc/​html/​latest/​admin-guide/​mm/​ksm.html|KSM]] is a page de-duplication strategy introduced in kernel version 2.6.32. In case you are wondering, it's not the same thing as the file page cache. KSM was originally developed in tandem with KVM in order to detect data pages with //exactly// the same content and make their page table entries point to the same physical address (marked Copy-On-Write.) The end goal was to allow more VMs to run on the same host. Since each page must be scanned for identical content, this solution had no chance of scaling well with the available quantity of RAM. So, the developers compromised to scan only with the private anonymous pages that were marked as likely candidates via ''​madvise(addr,​ length, MADV_MERGEABLE)''​.
Line 5: Line 5:
 Download the {{:​ep:​labs:​02:​contents:​tasks:​ksm.zip|skeleton}} for this task. Download the {{:​ep:​labs:​02:​contents:​tasks:​ksm.zip|skeleton}} for this task.
  
-=== [??p] Task A - Check kernel support & enable ksmd ===+=== [15p] Task A - Check kernel support & enable ksmd ===
  
-First things first, you need to verify that KSM was enabled during your kernel'​s compilation. For this, you need to check the Linux make config ​build file that is stored on your /boot partition. Hopefully, you should see something like this:+First things first, you need to verify that KSM was enabled during your kernel'​s compilation. For this, you need to check the Linux build configuration ​file. Hopefully, you should see something like this:
  
 <code bash> <code bash>
 +# on Ubuntu you can usually find it in your /boot partition
 $ grep CONFIG_KSM /​boot/​config-$(uname -r) $ grep CONFIG_KSM /​boot/​config-$(uname -r)
 +CONFIG_KSM=y
 +
 +# otherwise, you can find a gzip compressed copy in /proc
 +$ zcat /​proc/​config.gz | grep CONFIG_KSM
 CONFIG_KSM=y CONFIG_KSM=y
 </​code>​ </​code>​
Line 17: Line 22:
  
 Moving forward. Next thing on the list is to check that the **ksmd** daemon is functioning. Any configuration that we'll do will be through the sysfs files in ''/​sys/​kernel/​mm/​ksm''​. Consequently,​ you should change user to root (even ''​sudo''​ should not allow you to write to these files.) Moving forward. Next thing on the list is to check that the **ksmd** daemon is functioning. Any configuration that we'll do will be through the sysfs files in ''/​sys/​kernel/​mm/​ksm''​. Consequently,​ you should change user to root (even ''​sudo''​ should not allow you to write to these files.)
-  * **/.../read** : this is **1** if the daemon is active; write 1 to it if it's not+  * **/.../run** : this is **1** if the daemon is active; write 1 to it if it's not
   * **/​.../​pages_to_scan** : this is how many pages will be scanned before going to sleep; you can increase this to 1000 if you want to see faster results   * **/​.../​pages_to_scan** : this is how many pages will be scanned before going to sleep; you can increase this to 1000 if you want to see faster results
   * **/​.../​sleep_millisecs** : this is how many ms the daemon sleeps in between scans; since you've modified **pages_to_scan**,​ you can leave this be   * **/​.../​sleep_millisecs** : this is how many ms the daemon sleeps in between scans; since you've modified **pages_to_scan**,​ you can leave this be
Line 24: Line 29:
 There are a few more files in the ksm/ directory. We will still use one or two later on. But for now, configuring the previous ones should be enough. Google the rest if you're interested. There are a few more files in the ksm/ directory. We will still use one or two later on. But for now, configuring the previous ones should be enough. Google the rest if you're interested.
  
-=== [??p] Task B - Watch the magic happen ===+=== [15p] Task B - Watch the magic happen ===
  
 For this step it would be better to have a few terminals open. First, let's start a ''​vmstat''​. Keep your eyes on the active memory column when we run the sample program. For this step it would be better to have a few terminals open. First, let's start a ''​vmstat''​. Keep your eyes on the active memory column when we run the sample program.
Line 43: Line 48:
 Finally, look at the provided code, compile it, and launch the program. As an argument you will need to provide the number of pages that will be allocated and initialized with the same value. Note that not all pages will be de-duplicated instantly. So keep in mind your system'​s RAM limitations before deciding how much you can spare (1-2GB should be ok, right?) Finally, look at the provided code, compile it, and launch the program. As an argument you will need to provide the number of pages that will be allocated and initialized with the same value. Note that not all pages will be de-duplicated instantly. So keep in mind your system'​s RAM limitations before deciding how much you can spare (1-2GB should be ok, right?)
  
-The result should look something like this:+The result should look something like **Figure 1**:
  
-<​spoiler>​ 
 {{:​ep:​labs:​02:​contents:​tasks:​ksm_vmstat.png?​700|}} {{:​ep:​labs:​02:​contents:​tasks:​ksm_vmstat.png?​700|}}
-</spoiler+<html><​center
- +<​b>​Figure 1:</​b>​ <​b>​vmstat</​b>​ output during ​the execution of our sample program (unit of measure: MB). The free memory ​steadily decreases from a baseline value of ~4.5GB to a minimum of ~2.5GB after the process ​startsAs <b>ksmd</b> begins ​scanning ​and merging ​pages, the free memory ​steadily increasesWhen the process ​eventually ​terminates, the amount of free memory ​reverts ​to its initial ​value. 
-Here, we can see the active ​memory ​suddenly rising when we start the process. ​Over the next few seconds, as **ksmd** starts ​scanning pages, the active ​memory ​slowly dropsFinally, as the process terminates, ​all memory is reclaimed by the kernel and the active ​memory ​returns ​to roughly the same value as before.+</​center></​html>​
  
 If you ever want to make use of this in your own experiments,​ remember to adjust the configurations of **ksmd**. Waking too often or scanning to many pages at once could end up doing more harm than good. See what works for your particular system. If you ever want to make use of this in your own experiments,​ remember to adjust the configurations of **ksmd**. Waking too often or scanning to many pages at once could end up doing more harm than good. See what works for your particular system.
 +
 +Include a screenshot with the same output as the one in the spoiler above. \\
 +Edit the screenshot or note in writing at what point you started the application,​ where it reached max memory usage, the interval where KSM daemon was doing its job (in the 10s sleep interval) and where the process died.
ep/labs/02/contents/tasks/ex3.1597149155.txt.gz · Last modified: 2020/08/11 15:32 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