#include #include #include #include #include #include #include static void *alloc_arena_shm(size_t arena_size, unsigned flags) { FILE *fh; char buf[512]; size_t huge_page_size; char *p; int shmid; void *arena; // find Hugepagesize in /proc/meminfo if ((fh = fopen("/proc/meminfo", "r")) == NULL) { perror("open(/proc/meminfo)"); exit(1); } for (;;) { if (fgets(buf, sizeof(buf)-1, fh) == NULL) { fprintf(stderr, "didn't find Hugepagesize in /proc/meminfo"); exit(1); } buf[sizeof(buf)-1] = '\0'; if (strncmp(buf, "Hugepagesize:", 13) == 0) break; } p = strchr(buf, ':') + 1; huge_page_size = strtoul(p, 0, 0) * 1024; fclose(fh); // round the size up to multiple of huge_page_size arena_size = (arena_size + huge_page_size - 1) & ~(huge_page_size - 1); shmid = shmget(IPC_PRIVATE, arena_size, IPC_CREAT|IPC_EXCL|flags|0600); if (shmid == -1) { perror("shmget"); exit(1); } arena = shmat(shmid, NULL, 0); if (arena == (void *)-1) { perror("shmat"); exit(1); } if (shmctl(shmid, IPC_RMID, 0) == -1) { perror("shmctl warning"); } return arena; } int main(int argc, char **argv) { char buf[1024]; const size_t sz = 64*1024*1024; void *arena = alloc_arena_shm(sz, SHM_HUGETLB); memset(arena, 0, sz); snprintf(buf, sizeof(buf), "grep ^%llx /proc/%d/numa_maps", (unsigned long long)arena, (int)getpid()); system(buf); arena = alloc_arena_shm(sz, 0); memset(arena, 0, sz); snprintf(buf, sizeof(buf), "grep ^%llx /proc/%d/numa_maps", (unsigned long long)arena, (int)getpid()); system(buf); return 0; }