admin管理员组

文章数量:1658586

zram 压缩流

Raspberry Pi and other single-board computers (SBCs) have always had minimal memory. At a maximum of 1GB in the Raspberry Pi 3, it was just sufficient to run lightweight programs and scripts. That trend held until the advent of the Raspberry Pi 4 with 2GB, 4GB and a few months later, 8GB models, opening doors to running databases and other memory intensive applications.

Raspberry Pi和其他单板计算机(SBC)始终具有最小的内存。 Raspberry Pi 3的最大容量为1GB,足以运行轻量级程序和脚本。 这种趋势一直持续到2GB4GB以及几个月后的8GB型号Raspberry Pi 4问世,为运行数据库和其他内存密集型应用程序打开了大门。

This shift is due in no small part to the explosive growth of the data industry, where previously the focus of SBCs was IoT and embedded applications and now the focus is slowly shifting towards data applications. Memory has also become cheaper over the years, allowing newer SBCs to have more memory while maintaining a similar price range.

这种转变在很大程度上是由于数据行业的爆炸性增长,以前SBC的重点是物联网和嵌入式应用程序,而现在的重点正在慢慢地转向数据应用程序。 多年来,内存也变得越来越便宜,从而使新型SBC可以拥有更多内存,同时保持相似的价格范围。

我的担忧 (My concerns)

With the Raspberry Pi 3 trending towards obsolescence, I got worried about how my 28-core Kraken cluster can stay relevant and useful for at least another year.

随着Raspberry Pi 3趋于过时的趋势,我担心我的28核Kraken集群如何在至少一年内保持相关性和有用性。

On the other hand, I’m secretly looking forward to deprecating the Kraken cluster as I’ve run into countless awkward situations where the system runs out of memory and all SSH sessions lock up while the kernel struggles for a good 10 minutes before panicking and forcing a reboot.

另一方面,我暗自希望弃用Kraken群集,因为我遇到了无数尴尬的情况,其中系统用尽了内存,所有SSH会话都被锁定,而内核在正常运行10分钟之前就出现了崩溃和崩溃。强制重启。

In fact, that happens to me about once a week! If you’ve run a good number of applications on the Raspberry Pi 3 before, I’m sure you share my pain.

实际上,这大约一周一次发生在我身上! 如果您以前在Raspberry Pi 3上运行过很多应用程序,那么我相信您会分担痛苦。

After a round of excavation in the depths of the Raspberry Pi forums, I found a glimmer of hope for the issues of impending obsolescence and restrictive memory capacity of the Raspberry Pi 3.

在Raspberry Pi论坛的深处进行了一轮挖掘之后,我发现了即将到来的Raspberry Pi 3过时和限制性内存容量问题的希望。

The solution is zram swap.

解决方案是zram swap

In this piece, I’ll demonstrate how you can use zram to increase the available memory by about 50% with negligible performance impact.

在本文中,我将演示如何使用zram将可用内存增加约50%,而对性能的影响却微不足道。

什么是zram? (What is zram?)

zram, formerly called compcache, is a Linux kernel module for creating a compressed block device in RAM, i.e. a RAM disk, but with on-the-fly “disk” compression. The block device created with zram can then be used for swap or as general-purpose RAM disk. (Wikipedia)

zram (以前称为compcache )是一个Linux内核模块,用于在RAM(即RAM磁盘)中创建压缩块设备 ,但具有即时的“磁盘”压缩功能。 然后可以将使用zram创建的块设备用于交换或用作通用RAM磁盘。 ( 维基百科 )

In English, the idea behind zram is basically setting aside a portion of your available memory, using it as a block device à la conventional disk storage and enforcing compression/decompression on all data being written/read from it.

用英语来说,zram的基本思想是预留一部分可用内存,将其用作常规磁盘存储的块设备,并对所有要写入/读取的数据强制执行压缩/解压缩。

有表现上的打击吗? (Is there a performance hit?)

With great power comes great responsibility

拥有权利的同时也被赋予了重大的责任

Although this widely-abused Spider-Man quote applies in most cases, this is an exception. Indeed, it is generally true that whenever compression is used, there would be increased computational requirements. But compression is a mature technology that has seen advancements over the years, that the additional compute load is minimal in modern-day processors, allowing for real-time applications such as in random-access memory.

尽管在大多数情况下都适用这种被广泛滥用的蜘蛛侠报价,但这是一个例外。 确实,通常确实是,每当使用压缩时,都会增加计算需求。 但是压缩是一项经过多年发展的成熟技术,在现代处理器中,额外的计算负载是最小的,从而允许诸如随机存取存储器之类的实时应用。

In the case of SBCs such as the Raspberry Pi 3, you are more likely to be memory-bound rather than CPU-bound in most self-hosting scenarios as self-hosted apps tend to be relatively CPU-light and memory-heavy. With 4 cores available and only 1 GB of RAM, you’re unlikely to sustain full utilization all 4 cores for extended periods of time without first running out of memory.

对于像Raspberry Pi 3这样的SBC,在大多数自托管方案中,您更有可能是受内存限制的,而不是受CPU限制的,因为自托管的应用程序通常占用CPU较少且占用大量内存。 有了4个可用核心以及仅1 GB的RAM,您就不可能在没有先用尽内存的情况下长时间维持所有4个核心的完全利用率。

Given that there is always some CPU resource available, increasing the memory at the cost of increased CPU utilization is definitely not going to produce a noticeable performance impact.

考虑到总有一些CPU资源可用,以增加CPU利用率为代价增加内存绝对不会产生明显的性能影响。

你能从中得到什么? (What can you get out of it?)

Source) 来源 )

In the table above (Github), we see the compression ratio and performances of each compression algorithm. With those performance numbers, we can easily see that majority of the compression algorithms qualify for real-time usage as their Compression/Decompression throughput exceeds or are close to the maximum memory Write/Read throughput of all Raspberry Pi models, respectively.

在上表( Github )中,我们看到了每种压缩算法的压缩率和性能。 有了这些性能指标,我们可以很容易地看到,大多数压缩算法都可以实时使用,因为它们的压缩/解压缩吞吐量分别超过或接近所有Raspberry Pi模型的最大内存写入/读取吞吐量。

Even in the most conservative case, just using compression alone would yield an additional 108% of space, and even that estimate itself is conservative. This is because the tests assume a mixture of data types to be compressed, non-compressible binary data as well as text. In memory, most of the data can be safely assumed to be text so the real-world space yield is expected to be vastly larger.

即使在最保守的情况下,仅使用压缩也会产生额外的 108% 的空间 ,甚至该估计本身也是保守的。 这是因为测试假定混合了要压缩的数据类型,不可压缩的二进制数据以及文本。 在内存中,可以安全地将大多数数据假定为文本,因此,现实世界中的空间产量预计会大得多。

(Example)

Assuming that 512MiB of RAM was set aside for zram swap, and assuming a conservative compression ratio of 2, then we’d stand to gain 500MB additional memory.

假设为zram交换留出了512MiB的内存,并假设保守的压缩率为2,那么我们将获得500MB的额外内存。

For an RPi 3, that brings the total memory to a relatively comfortable 1.5GiB!

对于RPi 3,总内存达到了相对舒适的1.5GiB!

安装zram-swap-config (Installing zram-swap-config)

Enough idle talk, let’s get down to business. There are just a handful of steps required to install zram-swap-config.

闲聊无聊,让我们开始谈谈。 安装zram-swap-config仅需要几个步骤。

Ensure that git is installed:

确保已安装git:

$ sudo apt-get install git

Clone the zram-swap-config repository:

克隆zram-swap-config存储库:

$ git clone https://github/StuartIanNaylor/zram-swap-config \ && cd zram-swap-config

Install zram-swap-config:

安装zram-swap-config:

$ chmod +x install.sh && sudo ./install.sh

Cleanup installation files

清理安装文件

$ cd .. && rm -r zram-swap-config

配置zram-swap-config (Configuring zram-swap-config)

zram-swap-config comes installed with sane defaults. But if you’re the kind that absolutely cannot sleep without tinkering with every knob and lever available, you’re in luck. Here’s how you can tweak it.

zram-swap-config随同默认安装。 但是,如果您是那种绝对不能不动动所有旋钮和杠杆就无法入睡的人,那么您很幸运。 调整方法如下。

Edit /etc/zram-swap-config.conf and modify the following parameters to your liking.

编辑/etc/zram-swap-config.conf并根据自己的喜好修改以下参数。

Note: Numeric values in the configuration are integer only.

注意:配置中的数值仅整数

Generally, there is no need to concern yourself with any setting other than MEM_FACTOR, DRIVE_FACTOR and COMP_ALG.

通常,除了MEM_FACTORDRIVE_FACTORCOMP_ALG MEM_FACTOR ,无需担心任何其他设置。

警告:压缩算法的可用性 (Caveat: Compression algorithm availability)

Looking at the compression algorithms table above, one would easily surmise that zstd gives the best balance of performance and compression ratio. However, I’m afraid I would have to disappoint here, as zstd like many other compression algorithms listed, is not necessarily available in your Linux distribution.

查看上面的压缩算法表,可以很容易地推断zstd在性能和压缩率之间达到了最佳平衡。 但是,恐怕我不得不在这里感到失望,因为zstd像列出的许多其他压缩算法一样, 不一定在您的Linux发行版中可用

To check the compression algorithms available for use, run the following command:

要检查可用的压缩算法,请运行以下命令:

$ modprobe zram && cat /sys/block/zram0/comp_algorithm
lzo [lz4] deflate

The output would show available compression algorithms as well as the currently selected compression algorithm enclosed by the square braces.

输出将显示可用的压缩算法以及由方括号括起来的当前选择的压缩算法。

我在Raspberry Pi 3B上对Raspbian的配置 (My configuration for Raspbian on Raspberry Pi 3B)

MEM_FACTOR=25
DRIVE_FACTOR=300
COMP_ALG=lz4
SWAP_DEVICES=1
SWAP_PRI=75
PAGE_CLUSTER=0
SWAPPINESS=80
  • MEM_FACTOR was set to 25, meaning 256MiB of memory will be set aside for zram swap.

    MEM_FACTOR设置为25,这意味着将为ram交换留出256MiB的内存。

  • DRIVE_FACTOR was set to 300, meaning that I'm estimating the net compression ratio to be 3.00 (still relatively conservative).

    DRIVE_FACTOR设置为300,这意味着我估计净压缩比为3.00 (仍然相对保守)。

  • COMP_ALG was set to lz4 as it is the best among the available algorithms in Raspbian lzo, lz4 and deflate.

    COMP_ALG设置为lz4,因为它是Raspbian lzo,lz4和deflate中可用算法中最好的。

I found these configuration values to be optimal for my setup but if you’re feeling adventurous or lucky, by all means, increase the DRIVE_FACTOR to 400.

我发现这些配置值对于我的设置来说是最佳的,但是如果您想冒险或很幸运,可以将DRIVE_FACTOR增大到400。

If you’re not using Raspbian, then there’s also a good chance that you have other compression algorithms available at your disposal, so do check and tweak away!

如果您不使用Raspbian,那么您很有可能会使用其他压缩算法,因此请检查并调整!

当你在 (While you’re at it)

If you’re installing zram-swap-config, you might want to disable swap on your Raspberry Pi.

如果要安装zram-swap-config,则可能要在Raspberry Pi上禁用交换功能。

Generally, regardless of whether you use zram swap, it’s always advisable to disable the default swap installed by Raspbian due to a couple of reasons:

通常,无论是否使用zram交换,总是建议禁用Raspbian安装的默认交换,这有两个原因:

  1. It reduces the life expectancy of your SD card as a swap device is subject to heavy and frequent writes.

    由于交换设备会频繁写入,因此会缩短SD卡的预期寿命。
  2. It yields extremely poor performance given the abysmal random I/O performance of SD cards in general.

    通常,鉴于SD卡的出色随机I / O性能,它的性能极差。

To disable the default swap device:

要禁用默认交换设备:

$ dphys-swapfile swapoff \
&& dphys-swapfile uninstall \
&& update-rc.d dphys-swapfile disable

With that you’re all set for the next year or so with your Raspberry Pi 3Bs!

这样,您将在明年左右为Raspberry Pi 3B做好准备!

下一步是什么? (What’s next?)

Next up, I’ll be introducing log2ram, a way to write logs to memory to reduce SD card wear and not to mention, it works in conjunction with zram-swap-config as well. See you around!

接下来,我将介绍log2ram ,这是一种将日志写入内存以减少SD卡磨损的方法,更不用说了,它还可以与zram-swap-config结合使用。 再见!

Originally published at ikarus.sg on August 17, 2020.

最初于2020年8月17日在ikarus.sg上发布。

翻译自: https://medium/@ikarus/using-zram-to-squeeze-more-out-of-your-raspberry-pi-55be552e309a

zram 压缩流

本文标签: 如何使用内存zramRaspberrypi