Compare commits

...

4 Commits

Author SHA1 Message Date
ubuntu201711081
4eaed57a0c use client display bar 2020-12-08 15:15:12 +00:00
ubuntu201711081
55f3dc98bb update p-client 2020-12-08 14:37:19 +00:00
ubuntu201711081
5348e11015 fix MAX_TERMINAL_ROW 2020-12-08 14:37:10 +00:00
ubuntu201711081
9e0401a22c fix some bug 2020-12-08 11:37:54 +00:00
9 changed files with 383 additions and 161 deletions

1
.gitignore vendored
View File

@ -61,3 +61,4 @@ p-server
server server
client client
slowclient slowclient
slowserver

View File

@ -3,7 +3,8 @@ CFLAGS = -lm -Wall -O2 -pthread
ServerBin = server p-server ServerBin = server p-server
ClientBin = client p-client ClientBin = client p-client
Bin = $(ServerBin) $(ClientBin) Bin = $(ServerBin) $(ClientBin)
ClientObject = display_bar.o socket_wrapper.o
ServerObject = display_bar.o socket_wrapper.o
all: all:
make $(Bin) make $(Bin)
@ -11,19 +12,23 @@ socket_wrapper.o: socket_wrapper.c socket_wrapper.h
$(CC) -c socket_wrapper.c -o socket_wrapper.o $(CFLAGS) $(CC) -c socket_wrapper.c -o socket_wrapper.o $(CFLAGS)
display_bar.o: display_bar.c display_bar.h display_bar.o: display_bar.c display_bar.h
$(CC) -c display_bar.c -o display_bar.o $(CFLAGS) $(CC) -c display_bar.c -o display_bar.o $(CFLAGS)
#client
client: socket_wrapper.o client.c client: client.c $(ClientObject)
$(CC) -o client client.c socket_wrapper.o $(CFLAGS) $(CC) -o client client.c $(ClientObject) $(CFLAGS)
server: socket_wrapper.o server.c display_bar.o p-client: p-client.c $(ClientObject)
$(CC) -o server server.c socket_wrapper.o display_bar.o $(CFLAGS)
p-server: socket_wrapper.o p-server.c
$(CC) -o p-server p-server.c socket_wrapper.o $(CFLAGS)
p-client: socket_wrapper.o p-client.c
$(CC) -o p-client p-client.c socket_wrapper.o $(CFLAGS) $(CC) -o p-client p-client.c socket_wrapper.o $(CFLAGS)
p-mulclient: socket_wrapper.o p-client.c p-mulclient: p-client.c $(ClientObject)
$(CC) -o p-slowclient p-client.c socket_wrapper.o $(CFLAGS) -D MUL_CLIENT=10 $(CC) -o p-slowclient p-client.c $(ClientObject) $(CFLAGS) -D MUL_CLIENT=10
slowclient: socket_wrapper.o client.c slowclient: client.c $(ClientObject)
$(CC) -o slowclient client.c socket_wrapper.o $(CFLAGS) -D SLOW_CLIENT=1000 $(CC) -o slowclient client.c $(ClientObject) $(CFLAGS) -D SLOW_CLIENT=1000
#server
server: server.c $(ServerObject)
$(CC) -o server server.c $(ServerObject) $(CFLAGS)
p-server: p-server.c $(ServerObject)
$(CC) -o p-server p-server.c $(ServerObject) $(CFLAGS)
slowserver: server.c $(ServerObject)
$(CC) -o slowserver server.c $(ServerObject) $(CFLAGS) -D SLOW_SERVER=100000
.PHONY: clean test .PHONY: clean test
clean: clean:

View File

@ -41,5 +41,5 @@ For client
For both For both
- TIMEOUT: 5(second unit) - TIMEOUT: 5(second unit)
- MAX_TERMINAL_ROW - DEFAULT_MAX_TERMINAL_ROW: 1000
- DEFAULT_PROGRESS_BAR_WIDTH: 30 - DEFAULT_PROGRESS_BAR_WIDTH: 30

144
client.c
View File

@ -16,21 +16,26 @@
#include <pthread.h> #include <pthread.h>
#include "socket_wrapper.h" #include "socket_wrapper.h"
#include "timerhelper.h" #include "timerhelper.h"
#include "display_bar.h"
#ifndef DEFAULT_TIMEOUT #ifndef DEFAULT_TIMEOUT
static const int TIMEOUT = 5; static const int TIMEOUT = 5;
#else #else
static const int TIMEOUT = DEFAULT_TIMEOUT; static const int TIMEOUT = DEFAULT_TIMEOUT;
#endif #endif
#ifndef DEFAULT_PROGRESS_BAR_WIDTH
static const int PROGRESS_BAR_WIDTH = 30;
#else
static const int PROGRESS_BAR_WIDTH = DEFAULT_PROGRESS_BAR_WIDTH;
#endif
enum{ enum{
FILENAME_BUF_SIZE = 1024 FILENAME_BUF_SIZE = 1024
}; };
static bool DisplayProgress = true;
static inline void server_perror(const char * msg){
if(DisplayProgress)
myd_perror(msg);
else
perror(msg);
}
/*======== /*========
*Operation *Operation
*========*/ *========*/
@ -40,93 +45,63 @@ int sendReadOp(int sock,const char * filename){
op.file_url_size = strlen(filename); op.file_url_size = strlen(filename);
op.padding0 = 0; op.padding0 = 0;
if(send(sock,&op,sizeof(op),0)<0){ if(send(sock,&op,sizeof(op),0)<0){
perror("readop send fail"); server_perror("readop send fail");
return -1; return -1;
} }
#ifdef MUL_CLIENT #ifdef MUL_CLIENT
sleep(MUL_CLIENT); sleep(MUL_CLIENT);
#endif #endif
if(send(sock,filename,op.file_url_size,0)<0){ if(send(sock,filename,op.file_url_size,0)<0){
perror("readop filename send fail"); server_perror("readop filename send fail");
return -1; return -1;
} }
return 0; return 0;
} }
/**
* @arg cur_progress : it is percentage.
*/
bool isProgressBarNeedUpdate(size_t offset,size_t total,double cur_progress){
return ((double)offset / (double)total) > (cur_progress / 100.0);
}
void DisplayProgressBar(size_t offset,size_t total,double cur_progress){
char buf[PROGRESS_BAR_WIDTH];
size_t i;
size_t cur_pos = (cur_progress / 100.0 * PROGRESS_BAR_WIDTH); //must be less than SIZE_MAX. other value is undefined behavior.
for (i = 0; i < PROGRESS_BAR_WIDTH; i++)
{
if (i < cur_pos)
buf[i] = '=';
else if(i == cur_pos)
buf[i] = '>';
else buf[i] = '.';
}
printf("\r[%s]: %.2f%% bytes: %ld/%ld bytes",buf,cur_progress,total,offset);
}
void DisplayProgressBar100Percent(size_t total){
size_t i;
char buf[PROGRESS_BAR_WIDTH+1];
for (i = 0; i < PROGRESS_BAR_WIDTH; i++){
buf[i] = '=';
}
buf[PROGRESS_BAR_WIDTH] = '\0';
printf("\r[%s]: 100%% bytes: %ld/%ld bytes\n",buf,total,total);
}
static bool DisplayProgress = true;
int recvFile(int sock, const char * filename,size_t file_size){ int recvFile(int sock, const char * filename,size_t file_size){
int fd; int fd;
size_t count = 0; size_t count = 0;
int i; int i;
double cur_progress = 1;
int return_value = 0; int return_value = 0;
int buf_sz = getBufferSizeFrom(sock); int buf_sz = getBufferSizeFrom(sock);
progress_bar_t pbar;
uint8_t * buf = malloc(buf_sz*sizeof(*buf)); uint8_t * buf = malloc(buf_sz*sizeof(*buf));
if (buf == NULL){ if (buf == NULL){
return -1; return -1;
} }
fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRWXG|S_IROTH); fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRWXG|S_IROTH);
if (fd < 0) if (fd < 0){
{ server_perror("file open fail");
perror("file open fail");
return -1; return -1;
} }
if(DisplayProgress){
init_progress_bar(&pbar,10);
}
while (file_size - count > 0) while (file_size - count > 0)
{ {
int readed = buf_sz < file_size - count ? buf_sz : file_size - count; int readed = buf_sz < file_size - count ? buf_sz : file_size - count;
if(DisplayProgress){
DisplayProgressBar(&pbar,count,file_size,filename,false);
}
if((i = recv_until_byte(sock,buf,readed,TIMEOUT)) < 0){ if((i = recv_until_byte(sock,buf,readed,TIMEOUT)) < 0){
if(i == -2) if(i == -2)
fprintf(stderr,"recv file failed : timeout connetion lost\n"); fprintf(stderr,"recv file failed : timeout connetion lost\n");
else perror("recv file failed"); else server_perror("recv file failed");
return_value = -1; return_value = -1;
goto END; goto END;
} }
if(write(fd,buf,readed)<0){ if(write(fd,buf,readed)<0){
perror("file write failed"); server_perror("file write failed");
return_value = -1; return_value = -1;
goto END; goto END;
} }
if( DisplayProgress && isProgressBarNeedUpdate(count,file_size,cur_progress) ){
DisplayProgressBar(count,file_size,cur_progress);
cur_progress = (int)((((double)count / (double)file_size)) * 100.0 + 1.0);
fflush(stdout);
}
count += readed; count += readed;
#ifdef SLOW_CLIENT #ifdef SLOW_CLIENT
usleep(SLOW_CLIENT); usleep(SLOW_CLIENT);
#endif #endif
} }
if(DisplayProgress) if(DisplayProgress)
DisplayProgressBar100Percent(file_size); DisplayProgressBar(&pbar,count,file_size,filename,true);
END: END:
free(buf); free(buf);
close(fd); close(fd);
@ -137,29 +112,56 @@ int recvData(int sock,const char * filename){
struct TransferResult res; struct TransferResult res;
int i=0; int i=0;
if((i=recv_until_byte(sock,&res,sizeof(res),TIMEOUT)) < 0){ if((i=recv_until_byte(sock,&res,sizeof(res),TIMEOUT)) < 0){
if (i == -2) fprintf(stderr,"timeout\n"); if (i == -2) {
else perror("recv fail"); if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"timeout\n");
unlock_scrolled();
}
else fprintf(stderr,"timeout\n");
}
else server_perror("recv fail");
return -1; return -1;
} }
char error_meesage_buf[80] = ""; char error_meesage_buf[256] = "";
switch(res.res){ switch(res.res){
case RES_ERR: case RES_ERR:
fprintf(stderr,"Server Fail: %s\n", strerror(res.err_number)); strerror_r(res.err_number,error_meesage_buf,sizeof(error_meesage_buf));
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"Server Fail: %s\n", error_meesage_buf);
unlock_scrolled();
}
else fprintf(stderr,"Server Fail: %s\n", error_meesage_buf);
return -1; return -1;
case RES_USR_ERR: case RES_USR_ERR:
assert(res.error_msg_size < 80);/*todo : fix*/ assert(res.error_msg_size < 80);/*todo : fix*/
if((i=recv_until_byte(sock,error_meesage_buf,res.error_msg_size,TIMEOUT)) < 0){ if((i=recv_until_byte(sock,error_meesage_buf,res.error_msg_size,TIMEOUT)) < 0){
if (i == -2) fprintf(stderr,"timeout\n"); if (i == -2) fprintf(stderr,"timeout\n");
else perror("recv fail"); else server_perror("recv fail");
return -1; return -1;
} }
fprintf(stderr,"Error Message From Server: %s\n",error_meesage_buf); if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"Error Message From Server: %s\n",error_meesage_buf);
unlock_scrolled();
}
else fprintf(stderr,"Error Message From Server: %s\n",error_meesage_buf);
return -1; return -1;
case RES_OK: case RES_OK:
return recvFile(sock,filename,res.file_size); return recvFile(sock,filename,res.file_size);
break; break;
default: default:
fprintf(stderr,"unknown value!\n"); if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"unknown value!\n");
unlock_scrolled();
}
else fprintf(stderr,"unknown value!\n");
return -1; return -1;
} }
@ -170,17 +172,23 @@ static bool quiet_mode = false;
int SendOpAndReceiveFile(const char * filename, struct sockaddr const * addr){ int SendOpAndReceiveFile(const char * filename, struct sockaddr const * addr){
int sock; int sock;
int ret = -1; int ret = -1;
if(!quiet_mode) if(!quiet_mode){
fprintf(stdout,"request %s\n",filename); if(DisplayProgress && isatty_file(stdout)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stdout,"request %s\n",filename);
unlock_scrolled();
}
else fprintf(stdout,"request %s\n",filename);
}
sock = socket(AF_INET,SOCK_STREAM,0); sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0){ if(sock < 0){
perror("sock create fail"); server_perror("sock create fail");
return -1; return -1;
} }
if(connect(sock,(struct sockaddr *)addr,sizeof(*addr)) < 0){ if(connect(sock,(struct sockaddr *)addr,sizeof(*addr)) < 0){
perror("connect failed"); server_perror("connect failed");
return -1; return -1;
} }
if(sendReadOp(sock,filename) == 0){ if(sendReadOp(sock,filename) == 0){
@ -343,6 +351,7 @@ static const char * show_help_message(FILE * file,const char * argv0){
"Options and arguments: \n" "Options and arguments: \n"
"-b or --benchmark\t:benchmark mode\n" "-b or --benchmark\t:benchmark mode\n"
"-nv or --no-verbose\t:no progress bar\n" "-nv or --no-verbose\t:no progress bar\n"
"-q or --quiet\t:show no message\n"
"-h\t:print help message.\n"; "-h\t:print help message.\n";
fprintf(file,msg,argv0); fprintf(file,msg,argv0);
return msg; return msg;
@ -384,11 +393,12 @@ static int parse_arg(int argc,const char *argv[]){
} }
else if(strcmp("-h",argv[cur]) == 0 || strcmp("--help",argv[cur]) == 0){ else if(strcmp("-h",argv[cur]) == 0 || strcmp("--help",argv[cur]) == 0){
show_help_message(stdout,argv[0]); show_help_message(stdout,argv[0]);
return 0; return -1;
} }
else if(strcmp("-q",argv[cur]) == 0 || strcmp("--quiet",argv[cur]) == 0){ else if(strcmp("-q",argv[cur]) == 0 || strcmp("--quiet",argv[cur]) == 0){
cmd_args.quiet_mode = true; cmd_args.quiet_mode = true;
quiet_mode = true; quiet_mode = true;
DisplayProgress = false;
} }
else break; else break;
} }
@ -413,7 +423,13 @@ int main(int argc, const char *argv[]){
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,cmd_args.server_name,(struct sockaddr *)&addr); 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)); if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"netdb fail: %s\n",gai_strerror(err));
unlock_scrolled();
}
else 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,cmd_args.server_name,&addr.sin_addr);
assert(check != -1); assert(check != -1);

View File

@ -19,7 +19,7 @@ enum{
#ifndef DEFAULT_MAX_TERMINAL_ROW #ifndef DEFAULT_MAX_TERMINAL_ROW
MAX_TERMINAL_ROW = 1000, MAX_TERMINAL_ROW = 1000,
#else #else
MAX_TERMINAL_ROW = MAX_TERMINAL_ROW; MAX_TERMINAL_ROW = DEFAULT_MAX_TERMINAL_ROW;
#endif #endif
}; };
@ -108,7 +108,7 @@ void DisplayProgressBar(progress_bar_t * bar,size_t offset,size_t total,const ch
clock_gettime(CLOCK_REALTIME,&ts); clock_gettime(CLOCK_REALTIME,&ts);
if (!sync){ if (!sync){
dif = timespec_sub(ts,bar->last_update); dif = timespec_sub(ts,bar->last_update);
diff = dif.tv_nsec / 1000000 + dif.tv_sec; diff = dif.tv_nsec / 1000000 + dif.tv_sec*1000;
if (diff < bar->update_rate){ if (diff < bar->update_rate){
return; return;
} }

View File

@ -29,7 +29,7 @@ typedef struct {
int update_rate; int update_rate;
struct timespec last_update; struct timespec last_update;
} progress_bar_t; } progress_bar_t;
//update rate is millsecond unit
void init_progress_bar(progress_bar_t * bar,int update_rate); void init_progress_bar(progress_bar_t * bar,int update_rate);
void DisplayProgressBar(progress_bar_t * bar,size_t offset,size_t total,const char * filename, bool sync); void DisplayProgressBar(progress_bar_t * bar,size_t offset,size_t total,const char * filename, bool sync);

View File

@ -13,18 +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 <pthread.h>
#include <sys/times.h>
#include "socket_wrapper.h" #include "socket_wrapper.h"
#include "timerhelper.h"
#ifdef USE_SENDFILE
#include <sys/sendfile.h>
#ifndef DEFAULT_SEND_FILE_CHUNK_SIZE
const size_t SEND_FILE_CHUNK_SIZE = 0x100000; /*1MB*/
#else
const size_t SEND_FILE_CHUNK_SIZE = DEFAULT_SEND_FILE_CHUNK_SIZE; /*1MB*/
#endif
#endif
#ifndef DEFAULT_TIMEOUT #ifndef DEFAULT_TIMEOUT
static const int TIMEOUT = 5; static const int TIMEOUT = 5;
#else #else
@ -35,10 +27,14 @@ static const int PROGRESS_BAR_WIDTH = 30;
#else #else
static const int PROGRESS_BAR_WIDTH = DEFAULT_PROGRESS_BAR_WIDTH; static const int PROGRESS_BAR_WIDTH = DEFAULT_PROGRESS_BAR_WIDTH;
#endif #endif
enum{
FILENAME_BUF_SIZE = 1024
};
/*======== /*========
*Operation *Operation
*========*/ *========*/
//if success, return zero
int sendReadOp(int sock,const char * filename){ int sendReadOp(int sock,const char * filename){
struct ReadOp op; struct ReadOp op;
op.file_url_size = strlen(filename); op.file_url_size = strlen(filename);
@ -133,6 +129,7 @@ int recvFile(int sock, const char * filename,size_t file_size){
DisplayProgressBar100Percent(file_size); DisplayProgressBar100Percent(file_size);
END: END:
free(buf); free(buf);
close(fd);
return return_value; return return_value;
} }
@ -144,7 +141,7 @@ int recvData(int sock,const char * filename){
else perror("recv fail"); else perror("recv fail");
return -1; return -1;
} }
static char error_meesage_buf[80] = ""; char error_meesage_buf[80] = "";
switch(res.res){ switch(res.res){
case RES_ERR: case RES_ERR:
fprintf(stderr,"Server Fail: %s\n", strerror(res.err_number)); fprintf(stderr,"Server Fail: %s\n", strerror(res.err_number));
@ -168,6 +165,33 @@ int recvData(int sock,const char * filename){
return 0; return 0;
} }
static bool quiet_mode = false;
int SendOpAndReceiveFile(const char * filename, struct sockaddr const * addr){
int sock;
int ret = -1;
if(!quiet_mode)
fprintf(stdout,"request %s\n",filename);
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0){
perror("sock create fail");
return -1;
}
if(connect(sock,(struct sockaddr *)addr,sizeof(*addr)) < 0){
perror("connect failed");
return -1;
}
if(sendReadOp(sock,filename) == 0){
ret = recvData(sock,filename);
}
close(sock);
return ret;
}
//====
//bench
//====
struct benchmark_data{ struct benchmark_data{
bool benchmode; bool benchmode;
struct timespec begin; struct timespec begin;
@ -187,88 +211,247 @@ 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; typedef enum{
if (ret.tv_nsec < 0){ From_CharArray,
ret.tv_sec--; From_FileStream
ret.tv_nsec += 1000000000L; } 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_FileStream;
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;
} 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;
int op_count;
} worker_return_t;
__attribute_malloc__ worker_return_t * create_worker_return(){
return (worker_return_t *)malloc(sizeof(worker_return_t));
}
void destroy_worker_return(worker_return_t * r){
free(r);
}
typedef struct SimpleThreadArg{
struct sockaddr addr;
} worker_arg_t;
__attribute_malloc__ worker_arg_t * create_thread_arg(struct sockaddr * r){
worker_arg_t * ret = (worker_arg_t *)malloc(sizeof(*ret));
memcpy(&ret->addr,r,sizeof(*r));
return ret; return ret;
} }
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();
char filename_buf[FILENAME_BUF_SIZE];
const char * filename;
for(;;)
{
pthread_mutex_lock(&global_state.queueing_mutex);
filename = dequeue_from_simplecharqueue(&global_state.queue,filename_buf,FILENAME_BUF_SIZE);
pthread_mutex_unlock(&global_state.queueing_mutex);
if(filename == NULL) break;
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;
bool quiet_mode;
} 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.quiet_mode = false;
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 -1;
}
else if(strcmp("-q",argv[cur]) == 0 || strcmp("--quiet",argv[cur]) == 0){
cmd_args.quiet_mode = true;
quiet_mode = true;
}
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; int err;
in_port_t server_port = 0;
int arg_filename_start = 3;
int sock, err;
int retval = 0; int retval = 0;
init_bench_data(); 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 (argc < 4){
fprintf(stderr,"USAUE: %s SERVERNAME PORT [Option]... [FILENAME]...\n",argv[0]);
return 1;
}
server_name = argv[1];
server_port = atoi(argv[2]);
for(;;){
if (strcmp("-b",argv[arg_filename_start])==0
||strcmp("--benchmark",argv[arg_filename_start])==0){
arg_filename_start++;
bench.benchmode = true;
}
else if(strcmp("--nogui",argv[arg_filename_start])==0){
arg_filename_start++;
DisplayProgress = false;
}
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,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);
} }
while (arg_filename_start < argc){ if(cmd_args.thread_number_option == 1){
filename = argv[arg_filename_start++]; char filename_buf[FILENAME_BUF_SIZE];
sock = socket(AF_INET,SOCK_STREAM,0); for (;;){
if(sock < 0){ filename = dequeue_from_simplecharqueue(&global_state.queue,filename_buf,FILENAME_BUF_SIZE);
perror("sock create fail"); if (filename == NULL) break;
return 1;
}
if(connect(sock,(struct sockaddr *)&addr,sizeof(addr)) < 0){ retval += SendOpAndReceiveFile(filename,(struct sockaddr *)&addr);
perror("connect failed"); bench.op_count++;
return 1;
} }
if(sendReadOp(sock,filename) == 0){ }
int ret = recvData(sock,filename); else{
retval += ret; int i = 0;
init_global_state(&global_state,cmd_args.thread_number_option);
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);
}
for (i = 0; i < global_state.thread_arr_size; i++){
worker_return_t * ret;
pthread_join(global_state.thread_arr[i],(void **)&ret);
bench.op_count += ret->op_count;
retval += ret->retval;
destroy_worker_return(ret);
} }
close(sock);
bench.op_count++;
} }
if (bench.benchmode){ if (bench.benchmode){
struct timespec result; struct timespec result;
@ -278,7 +461,7 @@ int main(int argc, const char *argv[]){
if (result.tv_sec == 0) avg = result.tv_nsec; if (result.tv_sec == 0) avg = result.tv_nsec;
else avg = result.tv_sec * 1e9 + result.tv_nsec; else avg = result.tv_sec * 1e9 + result.tv_nsec;
avg /= bench.op_count; avg /= bench.op_count;
fprintf(stdout,"operation: %lf ns/op\n",avg); fprintf(stdout,"operation: %lf us/op\n",avg / 1000.0);
fprintf(stdout,"resolution: %ld sec %ld ns\n",bench.resolution.tv_sec,bench.resolution.tv_nsec); fprintf(stdout,"resolution: %ld sec %ld ns\n",bench.resolution.tv_sec,bench.resolution.tv_nsec);
} }

View File

@ -58,8 +58,16 @@ static const int RESPONSE_REQUEST = DEFAULT_RESPONSE_REQUEST;
#include "timerhelper.h" #include "timerhelper.h"
enum{ enum{
#ifndef DEFAULT_TOP_TRACE_TIMER_ID
Top_Trace_Timer_ID = CLOCK_REALTIME, Top_Trace_Timer_ID = CLOCK_REALTIME,
#else
Top_Trace_Timer_ID = DEFAULT_TOP_TRACE_TIMER_ID,
#endif
#ifndef DEFAULT_BOTTOM_TRACE_TIMER_ID
Bottom_Trace_Timer_ID = CLOCK_THREAD_CPUTIME_ID Bottom_Trace_Timer_ID = CLOCK_THREAD_CPUTIME_ID
#else
Bottom_Trace_Timer_ID = DEFAULT_BOTTOM_TRACE_TIMER_ID
#endif
}; };
static inline void report_resolution() static inline void report_resolution()
@ -311,7 +319,7 @@ int main(int argc, const char *argv[]){
#ifdef USE_TRACE #ifdef USE_TRACE
clock_gettime(Top_Trace_Timer_ID,&ts_top_begin); clock_gettime(Top_Trace_Timer_ID,&ts_top_begin);
#endif #endif
fprintf(stderr,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(client_addr.sin_port)); fprintf(stdout,"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 #ifdef USE_TRACE
@ -328,8 +336,8 @@ int main(int argc, const char *argv[]){
#ifdef USE_TRACE #ifdef USE_TRACE
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end); clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end);
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin); struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin);
struct timespec bottomhelf = timespec_sub(ts_bottom_end,ts_bottom_end); struct timespec bottomhalf = timespec_sub(ts_bottom_end,ts_bottom_begin);
fprintf(stderr,"top: %ld ns, bottom: %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec); fprintf(stderr,"top: %ld ns, bottom: %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
#endif #endif
free(buf); free(buf);
return retval; return retval;

View File

@ -68,8 +68,16 @@ static const int SLOW_SERVER_TIME = SLOW_SERVER;
#ifdef USE_TRACE #ifdef USE_TRACE
#include "timerhelper.h" #include "timerhelper.h"
enum{ enum{
#ifndef DEFAULT_TOP_TRACE_TIMER_ID
Top_Trace_Timer_ID = CLOCK_REALTIME, Top_Trace_Timer_ID = CLOCK_REALTIME,
#else
Top_Trace_Timer_ID = DEFAULT_TOP_TRACE_TIMER_ID,
#endif
#ifndef DEFAULT_BOTTOM_TRACE_TIMER_ID
Bottom_Trace_Timer_ID = CLOCK_THREAD_CPUTIME_ID Bottom_Trace_Timer_ID = CLOCK_THREAD_CPUTIME_ID
#else
Bottom_Trace_Timer_ID = DEFAULT_BOTTOM_TRACE_TIMER_ID
#endif
}; };
static inline void report_resolution() static inline void report_resolution()
@ -221,6 +229,8 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
#else #else
while (offset < r.file_size) while (offset < r.file_size)
{ {
if(use_gui)
DisplayProgressBar(&pbar,offset,r.file_size,"",false);
readed = bufsize < (r.file_size - offset) ? bufsize : r.file_size - offset; readed = bufsize < (r.file_size - offset) ? bufsize : r.file_size - offset;
if(read(fd,buf,readed)<0){ if(read(fd,buf,readed)<0){
server_perror("send response read fail"); server_perror("send response read fail");
@ -231,8 +241,6 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
return -1; return -1;
} }
offset += readed; offset += readed;
if(use_gui)
DisplayProgressBar(&pbar,offset,r.file_size,"",false);
#ifdef SLOW_SERVER #ifdef SLOW_SERVER
usleep(SLOW_SERVER_TIME); usleep(SLOW_SERVER_TIME);
#endif #endif
@ -375,14 +383,15 @@ void * worker_proc(void * data){
} }
#ifdef USE_TRACE #ifdef USE_TRACE
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end); clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end);
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_end); struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin);
struct timespec bottomhelf = timespec_sub(ts_bottom_end,ts_bottom_begin); struct timespec bottomhalf = timespec_sub(ts_bottom_end,ts_bottom_begin);
if(use_gui&&isatty_file(stderr)){ if(use_gui&&isatty_file(stderr)){
lock_scrolled(); lock_scrolled();
add_scrolled_unlocked(1); add_scrolled_unlocked(1);
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec); fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
unlock_scrolled(); unlock_scrolled();
} }
else fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
#endif #endif
if(close(csock) < 0) if(close(csock) < 0)
server_perror("csock close error"); server_perror("csock close error");
@ -409,14 +418,14 @@ void * worker_proc(void * data){
#ifdef USE_TRACE #ifdef USE_TRACE
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end); clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end);
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin); struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin);
struct timespec bottomhelf = timespec_sub(ts_bottom_end,ts_bottom_begin); struct timespec bottomhalf = timespec_sub(ts_bottom_end,ts_bottom_begin);
if(use_gui&&isatty_file(stderr)){ if(use_gui&&isatty_file(stderr)){
lock_scrolled(); lock_scrolled();
add_scrolled_unlocked(1); add_scrolled_unlocked(1);
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec); fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
unlock_scrolled(); unlock_scrolled();
} }
else fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec); else fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
#endif #endif
if(close(csock) < 0) if(close(csock) < 0)
server_perror("csock close error"); server_perror("csock close error");