The list of zram parameters

Linux Kernel

In the past article log2ram, there was a useful kernel module called zram.
I investigated zram parameter with looking kernel source code.
If you are interested in zram, this article is useful.

FYI
log2ram article is here.

What is zram?

zram is a RAM disk with automatic compression/decompression mechanism provided by kernel. Finally, copy the contents of /var/hdd.log/ to /var/log/ with syncFromDisk and log2ram startup is finished.

The point is that “you can store data larger than the actual size below on RAM disk like /tmp”!

I searched by Google.

I searched by Google with keyword “zram kernel parameter”.
kernel.org was found, so take a rough look.

https://www.kernel.org/doc/Documentation/blockdev/zram.txt

a) using zram and zram_control sysfs attributes
b) using zramctl utility, provided by util-linux (util-linux@vger.kernel.org).

zram: Compressed RAM based block devices

We can handle zram by using sysfs or util-linux.
Let me focus on sysfs.

List of parameters

I summarized as list.

Parameter nameAttributeOverview
disksizeRWSpecifies the data size of the ZRAM block device.
initstateROIndicates whether zram has been initialized.
resetWORequests kernel to restart the zram module.
mem_used_maxWOUpdates the RAM size that zram can use to store compressed data.
mem_limitWOSpecifies the RAM size that zram can use to store compressed data.
writeback_limitWOSpecifies the maximum data size that can be written by the writeback function.
writeback_limit_enableRWSpecifies whether the data size limitation which is valid during write operation by writeback is active or not.
max_comp_streamsRWSpecifies the number of concurrent compression operations.
comp_algorithmRWSpecifies the compression algorithm.
compactWORequests memory compression.
debug_statRODebugging purpose.
backing_devRWSpecifies the block device used by the writeback feature.
IdleWORequests to be idle.

From here I digged deeper parts which I could not understand.

mem_used_max

This parameter is as follows in the kernel.org description:

reset the ‘mem_used_max’ counter (see later)
(snip)
the maximum amount of memory zram have consumed
to store the data

Let take a look kernel source code.
Downloaded from https://github.com/torvalds/linux.

The zram main logic can be found in drivers/block/zram/zram_drv.c
mem_used_max is used as follows.

static ssize_t mem_used_max_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t len)
{
	int err;
	unsigned long val;
	struct zram *zram = dev_to_zram(dev);

        err = kstrtoul(buf, 10, &val);
	if (err || val != 0)
		return -EINVAL;

        down_read(&zram->init_lock);
	if (init_done(zram)) {
		atomic_long_set(&zram->stats.max_used_pages,
				zs_get_total_pages(zram->mem_pool));
	}
	up_read(&zram->init_lock);

        return len;
}

Roughly I understood that,
– The argument must be 0.
– If zram initialization is done, it copies zs_get_total_pages(zram->mem_pool) to zram->stats.max_used>pages.
So the value 0 is just trigger to start reloading.

The updated value can be checked by reading /sys/block/zramX/mm_stat.

File /sys/block/zram/mm_stat
The stat file represents device’s mm statistics. It consists of a single line of text and contains the following stats separated by whitespace:
– orig_data_size
– compr_data_size
– mem_used_total
– mem_limit
– mem_used_max
– zero_pages
– num_migrated

https://www.malasuk.com/doc/kernel-doc-3.10.0/Documentation/blockdev/zram.txt

About writeback function

zram supports the writeback feature, but let’s find out exactly what it is.
Here’s the description at kernel.org:

= writeback

With CONFIG_ZRAM_WRITEBACK, zram can write idle/incompressible page to backing storage rather than keeping it in memory.

We can understand as a function to avoid using ZRAM’s own RAM by writing idle/uncompressable pages to an alternate block device.

The block device to be used can be specified bybacking_dev.
Example: echo /dev/sda5 > /sys/block/zram0/backing_dev

We can limit the size of data that writes in writeback.
Let see example.

First writeback_limit_enable the limiting feature by setting 1.
Example: echo 1 > /sys/block/zramX/writeback_limit_enable

Then specify the maximum data size to writeback_limit.
Example: echo 1024 > /sys/block/zram0/writeback_limit

Now maximum data size is limited to 4MB.
writeback_limit is in 4KB unit which is page size.

Conclusion

How was it?

Since zram is kernel module, so architectural design is needed to use consistently.
But this is very powerful because it seems to have higher performance than tmpfs.

zram can be used on Raspberry PI too, so it may be good idea to try and be used to!

Comments

Copied title and URL