chg high resolusion timer

aaa
This commit is contained in:
ubuntu201711081 2020-12-05 18:38:01 +00:00
parent 49505178b3
commit b8106683b9
2 changed files with 134 additions and 50 deletions

View File

@ -140,29 +140,29 @@ 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"); if (i == -2) fprintf(stderr,"timeout\n");
else perror("recv fail"); else perror("recv fail");
return -1; return -1;
} }
static char error_meesage_buf[80] = ""; static char error_meesage_buf[80] = "";
switch(res.res){ switch(res.res){
case RES_ERR: case RES_ERR:
fprintf(stderr,"Server Fail: %s", strerror(res.err_number)); fprintf(stderr,"Server Fail: %s\n", strerror(res.err_number));
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"); if (i == -2) fprintf(stderr,"timeout\n");
else perror("recv fail"); else perror("recv fail");
return -1; 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; 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!"); fprintf(stderr,"unknown value!\n");
return -1; return -1;
} }
@ -170,14 +170,32 @@ int recvData(int sock,const char * filename){
} }
struct benchmark_data{ struct benchmark_data{
bool benchmode; bool benchmode;
clock_t clock_sum; struct timespec begin;
clock_t begin_sclock; struct timespec end;
clock_t begin_uclock; clockid_t clock_id;
struct timespec resolution;
int op_count; int op_count;
} bench = {0,}; } bench = {0,};
void init_bench_data(){ void init_bench_data(){
int i;
memset(&bench,0,sizeof(bench)); 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[]){ int main(int argc, const char *argv[]){
@ -211,7 +229,7 @@ int main(int argc, const char *argv[]){
} }
if (server_port == 0){ if (server_port == 0){
fprintf(stderr,"port invalid"); fprintf(stderr,"port invalid\n");
return 1; return 1;
} }
@ -231,10 +249,7 @@ int main(int argc, const char *argv[]){
addr.sin_port = htons(server_port); addr.sin_port = htons(server_port);
if (bench.benchmode){ if (bench.benchmode){
struct tms t; clock_gettime(bench.clock_id,&bench.begin);
times(&t);
bench.begin_sclock = t.tms_stime;
bench.begin_uclock = t.tms_utime;
} }
while (arg_filename_start < argc){ while (arg_filename_start < argc){
filename = argv[arg_filename_start++]; filename = argv[arg_filename_start++];
@ -250,22 +265,21 @@ int main(int argc, const char *argv[]){
} }
if(sendReadOp(sock,filename) == 0){ if(sendReadOp(sock,filename) == 0){
int ret = recvData(sock,filename); int ret = recvData(sock,filename);
if (ret < 0){
fprintf(stderr,"%d",ret);
}
retval += ret; retval += ret;
} }
close(sock); close(sock);
bench.op_count++; bench.op_count++;
} }
if (bench.benchmode){ if (bench.benchmode){
struct tms t; struct timespec result;
times(&t); double avg;
bench.clock_sum += t.tms_stime - bench.begin_sclock; clock_gettime(bench.clock_id,&bench.end);
bench.clock_sum += t.tms_utime - bench.begin_uclock; result = timespec_sub(bench.end,bench.begin);
} if (result.tv_sec == 0) avg = result.tv_nsec;
if (bench.benchmode){ else avg = result.tv_sec * 1e9 + result.tv_nsec;
fprintf(stdout,"operation: %lf ticks/op\n",((double)bench.clock_sum)/((double)bench.op_count)); 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; return retval;

View File

@ -12,6 +12,9 @@
#include <stdbool.h> #include <stdbool.h>
#include <assert.h> #include <assert.h>
#include <fcntl.h> #include <fcntl.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
#include "socket_wrapper.h" #include "socket_wrapper.h"
#ifdef USE_SENDFILE #ifdef USE_SENDFILE
@ -82,6 +85,7 @@ void DisplayProgressBar100Percent(size_t total){
buf[PROGRESS_BAR_WIDTH] = '\0'; buf[PROGRESS_BAR_WIDTH] = '\0';
printf("\r[%s]: 100%% bytes: %ld/%ld bytes\n",buf,total,total); 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;
@ -115,7 +119,7 @@ int recvFile(int sock, const char * filename,size_t file_size){
return_value = -1; return_value = -1;
goto END; goto END;
} }
if( isProgressBarNeedUpdate(count,file_size,cur_progress) ){ if( DisplayProgress && isProgressBarNeedUpdate(count,file_size,cur_progress) ){
DisplayProgressBar(count,file_size,cur_progress); DisplayProgressBar(count,file_size,cur_progress);
cur_progress = (int)((((double)count / (double)file_size)) * 100.0 + 1.0); cur_progress = (int)((((double)count / (double)file_size)) * 100.0 + 1.0);
fflush(stdout); fflush(stdout);
@ -125,7 +129,8 @@ int recvFile(int sock, const char * filename,size_t file_size){
usleep(SLOW_CLIENT); usleep(SLOW_CLIENT);
#endif #endif
} }
DisplayProgressBar100Percent(file_size); if(DisplayProgress)
DisplayProgressBar100Percent(file_size);
END: END:
free(buf); free(buf);
return return_value; return return_value;
@ -135,58 +140,99 @@ 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"); if (i == -2) fprintf(stderr,"timeout\n");
else perror("recv fail"); else perror("recv fail");
return -1; return -1;
} }
static char error_meesage_buf[80] = ""; static char error_meesage_buf[80] = "";
switch(res.res){ switch(res.res){
case RES_ERR: case RES_ERR:
fprintf(stderr,"Server Fail: %s", strerror(res.err_number)); fprintf(stderr,"Server Fail: %s\n", strerror(res.err_number));
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"); if (i == -2) fprintf(stderr,"timeout\n");
else perror("recv fail"); else perror("recv fail");
return -1; 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; 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!"); fprintf(stderr,"unknown value!\n");
return -1; return -1;
} }
return 0; 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[]){ 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; const char * server_name;
in_port_t server_port = 0; in_port_t server_port = 0;
int arg_filename_start = 3;
int sock, err; int sock, err;
if (argc != 4){ int retval = 0;
fprintf(stderr,"USAUE: %s SERVERNAME PORT FILENAME\n",argv[0]); init_bench_data();
if (argc < 4){
fprintf(stderr,"USAUE: %s SERVERNAME PORT [Option]... [FILENAME]...\n",argv[0]);
return 1; return 1;
} }
server_name = argv[1]; server_name = argv[1];
server_port = atoi(argv[2]); server_port = atoi(argv[2]);
filename = argv[3]; for(;;){
if (server_port == 0) if (strcmp("-b",argv[arg_filename_start])==0
{ ||strcmp("--benchmark",argv[arg_filename_start])==0){
fprintf(stderr,"port invalid"); arg_filename_start++;
return 1; bench.benchmode = true;
} }
sock = socket(AF_INET,SOCK_STREAM,0); else if(strcmp("--nogui",argv[arg_filename_start])==0){
if(sock < 0){ arg_filename_start++;
perror("sock create fail"); DisplayProgress = false;
}
else break;
}
if (server_port == 0){
fprintf(stderr,"port invalid\n");
return 1; return 1;
} }
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,server_name,(struct sockaddr *)&addr); err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,server_name,(struct sockaddr *)&addr);
if (err != 0){ if (err != 0){
int check; int check;
@ -196,21 +242,45 @@ int main(int argc, const char *argv[]){
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");
close(sock);
return 1; return 1;
} }
} }
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(server_port); addr.sin_port = htons(server_port);
if(connect(sock,(struct sockaddr *)&addr,sizeof(addr)) < 0){
perror("connect failed"); if (bench.benchmode){
return 1; clock_gettime(bench.clock_id,&bench.begin);
} }
if(sendReadOp(sock,filename) == 0){ while (arg_filename_start < argc){
int ret = recvData(sock,filename); 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); close(sock);
return ret; bench.op_count++;
} }
close(sock); if (bench.benchmode){
return 0; 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;
} }