尝试映射大页面时,mmap失败(1GB) [英] mmap failed when trying to map huge page (1GB)

查看:676
本文介绍了尝试映射大页面时,mmap失败(1GB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了什么:1.使用root启用大页面(我的系统支持1MB大页面)

What I did: 1. Enable huge page with root (my system supports 1MB huge page)

$echo 20 > /proc/sys/vm/nr_hugepages

2.将大页面文件系统安装到/mnt/hugepages

2.Mount huge page filesystem to /mnt/hugepages

$mount -t hugetlbfs nodev /mnt/hugepages

3.在大页面文件系统中创建文件

3.Create a file in huge page filesystem

$touch /mnt/hugepages/hello

4.然后使用mmap将一个巨大的页面映射到地址0,如下面的代码所示

4.Then map a huge page using mmap to address 0 as shown in the code below

   #define FILE_NAME "/mnt/hugepages/hello"
   #define PROTECTION (PROT_READ | PROT_WRITE) // page flag
   #define LENGTH (1024*1024*1024)     // huge page size
   #define FLAGS (MAP_SHARED)      //page flag
   #define ADDR (void *) (0x0UL)   //starting address of the page

   fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);    
   if (fd < 0) {                    // 
         perror("Open failed");
         exit(1);
    }

   // allocate a buffer using huge pages
   buf = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0);
   if (buf == MAP_FAILED) {
           perror("mmap");
           unlink(FILE_NAME);
           exit(1);
    }

程序输出: mmap:无法分配内存

The program outputs: mmap: Cannot allocate memory

推荐答案

Linux仅支持大型页面用于私有匿名映射(不由文件支持). IE.您只能为堆栈,数据和堆启用大表.

Linux only supports huge pages for private anonymous mappings (not backed by a file). I.e. you can only enable huge tables for stack, data and heap.

如今,有 hugeadm 可以配置系统巨大的页面池,无需摆弄/procmount.并且 hugectl 可以使用巨大的页面来存储代码和数据.

Nowadays, there is hugeadm to configure the system huge page pools, no need to fiddle with /proc and mount. And hugectl to use huge pages for code and data.

这篇关于尝试映射大页面时,mmap失败(1GB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆