前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ksm over-commit

ksm over-commit

作者头像
Laikee
发布2022-04-25 17:44:13
3950
发布2022-04-25 17:44:13
举报
文章被收录于专栏:Laikee Tech SpaceLaikee Tech Space

1 Intro

1.1 KSM

KSM (Kernel Samepage Merging) is running in the Linux kernel and scans the memory of all the virtual machines running on a single host, looking for duplication and consolidating. With KSM we’re able to improve virtual machine density by as much as 300% without impacting performance. One of the great benefits of using Linux as the hypervisor means KSM is not limited to KVM and virtual machines, but can also reduce memory pressure with normal Linux applications. It was integrated in PVE since version 1.5, and is implemented with the package “ksm-control-daemon” (check your version with the cli command “pveversion -v”). [1]

1.2 KSM in action

Just install several KVM virtual machines with the same OS (using at least 80% of your physical memory on the host) and wait a few minutes. You will notice higher CPU activities on the host (ksm daemon) and the used memory on the host will be lowered significantly (see start page showing the overall memory usage). Howto verify that KSM is working (how many pages are being shared between your KVM guests):

watch cat /sys/kernel/mm/ksm/pages_sharing Note: a page is 4096 bytes

The file /etc/ksmtuned.conf allows for some customization of its behaviour. [1]

2 Tuning KSM in PVE

2.1 Tuning

The KSM configuration file is located at /etc/ksmtuned.conf on PVE

To edit/modify/tune the configuration for KSM on PVE we can use following command

nano /etc/ksmtuned.conf We will see following contents

代码语言:javascript
复制
# Configuration file for ksmtuned.
# How long ksmtuned should sleep between tuning adjustments
# KSM_MONITOR_INTERVAL=60
# Millisecond sleep between ksm scans for 16Gb server.
# Smaller servers sleep more, bigger sleep less.
# KSM_SLEEP_MSEC=100
# KSM_NPAGES_BOOST=300
# KSM_NPAGES_DECAY=-50
# KSM_NPAGES_MIN=64
# KSM_NPAGES_MAX=1250
# KSM_THRES_COEF=20
# KSM_THRES_CONST=2048
# uncomment the following if you want ksmtuned debug info
# LOGFILE=/var/log/ksmtuned
# DEBUG=1

KSM_THRES_COEF=20 表示当只有20%内存的时候开始使用ksm压缩

KSM_THRES_COEF=20 represents that when there is only 20% free memory left, KSM will kick in (In other words, when memory utilization is 80% / a threshold to activate ksm)

We can change this number to fit our needs, in most cases, the default value is good enough and stable.

Note: ksmd is activated if either of the following occurs

The amount of free memory drops below the threshold, set in KSM_THRES_CONST. The amount of committed memory plus the threshold, KSM_THRES_CONST, exceeds the total amount of memory.

2.2 Other values explained

代码语言:javascript
复制
# Configuration file for ksmtuned.
# How long ksmtuned should sleep between tuning adjustments
# KSM_MONITOR_INTERVAL=60
# Millisecond sleep between ksm scans for 16Gb server.
# Smaller servers sleep more, bigger sleep less.
# KSM_SLEEP_MSEC=10
# KSM_NPAGES_BOOST - is added to the `npages` value, when `free memory` is less than `thres`.
# KSM_NPAGES_BOOST=300
# KSM_NPAGES_DECAY - is the value given is subtracted to the `npages` value, when `free memory` is greater than `thres`.
# KSM_NPAGES_DECAY=-50
# KSM_NPAGES_MIN - is the lower limit for the `npages` value.
# KSM_NPAGES_MIN=64
# KSM_NPAGES_MAX - is the upper limit for the `npages` value.
# KSM_NPAGES_MAX=1250
# KSM_THRES_COEF - is the RAM percentage to be calculated in parameter `thres`.
# KSM_THRES_COEF=20
# KSM_THRES_CONST - If this is a low memory system, and the `thres` value is less than `KSM_THRES_CONST`, then reset `thres` value to `KSM_THRES_CONST` value.
# KSM_THRES_CONST=2048
# uncomment the following to enable ksmtuned debug information
# LOGFILE=/var/log/ksmtuned
# DEBUG=1

3 Monitoring KSM status

Kernel same-page merging (KSM) stores monitoring data in the /sys/kernel/mm/ksm/ directory. Files in this directory are updated by the kernel and are an accurate record of KSM usage and statistics. The variables in the list below are also configurable variables in the /etc/ksmtuned.conf file, as noted above.

Files in /sys/kernel/mm/ksm/:

代码语言:javascript
复制
Name    Value
full_scans  Full scans run
merge_across_nodes  Whether pages from different NUMA nodes can be merged
pages_shared    Total pages shared
pages_sharing   Pages currently shared
pages_to_scan   Pages not scanned
pages_unshared  Pages no longer shared
pages_volatile  Number of volatile pages
run Whether the KSM process is running
sleep_millisecs Sleep milliseconds

[2]

4 Disable KSM

代码语言:javascript
复制
systemctl disable ksmtuned
reboot

5 Enable KSM

代码语言:javascript
复制
systemctl enable ksmtuned
reboot

6 References

[1] “Dynamic Memory Management – Proxmox VE”, Pve.proxmox.com, 2020. [Online]. Available: https://pve.proxmox.com/wiki/Dynamic_Memory_Management#KSM

[2] “8.3. Kernel Same-page Merging (KSM) Red Hat Enterprise Linux 7 | Red Hat Customer Portal”, Red Hat Customer Portal, 2020. [Online]. Available: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/virtualization_tuning_and_optimization_guide/chap-ksm

[3] https://zhuanlan.zhihu.com/p/62829713

[4] https://blog.csdn.net/liukuan73/article/details/90315886

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-07-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 Intro
    • 1.1 KSM
      • 1.2 KSM in action
      • 2 Tuning KSM in PVE
        • 2.1 Tuning
          • 2.2 Other values explained
          • 3 Monitoring KSM status
          • 4 Disable KSM
          • 5 Enable KSM
          • 6 References
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档