Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling THP (Transparent hugepages) with ansible role

I'm trying to disable THP via ansible on vagrant up, because it interferes with Redis (causes latency and memore usage issues with redis if enabled) The command to disable THP is "echo never > /sys/kernel/mm/transparent_hugepage/enabled" but it doesn't seem to be working with a simple shell role as shown below.

- name: Disable THP support (causes latency and mem usage issues with redis)
  shell: echo never {{ ">" }} /sys/kernel/mm/transparent_hugepage/enabled
  become: yes
  become_method: sudo
  become_user: root

This is the ansible output:

TASK [Disable-THP : Disable THP support (causes latency and mem usage issues with redis)] *** changed: [127.0.0.1] => {"changed": true, "cmd": "echo never > /sys/kernel/mm/transparent_hugepage/enabled", "delta": "0:00:00.003939", "end": "2018-07-09 12:22:33.183451", "rc": 0, "start": "2018-07-09 12:22:33.179512", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

After this i ssh into the virtual machine and start the redis-server, which still gives me the warning message.

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.

Am I doing something wrong with the ansible script or can anyone help me as to why this simple shell command is not working?

Br, Victor

UPDATE: I slightly modified my ansible role to check if the contents of the file actually changes. The role looks like this now:

- name: Disable THP support (causes latency and mem usage issues with redis)
  shell: |
    echo "never" >> /sys/kernel/mm/transparent_hugepage/enabled
    cat /sys/kernel/mm/transparent_hugepage/enabled
  become: true
  become_user: root
  become_method: sudo

And according to the output, the enabled file actually changes the value to [never]. But when I ssh into the VM and cat the enabled file, it shows that the value is still [always]

TASK [Disable-THP : Disable THP support (causes latency and mem usage issues with redis)] *** changed: [127.0.0.1] => {"changed": true, "cmd": "echo \"never\" >> /sys/kernel/mm/transparent_hugepage/enabled\n cat /sys/kernel/mm/transparent_hugepage/enabled", "delta": "0:00:00.005309", "end": "2018-07-10 10:41:27.801697", "rc": 0, "start": "2018-07-10 10:41:27.796388", "stderr": "", "stderr_lines": [], "stdout": "always madvise [never]", "stdout_lines": ["always madvise [never]"]}

Why does the content of the files show that it has been changed, but then when i SSH into the VM it seems to tell me otherwise?

[vagrant@test ~]$ cd ..
[vagrant@test home]$ cd ..
[vagrant@test /]$ cd sys/kernel/mm/transparent_hugepage/
[vagrant@test transparent_hugepage]$ cat enabled
[always] madvise never
like image 693
madvic Avatar asked Dec 17 '25 12:12

madvic


2 Answers

Based on this question you can install package sysfs and set the configuration of the sysfs.conf file using template or lineinfile modules. It has the advantage to be idempotent.

---
- hosts: target
  become: yes
  tasks:
    - package:
        name: sysfsutils
    - lineinfile:
        path: /etc/sysfs.conf
        line: kernel/mm/transparent_hugepage/enabled = never

Of course packages name can differ based on your distribution.

You need to reboot the target to take the change into account.

like image 156
Baptiste Mille-Mathias Avatar answered Dec 19 '25 06:12

Baptiste Mille-Mathias


@clopez: sysfsutils does not create /etc/sysfs.conf in centOS. Your 'lineinfile' conf is also adding the same line every time the playbook is run.

Here is my configuration - that should also work for debian-based systems :

    - name: install sysfsutils for disabling transparent huge pages
      package:
        name: sysfsutils
        state: latest
    - name: disable transparent huge pages for redis performance - persistent change
      lineinfile:
        path: /etc/sysfs.conf
        create: true
        regexp: '^kernel\/mm\/transparent\_hugepage\/enabled'
        line: "kernel/mm/transparent_hugepage/enabled = never"
    - name: disable transparent huge pages for redis performance - live change
      shell: echo never {{ ">" }} /sys/kernel/mm/transparent_hugepage/enabled
like image 21
Serge Hartmann Avatar answered Dec 19 '25 07:12

Serge Hartmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!