PatchworkOS
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1#include <stdint.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <time.h>
5
6#define TEST_ITERATIONS 10000
7
8#ifdef __PATCHWORK_OS__
9#include <sys/io.h>
10#include <sys/proc.h>
11
12static fd_t zeroDev;
13
14static void init_generic()
15{
16 zeroDev = open("/dev/zero");
17 if (zeroDev == ERR)
18 {
19 perror("Failed to open /dev/zero");
20 abort();
21 }
22}
23
24static void* mmap_generic(size_t length)
25{
26 void* ptr = mmap(zeroDev, NULL, length, PROT_READ | PROT_WRITE);
27 if (ptr == NULL)
28 {
29 return NULL;
30 }
31 return ptr;
32}
33
34static uint64_t munmap_generic(void* addr, size_t length)
35{
36 return munmap(addr, length);
37}
38
39#else
40
41#include <fcntl.h>
42#include <sys/mman.h>
43#include <unistd.h>
44#define ERR ((uint64_t)-1)
45
46static void init_generic()
47{
48 // Nothing to do
49}
50
51static void* mmap_generic(size_t length)
52{
53 void* ptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
54 if (ptr == MAP_FAILED)
55 {
56 return NULL;
57 }
58 return ptr;
59}
60
61static uint64_t munmap_generic(void* addr, size_t length)
62{
63 return munmap(addr, length) == -1 ? ERR : 0;
64}
65
66#endif
67
68static void benchmark_mmap(uint64_t pages)
69{
71
72 for (uint64_t i = 0; i < TEST_ITERATIONS; i++)
73 {
74 void* ptr = mmap_generic(pages * 0x1000);
75 if (ptr == NULL)
76 {
77 perror("mmap failed");
78 return;
79 }
80
81 for (uint64_t j = 0; j < pages; j++)
82 {
83 ((uint8_t*)ptr)[j * 0x1000] = 0;
84 }
85
86 if (munmap_generic(ptr, pages * 0x1000) != 0)
87 {
88 perror("munmap failed");
89 return;
90 }
91 }
92
93 clock_t end = clock();
94 printf("mmap pages=%llu bytes: %llums\n", pages, (end - start) / (CLOCKS_PER_SEC / 1000));
95}
96
97int main()
98{
100
101 printf("Starting mmap benchmark with %llu iterations\n", TEST_ITERATIONS);
103 for (uint64_t i = 50; i <= 1500; i += 50)
104 {
106 }
107
108 return 0;
109}
#define CLOCKS_PER_SEC
Definition clock_t.h:15
fd_t open(const char *path)
System call for opening files.
Definition open.c:9
void * mmap(fd_t fd, void *address, uint64_t length, prot_t prot)
System call to map memory from a file.
Definition mmap.c:6
uint64_t munmap(void *address, uint64_t length)
System call to unmap mapped memory.
Definition munmap.c:6
@ PROT_READ
Memory can be read from.
Definition proc.h:172
@ PROT_WRITE
Memory can be written to.
Definition proc.h:173
#define NULL
Pointer error value.
Definition NULL.h:23
__UINT64_TYPE__ fd_t
A file descriptor.
Definition fd_t.h:12
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static fd_t zeroDev
Definition heap.c:37
static void init_generic()
Definition main.c:46
#define TEST_ITERATIONS
Definition main.c:6
static uint64_t munmap_generic(void *addr, size_t length)
Definition main.c:61
#define ERR
Definition main.c:44
static void benchmark_mmap(uint64_t pages)
Definition main.c:68
static void * mmap_generic(size_t length)
Definition main.c:51
int main()
Definition main.c:97
static void start()
Definition main.c:542
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
_PUBLIC int printf(const char *_RESTRICT format,...)
Definition printf.c:5
_PUBLIC void perror(const char *s)
Definition perror.c:5
_PUBLIC _NORETURN void abort(void)
Definition abort.c:7
_PUBLIC clock_t clock(void)
Definition clock.c:5