72 lines
1.7 KiB
C
72 lines
1.7 KiB
C
|
|
// pf_test.c - Page Fault Investigation
|
|||
|
|
#include "hakmem.h"
|
|||
|
|
#include <stdio.h>
|
|||
|
|
#include <stdlib.h>
|
|||
|
|
#include <string.h>
|
|||
|
|
#include <unistd.h>
|
|||
|
|
|
|||
|
|
#define SIZE (2 * 1024 * 1024)
|
|||
|
|
|
|||
|
|
static void get_pf(unsigned long *soft, unsigned long *hard) {
|
|||
|
|
FILE* f = fopen("/proc/self/stat", "r");
|
|||
|
|
if (!f) { *soft = 0; *hard = 0; return; }
|
|||
|
|
|
|||
|
|
unsigned long minflt = 0, majflt = 0;
|
|||
|
|
(void)fscanf(f, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %lu %*u %lu",
|
|||
|
|
&minflt, &majflt);
|
|||
|
|
fclose(f);
|
|||
|
|
*soft = minflt;
|
|||
|
|
*hard = majflt;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void test_hakmem() {
|
|||
|
|
unsigned long pf_before, pf_after, hard_before, hard_after;
|
|||
|
|
|
|||
|
|
printf("\n=== hakmem Test ===\n");
|
|||
|
|
hak_init();
|
|||
|
|
|
|||
|
|
get_pf(&pf_before, &hard_before);
|
|||
|
|
|
|||
|
|
for (int i = 0; i < 10; i++) {
|
|||
|
|
void* buf = hak_alloc_cs(SIZE);
|
|||
|
|
memset(buf, 0xEF, SIZE);
|
|||
|
|
hak_free_cs(buf, SIZE);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
get_pf(&pf_after, &hard_after);
|
|||
|
|
|
|||
|
|
printf("Page Faults: %lu (soft), %lu (hard)\n",
|
|||
|
|
pf_after - pf_before, hard_after - hard_before);
|
|||
|
|
|
|||
|
|
hak_shutdown();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void test_system() {
|
|||
|
|
unsigned long pf_before, pf_after, hard_before, hard_after;
|
|||
|
|
|
|||
|
|
printf("\n=== system malloc Test ===\n");
|
|||
|
|
|
|||
|
|
get_pf(&pf_before, &hard_before);
|
|||
|
|
|
|||
|
|
for (int i = 0; i < 10; i++) {
|
|||
|
|
void* buf = malloc(SIZE);
|
|||
|
|
memset(buf, 0xEF, SIZE);
|
|||
|
|
free(buf);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
get_pf(&pf_after, &hard_after);
|
|||
|
|
|
|||
|
|
printf("Page Faults: %lu (soft), %lu (hard)\n",
|
|||
|
|
pf_after - pf_before, hard_after - hard_before);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int main() {
|
|||
|
|
printf("=== Page Fault Comparison ===\n");
|
|||
|
|
printf("Size: 2MB × 10 iterations\n");
|
|||
|
|
|
|||
|
|
test_system();
|
|||
|
|
test_hakmem();
|
|||
|
|
|
|||
|
|
return 0;
|
|||
|
|
}
|