Sunday, July 24, 2011

SSD Speed Tweaks


Solid State Drives (SSDs) have a great potential to speed up your system, and they're becoming mainstream with prices dropping and the technology maturing. SSDs are faster, have no moving parts, use less power, are quieter, less succeptible to physical shock, with lower access time and latency than traditional mechanical hard disk drives (HDDs).
However, SSDs use memory chips to store data, which makes them quite different than traditional mechanical drives and most current OSes are still not tuned by default to use this type of storage media to its full potential. It may take a bit of knowledge and tinkering to setup and tune the SSDs and squeeze that last bit of potential and  and improve their reliability.
This article aims at some general principles in improving the speed and reliability of Solid State Drives, discussing some  tweaking recommendations. Even though most tweaks are illustrated under Windows 7/Vista, the general ideas can be applied to any OS. In a couple of words, the main idea behind SSD tweaking for performance revolves around partition alignment, enabling TRIM, reducing unnecessary writes (very fast drives with somewhat limited write cycles), and freeing up space where possible. Note that newer generation, and more expensive SSDs have improved write endurance.
Windows 7 is better at recognizing and working with Solid State Drives (SSD) than previous versions of Windows. It has the ability to detect SSDs and set the disk defragmentation service to manual, even use the TRIM command on some drives. Even with these basic optimization steps, there is always some room for improvement, as outlined below.

SSD Partition Alignmentincrease write speed and lifespan
SSDs are divided into blocks composed of multiple pages, and each flash block must be fully erased prior to re-writing. The write speed will suffer greatly if the SSD controller has to perform unnecessary block erase operations due to partition misalignment. Proper partition alignment is one of the most critical attributes that can greatly boost the I/O performance of an SSD (and increase its lifespan) due to reduced read-modify-write operations.
In essense, you simply have to align the partitions for best performance. I don't know of an easy way to realign a partition after the drive has been formatted (short of ghosting a disk image, aligning the partition and restoring it).
Some geeky technical info
MLC NAND flash drives generally have 512KB erase block size (128 pages * 4KB per page = 512KB per block). Filesystems like NTFS usually have 4KB (4096 bytes) default cluster size.
To align the partition of a single drive with a single NAND flash controller, you should use 512KB.
For a single drive with dual NAND flash controllers, you should double the sectors to 1024KB.
For RAID 0 with each drive having a single controller, you match this to the stripe size of the RAID array -> 128KB stripe = 256KB block offset.
For RAID 0 with each drive having a dual controller, you match to the (double) stripe size again -> 256KB stripe = 512KB block offset.
In fact, for best performance the partition offset should allign with (by being a multiple of) all three of the following:
  • NAND Page Size - generally 4KB
  • NAND Erase Block Size - generally 512KB
  • File Allocation Unit (cluster) size - 4KB by default in NTFS
Windows SSD alignment
To create an aligned SSD partition under Windows Vista/7, open elevated command prompt and run the following sequence of commands (each line followed by enter):
diskpart
list disk
select disk N
      (where "N" is the number for your SSD in the "list disk" command)
create partition primary align=1024
active
exit
Note: to create a partition of specific size (100MB), use: create partition primary size=100 align=1024
To verify the alignment:
diskpart
list disk
delect disk N    
(where "N" is the number for your SSD in the "list disk" command)
list partition
You should see a result similar to this:
  Partition ###  Type              Size     Offset
  -------------  ----------------  -------  -------
  Partition 1    Primary            128 GB  1024 KB
512KB or 1024KB is best, even though any multiple of 64KB should be somewhat ok (aligns to two out of three parameters, with the exception of erase block size).
Under Linux, you'd have to use something like: 32*7=224 heads and 8*7=56 sectors/cylinder for 256*49=12544 sectors/cylinder, so that each cylinder is 49*128K.
# fdisk -H 224 -S 56 /dev/sda
Then, turn on experd mode with fdisk and force the partition to begin on 512KB alignment.
Notes:
Windows 7 should automatically align SSDs properly if using the install DVD, but not when partitioning within Windows.
Windows XP will not align partitions.

TRIM Supportavoid write performance degradation over time
Traditionally, when you delete data on a disk, the data isn't immediately scrubbed, just marked as overwriteable. Given the way SSDs store and overwrite data, it is advantageous for the drive to use the TRIM command and scrub deleted data instead of performing an entire block erase when you are writing new data.
In essense, TRIM support handles deletes/writes in a way that avoids write performance degradation over time to the involved blocks. As an added benefit, the TRIM operaton can help reduce wear by reducing the number of merge operations on a SSD drive. You can think of it as the SSD equivalent of defrag/disk cleanup for SSDs that support it.
For the TRIM command to work, it has to be supported both by the SSD and your OS. This may require firmware upgrade of older first-generation SSDs. It can sometimes be accomplished with third-party software for older OSes (and even drives) that do not support it natively.
TRIM Support by OS:
Windows 7 and Windows Server 2008 R2 support TRIM out of the box since October 2009.
Linux has TRIM support since kernel version 2.6.33 (February 2010).
FreeBSD and OpenSolaris got TRIM support in July 2010.
Max OS X supports TRIM since 10.6.6, but in both Lion and Snow Leopard it only works for Apple-supplied SSDs (there is an unsupported method to make it work on any SSD, using a program called "TRIM Enabler").
To check whether TRIM support is enabled under Windows 7, in elevated command prompt, type:
fsutil behavior query disabledeletenotify
DisableDeleteNotify = 0  (Windows TRIM commands are enabled)
DisableDeleteNotify = 1  (Windows TRIM commands are disabled)
You may also need to verify that your ATA/ATAPI AHCI controller is using a TRIM compliant driver. For Intel chipsets, instead of the default MS AHCI chipsed drivers, download and use the newer Intel chipset drivers 9.6.0.1014 or later for TRIM support. For non-Intel chipsets, navigate to:
Control Panel -> System and Security -> System -> Device Manager -> expand IDE ATA/ATAPI controllers -> verify that your SSD controller is "Standard AHCI 1.0 Serial ATAController". If not, double click it and select the Driver tab -> Click update driver -> browse -> let me pick -> select "Standard AHCI 1.0 Serial ATA Controller", then reboot.

Disable Indexing
Increase System Speed

Under most current versions of Windows, Indexing creates and maintains a database of file attributes of hard drives. This can lead to multiple small writes when creating/deleting/modifying files. Searching for files works with indexing off, just a bit slower. You can turn indexing off for all drives, or just for the SSD at your discretion.
Instructions:
Start Menu -> click on Computer -> right-click on the SSD drive -> choose "Properties" -> untick "Allow files on this drive to have contents indexed in addition to file properties"
Alternatively, you can stop the indexing service for all drives:
Start Menu -> right-click Computer -> Manage -> Services and Applications -> Services - > right-click Windows Search -> Startup type: Disabled -> OK

Disable Defragmentation
Defragmenting a hard disk's used space is only useful on mechanical disks with multi-millisecond latencies. Free-space defragmentation for SSDs is not available in the default Windows Defragmenter. Also, defragmenting SSDs may theoretically shorten the lifespan of the drive. To check if your SSD is scheduled for defragmenation, or turn off the service, follow the instructions below.
Start Menu > type: dfrgui in the search dialog -> highlight your SSD drive in the list -> click "Configure schedule" -> untick "Run on a schedule"
Alternatively, to stop the service if you only have a SSD in the system:
Start Menu -> right-click Computer -> Manage -> Services and Applications -> Services - > right-click Disk Defragmenter -> Startup type: Disabled -> OK
Note: Windows 7 should turn defragmentation for SSDs off by default, but it should be verified. 

Disable/Move the Page File
Save disk space, reduce writes 
As drives have a limited number of write-cycles, it is generally a good idea to reduce unnecessary writes to your fast/expensive SSD. It has the added benefit of freeing up over 2GB of disk space.
To set a custom size (or disable) the Page file:
Start Menu -> right-click Computer -> choose Properties -> Advanced System Settings (left pane) -> Performance / Settings -> Advanced tab -> Change -> untick "Automatically manage" -> Set to a custom size (same min and max), or turn off completely for the SSD and leave it to any traditional HDDs in the system. Ideally, it should be a set size (same max/min values) that is the same, or larger than your RAM.
Note: If you completely turn off the Page file, and run out of memory, the program you're running will crash.

Disable/Reduce System RestoreSave disk space, reduce writes
The System Restore feature allows for software installations, drivers and other updates to be rolled back. Disabling this feature can free up between a couple of hundred Megabytes and a couple of Gigabytes. You can turn down the amount of disk space System Restore can use, or turn it off completely at the risk of not being able to automatically recover from problems caused by system changes.
To change System Restore (or disable it completely):
Start Menu -> right-click Computer -> Properties -> Advanced System Settings (left pane) -> System Protection tab -> Highlight SSD -> Configure -> Reduce "Max Usage" or "turn off system protection" at your discretion.
Notes:
Turning off system restore completely will keep you from automatically recovering from some crashes and bugs during system changes.

Disable HibernateSave disk space
You can free up SSD space, (up to the amount of your RAM) by disabling the Hibernation feature in Windows. By default, the size of the hibernation file (hiberfil.sys) is the same as the amount of installed RAM on your computer. Naturally, disabling Hibernate will prevent you from using this power-saving mode.
To disable hibernate (needs elevated command prompt):
Start Menu -> Type cmd -> Right-Click the cmd Icon -> Run as Administrator -> type: powercfg -h off

Write Cachingwrite combining (NCQ)

Write caching in a hard drive refers to the use of high-speed volatile memory (RAM) to collect write commands sent to the drive and cache them until the hard drive can accomodate them. The downside of write-caching is that data not yet written to disk can be lost in the event of power interruption. As flash-based SSDs are much faster than traditional mechanical HDDs, the benefit of using write caching in terms of speed is much smaller. Some SSD guides may even go as far as recommending turning it off. Keep in mind that write caching adds the very relevant benefit of reduced small block writes. If write caching is off, every write command is sent by the OS one at a time to the SSD, including any TRIM commands, waiting for responses for each. With write caching enabled, they're combined in RAM before being written to the disk. Write combining (NCQ) is important and can benefit any drive, including SSDs, so we recommend that you leave write caching enabled.
To change the write-caching behavior, navigate to:
Start Menu -> right-click on Computer -> choose Properties -> Device Manager (left-hand pane) -> Disk drives -> right-click on your SSD Device -> Policies tab -> tick/untick "Enable write caching on the device"
Notes: The main benefit of leaving write caching on is NCQ, or write combining. Only if you use a UPS, to squeeze the last bit of performance from write caching you may try ticking the second option under the write caching policies tab.

Configure SuperfetchFree up RAM - do not preload program files
Disabling Superfetch (prefetch/bootfetch) frees up some RAM and resources by not preloading program files. Its benefits are minimal with SSDs, and disabling it reduces unnecessary writes to the drive as well. To make changes to the Superfetch behavior, open regedit.exe and browse down to the following key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\PrefetchParameters
EnablePrefetcher = 0  
( 0 = disable, 1= applications only, 2 = boot files only, 3 = cache everything [default])

We recommend using one of the more conservative Superfetch settings with SSDs, or turning it off completely.
While there, you may also want to turn off:
EnableSuperfetch = 0 (0=disable, 1 = applications only, 2 = boot files only, 3 = cache everything)
EnableBoottrace = 0 (startup logging and tracing)
Notes:
A restart is required before this takes effect.
You can clear out the \Windows\Prefetch folder after making this change to start with a fresh prefetcher cache (the next boot will be a bit slower).
Superfetch can be disabled from Admin Tools -> Services as well.

Reduce the Recycle Bin size
Even though it does not affect speed, reducing the maximum Recycle Bin size to 200 MB for example can help to free up space and reduce writes to your SSD:
Right-click on the "Recycle Bin" icon -> choose "Properties" -> Custom size -> 200 MB

Firefox - Use memory cachereduce disk writes
Firefox has the ability to write cached files to RAM instead of the hard disk. This is not only faster, but will significantly reduce writes to the SSD while using the browser.
To change the cache location to RAM:
Open Firefox -> Type about:config into the address bar -> Enter -> double-click browser.cache.disk.enable to set the value to False -> Right-Click anywhere -> New -> Integer -> Preference Name "disk.cache.memory.capacity" -> value memory size in KB. Enter 32768 for 32MB, 65536 for 64MB, etc. -> restart Firefox

IE - Move Temporary Internet Filesreduce unnecessary disk writes
To further reduce unnecessary disk writes to the SSD, you can move Internet Explorer temporary files to a separate mechanical drive.
In Internet Explorer, navigate to:
Tools -> General tab ->  Browsing History/Settings -> "Move folder" to another disk

Reduce other unnecessary disk writes
Even though not necessary, below are some ideas about reducing disk writes further, at your discretion:
  • reduce logging - Windows 7 writes extensive event logs. With the exception of some necessary logs (EventLog-Application, Security, System, Security Essentials), you can safely stop some logs that write to the drive periodically:
    Control Panel -> System and Security -> Administrative Tools -> expand Performance Monitor -> Data Collector Sets -> Startup Event Trace Sessions -> disable unnecessary logs, with the exception of the ones mentioned above.
  • Windows reliability monitor - it provides an overview of hardware and software problems over time. It writes to the drive on events, and every hour. It can be stopped (or slowed down) on a stable system at your discretion:
    Control Panel -> System and Security - Administrative Tools -> Computer Management -> Task Schedule ->Task Scheduler Library -> Microsoft -> Windows -> RAC -> right-click on the RAC Task and either disable or change its schedule.  Note: disabling it only stops unnecessary processing of the data, it is still being collected by the logs.

Any additional tips and feedback are always welcome.