PatchworkOS  19e446b
A non-POSIX operating system.
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 MMAP_ITER 10000
7#define GETPID_ITER 100000
8
9#ifdef _PATCHWORK_OS_
10#include <sys/fs.h>
11#include <sys/proc.h>
12
13static fd_t zeroDev;
14
15static void init_generic()
16{
17 zeroDev = open("/dev/const/zero");
18 if (zeroDev == ERR)
19 {
20 perror("Failed to open /dev/const/zero");
21 abort();
22 }
23}
24
25static void* mmap_generic(size_t length)
26{
27 void* ptr = mmap(zeroDev, NULL, length, PROT_READ | PROT_WRITE);
28 if (ptr == NULL)
29 {
30 return NULL;
31 }
32 return ptr;
33}
34
35static uint64_t munmap_generic(void* addr, size_t length)
36{
37 return munmap(addr, length) == NULL ? ERR : 0;
38}
39
40static void benchmark_getpid(void)
41{
43
44 for (uint64_t i = 0; i < GETPID_ITER; i++)
45 {
46 getpid();
47 }
48
49 clock_t end = clock();
50 printf("getpid: %llums\n", (end - start) / (CLOCKS_PER_MS));
51
52 clock_t procStart = clock();
53
54 char buffer[32];
55 for (uint64_t i = 0; i < GETPID_ITER; i++)
56 {
57 readfile("/proc/self/pid", buffer, sizeof(buffer), 0);
58 }
59
60 clock_t procEnd = clock();
61 printf("/proc/self/pid: %llums\n", (procEnd - procStart) / (CLOCKS_PER_MS));
62
63 printf("overhead: %lluns\n", ((procEnd - procStart) - (end - start)) / GETPID_ITER);
64}
65
66#else
67
68#include <fcntl.h>
69#include <sys/mman.h>
70#include <unistd.h>
71#define ERR ((uint64_t)-1)
72
73static void init_generic()
74{
75 // Nothing to do
76}
77
78static void* mmap_generic(size_t length)
79{
80 void* ptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
81 if (ptr == MAP_FAILED)
82 {
83 return NULL;
84 }
85 return ptr;
86}
87
88static uint64_t munmap_generic(void* addr, size_t length)
89{
90 return munmap(addr, length) == -1 ? ERR : 0;
91}
92
93#endif
94
96{
98
99 for (uint64_t i = 0; i < MMAP_ITER; i++)
100 {
101 void* ptr = mmap_generic(pages * 0x1000);
102 if (ptr == NULL)
103 {
104 perror("mmap failed");
105 return;
106 }
107
108 for (uint64_t j = 0; j < pages; j++)
109 {
110 ((uint8_t*)ptr)[j * 0x1000] = 0;
111 }
112
113 if (munmap_generic(ptr, pages * 0x1000) != 0)
114 {
115 perror("munmap failed");
116 return;
117 }
118 }
119
120 clock_t end = clock();
121 printf("mmap pages=%llu bytes: %llums\n", pages, (end - start) / (CLOCKS_PER_MS));
122}
123
124int main()
125{
126 init_generic();
127
128#ifdef _PATCHWORK_OS_
129 benchmark_getpid();
130#endif
131
133 for (uint64_t i = 50; i <= 1500; i += 50)
134 {
136 }
137
138 return 0;
139}
EFI_PHYSICAL_ADDRESS buffer
Definition main.c:237
int main(void)
Definition main.c:5
static void start()
Definition main.c:542
#define CLOCKS_PER_MS
Definition clock_t.h:16
fd_t open(const char *path)
System call for opening files.
Definition open.c:8
size_t readfile(const char *path, void *buffer, size_t count, size_t offset)
Wrapper for reading a file directly using a path.
Definition readfile.c:3
void * mmap(fd_t fd, void *address, size_t length, prot_t prot)
System call to map memory from a file.
Definition mmap.c:6
pid_t getpid(void)
System call to retrieve the current pid.
Definition getpid.c:6
void * munmap(void *address, size_t length)
System call to unmap mapped memory.
Definition munmap.c:6
@ PROT_READ
Readable memory.
Definition proc.h:124
@ PROT_WRITE
Writable memory.
Definition proc.h:125
#define NULL
Pointer error value.
Definition NULL.h:25
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ fd_t
File descriptor type.
Definition fd_t.h:10
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static fd_t zeroDev
Definition heap.c:40
static page_t * pages
Definition pmm.c:38
static void init_generic()
Definition main.c:73
#define MMAP_ITER
Definition main.c:6
static uint64_t munmap_generic(void *addr, size_t length)
Definition main.c:88
static void benchmark_mmap(uint64_t pages)
Definition main.c:95
static void * mmap_generic(size_t length)
Definition main.c:78
#define GETPID_ITER
Definition main.c:7
__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:3
_PUBLIC void perror(const char *s)
Definition perror.c:5
_PUBLIC _NORETURN void abort(void)
Definition abort.c:9
_PUBLIC clock_t clock(void)
Definition clock.c:5