{"id":67184,"date":"2022-09-13T09:01:11","date_gmt":"2022-09-13T09:01:11","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=67184"},"modified":"2022-09-13T09:01:11","modified_gmt":"2022-09-13T09:01:11","slug":"how-i-troubleshoot-swappiness-and-startup-time-on-linux","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=67184","title":{"rendered":"How I troubleshoot swappiness and startup time on Linux"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">How I troubleshoot swappiness and startup time on Linux<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/dboth\" class=\"username\">David Both<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Tue, 09\/13\/2022 &#8211; 03:00<\/span><\/p>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>I recently experienced another interesting problem in the Linux startup sequence that has a circumvention\u2013not a solution. It started quite unexpectedly.<\/p>\n<p>I was writing a couple of articles while making some updates to my personal copy of my series of books, &#8220;Using and Administering Linux: Zero to SysAdmin.&#8221; I had four instances of <a href=\"https:\/\/opensource.com\/tags\/libreoffice\" target=\"_blank\" rel=\"noopener\">LibreOffice Write<\/a> open to doing all that. I had three VMs running with VirtualBox to test some of the things I was writing about. I also had LibreOffice Impress open to work on an unrelated presentation. I like to listen to music, so I had one of several tabs in Firefox open to Pandora, my music streaming service of choice. I had multiple Bash shells open using Konsole with numerous tabs and the Alpine text-mode email client in one. Then there were the various tabs in the Thunar file manager.<\/p>\n<p>So I had a lot going on. Just like I do now as I write this article.<\/p>\n<h2>The symptoms<\/h2>\n<p>As I used these open sessions, I noticed that things slowed down considerably while waiting for the system to write a document to the M.3 SSD\u2013a process that should have been really fast. I also noticed that the music was choppy and dropped out completely every few minutes. Overall performance was generally poor. I began to think that Fedora had a serious problem.<\/p>\n<p>My primary workstation, the one I was working on at the time, has 64GB of RAM and an Intel Core i9 Extreme with 16 cores and Hyperthreading (32 CPUs) that can run as fast as 4.1 GHz using my configured overclocking. So I should not have experienced any slowdowns\u2013or so I thought at the time.<\/p>\n<h2>Determine the problem<\/h2>\n<p>It did not take long to find the problem because I have experienced similar symptoms before on systems with far less memory. The issue looked like delays due to page swapping. But why?<\/p>\n<p>I started with one of my go-to tools for problem determination, htop. It showed that the system was using 13.6GB of memory for programs, and most of the rest of the RAM was in cache and buffers. It also showed that swapping was actively occurring and that about 253MB of data was stored in the swap partitions.<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">Date <span class=\"sy0\">&amp;<\/span> Time: <span class=\"nu0\">2022<\/span>-08-<span class=\"nu0\">12<\/span> <span class=\"nu0\">10<\/span>:<span class=\"nu0\">53<\/span>:08<br>\nUptime: <span class=\"nu0\">2<\/span> days, <span class=\"nu0\">23<\/span>:<span class=\"nu0\">47<\/span>:<span class=\"nu0\">15<\/span><br>\nTasks: <span class=\"nu0\">200<\/span>, <span class=\"nu0\">1559<\/span> thr, <span class=\"nu0\">371<\/span> kthr; <span class=\"nu0\">4<\/span> running<br>\nLoad average: <span class=\"nu0\">3.97<\/span> <span class=\"nu0\">3.05<\/span> <span class=\"nu0\">2.08<\/span><br>\n\u00a0 \u00a0<br>\nDisk IO: <span class=\"nu0\">202.6<\/span><span class=\"sy0\">%<\/span> read: 687M write: 188K<br>\nNetwork: rx: 0KiB<span class=\"sy0\">\/<\/span>s tx: 0KiB<span class=\"sy0\">\/<\/span>s <span class=\"br0\">(<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">\/<\/span><span class=\"nu0\">0<\/span> packets<span class=\"br0\">)<\/span><br>\nSystemd: running <span class=\"br0\">(<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">\/<\/span><span class=\"nu0\">662<\/span> failed<span class=\"br0\">)<\/span> <span class=\"br0\">(<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">\/<\/span><span class=\"nu0\">7912<\/span> <span class=\"kw3\">jobs<\/span><span class=\"br0\">)<\/span> \u00a0 \u00a0 <br>\nMem<span class=\"br0\">[<\/span><span class=\"sy0\">|||||||<\/span><span class=\"co0\">##*@@@@@@@@@@@@@@@@@@@@@@@@@@ \u00a0 \u00a013.6G\/62.5G]<\/span><br>\nSwp<span class=\"br0\">[<\/span><span class=\"sy0\">||<\/span><span class=\"co0\"># \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0253M\/18.0G]<\/span><\/div><\/div><\/pre>\n<p>But that meant I still had lots of memory left the system could use directly for programs and data and more that it could recover from cache and buffers. So why was this system even swapping at all?<\/p>\n<p>I remembered hearing about the &#8220;swappiness&#8221; factor in one of my <a href=\"https:\/\/www.redhat.com\/en\/services\/training-and-certification?intcmp=7013a000002qLH8AAM\" target=\"_blank\" rel=\"noopener\">Red Hat training classes<\/a>. But that was a long time ago. I did some searches on &#8220;swappiness&#8221; to learn about the kernel setting <strong>vm.swappiness<\/strong>.<\/p>\n<p>The default value for this kernel parameter is <strong>60<\/strong>. That represents the percent of free memory not yet in use. When the system reaches that 60% trigger point, it begins to swap, no matter how much free memory is available. My system started swapping when about 0.6 * 62.5GB = 37.5GB of unused memory remained.<\/p>\n<p>Based on my online reading, I discovered that 10% is a better setting for many Linux systems. With that setting, swapping starts when only 10% of RAM is free.<\/p>\n<p>I checked the current swappiness setting on my system, and it was set to the default.<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\"><span class=\"co0\"># sysctl vm.swappiness<\/span><br>\nvm.swappiness = <span class=\"nu0\">60<\/span><\/div><\/div><\/pre>\n<p>Time to change this kernel setting.<\/p>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">More for sysadmins<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/sysadmin\/?intcmp=7013a000002CxqpAAC\">Enable Sysadmin blog<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/go.redhat.com\/automated-enterprise-ebook-20180920?intcmp=7013a000002CxqpAAC\">The Automated Enterprise: A guide to managing IT with automation<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/ansible-quickstart?intcmp=7013a000002CxqpAAC\">eBook: Ansible automation for Sysadmins<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/engage\/system-administrator-guide-s-202107300146?intcmp=7013a000002CxqpAAC\">Tales from the field: A system administrator&#8217;s guide to IT automation<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/kubernetes-sysadmin?intcmp=7013a000002CxqpAAC\">eBook: A guide to Kubernetes for SREs and sysadmins<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/sysadmin?intcmp=7013a000002CxqpAAC\">Latest sysadmin articles<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<h2>Fix the issue<\/h2>\n<p>I won&#8217;t dive into the gory details, but the bottom line is that either of the following commands, run as root, will instantly do the job on a running Linux computer without a reboot.<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\"># <\/span>sysctl <span class=\"re5\">-w<\/span> vm.swappiness=<span class=\"nu0\">10<\/span><\/code><\/span><\/pre>\n<p>You could also use this next command to do the same thing.<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\"># <\/span><span class=\"kw3\">echo<\/span> <span class=\"nu0\">10<\/span> <span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">\/<\/span>proc<span class=\"sy0\">\/<\/span>vm<span class=\"sy0\">\/<\/span>swappiness<\/code><\/span><\/pre>\n<p>Tecmint has an excellent article about setting kernel parameters.<\/p>\n<p>Both commands change the live kernel setting in the <code>\/proc<\/code> filesystem. After running either of those commands, you should run the <code>sysctl vm.swappiness<\/code> command to verify that the kernel setting has changed.<\/p>\n<p>But those commands only change the swappiness value for the currently running system. A reboot returns the value to its default. I needed to ensure that this change is made persistent across reboots.<\/p>\n<h2>But first, the failure<\/h2>\n<p>To permanently change the kernel <strong>vm.swappiness<\/strong> variable, I used the procedure described in my previous article, <a href=\"https:\/\/opensource.com\/article\/22\/8\/disable-ipv6\" target=\"_blank\" rel=\"noopener\">How I disabled IPv6 on Linux<\/a>, to add the following line to the end of the <code>\/etc\/default\/grub<\/code> file:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"text geshifilter-text\">GRUB_CMDLINE_LINUX=\"vm.swappiness=1\"<\/code><\/span><\/pre>\n<p>I then ran the <code>grub2-mkconfig<\/code> command as root to rebuild the <code>\/boot\/grub2\/grub.cfg<\/code> file. However, testing with VMs and real hardware showed that it did not work, and the swappiness value did not change. So I tried another approach.<\/p>\n<h2>And the success<\/h2>\n<p>Between this failure at startup time, the one I describe in the <em>How I disabled IPv6 on Linux<\/em> article, and other startup issues I explored due to encountering those two, I decided that this was a Linux startup timing problem. In other words, some required services, one of which might be the network itself, were not up and running, which prevented these kernel option changes from being committed to the <code>\/proc<\/code> filesystem, or they were committed and then overwritten when the service started.<\/p>\n<p>I could make all of these work as they should by adding them to a new file, <code>\/etc\/sysctl.d\/local-sysctl.conf<\/code> with the following content, which includes all of my local kernel option changes:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"text geshifilter-text\">###############################################<br>\n# \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0local-sysctl.conf \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#<br>\n# \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #<br>\n# Local kernel option settings. \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #<br>\n# Install this file in the \/etc\/sysctl.d \u00a0 \u00a0 \u00a0#<br>\n# directory. \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#<br>\n# \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #<br>\n# Use the command: \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#<br>\n# sysctl -p \/etc\/sysctl.d\/local-sysctl.conf \u00a0 #<br>\n# to activate. \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#<br>\n# \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #<br>\n###############################################<br>\n###############################################<br>\n# Local Network settings \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#<br>\n# Specifically to disable IPV6 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#<br>\n###############################################<br>\nnet.ipv6.conf.all.disable_ipv6 = 1<br>\nnet.ipv6.conf.default.disable_ipv6 = 1<br><br>\n###############################################<br>\n# Virtual Memory \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#<br>\n###############################################<br>\n# Set swappiness<br>\nvm.swappiness = 1<\/div><\/div><\/pre>\n<p>I then ran the following command, which activated only the kernel options in the specified file:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\"><span class=\"co0\"># sysctl -p \/etc\/sysctl.d\/local-sysctl.conf<\/span><br>\nnet.ipv6.conf.all.disable_ipv6 = <span class=\"nu0\">1<\/span><br>\nnet.ipv6.conf.default.disable_ipv6 = <span class=\"nu0\">1<\/span><br>\nvm.swappiness = <span class=\"nu0\">13<\/span><\/div><\/div><\/pre>\n<p>This is a more targeted approach to setting kernel options than I used in my article about disabling IPv6.<\/p>\n<h2>Reporting the bug<\/h2>\n<p>At the time of this writing, there is no true fix for the root cause of this problem\u2013whatever the cause. There is a way to temporarily circumvent the issue until a fix is provided. I used the <code>\/etc\/sysctl.d\/local-sysctl.conf<\/code> file that I had created for testing and added a systemd service to run at the end of the startup sequence, wait for a few seconds, and run <code>sysctl<\/code> on that new file. The details of how to do that are in the <em>How I disabled IPv6 on Linux<\/em> article.<\/p>\n<p>I had already reported this as bug 2103517 using Red Hat&#8217;s Bugzilla when trying to disable IPv6. I added this new information to that bug to ensure that my latest findings were available to the kernel developers.<\/p>\n<p>You can follow the <a href=\"https:\/\/bugzilla.redhat.com\/show_bug.cgi?id=2103517\" target=\"_blank\" rel=\"noopener\">link to view the bug report<\/a>. You do not need an account to view bug reports.<\/p>\n<h2>Final thoughts<\/h2>\n<p>After experimenting to see how well I could reproduce the symptoms, along with many others, I have determined that the <strong>vm.swappiness<\/strong> setting of 60% is far too aggressive for many large-memory Linux systems. Without a lot more data points than those of my own computers, all I can tentatively conclude is that systems with huge amounts of RAM that get used only infrequently are the primary victims of this problem.<\/p>\n<p>The immediate solution to the problem of local kernel option settings not working is to set them after startup. The automation I implemented is a good example of how to use systemd to replace the old SystemV startup file <code>rc.local<\/code>.<\/p>\n<p>This bug had not been previously reported. It took a few days of experimenting to verify that the general problem in which locally-set kernel options were not being set or retained at startup time was easily repeatable on multiple physical and virtual systems. At that point, I felt it important to report the bug to ensure it gets fixed. Reporting it is another way I can give back to the Linux community.<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>I recently experienced another interesting problem in the Linux startup sequence that has a circumvention\u2013not a solution. It started quite unexpectedly.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/09\/gears_devops_learn_troubleshooting_lightbulb_tips_520.png\" width=\"520\" height=\"292\" alt=\"Tips and gears turning\" title=\"Tips and gears turning\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>opensource.com<\/p>\n<\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/linux\" hreflang=\"en\">Linux<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/sysadmin\" hreflang=\"en\">Sysadmin<\/a><\/div>\n<\/p><\/div>\n<div class=\"field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-listicles field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/article\/22\/9\/recover-linux-system-live-usb\" hreflang=\"en\">How I recovered my Linux system using a Live USB device<\/a><\/div>\n<\/p><\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/09\/cc-by-sa--16.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How I troubleshoot swappiness and startup time on Linux David Both Tue, 09\/13\/2022 &#8211; 03:00 I recently experienced another interesting problem in the Linux startup sequence that has a circumvention\u2013not a solution. It started quite unexpectedly. I was writing a couple of articles while making some updates to my personal copy of my series of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":67185,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-67184","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/67184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=67184"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/67184\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/67185"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=67184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=67184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=67184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}