How to speed up Ubuntu 18

Illustration: Bionic Beaver is slow
Illustration: Bionic Beaver is slow

TL;DR: A list of tweaks and settings that can improve your Ubuntu 18x’s speed A LOT.

Reading time: around 40 minutes, so if you don’t have that much time on your hand, you can go read something else.

If you’re one of the people who have noticed a significant change in booting speed, shutdown speed, and generally, operating speed of your computer after upgrading or installing Ubuntu 18.x, then read on, this article is for you.

I have an older model Lenovo T410 (4×2.4Ghz i5, 4GB RAM), that was working absolutely flawlessly with Ubuntu before the 18.04 version, and even with flavors of Ubuntu like Linux Mint Cinnamon based on Ubuntu 17, but after 18x came along, things started moving slower and slower.

At first I thought I must have installed too many apps, and extensions, and my HDD space must be running out, or that some component like HDD or memory must be failing, but then, as it turned out, after extensive testing, none of the component were at fault, and the T410 runs perfectly smoothly if I just reinstall an older version of Ubuntu or any of its flavours.

So I started digging into what might improve the bootup, shutdown, and overall working speed of my Lenovo T410, here’s a list of improvements I made that has regiven my linuxbox the performance it had with previous Ubuntu versions.

NOTE: all the settings and tweaks I provide here, are used *at your own risk*, I do not and cannot guarantee that they will have the same effect on your computer as they did on mine, nor do I take any responsability if implementing these tweaks mess your system up. You take responsability for implementing them or not.*

The list of tweaks:

– add a supplementary in-memory filesystem for temporary I/O operations
– modify the way swapspace is used by the system
– modify how the harddrive is accessed and what scheduling type is used on r/w operations
– reduce the amount of memory alotted to chrome/chromium
– install the cooling application (thinkfan in this case ) and modify the config to fit a more permanent, more efficient cooling of the system

Needless to say, if your system needs third party drivers for operating at full capability, the first culprit of a sluggish system may very well be that third-party, closed-source driver. With that being said, if you’re in a more fortunate case like I was, that your computer doesn’t need any third-party driver, implementing these tweaks will have a major impact on your computer’s speed.

To add a supplementary in-memory filesystem for temporary I/O operations, first let’s check what / where the mounted filesystems are after a full boot. Log into your terminal application, and type

sudo mount

This will show you a list of mounted filesystems. Now look for the line that has SHM (shared memory) in it. It is usually set to around 384Mb, or it’s not set at all, which is a bit small if it’s at 384, or bad, if not set at all, so you might want to tweak it to at least 512MB.

Add the following line to your /etc/fstab file

tmpfs /tmp tmpfs size=512M,rw,rw,relatime 0 0
tmpfs /dev/shm tmpfs size=512M,rw,rw,relatime 0 0

The first line creates a 512MB limit on /tmp and sets it to type tmpfs instead of the original filesystem of the partition, because tmpfs will then be read and written to and from memory, which is a lot faster then I/O on harddrive, or even SSD.

The second line, limits the size of the shared memory, to 512MB limit, which should be more then sufficient, especially on computers and laptops with limited amount of RAM, like my 4GB on the T410. These two lines will indeed take up 1GB of that RAM, but will give you a lot of speed, so it’s worth sacrificing.

Next, you need to test how much caching and how much operating mode improvement can your harddrive take. For some reason, r/w caching is not by default turned on in the drivers that manage HDDs, so you have to first turn it on. You’ll be surprised to see how much I/O improvement can be achieved, by simply utilising the drive’s own caching mechanism. Type this in your terminal

sudo hdparm -W1 /dev/sdX

where X is the actual drive letter of your HDD’s main device. In my case, it was /dev/sda/, but it could be any other letter, depending on the number of actual devices you have mounted on the machine. This is drive level (phisical block device level), and has got nothing to do with partitioning, so there’s no partition number after, so no /dev/sda1, in this case, just /dev/sda

If your drive successfully accepted the command for turning its caching on, then next, it’s best to try if it will accept keeping the feature turned on after a reset or after a reboot. Try the following 2 commands, one after the other, and take note of their results (success or failure).

sudo hdparm -K1 /dev/sdX

And

sudo hdparm -k1 /dev/sdX

Where X, again, is the letter of your actual device.

If any of them is successful, take a note of it, because later we’ll add each succesfully registered command into a single line, that will be automatically executed on each boot.

Next, you have to check whether the drive’s I/O support at driver level can be set to be one of 32 bits or not (check the manual, by typing “man hdparm” into the terminal if you want to understand more about this)
sudo hdparm -c1 /dev/sdX

Again, X is the actual letter of your drive. In my case it was sudo hdparm -c1 /dev/sda, and here’s the result of that command:

Setting the hard-drive's I/O at driver level, to 32bits, using hdparm -c1 /dev/sdX
Setting the hard-drive’s I/O at driver level, to 32bits, using hdparm -c1 /dev/sdX

If your drive will not accept this command, don’t worry, there still are plenty of other improvements to its performance that might work just fine, so keep reading and try the following commands, the order in which you try them doesn’t matter, but it’s important to try them all.

sudo hdparm -A1 /dev/sdX

This will set the drive’s lookahead feature to “on”, meaning that it will look ahead of the data that is requested of it at bare minimum, and will cache also the next piece of data available in the same sector (if this is too technical, suffice to say, it will pre-read some data so next time it will be available much faster, then it would be otherwise).

If the command is successfully accepted, then next, you may want to set a preset amount of data that will be read in the lookahead / readahead cycles, and numbers will depend on what your system accepts, but usually between 0 and 2048 (driver limitation)

sudo hdparm -a2048 /dev/sdX

If your drive accepts this, you’re instantly on a faster machine, for the duration of the current session, as these are settings that are reset at boot time, that’s why you’ll want to save these settings into a single line and then run that at boot.

Next, you’ll want to tweak the I/O scheduler. Most system come with “noop” as a scheduler, but that is not the most performant scheduler type for I/O operations. If you work on large files (architectural design, graphic design, enormous amounts of code in-memory like game programming, etc.), you’ll want to use the “deadline” scheduler, but for all other operations, like regular day-to-day use of the computer, browsing, email, office work, and an occasional picture editing, social media, etc., then you’ll definitely want the “cfq” scheduler, more suited for a large number, small file size operations.

To achieve this, you have to add a value to a very specific location, namely the /sys/block/sda/queue/scheduler  . Type the following into the terminal, to check which scheduler is active on your system:

cat /sys/block/sda/queue/scheduler

Obviously, if your device’s letter is not “A”, you would replace it with the corresponding letter, like “/sdb”, or “/sdc” or whatever it may be.

The line that is output from the command you just typed, contains the currently active scheduler.

noop deadline [cfq]

If it’s already set to “[cfq]”, there’s nothing more to set. If it’s at “[noop]”, you may want to type the following command:

echo cfq > /sys/block/sda/queue/scheduler

And if it’s accepted, then make sure that just as with the hdparm command above, that has set the lookahead of the harddrive to 2048Kbytes, set the same amount for the I/O scheduler, with the following command:

echo 2048 >/sys/block/sda/queue/read_ahead_kb

Now, if you want to save all the commands that you’ve just typed,  (remember, I mentioned above that you should take a note of which command was successful and which not ?), then you need to edit the /etc/rc.local file and add all the succesfully executed commands to it.

All the commands from the hdparm lines can be combined into one single line.  So in my T410’s case, the hdparm line has become in the end:

sudo hdparm -W1 -A1 -a2048 -S 40 /dev/sda

The “S 40” simply means that the drive will standby after 40*5 seconds, so if I don’t use the system for 3 minutes and 20 seconds, the drive will spin down to conserve power. This is a very good trick for saving power, when you’re mostly reading stuff online, preparing for exams, or reading any document saved locally.

Now let’s take the tweaking a bit furter. Given the fact that even though swap space is less and less used compared to just a few years ago when machines equipped with barely 1G of RAM were the norm (how did we ever work on those ?!), swap is still being used by the kernel to write out data temporarily from RAM to make space for other more currently important data, and then back, which means if the ratio of swap usage compared to the pressure of its usage is not set correctly, your system will work more sluggishly even with large amounts or RAM available.

The ratio of swap usage vs. RAM usage can be set by modifying actual system files, but it’s not recommended, instead it’s best if you just create a separate directive file for your systemctl parser, in the /etc/sysctl.d folder, optionally with a very high number, like 99 (files in the system directive folders are always parsed in order of their preceding number’s value, so files named 10-….whatever will be parsed much sooner then files named 99… whatever).

Create a file named 99-custom.conf, by typing the following

sudo nano /etc/sysctl.d/99-custom.conf

Once the editor is fired up, type the following commands into it, one on each line

vm.swappiness=40
vm.vfs_cache_pressure=60

If you want to also further improve the snappines of the system, you can also use the following lines. These are NOT filesystem-related, but network related, however, they can improve a lot, how your computer reacts to requests sent over the network and to responses received from servers.
net.ipv4.tcp_keepalive_time = 3000
net.ipv4.tcp_keepalive_intvl = 305
net.ipv4.tcp_keepalive_probes = 8

net.core.wmem_max = 2621440
net.core.somaxconn = 240
net.ipv4.inet_peer_maxttl = 329
net.ipv4.inet_peer_minttl = 64
net.ipv6.conf.all.mtu = 1500

Once you’ve typed the commands, press F2 or CTRL_X to exit, choose “save” to save the file.

Now in order to have that file executed, there’s nothing else you need to do, however, it is entirely possible, that another command will be executed on bootup, by sysctl, so in order to make sure that this particular set of commands will be the last sysctl command set executed on bootup, you have to include a reference to it in the /etc/rc.local file.

So edit the file again, by typing sudo nano /etc/rc.local, and add this line to it:

sysctl -p /etc/sysctl.d/99-custom.conf

Now, last, but not least, you have to make sure, that your machine is cooled properly, and by default, not all installations include a fan management app, in fact, I’ve seen very little evidence that a cooling app or a fan control app is installed on any Ubuntu distro, so it’s up to you to install the proper cooling app for your machine. In my case, on Lenovo Thinkpad T410, it was installing the thinkfan app, by typing

sudo apt install thinkfan

into the terminal, and then tweaking the temperatures in /etc/thinkfan.conf so that it would match my personal cooling needs, so after having extensively tested for months (!), with different configurations, I’ve currently ended up using this set of temperatures and speeds (relevant content of /etc/thinkfan.conf)

tp_fan /proc/acpi/ibm/fan
tp_thermal /proc/acpi/ibm/thermal (5,5,10,20,10,5,15,10,10,10)

(0, 0, 57)
(1, 55, 61)
(2, 59, 64)
(3, 63, 65)
(4, 62, 68)
(5, 67, 70)
(6, 69, 77)
(7, 75, 32767)

Be extremely careful when you tweak things you don’t understand, you can either completely wrack your machine, or need to frequently replace a cooling fan, in this particular case.

Now, hopefully, your rc.local will look like this:

The last line, sudo service thinkfan restart , simply makes sure that after all else is loaded, the thinkfan fan service will be restarted, because I’ve noticed and have read about it on several online forums, that on several thinkpad models, the fan stops or gets stuck at a specific speed at bootup, and that means that your laptop could get overheated and even burnt. This is a thinkpad specific issue, and might not related to your computer or laptop, if you are using anything else then a thinkpad, you are free to leave that last line out.

Now, last, but not least, if you’re using Chrome or Chromium, you have to understand that it’s a goddamn resource hog, it will eat up however much memory you will allow it to eat up, and that’s by default “however much is available in the system”. On computers with limited amount of RAM, like merely 4GB, it’s a no-brainer, really, to limit the amount of RAM your browser will be able to eat up.

In oder to limit Chrome’s memory usage, simply edit any icon or launcher on your system, and add the following tweak to the chrome command line:

–memory-model=medium

That’s two dashes, – – , one after the other, in the line above. Save the launcher or icon, and next time you’ll launch your browser, you’ll be surprised how smoother your system will function even with several tabs open in the browser.

That’s it. After implementing all these tweaks, your machine will hopefully run much better, faster, and become snappier. After making these settings, it is of course, recommended, to reboot the machine, so the tweaks and settings can load at boot time.

Also, as a simple common sense advice, you can use tools to check what apps are loaded at boot time, and if you can afford, delete or uninstall, at least, uncheck all applications that you don’t really need, automated backups, wheather apps, etc., that might offer some eyecandy on the desktop once you’re logged in, but they are really nothing else then resource hogs.

Have you implemented any of these settings and tweakss ? Did you notice any improvement ?
Do you know of any other improvements that might be added to this list ?

I’m looking forward to questions and comments, and I’ll do my best to answer them.

 

Leave a Reply

Your email address will not be published. Required fields are marked *