Skip to content

Commit 70368d2

Browse files
hunleydCopilot
andauthored
feat(tuned): enable tuned and disable THP (#2060)
* feat(tuned): enable tuned and disable THP Implement a specialized tuned profile for PostgreSQL to disable Transparent Huge Pages (THP) and ensure proper service ordering. Background: Transparent Huge Pages (THP) In Linux, memory is typically managed in 4KB pages. Huge Pages allow the system to manage memory in much larger chunks (e.g., 2MB or 1GB), which reduces the overhead of the Translation Lookaside Buffer (TLB). Transparent Huge Pages (THP) is a kernel feature that attempts to automate this by dynamically allocating huge pages for processes. While beneficial for some workloads, it is generally detrimental to database systems like PostgreSQL for several reasons: 1. Memory Bloat & Fragmentation: THP can lead to significant memory waste. If a process only needs a small portion of a 2MB page, the entire 2MB is still allocated. In highly concurrent database environments, this often leads to rapid memory exhaustion. 2. Latency Spikes (khugepaged): The kernel's khugepaged thread periodically scans memory to "collapse" standard pages into huge pages. This process can cause unpredictable latency spikes and CPU contention, often referred to as "stalls," which are unacceptable for high-performance database transactions. 3. Inefficient I/O: PostgreSQL manages its own shared buffers and is optimized for 8KB blocks. The mismatch between the kernel's 2MB THP allocations and PostgreSQL's internal memory management can lead to inefficient paging and increased I/O pressure during page faults. Summary of Changes 1. PostgreSQL Service Ordering (ansible/tasks/setup-postgres.yml) * SystemD Overrides: Created a directory /etc/systemd/system/postgresql.service.d to house service customizations. * Dependency Management: Added a Unit dependency (After=tuned.service) via overrides.conf. This ensures that the tuned profile—and the resulting kernel optimizations—are fully applied before the PostgreSQL daemon initializes. 2. PostgreSQL-Specific Tuned Profile (ansible/tasks/setup-tuned.yml) * Profile Definition: Created a new tuned profile directory and configuration at /etc/tuned/postgresql/tuned.conf. * THP Deactivation: * Runtime: Sets vm.transparent_hugepages=never to disable THP at the kernel level immediately. * Boot-time: Appends transparent_hugepages=never to the bootloader command line to ensure THP remains disabled after system reboots. * Service Activation: Restarts the tuned service to register the new profile and executes tuned-adm profile postgresql to apply the optimizations. These changes ensure a more stable and predictable performance profile for PostgreSQL by preventing kernel-level memory management interference. * Update ansible/tasks/setup-postgres.yml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update ansible/tasks/setup-postgres.yml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update ansible/tasks/setup-postgres.yml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update ansible/tasks/setup-tuned.yml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update ansible/tasks/setup-tuned.yml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * test(tuned): verify tuned profile and THP settings Add test cases to test_ami_nix.py to ensure the 'postgresql' tuned profile is active and Transparent Huge Pages (THP) are set to 'never', validating the recent performance optimizations. * Update test_ami_nix.py --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 578022e commit 70368d2

2 files changed

Lines changed: 100 additions & 9 deletions

File tree

ansible/tasks/setup-postgres.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,42 @@
325325

326326

327327
# Reload
328+
- name: Create a SystemD override dir for PostgreSQL
329+
ansible.builtin.file:
330+
group: 'root'
331+
mode: '0755'
332+
owner: 'root'
333+
path: '/etc/systemd/system/postgresql.service.d'
334+
state: 'directory'
335+
become: true
336+
337+
- name: Ensure PostgreSQL starts after tuned
338+
become: true
339+
community.general.ini_file:
340+
create: true
341+
group: 'root'
342+
mode: '0644'
343+
no_extra_spaces: true
344+
option: 'After'
345+
owner: 'root'
346+
path: '/etc/systemd/system/postgresql.service.d/override.conf'
347+
section: 'Unit'
348+
state: 'present'
349+
value: 'tuned.service'
350+
351+
- name: Ensure PostgreSQL wants tuned
352+
become: true
353+
community.general.ini_file:
354+
create: true
355+
group: 'root'
356+
mode: '0644'
357+
no_extra_spaces: true
358+
option: 'Wants'
359+
owner: 'root'
360+
path: '/etc/systemd/system/postgresql.service.d/overrides.conf'
361+
section: 'Unit'
362+
state: 'present'
363+
value: 'tuned.service'
328364
- name: System - systemd reload
329365
ansible.builtin.systemd_service:
330366
daemon_reload: true

ansible/tasks/setup-tuned.yml

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,65 @@
1-
- name: Install tuned
2-
ansible.builtin.apt:
3-
force_apt_get: true
4-
name: tuned
5-
policy_rc_d: 101
6-
state: 'present'
7-
update_cache: true
8-
become: true
1+
- name: Install and configure tuned when stage2_nix
92
when:
10-
- stage2_nix
3+
- (stage2_nix or nixpkg_mode)
4+
block:
5+
- name: Install tuned
6+
ansible.builtin.apt:
7+
force_apt_get: true
8+
name: 'tuned'
9+
policy_rc_d: 101
10+
state: 'present'
11+
update_cache: true
12+
become: true
13+
14+
- name: Create a tuned profile directory
15+
ansible.builtin.file:
16+
group: 'root'
17+
mode: '0755'
18+
owner: 'root'
19+
path: '/etc/tuned/postgresql'
20+
state: 'directory'
21+
become: true
22+
23+
- name: Create a tuned profile
24+
community.general.ini_file:
25+
create: true
26+
group: 'root'
27+
mode: '0644'
28+
no_extra_spaces: true
29+
option: 'summary'
30+
path: '/etc/tuned/postgresql/tuned.conf'
31+
section: 'main'
32+
state: 'present'
33+
value: 'Tuned profile for PostgreSQL'
34+
become: true
35+
36+
- name: Disable Transparent Huge Pages (THP)
37+
community.general.ini_file:
38+
create: true
39+
group: 'root'
40+
mode: '0644'
41+
no_extra_spaces: true
42+
option: "{{ thp_item['option'] }}"
43+
path: '/etc/tuned/postgresql/tuned.conf'
44+
section: "{{ thp_item['section'] }}"
45+
state: 'present'
46+
value: "{{ thp_item['value'] }}"
47+
become: true
48+
loop:
49+
- { section: 'bootloader', option: 'cmdline', value: 'transparent_hugepage=never' }
50+
- { section: 'vm', option: 'transparent_hugepages', value: 'never' }
51+
loop_control:
52+
loop_var: 'thp_item'
53+
54+
- name: Activate the tuned service
55+
ansible.builtin.systemd_service:
56+
daemon_reload: true
57+
enabled: true
58+
name: 'tuned'
59+
state: 'restarted'
60+
become: true
61+
62+
- name: Activate the PostgreSQL tuned profile
63+
ansible.builtin.command:
64+
cmd: tuned-adm profile postgresql
65+
become: true

0 commit comments

Comments
 (0)