diff --git a/Makefile b/Makefile index 3480bf1..844761e 100644 --- a/Makefile +++ b/Makefile @@ -22,5 +22,9 @@ build/guess_cpu_speed: src/guess_cpu_speed.c src/timer.c mkdir -p build 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: rm -r build diff --git a/src/page_fault_prober.c b/src/page_fault_prober.c new file mode 100644 index 0000000..9612d22 --- /dev/null +++ b/src/page_fault_prober.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include + +#define RPROF_IMPLEMENTATION +#include "rprof.h" + +int main(int argc, char **argv) { + if (argc != 3) { + printf("Usage: %s \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; +} diff --git a/src/repetition_tester.c b/src/repetition_tester.c index 6f66684..1e5eae4 100644 --- a/src/repetition_tester.c +++ b/src/repetition_tester.c @@ -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) { if (tester->state != STATE_RUNNING) { return false; diff --git a/src/rprof.h b/src/rprof.h index 4cf72cd..fe94603 100644 --- a/src/rprof.h +++ b/src/rprof.h @@ -142,6 +142,19 @@ static uint64_t rprof_get_cpu_timer_hz(uint64_t measure_time_ms) } } +// ------------------------ Page faults ------------------------- + +#include + +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 ------------------------- void rprof_init() diff --git a/src/tests.c b/src/tests.c index 1291196..be9a4b1 100644 --- a/src/tests.c +++ b/src/tests.c @@ -166,7 +166,5 @@ int main() } } - printf("page faults: %lu\n", read_page_faults()); - return 0; }