Compare commits
No commits in common. "07aa3824a94cab5b02401dce6eebea74c55e7410" and "d02eb2e69c513bb145e21bce4969a9c4645ffc67" have entirely different histories.
07aa3824a9
...
d02eb2e69c
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -4,7 +4,6 @@
|
|||||||
],
|
],
|
||||||
"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,6 +26,8 @@ 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,8 +30,6 @@ 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)
|
||||||
|
244
client.c
244
client.c
@ -13,9 +13,10 @@
|
|||||||
#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;
|
||||||
@ -187,9 +188,7 @@ 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;
|
||||||
@ -209,75 +208,32 @@ 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){
|
||||||
//simple queue
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
simple_queue_t queue;
|
queueing_method_t method;
|
||||||
|
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;
|
||||||
@ -304,6 +260,7 @@ 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();
|
||||||
@ -312,128 +269,123 @@ void * WorkerProc(void * args){
|
|||||||
for(;;)
|
for(;;)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&global_state.queueing_mutex);
|
pthread_mutex_lock(&global_state.queueing_mutex);
|
||||||
filename = dequeue_from_simplecharqueue(&global_state.queue,filename_buf,FILENAME_BUF_SIZE);
|
if (global_state.method == From_CharArray){
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
//====
|
|
||||||
// cmd parse
|
|
||||||
//====
|
|
||||||
static struct {
|
|
||||||
const char * server_name;
|
|
||||||
in_port_t server_port;
|
|
||||||
bool stdinisatty;
|
|
||||||
int thread_number_option;
|
|
||||||
} cmd_args = {
|
|
||||||
.server_name = "",
|
|
||||||
.server_port = 0
|
|
||||||
};
|
|
||||||
|
|
||||||
static const char * show_help_message(FILE * file,const char * argv0){
|
static bool stdinisatty;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int parse_arg(int argc,const char *argv[]){
|
|
||||||
int cur;
|
|
||||||
cmd_args.stdinisatty = isatty(STDIN_FILENO);
|
|
||||||
if (argc < (cmd_args.stdinisatty ? 4 : 3)){
|
|
||||||
show_help_message(stderr,argv[0]);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
cmd_args.server_name = argv[1];
|
|
||||||
cmd_args.server_port = atoi(argv[2]);
|
|
||||||
if (cmd_args.server_port == 0){
|
|
||||||
fprintf(stderr,"port invalid\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
else if(strcmp("-nv",argv[cur]) == 0||strcmp("--no-verbose",argv[cur])==0){
|
|
||||||
DisplayProgress = false;
|
|
||||||
}
|
|
||||||
else if(strcmp("-t",argv[cur]) == 0 || strcmp("--thread",argv[cur]) == 0){
|
|
||||||
cur++;
|
|
||||||
if (cur >= argc){
|
|
||||||
fprintf(stderr,"need number");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
cmd_args.thread_number_option = atoi(argv[cur]);
|
|
||||||
if(cmd_args.thread_number_option == 0){
|
|
||||||
fprintf(stderr,"not number or zero");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if(strcmp("-h",argv[cur]) == 0 || strcmp("--help",argv[cur]) == 0){
|
|
||||||
show_help_message(stdout,argv[0]);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else break;
|
|
||||||
}
|
|
||||||
if (cmd_args.stdinisatty){
|
|
||||||
init_queue_from_chararray(&global_state.queue, &argv[cur],&argv[argc]);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
init_queue_from_file(&global_state.queue,stdin);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, const char *argv[]){
|
int main(int argc, const char *argv[]){
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
const char * filename;
|
const char * filename;
|
||||||
|
const char * server_name;
|
||||||
|
in_port_t server_port = 0;
|
||||||
|
int arg_filename_start = 3;
|
||||||
int err;
|
int err;
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
init_bench_data();
|
init_bench_data();
|
||||||
if(parse_arg(argc,argv) < 0) return EXIT_FAILURE;
|
stdinisatty = isatty(STDIN_FILENO);
|
||||||
|
|
||||||
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,cmd_args.server_name,(struct sockaddr *)&addr);
|
if (argc < (stdinisatty ? 4 : 3)){
|
||||||
|
fprintf(stderr,"USAUE: %s SERVERNAME PORT [Option]... [FILENAME]...\n",argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
server_name = argv[1];
|
||||||
|
server_port = atoi(argv[2]);
|
||||||
|
while(arg_filename_start < argc){
|
||||||
|
if (strcmp("-b",argv[arg_filename_start])==0
|
||||||
|
||strcmp("--benchmark",argv[arg_filename_start])==0){
|
||||||
|
arg_filename_start++;
|
||||||
|
bench.benchmode = true;
|
||||||
|
}
|
||||||
|
else if(strcmp("-nv",argv[arg_filename_start]) == 0||strcmp("--no-verbose",argv[arg_filename_start])==0){
|
||||||
|
arg_filename_start++;
|
||||||
|
DisplayProgress = false;
|
||||||
|
}
|
||||||
|
else if(strcmp("-t",argv[arg_filename_start]) == 0 || strcmp("--thread",argv[arg_filename_start]) == 0){
|
||||||
|
arg_filename_start++;
|
||||||
|
if (arg_filename_start >= argc){
|
||||||
|
fprintf(stderr,"need number");
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
thread_number_option = atoi(argv[arg_filename_start++]);
|
||||||
|
if(thread_number_option == 0){
|
||||||
|
fprintf(stderr,"not number or zero");
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
if (server_port == 0){
|
||||||
|
fprintf(stderr,"port invalid\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,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,cmd_args.server_name,&addr.sin_addr);
|
check = inet_pton(AF_INET,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(cmd_args.server_port);
|
addr.sin_port = htons(server_port);
|
||||||
|
|
||||||
if (bench.benchmode){
|
if (bench.benchmode){
|
||||||
clock_gettime(bench.clock_id,&bench.begin);
|
clock_gettime(bench.clock_id,&bench.begin);
|
||||||
}
|
}
|
||||||
if(cmd_args.thread_number_option == 1){
|
if(thread_number_option == 1){
|
||||||
char filename_buf[FILENAME_BUF_SIZE];
|
char filename_buf[FILENAME_BUF_SIZE];
|
||||||
for (;;){
|
for (;;){
|
||||||
filename = dequeue_from_simplecharqueue(&global_state.queue,filename_buf,FILENAME_BUF_SIZE);
|
if (stdinisatty){
|
||||||
if (filename == NULL) break;
|
if (arg_filename_start >= argc) 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;
|
||||||
init_global_state(&global_state,cmd_args.thread_number_option);
|
global_state.method = stdinisatty ? From_CharArray : From_FileStream;
|
||||||
|
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,7 +13,6 @@
|
|||||||
#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
|
||||||
@ -52,15 +51,6 @@ 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
|
||||||
*========*/
|
*========*/
|
||||||
@ -286,22 +276,15 @@ 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);
|
||||||
@ -309,14 +292,6 @@ 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,7 +16,6 @@
|
|||||||
|
|
||||||
#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;
|
||||||
@ -60,8 +59,6 @@ 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
|
||||||
@ -235,19 +232,9 @@ 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];
|
||||||
@ -255,38 +242,25 @@ 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
|
|
||||||
int csock;
|
//pthread_mutex_t * cond_mutex;
|
||||||
#ifdef USE_TRACE
|
//pthread_cond_t cond;
|
||||||
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){
|
||||||
@ -295,76 +269,30 @@ __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];
|
|
||||||
#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 pthread_t worker_threads[MAX_THREAD_NUMBER];
|
||||||
|
|
||||||
static int sock;
|
static int sock;
|
||||||
void safe_exit(){
|
void safe_exit(){
|
||||||
@ -376,7 +304,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 = 0;
|
int i;
|
||||||
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);
|
||||||
@ -395,9 +323,8 @@ int main(int argc, const char *argv[]){
|
|||||||
perror("setsockopt");
|
perror("setsockopt");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bufsize = getBufferSizeFrom(sock);
|
|
||||||
#ifndef USE_NO_QUEUE
|
|
||||||
init_shared_state(&globalState);
|
init_shared_state(&globalState);
|
||||||
|
bufsize = getBufferSizeFrom(sock);
|
||||||
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) {
|
||||||
@ -406,7 +333,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);
|
||||||
@ -431,41 +358,22 @@ 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 {
|
else enqueue(&globalState.socks,csock);
|
||||||
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;
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
#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