Skip to content

Commit 44c7c04

Browse files
committed
added io uring server
1 parent db3b09a commit 44c7c04

File tree

10 files changed

+333
-25
lines changed

10 files changed

+333
-25
lines changed

Dockerfile

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@ RUN apk add --no-cache \
66
clang \
77
llvm \
88
musl-dev \
9-
build-base
9+
build-base \
10+
git \
11+
linux-headers
12+
#liburing-dev
13+
14+
15+
# liburing
16+
RUN git clone https://github.com/axboe/liburing.git && \
17+
cd liburing && \
18+
make && \
19+
make install
20+
#ldconfig
1021

1122
COPY . .
1223

@@ -20,5 +31,5 @@ WORKDIR /home
2031

2132
COPY --from=build /app/simple-server .
2233
COPY --from=build /app/epoll-server .
34+
COPY --from=build /app/uring-server .
2335

24-
#CMD ["./app"]

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ CC = clang
22
CFLAGS = -static -lm -Wall -Wextra -O2
33

44
# files
5-
SOURCES = simple-server.c epoll-server.c #uring-server.c
6-
TARGETS = simple epoll #uring-server
5+
SOURCES = simple-server.c epoll-server.c uring-server.c
6+
TARGETS = simple epoll uring
77

88
all: $(TARGETS)
99

@@ -17,8 +17,8 @@ epoll: epoll-server.c
1717
# $(CC) $(CFLAGS) -o $@ $< -DFILE2_DEFINE -DADDITIONAL_FLAG
1818

1919
uring: uring-server.c
20-
# # $(CC) $(CFLAGS) -o $@ $< -DFILE3_DEFINE -lm
21-
$(CC) -g -o uring-server uring-server.c request-handle.c cpu-bound.c $(CFLAGS) -I/usr/local/include -L/usr/local/lib -luring
20+
$(CC) -o uring-server uring-server.c request-handle.c cpu-bound.c $(CFLAGS) -I/usr/local/include -L/usr/local/lib -luring
21+
# # $(CC) $(CFLAGS) -g -o $@ $< -DFILE3_DEFINE -lm
2222
# # musl-gcc -o rest_server rest_server.c -I/usr/local/include -L/usr/local/lib -luring
2323

2424

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# c-http-server-from-scratch
22

3-
This is a implementation of Http server using directly Linux SystemCalls.
3+
This is a implementation of Http server using Linux SystemCalls.
44

55
There are some implementations:
66
- simple-server.c: Using only accept syscall.
77
- epoll-server.c: Using epoll syscall with 10 events.
8-
- io_uring.c: TODO.
8+
- uring-server.c: Using io_uring (liburing) with queue depth 256.
99

1010
### Default port is 8080.
1111

@@ -19,4 +19,5 @@ There are some implementations:
1919

2020
# Entrypoints:
2121
- /home/simple-server
22-
- /home/epoll-server
22+
- /home/epoll-server
23+
- /home/uring-server

cpu-bound.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
#include <time.h>
4+
#include <stdbool.h>
5+
#include "cpu-bound.h"
6+
7+
//int timeout = 5; seconds
8+
//simulateCPU(timeout);
9+
10+
void simulateCPU(int timeout) {
11+
clock_t start = clock();
12+
int i = 2;
13+
14+
while (1) {
15+
// Verifica se o tempo limite foi atingido
16+
if (((double)(clock() - start) / CLOCKS_PER_SEC) * 1000 > timeout) { // to ms
17+
return;
18+
}
19+
isPrime(i); // Execute CPU-bound work
20+
i++;
21+
}
22+
}
23+
24+
bool isPrime(int n) {
25+
if (n < 2) {
26+
return false;
27+
}
28+
for (int i = 2; i <= (int)sqrt((double)n); i++) {
29+
if (n % i == 0) {
30+
return false;
31+
}
32+
}
33+
return true;
34+
}
35+

cpu-bound.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdbool.h>
2+
3+
#ifndef CPU_BOUND_H
4+
#define CPU_BOUND_H
5+
6+
7+
void simulateCPU(int);
8+
bool isPrime(int);
9+
10+
#endif

epoll-server.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int main() {
9898
exit(EXIT_FAILURE);
9999
}
100100

101-
printf("Servidor ouvindo na porta %d\n", PORT);
101+
printf("epoll server listening on port %d\n", PORT);
102102

103103
while (1) {
104104
int n = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
@@ -113,7 +113,18 @@ int main() {
113113
printf("Nova conexão aceita\n");
114114
} else {
115115
// process request
116-
handle_request(events[i].data.fd);
116+
char buffer[BUFFER_SIZE];
117+
int bytes_read = read(events[i].data.fd, buffer, BUFFER_SIZE - 1);
118+
119+
if (bytes_read < 0) {
120+
perror("Erro ao ler do cliente");
121+
close(events[i].data.fd);
122+
continue;
123+
}
124+
125+
buffer[bytes_read] = '\0';
126+
127+
handle_request(events[i].data.fd, buffer);
117128
}
118129
}
119130
}

request-handle.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,7 @@ char* getParamQueryString(char buffer[BUF_SIZE], const char *param_name) {
5151
return NULL;
5252
}
5353

54-
void handle_request(int client_fd) {
55-
char buffer[BUF_SIZE];
56-
int bytes_read = read(client_fd, buffer, BUF_SIZE - 1);
57-
58-
if (bytes_read < 0) {
59-
perror("Erro ao ler do cliente");
60-
close(client_fd);
61-
return;
62-
}
63-
64-
buffer[bytes_read] = '\0'; // Garante que o buffer seja uma string
54+
void handle_request(int client_fd, char *buffer) {
6555
printf("Requisição recebida:\n%s\n", buffer);
6656

6757
//params

request-handle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
#define REQUEST_HANDLE_H
33

44

5-
void handle_request(int);
5+
void handle_request(int client_fd, char *buffer);
66

77
#endif

simple-server.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ int main() {
7373
exit(EXIT_FAILURE);
7474
}
7575

76-
printf("Servidor ouvindo na porta %d...\n", PORT);
76+
printf("simple server listening on port %d...\n", PORT);
7777

7878
// 5. Loop principal para aceitar conexões
7979
while (1) {
@@ -90,7 +90,19 @@ int main() {
9090
inet_ntoa(client_addr.sin_addr),
9191
ntohs(client_addr.sin_port));
9292

93-
handle_request(client_fd);
93+
94+
char buffer[BUF_SIZE];
95+
int bytes_read = read(client_fd, buffer, BUF_SIZE - 1);
96+
97+
if (bytes_read < 0) {
98+
perror("Erro ao ler do cliente");
99+
close(client_fd);
100+
continue;
101+
}
102+
103+
buffer[bytes_read] = '\0';
104+
105+
handle_request(client_fd, buffer);
94106
}
95107

96108
close(server_fd);

0 commit comments

Comments
 (0)