Start probing page fault behaviour
This commit is contained in:
		
							
								
								
									
										62
									
								
								haversine_02/src/memtester/main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								haversine_02/src/memtester/main.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,62 @@
 | 
			
		||||
#include "aliases.h"
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <sys/mman.h>
 | 
			
		||||
#include <sys/resource.h>
 | 
			
		||||
#include <sys/time.h>
 | 
			
		||||
 | 
			
		||||
#define PAGESIZE 4096
 | 
			
		||||
 | 
			
		||||
typedef struct rusage rusage_t;
 | 
			
		||||
 | 
			
		||||
u64 page_fault_count() {
 | 
			
		||||
  rusage_t usage;
 | 
			
		||||
 | 
			
		||||
  getrusage(RUSAGE_SELF, &usage);
 | 
			
		||||
 | 
			
		||||
  return usage.ru_minflt + usage.ru_majflt;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[]) {
 | 
			
		||||
  if (argc < 2 || argc > 2) {
 | 
			
		||||
    printf("Usage: %s [NUMBER OF PAGES TO ALLOCATE]\n", argv[0]);
 | 
			
		||||
 | 
			
		||||
    return EXIT_FAILURE;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  u64 page_count = atol(argv[1]);
 | 
			
		||||
  u64 alloc_size = page_count * PAGESIZE;
 | 
			
		||||
  u64 touch_size = 0;
 | 
			
		||||
 | 
			
		||||
  printf("Page Count,Touch Count,Fault Count,Extra Faults\n");
 | 
			
		||||
 | 
			
		||||
  for (u64 touch_count = 0; touch_count <= page_count; ++touch_count) {
 | 
			
		||||
    touch_size = touch_count * PAGESIZE;
 | 
			
		||||
 | 
			
		||||
    u8 *data = (u8 *)mmap(NULL, alloc_size, PROT_READ | PROT_WRITE,
 | 
			
		||||
                          MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
 | 
			
		||||
 | 
			
		||||
    if (!data) {
 | 
			
		||||
      printf("Failed to allocate memory\n");
 | 
			
		||||
 | 
			
		||||
      return EXIT_FAILURE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    u64 fault_start = page_fault_count();
 | 
			
		||||
 | 
			
		||||
    for (u64 i = 0; i < touch_size; ++i) {
 | 
			
		||||
      data[i] = (u8)i;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    u64 fault_end = page_fault_count();
 | 
			
		||||
 | 
			
		||||
    u64 faults = fault_end - fault_start;
 | 
			
		||||
 | 
			
		||||
    printf("%lu,%lu,%lu,%ld\n", page_count, touch_count, faults,
 | 
			
		||||
           ((i64)faults - touch_count));
 | 
			
		||||
 | 
			
		||||
    munmap((void *)data, alloc_size);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return EXIT_SUCCESS;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user