add TRACE p-server

This commit is contained in:
ubuntu201711081 2020-12-07 10:05:19 +00:00
parent 50eeba56a4
commit 07aa3824a9
3 changed files with 28 additions and 2 deletions

View File

@ -4,6 +4,7 @@
], ],
"files.associations": { "files.associations": {
"socket_wrapper.h": "c", "socket_wrapper.h": "c",
"stdalign.h": "c" "stdalign.h": "c",
"timerhelper.h": "c"
} }
} }

View File

@ -13,6 +13,7 @@
#include <assert.h> #include <assert.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/wait.h> #include <sys/wait.h>
#include "socket_wrapper.h" #include "socket_wrapper.h"
#ifndef DEFAULT_MAX_LISTEN_SOCKET #ifndef DEFAULT_MAX_LISTEN_SOCKET
@ -51,6 +52,15 @@ static const int RESPONSE_REQUEST = 3;
static const int RESPONSE_REQUEST = DEFAULT_RESPONSE_REQUEST; static const int RESPONSE_REQUEST = DEFAULT_RESPONSE_REQUEST;
#endif #endif
#define USE_TRACE
#include <sys/time.h>
#include <time.h>
#include "timerhelper.h"
enum{
Trace_Timer_ID = CLOCK_REALTIME
};
/*======== /*========
*Operation *Operation
*========*/ *========*/
@ -276,15 +286,22 @@ int main(int argc, const char *argv[]){
char ip_buf[INET_ADDRSTRLEN]; char ip_buf[INET_ADDRSTRLEN];
const char * msg; const char * msg;
int retval = 0; int retval = 0;
struct timespec ts,tsmiddle,tsend;
if((csock = accept(sock, (struct sockaddr *)&client_addr,&client_addr_len)) < 0){ if((csock = accept(sock, (struct sockaddr *)&client_addr,&client_addr_len)) < 0){
free(buf); free(buf);
perror("accept error"); perror("accept error");
return 1; return 1;
} }
msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf)); msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf));
#ifdef USE_TRACE
clock_gettime(Trace_Timer_ID,&ts);
#endif
fprintf(stderr,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(client_addr.sin_port)); fprintf(stderr,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(client_addr.sin_port));
pid = fork(); pid = fork();
if(pid == 0){ if(pid == 0){
#ifdef USE_TRACE
clock_gettime(Trace_Timer_ID,&tsmiddle);
#endif
if((fd = read_request(csock,buf,bufsize)) > 0){ if((fd = read_request(csock,buf,bufsize)) > 0){
retval = send_response(csock,fd,buf,bufsize); retval = send_response(csock,fd,buf,bufsize);
close(fd); close(fd);
@ -292,6 +309,14 @@ int main(int argc, const char *argv[]){
else retval = fd; else retval = fd;
if(close(csock) < 0) if(close(csock) < 0)
perror("csock close error"); 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);
#endif
free(buf); free(buf);
return retval; return retval;
} }

View File

@ -237,7 +237,7 @@ int parse_args(int argc,const char * argv[] , in_port_t * port){
//============ //============
#ifdef USE_TRACE #ifdef USE_TRACE
enum{ enum{
Trace_Timer_ID = CLOCK_PROCESS_CPUTIME_ID Trace_Timer_ID = CLOCK_REALTIME
}; };
#endif #endif