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:
The timeval_subtract is from GNU libc's doc(I have my own one, but I
just think GNU's is absolutely correct one).
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.
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.
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.