J
Johann Schlamp
Hi folks,
I have to calculate the time needed for specific parts of my program.
Here is some sample code:
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
int main(int argc, char *argv[])
{
int timerUsed = CLOCK_THREAD_CPUTIME_ID;
double timeStart, timeEnd;
struct timespec realtime_timeStart, realtime_timeEnd;
clock_gettime (timerUsed, &realtime_timeStart);
// the time consuming part
clock_gettime (timerUsed, &realtime_timeEnd);
timeStart = ((realtime_timeStart.tv_sec) * 1000000000) +
(realtime_timeStart.tv_nsec);
timeEnd = ((realtime_timeEnd.tv_sec) * 1000000000) +
(realtime_timeEnd.tv_nsec);
printf("OVERALL TIME: %g microseconds\n", (timeEnd-timeStart)/1000);
return 0;
}
This code works fine as far as I can tell, but not for the following
"time consuming part":
char* msg = "SSH-2.0-test\r\n";
char* target = "localhost";
int portno = 22;
char buffer[256];
bzero(buffer, sizeof(buffer));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
inet_aton(target, &server);
serv_addr.sin_addr = server;
socketfd = socket(AF_INET, SOCK_STREAM, 0);
connect(socketfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
write(socketfd, msg, strlen(msg));
read(socketfd,buffer,255);
close(socketfd);
The overall time results for opening a socket, writing, reading and
closing it seem to be random. Sometimes there are valid times in the
range of 300-500 microseconds, but then again I get even times less than
5 nanoseconds which definitely can't be right, can it?
I tried to break it down to as less statements as possible being in the
measured block, and I checked all return values as well as the contents
of the read buffer. I also used tcpdump to verify the correct sending of
packets and checked my sshd-logs for the corresponding connection try
entries. Everything seems to be just right, except for the random timing
results.
Compiling with -O0 or other flags preventing code optimization doesn't
help either.
Maybe someone around here has more experience in C socket programming
than I do, and could give me some advise or point me to the right
direction... Any help would be appreciated!
Best regards,
Johann
I have to calculate the time needed for specific parts of my program.
Here is some sample code:
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
int main(int argc, char *argv[])
{
int timerUsed = CLOCK_THREAD_CPUTIME_ID;
double timeStart, timeEnd;
struct timespec realtime_timeStart, realtime_timeEnd;
clock_gettime (timerUsed, &realtime_timeStart);
// the time consuming part
clock_gettime (timerUsed, &realtime_timeEnd);
timeStart = ((realtime_timeStart.tv_sec) * 1000000000) +
(realtime_timeStart.tv_nsec);
timeEnd = ((realtime_timeEnd.tv_sec) * 1000000000) +
(realtime_timeEnd.tv_nsec);
printf("OVERALL TIME: %g microseconds\n", (timeEnd-timeStart)/1000);
return 0;
}
This code works fine as far as I can tell, but not for the following
"time consuming part":
char* msg = "SSH-2.0-test\r\n";
char* target = "localhost";
int portno = 22;
char buffer[256];
bzero(buffer, sizeof(buffer));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
inet_aton(target, &server);
serv_addr.sin_addr = server;
socketfd = socket(AF_INET, SOCK_STREAM, 0);
connect(socketfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
write(socketfd, msg, strlen(msg));
read(socketfd,buffer,255);
close(socketfd);
The overall time results for opening a socket, writing, reading and
closing it seem to be random. Sometimes there are valid times in the
range of 300-500 microseconds, but then again I get even times less than
5 nanoseconds which definitely can't be right, can it?
I tried to break it down to as less statements as possible being in the
measured block, and I checked all return values as well as the contents
of the read buffer. I also used tcpdump to verify the correct sending of
packets and checked my sshd-logs for the corresponding connection try
entries. Everything seems to be just right, except for the random timing
results.
Compiling with -O0 or other flags preventing code optimization doesn't
help either.
Maybe someone around here has more experience in C socket programming
than I do, and could give me some advise or point me to the right
direction... Any help would be appreciated!
Best regards,
Johann