From b53ed414b03017178d43569ecd3218b00672dc58 Mon Sep 17 00:00:00 2001 From: ubuntu201711081 <201711081@jbnu.ac.kr> Date: Sun, 6 Dec 2020 15:26:59 +0000 Subject: [PATCH] add server USE_TRACE --- Makefile | 2 -- README.md | 1 + client.c | 13 +------------ server.c | 38 +++++++++++++++++++++++++++++++++++++- timerhelper.h | 16 ++++++++++++++++ 5 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 timerhelper.h diff --git a/Makefile b/Makefile index adc86df..5a0fe7d 100644 --- a/Makefile +++ b/Makefile @@ -26,8 +26,6 @@ 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 diff --git a/README.md b/README.md index ff93f02..d3e2622 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ 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 For client - MUL_CLIENT(second unit) diff --git a/client.c b/client.c index 6958711..53c0f0f 100644 --- a/client.c +++ b/client.c @@ -13,10 +13,9 @@ #include #include #include -#include -#include #include #include "socket_wrapper.h" +#include "timerhelper.h" #ifndef DEFAULT_TIMEOUT static const int TIMEOUT = 5; @@ -208,16 +207,6 @@ void init_bench_data(){ 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; -} static size_t thread_number_option = 1; diff --git a/server.c b/server.c index 81ec9eb..f3ec465 100644 --- a/server.c +++ b/server.c @@ -16,6 +16,7 @@ #include "socket_wrapper.h" #include "simple_circular_buffer.h" +#include "timerhelper.h" #ifndef DEFAULT_MAX_LISTEN_SOCKET static const int MAX_LISTEN_SOCKET = 16; @@ -235,13 +236,24 @@ int parse_args(int argc,const char * argv[] , in_port_t * port){ 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]; } shared_state_t; +#ifdef USE_TRACE +enum{ + Trace_Timer_ID = CLOCK_PROCESS_CPUTIME_ID +}; +#endif 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); } @@ -275,17 +287,32 @@ 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"); @@ -358,6 +385,10 @@ 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 for(;;){ pthread_mutex_lock(&globalState.sock_mutex); if (queue_isfull(&globalState.socks)){ @@ -369,7 +400,12 @@ int main(int argc, const char *argv[]){ #endif continue; } - else enqueue(&globalState.socks,csock); + else { + enqueue(&globalState.socks,csock); +#ifdef USE_TRACE + enqueue(&globalState.trace_timer,ts); +#endif + } break; } pthread_mutex_unlock(&globalState.sock_mutex); diff --git a/timerhelper.h b/timerhelper.h new file mode 100644 index 0000000..ae90e96 --- /dev/null +++ b/timerhelper.h @@ -0,0 +1,16 @@ +#ifndef _TIMERHELPER_H_ +#define _TIMERHELPER_H_ +#include + +__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 \ No newline at end of file