1
0

add page fault prober

This commit is contained in:
Rokas Puzonas 2023-09-24 20:05:29 +03:00
parent a7d0e49ab9
commit 46578d7ce9
5 changed files with 66 additions and 11 deletions

View File

@ -22,5 +22,9 @@ build/guess_cpu_speed: src/guess_cpu_speed.c src/timer.c
mkdir -p build mkdir -p build
gcc -o build/guess_cpu_speed src/guess_cpu_speed.c $(CFLAGS) gcc -o build/guess_cpu_speed src/guess_cpu_speed.c $(CFLAGS)
build/page_fault_prober: src/page_fault_prober.c
mkdir -p build
gcc -o build/page_fault_prober src/page_fault_prober.c $(CFLAGS)
clean: clean:
rm -r build rm -r build

49
src/page_fault_prober.c Normal file
View File

@ -0,0 +1,49 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#define RPROF_IMPLEMENTATION
#include "rprof.h"
int main(int argc, char **argv) {
if (argc != 3) {
printf("Usage: %s <output-file> <page-count>\n", argv[0]);
return -1;
}
uint64_t page_size = sysconf(_SC_PAGESIZE);
char *output_path = argv[1];
uint64_t page_count = atol(argv[2]);
char *memory = mmap(NULL, page_size * page_count, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0);
if (memory == NULL) {
perror("mmap");
return -1;
}
// char *memory = malloc(page_size * page_count);
// if (memory == NULL) {
// perror("malloc");
// return -1;
// }
FILE *f = fopen(output_path, "w");
if (f == NULL) {
perror("fopen");
return -1;
}
for (int i = 0; i < page_count; i++) {
uint64_t fault_count = read_page_faults();
memory[i*page_size] = i;
uint64_t page_fault_diff = read_page_faults() - fault_count;
char line[128];
int line_length = snprintf(line, sizeof(line), "%d,%lu\n", i+1, page_fault_diff);
fwrite(line, line_length, 1, f);
}
return 0;
}

View File

@ -71,15 +71,6 @@ void print_repetition_time(char *label, float cpu_time, uint64_t bytes, uint64_t
} }
} }
uint64_t read_page_faults() {
// NOTE: ru_minflt the number of page faults serviced without any I/O activity.
// ru_majflt the number of page faults serviced that required I/O activity.
struct rusage usage = {};
getrusage(RUSAGE_SELF, &usage);
int Result = usage.ru_minflt + usage.ru_majflt;
return Result;
}
bool repetition_tester_continue(struct repetition_tester *tester, struct repetition_results *results) { bool repetition_tester_continue(struct repetition_tester *tester, struct repetition_results *results) {
if (tester->state != STATE_RUNNING) { if (tester->state != STATE_RUNNING) {
return false; return false;

View File

@ -142,6 +142,19 @@ static uint64_t rprof_get_cpu_timer_hz(uint64_t measure_time_ms)
} }
} }
// ------------------------ Page faults -------------------------
#include <sys/resource.h>
uint64_t read_page_faults() {
// NOTE: ru_minflt the number of page faults serviced without any I/O activity.
// ru_majflt the number of page faults serviced that required I/O activity.
struct rusage usage = {};
getrusage(RUSAGE_SELF, &usage);
int Result = usage.ru_minflt + usage.ru_majflt;
return Result;
}
// ------------------------ Profiling ------------------------- // ------------------------ Profiling -------------------------
void rprof_init() void rprof_init()

View File

@ -166,7 +166,5 @@ int main()
} }
} }
printf("page faults: %lu\n", read_page_faults());
return 0; return 0;
} }