如何使“/sys/kernel/mm/transparent_hugepage/defrag”从不处于关闭 THP 警告的状态

如何使“/sys/kernel/mm/transparent_hugepage/defrag”从不处于关闭 THP 警告的状态

重启linux(ubuntu 16.04 redis 4.011)

/var/log/redis.log

节目

# WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

所以我

方法一:

/etc/rc.local

if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
   echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
   echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi

重启机器时

the /var/log/redis.log shows the THP warning still.

但是当我使用

$cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
$cat /sys/kernel/mm/transparent_hugepage/defrag
always defer madvise [never]

我有权[永不]

$systemctl restart redis

the /var/log/redis.log shows no THP warning.

但这并不正确,因为当机器重启时仍然会出现 THP 警告。

所以我尝试方法2:先删除 /etc/rc.local if...fi 句子

$apt install sysfsutils

将这两行添加到 /etc/sysfs.conf:

kernel/mm/transparent_hugepage/enabled = never  
kernel/mm/transparent_hugepage/defrag = never

重启

/var/log/redis.log got no any new message???

我如何才能再次获取我的日志? sysfutils 会吞噬它吗?

$systemctl status redis

redis 处于活动状态。

$cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
$cat /sys/kernel/mm/transparent_hugepage/defrag
always defer [madvise] never

我得到了 [madvise] ?????

然后

$systemctl restart redis
$cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
$cat /sys/kernel/mm/transparent_hugepage/defrag
always defer [madvise] never

[madvise] 又来了?
碎片整理的[never] 不是选择对吗??!

当我更改 /etc/sysfs.conf 上的启用和碎片整理时,出现了奇怪的情况:

kernel/mm/transparent_hugepage/defrag = never   
kernel/mm/transparent_hugepage/enabled = never 

我得到了正确的 cat 结果 $systemctl restart redis $cat /sys/kernel/mm/transparent_hugepage/enabled always madvise [never] $cat /sys/kernel/mm/transparent_hugepage/defrag always defer madvise [never]

但是当重新启动 Linux 时,仍然没有任何消息添加到我的 /var/log/redis.log 中。

但是当我命令“systemctl restart redis”时可以添加新消息

为什么会出现如此奇怪的情况?哪个是正确的,如何解决?

答案1

就像你一样,我有这个问题,我去 github 和论坛寻找答案,实际上它是所有这些的混合结果

我认为你的方法是正确的,但每个人似乎都会犯的主要错误是在应用新的 THP 规则后不重新启动 redis 服务。

1. 添加 rc.local

首先,在 ubuntu 20.04 上,默认情况下 rc.local 不存在,我按照以下步骤修复了这个问题(參考

以 root 身份

1.1. 为 rc-local 创建 systemd 服务

添加 /etc/systemd/system/rc-local.service

[Unit]  
 Description=/etc/rc.local Compatibility  
 ConditionPathExists=/etc/rc.local  

[Service]  
 Type=forking  
 ExecStart=/etc/rc.local start  
 TimeoutSec=0  
 StandardOutput=tty  
 RemainAfterExit=yes  
 SysVStartPriority=99  

[Install]  
 WantedBy=multi-user.target  

1.2. 激活它

touch /etc/rc.local  
chmod +x /etc/rc.local  
systemctl enable rc-local  
1.2.1 在该文件(rc.local)中你可以添加
#!/bin/bash  
echo never > /sys/kernel/mm/transparent_hugepage/enabled  
echo never > /sys/kernel/mm/transparent_hugepage/defrag  
systemctl restart redis-server.service  
exit 0  

答案2

使用 systemd-tmpfiles 更简单:

# cat /etc/tmpfiles.d/disable-thp.conf
#Type Path                                                  Mode UID GID  Age Argument
w     /sys/kernel/mm/transparent_hugepage/enabled           -    -   -    -   never
w     /sys/kernel/mm/transparent_hugepage/defrag            -    -   -    -   never

# systemd-tmpfiles --create --prefix=/sys/kernel/mm/transparent_hugepage/ /etc/tmpfiles.d/disable-thp.conf

# grep -E . /sys/kernel/mm/transparent_hugepage/defrag /sys/kernel/mm/transparent_hugepage/enabled
/sys/kernel/mm/transparent_hugepage/defrag:always defer defer+madvise madvise [never]
/sys/kernel/mm/transparent_hugepage/enabled:always madvise [never]

当然,不需要总是systemd-tmpfiles手动运行,它会在systemd-tmpfiles-setup.service开机时运行。

相关内容