How to tweak and optimize SSD for Ubuntu, Linux Mint

I have been using Intel 520 SSD 180GB on my Dell Inspiron 11z-1121 laptop for a while and get great speed. However, if you want to tweak and optimize your Linux SSD for Ubuntu, Linux Mint, Debian or any linux distribution to make your SSD run even faster and live longer, here how I do my ubuntu ssd tweak.

Note: this article will help both Linux SSD and mechanical hard drive performance. If you want to improve and tweak your Ubuntu, Linux Mint, Fedora, Centos or any Linux distribution, this article will help you.

SSD benefits

  • Faster than traditional HDD, about 2-3 times faster than SATA2 7200 RPM HDD
  • No moving parts
  • Lower heat during operating
  • Uses less power
  • Higher reliability

SSD types

ssd nam huy linux blogLet’s talk about SSD types for a little bit. If you are in the market and looking to purchase your first SSD, you should do a bit of research before you order your SSD. Not every SSD is created equal, there are differences in type of ssd controller, ssd memory chip, MTBF (Mean time between failures) or lifespan for short, features. Every SSD manufacture “cook” their SSD differently with different feature, but in general there are three SSD types in the market, SLC (single level cell), MLC (multi level cell), and TLC (triple level cell). There are huge differences between SLC, MLC, and TLC in term of performance and MTBF.

SLC (single level cell) has the highest performance and longer endurance over Multi-Level Cell (MLC) NAND and Triple-Level Cell (TLC). Each SLC cell can have 100.000 write/erase cycles. SLC is most often found in enterprise-grade SSDs, usually 10x more persistent, 3x faster Sequential Write same Sequential Read, and about 30% more expensive than MLC.

MLC (multi level cell) is the most popular SSDs in mid range market which is targeted at consumer-grade. Each MLC cell can have 10.000 write/erase cycles. Nowadays cameras, smartphones, USB flash drive and portable music players use MLC SSDs. Most SSDs in the market now are MLC. Being slower and has lower lifespan than SLC, MLC SSDs are usually cheaper while provide excellent performance compare to traditional mechanic hard drive.

TLC (triple level cell) has higher density than SLC and MLC, means TLC will have the lowest endurance limit and slower read and write speeds than other twos. Each TLC cell can have 5.000 write/erase cycle. Most TLC SSDs are manufactured by Samsung, has higher power and error correction requirements, and higher wear levels. TLC can be found at low-end or entry-level SSDs.

If you have the budget, SLC (single level cell) SSDs are the best choice, otherwise MLC (multi level cell) SSDs should be great for most office/home user while the price is right. There are many bad reviews about TLC (triple level cell) SSDs, usually sudden dead (stop working without any sign), while very small percentage reach their MTBF (Mean time between failures). If you can, avoid TCL SSDs.

Update your SSD firmware

SSD manufactures (Intel, Samsung, SanDisk, Plextor, Corsair, Crucial…) often public new firmwares for their SSD to improve
improve performance, reliability, and system compatibility, fix technical issues, bugs). Check your SSD’s manufacture often to make sure you have the latest firmware for your SSD. *** Always backup before you do anything ***

Use EXT4 file system

Most modern new linux distributions default file system is EXT4. EXT4 gives you good speed and reliable for your SSD, EXT4 also supports TRIM. EXT4 is matured for SSD usage since linux kernel 2.6.28. If you have older linux distributions, you may have an option to choose EXT4 as file system, but newer linux distributions will install with EXT4 as default so you don’t need to worry about.

Enable TRIM

TRIM (Trim command let an OS know which SSD blocks are not being used and can be cleared)

Back up fstab first in case something wrong happen.
# cp /etc/fstab ~/fstab.bk

Edit fstab file
# nano /etc/fstab

Add discard to your ssd drives or partitions, after ext4
UUID=bef10b86-494d-41c6-aa46-af72cfba90fd / ext4 discard,errors=remount-ro 0 1

Adding noatime and nodiratime

noatime and nodiratime are mount options in linux for linux file system. noatime disables atime updates on file system, and nodiratime will disables atime updates on directory system. By adding noatime and nodiratime will greatly reduce ssd load means performance gains.

Edit fstab file
# nano /etc/fstab

Add noatime,nodiratime to your ssd drives or partitions, after ext4
UUID=bef10b86-494d-41c6-aa46-af72cfba90fd / ext4 discard,noatime,nodiratime,errors=remount-ro 0 1

Disable hibernation

Hibernate and suspend sometimes don’t work depends on the hardware you have for your system. Sometimes they make your system crash and unstable. Plus hibernation will put a lot of writes to your SSD which will shorten your SSD life.

# nano /usr/share/polkit-1/actions/org.freedesktop.upower.policy

Look for
<allow_active>yes</allow_active>

Change from “yes” to “no”, there are two, one for hibernation, and another one for suspend. If you have to disable them both, make sure to replace them both from yes to no.
<allow_active>no</allow_active>

Tmpfs

Files and directories store in tmpfs is temporary, tmpfs keeps everything in virtual memory (kernel internal caches), nothing will be saved on your hard drive or SSD. Once your system is restarted, everything in tmpfs will be gone. Normally linux system cache stores in /tmp directory. To reduce writes on SSD, you can mount /tmp to tmpfs.

Edit fstab file
# nano /etc/fstab

Add the line to the end of fstab file
tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0

If logs aren’t important for you (laptop or desktop), you can also mount /var/log to tmpfs. Add the line to the end of fstab file

Preload

Preload is a Linux software developed by Behdad Esfahbod. Preload learns programs that users use often, records statics using Markov chains, analyzes, and predicts what programs will be most used. Preload then will load those programs, binaries, and dependencies into memory or ram. By having programs already in RAM or memory, it will take less time when you actually start that program or programs.

To install preload on Ubuntu, Linux Mint or debian based distributions
# apt-get update && apt-get install preload

To install preload on Fedora, Centos or Redhat based distributions
# yum install preload

Swap and Swapiness

Swappiness is a part of Linux kernel that let you control how much swap (virtual memory) file is being used. Swappiness values can be changed from 0 to 100. The higher swappiness values the more Linux kernel will try to use swap space, the lower swappiness values means linux kernel will useless or try not to use swap space depends on our setting. The default swappiness value from linux kernel is 60, if your system have plenty have RAM, you should avoid using swap space which writes and reads will be on your SSD or hard drive. For system with 4 GB or more RAM, I would suggest to reduce the usage of swap by changing swappiness settings to between 10 even 0.

To check your swappiness setting on your system, you should see the value of 60 as default.
$ cat /proc/sys/vm/swappiness

The value is up to you to decide. Here is my suggestion

  • 2 GB = 30
  • 4 GB = 10
  • 6 GB or more = 0

To change swappiness setting:
$ su -<h1>nano /etc/sysctl.conf</h1>

And add this line into sysctl.conf file.
vm.swappiness = 10

Disk Caching and virtual memory (VM) subsystem

dirty_background_ratio

dirty_background_ratio is the maximum percentage of dirty pages stores in total system memory before need to be written to disk when pdflush/flush/kdmflush or kernel flusher threads kick in. The default value of dirty_background_ratio depends on your linux distribution, but we are going to decrease its value to prevent heavy disk I/O.
To check default setting for dirty_background_ratio
# sysctl vm.dirty_background_ratio

vm.dirty_ratio

vm.dirty_ratio is the percentage of total system memory to store dirty pages. Low vm.dirty_ratio means the kernel will flush dirty data to disk more often with smaller writes, and higher vm.dirty_ratio will allow kernel to flush dirty data to disk less often with bigger writes. We want to set vm.dirty_ratio high.
To check default setting for vm.dirty_ratio and vm.dirty_background_ratio
# sysctl vm.dirty_ratio

To adjust vm.dirty_ratio and dirty_background_ratio, edit sysctl.conf
# nano /etc/sysctl.conf

Add to your sysctl.conf file

Move Firefox cache to RAM

Firefox saves its cache to HDD or SSD, if you want to reduce writes to your SSD to increase SSD’s life, well move all firefox cache to RAM which will speed up firefox a little bit but all the cache will be gone after you restart your computer.

Open firefox, in the address bar type in
about:config

You will see some sort of warn, basically if you don’t know what you are doing you may break firefox. You can move on and click on “I will be careful, I promise”. In the search bar (the about:config search bar, not the regular firefox search bar), type in
browser.cache.disk.parent_directory

If the search result is blank means your firefox does not have that string. To create one, right click on firefox empty white space, click New > String with the value name is
browser.cache.disk.parent_directory

Click Ok, the next box is the cache location, type in
/dev/shm/firefox

Restart firefox after you done.

Comments

  1. Zapho says

    Nice post – great info. One point though, be careful with tmpfs. By default, tmpfs allocates half of your RAM for the temporary file system. This block is then unavailable to the OS. In my case, I found that 4GB of my 8GB RAM was being used to hold a handful of temp files that collectively were no bigger than 10mb. Naturally, I was a bit annoyed!

    You should specify a limit in fstab as follows:

    tmpfs /tmp tmpfs defaults,noatime,size=50mb,mode=1777 0 0

  2. agor says

    Good point the one Zapho makes, another way to do that is to specify percentage.
    My system has 16G ram, so i decided to go close to 1.5-2G:
    tmpfs /tmp tmpfs defaults,noatime,mode=1777,size=15% 0 0

  3. james says

    Nice and concise. However one note. noatime includes nodiratime. So you don’t need to do #2 if you have #1 in place. Won’t hurt anything, just a note. Otherwise thanks

  4. ken says

    no comment, but a question. if hibernation and suspend are disabled, why create a swap partition and limit “swapiness”? most seem to “get it”, but to me, a novice (have not run ubuntu at all), it seems like a lot of work when swap could be eliminated if
    enough ddr3 is installed. i am planning on 8gb of ddr3.
    please reply.

    • namhuy says

      If you have enough RAM on your system, it’s safe to turn off swap completely. Don’t forget some programs like VM (virtual machines), video editors, LibreOffice uses lots of memory.

      You can turn off swap
      1. If you don’t run any RAM intensive programs
      2. Don’t use hibernation (save contents of RAM to swap partitions)

      Sometimes some bad programs *may* eat your RAM like crazy, swap partition is still like a safety net to prevent your system to crash (not likely to happen though).

      My laptop w/ 8 Gb RAM, I do run VMs sometimes with 2 Gb RAM each VMs, the system never use any swap space (I mount swap partition on RAM to prevent SSD wear/tear), so technically even I do have swap partition it’s on RAM not on my SSD. In your case depends on how you use your system, 8 Gb of RAM is safe to disable swap completely, if you want to.

      • ken says

        thanks. i will use your optimization tips. let me just keep it simple and keep a small swap
        because the machine will be for editing photos, though not much else.
        how is swap mounted to ddr3? i assume you have no swap partition on the ssd?

        • namhuy says

          I just checked my laptop. I do have swap parition on my SSD, but since I set vm.swappiness = 0, my system won’t write anything swap but use RAM space first. You can use swapon command to see if you have swap parition or not
          # swapon -s
          This is my swap parition
          Filename Type Size Used Priority
          /dev/sda5 partition 8179708 0 -1

          or
          # blkid | grep swap
          /dev/sda5: UUID=”ab83ccb7-53af-4ef7-aff5-9157ee85dc30″ TYPE=”swap”

          You can completely disable swap parition by add “#” sign in front of your swap parition in fstab file.

  5. Ejoo says

    I used this line on ubuntu 12.04, and my system crashed. It has to be “size=50M” instead of “size=50mb”. Before rebooting, check out the new fstab with ‘sudo mount -a’, and look for errors. BUT nevertheless, great work here on this side.

    Though it’s good, to set the limit, I don’t think that tmpfs “allocates” all the size given by option. You can check this out with ‘df -h’ and then check your free ram. A good german :-/ article: http://wiki.ubuntuusers.de/SSD/Auslagerung

  6. isaac says

    @Ejoo Many thanks for the note on the size needing to be “size=50M”

    Following the first commenter’s note also crashed my system (14.04 running Gnome Shell). For anyone else having this problem, here’s what got my system back up:

    1. Log into the terminal via safe mode
    2. Run the following command to be able to edit the /etc/fstab file: “mount -o remount /” (see this ask ubuntu discussion http://askubuntu.com/questions/197459/how-to-fix-sudo-unable-to-open-read-only-file-system)
    3. Change “50mb” to “50M”
    4. Reload (reloading may take a minute or two… be patient)

  7. says

    Hi,
    interesting topic. Thanks for the infos.

    If you’re interested on using Linux on servers with huge partitions, consider rather using XFS than ext4. It performs better on large partitions, even better on large files (videodata), has no issue on RAID configs, has all the features you need, including TRIM support for SSD.

    And last but not least its invented by SGI ;).

    If you want to use XFS for intensive read/write work there are a lot of tweaks around for XFS, beginning from the creation of partitions to parameters on fstab.

    Creating proper optimized XFS partitions: http://www.mythtv.org/wiki/Optimizing_Performance or http://brian.lanierfamily.net/xfs-file-system-tweaks-for-performance/

    Great fstab parameters for XFS: http://everything2.com/title/Filesystem+performance+tweaking+with+XFS+on+Linux

    At the end you land on “noatime,nodiratime,logbufs=8,discard” for an SSD optimized XFS partition. Blazingly fast initialized, reliable & fast. Never forget that booting however can’t be done through XFS.

    So you need a 500mb /boot partition as extX. At least. Don’t know if this changed in the newer Kernels. Correct me if I’m wrong.

    P.S.:
    Don’t forget to increase “blockdev” parameters in your rc.local file for SSD (Depending on you CPU)
    “blockdev –setra 32768 (or) 65536 /dev/sdX”

    (begin tuning with “8192” and measure write/read performance & increase it to the best value for your system).

    • namhuy says

      thanks for the tips. I head about XFS but never try. I will try on virtualbox later and maybe write another tutorial for XFS. Again, thanks :)

  8. Alsan Wong says

    I’m having a problem of can’t startup the session caused by the line:

    tmpfs /var/tmp tmpfs defaults,noatime,mode=1777 0 0

    by removing this line, everythings works fine. thanks for the tips anyway.

Leave a Reply