Zram

From Segfault
Jump to: navigation, search

zram is included since Linux 3.14[1] and can be described as follows: [2]

> The zram module creates RAM based block devices [...]
> Pages written to these disks are compressed and stored
> in memory itself. These disks allow very fast I/O and compression provides
> good amounts of memory savings. Some of the usecases include /tmp storage,
> use as swap disks, various caches under /var and maybe many more :)

Usage

$ sudo modprobe zram
$ cat /sys/block/zram0/comp_algorithm 
[lzo]

Note: to use the lz4 compression algorithm, CONFIG_ZRAM_LZ4_COMPRESS needs to be enabled in the kernel.

$ echo 800M > /sys/block/zram0/disksize
$ sudo mkswap -L zram0 /dev/zram0 && sudo swapon -p2 /dev/zram0                 # prioritize over swap0 and swap1

Statistics

$ cat /proc/swaps 
Filename				Type		Size	Used	Priority
/dev/dm-1                               partition 	524284	0	0
/dev/dm-0                               partition	976556	157660	1
/dev/zram0                              partition 	819196	80148	2
$ grep . /sys/block/zram0/{initstate,num_reads,failed_reads,num_writes,failed_writes,invalid_io,notify_free,zero_pages,orig_data_size,compr_data_size}
/sys/block/zram0/initstate:1
/sys/block/zram0/num_reads:776
/sys/block/zram0/failed_reads:0
/sys/block/zram0/num_writes:20157
/sys/block/zram0/failed_writes:0
/sys/block/zram0/invalid_io:0
/sys/block/zram0/notify_free:747
/sys/block/zram0/zero_pages:1355
/sys/block/zram0/orig_data_size:74301440
/sys/block/zram0/compr_data_size:21519592

Many of these objects are already obsolete[3], so it's best to use the following files to get zram metrics:

$ awk '{print "failed_reads:", $1, "failed_writes:", $2, "invalid_io:", $3, "notify_free:", $4}'  /sys/block/zram0/io_stat 
failed_reads: 0 failed_writes: 0 invalid_io: 0 notify_free: 5565
$ awk '{print "orig_data_size", $1/1024/1024"M", "\ncompr_data_size", $2/1024/1024"M", "\nmem_used_total", $3/1024/1024"M", "\nmem_limit", $4, "\nmem_used_max", $5/1024/1024"M", "\nzero_pages", $6, "\nnum_migrated", $7}' /sys/block/zram0/mm_stat
orig_data_size 368.781M 
compr_data_size 25.2751M 
mem_used_total 27.0977M 
mem_limit 0 
mem_used_max 27.0977M 
zero_pages 2457 
num_migrated 263

zramctl

A command named zramctl has been added[4] to util-linux a while ago.

It's already included in Fedora 22. But it's not in Debian/jessie yet, so we have three options here:

  1. Compile util-linux from source, and update it if necessary
  2. Use APT pinning or other means of mixing newer distribution branches into our stable system.
  3. Just install /sbin/zramctl and be done with it.
VER=2.27-3
wget http://ftp.us.debian.org/debian/pool/main/u/util-linux/util-linux_${VER}_powerpc.deb        # powerpc
wget http://ftp.us.debian.org/debian/pool/main/u/util-linux/util-linux_${VER}_amd64.deb          # x86_64
wget http://ftp.us.debian.org/debian/pool/main/u/util-linux/util-linux_${VER}_i386.deb           # i386

dpkg --fsys-tarfile util-linux*.deb | tar -xvf - ./sbin/zramctl
chmod 0755 sbin/zramctl
sudo chown root:root sbin/zramctl
sudo mv sbin/zramctl /usr/local/sbin/

Usage

sudo modprobe zram num_devices=2
sudo zramctl -f -s 512M -t 2 -a lzo                                             # Use 50%..75% of RAM
sudo mkswap /dev/zram0 && swapon -p2 /dev/zram0

$ zramctl
NAME       ALGORITHM DISKSIZE  DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo           512M 25.1M   12M   14M       2 [SWAP]

Links

References