xref: /haiku/docs/develop/kernel/vm/swap_file_support.rst (revision 3d4afef9cba2f328e238089d4609d00d4b1524f3)
1Swap file
2#######################
3
4This section describes how to use swap file in Haiku and how the swap system
5works.
6
7How to use a swap file?
8=======================
9
10Like BeOS, Haiku uses "/var/swap" as default swap file. It is created
11during the boot process and its size is twice the size of physical memory by
12default. You can change its size through the VirtualMemory preference
13application and your settings will take effect after restarting the system.
14
15The default swap file "/var/swap" may not satisfy your need. Haiku allows
16adding/removing a swap file dynamically. (This is *NOT* implemented yet, since
17I do not know how to add bin commands "swapon" and "swapoff" in the system.
18It needs to be done in the future.)
19
20How swap system works?
21======================
22
23The virtual memory subsystem of Haiku is very similar to that of FreeBSD,
24therefore our swap system implementation is borrowed from FreeBSD.
25
26A swap system has two main functions: (1) maintain a map between anonymous
27pages and swap space, so we can page in/out when needed. (2) manage the
28allocation/deallocation of swap space. Let's see how these are implemented in
29Haiku.
30
31In order to maintain a map between pages and swap space, we need to record
32the pages' swap address somewhere. Here we use swap blocks. A "swap_block"
33structure contains swap address information for 32 (value of SWAP_BLOCK_PAGES)
34consecutive pages from a same cache. So whenever we look for a page in swap
35files, we should get the swap block for it. But how to get the swap block?
36Here we use hash table. All swap blocks in the system are arranged into a global
37hash table. The hash table uses a cache's address and page index in this cache
38as hash key.
39
40Here is an example. Suppose a page has been paged out to swap space and now
41its cache wants to page it in. It works as follows: look up the swap hash table
42using address of the cache and page index as hash key, if successful, we get
43the swap block containing the this page's swap address. Then search the swap
44block to get the exact swap address of this page. After that, we can read the
45page from swap file using vfs functions.
46
47I draw a picture and hope it could help you understand the above words.
48
49.. code-block:: text
50
51                    ___________________________________________________________
52    sSwapHashTable  |__________|___NULL___|___NULL___|___________|____NULL____|
53                         |                                 |
54                         |                                 |
55                      ___V___                           ___V___
56    swap_block   /----|__0__|                  /--------|__5__|
57                 |    |__3__|--------\         |    /---|__6__|
58                 |    |_..._|        |         |    |   |_..._|
59                 |    |__2__|----\   |         |    |   |__20_|--------------->
60                 |               |   |         |    |
61                 |  _____________V___V_________V____V_________________________
62    swap_file    `->|slot|slot|slot|slot|slot|slot|slot|slot|slot|slot|....|
63                    |_0__|_1__|_2__|_3__|_4__|_5__|_6__|_7__|_8__|_9__|____|__
64
65
66The swap system also manages allocation/deallocation of swap space.	In our
67implementation, each swap file is divided into page-sized slots(called "swap
68pages") and a swap file can be seen as an array of many swap pages(see the
69above picture). Swap page is the unit for swap space allocation/deallocation
70and we use swap page index (slot index) as swap space address instead of offset.
71All the swap pages in the system are given a unified address and we leave one
72page gap between two swap files. (e.g. there are 3 swap files in the system,
73each has 100 swap pages, the address range(to be exact, page index) for each
74swap file is: 0-99, 101-200, 202-301) Why leave a page gap between swap files?
75Because in this way, we can easily tell if two adjacent pages are in a same
76swap file. (See the code in VMAnonymousCache::Read()).
77
78The efficiency of the FreeBSD swap system lies in a special data structure:
79radix bitmap(i.e. bitmap using radix tree for hinting.) It can operate well no
80matter how much fragmentation there is and no matter how large a bitmap is
81used. I have ported the radix bitmap structure to Haiku, so our swap system
82will have a good performance. More information on radix bitmap, please look
83at the source code.
84
85Swap space allocation takes place when we swap anonymous pages out.
86In order to make the allocation less probable to fail, anonymous cache will
87reserve swap space when it is initialized. If there is not enough swap space
88left, physical memory will be reserved. Swap space deallocation happens when
89available swap space is low. The page daemon will scan a number of pages and
90if the scanned page has swap space assigned, its swap space will be freed.
91
92Acknowledgement
93---------------
94
95Special thanks to my mentor Ingo. He is a knowledged person and always
96gives me encouragement. Without his consistent and illuminating instructions,
97this project would not have reached its present status.
98
99If you find bugs or have suggestions for swap system, you can contact me
100via upczhsh@163.com. Thanks in advance.
101
102Zhao Shuai - upczhsh@163.com - 2008-08-21
103