测试内存池
This commit is contained in:
110
reactor.c
110
reactor.c
@@ -1,6 +1,6 @@
|
||||
|
||||
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <errno.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/eventfd.h>
|
||||
#include <sys/timerfd.h>
|
||||
|
||||
#include "server.h"
|
||||
|
||||
@@ -33,6 +34,9 @@ typedef int (*msg_handler)(struct conn* conn);
|
||||
|
||||
static msg_handler kvs_handler;
|
||||
|
||||
extern void cleanup_finished_iouring_tasks();
|
||||
|
||||
|
||||
// 0 need more, -1 error, =1 suc
|
||||
int kvs_request(struct conn *c) {
|
||||
// int consumed_out = kvs_handler(c->rbuffer, c->rlength, c->wbuffer, &c->wlength);
|
||||
@@ -61,7 +65,7 @@ int epfd = 0;
|
||||
struct timeval begin;
|
||||
|
||||
int wakeup_fd = -1;
|
||||
|
||||
int timer_fd = -1;
|
||||
|
||||
struct conn conn_list[CONNECTION_SIZE] = {0};
|
||||
// fd
|
||||
@@ -296,31 +300,6 @@ int send_cb(int fd) {
|
||||
|
||||
// wakup fd
|
||||
|
||||
int handle_wakeup_fd_cb(int fd);
|
||||
|
||||
int init_wakeup_fd(void) {
|
||||
wakeup_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
|
||||
if (wakeup_fd < 0) {
|
||||
printf("eventfd failed: errno=%d %s\n", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
conn_list[wakeup_fd].fd = wakeup_fd;
|
||||
conn_list[wakeup_fd].r_action.recv_callback = handle_wakeup_fd_cb;
|
||||
set_event(wakeup_fd, EPOLLIN, 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// EPOLLOUT
|
||||
void sync_wakeup(int fd) {
|
||||
if (wakeup_fd < 0) return;
|
||||
set_event(fd, EPOLLOUT, 0);
|
||||
|
||||
uint64_t one = 1;
|
||||
ssize_t n = write(wakeup_fd, &one, sizeof(one));
|
||||
}
|
||||
|
||||
int handle_wakeup_fd_cb(int fd) {
|
||||
uint64_t v;
|
||||
while (1) {
|
||||
@@ -329,9 +308,65 @@ int handle_wakeup_fd_cb(int fd) {
|
||||
if (n < 0 && errno == EAGAIN) break; // 已经读空
|
||||
break;
|
||||
}
|
||||
cleanup_finished_iouring_tasks();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int init_wakeup_fd(void) {
|
||||
int wfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
|
||||
if (wfd < 0) {
|
||||
printf("eventfd failed: errno=%d %s\n", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
conn_list[wfd].fd = wfd;
|
||||
conn_list[wfd].r_action.recv_callback = handle_wakeup_fd_cb;
|
||||
set_event(wfd, EPOLLIN, 1);
|
||||
|
||||
return wfd;
|
||||
}
|
||||
|
||||
// EPOLLOUT
|
||||
void sync_wakeup() {
|
||||
if (wakeup_fd < 0) return;
|
||||
// set_event(wakeup_fd, EPOLLOUT, 0);
|
||||
|
||||
uint64_t one = 1;
|
||||
ssize_t n = write(wakeup_fd, &one, sizeof(one));
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 定时器
|
||||
int handle_timer_fd_cb(int fd){
|
||||
uint64_t v;
|
||||
while (1) {
|
||||
ssize_t n = read(fd, &v, sizeof(v));
|
||||
if (n == sizeof(v)) {
|
||||
continue;
|
||||
}
|
||||
if (n < 0 && errno == EAGAIN) break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int init_timer_fd(void){
|
||||
int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
|
||||
|
||||
struct itimerspec its = {
|
||||
.it_interval = {1, 0}, // 每 1 秒
|
||||
.it_value = {1, 0}, // 1 秒后首次触发
|
||||
};
|
||||
timerfd_settime(tfd, 0, &its, NULL);
|
||||
|
||||
conn_list[tfd].fd = tfd;
|
||||
conn_list[tfd].r_action.recv_callback = handle_timer_fd_cb;
|
||||
set_event(tfd, EPOLLIN, 1);
|
||||
|
||||
return tfd;
|
||||
}
|
||||
|
||||
int r_init_server(unsigned short port) {
|
||||
|
||||
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
@@ -365,11 +400,19 @@ int reactor_start(unsigned short port, msg_handler handler) {
|
||||
|
||||
epfd = epoll_create(1);
|
||||
|
||||
if(init_wakeup_fd() < 0){
|
||||
wakeup_fd = init_wakeup_fd();
|
||||
if(wakeup_fd < 0){
|
||||
close(epfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// timer_fd = init_timer_fd();
|
||||
// if(timer_fd < 0){
|
||||
// close(epfd);
|
||||
// close(wakeup_fd);
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int i = 0;
|
||||
|
||||
for (i = 0;i < MAX_PORTS;i ++) {
|
||||
@@ -386,23 +429,15 @@ int reactor_start(unsigned short port, msg_handler handler) {
|
||||
gettimeofday(&begin, NULL);
|
||||
|
||||
while (1) { // mainloop
|
||||
|
||||
struct epoll_event events[1024] = {0};
|
||||
int nready = epoll_wait(epfd, events, 1024, -1);
|
||||
// cleanup_finished_iouring_tasks();
|
||||
|
||||
int i = 0;
|
||||
for (i = 0;i < nready;i ++) {
|
||||
|
||||
int connfd = events[i].data.fd;
|
||||
|
||||
#if 0
|
||||
if (events[i].events & EPOLLIN) {
|
||||
conn_list[connfd].r_action.recv_callback(connfd);
|
||||
} else if (events[i].events & EPOLLOUT) {
|
||||
conn_list[connfd].send_callback(connfd);
|
||||
}
|
||||
|
||||
#else
|
||||
if (events[i].events & EPOLLIN) {
|
||||
// printf("connlist:%p, r_action:%p, recv_callaback:%p\n", &conn_list[connfd], &conn_list[connfd].r_action, conn_list[connfd].r_action.recv_callback);
|
||||
conn_list[connfd].r_action.recv_callback(connfd);
|
||||
@@ -411,7 +446,6 @@ int reactor_start(unsigned short port, msg_handler handler) {
|
||||
if (events[i].events & EPOLLOUT) {
|
||||
conn_list[connfd].send_callback(connfd);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user