trace bottom up time

This commit is contained in:
ubuntu201711081 2020-12-08 09:58:19 +00:00
parent c23db73d71
commit bf3cc14c4d
2 changed files with 61 additions and 35 deletions

View File

@ -52,15 +52,25 @@ static const int RESPONSE_REQUEST = 3;
static const int RESPONSE_REQUEST = DEFAULT_RESPONSE_REQUEST;
#endif
#define USE_TRACE
#ifdef USE_TRACE
#include <sys/time.h>
#include <time.h>
#include "timerhelper.h"
enum{
Trace_Timer_ID = CLOCK_REALTIME
Top_Trace_Timer_ID = CLOCK_REALTIME,
Bottom_Trace_Timer_ID = CLOCK_THREAD_CPUTIME_ID
};
static inline void report_resolution()
{
struct timespec top_res,bottom_res;
clock_getres(Top_Trace_Timer_ID,&top_res);
clock_getres(Bottom_Trace_Timer_ID,&bottom_res);
fprintf(stderr,"top res: %ld, bottom res: %ld\n",top_res.tv_nsec,bottom_res.tv_nsec);
}
#endif
/*========
*Operation
*========*/
@ -243,6 +253,9 @@ int main(int argc, const char *argv[]){
int d = parse_args(argc,argv,&binding_port_number);
if(d != 0 ) return d;
}
#ifdef USE_TRACE
report_resolution();
#endif
sock = socket(AF_INET,SOCK_STREAM,0);
atexit(safe_exit);
if(sock < 0){
@ -286,7 +299,7 @@ int main(int argc, const char *argv[]){
char ip_buf[INET_ADDRSTRLEN];
const char * msg;
int retval = 0;
struct timespec ts,tsmiddle,tsend;
struct timespec ts_top_begin,ts_top_end, ts_bottom_begin, ts_bottom_end;
if((csock = accept(sock, (struct sockaddr *)&client_addr,&client_addr_len)) < 0){
free(buf);
perror("accept error");
@ -294,13 +307,14 @@ int main(int argc, const char *argv[]){
}
msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf));
#ifdef USE_TRACE
clock_gettime(Trace_Timer_ID,&ts);
clock_gettime(Top_Trace_Timer_ID,&ts_top_begin);
#endif
fprintf(stderr,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(client_addr.sin_port));
pid = fork();
if(pid == 0){
#ifdef USE_TRACE
clock_gettime(Trace_Timer_ID,&tsmiddle);
clock_gettime(Top_Trace_Timer_ID,&ts_top_end);
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_begin);
#endif
if((fd = read_request(csock,buf,bufsize)) > 0){
retval = send_response(csock,fd,buf,bufsize);
@ -310,12 +324,10 @@ int main(int argc, const char *argv[]){
if(close(csock) < 0)
perror("csock close error");
#ifdef USE_TRACE
clock_gettime(Trace_Timer_ID,&tsend);
struct timespec tophalf = timespec_sub(tsmiddle,ts);
struct timespec bottomhelf = timespec_sub(tsend,tsmiddle);
struct timespec resolution;
clock_getres(Trace_Timer_ID,&resolution);
fprintf(stderr,"top: %ld ns, bottom: %ld ns, res: %ld\n",tophalf.tv_nsec,bottomhelf.tv_nsec,resolution.tv_nsec);
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end);
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin);
struct timespec bottomhelf = timespec_sub(ts_bottom_end,ts_bottom_end);
fprintf(stderr,"top: %ld ns, bottom: %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec);
#endif
free(buf);
return retval;

View File

@ -16,7 +16,6 @@
#include "socket_wrapper.h"
#include "simple_circular_buffer.h"
#include "timerhelper.h"
#include "display_bar.h"
#ifndef DEFAULT_MAX_LISTEN_SOCKET
@ -65,6 +64,22 @@ enum{
//micro second unit
static const int SLOW_SERVER_TIME = SLOW_SERVER;
#endif
//#define USE_TRACE
#ifdef USE_TRACE
#include "timerhelper.h"
enum{
Top_Trace_Timer_ID = CLOCK_REALTIME,
Bottom_Trace_Timer_ID = CLOCK_THREAD_CPUTIME_ID
};
static inline void report_resolution()
{
struct timespec top_res,bottom_res;
clock_getres(Top_Trace_Timer_ID,&top_res);
clock_getres(Bottom_Trace_Timer_ID,&bottom_res);
fprintf(stderr,"top res: %ld, bottom res: %ld\n",top_res.tv_nsec,bottom_res.tv_nsec);
}
#endif
static bool use_gui = false;
@ -274,12 +289,6 @@ int parse_args(int argc,const char * argv[] , in_port_t * port){
//============
//Simple Thread Pool
//============
#ifdef USE_TRACE
enum{
Trace_Timer_ID = CLOCK_REALTIME
};
#endif
#ifndef USE_NO_QUEUE
typedef struct SharedState{
//empty if less than 0
@ -344,7 +353,7 @@ 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;
struct timespec ts_top_begin,ts_top_end, ts_bottom_begin, ts_bottom_end;
#endif
for(;;){
pthread_mutex_lock(&globalState.sock_mutex);
@ -353,20 +362,21 @@ void * worker_proc(void * data){
}
csock = dequeue(&globalState.socks);
#ifdef USE_TRACE
ts = dequeue(&globalState.trace_timer);
ts_top_begin = dequeue(&globalState.trace_timer);
#endif
pthread_mutex_unlock(&globalState.sock_mutex);
#ifdef USE_TRACE
clock_gettime(Trace_Timer_ID,&ts_middle);
clock_gettime(Top_Trace_Timer_ID,&ts_top_end);
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_begin);
#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);
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end);
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_end);
struct timespec bottomhelf = timespec_sub(ts_bottom_end,ts_bottom_begin);
if(use_gui&&isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
@ -387,18 +397,19 @@ void * worker_proc(void * data){
int fd, csock;
csock = args->csock;
#ifdef USE_TRACE
struct timespec ts,ts_middle,ts_end;
ts = args->ts;
clock_gettime(Trace_Timer_ID,&ts_middle);
struct timespec ts_top_begin,ts_top_end, ts_bottom_begin, ts_bottom_end;
ts_top_begin = args->ts;
clock_gettime(Top_Trace_Timer_ID,&ts_top_end);
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_begin);
#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);
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end);
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin);
struct timespec bottomhelf = timespec_sub(ts_bottom_end,ts_bottom_begin);
if(use_gui&&isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
@ -433,6 +444,9 @@ int main(int argc, const char *argv[]){
}
if(use_gui)
ready_progress_bar();
#ifdef USE_TRACE
report_resolution();
#endif
sock = socket(AF_INET,SOCK_STREAM,0);
atexit(safe_exit);
if(sock < 0){
@ -494,8 +508,8 @@ int main(int argc, const char *argv[]){
}
else 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);
struct timespec ts_top_begin;
clock_gettime(Top_Trace_Timer_ID, &ts_top_begin);
#endif
#ifndef USE_NO_QUEUE
for(;;){
@ -512,7 +526,7 @@ int main(int argc, const char *argv[]){
else {
enqueue(&globalState.socks,csock);
#ifdef USE_TRACE
enqueue(&globalState.trace_timer,ts);
enqueue(&globalState.trace_timer,ts_top_begin);
#endif
}
break;
@ -523,7 +537,7 @@ int main(int argc, const char *argv[]){
pthread_t thread_a;
worker_argument_t * args = create_worker_argument(i++,bufsize,csock);
#ifdef USE_TRACE
args->ts = ts;
args->ts = ts_top_begin;
#endif
pthread_create(&thread_a,NULL,worker_proc,args);
pthread_detach(thread_a);