From b8106683b96e98ca228679335cec3fca7e20635b Mon Sep 17 00:00:00 2001 From: ubuntu201711081 <201711081@jbnu.ac.kr> Date: Sat, 5 Dec 2020 18:38:01 +0000 Subject: [PATCH] chg high resolusion timer aaa --- client.c | 60 ++++++++++++++++---------- p-client.c | 124 +++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 134 insertions(+), 50 deletions(-) diff --git a/client.c b/client.c index 09b69a6..6053cb8 100644 --- a/client.c +++ b/client.c @@ -140,29 +140,29 @@ int recvData(int sock,const char * filename){ struct TransferResult res; int i=0; if((i=recv_until_byte(sock,&res,sizeof(res),TIMEOUT)) < 0){ - if (i == -2) fprintf(stderr,"timeout"); + if (i == -2) fprintf(stderr,"timeout\n"); else perror("recv fail"); return -1; } static char error_meesage_buf[80] = ""; switch(res.res){ case RES_ERR: - fprintf(stderr,"Server Fail: %s", strerror(res.err_number)); + fprintf(stderr,"Server Fail: %s\n", strerror(res.err_number)); return -1; case RES_USR_ERR: 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 == -2) fprintf(stderr,"timeout"); + if (i == -2) fprintf(stderr,"timeout\n"); else perror("recv fail"); return -1; } - fprintf(stderr,"Error Message From Server: %s",error_meesage_buf); + fprintf(stderr,"Error Message From Server: %s\n",error_meesage_buf); return -1; case RES_OK: return recvFile(sock,filename,res.file_size); break; default: - fprintf(stderr,"unknown value!"); + fprintf(stderr,"unknown value!\n"); return -1; } @@ -170,14 +170,32 @@ int recvData(int sock,const char * filename){ } struct benchmark_data{ bool benchmode; - clock_t clock_sum; - clock_t begin_sclock; - clock_t begin_uclock; + struct timespec begin; + struct timespec end; + clockid_t clock_id; + struct timespec resolution; int op_count; } bench = {0,}; void init_bench_data(){ + int i; memset(&bench,0,sizeof(bench)); + bench.clock_id = CLOCK_PROCESS_CPUTIME_ID; + i = clock_getres(bench.clock_id,&bench.resolution); + if (i < 0){ + bench.clock_id = CLOCK_REALTIME; + clock_getres(bench.clock_id,&bench.resolution); + } +} +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; } int main(int argc, const char *argv[]){ @@ -211,7 +229,7 @@ int main(int argc, const char *argv[]){ } if (server_port == 0){ - fprintf(stderr,"port invalid"); + fprintf(stderr,"port invalid\n"); return 1; } @@ -231,10 +249,7 @@ int main(int argc, const char *argv[]){ addr.sin_port = htons(server_port); if (bench.benchmode){ - struct tms t; - times(&t); - bench.begin_sclock = t.tms_stime; - bench.begin_uclock = t.tms_utime; + clock_gettime(bench.clock_id,&bench.begin); } while (arg_filename_start < argc){ filename = argv[arg_filename_start++]; @@ -250,22 +265,21 @@ int main(int argc, const char *argv[]){ } if(sendReadOp(sock,filename) == 0){ int ret = recvData(sock,filename); - if (ret < 0){ - fprintf(stderr,"%d",ret); - } retval += ret; } close(sock); bench.op_count++; } if (bench.benchmode){ - struct tms t; - times(&t); - bench.clock_sum += t.tms_stime - bench.begin_sclock; - bench.clock_sum += t.tms_utime - bench.begin_uclock; - } - if (bench.benchmode){ - fprintf(stdout,"operation: %lf ticks/op\n",((double)bench.clock_sum)/((double)bench.op_count)); + struct timespec result; + double avg; + clock_gettime(bench.clock_id,&bench.end); + result = timespec_sub(bench.end,bench.begin); + if (result.tv_sec == 0) avg = result.tv_nsec; + else avg = result.tv_sec * 1e9 + result.tv_nsec; + avg /= bench.op_count; + fprintf(stdout,"operation: %lf ns/op\n",avg); + fprintf(stdout,"resolution: %ld sec %ld ns\n",bench.resolution.tv_sec,bench.resolution.tv_nsec); } return retval; diff --git a/p-client.c b/p-client.c index 47d03f5..6053cb8 100644 --- a/p-client.c +++ b/p-client.c @@ -12,6 +12,9 @@ #include #include #include +#include +#include +#include #include "socket_wrapper.h" #ifdef USE_SENDFILE @@ -82,6 +85,7 @@ void DisplayProgressBar100Percent(size_t total){ 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 fd; size_t count = 0; @@ -115,7 +119,7 @@ int recvFile(int sock, const char * filename,size_t file_size){ return_value = -1; goto END; } - if( isProgressBarNeedUpdate(count,file_size,cur_progress) ){ + 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); @@ -125,7 +129,8 @@ int recvFile(int sock, const char * filename,size_t file_size){ usleep(SLOW_CLIENT); #endif } - DisplayProgressBar100Percent(file_size); + if(DisplayProgress) + DisplayProgressBar100Percent(file_size); END: free(buf); return return_value; @@ -135,58 +140,99 @@ int recvData(int sock,const char * filename){ struct TransferResult res; int i=0; if((i=recv_until_byte(sock,&res,sizeof(res),TIMEOUT)) < 0){ - if (i == -2) fprintf(stderr,"timeout"); + if (i == -2) fprintf(stderr,"timeout\n"); else perror("recv fail"); return -1; } static char error_meesage_buf[80] = ""; switch(res.res){ case RES_ERR: - fprintf(stderr,"Server Fail: %s", strerror(res.err_number)); + fprintf(stderr,"Server Fail: %s\n", strerror(res.err_number)); return -1; case RES_USR_ERR: 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 == -2) fprintf(stderr,"timeout"); + if (i == -2) fprintf(stderr,"timeout\n"); else perror("recv fail"); return -1; } - fprintf(stderr,"Error Message From Server: %s",error_meesage_buf); + fprintf(stderr,"Error Message From Server: %s\n",error_meesage_buf); return -1; case RES_OK: return recvFile(sock,filename,res.file_size); break; default: - fprintf(stderr,"unknown value!"); + fprintf(stderr,"unknown value!\n"); return -1; } return 0; } +struct benchmark_data{ + bool benchmode; + struct timespec begin; + struct timespec end; + clockid_t clock_id; + struct timespec resolution; + int op_count; +} bench = {0,}; + +void init_bench_data(){ + int i; + memset(&bench,0,sizeof(bench)); + bench.clock_id = CLOCK_PROCESS_CPUTIME_ID; + i = clock_getres(bench.clock_id,&bench.resolution); + if (i < 0){ + bench.clock_id = CLOCK_REALTIME; + clock_getres(bench.clock_id,&bench.resolution); + } +} +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; +} 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 sock, err; - if (argc != 4){ - fprintf(stderr,"USAUE: %s SERVERNAME PORT FILENAME\n",argv[0]); + int retval = 0; + init_bench_data(); + + + 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]); - filename = argv[3]; - if (server_port == 0) - { - fprintf(stderr,"port invalid"); - return 1; - } - sock = socket(AF_INET,SOCK_STREAM,0); - if(sock < 0){ - perror("sock create fail"); + 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){ int check; @@ -196,21 +242,45 @@ int main(int argc, const char *argv[]){ assert(check != -1); if (check == 0){ fprintf(stderr,"parsing fail : invaild format\n"); - close(sock); return 1; } } addr.sin_family = AF_INET; addr.sin_port = htons(server_port); - if(connect(sock,(struct sockaddr *)&addr,sizeof(addr)) < 0){ - perror("connect failed"); - return 1; + + if (bench.benchmode){ + clock_gettime(bench.clock_id,&bench.begin); } - if(sendReadOp(sock,filename) == 0){ - int ret = recvData(sock,filename); + while (arg_filename_start < argc){ + filename = argv[arg_filename_start++]; + 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){ + int ret = recvData(sock,filename); + retval += ret; + } close(sock); - return ret; + bench.op_count++; } - close(sock); - return 0; + if (bench.benchmode){ + struct timespec result; + double avg; + clock_gettime(bench.clock_id,&bench.end); + result = timespec_sub(bench.end,bench.begin); + if (result.tv_sec == 0) avg = result.tv_nsec; + else avg = result.tv_sec * 1e9 + result.tv_nsec; + avg /= bench.op_count; + fprintf(stdout,"operation: %lf ns/op\n",avg); + fprintf(stdout,"resolution: %ld sec %ld ns\n",bench.resolution.tv_sec,bench.resolution.tv_nsec); + } + + return retval; } \ No newline at end of file