add server USE_TRACE
This commit is contained in:
parent
d02eb2e69c
commit
b53ed414b0
2
Makefile
2
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
|
||||
|
@ -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)
|
||||
|
13
client.c
13
client.c
@ -13,10 +13,9 @@
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/times.h>
|
||||
#include <pthread.h>
|
||||
#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;
|
||||
|
||||
|
38
server.c
38
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);
|
||||
|
16
timerhelper.h
Normal file
16
timerhelper.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef _TIMERHELPER_H_
|
||||
#define _TIMERHELPER_H_
|
||||
#include <sys/time.h>
|
||||
|
||||
__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
|
Loading…
Reference in New Issue
Block a user