linux zswap_Linux技巧:ZSWAP带来更高的性能

linux zswap

What do you do when your Linux computer doesn’t have much memory? Swapping is one answer, but since your hard drive — even if it is solid state — is much slower than memory, that’s not always the best option. However, most modern Linux kernels have a built in provision for what’s known as ZSWAP. This lets you use some of your memory as swap space.

当您的Linux计算机内存不足时,该怎么办? 交换是一个解决方案,但是由于硬盘驱动器(即使它是固态驱动器)也比内存慢得多,所以这并不总是最佳选择。 但是,大多数现代Linux内核都为ZSWAP提供了内置功能。 这使您可以将部分内存用作交换空间。

等一下? (Wait a Minute?)

At first, that doesn’t seem to make sense. After all, you are low on memory so how can using more memory be the answer? ZSWAP acts like a swap file and it compresses memory as much as 3:1. With a modern CPU, accessing memory and decompressing it is still lightning fast compared to accessing even a fast disk drive. So it is a winning proposition.

起初,这似乎没有意义。 毕竟,您的内存不足,那么如何才能使用更多的内存呢? ZSWAP就像交换​​文件一样,它压缩内存的比例高达3:1。 使用现代CPU,与访问快速磁盘驱动器相比,访问内存并对其进行解压缩仍然非常快。 因此,这是一个成功的主张。

什么是交换? (What is Swap?)

In the old days, you had a certain amount of memory for your computer. Programs and data shared this memory and if you ran out, that was it. Nowadays most computers can handle a swap file.

在过去,您的计算机有一定的内存量。 程序和数据共享此内存,如果用完了就可以了。 如今,大多数计算机都可以处理交换文件。

Image for post
Photo by Markus Spiske on Unsplash
Markus SpiskeUnsplash拍摄的照片

If the computer decides it would like more memory it can evict some pages — hopefully, those that aren’t being used much — out to a disk file and get them back later. For programs, it can sometimes just dump the memory and get it from the executable file later. For data and some programs, it has to write the memory to a special area of the disk — the swap file or partition — and then reuse that memory.

如果计算机决定要更多的内存,它可以将某些页面(希望是那些使用不多的页面)逐出到磁盘文件中,并在以后将其恢复。 对于程序,它有时可以转储内存,以后再从可执行文件中获取。 对于数据和某些程序,它必须将内存写入磁盘的特殊区域(交换文件或分区),然后重新使用该内存。

Think of it as having a digital camera that can hold 100 pictures. If you know you have 60 pictures and you are going to take 50 more, then you have to roll some pictures off to an external hard drive. Later, you can get them back but you’ll have to send some other pictures to the external drive, first.

可以想象它拥有一台可以容纳100张照片的数码相机。 如果您知道有60张图片,并且打算再拍摄50张,则必须将一些图片滚动到外部硬盘驱动器上。 稍后,您可以将它们取回,但首先必须将其他图片发送到外部驱动器。

The swap area can be on a reserved part of the disk (a partition) or a file. Modern kernels don’t care, although older kernels didn’t perform as well with a file swap area.

交换区域可以位于磁盘(分区)或文件的保留部分上。 尽管较旧的内核在文件交换区域中的性能不佳,但现代的内核并不在乎。

The advantage to swap is that it lets you run programs that exceed the amount of memory you have. The downside is the disk is very slow compared to memory, so performance will suffer. ZRAM lets the kernel compress unused memory into a reserved area of memory before writing out to a real swap file. This operation is very fast and even though it eats up memory it can provide more benefits than harm thanks to compression.

交换的好处是,它使您可以运行超出内存量的程序。 缺点是磁盘与内存相比非常慢,因此会降低性能。 ZRAM允许内核在写出实际交换文件之前将未使用的内存压缩到内存的保留区域中。 此操作非常快,即使占用了内存,但由于压缩,它所带来的好处多于危害。

我有很多记忆! (I Have Lots of Memory!)

Image for post
Photo by Stef Westheim on Unsplash
Stef WestheimUnsplash拍摄的照片

If you have lots of memory, you probably won’t get much benefit from ZSWAP. It still pays to have a swap file or partition (modern kernels work fine with either), though, because it allows the kernel to unload infrequently used programs which gives better performance to programs you are using. The kicker is when everything you do requires something else to be evicted.

如果您有大量内存,则ZSWAP可能不会带来太多好处。 不过,仍然值得拥有一个交换文件或分区(现代内核都可以正常工作),因为它允许内核卸载不常用的程序,从而为正在使用的程序提供更好的性能。 当您所做的每件事都需要驱逐其他东西时,踢球者就是您。

Want to try? You can get it working in two simple steps and, if you like it, make it permanent with a 3rd step.

要尝试吗? 您可以通过两个简单的步骤使它正常工作,如果喜欢,可以通过第3步使其永久化。

步骤1.确保可以 (Step 1. Make Sure You Can)

Type this command line (you don’t need to be root):

键入以下命令行(您不必是root用户):

cat /sys/module/zswap/parameters/enabled

If you get a Y back from this command, then congratulations, you are done. Zswap is enabled and it is running. You can skip the rest of this post! If it says N then you are halfway there. Your kernel knows about zswap, but it isn’t turned on. If you get some sort of file not found error, then your kernel doesn’t have the code needed for zswap. You can try installing a different kernel, or — if you are really motivated — build your own kernel.

如果您从该命令获得了Y,那么恭喜您,您已完成。 Zswap已启用并且正在运行。 您可以跳过本文的其余部分! 如果显示“ N”,则说明您已经中途了。 您的内核了解zswap,但尚未打开。 如果您遇到某种找不到文件的错误,则您的内核没有zswap所需的代码。 您可以尝试安装其他内核,或者-如果您确实有动力,则可以构建自己的内核。

步骤2.运行时实验 (Step 2. Run Time Experiments)

I’ll assume you got a N. The good news is you can actually make the change on the fly if you want to experiment:

我假设您的得分为N。好消息是,如果您想进行实验,实际上可以即时进行更改:

echo Y | sudo tee /sys/module/zswap/parameters/enabled

You are now using zswap. There are a few other parameters you can try. The default pool is zbud which gets a 2:1 compression ratio. But the z3fold is better. You can also try different compressors such as lzo:

您现在正在使用zswap。 您可以尝试其他一些参数。 默认池为zbud,压缩比为2:1。 但是z3fold更好。 您也可以尝试使用其他压缩器,例如lzo:

# list all
grep . /sys/module/zswap/parameters/*
echo z3fold | sudo tee /sys/module/zswap/parameters/zpool
echo lzo | sudo tee /sys/module/zswap/parameters/compressor

Try it out for a while. If you like it, go to step 3.

尝试一会儿。 如果喜欢,请转到步骤3。

第3步:永久使用-也许 (Step 3. Make it Permanent — Maybe)

It should be easy to make this permanent, but it isn’t as easy as you might like. You can enable zswap by putting zswap=Y on your kernel command line. On a desktop computer, that will probably involve editing and regenerating grub. On a Raspberry Pi, there is a /boot/cmdline.txt file.

使其永久化应该很容易,但是却没有您想的那么容易。 您可以通过在内核命令行上放置zswap = Y来启用zswap。 在台式计算机上,这可能涉及编辑和重新生成grub。 在Raspberry Pi上,有一个/boot/cmdline.txt文件。

However, there is a problem with this, especially if you want to change the pool or compressor settings. During boot, the system is in a reduced state running on a RAM disk until it can mount the root directory. That means that some of these options might not work until after that mount.

但是,这有一个问题,尤其是在您要更改池或压缩器设置的情况下。 在引导过程中,系统在RAM磁盘上处于还原状态,直到可以装入根目录为止。 这意味着其中某些选项可能要等到该安装之后才能起作用。

If you are comfortable rebuilding your initramfs or initcpio or whatever stands for that in your distribution, fine. You probably don’t need me to tell you what to do to get the modules loaded. However, there is an easier solution.

如果您愿意重建initramfs或initcpio或您发行版中所代表的内容,则可以。 您可能不需要我告诉您如何加载模块。 但是,有一个更简单的解决方案。

I simply put the lines I want in /etc/rc.local. You don’t need to use sudo or tee because that script runs as root at boot time. So you might open up your favorite editor with sudo and add the following to /etc/rc.local:

我只是将我想要的行放在/etc/rc.local中。 您不需要使用sudo或tee,因为该脚本在引导时以root身份运行。 因此,您可以使用sudo打开您喜欢的编辑器,然后将以下内容添加到/etc/rc.local中:

echo Y >/sys/module/zswap/parameters/enabled
echo z3fold >/sys/module/zswap/parameters/zpool

Sure, this means you don’t get the advantage very early in the boot process but by the time rc.local runs, all the things you need will be ready.

当然,这意味着您不会在启动过程的早期就获得优势,但是到rc.local运行时,您所需的所有东西都将准备就绪。

结语 (Wrap Up)

If you have a little bit of memory, by all means, try zswap. On a 4G Rasberry Pi it makes a big difference. Even on a 2GB Intel Compute Stick, it helped, although not really enough.

如果您有一点内存,请尝试zswap。 在4G Rasberry Pi上,它有很大的不同。 即使在2GB的Intel Compute Stick上,它也提供了帮助,尽管还远远不够。

The good news is that it is hard to mess up. A bad value probably isn’t going to brick your system or wipe your hard drive. If you don’t like it, you can always reverse it easily.

好消息是,很难搞砸了。 不好的价值可能不会使您的系统变得笨拙或擦除硬盘。 如果您不喜欢它,可以随时轻松地将其反转。

翻译自: https://medium.com/for-linux-users/linux-tip-higher-performance-with-zswap-2a4654b935de

linux zswap

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值