Hi All,
I am facing a strang behaviour of gettimeofday() function in the TTCP
application, this application I am using for testing the available
bandwidth of my LAN. The URL to download the source is
http://www.pcausa.com/Utilities/pcattcp.htm.
Its just a simple send and receive application, in which sender will
send some amount of data and receiver will receive, after receiving it
will calculate the time spent while receiving, based on that it will
calculate the data transfer speed in Mbps.
Now if we run the receiver, the in its first 1-2 execution it will
show a time difference in 0.00 Sec(I mean for 2048 calls of recvfrom()
and each packet is of size 8192 bytes in approx 2 microsec). Later it
gives the exact results(approx 1.42 Sec.). It seems like a problem
with gettimeofday() API.
Can any one comment on this????
Though I am attaching a simplify receiver code below,
int main (int argc, char *argv[])
{
int buflen = 65535; //length of buffer
char *buf; //ptr to dynamic buffer
int recvd_buflen = 0; //Length of received buffer.
int eof_flag = 0; //Holds end of transfer flag.
unsigned long io_calls = 0; //Holds number of I/O system calls.
struct timeval start_time; //Time at which timing started
struct timeval end_time; //Time at which timing ended
struct timeval total_time; //Total time elapsed.
double nbytes = 0.0; //Total bytes transfered.
double real_time = 0.0; //Real time in seconds.
if (argc != 2) {
printf("\nUSAGE : speed_measure_recv source_port\n\n");
return 1;
}
//Holds UDP class instance.
ofi_net_udp *udp_recv = new ofi_net_udp();
//Allocate memory for recv packet buffers.
buf = (char*)malloc(buflen*sizeof(char));
printf("Receiver is listening...........\n");
//Initialize receiver for receiving purpose.
udp_recv->initialize_udp_recv("127.0.0.1", atoi(argv[1]));
//Start receiving data.
do {
udp_recv->receive_packet(buf, buflen, &recvd_buflen);
io_calls++;
if(recvd_buflen <= 4) {
if(eof_flag) {
break; //End of transfer.
}
//Set end of transfer flag.
eof_flag = 1;
//Get start time
gettimeofday(&start_time, (struct timezone *)NULL);
} else {
nbytes += recvd_buflen;
}
} while (recvd_buflen > 0);
//Get end time.
gettimeofday(&end_time, (struct timezone *)NULL);
//Get real time in seconds.
tvsub( &total_time, &end_time, &start_time);
real_time = total_time.tv_sec + ((double)total_time.tv_usec) /
1000000;
//Set minimum time.
if(real_time <= 0.0)
real_time = 0.001;
printf("Recvd %0.f bytes in %.2f real seconds\n",nbytes,
real_time);
printf("Total number of I/O calls is %d\n", io_calls);
printf("Hence Speed = %.2f Mbps\n",
(nbytes*8)/(real_time*1000*1000));
//Free packet buffer.
delete(buf);
//Delete udp class instance.
delete(udp_recv);
}
//Function for getting the difference of two timeval structure.
void tvsub(struct timeval *tdiff, struct timeval *t1, struct timeval
*t0)
{
tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
if (tdiff->tv_usec < 0)
tdiff->tv_sec--, tdiff->tv_usec += 1000000;
}