Benutzer:Rdiez/Linux zram: Unterschied zwischen den Versionen

Aus /dev/tal
Wechseln zu: Navigation, Suche
Zeile 47: Zeile 47:
 
Not all of the zram device will be used. Sometimes, the kernel reserves swap page slots but never actually uses them, which is the reason why the swap usage reported by "cat /proc/swaps" differs from the data size reported by "cat /sys/block/zram*/orig_data_size". Many pages are shared but need to be unfold if they are written to. For more information, see [http://en.wikipedia.org/wiki/Copy-on-write copy-on-write] and configuration setting ''/proc/sys/vm/overcommit_memory'' . As a consequence, the theoretical zram capacity will probably never be reached, but at least no RAM will be allocated for that reserved but unused capacity either. I can think of the following optimisation in Linux's virtual memory manager: allocate unused page slots in the lowest-priority swap device, and, when the page is actually written to disk, try to allocate it in the highest-priority device available instead.
 
Not all of the zram device will be used. Sometimes, the kernel reserves swap page slots but never actually uses them, which is the reason why the swap usage reported by "cat /proc/swaps" differs from the data size reported by "cat /sys/block/zram*/orig_data_size". Many pages are shared but need to be unfold if they are written to. For more information, see [http://en.wikipedia.org/wiki/Copy-on-write copy-on-write] and configuration setting ''/proc/sys/vm/overcommit_memory'' . As a consequence, the theoretical zram capacity will probably never be reached, but at least no RAM will be allocated for that reserved but unused capacity either. I can think of the following optimisation in Linux's virtual memory manager: allocate unused page slots in the lowest-priority swap device, and, when the page is actually written to disk, try to allocate it in the highest-priority device available instead.
  
The Linux kernel has an small optimisation when swapping to zram. When a swap page is read back in, the system keeps the copy on disk allocated. If the same page gets swapped out again and was not modified in the meantime, it can be discarded from memory, as the copy on disk is still valid. However, if the swap device is based on zram, zram's copy is discarded as soon as the page is read back in, in order to free the memory used to hold the compressed version.
+
The Linux kernel has an small optimisation when swapping to zram called "swap free notify". When a swap page is read back in, the system keeps the copy on disk allocated. If the same page gets swapped out again and was not modified in the meantime, it can be discarded from memory, as the copy on disk is still valid. However, if the swap device is based on zram, zram's copy is discarded as soon as the page is read back in, in order to free the memory used to hold the compressed version.
  
 
zram implements an optimisation for pages that contain only zeros. This makes sense if you expect filesystems to write zeros in order to wipe sensitive data on disk, but I was surprised to see zeroed pages when using zram as a swap device. I would have thought that the virtual memory system would implement such a basic optimisation itself. If the system wants to wipe the swap file, overwriting large sequential blocks during shutdown would probably be a better approach.
 
zram implements an optimisation for pages that contain only zeros. This makes sense if you expect filesystems to write zeros in order to wipe sensitive data on disk, but I was surprised to see zeroed pages when using zram as a swap device. I would have thought that the virtual memory system would implement such a basic optimisation itself. If the system wants to wipe the swap file, overwriting large sequential blocks during shutdown would probably be a better approach.

Version vom 31. Mai 2014, 18:32 Uhr

Warning sign
Dies sind die persönlichen Benutzerseiten von rdiez, bitte nicht verändern! Ausnahmen sind nur einfache Sprachkorrekturen wie Tippfehler, falsche Präpositionen oder Ähnliches. Alles andere bitte nur dem Benutzer melden!


Linux zram

zram is a Linux block device that lives in RAM and compresses any data written to it.

When created, a zram device advertises its nominal capacity as empty space but allocates no actual memory. You can then create a filesystem on the zram device and use it as a RAM disk. As data is gradually written to it, zram will allocate as much kernel memory as necessary to hold the compressed data.

Compressing data takes CPU time, but can be faster than writing to a physical hard disk. If the data happens to not compress well, zram will allocate as much RAM as all the original data. If that is always the case, then it is best to use an uncompressed RAM disk like ramdisk, ramfs or tmpfs.

zram does not support the TRIM command, which is typically used with SSD disks. Therefore, zram will not free allocated memory even if all files are deleted. It's a little weird, because zram's predecessor project compcache does have this feature.

Availability

zram was considered stable as of Linux kernel version 3.14, released in March 2014. Ubuntu 14.04 has kernel version 3.13 and its zram is still considered experimental, although it had already enjoyed widespread usage then.

The version planned for kernel 3.15 supports the alternative compression algorithm LZ4, in addition to the default LZO. zram does NOT attempt to recompress the data in the background if the CPU is idle.

Using zram as a Swap Device

Using zram as a swap device is an interesting idea. I have not found on the Internet many performance tests yet, but most people who have tried it report an increased performance for standard office tasks, especially if the computer has little RAM. That seems like a contradiction at first, because zram uses RAM itself, making a scarce resource even scarcer.

A typical compression ratio for a zram swap file, taking all the management overhead into consideration, is factor 2.5. This means that "normal" swap data gets compressed down to a 40% of its original size. Therefore, for every byte of RAM that zram takes away, you get 2.5 bytes of a very fast swap file, at least compared to a traditional hard disk. You will also lose some CPU performance during the data compression.

Usually, one zram block device (with its corresponding swap file) is created per CPU core, in an attempt at distributing the CPU load. By the way, I heard that a future version will support multiple threads on a single device. All those swap files will have a higher priority than any other hard disk-based swap areas, so Linux will swap to zram first, and continue swapping to disk only if zram fills up.

If you have an SSD disk, zram may not offer a performance advantage. On the other hand, swapping to RAM first may avoid constant writes to the SSD and help prolong its lifetime. This applies of course to any storage type that needs wear leveling.

Note that zram can be used in embedded devices without any storage device for a standard swap file. This way, the virtual memory size can be somewhat larger than the RAM size.

How zram Swapping Works

Say your laptop has only 512 MiB of RAM and you create a 256 MiB zram swap device. At first, no swap memory is allocated at all. Linux will use any free RAM first, but at some point in time, memory pressure will build up and swapping will commence. Note that, if your computer has plenty of RAM and never swaps, enabling zram will not consume any resources.

If the kernel decides to swap out 100 MiB of RAM contents, zram will hopefully compress it down to 40 MiB, which means that the kernel will gain 100 MiB but lose 40 MiB at the same time compared to a non-zram scenario. Therefore, the kernel will probably want to swap an extra 40 MiB out to compensate. Again, this will cause a 16 MiB memory loss. In the end, trying to swap 100 MiB out has actually moved 160 MiB to the zram disk.

On the other hand, laptop hard disks are very slow, and writing 160 MiB to compressed RAM is probably faster than writing 100 Mib to disk. Later on, the data will need to be read back in, and that's where the speed difference really matters. Because of their high seek times, traditional hard disks are actually ill-suited for this task, as swap pages are not read in sequential blocks, but at fairly random positions. Besides, if the system is under pressure, the kernel automatically shrinks the file system cache, which also causes increased disk activity that will probably interfere with paging (assuming that you have not moved the swap area to a different drive).

When the 256 MiB zram device fills up, it will actually have consumed 102 MiB of RAM. Therefore, you can look at the situation this way: when under pressure, the system has lost 102 MiB of RAM, so it can only use 410 MiB instead of the physical 512 MiB. In return, the system has gained 256 MiB of very fast swap space.

If your software permanently accesses 450 MiB of memory, zram will drastically reduce performance. But that's not the normal scenario. Typically, if you keep a number of big applications like Firefox or LibreOffice running all the time, you will probably experience heavy swapping whether you have 410 or 512 MiB of physical memory. But that kind of software does not need all the allocated memory all the time. As long as your virtual memory needs do not exceed 666 MiB (that is 410 + 256), then zram will probably make your system faster overall. If your software needs more than 666 MiB, it will start paging out to disk, and that part will be as slow as before.

The trick is to achieve the right trade-off between RAM size, CPU performance, virtual memory requirements and hard disk speed. Your mileage may vary. I did a quick test on a 512 MiB PC running Xubuntu 14.04, Firefox, LibreOffice, Synaptic, KSysGuard and a Terminal, which always causes heavy paging, and my subjective impression was that performance was slightly worse with zram enabled.

Swap Device Usage and Implemented Optimisations

Not all of the zram device will be used. Sometimes, the kernel reserves swap page slots but never actually uses them, which is the reason why the swap usage reported by "cat /proc/swaps" differs from the data size reported by "cat /sys/block/zram*/orig_data_size". Many pages are shared but need to be unfold if they are written to. For more information, see copy-on-write and configuration setting /proc/sys/vm/overcommit_memory . As a consequence, the theoretical zram capacity will probably never be reached, but at least no RAM will be allocated for that reserved but unused capacity either. I can think of the following optimisation in Linux's virtual memory manager: allocate unused page slots in the lowest-priority swap device, and, when the page is actually written to disk, try to allocate it in the highest-priority device available instead.

The Linux kernel has an small optimisation when swapping to zram called "swap free notify". When a swap page is read back in, the system keeps the copy on disk allocated. If the same page gets swapped out again and was not modified in the meantime, it can be discarded from memory, as the copy on disk is still valid. However, if the swap device is based on zram, zram's copy is discarded as soon as the page is read back in, in order to free the memory used to hold the compressed version.

zram implements an optimisation for pages that contain only zeros. This makes sense if you expect filesystems to write zeros in order to wipe sensitive data on disk, but I was surprised to see zeroed pages when using zram as a swap device. I would have thought that the virtual memory system would implement such a basic optimisation itself. If the system wants to wipe the swap file, overwriting large sequential blocks during shutdown would probably be a better approach.

Installing and Removing zram

On Ubuntu 14.04 and derivatives, install package zram-config, which will create a zram swap device with an advertised capacity of 50% of the physical memory size. For example:

apt-get install zram-config

You can verify that it has been enabled by looking for "zram" in the list of swap devices:

cat /proc/swaps

In order to uninstall zram, you need to "completely remove" it, or use apt-get's "--purge" switch. Otherwise, the zram device will disappear now only to silently come back after a reboot. I would consider that a bug in the zram-config package. For example:

apt-get remove --purge zram-config

If you forgot to do that, you can install it again and then purge it. Or you can just "completely remove" it in Synaptic, but the zram swap device will only be gone then after a system restart.

Alternatives

zswap takes the cache approach instead, so it always needs a swap device underneath. It is more flexible, as it can dynamically cache any part of the swap file, no matter how much RAM the computer has. Ubuntu's 14.04 stock kernel does not support zswap.

If your system is low on memory, you may have other options:

  • If you have more than one drive, move the swap partition or file to the least busy disk.
  • Try swapping to a USB stick.
  • If the computer has a memory card reader, you could use a fast memory card as swap drive.
  • If your video card has a lot of memory, some people have managed to use some of it as a swap device.