{"id":1110,"date":"2021-09-07T01:14:31","date_gmt":"2021-09-07T01:14:31","guid":{"rendered":"http:\/\/www.vidarholen.net\/contents\/blog\/?p=1110"},"modified":"2021-09-07T01:17:07","modified_gmt":"2021-09-07T01:17:07","slug":"swap-on-hdd-does-placement-matter-tldr-yes","status":"publish","type":"post","link":"https:\/\/www.vidarholen.net\/contents\/blog\/?p=1110","title":{"rendered":"Swap on HDD: Does placement matter? (tl;dr: Yes)"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p>Someone was partitioning an ancient laptop&#8217;s spinning rust, and asked where they should put their swap. At the start of the drive? At the end? As a swap file? Does it even matter?<\/p>\n<p>While I&#8217;m on my 4th generation SSD at this point, I was interested enough to benchmark and find out. I would have used a swap file thinking it wouldn&#8217;t matter much, but boy was I wrong!<\/p>\n<h3>Test setup<\/h3>\n<p>My only remaining functional HDD &#8212; after putting two others in the electronics recycling bin &#8212; was a Western Digital 2.0TB WD20EARX SATA drive with 64MB cache.<\/p>\n<p>I installed Ubuntu 21.04 Server on it, and partitioned it with 1G boot, 4G swap, 1.8T ext4 root, and another 4G swap. For easy testing, I did it all through <code>kvm<\/code> using the raw device without cache:<\/p>\n<pre><code>kvm -m 2048 -drive file=\/dev\/sda,format=raw,cache=none\n<\/code><\/pre>\n<p>I verified that the host did indeed not use its rather more generous RAM to cache the device.<\/p>\n<p>The standard test, building Linux, did not swap as much as I hoped. With enough RAM to successfully boot, it didn&#8217;t really touch swap for as long as I bothered to watch it. I instead turned to my favorite RAM hog: building <a href=\"https:\/\/www.shellcheck.net\">ShellCheck<\/a> with the GHC Haskell compiler. This is a high RAM, low disk process.<\/p>\n<p>I tried once with plenty of RAM, then each of the following with 2GB RAM and 4GB swap:<\/p>\n<ul>\n<li>Swap at the start of the drive<\/li>\n<li>Swap at the end of the drive<\/li>\n<li>Swap in a file created with <code>dd if=\/dev\/zero of=4g bs=1M count=4096<\/code> (which <code>hdparm --fibmap<\/code> revealed to be allocated across four contiguous areas in the first 14GB of the device)<\/li>\n<\/ul>\n<p>For fun, I also included:<\/p>\n<ul>\n<li>Swap on an old OCZ Vertex 4 SATA 256GB SSD<\/li>\n<li>Swap on my current Samsung 970 Evo Plus 1TB SSD<\/li>\n<\/ul>\n<p>Each test was preceded by a <code>git clean -fdx<\/code>, swapoff\/swapon, dropping caches, and finally a <code>time cabal v1-build shellcheck<\/code>.<\/p>\n<p>I ran each test twice and picked the lowest number, though the variance was not large. For the swap file, I generated a second one to see if it was the luck of the allocator, but results were similar. <code>dmesg<\/code> showed no read errors after the tests finished.<\/p>\n<h3>Results<\/h3>\n<p><img decoding=\"async\" src=\"\/imgs\/blog\/shellcheckswap.png\" alt=\"Bar chart of numbers below\"><\/p>\n<p>Or in numbers:<\/p>\n<ul>\n<li>165s (2:45) &#8212; RAM only<\/li>\n<li>451s (7:31) &#8212; NVMe SSD<\/li>\n<li>771s (12:51) &#8212; SATA SSD<\/li>\n<li>1701s (28:21) &#8212; Start of HDD<\/li>\n<li>2600s (43:20) &#8212; Swap file on HDD<\/li>\n<li>3161s  (52:41) &#8212; End of HDD<\/li>\n<\/ul>\n<p>In this test, using a swap file was surprisingly 50%+ slower than simply allocating a swap partition at the start of the drive, in spite of the low fragmentation and Linux&#8217;s bypass of the FS layer.<\/p>\n<p>Drives and workloads obviously vary, but if nothing else we can safely say that yes, placement matters.<\/p>\n<\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[6,4],"tags":[53],"class_list":["post-1110","post","type-post","status-publish","format-standard","hentry","category-basic-linux","category-linux","tag-linux"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1110"}],"version-history":[{"count":12,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1110\/revisions"}],"predecessor-version":[{"id":1123,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1110\/revisions\/1123"}],"wp:attachment":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}