SSD/NVMe Tweaks (TRIM/Discard): Difference between revisions

From WikiMLT
m (Стадий: 3 [Фаза:Разработване, Статус:Разработван]; Категория:Linux Desktop)
 
mNo edit summary
Line 1: Line 1:
<noinclude><!--[[Category:Linux_Desktop|?]]-->{{ContentArticleHeader/Linux_Desktop}}</noinclude>
<noinclude><!--[[Category:Linux_Desktop|?]]-->{{ContentArticleHeader/Linux_Desktop}}</noinclude>
Note, within the examples in the following sections, <code>/dev/sda</code> refers to a SSD device while <code>/dev/nvme0n1</code> refers to a NVMe device.


'''Get Full SSD Info'''<syntaxhighlight lang="shell" line="1" class="root-prompt">
== Get Full SSD/NVMe Device Info ==
smartctl --all /dev/sda
For SSD devices use:<syntaxhighlight lang="shell" line="1" class="root-prompt">
hdparm -I /dev/sda
smartctl -x /dev/sda           # -x, --xall; -a, --all
hdparm -I /dev/sda             # doesn't support NVMe
</syntaxhighlight>For NVMe devices use:<syntaxhighlight lang="shell" line="1" class="root-prompt">
smartctl -x /dev/nvme0n1        # -x, --xall; -a, --all
nvme smart-log -H /dev/nvme0n1  # apt install nvme-cli
</syntaxhighlight>
</syntaxhighlight>


'''Tweak the AMP value'''
== 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.<syntaxhighlight lang="shell" line="1">
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.<syntaxhighlight lang="shell" line="1">
sudo hdparm -B254 /dev/sda
sudo hdparm -B254 /dev/sda
Line 17: Line 22:
</syntaxhighlight>
</syntaxhighlight>


References:
=== References ===
 
* [https://askubuntu.com/a/879291/566421 AskUbuntu: Ubuntu SSD - Was fast, is now extremely slow]
* [https://askubuntu.com/a/879291/566421 AskUbuntu: Ubuntu SSD - Was fast, is now extremely slow]
* [https://linuxhint.com/improve_ssd_performance_linux_laptops/ LinuxHint.com: How to Improve SSD Performance in Linux Laptops]
* [https://linuxhint.com/improve_ssd_performance_linux_laptops/ LinuxHint.com: How to Improve SSD Performance in Linux Laptops]


'''Enable TRIM'''
== Enable TRIM/Discard in FSTab ==
 


TRIM (Trim command let an OS know which SSD blocks are not being used and can be cleared).<syntaxhighlight lang="shell" line="1">
TRIM (Trim command let an OS know which SSD blocks are not being used and can be cleared).<syntaxhighlight lang="shell" line="1">
Line 30: Line 35:
/dev/disk/by-uuid/09e7c8ed-fb55-4a44-8be4-18b1696fc714 / ext4 discard,async,noatime,nodiratime,errors=remount-ro 0 1
/dev/disk/by-uuid/09e7c8ed-fb55-4a44-8be4-18b1696fc714 / ext4 discard,async,noatime,nodiratime,errors=remount-ro 0 1


</syntaxhighlight>'''Warning:''' Users need to be certain that their SSD supports TRIM before attempting to use it. Data loss can occur otherwise! Tp test wheatear the SSD device supports TRIM/Discard option you can use either of the following commands. <syntaxhighlight lang="shell" line="1">
</syntaxhighlight>'''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. <syntaxhighlight lang="shell" line="1">
sudo hdparm -I /dev/sda | grep TRIM
sudo hdparm -I /dev/sda | grep TRIM
</syntaxhighlight><syntaxhighlight lang="bash">
</syntaxhighlight><syntaxhighlight lang="bash">
Line 40: Line 45:
</syntaxhighlight>
</syntaxhighlight>


References:
To test does an NVMe supports TRIM (physical discard option) the output of the following command must be greater than 0.


* [https://wiki.archlinux.org/title/Solid_state_drive#Continuous_TRIM '''Arch Linux Wiki: Solid state drive''']
=== References ===
* Arch Linux Wiki: [https://wiki.archlinux.org/title/Solid_state_drive#Continuous_TRIM '''Solid state drive''']
* Red Hat Documentation: [https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/managing_storage_devices/discarding-unused-blocks_managing-storage-devices Red Hat Enterprise Linux > '''8''' > Managing storage devices > Chapter 8. '''Discarding unused blocks''']


* [https://www.howtogeek.com/62761/how-to-tweak-your-ssd-in-ubuntu-for-better-performance/ How-To Geek: '''How to Tweak Your SSD in Ubuntu for Better Performance''']
* How-To Geek: [https://www.howtogeek.com/62761/how-to-tweak-your-ssd-in-ubuntu-for-better-performance/ '''How to Tweak Your SSD in Ubuntu for Better Performance''']
* [https://www.howtogeek.com/howto/38125/htg-explains-what-is-the-linux-fstab-and-how-does-it-work/ How-To Geek: '''What Is the Linux fstab File, and How Does It Work?''']
* How-To Geek: [https://www.howtogeek.com/howto/38125/htg-explains-what-is-the-linux-fstab-and-how-does-it-work/ '''What Is the Linux fstab File, and How Does It Work?''']
* [https://itbeginner.net/tweak-optimize-ssd-ubuntu-linux-mint/ itBeginner.net: How to tweak and optimize SSD for Ubuntu, Linux Mint]
* itBeginner.net: [https://itbeginner.net/tweak-optimize-ssd-ubuntu-linux-mint/ How to tweak and optimize SSD for Ubuntu, Linux Mint]
* [https://askubuntu.com/questions/1400/how-do-i-optimize-the-os-for-ssds Ask Ubuntu: do I optimize the OS for SSDs?]
* Ask Ubuntu: [https://askubuntu.com/questions/1400/how-do-i-optimize-the-os-for-ssds How do I optimize the OS for SSDs?]
*


'''Set the automatic TRIM job to daily'''<syntaxhighlight lang="shell" line="1">
== Set the automatic TRIM job to daily ==
<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 79: Line 88:
</syntaxhighlight>Undo the change if you need.<syntaxhighlight lang="shell" line="1">
</syntaxhighlight>Undo the change if you need.<syntaxhighlight lang="shell" line="1">
sudo rm -v /etc/systemd/system/fstrim.timer.d/override.conf
sudo rm -v /etc/systemd/system/fstrim.timer.d/override.conf
</syntaxhighlight>References:
</syntaxhighlight>


=== References ===
* [https://easylinuxtipsproject.blogspot.com/p/ssd.html Easy Linux Tips Project: SSD: how to optimize your Solid State Drive for Linux Mint and Ubuntu]
* [https://easylinuxtipsproject.blogspot.com/p/ssd.html Easy Linux Tips Project: SSD: how to optimize your Solid State Drive for Linux Mint and Ubuntu]


'''Install Preload'''
== 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.<syntaxhighlight lang="shell" line="1">
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.<syntaxhighlight lang="shell" line="1">
Line 91: Line 102:
* [https://itbeginner.net/tweak-optimize-ssd-ubuntu-linux-mint/ itBeginner.net: How to tweak and optimize SSD for Ubuntu, Linux Mint]
* [https://itbeginner.net/tweak-optimize-ssd-ubuntu-linux-mint/ itBeginner.net: How to tweak and optimize SSD for Ubuntu, Linux Mint]


'''Swap and Swapiness'''
== 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.
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.
{| class="wikitable"
{| class="wikitable"
Line 125: Line 135:
* [https://itbeginner.net/tweak-optimize-ssd-ubuntu-linux-mint/ itBeginner.net: How to tweak and optimize SSD for Ubuntu, Linux Mint]
* [https://itbeginner.net/tweak-optimize-ssd-ubuntu-linux-mint/ itBeginner.net: How to tweak and optimize SSD for Ubuntu, Linux Mint]


'''Common References'''
== Common References ==
 
* [https://wiki.archlinux.org/title/Solid_state_drive#Continuous_TRIM '''Arch Linux Wiki: Solid state drive'''] (Need to read carefully!)
* [https://wiki.archlinux.org/title/Solid_state_drive#Continuous_TRIM '''Arch Linux Wiki: Solid state drive'''] (Need to read carefully!)
* [https://wiki.archlinux.org/title/Improving_performance#Storage_devices Arch Linux Wiki: Improving performance] (Need to read carefully!)
* [https://wiki.archlinux.org/title/Improving_performance#Storage_devices Arch Linux Wiki: Improving performance] (Need to read carefully!)

Revision as of 15:50, 17 August 2022

Note, with­in the ex­am­ples in the fol­low­ing sec­tions, /​​​dev/​​​sda refers to a SSD de­vice while /​​​dev/​​​nvme0n1 refers to a NVMe de­vice.

Get Full SSD/​​​NVMe De­vice In­fo

For SSD de­vices use:

smartctl -x /dev/sda            # -x, --xall; -a, --all
hdparm -I /dev/sda              # doesn't support NVMe

For NVMe de­vices use:

smartctl -x /dev/nvme0n1        # -x, --xall; -a, --all
nvme smart-log -H /dev/nvme0n1  # apt install nvme-cli

Tweak the AMP val­ue of SSD

Most Lin­ux dis­tri­b­u­tions use Lin­ux Kernel’s “Ad­vanced Pow­er Man­age­ment (APM)” API to han­dle con­fig­u­ra­tion, op­ti­mize per­for­mance, and en­sure sta­bil­i­ty of stor­age de­vices. These de­vices are as­signed an APM val­ue be­tween 1 and 255 to con­trol their pow­er man­age­ment thresh­olds. A val­ue of 254 in­di­cates best per­for­mance, while a val­ue of 1 in­di­cates bet­ter pow­er man­age­ment. As­sign­ing a val­ue of 255 will dis­able APM al­to­geth­er. By de­fault, SS­Ds are as­signed an APM of 254 when the sys­tem is run­ning on ex­ter­nal pow­er. In bat­tery mode, the APM lev­el is set to 128, re­duc­ing the read and write speeds of SS­Ds. This ar­ti­cle ex­plains how to in­crease SSD APM lev­els to 254 when your Lin­ux lap­top is run­ning on bat­tery mode.

sudo hdparm -B254 /dev/sda

Get the cur­rent AMP val­ue.

sudo hdparm -B /dev/sda

Test the per­for­mance.

sudo hdparm -tT /dev/sda

Ref­er­ences

En­able TRIM/​​​Discard in FSTab

TRIM (Trim com­mand let an OS know which SSD blocks are not be­ing 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

Warn­ing: Users need to be cer­tain that their SSD sup­ports TRIM be­fore at­tempt­ing to use it. Da­ta loss can oc­cur oth­er­wise! Tp test whether the SSD de­vice sup­ports TRIM/​​​Discard op­tion you can use ei­ther of the fol­low­ing com­mands.

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 sup­ports TRIM (phys­i­cal dis­card op­tion) the out­put of the fol­low­ing com­mand must be greater than 0.

Ref­er­ences

Set the au­to­mat­ic TRIM job to dai­ly

sudo mkdir /etc/systemd/system/fstrim.timer.d
sudo nano /etc/systemd/system/fstrim.timer.d/override.conf
[Timer]
OnCalendar=
OnCalendar=daily

Re­boot and check the up­dat­ed val­ue.

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

Un­do the change if you need.

sudo rm -v /etc/systemd/system/fstrim.timer.d/override.conf

Ref­er­ences

In­stall Pre­load

Pre­load is a Lin­ux soft­ware de­vel­oped by Be­hdad Es­fah­bod. Pre­load learns pro­grams that users use of­ten, records sta­t­ics us­ing Markov chains, an­a­lyzes, and pre­dicts what pro­grams will be most used. Pre­load then will load those pro­grams, bi­na­ries, and de­pen­den­cies in­to mem­o­ry or ram. By hav­ing pro­grams al­ready in RAM or mem­o­ry, it will take less time when you ac­tu­al­ly start that pro­gram or pro­grams.

sudo aptt update && sudo ap install preload

Ref­er­ences:

Swap and Swap­i­ness

Swap­pi­ness is a part of Lin­ux ker­nel that let you con­trol how much swap (vir­tu­al mem­o­ry) file is be­ing used. Swap­pi­ness val­ues can be changed from 0 to 100. The high­er swap­pi­ness val­ues the more Lin­ux ker­nel will try to use swap space, the low­er swap­pi­ness val­ues means lin­ux ker­nel will use­less or try not to use swap space de­pends on our set­ting. The de­fault swap­pi­ness val­ue from lin­ux ker­nel is 60, if your sys­tem have plen­ty have RAM, you should avoid us­ing swap space which writes and reads will be on your SSD or hard dri­ve. For sys­tem with 4 GB or more RAM, I would sug­gest to re­duce the us­age of swap by chang­ing swap­pi­ness set­tings to be­tween 10 even 0.

RAM Swap­i­ness Com­ment
De­fault 60 cat /​​​proc/​​​sys/​​​vm/​​​swappiness
2GB 30
4GB 10 OK
6GB or more 0 ?
sudo nano /etc/sysctl.conf
vm.swappiness = 10

Ref­er­ences:

Com­mon Ref­er­ences