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

Discussion in 'Java' started by yueyu, Jan 23, 2007.

  1. yueyu

    yueyu Guest

    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.
     
    yueyu, Jan 23, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philip Meyer
    Replies:
    0
    Views:
    451
    Philip Meyer
    Nov 30, 2003
  2. Fred Ma

    How [non]standard is gettimeofday?

    Fred Ma, Nov 19, 2003, in forum: C Programming
    Replies:
    9
    Views:
    957
    Mark McIntyre
    Nov 20, 2003
  3. danu

    aio_read Help

    danu, Aug 1, 2006, in forum: C Programming
    Replies:
    3
    Views:
    431
    William Ahern
    Aug 1, 2006
  4. key9
    Replies:
    7
    Views:
    390
    Pete Becker
    Sep 13, 2006
  5. Laszlo Nagy
    Replies:
    0
    Views:
    277
    Laszlo Nagy
    Feb 24, 2009
Loading...

Share This Page