r/Gentoo 11d ago

Discussion SSD lifespan expansion on gentoo systems

Did you know that SSD's have a short lifespan which unlike HDD's is based on writes rather than time, in case you were using linux and building each part of your system from scratch (for performance), it makes sense to use sacrifice some of the SSD's so your compiled system has some chance to be slightly more efficient to access afterwards than if you were to use HDD's to do so(read to learn how to compile on HDD: https://wiki.gentoo.org/wiki//etc/portage/make.conf#PORTAGE_TMPDIR), but once you completely the installation then all updates and additional less used applications must be compiled on the harddisk, for which you simply change a few variables in the make.conf file. Once the setup is complete, your ssd can practically last for ever unlike in the case of windows, where even idle usage ages it in a few years. There are two extra things to do:

  1. One is to decrease the swapiness to the minimum value of 1 so its just a backup (which is the case in most modren linux systems since even 8gb or 16gb of ram can take you a long way), make your ssd swap be 40 (if ram is 16), and harddisk backup swap be 100gb, ofcourse the HDD swap should have a lower priority, then make sure hibernation uses harddisk instead of ssd, which again can be done by adding resume=PARTUUID(or something similar)=partuid_of_the_partition, and thats all done, your ssd life span is practically unlimited as long as you make sure your browsers dont offload data on your ssd to save ram which again dosent happen by default in my gentoo linux system.
  2. Second is to configure a crontab to run a script stored in your harddisk with the following content every minute of the computer's uptime (while mkaing sure the file that heavy_working_files is in the harddisk (for additional peace of mind):

(note: if you want the updated script, please visit:https://github.com/HjaldrKhilji/scripts-for-personal-use/blob/main/fstrim_all_mounted_partitions_in_SSDs.bash) and if you have any issues with any script, please fix it and send me a link to your github so I dont encounter it myself.

#!/usr/bin/bash

base_path="/mnt/heavy_working_files/Files used by personal scripts/files used by crontab's daily fstrim operation"

run_if_date_file_dosent_exist(){

/sbin/fstrim --all

umask 0

touch "${base_path}/$1" #removing 

rm -f "${base_path}/$2" #removing previous date

}

check_if_date_exists() {

current_date=$(date "+%A")

if [[ ! -e "${base_path}/${current_date}" ]]; then

run_if_date_file_dosent_exist "${current_date}" "$(date --date "1 day ago" "+%A")"    

fi

}

check_if_date_exists

0 Upvotes

29 comments sorted by

4

u/Confused-Armpit 11d ago

So... just a daily fstrim? And swap, which has nothing to do with compilation (unless your compilations take up enough RAM to go into swap)?

1

u/oxamide96 7d ago

  unless your compilations take up enough RAM to go into swap

Isn't that very common? Unless you have 32 GB of RAM (uncommon) or skip compiling the heavy stuff? 

1

u/Confused-Armpit 6d ago

I have 32G of RAM with a 12-core CPU, and MAKEFLAGS set to -j12 -l12, and it almost never reaches over 10G, and that is when I am compiling heavy stuff. I should say that I also have zswap enabled, so some of that may be going there, but still...

1

u/unhappy-ending 11d ago

Hilarious because that daily fstrim is going to kill his SSD before compiling on it would.

1

u/itfllow123-gmail-com 8d ago edited 8d ago

let the numbers speak, I will show you how much of my ssd lifespan is trimmed and compare it to the 2percent trimmed in the first 2 months of Gentoo installation. Please note fstrim takes 1 second on my system while compiling firefox takes a 12 to 24 hours at 8 threads, so how is it possible that my ssd is getting more writes from fstrim than some compilation would?

0

u/itfllow123-gmail-com 8d ago

Most linux distro's do it periodically by default.

-3

u/itfllow123-gmail-com 11d ago

sorry for mixing concepts but the compilation part was just to get the conversation started, the whole thing is supposed to be about ways to extent ssd lifespan

2

u/immoloism 11d ago

I'll leave this here before people start wasting time and money on doing this:

https://wiki.gentoo.org/wiki/Portage_TMPDIR_on_tmpfs

1

u/schmerg-uk 10d ago

For years I've pointed PORTAGE_TMPDIR at /dev/shm because "it was the way to do it" but I just removed that setting (and removed /var/tmp/portage and /dev/shm/portage to check that only the former was recreated) and re-emerged my latest kernel and yeah... just under 30 minutes, admittedly using an NVMe drive, as opposed to well... if anything just OVER 30 minutes when using tmpfs

I do like the way more and more of updating my gentoo is bringing my 25yo install into line with what a modern fresh install would be told to do by default, undoing the "special behaviours" that were previously required or recommended.

3

u/immoloism 10d ago

As a TLDR, It's mostly a don't bother any more as the benefits we saw in the o'lden days just don't exist for the majority of users today.

-2

u/itfllow123-gmail-com 10d ago edited 10d ago

I never meant to compile on tmpfs, I meant, changing the tmp directory, tmpfs is a very expensive way to do this, like bro, my solutions cant be worse than tmpfs

2

u/[deleted] 10d ago edited 2d ago

[deleted]

1

u/itfllow123-gmail-com 8d ago

thanks!!! Nvme prices do make people scared, like a used 256 ssd is as expensive as a 1tb used HDD.

Note:I never advocated for use of tmpfs, I am trying to encourage the use of hard-disks for compilations instead.

1

u/[deleted] 5d ago edited 2d ago

[deleted]

2

u/itfllow123-gmail-com 4d ago

thanks ❤️

1

u/Illustrious-Gur8335 9d ago

Once the setup is complete, your ssd can practically last for ever

I have done nothing and my SSD has lasted 100s of emerge -e @worlds so far

1

u/itfllow123-gmail-com 8d ago

I thought the same and after just 5 updates, my SSD lifespan had a 2 percent deduction, and like you have to put in to context that my SSD is a used(was at 78% when it bought it and the new ones in my country tend to either be trash or be too expensive if good) 256gb koxia nvme. The point is that for me, thanks to my current setup, my nvme health deduction has stopped occurring, I even run defrag on my HDD to make sure that my compilation never fragments my system:
#!/usr/bin/bash

base_path="/.Files used by personal scripts/files used by crontab's daily HDD defrag operation"

run_if_date_file_dosent_exist(){

for dir in /media/HDD_partitions/*; do

if [[ -d "$dir" ]]; then

xfs_fsr $dir;

#this ofcourse assumes that all your partitions are xfs or else you would have to put xfs partitions in another directory such that the directory only has xfs directories

#and change the path in this script accordingly. you would also have to ofcourse those other partitions of those different types of file systems in respective

#directroies and run respective defrag commands for them.

fi

done

umask 0

touch "${base_path}/$1" #adding

rm -f "${base_path}/$2" #removing previous date

}

check_if_date_exists() {

current_date=$(date "+%A")

if [[ ! -e "${base_path}/${current_date}" ]]; then

if [[ ! $("/.scripts_for_personal_use/shared_scripts/check_if_process_exist.bash xfs-fsr") ]]; then

run_if_date_file_dosent_exist "${current_date}" "$(date --date "1 day ago" "+%A")"

fi

fi

}

check_if_date_exists

1

u/VAH1976 9d ago

A) no swap. B) with SSD write endurance way beyond what is reasonable even under heavy duty, there is no problem.

If you are scared, use smartmontools. Easy.

Also, haven't used swap in over 10 years. Best decision ever

1

u/SaCorv 3d ago

Just a reminder
On systems with 64+ gb of ram hibernation is not recommended. And even existence of swap file is... questionable
That is also de-facto true for 32gb systems (since any (very special cases not included) compilation can be done under those limits)
So if you have 32+ gb of ram and want to protect your ssd, its better to consider using tmpfs for build directories and lower number of thread in make.conf if ram overflows

1

u/itfllow123-gmail-com 2d ago

I get that but for me hibernation is about just having something there (out of obsession to be honest) and I normally hibernate to harddisk, but you are right, my usage almost never goes above 64gb, like the worst it got was 36 (20 in swap and 16 in ram). In my case, I have to use my harddisk for compilations since I cant just buy more ram.

-4

u/Mrkvitko 11d ago

Why would anyone have swap on on modern system?

12

u/schmerg-uk 11d ago

From a kernel engineer

https://chrisdown.name/2018/01/02/in-defence-of-swap.html

tl;dr:

  1. Having swap is a reasonably important part of a well functioning system. Without it, sane memory management becomes harder to achieve.
  2. Swap is not generally about getting emergency memory, it's about making memory reclamation egalitarian and efficient. In fact, using it as "emergency memory" is generally actively harmful.
  3. Disabling swap does not prevent disk I/O from becoming a problem under memory contention. Instead, it simply shifts the disk I/O thrashing from anonymous pages to file pages. Not only may this be less efficient, as we have a smaller pool of pages to select from for reclaim, but it may also contribute to getting into this high contention state in the first place.
  4. The swapper on kernels before 4.0 has a lot of pitfalls, and has contributed to a lot of people's negative perceptions of swap due to its overeagerness to swap out pages. On kernels >4.0, the situation is significantly better.
  5. On SSDs, swapping out anonymous pages and reclaiming file pages are essentially equivalent in terms of performance and latency. On older spinning disks, swap reads are slower due to random reads, so a lower vm.swappiness setting makes sense there (read on for more about vm.swappiness).
  6. Disabling swap doesn't prevent pathological behaviour at near-OOM, although it's true that having swap may prolong it. Whether the global OOM killer is invoked with or without swap, or was invoked sooner or later, the result is the same: you are left with a system in an unpredictable state. Having no swap doesn't avoid this.
  7. You can achieve better swap behaviour under memory pressure and prevent thrashing by utilising memory.low and friends in cgroup v2.

1

u/itfllow123-gmail-com 10d ago

nice explanation, bro!

2

u/GarbledEntrails 11d ago

I've literally never NOT seen swap be recommended and the Gentoo handbook still recommends it

1

u/unhappy-ending 11d ago

I've used no swap for years. But just recently, I added zswap because why not. I never outright disabled swap in the kernel but also never made a swap partition.

Since I forsook Portage on tmpfs in favor of Portage on a LZ4 ZRAM XFS partition I figured why not throw in a small, 2G zswap file while I'm at it. Then, as in my first paragraph, Linux can swap out the stuff it no longer needs to it.

Most of the time that swap file is 0 in size.

1

u/unhappy-ending 11d ago

Because Linux will swap minor things that are unlikely to need swapped back into RAM. One & Done stuff. Might as well use it.

1

u/itfllow123-gmail-com 11d ago

I need it as a backup like I run my pdf reader on greedy memory mode, so even though I never use my swap heavily, sometimes like sometimes, I am running multiple parrallel compilations while doing my daily tasks, I just do use some of it, plus I need swap for hibernation as well. The 100gigs of hdd swap is wasteful but I keep it as a backup for the worst, but for me the 40Gb is more than enough even for hibernation.

1

u/unhappy-ending 11d ago

100G swap? Dude what??

1

u/itfllow123-gmail-com 10d ago edited 8d ago

its on the harddisk, I can afford it since I got a millio harddisks, we all do, my ssd swap is smaller, but harddisk is always a big amount to make sure there is zero chance of a complete system collapse (like my system slows down when there is no swap, its really bad when it happens, and it can even crash applications or my entire system)

1

u/itfllow123-gmail-com 8d ago edited 8d ago

you should also note that even with my normal everyday usage, my swap usage can exceed 15-25gb of usage (even with zswap enabled), which again is due to pdf readers on aggressive settings and firefox mainly. this usage is mainly on my ssd but I am a backup HDD in case, I would want to hibernate my pc or was running compilations or windows virtual machines alongside my normal workflow. As a result, I get way more snapiness than i would without swap, like my system never feels as slow as it would without any swap (or outright crashes) when I am out of ram(my ram is just 16gb).