^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Test selecting other page sizes for mmap/shmget.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) Before running this huge pages for each huge page size must have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) For large pages beyond MAX_ORDER (like 1GB on x86) boot options must be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) Also shmmax must be increased.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) And you need to run as root to work around some weird permissions in shm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) And nothing using huge pages should run in parallel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) When the program aborts you may need to clean up the shm segments with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) ipcrm -m by hand, like this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) sudo ipcs | awk '$1 == "0x00000000" {print $2}' | xargs -n1 sudo ipcrm -m
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) (warning this will remove all if someone else uses them) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define _GNU_SOURCE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <sys/ipc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <sys/shm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <glob.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define err(x) perror(x), exit(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define MAP_HUGE_1GB (30 << MAP_HUGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define MAP_HUGE_SHIFT 26
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define MAP_HUGE_MASK 0x3f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #if !defined(MAP_HUGETLB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define MAP_HUGETLB 0x40000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define SHM_HUGETLB 04000 /* segment will use huge TLB pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define SHM_HUGE_SHIFT 26
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define SHM_HUGE_MASK 0x3f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define SHM_HUGE_2MB (21 << SHM_HUGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define SHM_HUGE_1GB (30 << SHM_HUGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define NUM_PAGESIZES 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define NUM_PAGES 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define Dprintf(fmt...) // printf(fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned long page_sizes[NUM_PAGESIZES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int num_page_sizes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int ilog2(unsigned long v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) while ((1UL << l) < v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) l++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) void find_pagesizes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) glob_t g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) glob("/sys/kernel/mm/hugepages/hugepages-*kB", 0, NULL, &g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) assert(g.gl_pathc <= NUM_PAGESIZES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) for (i = 0; i < g.gl_pathc; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) sscanf(g.gl_pathv[i], "/sys/kernel/mm/hugepages/hugepages-%lukB",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) &page_sizes[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) page_sizes[i] <<= 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) printf("Found %luMB\n", page_sizes[i] >> 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) num_page_sizes = g.gl_pathc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) globfree(&g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned long default_huge_page_size(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unsigned long hps = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) char *line = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) size_t linelen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) FILE *f = fopen("/proc/meminfo", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) while (getline(&line, &linelen, f) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) hps <<= 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) free(line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return hps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) void show(unsigned long ps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) char buf[100];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (ps == getpagesize())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) printf("%luMB: ", ps >> 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) snprintf(buf, sizeof buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) "cat /sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ps >> 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) system(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) unsigned long read_sysfs(int warn, char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) char *line = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) size_t linelen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) char buf[100];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) unsigned long val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) va_start(ap, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) vsnprintf(buf, sizeof buf, fmt, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) f = fopen(buf, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (!f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) printf("missing %s\n", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (getline(&line, &linelen, f) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) sscanf(line, "%lu", &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) fclose(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) free(line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) unsigned long read_free(unsigned long ps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return read_sysfs(ps != getpagesize(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) "/sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) ps >> 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) void test_mmap(unsigned long size, unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) char *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) unsigned long before, after;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) before = read_free(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) map = mmap(NULL, size*NUM_PAGES, PROT_READ|PROT_WRITE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB|flags, -1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (map == (char *)-1) err("mmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) memset(map, 0xff, size*NUM_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) after = read_free(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) Dprintf("before %lu after %lu diff %ld size %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) before, after, before - after, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) assert(size == getpagesize() || (before - after) == NUM_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) show(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) err = munmap(map, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) assert(!err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) void test_shmget(unsigned long size, unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) unsigned long before, after;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) before = read_free(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) id = shmget(IPC_PRIVATE, size * NUM_PAGES, IPC_CREAT|0600|flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (id < 0) err("shmget");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct shm_info i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (shmctl(id, SHM_INFO, (void *)&i) < 0) err("shmctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) Dprintf("alloc %lu res %lu\n", i.shm_tot, i.shm_rss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) Dprintf("id %d\n", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) char *map = shmat(id, NULL, 0600);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (map == (char*)-1) err("shmat");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) shmctl(id, IPC_RMID, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) memset(map, 0xff, size*NUM_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) after = read_free(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) Dprintf("before %lu after %lu diff %ld size %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) before, after, before - after, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) assert(size == getpagesize() || (before - after) == NUM_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) show(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) err = shmdt(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) assert(!err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) void sanity_checks(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) unsigned long largest = getpagesize();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) for (i = 0; i < num_page_sizes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (page_sizes[i] > largest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) largest = page_sizes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (read_free(page_sizes[i]) < NUM_PAGES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) printf("Not enough huge pages for page size %lu MB, need %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) page_sizes[i] >> 20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) NUM_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (read_sysfs(0, "/proc/sys/kernel/shmmax") < NUM_PAGES * largest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) printf("Please do echo %lu > /proc/sys/kernel/shmmax", largest * NUM_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) #if defined(__x86_64__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (largest != 1U<<30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) printf("No GB pages available on x86-64\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) "Please boot with hugepagesz=1G hugepages=%d\n", NUM_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) int main(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) unsigned default_hps = default_huge_page_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) find_pagesizes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) sanity_checks();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) for (i = 0; i < num_page_sizes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) unsigned long ps = page_sizes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int arg = ilog2(ps) << MAP_HUGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) printf("Testing %luMB mmap with shift %x\n", ps >> 20, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) test_mmap(ps, MAP_HUGETLB | arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) printf("Testing default huge mmap\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) test_mmap(default_hps, SHM_HUGETLB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) puts("Testing non-huge shmget");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) test_shmget(getpagesize(), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) for (i = 0; i < num_page_sizes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) unsigned long ps = page_sizes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) int arg = ilog2(ps) << SHM_HUGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) printf("Testing %luMB shmget with shift %x\n", ps >> 20, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) test_shmget(ps, SHM_HUGETLB | arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) puts("default huge shmget");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) test_shmget(default_hps, SHM_HUGETLB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }