Modify FF_SOCKET_THREAD disable by default.

This commit is contained in:
fengbojiang 2023-04-07 13:44:41 +08:00
parent 88318c48c4
commit e8ed03b521
4 changed files with 122 additions and 122 deletions

View File

@ -12,10 +12,10 @@ DEBUG=-O0 -gdwarf-2 -g3
# Per thread separate initialization dpdk lib and attach sc when needed,
# such as listen same port in different threads, and socket can use in own thread.
# Default enable.
# Default disable.
#
# If disable it, one socket can use in all threads.
FF_THREAD_SOCKET=1
#FF_THREAD_SOCKET=1
PKGCONF ?= pkg-config

View File

@ -21,7 +21,7 @@ all:
cc ${CFLAGS} -DINET6 -o ${TARGET} main.c ${LIBS}
cc ${CFLAGS} -o ${TARGET}_epoll main_epoll.c ${LIBS}
cc ${CFLAGS} -I ../adapter -o ${TARGET}_stack_epoll main_stack_epoll.c ${LIBS}
cc ${CFLAGS} -I ../adapter -o ${TARGET}_stack_epoll_pipeline main_stack_epoll_pipeline.c ${LIBS}
cc ${CFLAGS} -I ../adapter -o ${TARGET}_stack_epoll_thread_socket main_stack_epoll_thread_socket.c ${LIBS}
.PHONY: clean
clean:

View File

@ -16,8 +16,12 @@
#define MAX_WORKERS 128
pthread_t hworker[MAX_WORKERS];
pthread_spinlock_t worker_lock;
#define MAX_EVENTS 512
struct epoll_event ev;
struct epoll_event events[MAX_EVENTS];
int epfd;
int sockfd;
static int exit_flag = 0;
@ -62,69 +66,18 @@ void sig_term(int sig)
void *loop(void *arg)
{
struct epoll_event ev;
struct epoll_event events[MAX_EVENTS];
int epfd;
int sockfd;
int thread_id;
thread_id = *(int *)arg;
printf("start thread %d\n", thread_id);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
printf("thread %d, sockfd:%d\n", thread_id, sockfd);
if (sockfd < 0) {
printf("thread %d, ff_socket failed\n", thread_id);
pthread_spin_unlock(&worker_lock);
exit(1);
}
/* socket will init adapter,so unlock after socket */
pthread_spin_unlock(&worker_lock);
int on = 1;
ioctl(sockfd, FIONBIO, &on);
struct sockaddr_in my_addr;
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(80);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int ret = bind(sockfd, (const struct sockaddr *)&my_addr, sizeof(my_addr));
if (ret < 0) {
printf("thread %d, ff_bind failed\n", thread_id);
exit(1);
}
ret = listen(sockfd, MAX_EVENTS);
if (ret < 0) {
printf("thread %d, ff_listen failed\n", thread_id);
exit(1);
}
epfd = epoll_create(0);
if (epfd <= 0) {
printf("thread %d, ff_epoll_create failed, errno:%d, %s\n",
thread_id, errno, strerror(errno));
exit(1);
}
ev.data.fd = sockfd;
ev.events = EPOLLIN;
epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev);
/* Wait for events to happen */
while (!exit_flag) {
int nevents = epoll_wait(epfd, events, MAX_EVENTS, 100);
if (nevents <= 0) {
if (nevents) {
printf("thread %d, hello world epoll wait ret %d, errno:%d, %s\n",
thread_id, nevents, errno, strerror(errno));
printf("hello world epoll wait ret %d, errno:%d, %s\n",
nevents, errno, strerror(errno));
}
//usleep(100);
sleep(1);
}
printf("thread %d, get nevents:%d\n", thread_id, nevents);
printf("get nevents:%d\n", nevents);
int i;
for (i = 0; i < nevents; ++i) {
@ -140,8 +93,8 @@ void *loop(void *arg)
ev.data.fd = nclientfd;
ev.events = EPOLLIN;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, nclientfd, &ev) != 0) {
printf("thread %d, ff_epoll_ctl failed:%d, %s\n",
thread_id, errno, strerror(errno));
printf("ff_epoll_ctl failed:%d, %s\n",
errno, strerror(errno));
break;
}
}
@ -160,7 +113,7 @@ void *loop(void *arg)
close(events[i].data.fd);
}
} else {
printf("thread %d, unknown event: %8.8X\n", thread_id, events[i].events);
printf("unknown event: %8.8X\n", events[i].events);
}
}
}
@ -169,38 +122,60 @@ void *loop(void *arg)
int main(int argc, char * argv[])
{
int i, worker_num;
int i, worker_num = 1;
signal(SIGINT, sig_term);
signal(SIGTERM, sig_term);
if (argc == 1) {
worker_num = 1;
} else {
worker_num = atoi(argv[1]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
printf("sockfd:%d\n", sockfd);
if (sockfd < 0) {
printf("ff_socket failed\n");
exit(1);
}
printf("to init %d workers.\n", worker_num);
pthread_spin_init(&worker_lock, PTHREAD_PROCESS_PRIVATE);
pthread_spin_lock(&worker_lock);
int on = 1;
ioctl(sockfd, FIONBIO, &on);
struct sockaddr_in my_addr;
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(80);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int ret = bind(sockfd, (const struct sockaddr *)&my_addr, sizeof(my_addr));
if (ret < 0) {
printf("ff_bind failed\n");
exit(1);
}
ret = listen(sockfd, MAX_EVENTS);
if (ret < 0) {
printf("ff_listen failed\n");
exit(1);
}
epfd = epoll_create(0);
if (epfd <= 0) {
printf("ff_epoll_create failed, errno:%d, %s\n",
errno, strerror(errno));
exit(1);
}
ev.data.fd = sockfd;
ev.events = EPOLLIN;
epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev);
for (i = 0; i < worker_num; i++) {
if(pthread_create(&hworker[i], NULL, loop, (void *)&i) < 0) {
printf("create loop thread failed., errno:%d/%s\n",
errno, strerror(errno));
pthread_spin_unlock(&worker_lock);
pthread_spin_destroy(&worker_lock);
return -1;
}
pthread_spin_lock(&worker_lock);
//sleep(1);
}
for (i = 0; i < worker_num; i++) {
pthread_join(hworker[i], NULL);
}
pthread_spin_destroy(&worker_lock);
return 0;
}

View File

@ -16,12 +16,8 @@
#define MAX_WORKERS 128
pthread_t hworker[MAX_WORKERS];
pthread_spinlock_t worker_lock;
#define MAX_EVENTS 512
struct epoll_event ev;
struct epoll_event events[MAX_EVENTS];
int epfd;
int sockfd;
static int exit_flag = 0;
@ -66,18 +62,69 @@ void sig_term(int sig)
void *loop(void *arg)
{
struct epoll_event ev;
struct epoll_event events[MAX_EVENTS];
int epfd;
int sockfd;
int thread_id;
thread_id = *(int *)arg;
printf("start thread %d\n", thread_id);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
printf("thread %d, sockfd:%d\n", thread_id, sockfd);
if (sockfd < 0) {
printf("thread %d, ff_socket failed\n", thread_id);
pthread_spin_unlock(&worker_lock);
exit(1);
}
/* socket will init adapter,so unlock after socket */
pthread_spin_unlock(&worker_lock);
int on = 1;
ioctl(sockfd, FIONBIO, &on);
struct sockaddr_in my_addr;
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(80);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int ret = bind(sockfd, (const struct sockaddr *)&my_addr, sizeof(my_addr));
if (ret < 0) {
printf("thread %d, ff_bind failed\n", thread_id);
exit(1);
}
ret = listen(sockfd, MAX_EVENTS);
if (ret < 0) {
printf("thread %d, ff_listen failed\n", thread_id);
exit(1);
}
epfd = epoll_create(0);
if (epfd <= 0) {
printf("thread %d, ff_epoll_create failed, errno:%d, %s\n",
thread_id, errno, strerror(errno));
exit(1);
}
ev.data.fd = sockfd;
ev.events = EPOLLIN;
epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev);
/* Wait for events to happen */
while (!exit_flag) {
int nevents = epoll_wait(epfd, events, MAX_EVENTS, 100);
if (nevents <= 0) {
if (nevents) {
printf("hello world epoll wait ret %d, errno:%d, %s\n",
nevents, errno, strerror(errno));
printf("thread %d, hello world epoll wait ret %d, errno:%d, %s\n",
thread_id, nevents, errno, strerror(errno));
}
//usleep(100);
sleep(1);
}
printf("get nevents:%d\n", nevents);
printf("thread %d, get nevents:%d\n", thread_id, nevents);
int i;
for (i = 0; i < nevents; ++i) {
@ -93,8 +140,8 @@ void *loop(void *arg)
ev.data.fd = nclientfd;
ev.events = EPOLLIN;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, nclientfd, &ev) != 0) {
printf("ff_epoll_ctl failed:%d, %s\n",
errno, strerror(errno));
printf("thread %d, ff_epoll_ctl failed:%d, %s\n",
thread_id, errno, strerror(errno));
break;
}
}
@ -113,7 +160,7 @@ void *loop(void *arg)
close(events[i].data.fd);
}
} else {
printf("unknown event: %8.8X\n", events[i].events);
printf("thread %d, unknown event: %8.8X\n", thread_id, events[i].events);
}
}
}
@ -122,60 +169,38 @@ void *loop(void *arg)
int main(int argc, char * argv[])
{
int i, worker_num = 1;
int i, worker_num;
signal(SIGINT, sig_term);
signal(SIGTERM, sig_term);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
printf("sockfd:%d\n", sockfd);
if (sockfd < 0) {
printf("ff_socket failed\n");
exit(1);
if (argc == 1) {
worker_num = 1;
} else {
worker_num = atoi(argv[1]);
}
printf("to init %d workers.\n", worker_num);
int on = 1;
ioctl(sockfd, FIONBIO, &on);
struct sockaddr_in my_addr;
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(80);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int ret = bind(sockfd, (const struct sockaddr *)&my_addr, sizeof(my_addr));
if (ret < 0) {
printf("ff_bind failed\n");
exit(1);
}
ret = listen(sockfd, MAX_EVENTS);
if (ret < 0) {
printf("ff_listen failed\n");
exit(1);
}
epfd = epoll_create(0);
if (epfd <= 0) {
printf("ff_epoll_create failed, errno:%d, %s\n",
errno, strerror(errno));
exit(1);
}
ev.data.fd = sockfd;
ev.events = EPOLLIN;
epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev);
pthread_spin_init(&worker_lock, PTHREAD_PROCESS_PRIVATE);
pthread_spin_lock(&worker_lock);
for (i = 0; i < worker_num; i++) {
if(pthread_create(&hworker[i], NULL, loop, (void *)&i) < 0) {
printf("create loop thread failed., errno:%d/%s\n",
errno, strerror(errno));
pthread_spin_unlock(&worker_lock);
pthread_spin_destroy(&worker_lock);
return -1;
}
pthread_spin_lock(&worker_lock);
//sleep(1);
}
for (i = 0; i < worker_num; i++) {
pthread_join(hworker[i], NULL);
}
pthread_spin_destroy(&worker_lock);
return 0;
}