https://www.kernel.org/doc/html/latest/admin-guide/mm/ksm.html

示例:https://github.com/Rtoax/test/tree/master/linux/mm/ksm

目录

Kernel Samepage Merging

Overview

Controlling KSM with madvise

KSM daemon sysfs interface


#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>int main(int argc, char *argv[])
{char *buf;char filename[64] = "";struct stat stat;int size = 100 * 4096;int fd = 0;strcpy(filename, argv[1]);fd = open(filename, O_RDWR|O_CREAT, 0664);fstat(fd, &stat);buf = mmap(NULL, stat.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0);memset(buf, 0x55, stat.st_size);madvise(buf, stat.st_size, MADV_MERGEABLE);while(1) {sleep(1);}
}#if 0
Kernel SamePage Merging - 同页合并-----------
1. 编译源代码gcc test.c 2. 使用 dd 命令创建 ksm.dat.out 文件 , 大小 100Mdd if=/dev/zero of=ksm.dat.out bs=1M count=1003. 使能 KSMecho 1 > /sys/kernel/mm/ksm/run4. 运行 test.c 程序./a.out ksm.dat.out5. 查看系统有多少页被 合并 了cat /sys/kernel/mm/ksm/pages_sharingcat /sys/kernel/mm/ksm/pages_sharedcat /sys/kernel/mm/ksm/pages_unshared
#endif

Kernel Samepage Merging

Overview

KSM is a memory-saving de-duplication feature, enabled by CONFIG_KSM=y, added to the Linux kernel in 2.6.32. See mm/ksm.c for its implementation, and http://lwn.net/Articles/306704/ and https://lwn.net/Articles/330589/

KSM was originally developed for use with KVM (where it was known as Kernel Shared Memory), to fit more virtual machines into physical memory, by sharing the data common between them. But it can be useful to any application which generates many instances of the same data.

The KSM daemon ksmd periodically scans those areas of user memory which have been registered with it, looking for pages of identical content which can be replaced by a single write-protected page (which is automatically copied if a process later wants to update its content). The amount of pages that KSM daemon scans in a single pass and the time between the passes are configured using sysfs intraface

KSM only merges anonymous (private) pages, never pagecache (file) pages. KSM’s merged pages were originally locked into kernel memory, but can now be swapped out just like other user pages (but sharing is broken when they are swapped back in: ksmd must rediscover their identity and merge again).

Controlling KSM with madvise

KSM only operates on those areas of address space which an application has advised to be likely candidates for merging, by using the madvise(2) system call:

int madvise(addr, length, MADV_MERGEABLE)

The app may call

int madvise(addr, length, MADV_UNMERGEABLE)

to cancel that advice and restore unshared pages: whereupon KSM unmerges whatever it merged in that range. Note: this unmerging call may suddenly require more memory than is available - possibly failing with EAGAIN, but more probably arousing the Out-Of-Memory killer.

If KSM is not configured into the running kernel, madvise MADV_MERGEABLE and MADV_UNMERGEABLE simply fail with EINVAL. If the running kernel was built with CONFIG_KSM=y, those calls will normally succeed: even if the KSM daemon is not currently running, MADV_MERGEABLE still registers the range for whenever the KSM daemon is started; even if the range cannot contain any pages which KSM could actually merge; even if MADV_UNMERGEABLE is applied to a range which was never MADV_MERGEABLE.

If a region of memory must be split into at least one new MADV_MERGEABLE or MADV_UNMERGEABLE region, the madvise may return ENOMEM if the process will exceed vm.max_map_count (see Documentation for /proc/sys/vm/).

Like other madvise calls, they are intended for use on mapped areas of the user address space: they will report ENOMEM if the specified range includes unmapped gaps (though working on the intervening mapped areas), and might fail with EAGAIN if not enough memory for internal structures.

Applications should be considerate in their use of MADV_MERGEABLE, restricting its use to areas likely to benefit. KSM’s scans may use a lot of processing power: some installations will disable KSM for that reason.

KSM daemon sysfs interface

The KSM daemon is controlled by sysfs files in /sys/kernel/mm/ksm/, readable by all but writable only by root:

pages_to_scan

how many pages to scan before ksmd goes to sleep e.g.

echo 100 > /sys/kernel/mm/ksm/pages_to_scan.

Default: 100 (chosen for demonstration purposes)

sleep_millisecs

how many milliseconds ksmd should sleep before next scan e.g.

echo 20 > /sys/kernel/mm/ksm/sleep_millisecs

Default: 20 (chosen for demonstration purposes)

merge_across_nodes

specifies if pages from different NUMA nodes can be merged. When set to 0, ksm merges only pages which physically reside in the memory area of same NUMA node. That brings lower latency to access of shared pages. Systems with more nodes, at significant NUMA distances, are likely to benefit from the lower latency of setting 0. Smaller systems, which need to minimize memory usage, are likely to benefit from the greater sharing of setting 1 (default). You may wish to compare how your system performs under each setting, before deciding on which to use. merge_across_nodes setting can be changed only when there are no ksm shared pages in the system: set run 2 to unmerge pages first, then to 1 after changing merge_across_nodes, to remerge according to the new setting.

Default: 1 (merging across nodes as in earlier releases)

run

  • set to 0 to stop ksmd from running but keep merged pages,

  • set to 1 to run ksmd e.g. echo 1 > /sys/kernel/mm/ksm/run,

  • set to 2 to stop ksmd and unmerge all pages currently merged, but leave mergeable areas registered for next run.

Default: 0 (must be changed to 1 to activate KSM, except if CONFIG_SYSFS is disabled)

use_zero_pages

specifies whether empty pages (i.e. allocated pages that only contain zeroes) should be treated specially. When set to 1, empty pages are merged with the kernel zero page(s) instead of with each other as it would happen normally. This can improve the performance on architectures with coloured zero pages, depending on the workload. Care should be taken when enabling this setting, as it can potentially degrade the performance of KSM for some workloads, for example if the checksums of pages candidate for merging match the checksum of an empty page. This setting can be changed at any time, it is only effective for pages merged after the change.

Default: 0 (normal KSM behaviour as in earlier releases)

max_page_sharing

Maximum sharing allowed for each KSM page. This enforces a deduplication limit to avoid high latency for virtual memory operations that involve traversal of the virtual mappings that share the KSM page. The minimum value is 2 as a newly created KSM page will have at least two sharers. The higher this value the faster KSM will merge the memory and the higher the deduplication factor will be, but the slower the worst case virtual mappings traversal could be for any given KSM page. Slowing down this traversal means there will be higher latency for certain virtual memory operations happening during swapping, compaction, NUMA balancing and page migration, in turn decreasing responsiveness for the caller of those virtual memory operations. The scheduler latency of other tasks not involved with the VM operations doing the virtual mappings traversal is not affected by this parameter as these traversals are always schedule friendly themselves.

stable_node_chains_prune_millisecs

specifies how frequently KSM checks the metadata of the pages that hit the deduplication limit for stale information. Smaller milllisecs values will free up the KSM metadata with lower latency, but they will make ksmd use more CPU during the scan. It’s a noop if not a single KSM page hit the max_page_sharing yet.

The effectiveness of KSM and MADV_MERGEABLE is shown in /sys/kernel/mm/ksm/:

pages_shared

how many shared pages are being used

pages_sharing

how many more sites are sharing them i.e. how much saved

pages_unshared

how many pages unique but repeatedly checked for merging

pages_volatile

how many pages changing too fast to be placed in a tree

full_scans

how many times all mergeable areas have been scanned

stable_node_chains

the number of KSM pages that hit the max_page_sharing limit

stable_node_dups

number of duplicated KSM pages

A high ratio of pages_sharing to pages_shared indicates good sharing, but a high ratio of pages_unshared to pages_sharing indicates wasted effort. pages_volatile embraces several different kinds of activity, but a high proportion there would also indicate poor use of madvise MADV_MERGEABLE.

The maximum possible pages_sharing/pages_shared ratio is limited by the max_page_sharing tunable. To increase the ratio max_page_sharing must be increased accordingly.

– Izik Eidus, Hugh Dickins, 17 Nov 2009

Kernel Samepage Merging | 同页合并相关推荐

  1. 虚拟机——KSM Kernel Samepage Merging

    KSM--Kernel Samepage Merging KSM is a memory-saving de-duplication feature,将有相同数据的多个虚拟机通过共享共同数据的方式融合 ...

  2. KSM(Kernel Samepage Merging) 剖析:Linux 内核中的内存去耦合

    简介:作为一个系统管理程序(hypervisor),Linux® 有几个创新,2.6.32 内核中一个有趣的变化是 KSM(Kernel Samepage Merging)  允许这个系统管理程序通过 ...

  3. KSM(Kernel samepage merging)

    简介:作为一个系统管理程序(hypervisor),Linux® 有几个创新,2.6.32 内核中一个有趣的变化是 KSM(Kernel Samepage Merging)  允许这个系统管理程序通过 ...

  4. innodb一页为什么要存储两行记录_InnoDB中的页合并与分裂

    原文标题:InnoDB Page Merging and Page Splitting 原文链接:https://www.percona.com/blog/2017/04/10/innodb-page ...

  5. MySql 笔记(五)InnoDB引擎页分裂与页合并的原理

    MySql 笔记(五)InnoDB引擎页分裂与页合并的原理 一.文件表(File-Table)结构 二.根,分支与叶子 三.页的内部原理 四.页合并的原理 五.页分裂的原理 六.主键的使用 七.总结 ...

  6. mysql 页分裂_[翻译] InnoDB 页合并与页分裂

    原文标题:InnoDB Page Merging and Page Splitting 作者:Marco Tusa 译者:2014BDuck 翻译时间:2019-12-22 备注:因为 V 站限制注册 ...

  7. Innodb页合并和页分裂

    作者:Marco Tusa . Sri Sakthivel 译者:孟维克,知数堂优秀校友 原文链接: https://www.percona.com/blog/2017/04/10/innodb-pa ...

  8. python合并excel工作簿_使用Python将多个excel的多个sheet页合并到一个excel

    使用Python将多个excel的多个sheet页合并到一个excel ##读取Excel文件 import xlrd ##将文本写入excel文件 import xlsxwriter ##读取exc ...

  9. brighthouse mysql_MySQL 数据库中的数据页合并-爱可生

    原标题:MySQL 数据库中的数据页合并-爱可生 MySQL InnoDB 表数据页或者二级索引页(简称数据页或者索引页)的合并与分裂对 InnoDB 表整体性能影响很大:数据页的这类操作越多,对 I ...

最新文章

  1. 建立注册DLL和反注册DLL文件的快捷方式
  2. 【坑爹微信】微信支付相关问题解决
  3. js中常用的算法排序
  4. 自己在windows下第一次安装pygame成功的经历
  5. Java这些高端技术只有你还不知道,移动架构师成长路线
  6. Mybatis中Mapper动态代理方式
  7. 计算机无法共享磁盘分区,win7系统下怎么创建虚拟磁盘分区以达到网络共享的目的...
  8. redis 3.0.0 linux下载,CentOS7安装配置redis-3.0.0
  9. ai python 面试_面试分享系列 | 17道Python面试题,让你在求职中无往不利
  10. android系统app打开蓝牙+设置可见性
  11. # 淘宝客导购小程序最新版本
  12. 水面船舶雷达监视-信天翁雷达监视系统
  13. Axure8.0汉化包+注册码
  14. word文档通配符换行_[Word] 学习笔记
  15. 用ARCGIS做DEM地形分析
  16. python实现mysql多条件查询筛选功能
  17. 海思方案技术研发交流群/海思方案供需交流群
  18. 1224 红魔馆的纸牌游戏 (计算24点,dfs)
  19. mysql嵌套查询效率低,连接查询代替嵌套查询提高select效率
  20. 全国计算机等级考试office无纸化考试,全国计算机等级考试真题汇编与专用题库 二级MS Office高级应用 2016年无纸化考试专用...

热门文章

  1. java.io.file.sync_java.io.FileDescriptor#sync()是否特定于单个FileDescriptor
  2. mysql中最新7天的数据_mysql中最新7天的数据库
  3. 模块字体不生效_新版iPhone敬业签便签软件怎么将字体调大?
  4. Spring包名代码提示
  5. 分治之快速排序以及快速排序为何最快
  6. 关键字 标识符 数据类型
  7. 页面重绘和回流以及优化
  8. 我眼中BA(业务需求分析师)的技能广度和深度
  9. Frame - 快速创建高品质的 Web 应用原型
  10. 如何实现图像访问计数器