Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kernel THP] Recommend the following additional settings for Go programs. #5437

Open
kmlebedev opened this issue Mar 29, 2024 · 0 comments
Open

Comments

@kmlebedev
Copy link
Contributor

It is necessary to configure THP when building rpm or deb packages https://go.dev/doc/gc-guide#Linux_transparent_huge_pages

One can enable or disable transparent huge pages in a Linux environment by modifying.
/sys/kernel/mm/transparent_hugepage/enabled. See the official Linux admin guide for more details. If you choose to have your Linux production environment enable transparent huge pages, we recommend the following additional settings for Go programs.

Set /sys/kernel/mm/transparent_hugepage/defrag to defer or defer+madvise.

This setting controls how aggressively a Linux kernel coalesces regular pages into huge pages. defer tells the kernel to coalesce huge pages lazily and in the background. A more aggressive setting can induce stalls in memory constrained systems and can often hurt application latencies. defer+madvise is like defer, but is friendlier to other applications on the system that request huge pages explicitly and require them for performance.

Set /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none to 0.

This setting controls how many additional pages the Linux kernel daemon can allocate when trying to allocate a huge page. The default setting is maximally aggressive, and can often undo work the Go runtime does to return memory to the OS. Before Go 1.21, the Go runtime tried to mitigate the negative effects of the default setting, but it came with a CPU cost. With Go 1.21+ and Linux 6.2+, the Go runtime no longer mutates huge page state.

If you experience an increase in memory usage when upgrading to Go 1.21.1 or later, try applying this setting; it will likely resolve your issue. As an additional workaround, you can call the Prctl function with PR_SET_THP_DISABLE to disable huge pages at the process level, or you can set GODEBUG=disablethp=1 (to be added in Go 1.21.6 and Go 1.22) to disable huge pages for heap memory. Note that the GODEBUG setting may be removed in a future release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant