我正在尝试在Linux C中编写代码。基本上我试图绘制一个应该显示的统计信息:pid, number of process, page fault(major/minor) and total number of page faults.
val, pid, pagefault, number of processes, total number of pages faults(Majpr+Minor)
1 127 major 1 2323
对于一个想法,我从https://unix.stackexchange.com/questions/188170/generate-major-page-faults及其代码中获取了代码解决方案:
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
int main(int argc, char ** argv) {
int fd = open(argv[1], O_RDONLY);
struct stat stats;
fstat(fd, &stats);
posix_fadvise(fd, 0, stats.st_size, POSIX_FADV_DONTNEED);
char * map = (char *) mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
perror("Failed to mmap");
return 1;
}
int result = 0;
int i;
for (i = 0; i < stats.st_size; i++) {
result += map[i];
}
munmap(map, stats.st_size);
return result;
}
这段代码确实提供了太多东西。我也看到了这个链接sol:Measure page faults from a c program 但未能让我知道如何获得页面错误(主要/次要)。谁能告诉我如何获得主要和次要的错误?