High resolution timing

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
invalid
Member
Member
Posts: 60
Joined: Thu Feb 23, 2012 8:39 am

High resolution timing

Post by invalid »

Hi,

I'm really embarrassed to ask this question, but all searches that I perform from time to time (as this is not a priority) fail - most probably because I use some wrong keywords.

The question is: how to get hardware timer readings (on standard x86 PC)?

I know about:
- counting ticks in the timer interrupt (but is it reliable?)
- reading CMOS timer (but I can only read 1-second resolution)
- RDTSC (this is what I use now - but how to convert it into time reliably?)

Is there a way to somehow read "crystal vibrations counter" or whatever from the hardware timer?

Please don't bash me, I really searched :)
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: High resolution timing

Post by bluemoon »

Try APIC_timer

All timer on PC may not be reliable, you should take account for:
- variable IO delay caused by bus, circuits etc.
- The CPU may be in SMM and queued the timer event(or skipped).
- The CPU may have CLI or handling other IRQ and the event is queued(or skipped).
- You ISR to actually handle such event also require some time, although it is neglectable in most case.

These all sum up to the error, dT

For low resolution timer, dT is neglectable compared to the frequency,
For very high resolution timer, dT may become observable.

In general, the high resolution timer is good to serval as signal for deadlines, but not accurate to be used as clock (counting ticks for a few days you may loss some seconds)
Rudster816
Member
Member
Posts: 141
Joined: Thu Jun 17, 2010 2:36 am

Re: High resolution timing

Post by Rudster816 »

http://en.wikipedia.org/wiki/High_Precision_Event_Timer

The HPET is at least 8 times more precise than the PIT. I think the most common HPET frequency is 15mhz though, which would make it ~12.5 times more precise.
rdos
Member
Member
Posts: 3396
Joined: Wed Oct 01, 2008 1:55 pm

Re: High resolution timing

Post by rdos »

Although this thread contains some junk, it goes into these details quite thoroughly: http://forum.osdev.org/viewtopic.php?f=15&t=24550
invalid
Member
Member
Posts: 60
Joined: Thu Feb 23, 2012 8:39 am

Re: High resolution timing

Post by invalid »

bluemoon wrote:All timer on PC may not be reliable, you should take account for:
- variable IO delay caused by bus, circuits etc.
- The CPU may be in SMM and queued the timer event(or skipped).
- The CPU may have CLI or handling other IRQ and the event is queued(or skipped).
- You ISR to actually handle such event also require some time, although it is neglectable in most case.
Thanks, guys. I'm aware of above problems, and that's why I'd rather read some hardware counter instead of software "tick counting".

EDIT: Dear future reader, I've found those things myself, I "thank guys" for their time and effort, the community here is really helpful if you're not superlame :wink:
Last edited by invalid on Thu Apr 12, 2012 7:22 am, edited 1 time in total.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: High resolution timing

Post by gravaera »

rdos wrote:Although this thread contains some junk, it goes into these details quite thoroughly: http://forum.osdev.org/viewtopic.php?f=15&t=24550
I'm sensing some sort of subtext here ;)
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
rdos
Member
Member
Posts: 3396
Joined: Wed Oct 01, 2008 1:55 pm

Re: High resolution timing

Post by rdos »

Rudster816 wrote:http://en.wikipedia.org/wiki/High_Precision_Event_Timer

The HPET is at least 8 times more precise than the PIT. I think the most common HPET frequency is 15mhz though, which would make it ~12.5 times more precise.
The HPET is perfect. If it supports MSI interrupt delivery. Otherwise it can be a nightmare if its IRQ routings are not working properly.
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: High resolution timing

Post by Chandra »

rdos wrote:The HPET is perfect. If it supports MSI interrupt delivery. Otherwise it can be a nightmare if its IRQ routings are not working properly.
Indeed. But I guess it has rare problems of Interrupt Missing, though I'm not quite sure how occasional is that. Besides, it doesn't support one shot mode.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
invalid
Member
Member
Posts: 60
Joined: Thu Feb 23, 2012 8:39 am

Re: High resolution timing

Post by invalid »

Ok, so basically I need to keep the "software counter" (using whichever method is available), and periodically synchronize it with the "1-second resolution CMOS clock", right?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: High resolution timing

Post by bluemoon »

If you need clock, read the CMOS directly, or better, sync with time server :p
You may however "cache the readings" and keep it refreshed by timer tick within a small period

For long run, I still think timer are not suppose to act as clock, they are used to trigger deadlines.
rdos
Member
Member
Posts: 3396
Joined: Wed Oct 01, 2008 1:55 pm

Re: High resolution timing

Post by rdos »

ydoom wrote:Ok, so basically I need to keep the "software counter" (using whichever method is available), and periodically synchronize it with the "1-second resolution CMOS clock", right?
Basically, yes, except that I think you need to use a 2Hz synchronization interrupt from the CMOS, as 1Hz is not available (from my memory, so check it).
invalid
Member
Member
Posts: 60
Joined: Thu Feb 23, 2012 8:39 am

Re: High resolution timing

Post by invalid »

bluemoon wrote:If you need clock, read the CMOS directly, or better, sync with time server :p
I hoped that there's some hardware clock giving real world time reading including fractions of a second. Sure, this also needs to be synced with ntp, but you know - hardware counter feels more trustworthy than "increasing eax ocasionally by CPU" ;)

Thank you, everyone! To work, then... :)

PS: rdos, yeah I've noticed that they like to whip your @$$ whenever you post, but I haven't been around long enough to know why ;)
Post Reply