View unanswered posts | View active topics It is currently Sat Oct 10, 2015 12:43 am

Reply to topic  [ 4 posts ] 
 gettimeofday() inaccuracy 
Author Message

Joined: Fri Oct 03, 2008 4:18 am
Posts: 4
Reply with quote
Hey folks,

I'm in search of a microsecond- or nanosecond-accurate timer for a project but the microsecond field returned from gettimeofday() is completely bogus. As a test I wrote a simple loop that calls gettimeofday() 100 times and prints the results each time before quitting.

Here's what it looks like in Windows (truncated for brevity, MinGW used):

1331965376; 300935
1331965376; 301935
1331965376; 302935
1331965376; 303935
1331965376; 304935
1331965376; 305935

This is what it looks like when ran on both a physical Wii and in the Dolphin emulator with any of the CPU options:

1331930967; 454
1331930967; 192
1331930967; 30
1331930967; 304
1331930967; 416
1331930967; 154

They are both in the order that they were executed. The Wii version doesn't just fail but it also bounces around with seemingly random numbers in the microsecond slot.

Is there anything that I can do either to fix gettimeofday() or replace it with another high-granularity timer?


Sat Mar 17, 2012 7:32 am

Joined: Fri Oct 03, 2008 4:18 am
Posts: 4
Reply with quote
For the record, after much Google'ing around I finally found that if I use gettime() and diff_usec(start_time, current_time) I can get an elapsed time in microseconds.
There are various diff_ functions for different time measurements available.
All are found in <ogc/lwp_watchdog.h>

Thu Mar 22, 2012 12:54 am

Joined: Thu Dec 10, 2009 9:26 am
Posts: 40
Reply with quote
It looks like __libogc_gettod_r() in timesupp.c should be using tick_to_microsecs() instead of tick_microsecs().

Sun Apr 01, 2012 1:52 pm

Joined: Mon Jul 04, 2011 1:03 pm
Posts: 23
Reply with quote
tueidj wrote:
It looks like __libogc_gettod_r() in timesupp.c should be using tick_to_microsecs() instead of tick_microsecs().

The function to be used in __libogc_gettod_r should be tick_microsecs() since tv_usec indicates the number of nanoseconds expired in the current second whilst ticks_to_microsecs() reports the total number of microsecs.

Anyhow there are two considerations:

1) The tick_microsecs() as well tick_nanosecs() macros of libogc are wrong:

In lwp_watchdog.h they are defined as

#define tick_microsecs(ticks) ((((u64)(ticks)*8)%(u64)(TB_TIMER_CLOCK/125)))
#define tick_nanosecs(ticks) ((((u64)(ticks)*8000)%(u64)(TB_TIMER_CLOCK/125)))

TB_TIMER_CLOCK/125 is 486. It means that the gives the rest of 486 that's wrong.

The correct functions should be:

#define tick_microsecs(ticks) ((((u64)(ticks)*8)/(u64)(TB_TIMER_CLOCK/125))%1000000)
#define tick_nanosecs(ticks) ((((u64)(ticks)*8000)/(u64)(TB_TIMER_CLOCK/125))%1000000000)

2) Also the use of tick_nanosecs() is not appropriate for __libogc_gettod_r. Indeed gettimeoftheday() should return the number of seconds since 00:00 hours, Jan 1, 1970 UTC whilst ticks functions represent the time elapsed from the WII switch on.

In timesupp.c there is another bug. The function clock_gettime() define tv_nsec as ticks_to_nanosecs(gettick()).
It would be more appropriate to use:

tp->tv_nsec = tick_nanosecs(gettick());

as in __libogc_gettod_r with the considerations made above for gettimeofday.

Sat Dec 13, 2014 1:46 pm
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
  Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.
Get devkitPro at Fast, secure and Free Open Source software downloads