我有一個計算缺頁服務時間C. 對於這個程序的C程序我有2個大文件(每個不到3GB的 - 幾乎與RAM的大小)MMAP:無法分配內存
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "rdstc.h"
#include "config.h"
#define KB 1024
#define MB 1024 * KB
#define GB 1024 * MB
#define SIZE_OF_MEMORY 1 * GB // Main memory size
#define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
int main(int argc, char *argv[]){
int fd1, fd2;
char *addr1, *addr2, c;
int i, j;
long long unsigned int s_t, e_t, t=0;
if (argc != 3){
printf("usage: a.out <file1> <file2> \n");
exit(EXIT_FAILURE);
}
if ((fd1 = open(argv[1], O_RDONLY)) == -1){
handle_error("open");
}
if ((fd2 = open(argv[2], O_RDONLY)) == -1){
handle_error("open");
}
posix_fadvise(fd1, 0, 0, POSIX_FADV_RANDOM);
posix_fadvise(fd2, 0, 0, POSIX_FADV_RANDOM);
addr1 = (char *) mmap(0, SIZE_OF_MEMORY, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd1, 0);
if (addr1 == MAP_FAILED){
handle_error("mmap");
}
addr2 = (char *) mmap(0, SIZE_OF_MEMORY, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd2, 0);
if (addr2 == MAP_FAILED){
handle_error("mmap");
}
madvise(addr1, 0, MADV_RANDOM);
madvise(addr2, 0, MADV_RANDOM);
j = 32; // default read ahead size if 256 blocks (assuming each block is of 512 bytes)
for(i = 0; i < ITERATIONS; i++){
s_t = rdtsc();
c = addr1[i + j*4*KB]; // read at multiple of page size, so every read causes a page fault
j *= 2;
e_t = rdtsc();
t += (e_t - s_t);
}
printf("Time required to service a page faut is %f \n", (t/ITERATIONS)/CPU_FREQ);
munmap(addr1, SIZE_OF_MEMORY);
munmap(addr2, SIZE_OF_MEMORY);
return 0;
}
我得到以下編譯器警告:
[email protected]:~/projects/mem$ gcc mem1_4.c -lm
mem1_4.c: In function ‘main’:
mem1_4.c:11:17: warning: integer overflow in expression [-Woverflow]
#define MB 1024 * KB
^
mem1_4.c:12:19: note: in expansion of macro ‘MB’
#define GB 1024 * MB
^
mem1_4.c:13:28: note: in expansion of macro ‘GB’
#define SIZE_OF_MEMORY 2 * GB // Main memory size
^
mem1_4.c:40:30: note: in expansion of macro ‘SIZE_OF_MEMORY’
addr1 = (char *) mmap(0, SIZE_OF_MEMORY, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd1, 0);
^
mem1_4.c:11:17: warning: integer overflow in expression [-Woverflow]
#define MB 1024 * KB
^
mem1_4.c:12:19: note: in expansion of macro ‘MB’
#define GB 1024 * MB
^
mem1_4.c:13:28: note: in expansion of macro ‘GB’
#define SIZE_OF_MEMORY 2 * GB // Main memory size
^
mem1_4.c:44:30: note: in expansion of macro ‘SIZE_OF_MEMORY’
addr2 = (char *) mmap(0, SIZE_OF_MEMORY, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd2, 0);
^
mem1_4.c:11:17: warning: integer overflow in expression [-Woverflow]
#define MB 1024 * KB
^
mem1_4.c:12:19: note: in expansion of macro ‘MB’
#define GB 1024 * MB
^
mem1_4.c:13:28: note: in expansion of macro ‘GB’
#define SIZE_OF_MEMORY 2 * GB // Main memory size
^
mem1_4.c:62:19: note: in expansion of macro ‘SIZE_OF_MEMORY’
munmap(addr1, SIZE_OF_MEMORY);
^
mem1_4.c:11:17: warning: integer overflow in expression [-Woverflow]
#define MB 1024 * KB
^
mem1_4.c:12:19: note: in expansion of macro ‘MB’
#define GB 1024 * MB
^
mem1_4.c:13:28: note: in expansion of macro ‘GB’
#define SIZE_OF_MEMORY 2 * GB // Main memory size
^
mem1_4.c:63:19: note: in expansion of macro ‘SIZE_OF_MEMORY’
munmap(addr2, SIZE_OF_MEMORY);
^
當我用命令來運行它,我得到的錯誤
./a.out file1.txt file2.txt
mmap: Cannot allocate memory
代碼是做什麼的? 我們使用標誌
MAP_PRIVATE映射這兩個文件(所以沒有其他進程應該訪問此文件)和MAP_POPULATE(使
當我們調用mmap()的完整文件映射到存儲器中)與PROT_READ保護沿旗。
首先我們映射file1,並且由於我們使用MAP_POPULATE,完整的RAM由與此文件對應的數據填充。在此之後,我們使用相同的標誌映射file2,因此現在我們將file2完全映射到RAM中。因此,訪問file1的數據將導致頁面錯誤,因爲file2佔用了所有可用的RAM。我們還調用帶有MADV_RANDOM標誌設置的madvise()系統調用,以告知內核不要爲這兩個文件讀取頁面。因此,現在一旦這個初始設置完成,file2佔用所有可用的RAM,我們隨機訪問與file1相對應的數據(以避免由內核執行的預讀讀取優化的任何效果,並且也避免從L3緩存讀取)。因爲RAM充滿數據對應於file2,每次訪問與文件對應的數據都會導致頁面錯誤。我們在循環中的映射區域執行10個隨機讀數,並測量此操作所需的平均時間。
編譯器警告應該給你一個提示 - 2 * 1024 * 1024 * 1024溢出到負數。 – immibis