pastebin - collaborative debugging tool
rovema.kpaste.net RSS


Linux hugepage alloc demo1
Posted by Anonymous on Tue 6th Sep 2022 14:06
raw | new post
view followups (newest first): Linux hugepage alloc demo1 by Anonymous

  1. /*
  2.  * hugepage_alloc_test1.c - test the availabilty of hugepages
  3.  *
  4.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  5.  */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <stdbool.h>
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13.  
  14. #include <sys/mman.h>
  15. #include <sys/stat.h>
  16.  
  17. #include <linux/mman.h>
  18.  
  19. /*
  20.  * Before using this Linux 5.10.0-13-rt-686-pae requires that "root"
  21.  * reserves hugepages like this:
  22.  *
  23.  * $ printf '128' > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
  24.  * $ cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
  25.  * 128
  26.  *
  27.  * Notes:
  28.  * - Terminology: UNIX uses the term "largepages", Linux defiantly uses
  29.  * "hugepages"
  30.  * - the value is the number of MMU (large-/huge-)pages, NOT the number
  31.  * of bytes.
  32.  * - $ /usr/bin/pagesize -a # can inform you which pagesizes are
  33.  * available (i686 usually has { 4k, 2M } pagesizes, SPARCv9 has
  34.  * { 8k, 64k, 4M, 32M, ... } )
  35.  */
  36.  
  37. ssize_t get_number_of_free_hugepages(void)
  38. {
  39.         int     dirfd;
  40.         int     fd;
  41.         char    buf[256];
  42.         ssize_t numpages;
  43.         ssize_t rs; /* read size */
  44.        
  45.         /*
  46.          * extra step here, since real-world applications likely
  47.          * want to look at "nr_hugepages" etc, too
  48.          */
  49.         dirfd = open("/sys/kernel/mm/hugepages/hugepages-2048kB/",
  50.                 O_DIRECTORY);
  51.         if (dirfd < 0) {
  52.                 perror("open sysfs hugepage dir");
  53.                 return -1;
  54.         }
  55.        
  56.         fd = openat(dirfd, "free_hugepages", O_RDONLY);
  57.         if (fd < 0) {
  58.                 perror("open sysfs hugepage free pages file");
  59.                 (void)close(dirfd);
  60.                 return -1;
  61.         }
  62.         rs = read(fd, buf, sizeof(buf));
  63.         if (rs < 0) {
  64.                 perror("read error sysfs hugepage free pages file");
  65.                 (void)close(dirfd);
  66.                 (void)close(fd);
  67.                 return -1;
  68.         }
  69.  
  70.         (void)close(dirfd);
  71.         (void)close(fd);
  72.        
  73.         buf[rs] = '\0';
  74. #if MYDEBUG
  75.         if ((rs > 0) && (buf[rs-1] == '\n'))
  76.                 buf[rs-1] = '\0';
  77.         (void)printf("string returned: |%s|\n", buf);
  78. #endif /* MYDEBUG */
  79.  
  80.         errno = 0;
  81.         numpages = strtol(buf, NULL, 10);
  82.         if (errno != 0) {
  83.                 perror("error parsing value of free hugepages");
  84.                 return -1;
  85.         }
  86.        
  87.         return numpages;
  88. }
  89.  
  90.  
  91. int main(int ac, char *av[])
  92. {
  93.         void *p;
  94.         int saved_errno;
  95. #define TEST_NUM_ALLOC_PAGES (32)
  96.         size_t size = TEST_NUM_ALLOC_PAGES*1024*1024;
  97.         ssize_t free_numpages;
  98.  
  99.         (void)puts("#start.");
  100.        
  101.         free_numpages = get_number_of_free_hugepages();
  102.  
  103.         if (free_numpages < (TEST_NUM_ALLOC_PAGES+8)) {
  104.                 fprintf(stderr, "not enough hugepages\n");
  105.                 return EXIT_FAILURE;
  106.         }
  107.        
  108.         p = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB, -1, 0);
  109.         saved_errno = errno;
  110.         if (p == MAP_FAILED)
  111.                 perror("mmap");
  112.  
  113.         (void)printf("map address = %lx, errno=%d\n",
  114.                 (unsigned long)p, (int)saved_errno);
  115.  
  116.         if (munmap(p, size) == -1)
  117.                 perror("munmap");
  118.        
  119.         (void)puts("#done.");
  120.  
  121.         return EXIT_SUCCESS;
  122. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at