Difference between
modified post e1ff71 by Anonymous on Tue 6th Sep 2022 15:27
original post adc by Anonymous on Tue 6th Sep 2022 15:19
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | /* | |
| 2 | 2 | * hugepage_alloc_test1.c - test the availabilty of | |
| 3 | 3 | * largepages/hugepages | |
| 4 | 4 | * | |
| 5 | 5 | * Written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 6 | 6 | */ | |
| 7 | 7 | ||
| 8 | 8 | #include <stdlib.h> | |
| 9 | 9 | #include <stdio.h> | |
| 10 | 10 | #include <stdbool.h> | |
| 11 | 11 | #include <errno.h> | |
| 12 | 12 | #include <fcntl.h> | |
| 13 | 13 | #include <unistd.h> | |
| 14 | 14 | ||
| 15 | 15 | #include <sys/mman.h> | |
| 16 | 16 | #include <sys/stat.h> | |
| 17 | 17 | ||
| 18 | 18 | #include <linux/mman.h> | |
| 19 | 19 | ||
| 20 | 20 | /* | |
| 21 | 21 | * Before using this Linux 5.10.0-13-rt-686-pae requires that "root" | |
| 22 | 22 | * reserves hugepages like this: | |
| 23 | 23 | * | |
| 24 | 24 | * $ printf '128' > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages | |
| 25 | 25 | * $ cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages | |
| 26 | 26 | * 128 | |
| 27 | 27 | * | |
| 28 | 28 | * Notes: | |
| 29 | 29 | * - Terminology: UNIX uses the term "largepages", Linux defiantly | |
| 30 | 30 | * uses "hugepages" | |
| 31 | 31 | * - the value is the number of MMU (large-/huge-)pages, NOT the | |
| 32 | 32 | * number of bytes. | |
| 33 | 33 | * - $ /usr/bin/pagesize -a # can inform you which pagesizes are | |
| 34 | 34 | * available (i686 usually has { 4k, 2M } pagesizes, SPARCv9 has | |
| 35 | 35 | * { 8k, 64k, 4M, 32M, ... } ) | |
| 36 | 36 | */ | |
| 37 | 37 | ||
| 38 | 38 | /* Linux-specific version */ | |
| 39 | 39 | ssize_t get_number_of_free_hugepages(void) | |
| 40 | 40 | { | |
| 41 | 41 | int dirfd; | |
| 42 | 42 | int fd; | |
| 43 | 43 | /* | |
| 44 | 44 | * |round(log10(18446744073709551615))| is |19| on a 64bit | |
| 45 | 45 | * system, so |128| should be enough (including sign) | |
| 46 | 46 | */ | |
| 47 | 47 | char buf[128]; | |
| 48 | 48 | ssize_t numpages; | |
| 49 | 49 | ssize_t rs; /* read size */ | |
| 50 | 50 | ||
| 51 | 51 | /* | |
| 52 | 52 | * extra step here, since real-world applications likely | |
| 53 | 53 | * want to look at "nr_hugepages" etc, too | |
| 54 | 54 | */ | |
| 55 | 55 | dirfd = open("/sys/kernel/mm/hugepages/hugepages-2048kB/", | |
| 56 | 56 | O_DIRECTORY); | |
| 57 | 57 | if (dirfd < 0) { | |
| 58 | 58 | perror("open sysfs hugepage dir"); | |
| 59 | 59 | return -1; | |
| 60 | 60 | } | |
| 61 | 61 | ||
| 62 | 62 | fd = openat(dirfd, "free_hugepages", O_RDONLY); | |
| 63 | 63 | if (fd < 0) { | |
| 64 | 64 | perror("open sysfs hugepage free pages file"); | |
| 65 | 65 | (void)close(dirfd); | |
| 66 | 66 | return -1; | |
| 67 | 67 | } | |
| 68 | 68 | rs = read(fd, buf, sizeof(buf)); | |
| 69 | 69 | if (rs < 0) { | |
| 70 | 70 | perror("read error sysfs hugepage free pages file"); | |
| 71 | 71 | (void)close(dirfd); | |
| 72 | 72 | (void)close(fd); | |
| 73 | 73 | return -1; | |
| 74 | 74 | } | |
| 75 | 75 | ||
| 76 | 76 | (void)close(dirfd); | |
| 77 | 77 | (void)close(fd); | |
| 78 | 78 | ||
| 79 | 79 | buf[rs] = '\0'; | |
| 80 | 80 | #if MYDEBUG | |
| 81 | 81 | if ((rs > 0) && (buf[rs-1] == '\n')) | |
| 82 | 82 | buf[rs-1] = '\0'; | |
| 83 | 83 | (void)printf("string returned: |%s|\n", buf); | |
| 84 | 84 | #endif /* MYDEBUG */ | |
| 85 | 85 | ||
| 86 | 86 | errno = 0; | |
| 87 | 87 | numpages = strtol(buf, NULL, 10); | |
| 88 | 88 | if (errno != 0) { | |
| 89 | 89 | perror("error parsing value of free hugepages"); | |
| 90 | 90 | return -1; | |
| 91 | 91 | } | |
| 92 | 92 | ||
| 93 | 93 | return numpages; | |
| 94 | 94 | } | |
| 95 | 95 | ||
| 96 | 96 | ||
| 97 | 97 | int main(int ac, char *av[]) | |
| 98 | 98 | { | |
| 99 | 99 | void *p; | |
| 100 | 100 | int saved_errno; | |
| 101 | 101 | #define TEST_NUM_ALLOC_PAGES (32) | |
| 102 | 102 | const size_t size = TEST_NUM_ALLOC_PAGES*1024*1024; | |
| 103 | 103 | ssize_t free_numpages; | |
| 104 | 104 | ||
| 105 | 105 | (void)puts("#start."); | |
| 106 | 106 | ||
| 107 | 107 | free_numpages = get_number_of_free_hugepages(); | |
| 108 | 108 | ||
| 109 | 109 | if (free_numpages < (TEST_NUM_ALLOC_PAGES+8)) { | |
| 110 | 110 | (void)fprintf(stderr, | |
| 111 | 111 | "not enough hugepages, currently free %ld\n", | |
| 112 | 112 | (long)free_numpages); | |
| 113 | 113 | return EXIT_FAILURE; | |
| 114 | 114 | } | |
| 115 | 115 | ||
| 116 | 116 | /* get mmap()'ed memory with hugepage attribute */ | |
| 117 | 117 | p = mmap(0, | |
| 118 | 118 | size, | |
| 119 | 119 | PROT_READ|PROT_WRITE, | |
| 120 | 120 | MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB, -1, 0); | |
| 121 | 121 | saved_errno = errno; | |
| 122 | 122 | if (p == MAP_FAILED) { | |
| 123 | 123 | perror("mmap"); | |
| 124 | 124 | return EXIT_FAILURE; | |
| 125 | 125 | } | |
| 126 | 126 | ||
| 127 | 127 | (void)printf("map address = 0x%lx, errno=%d\n", | |
| 128 | 128 | (unsigned long)p, (int)saved_errno); | |
| 129 | 129 | ||
| 130 | 130 | /* use memory */ | |
| 131 | 131 | volatile unsigned char *c = p; | |
| 132 | 132 | for (size_t i = 0 ; i < size ; i++) { | |
| 133 | 133 | c[i] = (unsigned char)(i % 255); | |
| 134 | 134 | } | |
| 135 | 135 | ||
| 136 | 136 | #if MYDEBUG_USE_PROC_SMAPS | |
| 137 | 137 | /* | |
| 138 | 138 | * Wait here so we can check out | |
| 139 | 139 | * $ cat /proc/$mypid/smaps | fgrep -i huge # | |
| 140 | 140 | */ | |
| 141 | 141 | sleep(1000); | |
| 142 | 142 | #endif /* MYDEBUG_USE_PROC_SMAPS */ | |
| 143 | 143 | ||
| 144 | 144 | if (munmap(p, size) == -1) | |
| 145 | 145 | perror("munmap"); | |
| 146 | 146 | ||
| 147 | 147 | (void)puts("#done."); | |
| 148 | 148 | ||
| 149 | 149 | return EXIT_SUCCESS; | |
| 150 | 150 | } | |
| 151 | 151 |
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.
rovema.kpaste.net RSS