{"id":87,"date":"2017-09-20T00:50:23","date_gmt":"2017-09-20T00:50:23","guid":{"rendered":"http:\/\/imperfectcode.com\/?p=87"},"modified":"2017-09-20T00:50:23","modified_gmt":"2017-09-20T00:50:23","slug":"systemd-resource-limiting-for-mortals","status":"publish","type":"post","link":"https:\/\/imperfectcode.com\/?p=87","title":{"rendered":"systemd resource limiting for mortals"},"content":{"rendered":"<h2>Motivation<\/h2>\n<p>I&#8217;ve recently gotten completely fed-up with my backup process (using <a href=\"http:\/\/duplicity.nongnu.org\">duplicity<\/a>) in the way it gobbles up huge amounts of memory (6GB+ out of 8GB physical memory) when doing its daily backup. This annoyance is amplified by code in the display driver that bails out when there is intense memory pressure and exits out of my Wayland session (<a href=\"https:\/\/bugs.freedesktop.org\/show_bug.cgi?id=100599\">likely bug<\/a>).\u00a0 I&#8217;m documenting my solution for Fedora 26, as I haven&#8217;t found trivially-understandable articles elsewhere.<\/p>\n<h2>Context<\/h2>\n<p>Duplicity is a quite a nice program, and I am quite thankful for its development. While I am unhappy with the way its resource usage scales with 200GB backups and limited available knowledge on best tunings for various scales greater and smaller, I have not found any superior backup solution. I just need to do something about its ability to gobble memory undeterred by the Linux kernel.<\/p>\n<p>Intel video hardware is probably the best low-power graphics with &#8220;reasonable&#8221; Linux support in its open-source drivers, but unfortunately, some debilitating bugs are being triggered with\u00a0 the recent Wayland migration. Most recently, its eagerness to bail out (and kill the user session) under memory pressure has been a concern.<\/p>\n<p>Resource management tools have been available on Linux (and other POSIX-style OSes) for quite some time, so why can&#8217;t you just do a &#8220;nice -n &#8220;19 and be done? Some <a href=\"https:\/\/gist.github.com\/JPvRiel\/bcc5b20aac0c9cce6eefa6b88c125e03\">notes suggest that systemd may be of help<\/a>.<\/p>\n<h2>Solution<\/h2>\n<p>TL;DR: We need to create a systemd slice and put the backup process systemd unit into that slice.<\/p>\n<p>Create a slice by creating the file in \/etc\/systemd\/system\/. Here&#8217;s what mine looks like:<\/p>\n<pre>$ cat \/etc\/systemd\/system\/duplicity.slice \r\n[Unit]\r\nDescription=Duplicity resource-limited slice\r\nBefore=slices.target\r\n\r\n[Slice]\r\nMemoryAccounting=true\r\nMemoryHigh=2G<\/pre>\n<p>I&#8217;ve set MemoryHigh to 2G, which is supposed to mean that systemd will start preferentially swapping pages out from the slice, should tasks inside that slice use more thatn 2GB physical memory. Other pages suggested setting\u00a0 MemoryMax, but as I still wanted my backup to complete, and a re-run of the backup process might even demand more, I did not want the MemoryMax behavior of &#8220;kill processes with OOM if the max is reached&#8221;. MemoryAccounting=true is a necessary prerequisite for any memory tracking controls in that slice.<\/p>\n<p>With the slice file created, then I just needed to add:<\/p>\n<pre>Slice=duplicity.slice<\/pre>\n<p>to the corresponding duplicity.service file. While I was there, I also added (Nice=19, IOSchedulingClass=3, IOSchedulingPriority=7).<\/p>\n<p>To apply the changes:<\/p>\n<pre>$ systemctl daemon-reload<\/pre>\n<p>When the task is running, you can verify that it&#8217;s running in your new slice.<\/p>\n<pre>$ systemd-cgls # list running services by cgroup slice\r\n\r\n$ systemd-cgtop # list running services sorted by resource usage<\/pre>\n<p>So far, the resource limiting has prevented the abrupt session-exits that used to consistently coincide with the backup trigger, but I will update this if I find anything new.<\/p>\n<h2>References<\/h2>\n<ul>\n<li>The necessity of creating a slice was only clear after reading through <a href=\"https:\/\/github.com\/systemd\/systemd\/issues\/3945\">a particular systemd issue<\/a>, which thankfully included a concise example. If this issue were more findable to people looking for a HOWTO, I may not have bothered with this post.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Motivation I&#8217;ve recently gotten completely fed-up with my backup process (using duplicity) in the way it gobbles up huge amounts of memory (6GB+ out of 8GB physical memory) when doing its daily backup. This annoyance is amplified by code in &hellip; <a href=\"https:\/\/imperfectcode.com\/?p=87\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/imperfectcode.com\/index.php?rest_route=\/wp\/v2\/posts\/87"}],"collection":[{"href":"https:\/\/imperfectcode.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imperfectcode.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/imperfectcode.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/imperfectcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=87"}],"version-history":[{"count":1,"href":"https:\/\/imperfectcode.com\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":88,"href":"https:\/\/imperfectcode.com\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions\/88"}],"wp:attachment":[{"href":"https:\/\/imperfectcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imperfectcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imperfectcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}