SSD/NVMe Tweaks (TRIM/Discard): Difference between revisions
mNo edit summary |
|||
Line 59: | Line 59: | ||
== Set the automatic TRIM job to daily == | == Set the automatic TRIM job to daily == | ||
<syntaxhighlight lang="shell" line="1"> | <syntaxhighlight lang="shell" line="1"> | ||
sudo systemctl enable fstrim.timer | |||
</syntaxhighlight><syntaxhighlight lang="shell" line="1"> | |||
sudo mkdir /etc/systemd/system/fstrim.timer.d | sudo mkdir /etc/systemd/system/fstrim.timer.d | ||
sudo nano /etc/systemd/system/fstrim.timer.d/override.conf | sudo nano /etc/systemd/system/fstrim.timer.d/override.conf | ||
Line 65: | Line 67: | ||
OnCalendar= | OnCalendar= | ||
OnCalendar=daily | OnCalendar=daily | ||
</syntaxhighlight>Reboot and check the updated value.<syntaxhighlight lang="shell" line="1"> | </syntaxhighlight>Reboot the system or do daemon-reload and check the updated value.<syntaxhighlight lang="shell" line="1"> | ||
sudo systemctl daemon-reload | |||
</syntaxhighlight><syntaxhighlight lang="shell" line="1"> | |||
systemctl cat fstrim.timer | systemctl cat fstrim.timer | ||
</syntaxhighlight><syntaxhighlight lang="bash"> | </syntaxhighlight><syntaxhighlight lang="bash"> |
Revision as of 16:40, 17 August 2022
Note, within the examples in the following sections, /dev/sda
refers to a SSD device while /dev/nvme0n1
refers to a NVMe device.
Get Full SSD/NVMe Device Info
For SSD devices use:
smartctl -x /dev/sda # -x, --xall; -a, --all
hdparm -I /dev/sda # doesn't support NVMe
For NVMe devices use:
smartctl -x /dev/nvme0n1 # -x, --xall; -a, --all
nvme smart-log -H /dev/nvme0n1 # apt install nvme-cli
Tweak the AMP value of SSD
Most Linux distributions use Linux Kernel’s “Advanced Power Management (APM)” API to handle configuration, optimize performance, and ensure stability of storage devices. These devices are assigned an APM value between 1 and 255 to control their power management thresholds. A value of 254 indicates best performance, while a value of 1 indicates better power management. Assigning a value of 255 will disable APM altogether. By default, SSDs are assigned an APM of 254 when the system is running on external power. In battery mode, the APM level is set to 128, reducing the read and write speeds of SSDs. This article explains how to increase SSD APM levels to 254 when your Linux laptop is running on battery mode.
sudo hdparm -B254 /dev/sda
Get the current AMP value.
sudo hdparm -B /dev/sda
Test the performance.
sudo hdparm -tT /dev/sda
References
- AskUbuntu: Ubuntu SSD – Was fast, is now extremely slow
- LinuxHint.com: How to Improve SSD Performance in Linux Laptops
Enable TRIM/Discard in FSTab
TRIM (Trim command let an OS know which SSD blocks are not being used and can be cleared).
sudo nano /etc/fstab
#/dev/disk/by-uuid/09e7c8ed-fb55-4a44-8be4-18b1696fc714 / ext4 defaults 0 0
/dev/disk/by-uuid/09e7c8ed-fb55-4a44-8be4-18b1696fc714 / ext4 discard,async,noatime,nodiratime,errors=remount-ro 0 1
Warning: Users need to be certain that their SSD supports TRIM before attempting to use it. Data loss can occur otherwise! Tp test whether the SSD device supports TRIM/Discard option you can use either of the following commands.
sudo hdparm -I /dev/sda | grep TRIM
* Data Set Management TRIM supported (limit 8 blocks)
sudo smartctl --all /dev/sda | grep TRIM
TRIM Command: Available
To test does an NVMe supports TRIM (physical discard option) the output of the following command must be greater than 0.
References
- Arch Linux Wiki: Solid state drive
- Red Hat Documentation: Red Hat Enterprise Linux > 8 > Managing storage devices > Chapter 8. Discarding unused blocks
- How-To Geek: How to Tweak Your SSD in Ubuntu for Better Performance
- How-To Geek: What Is the Linux fstab File, and How Does It Work?
- itBeginner.net: How to tweak and optimize SSD for Ubuntu, Linux Mint
- Ask Ubuntu: How do I optimize the OS for SSDs?
Set the automatic TRIM job to daily
sudo systemctl enable fstrim.timer
sudo mkdir /etc/systemd/system/fstrim.timer.d
sudo nano /etc/systemd/system/fstrim.timer.d/override.conf
[Timer]
OnCalendar=
OnCalendar=daily
Reboot the system or do daemon-reload and check the updated value.
sudo systemctl daemon-reload
systemctl cat fstrim.timer
# /lib/systemd/system/fstrim.timer
[Unit]
Description=Discard unused blocks once a week
Documentation=man:fstrim
ConditionVirtualization=!container
[Timer]
OnCalendar=weekly
AccuracySec=1h
Persistent=true
[Install]
WantedBy=timers.target
# /etc/systemd/system/fstrim.timer.d/override.conf
[Timer]
OnCalendar=
OnCalendar=daily
Undo the change if you need.
sudo rm -v /etc/systemd/system/fstrim.timer.d/override.conf
References
Install 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.
sudo aptt update && sudo ap install preload
References:
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.
RAM | Swapiness | Comment |
---|---|---|
Default | 60 | cat /proc/sys/vm/swappiness
|
2GB | 30 | |
4GB | 10 | OK |
6GB or more | 0 | ? |
sudo nano /etc/sysctl.conf
vm.swappiness = 10
References:
Common References
- Arch Linux Wiki: Solid state drive (Need to read carefully!)
- Arch Linux Wiki: Improving performance (Need to read carefully!)
- Arch Linux Wiki: Ext4 (Need to read carefully!)
- Website for Students: Improve Nginx Cache Performance with
tmpfs
on Ubuntu - Easy Linux Tips Project: SSD: how to optimize your Solid State Drive for Linux Mint and Ubuntu
- AskUbuntu: Ubuntu SSD – Was fast, is now extremely slow
- Ask Ubuntu: do I optimize the OS for SSDs?
- LinuxHint.com: How to Improve SSD Performance in Linux Laptops
- How-To Geek: How to Tweak Your SSD in Ubuntu for Better Performance
- How-To Geek: What Is the Linux fstab File, and How Does It Work?
- itBeginner.net: How to tweak and optimize SSD for Ubuntu, Linux Mint
- itBeginner.net: How to tweak and optimize SSD for Ubuntu, Linux Mint