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

From WikiMLT
Line 24: Line 24:
* Unix and Linux: [https://unix.stackexchange.com/q/652623 How to evaluate the wear level of a NVMe SSD?]
* Unix and Linux: [https://unix.stackexchange.com/q/652623 How to evaluate the wear level of a NVMe SSD?]
* NVMexpress.org: [https://nvmexpress.org/ NVMe Current Specifications]
* NVMexpress.org: [https://nvmexpress.org/ NVMe Current Specifications]
== 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">
sudo hdparm -B254 /dev/sda
</syntaxhighlight>Get the current AMP value.<syntaxhighlight lang="shell" line="1">
sudo hdparm -B /dev/sda
</syntaxhighlight>
Test the performance.<syntaxhighlight lang="shell" line="1">
sudo hdparm -tT /dev/sda
</syntaxhighlight>
=== References ===
* [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]


== Enable TRIM/Discard in FsTab ==
== Enable TRIM/Discard in FsTab ==

Revision as of 08:31, 18 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

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 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

Re­boot the sys­tem or do dae­mon-re­load and check the up­dat­ed val­ue.

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

Un­do the change if you need.

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

Ref­er­ences

See al­so