A question about the gettimeofday(2) and aio_read(3)

Y

yueyu

Hi, guys, recently I meet a problem when invoking the gettimeofday(2).
The situation is that I'm using the aio_read(3) to send the IO requests
to kernel.
I want to know how long it will take to get the response pack from
kernel and the signal notification.
So I used the gettimeofday(2) to get the start time.
Then I called the aio_read(3), then wait for the result -- I set it
signal notification. When there is result return, the signal handler
will dispatch the result to another thread which will wake up the
thread that is waiting for the result.

When the thread is waken up, it will call the gettimeofday(2) again to
get the current time. Then it will calculate the elapsed time and add
them.

The codes snip is like:
Code:
               gettimeofday(&start,NULL);
		int err = 0;
		if( (err = aio_read(my_aiocb)) < 0){
 			perror("aio_reading");
 		}

		while(!my_data->ok){
			pthread_mutex_lock(&my_mutex);
			if(!my_data->ok)
				pthread_cond_wait(&my_cond,&my_mutex);
			pthread_mutex_unlock(&my_mutex);
		}
		gettimeofday(&end,NULL);
		timeval_subtract(&time,&end,&start);
		pthread_mutex_lock(&add_time_mutex);
		total_time += time.tv_sec * 1000000  + time.tv_usec;
		pthread_mutex_unlock(&add_time_mutex);

The timeval_subtract is from GNU libc's doc(I have my own one, but I
just think GNU's is absolutely correct one).
Code:
int
	timeval_subtract (result, x, y)
	     struct timeval *result, *x, *y;
	{
	  /* Perform the carry for the later subtraction by updating y. */
	  if (x->tv_usec < y->tv_usec) {
	    int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
	    y->tv_usec -= 1000000 * nsec;
	    y->tv_sec += nsec;
	  }
	  if (x->tv_usec - y->tv_usec > 1000000) {
	    int nsec = (y->tv_usec - x->tv_usec) / 1000000;
	    y->tv_usec += 1000000 * nsec;
	    y->tv_sec -= nsec;
	  }

	  /* Compute the time remaining to wait.
	     tv_usec  is certainly positive. */
	  result->tv_sec = x->tv_sec - y->tv_sec;
	  result->tv_usec = x->tv_usec - y->tv_usec;

	  /* Return 1 if result is negative. */
	  return x->tv_sec < y->tv_sec;
	}

The value total_time is a double,so it's safe to add them all.

When all threads exit, I will print out the whole read time. (divide
the total_time by 1000000 as seconds).
But the output confuses me.
Code:
$yueyu: time ./aiotest
finish,the read time is 28.956768

real    0m3.044s
user    0m1.948s
sys     0m0.424s

As you see, the whole time only 3 seconds, but the read time value is
28 seconds.
I've used a lot of method to replace how to measure the elapsed time.
But the same result.
Is there any limitation to use the gettimeofday in multi-threads or
signal driven program? I see nothing in the GNU libc's documentation.
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top