Skip to content

Instantly share code, notes, and snippets.

@furdarius
Last active June 3, 2024 16:12
Show Gist options
  • Save furdarius/94c178d437f56de19a115dc37941d972 to your computer and use it in GitHub Desktop.
Save furdarius/94c178d437f56de19a115dc37941d972 to your computer and use it in GitHub Desktop.
Linux system performance optimization

Ulimit

The ulimit is a mechanism for restrict the amount of various resources a process an consume. There's a shell command to set the ulimit for processes which are started in that shell.

If you are using bash as your shell, you can list the current ulimit settings by issuing this command in a terminal: ulimit -a

How to increase open file limits in linux

Increase system-wide limits:

Add to the end of /etc/security/limits.conf:

* hard nofile 131072
* soft nofile 65536

FileSystem optimizations

SSD Optimizations

The default I/O scheduler queues data to minimize seeks on HDDs, which is not necessary for SSDs. Thus, use the "deadline" scheduler that just ensures bulk transactions won't slow down small transactions.

Install sysfsutils: sudo apt install sysfsutils

Check what is your SSD name: for f in /sys/block/sd?/queue/rotational; do printf "$f is "; cat $f; done

/sys/block/sda/queue/rotational is 1
/sys/block/sdb/queue/rotational is 1
/sys/block/sdc/queue/rotational is 0   <=== Only this is SSD!

Add to the end of /etc/security/limits.conf:

block/sdX/queue/scheduler = deadline

(Adjust sdX to match your SSD)

/etc/fstab optimizations

Check what is your device name with mount command.

Edit /etc/fstab. Add options to your device:

  • noatime - option fully disables writing file access times to the drive every time you read a file. This works well for almost all applications, except for those that need to know if a file has been read since the last time it was modified. The write time information to a file will continue to be updated anytime the file is written to with this option enabled.
  • commit - Ext4 can be told to sync all its data and metadata every 'nrsec' seconds. The default value is 5 seconds. This means that if you lose your power, you will lose as much as the latest 5 seconds of work (your filesystem will not be damaged though, thanks to the journaling). This default value (or any low value) will hurt performance, but it's good for data-safety. Setting it to 0 will have the same effect as leaving it at the default (5 seconds). Setting it to very large values will improve performance.
Note: Don't use discardoptions.

The discard option is no longer suggested for SSD-hosted filesystems, as it slows things down because the Linux kernel has a non-optimized implementation of the command. (IIRC, TRIM requests only use one ATA sector range at a time, making it slow.)

Instead, create a daily cron job that runs fstrim -v / (or whatever your SSD partitions are).

Configure periodic TRIM for SSD

After finish your SSD device in /etc/fstab will looks like:

/dev/md/2 / ext4 defaults,noatime,commit=600 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment