[other] deepin如何为SSD固态硬盘开启优化
Tofloor
poster avatar
const/4 v1,0
deepin
2023-12-10 00:37
Author

很多情况下,linux需要对SSD固态硬盘进行优化,以提升效率和寿命。deepin下如何对固态硬盘优化呢?

Reply Favorite View the author
All Replies
neko
deepin
Ecological co-builder
2023-12-10 00:42
#1

elevator=noop 优化感觉不明显

image.png

Reply View the author
mozixun
deepin
2023-12-10 01:21
#2

定期fstrim一下即可

Reply View the author
毛毛虫
deepin
2023-12-10 01:35
#3

@const/4 v1,0 开启fstrim定时任务,一周自动清理一次,方便吧

sudo systemctl enable --now fstrim.timer

Reply View the author
const/4 v1,0
deepin
2023-12-10 01:54
#4

Linux环境下的SSD优化

前提

升级到最新的Linux发行版本(主要是Kernel)

升级到最新的SSD Firmware

使用sudo smartctl -a /dev/sda命令查看Firmware版本。

使用Ext4文件系统

btrfs 虽然支持专门的SSD mountc参数,但是本身文件系统的稳定性还不高。

开启BIOS AHCI

有条件的加满RAM,因为它比SSD便宜。

配置RAMDISK可以有效的将SWAP操作减少。参见Reduction of SSD write frequency via RAMDISK

不要使用TLC芯片的SSD

不要做碎片整理操作Defragmentation

不建议开启hibernation休眠功能,因为会有大量的数据读写。但是从笔记本使用角度来说,还是开着吧,关了也要操作很多配置。

开启磁盘的TRIM功能:

Linux对文件的删除只是删除对数据的指向,所以文件恢复非常方便。在删除数据后,文件系统在了解到这些存储空间的释放后,会对其进行重新分配。HDD在对这些空间的数据重写上效率很高,但是SSD就慢很多。SSD具有非常高效的写操作速度,但是对已有数据的重写速度比较忙。TRIM可以定期的将删除文件清除掉,避免重写过程,释放出空间,保证SSD的高效写操作。如果SSD空间充足,可以不必开启TRIM。

引用资料描述:

An SSD organizes data internally into 4k pages and groups 128 pages into a 512k block. SSDs can write only into empty 4k pages and erase in big 512k block increments. This means that although SSDs can write very quickly, overwriting is a much slower process. The TRIM command keeps your SSD running at top speed by giving the filesystem a way to tell the SSD about deleted pages. This gives the drive a chance to do the slow overwriting procedures in the background, ensuring that you always have a large pool of empty 4k pages at your disposal.

方法1-修改/etc/rc.local文件 推荐

在最后一条命令exit 0 前增加如下内容:

fstrim -v /

/为root分区(SSD硬盘分区)

不建议使用fstrim-all命令,在非三星和Intel SSD上会有性能瓶颈,参考URL:deepin 2014对SSD支持如何

方法2-cron

echo -e "#\x21/bin/sh\nfstrim -v /" | sudo tee /etc/cron.daily/trim

sudo chmod +x /etc/cron.daily/trim

方法3-修改/etc/fstab文件

修改SSD相关分区条目,增加discard 和 noatime参数

/dev/sda1 / ext4 discard,noatime,commit=600,errors=remount-ro 0 1

discard参数启动SSD的TRIM功能,可以提升性能和使用持久性。

notime参数告诉文件系统不要记录文件的最后访问(读取)时间,只记录最后修改时间。可以有效减少对磁盘的读写次数,因为访问频率相对修改来说非常多。

PS:

1.如果发现noatime参数影响了某些应用的使用,可以修改notime为relatime,将会让文件系统将最后修改时间作为文件的最后访问时间。

2.不推荐这个方式(只针对discard参数,noatime还是推荐的)。The disadvantage of this method is, that it may cause the system to slow down. Because it forces the system to apply TRIM instantly on every file deletion. That's why this method is not my favourite.

分区对齐

SSD硬盘内部的操作是512k的块大小。在SSD刚刚发布的时候,磁盘分区系统可能会有分区对其的问题,现在的版本都支持SSD的512k分区范围了:

fdisk uses a one megabyte boundary since util-linux version 2.17.1 (January 2010).

LVM uses a one megabyte boundary as the default since version 2.02.73 (August 2010).

建议使用fdisk, fdisk 会预留 2048 个扇区,gdisk 却是从 64 扇区开始分。 。

一个例子看一下512k对其的效果:

~$ sudo sfdisk -d /dev/sda

Warning: extended partition does not start at a cylinder boundary.

DOS and Linux will interpret the contents differently.

partition table of /dev/sda

unit: sectors

/dev/sda1 : start= 2048, size= 497664, Id=83, bootable

/dev/sda2 : start= 501758, size=155799554, Id= 5

/dev/sda3 : start= 0, size= 0, Id= 0

/dev/sda4 : start= 0, size= 0, Id= 0

/dev/sda5 : start= 501760, size=155799552, Id=83

每一个分区的开始和结束都是可以整除512的。

减少SWAP读写频率

完全不使用SWAP将会导致hibernation(休眠)机制失效,所以最好的方案就是将swappiness 值修改为最小,最小化SWAP分区的操作。这样Linux会优先使用RAM,然后才是SSD。

$ sudo vim /etc/sysctl.d/99-sysctl.conf

vm.swappiness = 1

vm.vfs_cache_pressure = 50

vm.swappiness=0太激进,有可能会导致内存不够用。

重启后生效

更换低延迟 IO-Scheduler

默认的IO调度器CFQ(Copletely Fair Queuing)是针对HDD的优化,对多个读操作进行分组队列。但是SSD的读取效率非常高,完全不必要分组排队,使用一个队列就可以了。建议更换为:

NOOP(当系统只有SSD的情况下非常建议)

Deadline模式

配置文件/etc/default/grub

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash video-1024x768M@75m"

修改为:

GRUB_CMDLINE_LINUX_DEFAULT="elevator=noop quiet splash video-1024x768M@75m"

更新grub配置:grub-mkconfig -o /boot/grub/grub.cfg

插播小技巧

如果grub需要改变分辨率,修改/etc/default/grub

GRUB_GFXMODE=1024x768

定期检查SSD状态,并做数据备份

可以使用命令sudo smartctl -data -A /dev/sda查看SSD状态,观察寿命。

maurits@nuc:~$ sudo smartctl -data -A /dev/sda

smartctl 5.41 2011-06-09 r3365 [x86_64-linux-3.8.0-26-generic] (local build)

Copyright (C) 2002-11 by Bruce Allen, http://smartmontools.sourceforge.net

=== START OF READ SMART DATA SECTION ===

SMART Attributes Data Structure revision number: 18

Vendor Specific SMART Attributes with Thresholds:

ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE

1 Raw_Read_Error_Rate 0x0000 006 000 000 Old_age Offline - 6

3 Spin_Up_Time 0x0000 100 100 000 Old_age Offline - 0

4 Start_Stop_Count 0x0000 100 100 000 Old_age Offline - 0

5 Reallocated_Sector_Ct 0x0000 100 100 000 Old_age Offline - 0

9 Power_On_Hours 0x0000 100 100 000 Old_age Offline - 2592

12 Power_Cycle_Count 0x0000 100 100 000 Old_age Offline - 258

232 Available_Reservd_Space 0x0000 100 100 000 Old_age Offline - 4914564640

233 Media_Wearout_Indicator 0x0000 100 000 000 Old_age Offline - 100

maurits@nuc:~$

233一行的值就是寿命,默认为100,当小于10的时候就要非常注意了。

更加深入的优化

Solid State Drive (SSD): optimize it for Ubuntu 14.04, Linux Mint 17.1 and Debian

Reply View the author
neko
deepin
Ecological co-builder
2023-12-10 18:15
#5
毛毛虫

@const/4 v1,0 开启fstrim定时任务,一周自动清理一次,方便吧

sudo systemctl enable --now fstrim.timer

fstrim默认就是开启的

Reply View the author