0

I was compiling and running this program but received 'mmap error : cannot allocate memory'. The comment at the top reads

/*
 * Example of using hugepage memory in a user application using the mmap
 * system call with MAP_HUGETLB flag.  Before running this program make
 * sure the administrator has allocated enough default sized huge pages
 * to cover the 256 MB allocation.
 *
 * For ia64 architecture, Linux kernel reserves Region number 4 for hugepages.
 * That means the addresses starting with 0x800000... will need to be
 * specified.  Specifying a fixed address is not required on ppc64, i386
 * or x86_64.
 */

I want to check if the administrator has allocated enough default sized huge pages to cover the 256 MB allocation but I am the system administrator. What should I do? I'm on ubuntu 20.04 x86_64 machine. ( a side question : Does mmap use heap area?)

ADD : please see my comment (I added a boot command argument and the code works. I temporarily added boot argument in the grub menu.) but I wish I could add a init script so that this takes effect every time the computer boots.

Chan Kim
  • 5,177
  • 12
  • 57
  • 112
  • 1
    Did you read [the documentation for huge pages](https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt)? – Joseph Sible-Reinstate Monica Jul 15 '21 at 20:02
  • @JosephSible-ReinstateMonica Hi, thanks! I've read the document( not to the end) and added 'hugepages=16' in the boot command . I and I had change the LENGTH in the code to 2M. (256M was to big). And the code works! Thanks! – Chan Kim Jul 16 '21 at 03:01
  • 1
    Kernel command line parameters can be added to `/etc/default/grub`. – Nate Eldredge Jul 16 '21 at 03:08
  • @NateEldredge Hi, thanks! I added your method in the answer. – Chan Kim Jul 16 '21 at 03:28
  • 1
    As for your side question "Does mmap use heap area?". No, `mmap` creates a new region, you can think of it as being positioned between the `heap` and `stack` regions. – Titan3 Jul 19 '21 at 17:30

1 Answers1

0

There seem to be 2 methods.

  1. add vm.nr_hugepages = 16 in /etc/sysctrl.conf and reboot.
    I've checked this works.

  2. (as Nate Eldredge commented) add 'hugepages=16' in GRUB_CMDLINE_LINUX="" line (inside quotes) of /etc/default/grub and do update-grub.

Chan Kim
  • 5,177
  • 12
  • 57
  • 112