3

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
chicks
  • 2,393
  • 3
  • 24
  • 40
madvic
  • 123
  • 2
  • 13
  • Did the contents of the file change? If yes, what does your question have to do with Ansible? If no, what have you done to troubleshoot and why is it not in the question? – techraf Jul 09 '18 at 17:14
  • I edited my original post to try to answer your question, hope this helps. Im pretty new to ansible so sorry if it was unclear at the start. – madvic Jul 10 '18 at 10:51
  • There is also a way how to disable it "indirectly". Via tuned. For example Oracle disables it via dedicated profile for tuned. https://access.redhat.com/solutions/2867881 – ibre5041 Oct 20 '21 at 14:23

5 Answers5

8

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.

Baptiste Mille-Mathias
  • 2,144
  • 4
  • 31
  • 37
  • Thanks alot! This seems like a smart way to go about this, I will report back to you as soon as I have tried this out! – madvic Jul 10 '18 at 07:52
  • 3
    This will just update the "sysfs.conf", but not activate the change until the next reboot, right? – A. Scherbaum Jul 10 '18 at 20:42
5

@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
Serge Hartmann
  • 138
  • 1
  • 3
  • 8
2

Ok, so based on the most voted answer, this combines both answers with the actual Ansible tasks:

- name: Install sysfsutils for disabling transparent huge pages
  become: yes
  apt:
    name: sysfsutils
    state: latest

- name: Disable transparent huge pages for performance
  become: yes
  lineinfile:
    path: /etc/sysfs.conf
    line: |
      kernel/mm/transparent_hugepage/enabled = never
clopez
  • 4,372
  • 3
  • 28
  • 42
0

Answier with sysfsutils didn't work for me on CentOS 7.9 but tuned did.

tasks/main.yml:

- name: dir /etc/tuned/no-thp
  file:
    path: "/etc/tuned/no-thp"
    state: directory
    mode: 0755

- name: tuned-adm profile to disable transparent hugepages
  template:
    src: "tuned-no-thp.conf"
    dest: "/etc/tuned/no-thp/tuned.conf"
    mode: 0644
  notify:
    - enable no-thp profile

- name: check tuned-adm active profile
  command: tuned-adm active
  register: tuned_adm_active_profile
  changed_when: False

- name: tuned-adm profile no-thp
  command: "tuned-adm profile no-thp"
  when: 'tuned_adm_active_profile.stdout.find("Current active profile: no-thp") < 0'

templates/tuned-no-thp.conf:

[main]
summary=disable transparent hugepages for DB performance
include=virtual-guest

[vm]
transparent_hugepages=never

handlers/main.yml:

- name: enable no-thp profile
  command: tuned-adm profile no-thp
kodstark
  • 463
  • 4
  • 11
-3

Okay so the problem apparently was that my /etc/rc.local file did not have the proper permissions to run on boot. I added a ansible role with this task:

- name: Change permissions of /etc/rc.local to make it run on boot
  shell: chmod +x /etc/rc.d/rc.local
  become_method: sudo

This makes the /etc/rc.local file run on boot, which solved this problem for me. now my whole task that removes THP from the kernel settings looks like this.

- name: Disable THP support scripts added to rc.local
  lineinfile:
    path: /etc/rc.local
    line: |
      echo never > /sys/kernel/mm/transparent_hugepage/enabled
      echo never > /sys/kernel/mm/transparent_hugepage/defrag

- name: Change permissions of /etc/rc.local to make it run on boot
  shell: chmod +x /etc/rc.d/rc.local
  become_method: sudo

Thanks for the help everyone!! Hope this solution works for anyone else having the same problem. :)

madvic
  • 123
  • 2
  • 13
  • 2
    To be honest putting some parameters in `/etc/rc.d/rc.local` is the last thing I'd advice to do nowadays (it used to be the case in 90's). In modern Linux area, there is infrastructure to put parameters in sysfs. – Baptiste Mille-Mathias Jul 14 '18 at 08:26
  • `lineinfile` is to put **1** line, using it with 2 lines will make it non-idempotent and add it will add the 2 lines every time – zigarn Dec 31 '19 at 11:34