大頁記憶體效果對比---ok
阿新 • • 發佈:2022-02-25
https://blog.csdn.net/mrpre/article/details/83586778
效能效果:hugepages》mmap》normal
gcc xxx
sudo perf record -e dTLB-loads -e faults ./a.out
sudo perf report
normal:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <inttypes.h> 4 #include <sys/types.h> 5 #include <unistd.h> 6#include <fcntl.h> 7 #include <sys/mman.h> 8 #include <malloc.h> 9 void access_mem(char *ptr, unsigned long memsize) 10 { 11 unsigned long i; 12 for (i = 0; i < memsize; i+=4096) { 13 ptr[i] = 1; 14 } 15 16 } 17 int main() { 18 unsigned long memsize = 1000000000; 19 20 char *my_string = malloc(memsize); 21 access_mem(my_string, memsize); 22 } 23 24 ~
mmap:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <inttypes.h> 4 #include <sys/types.h> 5 #include <unistd.h> 6 #include <fcntl.h> 7 #include <sys/mman.h> 8 9 void access_mem(char*ptr, unsigned long memsize) 10 { 11 unsigned long i; 12 for (i = 0; i < memsize; i+=4096) { 13 ptr[i] = 1; 14 } 15 16 } 17 int main() { 18 unsigned long memsize = 1000000000; 19 unsigned long i; 20 21 int fd = open("/dev/hugepages/fileA", O_CREAT|O_RDWR, 0600); 22 23 char *my_string = mmap(0, memsize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); 24 25 access_mem(my_string, memsize); 26 }
hugepages:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <inttypes.h> 4 #include <sys/types.h> 5 #include <unistd.h> 6 #include <fcntl.h> 7 #include <sys/mman.h> 8 9 void access_mem(char *ptr, unsigned long memsize) 10 { 11 unsigned long i; 12 for (i = 0; i < memsize; i+=4096) { 13 ptr[i] = 1; 14 } 15 16 } 17 int main() { 18 unsigned long memsize = 1000000000; 19 unsigned long i; 20 21 22 char *my_string = mmap(0, memsize, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB/**/, -1, 0); 23 24 access_mem(my_string, memsize); 25 26 sleep(20); 27 munmap(my_string, memsize); 28 29 }有時候,不小心知道了一些事,才發現自己所在乎的事是那麼可笑。