# Differences

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

radu.mantu [03. [30p] Kernel Samepage Merging]
Line 1: Line 1:
-==== 03. [??p] Kernel Samepage Merging ====+==== 03. [30p] Kernel Samepage Merging ====

-=== [??p] Task A - Check for feature and 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
-  * **/​.../​max_page_sharing** : this is the maximum number of pages that can be de-duplicated;​ in cases like this it's better to go bigso set it to something like 1000000, just to be sure+  * **/​.../​max_page_sharing** : this is the maximum number of pages that can be de-duplicated;​ in cases like this it's better to go big or go home; so set it to something like 1000000, just to be sure

-There are a few more files in the ksm/ directory. We will still use one or two of those 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 35: Line 40:
* **/​.../​pages_shared** : this file reports how many //​physical//​ pages are in use at the moment   * **/​.../​pages_shared** : this file reports how many //​physical//​ pages are in use at the moment
* **/​.../​pages_sharing** : this file reports how many //virtual// page table entries point to the aforementioned physical pages   * **/​.../​pages_sharing** : this file reports how many //virtual// page table entries point to the aforementioned physical pages
-For this experiment we will also want to monitor the number of de-duplicated virtual pagesso have at it:+For this experiment we will also want to monitor the number of de-duplicated virtual pagesso have at it:

<code bash> <code bash>
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**:
+
+<​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 starts. As <​b>​ksmd</​b>​ begins scanning and merging pages, the free memory steadily increases. When the process eventually terminates, the amount of free memory reverts to its initial value.
+</​center></​html>​

-<​spoiler>​ +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.
-</​spoiler>​+

-Here, we can see the active memory suddenly rising when we start the processOver the next few secondsas **ksmd** starts scanning pages, the active ​memory ​slowly drops. Finallyas the process terminates, all memory is reclaimed by the kernel ​and the active memory returns to roughly the same value as before. If you'll 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 applicationwhere it reached max memory ​usage, the interval where KSM daemon was doing its job (in the 10s sleep interval) ​and where the process died.