Compare commits
4 Commits
d02eb2e69c
...
07aa3824a9
Author | SHA1 | Date | |
---|---|---|---|
|
07aa3824a9 | ||
|
50eeba56a4 | ||
|
35aa8243b3 | ||
|
b53ed414b0 |
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -4,6 +4,7 @@
|
|||||||
],
|
],
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"socket_wrapper.h": "c",
|
"socket_wrapper.h": "c",
|
||||||
"stdalign.h": "c"
|
"stdalign.h": "c",
|
||||||
|
"timerhelper.h": "c"
|
||||||
}
|
}
|
||||||
}
|
}
|
2
Makefile
2
Makefile
@ -26,8 +26,6 @@ slowclient: socket_wrapper.o client.c
|
|||||||
.PHONY: clean test
|
.PHONY: clean test
|
||||||
clean:
|
clean:
|
||||||
rm *.o $(Bin)
|
rm *.o $(Bin)
|
||||||
rm $(addprefix client_test/,$(ClientBin))
|
|
||||||
rm $(addprefix server_test/,$(ServerBin))
|
|
||||||
|
|
||||||
test:
|
test:
|
||||||
make all
|
make all
|
||||||
|
@ -30,6 +30,8 @@ For server
|
|||||||
- DEFAULT_WORK_QUEUE_SIZE(server only): 10
|
- DEFAULT_WORK_QUEUE_SIZE(server only): 10
|
||||||
- DEFAULT_MAX_THREAD_NUMBER(server only): 10
|
- DEFAULT_MAX_THREAD_NUMBER(server only): 10
|
||||||
- DEFAULT_RESPONSE_REQUEST(p-server only): 3(-1 is INF)
|
- DEFAULT_RESPONSE_REQUEST(p-server only): 3(-1 is INF)
|
||||||
|
- USE_TRACE
|
||||||
|
- USE_NO_QUEUE
|
||||||
|
|
||||||
For client
|
For client
|
||||||
- MUL_CLIENT(second unit)
|
- MUL_CLIENT(second unit)
|
||||||
|
224
client.c
224
client.c
@ -13,10 +13,9 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <sys/time.h>
|
|
||||||
#include <sys/times.h>
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include "socket_wrapper.h"
|
#include "socket_wrapper.h"
|
||||||
|
#include "timerhelper.h"
|
||||||
|
|
||||||
#ifndef DEFAULT_TIMEOUT
|
#ifndef DEFAULT_TIMEOUT
|
||||||
static const int TIMEOUT = 5;
|
static const int TIMEOUT = 5;
|
||||||
@ -188,7 +187,9 @@ int SendOpAndReceiveFile(const char * filename, struct sockaddr const * addr){
|
|||||||
close(sock);
|
close(sock);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
//====
|
||||||
|
//bench
|
||||||
|
//====
|
||||||
struct benchmark_data{
|
struct benchmark_data{
|
||||||
bool benchmode;
|
bool benchmode;
|
||||||
struct timespec begin;
|
struct timespec begin;
|
||||||
@ -208,32 +209,75 @@ void init_bench_data(){
|
|||||||
clock_getres(bench.clock_id,&bench.resolution);
|
clock_getres(bench.clock_id,&bench.resolution);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static inline struct timespec timespec_sub(struct timespec a,struct timespec b){
|
//====
|
||||||
struct timespec ret;
|
//simple queue
|
||||||
ret.tv_sec = a.tv_sec - b.tv_sec;
|
//====
|
||||||
ret.tv_nsec = a.tv_nsec - b.tv_nsec;
|
|
||||||
if (ret.tv_nsec < 0){
|
|
||||||
ret.tv_sec--;
|
|
||||||
ret.tv_nsec += 1000000000L;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t thread_number_option = 1;
|
|
||||||
|
|
||||||
typedef enum{
|
typedef enum{
|
||||||
From_CharArray,
|
From_CharArray,
|
||||||
From_FileStream
|
From_FileStream
|
||||||
} queueing_method_t;
|
} queueing_method_t;
|
||||||
|
|
||||||
|
typedef struct SimpleCharQueue{
|
||||||
|
queueing_method_t method;
|
||||||
|
const char ** filename_begin;
|
||||||
|
const char ** filename_end;
|
||||||
|
FILE * fs;
|
||||||
|
} simple_queue_t;
|
||||||
|
//
|
||||||
|
// q have lifetime of arg b or arg e.
|
||||||
|
// do not deallocate before queue closed.
|
||||||
|
static bool init_queue_from_chararray(simple_queue_t * restrict q,const char ** b, const char ** e){
|
||||||
|
q->method = From_CharArray;
|
||||||
|
q->filename_begin = b;
|
||||||
|
q->filename_end = e;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// q have lifetime of arg f.
|
||||||
|
// do not close file f before queue closed.
|
||||||
|
static bool init_queue_from_file(simple_queue_t * restrict q, FILE * f){
|
||||||
|
q->method = From_CharArray;
|
||||||
|
q->fs = f;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char * dequeue_from_simplecharqueue(simple_queue_t * restrict q,char * restrict buf, size_t size){
|
||||||
|
const char * filename;
|
||||||
|
if (q->method == From_CharArray){
|
||||||
|
if (__glibc_unlikely(q->filename_begin == q->filename_end)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
filename = *(q->filename_begin++);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
//unsafe.
|
||||||
|
int t = fscanf(q->fs,"%s",buf);
|
||||||
|
if (__glibc_unlikely(t != 1)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
filename = buf;
|
||||||
|
}
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
//====
|
||||||
|
//thread
|
||||||
|
//====
|
||||||
struct SimpleThreadGlobal{
|
struct SimpleThreadGlobal{
|
||||||
size_t thread_arr_size;
|
size_t thread_arr_size;
|
||||||
pthread_t * thread_arr;
|
pthread_t * thread_arr;
|
||||||
pthread_mutex_t queueing_mutex;
|
pthread_mutex_t queueing_mutex;
|
||||||
queueing_method_t method;
|
simple_queue_t queue;
|
||||||
const char ** filename_begin;
|
|
||||||
const char ** filename_end;
|
|
||||||
} global_state;
|
} global_state;
|
||||||
|
//if initialization is success, return true
|
||||||
|
bool init_global_state(struct SimpleThreadGlobal * t, size_t size){
|
||||||
|
pthread_mutex_init(&t->queueing_mutex,NULL);
|
||||||
|
t->thread_arr_size = size;
|
||||||
|
t->thread_arr = (pthread_t *)malloc(sizeof(*global_state.thread_arr) * global_state.thread_arr_size);
|
||||||
|
if (t->thread_arr == NULL){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
typedef struct SimpleThreadReturn{
|
typedef struct SimpleThreadReturn{
|
||||||
int retval;
|
int retval;
|
||||||
@ -260,7 +304,6 @@ void destroy_thread_arg(worker_arg_t * arg){
|
|||||||
free(arg);
|
free(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void * WorkerProc(void * args){
|
void * WorkerProc(void * args){
|
||||||
worker_arg_t * arg = (worker_arg_t *)(args);
|
worker_arg_t * arg = (worker_arg_t *)(args);
|
||||||
worker_return_t * ret = create_worker_return();
|
worker_return_t * ret = create_worker_return();
|
||||||
@ -269,123 +312,128 @@ void * WorkerProc(void * args){
|
|||||||
for(;;)
|
for(;;)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&global_state.queueing_mutex);
|
pthread_mutex_lock(&global_state.queueing_mutex);
|
||||||
if (global_state.method == From_CharArray){
|
filename = dequeue_from_simplecharqueue(&global_state.queue,filename_buf,FILENAME_BUF_SIZE);
|
||||||
if (__glibc_unlikely(global_state.filename_begin == global_state.filename_end)) {
|
|
||||||
pthread_mutex_unlock(&global_state.queueing_mutex);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
filename = *(global_state.filename_begin++);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
//unsafe.
|
|
||||||
int t = fscanf(stdin,"%s",filename_buf);
|
|
||||||
if (__glibc_unlikely(t != 1)) {
|
|
||||||
pthread_mutex_unlock(&global_state.queueing_mutex);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
filename = filename_buf;
|
|
||||||
}
|
|
||||||
pthread_mutex_unlock(&global_state.queueing_mutex);
|
pthread_mutex_unlock(&global_state.queueing_mutex);
|
||||||
|
if(filename == NULL) break;
|
||||||
ret->retval += SendOpAndReceiveFile(filename,&arg->addr);
|
ret->retval += SendOpAndReceiveFile(filename,&arg->addr);
|
||||||
ret->op_count++;
|
ret->op_count++;
|
||||||
}
|
}
|
||||||
destroy_thread_arg(arg);
|
destroy_thread_arg(arg);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
//====
|
||||||
static bool stdinisatty;
|
// cmd parse
|
||||||
|
//====
|
||||||
int main(int argc, const char *argv[]){
|
static struct {
|
||||||
struct sockaddr_in addr;
|
|
||||||
const char * filename;
|
|
||||||
const char * server_name;
|
const char * server_name;
|
||||||
in_port_t server_port = 0;
|
in_port_t server_port;
|
||||||
int arg_filename_start = 3;
|
bool stdinisatty;
|
||||||
int err;
|
int thread_number_option;
|
||||||
int retval = 0;
|
} cmd_args = {
|
||||||
|
.server_name = "",
|
||||||
|
.server_port = 0
|
||||||
|
};
|
||||||
|
|
||||||
init_bench_data();
|
static const char * show_help_message(FILE * file,const char * argv0){
|
||||||
stdinisatty = isatty(STDIN_FILENO);
|
static const char * msg =
|
||||||
|
"USAUE: %s SERVERNAME PORT [Option]... [FILENAME]...\n"
|
||||||
|
"Options and arguments: \n"
|
||||||
|
"-b or --benchmark\t:benchmark mode\n"
|
||||||
|
"-nv or --no-verbose\t:no progress bar\n"
|
||||||
|
"-h\t:print help message.\n";
|
||||||
|
fprintf(file,msg,argv0);
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
if (argc < (stdinisatty ? 4 : 3)){
|
static int parse_arg(int argc,const char *argv[]){
|
||||||
fprintf(stderr,"USAUE: %s SERVERNAME PORT [Option]... [FILENAME]...\n",argv[0]);
|
int cur;
|
||||||
|
cmd_args.stdinisatty = isatty(STDIN_FILENO);
|
||||||
|
if (argc < (cmd_args.stdinisatty ? 4 : 3)){
|
||||||
|
show_help_message(stderr,argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
server_name = argv[1];
|
cmd_args.server_name = argv[1];
|
||||||
server_port = atoi(argv[2]);
|
cmd_args.server_port = atoi(argv[2]);
|
||||||
while(arg_filename_start < argc){
|
if (cmd_args.server_port == 0){
|
||||||
if (strcmp("-b",argv[arg_filename_start])==0
|
fprintf(stderr,"port invalid\n");
|
||||||
||strcmp("--benchmark",argv[arg_filename_start])==0){
|
return -1;
|
||||||
arg_filename_start++;
|
}
|
||||||
|
cmd_args.thread_number_option = 1;
|
||||||
|
for(cur = 3;cur < argc; cur++){
|
||||||
|
if (strcmp("-b",argv[cur])==0||strcmp("--benchmark",argv[cur])==0){
|
||||||
bench.benchmode = true;
|
bench.benchmode = true;
|
||||||
}
|
}
|
||||||
else if(strcmp("-nv",argv[arg_filename_start]) == 0||strcmp("--no-verbose",argv[arg_filename_start])==0){
|
else if(strcmp("-nv",argv[cur]) == 0||strcmp("--no-verbose",argv[cur])==0){
|
||||||
arg_filename_start++;
|
|
||||||
DisplayProgress = false;
|
DisplayProgress = false;
|
||||||
}
|
}
|
||||||
else if(strcmp("-t",argv[arg_filename_start]) == 0 || strcmp("--thread",argv[arg_filename_start]) == 0){
|
else if(strcmp("-t",argv[cur]) == 0 || strcmp("--thread",argv[cur]) == 0){
|
||||||
arg_filename_start++;
|
cur++;
|
||||||
if (arg_filename_start >= argc){
|
if (cur >= argc){
|
||||||
fprintf(stderr,"need number");
|
fprintf(stderr,"need number");
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
thread_number_option = atoi(argv[arg_filename_start++]);
|
cmd_args.thread_number_option = atoi(argv[cur]);
|
||||||
if(thread_number_option == 0){
|
if(cmd_args.thread_number_option == 0){
|
||||||
fprintf(stderr,"not number or zero");
|
fprintf(stderr,"not number or zero");
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if(strcmp("-h",argv[cur]) == 0 || strcmp("--help",argv[cur]) == 0){
|
||||||
|
show_help_message(stdout,argv[0]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
else break;
|
else break;
|
||||||
}
|
}
|
||||||
if (server_port == 0){
|
if (cmd_args.stdinisatty){
|
||||||
fprintf(stderr,"port invalid\n");
|
init_queue_from_chararray(&global_state.queue, &argv[cur],&argv[argc]);
|
||||||
return 1;
|
}
|
||||||
|
else{
|
||||||
|
init_queue_from_file(&global_state.queue,stdin);
|
||||||
}
|
}
|
||||||
|
|
||||||
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,server_name,(struct sockaddr *)&addr);
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, const char *argv[]){
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
const char * filename;
|
||||||
|
int err;
|
||||||
|
int retval = 0;
|
||||||
|
|
||||||
|
init_bench_data();
|
||||||
|
if(parse_arg(argc,argv) < 0) return EXIT_FAILURE;
|
||||||
|
|
||||||
|
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,cmd_args.server_name,(struct sockaddr *)&addr);
|
||||||
if (err != 0){
|
if (err != 0){
|
||||||
int check;
|
int check;
|
||||||
fprintf(stderr,"netdb fail: %s\n",gai_strerror(err));
|
fprintf(stderr,"netdb fail: %s\n",gai_strerror(err));
|
||||||
//assume that sernmae is *.*.*.* and try to parse addr
|
//assume that sernmae is *.*.*.* and try to parse addr
|
||||||
check = inet_pton(AF_INET,server_name,&addr.sin_addr);
|
check = inet_pton(AF_INET,cmd_args.server_name,&addr.sin_addr);
|
||||||
assert(check != -1);
|
assert(check != -1);
|
||||||
if (check == 0){
|
if (check == 0){
|
||||||
fprintf(stderr,"parsing fail : invaild format\n");
|
fprintf(stderr,"parsing fail : invaild format\n");
|
||||||
return 1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = htons(server_port);
|
addr.sin_port = htons(cmd_args.server_port);
|
||||||
|
|
||||||
if (bench.benchmode){
|
if (bench.benchmode){
|
||||||
clock_gettime(bench.clock_id,&bench.begin);
|
clock_gettime(bench.clock_id,&bench.begin);
|
||||||
}
|
}
|
||||||
if(thread_number_option == 1){
|
if(cmd_args.thread_number_option == 1){
|
||||||
char filename_buf[FILENAME_BUF_SIZE];
|
char filename_buf[FILENAME_BUF_SIZE];
|
||||||
for (;;){
|
for (;;){
|
||||||
if (stdinisatty){
|
filename = dequeue_from_simplecharqueue(&global_state.queue,filename_buf,FILENAME_BUF_SIZE);
|
||||||
if (arg_filename_start >= argc) break;
|
if (filename == NULL) break;
|
||||||
filename = argv[arg_filename_start++];
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
//unsafe.
|
|
||||||
int t = fscanf(stdin,"%s",filename_buf);
|
|
||||||
if (t != 1) break;
|
|
||||||
filename = filename_buf;
|
|
||||||
}
|
|
||||||
retval += SendOpAndReceiveFile(filename,(struct sockaddr *)&addr);
|
retval += SendOpAndReceiveFile(filename,(struct sockaddr *)&addr);
|
||||||
bench.op_count++;
|
bench.op_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
global_state.method = stdinisatty ? From_CharArray : From_FileStream;
|
init_global_state(&global_state,cmd_args.thread_number_option);
|
||||||
global_state.filename_begin = &argv[arg_filename_start];
|
|
||||||
global_state.filename_end = &argv[argc];
|
|
||||||
pthread_mutex_init(&global_state.queueing_mutex,NULL);
|
|
||||||
global_state.thread_arr_size = thread_number_option;
|
|
||||||
global_state.thread_arr = (pthread_t *)malloc(sizeof(*global_state.thread_arr) * global_state.thread_arr_size);
|
|
||||||
for (i = 0; i < global_state.thread_arr_size; i++){
|
for (i = 0; i < global_state.thread_arr_size; i++){
|
||||||
worker_arg_t * arg = create_thread_arg((struct sockaddr *)&addr);
|
worker_arg_t * arg = create_thread_arg((struct sockaddr *)&addr);
|
||||||
pthread_create(&global_state.thread_arr[i],NULL,WorkerProc,arg);
|
pthread_create(&global_state.thread_arr[i],NULL,WorkerProc,arg);
|
||||||
|
25
p-server.c
25
p-server.c
@ -13,6 +13,7 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
|
||||||
#include "socket_wrapper.h"
|
#include "socket_wrapper.h"
|
||||||
|
|
||||||
#ifndef DEFAULT_MAX_LISTEN_SOCKET
|
#ifndef DEFAULT_MAX_LISTEN_SOCKET
|
||||||
@ -51,6 +52,15 @@ static const int RESPONSE_REQUEST = 3;
|
|||||||
static const int RESPONSE_REQUEST = DEFAULT_RESPONSE_REQUEST;
|
static const int RESPONSE_REQUEST = DEFAULT_RESPONSE_REQUEST;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define USE_TRACE
|
||||||
|
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include "timerhelper.h"
|
||||||
|
enum{
|
||||||
|
Trace_Timer_ID = CLOCK_REALTIME
|
||||||
|
};
|
||||||
|
|
||||||
/*========
|
/*========
|
||||||
*Operation
|
*Operation
|
||||||
*========*/
|
*========*/
|
||||||
@ -276,15 +286,22 @@ int main(int argc, const char *argv[]){
|
|||||||
char ip_buf[INET_ADDRSTRLEN];
|
char ip_buf[INET_ADDRSTRLEN];
|
||||||
const char * msg;
|
const char * msg;
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
struct timespec ts,tsmiddle,tsend;
|
||||||
if((csock = accept(sock, (struct sockaddr *)&client_addr,&client_addr_len)) < 0){
|
if((csock = accept(sock, (struct sockaddr *)&client_addr,&client_addr_len)) < 0){
|
||||||
free(buf);
|
free(buf);
|
||||||
perror("accept error");
|
perror("accept error");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf));
|
msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf));
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
clock_gettime(Trace_Timer_ID,&ts);
|
||||||
|
#endif
|
||||||
fprintf(stderr,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(client_addr.sin_port));
|
fprintf(stderr,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(client_addr.sin_port));
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if(pid == 0){
|
if(pid == 0){
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
clock_gettime(Trace_Timer_ID,&tsmiddle);
|
||||||
|
#endif
|
||||||
if((fd = read_request(csock,buf,bufsize)) > 0){
|
if((fd = read_request(csock,buf,bufsize)) > 0){
|
||||||
retval = send_response(csock,fd,buf,bufsize);
|
retval = send_response(csock,fd,buf,bufsize);
|
||||||
close(fd);
|
close(fd);
|
||||||
@ -292,6 +309,14 @@ int main(int argc, const char *argv[]){
|
|||||||
else retval = fd;
|
else retval = fd;
|
||||||
if(close(csock) < 0)
|
if(close(csock) < 0)
|
||||||
perror("csock close error");
|
perror("csock close error");
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
clock_gettime(Trace_Timer_ID,&tsend);
|
||||||
|
struct timespec tophalf = timespec_sub(tsmiddle,ts);
|
||||||
|
struct timespec bottomhelf = timespec_sub(tsend,tsmiddle);
|
||||||
|
struct timespec resolution;
|
||||||
|
clock_getres(Trace_Timer_ID,&resolution);
|
||||||
|
fprintf(stderr,"top: %ld ns, bottom: %ld ns, res: %ld\n",tophalf.tv_nsec,bottomhelf.tv_nsec,resolution.tv_nsec);
|
||||||
|
#endif
|
||||||
free(buf);
|
free(buf);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
124
server.c
124
server.c
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
#include "socket_wrapper.h"
|
#include "socket_wrapper.h"
|
||||||
#include "simple_circular_buffer.h"
|
#include "simple_circular_buffer.h"
|
||||||
|
#include "timerhelper.h"
|
||||||
|
|
||||||
#ifndef DEFAULT_MAX_LISTEN_SOCKET
|
#ifndef DEFAULT_MAX_LISTEN_SOCKET
|
||||||
static const int MAX_LISTEN_SOCKET = 16;
|
static const int MAX_LISTEN_SOCKET = 16;
|
||||||
@ -59,6 +60,8 @@ enum{
|
|||||||
MAX_THREAD_NUMBER = DEFAULT_MAX_THREAD_NUMBER
|
MAX_THREAD_NUMBER = DEFAULT_MAX_THREAD_NUMBER
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
#define USE_TRACE
|
||||||
|
#define USE_NO_QUEUE
|
||||||
|
|
||||||
/*========
|
/*========
|
||||||
*Operation
|
*Operation
|
||||||
@ -232,9 +235,19 @@ int parse_args(int argc,const char * argv[] , in_port_t * port){
|
|||||||
//============
|
//============
|
||||||
//Simple Thread Pool
|
//Simple Thread Pool
|
||||||
//============
|
//============
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
enum{
|
||||||
|
Trace_Timer_ID = CLOCK_REALTIME
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef USE_NO_QUEUE
|
||||||
typedef struct SharedState{
|
typedef struct SharedState{
|
||||||
//empty if less than 0
|
//empty if less than 0
|
||||||
queue_struct(int,WORK_QUEUE_SIZE) socks;
|
queue_struct(int,WORK_QUEUE_SIZE) socks;
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
queue_struct(struct timespec,WORK_QUEUE_SIZE) trace_timer;
|
||||||
|
#endif
|
||||||
pthread_mutex_t sock_mutex;
|
pthread_mutex_t sock_mutex;
|
||||||
pthread_cond_t ready;
|
pthread_cond_t ready;
|
||||||
//int progress[MAX_THREAD_NUMBER];
|
//int progress[MAX_THREAD_NUMBER];
|
||||||
@ -242,25 +255,38 @@ typedef struct SharedState{
|
|||||||
|
|
||||||
void init_shared_state(shared_state_t * state) {
|
void init_shared_state(shared_state_t * state) {
|
||||||
queue_init(&state->socks);
|
queue_init(&state->socks);
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
queue_init(&state->trace_timer);
|
||||||
|
#endif
|
||||||
pthread_mutex_init(&state->sock_mutex,NULL);
|
pthread_mutex_init(&state->sock_mutex,NULL);
|
||||||
pthread_cond_init(&state->ready,NULL);
|
pthread_cond_init(&state->ready,NULL);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
//
|
//
|
||||||
typedef struct WorkerArgument
|
typedef struct WorkerArgument
|
||||||
{
|
{
|
||||||
int id;
|
int id;
|
||||||
int bufsize;
|
int bufsize;
|
||||||
uint8_t * buf;
|
uint8_t * buf;
|
||||||
|
#ifdef USE_NO_QUEUE
|
||||||
//pthread_mutex_t * cond_mutex;
|
int csock;
|
||||||
//pthread_cond_t cond;
|
#ifdef USE_TRACE
|
||||||
|
struct timespec ts;
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
} worker_argument_t;
|
} worker_argument_t;
|
||||||
|
|
||||||
__attribute_malloc__ worker_argument_t * create_worker_argument(int id, int bufsize){
|
__attribute_malloc__ worker_argument_t * create_worker_argument(int id, int bufsize
|
||||||
|
#ifdef USE_NO_QUEUE
|
||||||
|
, int csock
|
||||||
|
#endif
|
||||||
|
){
|
||||||
worker_argument_t * ret = (worker_argument_t *)malloc(sizeof(worker_argument_t));
|
worker_argument_t * ret = (worker_argument_t *)malloc(sizeof(worker_argument_t));
|
||||||
if (ret == NULL) return ret;
|
if (ret == NULL) return ret;
|
||||||
ret->id = id;
|
ret->id = id;
|
||||||
|
#ifdef USE_NO_QUEUE
|
||||||
|
ret->csock = csock;
|
||||||
|
#endif
|
||||||
ret->bufsize = bufsize;
|
ret->bufsize = bufsize;
|
||||||
ret->buf = (uint8_t *)malloc(sizeof(*ret->buf)*bufsize);
|
ret->buf = (uint8_t *)malloc(sizeof(*ret->buf)*bufsize);
|
||||||
if(ret->buf == NULL){
|
if(ret->buf == NULL){
|
||||||
@ -269,30 +295,76 @@ __attribute_malloc__ worker_argument_t * create_worker_argument(int id, int bufs
|
|||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
void destory_worker_argument(worker_argument_t * arg){
|
||||||
|
free(arg->buf);
|
||||||
|
free(arg);
|
||||||
|
}
|
||||||
|
#ifndef USE_NO_QUEUE
|
||||||
static shared_state_t globalState;
|
static shared_state_t globalState;
|
||||||
|
|
||||||
void * worker_proc(void * data){
|
void * worker_proc(void * data){
|
||||||
worker_argument_t * args = (worker_argument_t *)data;
|
worker_argument_t * args = (worker_argument_t *)data;
|
||||||
int fd, csock;
|
int fd, csock;
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
struct timespec ts,ts_middle,ts_end;
|
||||||
|
#endif
|
||||||
for(;;){
|
for(;;){
|
||||||
|
|
||||||
pthread_mutex_lock(&globalState.sock_mutex);
|
pthread_mutex_lock(&globalState.sock_mutex);
|
||||||
while (queue_isempty(&globalState.socks)){
|
while (queue_isempty(&globalState.socks)){
|
||||||
pthread_cond_wait(&globalState.ready,&globalState.sock_mutex);
|
pthread_cond_wait(&globalState.ready,&globalState.sock_mutex);
|
||||||
}
|
}
|
||||||
csock = dequeue(&globalState.socks);
|
csock = dequeue(&globalState.socks);
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
ts = dequeue(&globalState.trace_timer);
|
||||||
|
#endif
|
||||||
pthread_mutex_unlock(&globalState.sock_mutex);
|
pthread_mutex_unlock(&globalState.sock_mutex);
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
clock_gettime(Trace_Timer_ID,&ts_middle);
|
||||||
|
#endif
|
||||||
if((fd = read_request(csock,args->buf,args->bufsize)) > 0){
|
if((fd = read_request(csock,args->buf,args->bufsize)) > 0){
|
||||||
send_response(csock,fd,args->buf,args->bufsize);
|
send_response(csock,fd,args->buf,args->bufsize);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
clock_gettime(Trace_Timer_ID,&ts_end);
|
||||||
|
struct timespec tophalf = timespec_sub(ts_middle,ts);
|
||||||
|
struct timespec bottomhelf = timespec_sub(ts_end,ts_middle);
|
||||||
|
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec);
|
||||||
|
#endif
|
||||||
if(close(csock) < 0)
|
if(close(csock) < 0)
|
||||||
perror("csock close error");
|
perror("csock close error");
|
||||||
}
|
}
|
||||||
|
destory_worker_argument(args);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static pthread_t worker_threads[MAX_THREAD_NUMBER];
|
static pthread_t worker_threads[MAX_THREAD_NUMBER];
|
||||||
|
#else
|
||||||
|
void * worker_proc(void * data){
|
||||||
|
worker_argument_t * args = (worker_argument_t *)data;
|
||||||
|
int fd, csock;
|
||||||
|
csock = args->csock;
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
struct timespec ts,ts_middle,ts_end;
|
||||||
|
ts = args->ts;
|
||||||
|
clock_gettime(Trace_Timer_ID,&ts_middle);
|
||||||
|
#endif
|
||||||
|
if((fd = read_request(csock,args->buf,args->bufsize)) > 0){
|
||||||
|
send_response(csock,fd,args->buf,args->bufsize);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
clock_gettime(Trace_Timer_ID,&ts_end);
|
||||||
|
struct timespec tophalf = timespec_sub(ts_middle,ts);
|
||||||
|
struct timespec bottomhelf = timespec_sub(ts_end,ts_middle);
|
||||||
|
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec);
|
||||||
|
#endif
|
||||||
|
if(close(csock) < 0)
|
||||||
|
perror("csock close error");
|
||||||
|
destory_worker_argument(args);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static int sock;
|
static int sock;
|
||||||
void safe_exit(){
|
void safe_exit(){
|
||||||
@ -304,7 +376,7 @@ int main(int argc, const char *argv[]){
|
|||||||
socklen_t client_addr_len = sizeof(client_addr);
|
socklen_t client_addr_len = sizeof(client_addr);
|
||||||
int csock;
|
int csock;
|
||||||
int bufsize;
|
int bufsize;
|
||||||
int i;
|
int i = 0;
|
||||||
in_port_t binding_port_number = SERVER_PORT;
|
in_port_t binding_port_number = SERVER_PORT;
|
||||||
if (argc > 1){
|
if (argc > 1){
|
||||||
int d = parse_args(argc,argv,&binding_port_number);
|
int d = parse_args(argc,argv,&binding_port_number);
|
||||||
@ -323,8 +395,9 @@ int main(int argc, const char *argv[]){
|
|||||||
perror("setsockopt");
|
perror("setsockopt");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
init_shared_state(&globalState);
|
|
||||||
bufsize = getBufferSizeFrom(sock);
|
bufsize = getBufferSizeFrom(sock);
|
||||||
|
#ifndef USE_NO_QUEUE
|
||||||
|
init_shared_state(&globalState);
|
||||||
for (i = 0; i < MAX_THREAD_NUMBER; i++) {
|
for (i = 0; i < MAX_THREAD_NUMBER; i++) {
|
||||||
worker_argument_t * args = create_worker_argument(i,bufsize);
|
worker_argument_t * args = create_worker_argument(i,bufsize);
|
||||||
if (args == NULL) {
|
if (args == NULL) {
|
||||||
@ -333,7 +406,7 @@ int main(int argc, const char *argv[]){
|
|||||||
}
|
}
|
||||||
pthread_create(&worker_threads[i],NULL,worker_proc,args);
|
pthread_create(&worker_threads[i],NULL,worker_proc,args);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
addr.sin_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/
|
addr.sin_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = htons(binding_port_number);
|
addr.sin_port = htons(binding_port_number);
|
||||||
@ -358,22 +431,41 @@ int main(int argc, const char *argv[]){
|
|||||||
char ip_buf[INET_ADDRSTRLEN];
|
char ip_buf[INET_ADDRSTRLEN];
|
||||||
const char * msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf));
|
const char * msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf));
|
||||||
fprintf(stdout,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(addr.sin_port));
|
fprintf(stdout,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(addr.sin_port));
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
struct timespec ts;
|
||||||
|
clock_gettime(Trace_Timer_ID, &ts);
|
||||||
|
#endif
|
||||||
|
#ifndef USE_NO_QUEUE
|
||||||
for(;;){
|
for(;;){
|
||||||
pthread_mutex_lock(&globalState.sock_mutex);
|
pthread_mutex_lock(&globalState.sock_mutex);
|
||||||
if (queue_isfull(&globalState.socks)){
|
if (queue_isfull(&globalState.socks)){
|
||||||
pthread_mutex_unlock(&globalState.sock_mutex);
|
pthread_mutex_unlock(&globalState.sock_mutex);
|
||||||
#ifdef _GNU_SOURCE
|
#ifdef _GNU_SOURCE
|
||||||
pthread_yield();
|
pthread_yield();
|
||||||
#else
|
#else
|
||||||
usleep(400);
|
usleep(400);
|
||||||
#endif
|
#endif
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else enqueue(&globalState.socks,csock);
|
else {
|
||||||
|
enqueue(&globalState.socks,csock);
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
enqueue(&globalState.trace_timer,ts);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&globalState.sock_mutex);
|
pthread_mutex_unlock(&globalState.sock_mutex);
|
||||||
pthread_cond_signal(&globalState.ready);
|
pthread_cond_signal(&globalState.ready);
|
||||||
|
#else
|
||||||
|
pthread_t thread_a;
|
||||||
|
worker_argument_t * args = create_worker_argument(i++,bufsize,csock);
|
||||||
|
#ifdef USE_TRACE
|
||||||
|
args->ts = ts;
|
||||||
|
#endif
|
||||||
|
pthread_create(&thread_a,NULL,worker_proc,args);
|
||||||
|
pthread_detach(thread_a);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
16
timerhelper.h
Normal file
16
timerhelper.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#ifndef _TIMERHELPER_H_
|
||||||
|
#define _TIMERHELPER_H_
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
__always_inline struct timespec timespec_sub(struct timespec a,struct timespec b){
|
||||||
|
struct timespec ret;
|
||||||
|
ret.tv_sec = a.tv_sec - b.tv_sec;
|
||||||
|
ret.tv_nsec = a.tv_nsec - b.tv_nsec;
|
||||||
|
if (ret.tv_nsec < 0){
|
||||||
|
ret.tv_sec--;
|
||||||
|
ret.tv_nsec += 1000000000L;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user