Commit 70368d2
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
325 | 325 | | |
326 | 326 | | |
327 | 327 | | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
328 | 364 | | |
329 | 365 | | |
330 | 366 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
| 1 | + | |
9 | 2 | | |
10 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
0 commit comments