免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2564 | 回复: 4
打印 上一主题 下一主题

[内存管理] 请教:madvise为什么老是失败啊?(想调试下ksm) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-01-02 15:36 |只看该作者 |倒序浏览
本帖最后由 learn007 于 2013-01-02 15:36 编辑
  1. #define        MEM_SIZE        (64*1024)
  2. int main(int argc, char **argv)
  3. {
  4.         char *p = malloc(MEM_SIZE);
  5.         int ps = getpagesize();

  6.         printf("pagesize=%d\n", ps);
  7.         if (!p)
  8.         {
  9.                 perror("oom");
  10.                 return -1;
  11.         }

  12.         if (madvise(p, MEM_SIZE, MADV_MERGEABLE) != 0)
  13.         {
  14.                 perror("madvise");
  15.                 return -1;
  16.         }
  17.        
  18.         //advise_merge();
  19.         memset(p, 0, 64*1024);
  20.        
  21.         while (1)
  22.                 sleep(1);
  23.         return 0;
  24. }
复制代码
输出:
pagesize=4096
madvise: Invalid argument

论坛徽章:
1
天蝎座
日期:2013-10-23 21:11:03
2 [报告]
发表于 2013-01-04 20:10 |只看该作者
       MADV_MERGEABLE (since Linux 2.6.32)
              Enable Kernel Samepage Merging (KSM) for the pages in the range specified by addr and length.  The kernel  regu‐
              larly scans those areas of user memory that have been marked as mergeable, looking for pages with identical con‐
              tent.  These are replaced by a single write-protected page (which is automatically copied  if  a  process  later
              wants  to update the content of the page).  KSM only merges private anonymous pages (see mmap(2)).  The KSM fea‐
              ture is intended for applications that generate many instances of the same data  (e.g.,  virtualization  systems
              such  as  KVM).  It can consume a lot of processing power; use with care.  See the kernel source file Documenta‐
              tion/vm/ksm.txt for more details.  The MADV_MERGEABLE and MADV_UNMERGEABLE operations are only available if  the
              kernel was configured with CONFIG_KSM.

可以看看内核有没有编译进KSM

论坛徽章:
16
2015亚冠之吉达阿赫利
日期:2015-08-17 11:21:462015年迎新春徽章
日期:2015-03-04 09:58:11酉鸡
日期:2014-12-07 09:06:19水瓶座
日期:2014-11-04 14:23:29天秤座
日期:2014-03-02 08:57:52双鱼座
日期:2014-02-22 13:07:56午马
日期:2014-02-14 11:08:18双鱼座
日期:2014-02-13 11:09:37卯兔
日期:2014-02-06 15:10:34子鼠
日期:2014-01-20 14:48:19戌狗
日期:2013-12-19 09:37:46射手座
日期:2013-12-19 09:33:47
3 [报告]
发表于 2013-01-04 20:23 |只看该作者
回复 1# learn007


    addr is not page-aligned

论坛徽章:
0
4 [报告]
发表于 2013-01-05 23:40 |只看该作者
回复 3# embeddedlwp


:wink: 后来发现需要用posix_memalign分配

论坛徽章:
16
2015亚冠之吉达阿赫利
日期:2015-08-17 11:21:462015年迎新春徽章
日期:2015-03-04 09:58:11酉鸡
日期:2014-12-07 09:06:19水瓶座
日期:2014-11-04 14:23:29天秤座
日期:2014-03-02 08:57:52双鱼座
日期:2014-02-22 13:07:56午马
日期:2014-02-14 11:08:18双鱼座
日期:2014-02-13 11:09:37卯兔
日期:2014-02-06 15:10:34子鼠
日期:2014-01-20 14:48:19戌狗
日期:2013-12-19 09:37:46射手座
日期:2013-12-19 09:33:47
5 [报告]
发表于 2013-01-06 08:22 |只看该作者
回复 4# learn007


    yup
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP