Compare commits

..

No commits in common. "07aa3824a94cab5b02401dce6eebea74c55e7410" and "d02eb2e69c513bb145e21bce4969a9c4645ffc67" have entirely different histories.

7 changed files with 118 additions and 300 deletions

View File

@ -4,7 +4,6 @@
],
"files.associations": {
"socket_wrapper.h": "c",
"stdalign.h": "c",
"timerhelper.h": "c"
"stdalign.h": "c"
}
}

View File

@ -26,6 +26,8 @@ slowclient: socket_wrapper.o client.c
.PHONY: clean test
clean:
rm *.o $(Bin)
rm $(addprefix client_test/,$(ClientBin))
rm $(addprefix server_test/,$(ServerBin))
test:
make all

View File

@ -30,8 +30,6 @@ For server
- DEFAULT_WORK_QUEUE_SIZE(server only): 10
- DEFAULT_MAX_THREAD_NUMBER(server only): 10
- DEFAULT_RESPONSE_REQUEST(p-server only): 3(-1 is INF)
- USE_TRACE
- USE_NO_QUEUE
For client
- MUL_CLIENT(second unit)

244
client.c
View File

@ -13,9 +13,10 @@
#include <assert.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
#include <pthread.h>
#include "socket_wrapper.h"
#include "timerhelper.h"
#ifndef DEFAULT_TIMEOUT
static const int TIMEOUT = 5;
@ -187,9 +188,7 @@ int SendOpAndReceiveFile(const char * filename, struct sockaddr const * addr){
close(sock);
return ret;
}
//====
//bench
//====
struct benchmark_data{
bool benchmode;
struct timespec begin;
@ -209,75 +208,32 @@ void init_bench_data(){
clock_getres(bench.clock_id,&bench.resolution);
}
}
//====
//simple queue
//====
static 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;
}
static size_t thread_number_option = 1;
typedef enum{
From_CharArray,
From_FileStream
} 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{
size_t thread_arr_size;
pthread_t * thread_arr;
pthread_mutex_t queueing_mutex;
simple_queue_t queue;
queueing_method_t method;
const char ** filename_begin;
const char ** filename_end;
} 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{
int retval;
@ -304,6 +260,7 @@ void destroy_thread_arg(worker_arg_t * arg){
free(arg);
}
void * WorkerProc(void * args){
worker_arg_t * arg = (worker_arg_t *)(args);
worker_return_t * ret = create_worker_return();
@ -312,128 +269,123 @@ void * WorkerProc(void * args){
for(;;)
{
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);
if(filename == NULL) break;
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);
ret->retval += SendOpAndReceiveFile(filename,&arg->addr);
ret->op_count++;
}
destroy_thread_arg(arg);
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 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;
}
static bool stdinisatty;
int main(int argc, const char *argv[]){
struct sockaddr_in addr;
const char * filename;
const char * server_name;
in_port_t server_port = 0;
int arg_filename_start = 3;
int err;
int retval = 0;
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){
int check;
fprintf(stderr,"netdb fail: %s\n",gai_strerror(err));
//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);
if (check == 0){
fprintf(stderr,"parsing fail : invaild format\n");
return -1;
return 1;
}
}
addr.sin_family = AF_INET;
addr.sin_port = htons(cmd_args.server_port);
addr.sin_port = htons(server_port);
if (bench.benchmode){
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];
for (;;){
filename = dequeue_from_simplecharqueue(&global_state.queue,filename_buf,FILENAME_BUF_SIZE);
if (filename == NULL) break;
if (stdinisatty){
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);
bench.op_count++;
}
}
else{
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++){
worker_arg_t * arg = create_thread_arg((struct sockaddr *)&addr);
pthread_create(&global_state.thread_arr[i],NULL,WorkerProc,arg);

View File

@ -13,7 +13,6 @@
#include <assert.h>
#include <fcntl.h>
#include <sys/wait.h>
#include "socket_wrapper.h"
#ifndef DEFAULT_MAX_LISTEN_SOCKET
@ -52,15 +51,6 @@ static const int RESPONSE_REQUEST = 3;
static const int RESPONSE_REQUEST = DEFAULT_RESPONSE_REQUEST;
#endif
#define USE_TRACE
#include <sys/time.h>
#include <time.h>
#include "timerhelper.h"
enum{
Trace_Timer_ID = CLOCK_REALTIME
};
/*========
*Operation
*========*/
@ -286,22 +276,15 @@ int main(int argc, const char *argv[]){
char ip_buf[INET_ADDRSTRLEN];
const char * msg;
int retval = 0;
struct timespec ts,tsmiddle,tsend;
if((csock = accept(sock, (struct sockaddr *)&client_addr,&client_addr_len)) < 0){
free(buf);
perror("accept error");
return 1;
}
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));
pid = fork();
if(pid == 0){
#ifdef USE_TRACE
clock_gettime(Trace_Timer_ID,&tsmiddle);
#endif
if((fd = read_request(csock,buf,bufsize)) > 0){
retval = send_response(csock,fd,buf,bufsize);
close(fd);
@ -309,14 +292,6 @@ int main(int argc, const char *argv[]){
else retval = fd;
if(close(csock) < 0)
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);
return retval;
}

126
server.c
View File

@ -16,7 +16,6 @@
#include "socket_wrapper.h"
#include "simple_circular_buffer.h"
#include "timerhelper.h"
#ifndef DEFAULT_MAX_LISTEN_SOCKET
static const int MAX_LISTEN_SOCKET = 16;
@ -60,8 +59,6 @@ enum{
MAX_THREAD_NUMBER = DEFAULT_MAX_THREAD_NUMBER
#endif
};
#define USE_TRACE
#define USE_NO_QUEUE
/*========
*Operation
@ -235,19 +232,9 @@ int parse_args(int argc,const char * argv[] , in_port_t * port){
//============
//Simple Thread Pool
//============
#ifdef USE_TRACE
enum{
Trace_Timer_ID = CLOCK_REALTIME
};
#endif
#ifndef USE_NO_QUEUE
typedef struct SharedState{
//empty if less than 0
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_cond_t ready;
//int progress[MAX_THREAD_NUMBER];
@ -255,38 +242,25 @@ typedef struct SharedState{
void init_shared_state(shared_state_t * state) {
queue_init(&state->socks);
#ifdef USE_TRACE
queue_init(&state->trace_timer);
#endif
pthread_mutex_init(&state->sock_mutex,NULL);
pthread_cond_init(&state->ready,NULL);
}
#endif
//
typedef struct WorkerArgument
{
int id;
int bufsize;
uint8_t * buf;
#ifdef USE_NO_QUEUE
int csock;
#ifdef USE_TRACE
struct timespec ts;
#endif
#endif
//pthread_mutex_t * cond_mutex;
//pthread_cond_t cond;
} worker_argument_t;
__attribute_malloc__ worker_argument_t * create_worker_argument(int id, int bufsize
#ifdef USE_NO_QUEUE
, int csock
#endif
){
__attribute_malloc__ worker_argument_t * create_worker_argument(int id, int bufsize){
worker_argument_t * ret = (worker_argument_t *)malloc(sizeof(worker_argument_t));
if (ret == NULL) return ret;
ret->id = id;
#ifdef USE_NO_QUEUE
ret->csock = csock;
#endif
ret->bufsize = bufsize;
ret->buf = (uint8_t *)malloc(sizeof(*ret->buf)*bufsize);
if(ret->buf == NULL){
@ -295,76 +269,30 @@ __attribute_malloc__ worker_argument_t * create_worker_argument(int id, int bufs
}
return ret;
}
void destory_worker_argument(worker_argument_t * arg){
free(arg->buf);
free(arg);
}
#ifndef USE_NO_QUEUE
static shared_state_t globalState;
void * worker_proc(void * data){
worker_argument_t * args = (worker_argument_t *)data;
int fd, csock;
#ifdef USE_TRACE
struct timespec ts,ts_middle,ts_end;
#endif
for(;;){
pthread_mutex_lock(&globalState.sock_mutex);
while (queue_isempty(&globalState.socks)){
pthread_cond_wait(&globalState.ready,&globalState.sock_mutex);
}
csock = dequeue(&globalState.socks);
#ifdef USE_TRACE
ts = dequeue(&globalState.trace_timer);
#endif
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){
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;
}
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
if(close(csock) < 0)
perror("csock close error");
}
}
static pthread_t worker_threads[MAX_THREAD_NUMBER];
static int sock;
void safe_exit(){
@ -376,7 +304,7 @@ int main(int argc, const char *argv[]){
socklen_t client_addr_len = sizeof(client_addr);
int csock;
int bufsize;
int i = 0;
int i;
in_port_t binding_port_number = SERVER_PORT;
if (argc > 1){
int d = parse_args(argc,argv,&binding_port_number);
@ -395,9 +323,8 @@ int main(int argc, const char *argv[]){
perror("setsockopt");
}
}
bufsize = getBufferSizeFrom(sock);
#ifndef USE_NO_QUEUE
init_shared_state(&globalState);
bufsize = getBufferSizeFrom(sock);
for (i = 0; i < MAX_THREAD_NUMBER; i++) {
worker_argument_t * args = create_worker_argument(i,bufsize);
if (args == NULL) {
@ -406,7 +333,7 @@ int main(int argc, const char *argv[]){
}
pthread_create(&worker_threads[i],NULL,worker_proc,args);
}
#endif
addr.sin_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/
addr.sin_family = AF_INET;
addr.sin_port = htons(binding_port_number);
@ -431,11 +358,6 @@ int main(int argc, const char *argv[]){
char ip_buf[INET_ADDRSTRLEN];
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));
#ifdef USE_TRACE
struct timespec ts;
clock_gettime(Trace_Timer_ID, &ts);
#endif
#ifndef USE_NO_QUEUE
for(;;){
pthread_mutex_lock(&globalState.sock_mutex);
if (queue_isfull(&globalState.socks)){
@ -447,25 +369,11 @@ int main(int argc, const char *argv[]){
#endif
continue;
}
else {
enqueue(&globalState.socks,csock);
#ifdef USE_TRACE
enqueue(&globalState.trace_timer,ts);
#endif
}
else enqueue(&globalState.socks,csock);
break;
}
pthread_mutex_unlock(&globalState.sock_mutex);
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;

View File

@ -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