{"id":3215,"date":"2017-08-31T16:04:36","date_gmt":"2017-08-31T20:04:36","guid":{"rendered":"https:\/\/antipaucity.com\/?p=3215"},"modified":"2017-08-31T16:04:36","modified_gmt":"2017-08-31T20:04:36","slug":"fallocate-vs-dd-for-swap-file-creation","status":"publish","type":"post","link":"https:\/\/merikebi.warrenmyers.com\/?p=3215","title":{"rendered":"fallocate vs dd for swap file creation"},"content":{"rendered":"<p>I recently ran across this helpful <a href=\"https:\/\/www.digitalocean.com\/?refcode=d197a961987a\">Digital Ocean<\/a> community <a href=\"https:\/\/www.digitalocean.com\/community\/questions\/option-to-automatically-create-swap-space-when-creating-droplet?answer=22071\">answer<\/a> about creating a swap file at droplet creation time.<\/p>\n<p>So I decided to test how long using my <a href=\"https:\/\/antipaucity.com\/2014\/05\/08\/setting-up-etherpad-in-centos-6\/#.WahCm3d97go\">old method<\/a> (using <code>dd<\/code>) takes to run vs using <code>fallocate<\/code>.<\/p>\n<p>Here&#8217;s how long it takes to run <code>fallocate<\/code> on a fresh 40GB droplet:<\/p>\n<pre>\nroot@ubuntu:\/# rm swapfile && time fallocate -l 1G \/swapfile\nreal\t0m0.003s\nuser\t0m0.000s\nsys\t0m0.000s\n\nroot@ubuntu:\/# rm swapfile && time fallocate -l 2G \/swapfile\nreal\t0m0.004s\nuser\t0m0.000s\nsys\t0m0.000s\n\nroot@ubuntu:\/# rm swapfile && time fallocate -l 4G \/swapfile\nreal\t0m0.006s\nuser\t0m0.000s\nsys\t0m0.004s\n\nroot@ubuntu:\/# rm swapfile && time fallocate -l 8G \/swapfile\nreal\t0m0.007s\nuser\t0m0.000s\nsys\t0m0.004s\n\nroot@ubuntu:\/# rm swapfile && time fallocate -l 16G \/swapfile\nreal\t0m0.012s\nuser\t0m0.000s\nsys\t0m0.008s\n\nroot@ubuntu:\/# rm swapfile && time fallocate -l 32G \/swapfile\nreal\t0m0.029s\nuser\t0m0.000s\nsys\t0m0.020s\n<\/pre>\n<p>Interestingly, the relationship of <em>size<\/em> to <em>time<\/em> is non-linear when running <code>fallocate<\/code>.<\/p>\n<p>Compare to building a 4GB swap file with <code>dd<\/code> (on the same server, it turned out using either a 16KB or 4KB bs gives the fastest run time):<\/p>\n<pre>\ntime dd if=\/dev\/zero of=\/swapfile bs=16384 count=262144 \n\n262144+0 records in\n262144+0 records out\n4294967296 bytes (4.3 GB, 4.0 GiB) copied, 4.52602 s, 949 MB\/s\n\nreal\t0m4.528s\nuser\t0m0.048s\nsys\t0m4.072s\n<\/pre>\n<p>Yes, you read that correctly &#8211; using <code>dd<\/code> with an &#8220;optimum&#8221; bs of 16KB (after much testing different bs settings) takes ~<strong><em>1000x<\/em><\/strong> as long as using <code>fallocate<\/code> to create the same &#8220;size&#8221; file!<\/p>\n<p>How is <code>fallocate<\/code> so much faster? The details are in the <a href=\"http:\/\/man7.org\/linux\/man-pages\/man1\/fallocate.1.html\">man pages<\/a> for it <sup><sub>(emphasis added)<\/sub><\/sup>:<\/p>\n<blockquote><p>fallocate is used to manipulate the allocated disk space for a file, either to deallocate or preallocate it. For <strong>filesystems which support<\/strong> the <code>fallocate<\/code> system call, preallocation is done quickly by allocating blocks and marking them as uninitialized, <strong>requiring no IO to the data blocks<\/strong>. This is much faster than creating a file by filling it with zeroes.<\/p><\/blockquote>\n<p><code>dd<\/code> will &#8220;always&#8221; work. <code>fallocate<\/code> will work <em>almost<\/em>all of the time &#8230; but if you happen to be using a filesystem which <em>doesn&#8217;t<\/em> support it, you need to know how to use <code>dd<\/code>.<\/p>\n<p>But: if your filesystem supports <code>fallocate<\/code> (and it <em>probably<\/em> does), it is orders of magnitude more efficient to use it for file creation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently ran across this helpful Digital Ocean community answer about creating a swap file at droplet creation time. So I decided to test how long using my old method (using dd) takes to run vs using fallocate. Here&#8217;s how long it takes to run fallocate on a fresh 40GB droplet: root@ubuntu:\/# rm swapfile &#038;&#038; &hellip;<br \/><a href=\"https:\/\/merikebi.warrenmyers.com\/?p=3215\" class=\"more-link pen_button pen_element_default pen_icon_arrow_double\">Continue reading <span class=\"screen-reader-text\">fallocate vs dd for swap file creation<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[41],"tags":[414,450,559,725],"keyring_services":[],"class_list":["post-3215","post","type-post","status-publish","format-standard","hentry","category-technical","tag-io","tag-linux","tag-performance","tag-storage"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/3215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3215"}],"version-history":[{"count":0,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/3215\/revisions"}],"wp:attachment":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3215"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fkeyring_services&post=3215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}