2020-12-04 08:06:26 +09:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
2020-12-04 20:11:35 +09:00
|
|
|
#include <stdbool.h>
|
2020-12-04 08:06:26 +09:00
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
2020-12-06 02:33:41 +09:00
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/times.h>
|
2020-12-06 18:50:13 +09:00
|
|
|
#include <pthread.h>
|
2020-12-04 08:06:26 +09:00
|
|
|
#include "socket_wrapper.h"
|
|
|
|
|
2020-12-04 17:33:41 +09:00
|
|
|
#ifndef DEFAULT_TIMEOUT
|
|
|
|
static const int TIMEOUT = 5;
|
|
|
|
#else
|
|
|
|
static const int TIMEOUT = DEFAULT_TIMEOUT;
|
2020-12-04 08:21:37 +09:00
|
|
|
#endif
|
2020-12-05 11:13:13 +09:00
|
|
|
#ifndef DEFAULT_PROGRESS_BAR_WIDTH
|
2020-12-04 20:11:35 +09:00
|
|
|
static const int PROGRESS_BAR_WIDTH = 30;
|
2020-12-05 11:13:13 +09:00
|
|
|
#else
|
|
|
|
static const int PROGRESS_BAR_WIDTH = DEFAULT_PROGRESS_BAR_WIDTH;
|
|
|
|
#endif
|
2020-12-06 18:50:13 +09:00
|
|
|
|
|
|
|
enum{
|
|
|
|
FILENAME_BUF_SIZE = 1024
|
|
|
|
};
|
2020-12-04 08:06:26 +09:00
|
|
|
/*========
|
|
|
|
*Operation
|
|
|
|
*========*/
|
2020-12-06 18:50:13 +09:00
|
|
|
//if success, return zero
|
2020-12-04 08:21:37 +09:00
|
|
|
int sendReadOp(int sock,const char * filename){
|
|
|
|
struct ReadOp op;
|
|
|
|
op.file_url_size = strlen(filename);
|
|
|
|
op.padding0 = 0;
|
|
|
|
if(send(sock,&op,sizeof(op),0)<0){
|
|
|
|
perror("readop send fail");
|
2020-12-04 08:06:26 +09:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-05 11:13:13 +09:00
|
|
|
#ifdef MUL_CLIENT
|
|
|
|
sleep(MUL_CLIENT);
|
2020-12-04 20:11:35 +09:00
|
|
|
#endif
|
2020-12-04 08:21:37 +09:00
|
|
|
if(send(sock,filename,op.file_url_size,0)<0){
|
|
|
|
perror("readop filename send fail");
|
2020-12-04 08:06:26 +09:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2020-12-04 20:11:35 +09:00
|
|
|
/**
|
|
|
|
* @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] = '>';
|
2020-12-05 11:13:13 +09:00
|
|
|
else buf[i] = '.';
|
2020-12-04 20:11:35 +09:00
|
|
|
}
|
|
|
|
printf("\r[%s]: %.2f%% bytes: %ld/%ld bytes",buf,cur_progress,total,offset);
|
|
|
|
}
|
|
|
|
void DisplayProgressBar100Percent(size_t total){
|
|
|
|
size_t i;
|
2020-12-05 11:21:10 +09:00
|
|
|
char buf[PROGRESS_BAR_WIDTH+1];
|
2020-12-04 20:11:35 +09:00
|
|
|
for (i = 0; i < PROGRESS_BAR_WIDTH; i++){
|
|
|
|
buf[i] = '=';
|
|
|
|
}
|
2020-12-05 11:21:10 +09:00
|
|
|
buf[PROGRESS_BAR_WIDTH] = '\0';
|
2020-12-04 20:11:35 +09:00
|
|
|
printf("\r[%s]: 100%% bytes: %ld/%ld bytes\n",buf,total,total);
|
|
|
|
}
|
2020-12-06 02:55:36 +09:00
|
|
|
static bool DisplayProgress = true;
|
2020-12-04 08:21:37 +09:00
|
|
|
int recvFile(int sock, const char * filename,size_t file_size){
|
2020-12-04 08:06:26 +09:00
|
|
|
int fd;
|
2020-12-04 08:21:37 +09:00
|
|
|
size_t count = 0;
|
|
|
|
int i;
|
2020-12-05 11:13:13 +09:00
|
|
|
double cur_progress = 1;
|
2020-12-04 08:21:37 +09:00
|
|
|
int return_value = 0;
|
|
|
|
int buf_sz = getBufferSizeFrom(sock);
|
|
|
|
uint8_t * buf = malloc(buf_sz*sizeof(*buf));
|
|
|
|
if (buf == NULL){
|
2020-12-04 08:06:26 +09:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-04 08:21:37 +09:00
|
|
|
fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRWXG|S_IROTH);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
perror("file open fail");
|
2020-12-04 08:06:26 +09:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-04 08:21:37 +09:00
|
|
|
|
|
|
|
while (file_size - count > 0)
|
|
|
|
{
|
|
|
|
int readed = buf_sz < file_size - count ? buf_sz : file_size - count;
|
|
|
|
if((i = recv_until_byte(sock,buf,readed,TIMEOUT)) < 0){
|
|
|
|
if(i == -2)
|
|
|
|
fprintf(stderr,"recv file failed : timeout connetion lost\n");
|
|
|
|
else perror("recv file failed");
|
|
|
|
return_value = -1;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
if(write(fd,buf,readed)<0){
|
|
|
|
perror("file write failed");
|
|
|
|
return_value = -1;
|
|
|
|
goto END;
|
|
|
|
}
|
2020-12-06 02:55:36 +09:00
|
|
|
if( DisplayProgress && isProgressBarNeedUpdate(count,file_size,cur_progress) ){
|
2020-12-04 20:11:35 +09:00
|
|
|
DisplayProgressBar(count,file_size,cur_progress);
|
2020-12-04 08:21:37 +09:00
|
|
|
cur_progress = (int)((((double)count / (double)file_size)) * 100.0 + 1.0);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
count += readed;
|
2020-12-05 11:13:13 +09:00
|
|
|
#ifdef SLOW_CLIENT
|
|
|
|
usleep(SLOW_CLIENT);
|
|
|
|
#endif
|
2020-12-04 08:06:26 +09:00
|
|
|
}
|
2020-12-06 02:55:36 +09:00
|
|
|
if(DisplayProgress)
|
|
|
|
DisplayProgressBar100Percent(file_size);
|
2020-12-04 08:21:37 +09:00
|
|
|
END:
|
|
|
|
free(buf);
|
2020-12-06 14:19:22 +09:00
|
|
|
close(fd);
|
2020-12-04 08:21:37 +09:00
|
|
|
return return_value;
|
2020-12-04 08:06:26 +09:00
|
|
|
}
|
|
|
|
|
2020-12-04 08:21:37 +09:00
|
|
|
int recvData(int sock,const char * filename){
|
|
|
|
struct TransferResult res;
|
|
|
|
int i=0;
|
|
|
|
if((i=recv_until_byte(sock,&res,sizeof(res),TIMEOUT)) < 0){
|
2020-12-06 03:38:01 +09:00
|
|
|
if (i == -2) fprintf(stderr,"timeout\n");
|
2020-12-04 08:21:37 +09:00
|
|
|
else perror("recv fail");
|
2020-12-04 08:06:26 +09:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-04 08:21:37 +09:00
|
|
|
static char error_meesage_buf[80] = "";
|
|
|
|
switch(res.res){
|
|
|
|
case RES_ERR:
|
2020-12-06 03:38:01 +09:00
|
|
|
fprintf(stderr,"Server Fail: %s\n", strerror(res.err_number));
|
2020-12-04 08:06:26 +09:00
|
|
|
return -1;
|
2020-12-04 08:21:37 +09:00
|
|
|
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){
|
2020-12-06 03:38:01 +09:00
|
|
|
if (i == -2) fprintf(stderr,"timeout\n");
|
2020-12-04 08:21:37 +09:00
|
|
|
else perror("recv fail");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-12-06 03:38:01 +09:00
|
|
|
fprintf(stderr,"Error Message From Server: %s\n",error_meesage_buf);
|
2020-12-04 08:06:26 +09:00
|
|
|
return -1;
|
2020-12-04 08:21:37 +09:00
|
|
|
case RES_OK:
|
|
|
|
return recvFile(sock,filename,res.file_size);
|
|
|
|
break;
|
|
|
|
default:
|
2020-12-06 03:38:01 +09:00
|
|
|
fprintf(stderr,"unknown value!\n");
|
2020-12-04 08:06:26 +09:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-04 08:21:37 +09:00
|
|
|
|
2020-12-04 08:06:26 +09:00
|
|
|
return 0;
|
|
|
|
}
|
2020-12-06 18:50:13 +09:00
|
|
|
|
|
|
|
int SendOpAndReceiveFile(const char * filename, struct sockaddr const * addr){
|
|
|
|
int sock;
|
|
|
|
int ret = -1;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-12-06 02:55:36 +09:00
|
|
|
struct benchmark_data{
|
|
|
|
bool benchmode;
|
2020-12-06 03:38:01 +09:00
|
|
|
struct timespec begin;
|
|
|
|
struct timespec end;
|
|
|
|
clockid_t clock_id;
|
|
|
|
struct timespec resolution;
|
2020-12-06 02:55:36 +09:00
|
|
|
int op_count;
|
|
|
|
} bench = {0,};
|
|
|
|
|
|
|
|
void init_bench_data(){
|
2020-12-06 03:38:01 +09:00
|
|
|
int i;
|
2020-12-06 02:55:36 +09:00
|
|
|
memset(&bench,0,sizeof(bench));
|
2020-12-06 03:38:01 +09:00
|
|
|
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;
|
2020-12-06 02:55:36 +09:00
|
|
|
}
|
2020-12-04 08:21:37 +09:00
|
|
|
|
2020-12-06 18:50:13 +09:00
|
|
|
static size_t thread_number_option = 1;
|
|
|
|
|
|
|
|
typedef enum{
|
|
|
|
From_CharArray,
|
|
|
|
From_FileStream
|
|
|
|
} queueing_method_t;
|
|
|
|
|
|
|
|
struct SimpleThreadGlobal{
|
|
|
|
size_t thread_arr_size;
|
|
|
|
pthread_t * thread_arr;
|
|
|
|
pthread_mutex_t queueing_mutex;
|
|
|
|
queueing_method_t method;
|
|
|
|
const char ** filename_begin;
|
|
|
|
const char ** filename_end;
|
|
|
|
} global_state;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
|
|
|
|
ret->retval += SendOpAndReceiveFile(filename,&arg->addr);
|
|
|
|
ret->op_count++;
|
|
|
|
}
|
|
|
|
destroy_thread_arg(arg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-12-06 14:19:22 +09:00
|
|
|
static bool stdinisatty;
|
|
|
|
|
2020-12-04 08:06:26 +09:00
|
|
|
int main(int argc, const char *argv[]){
|
|
|
|
struct sockaddr_in addr;
|
2020-12-04 08:21:37 +09:00
|
|
|
const char * filename;
|
|
|
|
const char * server_name;
|
2020-12-04 20:11:35 +09:00
|
|
|
in_port_t server_port = 0;
|
2020-12-06 02:33:41 +09:00
|
|
|
int arg_filename_start = 3;
|
2020-12-06 18:50:13 +09:00
|
|
|
int err;
|
2020-12-06 02:33:41 +09:00
|
|
|
int retval = 0;
|
2020-12-06 18:50:13 +09:00
|
|
|
|
2020-12-06 02:55:36 +09:00
|
|
|
init_bench_data();
|
2020-12-06 14:19:22 +09:00
|
|
|
stdinisatty = isatty(STDIN_FILENO);
|
2020-12-06 02:55:36 +09:00
|
|
|
|
2020-12-06 14:19:22 +09:00
|
|
|
if (argc < (stdinisatty ? 4 : 3)){
|
2020-12-06 02:33:41 +09:00
|
|
|
fprintf(stderr,"USAUE: %s SERVERNAME PORT [Option]... [FILENAME]...\n",argv[0]);
|
2020-12-04 08:21:37 +09:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
server_name = argv[1];
|
|
|
|
server_port = atoi(argv[2]);
|
2020-12-06 14:19:22 +09:00
|
|
|
while(arg_filename_start < argc){
|
2020-12-06 02:55:36 +09:00
|
|
|
if (strcmp("-b",argv[arg_filename_start])==0
|
|
|
|
||strcmp("--benchmark",argv[arg_filename_start])==0){
|
|
|
|
arg_filename_start++;
|
|
|
|
bench.benchmode = true;
|
|
|
|
}
|
2020-12-06 13:12:20 +09:00
|
|
|
else if(strcmp("-nv",argv[arg_filename_start]) == 0||strcmp("--no-verbose",argv[arg_filename_start])==0){
|
2020-12-06 02:55:36 +09:00
|
|
|
arg_filename_start++;
|
|
|
|
DisplayProgress = false;
|
|
|
|
}
|
2020-12-06 18:50:13 +09:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-12-06 02:55:36 +09:00
|
|
|
else break;
|
2020-12-04 08:21:37 +09:00
|
|
|
}
|
2020-12-06 02:33:41 +09:00
|
|
|
if (server_port == 0){
|
2020-12-06 03:38:01 +09:00
|
|
|
fprintf(stderr,"port invalid\n");
|
2020-12-04 08:06:26 +09:00
|
|
|
return 1;
|
|
|
|
}
|
2020-12-06 02:33:41 +09:00
|
|
|
|
2020-12-04 20:11:35 +09:00
|
|
|
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,server_name,(struct sockaddr *)&addr);
|
2020-12-04 15:56:21 +09:00
|
|
|
if (err != 0){
|
|
|
|
int check;
|
2020-12-04 16:48:03 +09:00
|
|
|
fprintf(stderr,"netdb fail: %s\n",gai_strerror(err));
|
2020-12-04 15:56:21 +09:00
|
|
|
//assume that sernmae is *.*.*.* and try to parse 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;
|
2020-12-04 08:21:37 +09:00
|
|
|
}
|
2020-12-04 08:06:26 +09:00
|
|
|
}
|
|
|
|
addr.sin_family = AF_INET;
|
2020-12-04 08:21:37 +09:00
|
|
|
addr.sin_port = htons(server_port);
|
2020-12-06 02:33:41 +09:00
|
|
|
|
2020-12-06 02:55:36 +09:00
|
|
|
if (bench.benchmode){
|
2020-12-06 03:38:01 +09:00
|
|
|
clock_gettime(bench.clock_id,&bench.begin);
|
2020-12-04 08:06:26 +09:00
|
|
|
}
|
2020-12-06 18:50:13 +09:00
|
|
|
if(thread_number_option == 1){
|
|
|
|
char filename_buf[FILENAME_BUF_SIZE];
|
|
|
|
for (;;){
|
|
|
|
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++;
|
2020-12-06 02:33:41 +09:00
|
|
|
}
|
2020-12-06 18:50:13 +09:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
int i = 0;
|
|
|
|
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);
|
2020-12-06 02:33:41 +09:00
|
|
|
}
|
2020-12-06 18:50:13 +09:00
|
|
|
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);
|
2020-12-06 02:33:41 +09:00
|
|
|
}
|
2020-12-04 08:06:26 +09:00
|
|
|
}
|
2020-12-06 02:55:36 +09:00
|
|
|
if (bench.benchmode){
|
2020-12-06 03:38:01 +09:00
|
|
|
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;
|
2020-12-06 14:25:42 +09:00
|
|
|
fprintf(stdout,"operation: %lf us/op\n",avg / 1000.0);
|
2020-12-06 03:38:01 +09:00
|
|
|
fprintf(stdout,"resolution: %ld sec %ld ns\n",bench.resolution.tv_sec,bench.resolution.tv_nsec);
|
2020-12-06 02:33:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
2020-12-04 08:06:26 +09:00
|
|
|
}
|