GNU/Linux 交换块系统

Ole*_*nge 7 linux swap virtual-memory

我在 4 MB RAM 到 512 GB RAM 的系统上使用了 GNU/Linux。当他们开始交换时,大多数时候你仍然可以登录并终止违规进程——你只需要多 100-1000 倍的耐心。

在我的新 32 GB 系统上发生了变化:它在开始交换时阻塞。有时有完整的磁盘活动,但有时没有磁盘活动。

为了检查可能是什么问题,我编写了这个程序。这个想法是:

1 grab 3% of the memory free right now
2 if that caused swap to increase: stop
3 keep the chunk used for 30 seconds by forking off
4 goto 1
Run Code Online (Sandbox Code Playgroud)

——

#!/usr/bin/perl

sub freekb {
    my $free = `free|grep buffers/cache`;
    my @a=split / +/,$free;
    return $a[3];
}

sub swapkb {
    my $swap = `free|grep Swap:`;
    my @a=split / +/,$swap;
    return $a[2];
}


my $swap = swapkb();
my $lastswap = $swap;
my $free;
while($lastswap >= $swap) {
    print "$swap $free";
    $lastswap = $swap;
    $swap = swapkb();
    $free = freekb();
    my $used_mem = "x"x(1024 * $free * 0.03);
    if(not fork()) {
    sleep 30;
    exit();
    }
}
print "Swap increased $swap $lastswap\n";
Run Code Online (Sandbox Code Playgroud)

永远运行程序应该使系统保持在交换的极限,但只获取最少量的交换并且非常缓慢地执行此操作(即一次最多几 MB)。

如果我运行:

forever free | stdbuf -o0 timestamp > freelog
Run Code Online (Sandbox Code Playgroud)

我应该看到交换每秒钟都在缓慢上升。(永远和时间戳来自https://github.com/ole-tange/tangetools)。

但这不是我看到的行为:我看到交换在跳跃中增加,并且系统在这些跳跃期间完全被阻塞。这里系统被阻塞了 30 秒,交换使用量随着 1 GB 的增加而增加:

secs
169.527 Swap:     18440184     154184   18286000
170.531 Swap:     18440184     154184   18286000
200.630 Swap:     18440184    1134240   17305944
210.259 Swap:     18440184    1076228   17363956
Run Code Online (Sandbox Code Playgroud)

阻止:21 秒。交换增加 2000 MB:

307.773 Swap:     18440184     581324   17858860
308.799 Swap:     18440184     597676   17842508
330.103 Swap:     18440184    2503020   15937164
331.106 Swap:     18440184    2502936   15937248
Run Code Online (Sandbox Code Playgroud)

阻止:20 秒。交换增加 2200 MB:

751.283 Swap:     18440184     885288   17554896
752.286 Swap:     18440184     911676   17528508
772.331 Swap:     18440184    3193532   15246652
773.333 Swap:     18440184    1404540   17035644
Run Code Online (Sandbox Code Playgroud)

阻止:37 秒。交换增加 2400 MB:

904.068 Swap:     18440184     613108   17827076
905.072 Swap:     18440184     610368   17829816
942.424 Swap:     18440184    3014668   15425516
942.610 Swap:     18440184    2073580   16366604
Run Code Online (Sandbox Code Playgroud)

这已经够糟糕了,但更糟糕的是系统有时会完全停止响应 - 即使我等了几个小时。我感觉这与交换问题有关,但我不能确定。

我的第一个想法是将 /proc/sys/vm/swappiness 从 60 调整为 0 或 100,看看是否有任何影响。0 没有影响,但 100 确实导致问题出现的频率降低。

如何防止系统阻塞这么长时间?

当少于 10 MB 就足够了时,为什么它决定换出 1-3 GB?

系统信息:

$ uname -a
Linux aspire 3.8.0-32-generic #47-Ubuntu SMP Tue Oct 1 22:35:23 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

编辑:

我通过删除 24 GB 并仅尝试使用 8 GB 来测试问题是否是由于 32 GB RAM - 我看到了相同的行为。

我还可以通过在 VirtualBox 中安装 GNU/Linux Mint 15 来重现交换行为(虽然不是冻结)。

我无法在我的 8 GB 笔记本电脑上重现该问题:上面的脚本可以精美地运行几个小时 - 换出几兆字节,但从来没有换过完整的千兆字节。所以我比较了两个系统上 /proc/sys/vm/* 中的所有变量:它们完全相同。这让我相信问题出在别处。笔记本电脑运行不同的内核:

Linux hk 3.2.0-55-generic #85-Ubuntu SMP Wed Oct 2 12:29:27 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

也许VM系统中的某些东西从3.2.0更改为3.8.0?

Ole*_*nge 0

升级到以下版本后问题消失:

Linux aspire 3.16.0-31-lowlatency #43~14.04.1-Ubuntu SMP PREEMPT Tue Mar 10 20:41:36 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

并没有考虑到是这个内核升级修复了这个问题。