Page 1 of 1

High resolution timing

Posted: Mon Apr 09, 2012 10:36 am
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 :)

Re: High resolution timing

Posted: Mon Apr 09, 2012 10:56 am
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)

Re: High resolution timing

Posted: Mon Apr 09, 2012 11:42 am
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.

Re: High resolution timing

Posted: Mon Apr 09, 2012 12:10 pm
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

Re: High resolution timing

Posted: Mon Apr 09, 2012 12:17 pm
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:

Re: High resolution timing

Posted: Mon Apr 09, 2012 12:23 pm
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 ;)

Re: High resolution timing

Posted: Mon Apr 09, 2012 12:41 pm
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.

Re: High resolution timing

Posted: Tue Apr 10, 2012 2:57 am
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.

Re: High resolution timing

Posted: Tue Apr 10, 2012 8:33 am
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?

Re: High resolution timing

Posted: Tue Apr 10, 2012 8:36 am
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.

Re: High resolution timing

Posted: Tue Apr 10, 2012 8:45 am
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).

Re: High resolution timing

Posted: Tue Apr 10, 2012 9:21 am
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 ;)