Best way of having synchronised periodic interrupts in SMP

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
limp
Member
Member
Posts: 90
Joined: Fri Jun 12, 2009 7:18 am

Best way of having synchronised periodic interrupts in SMP

Post by limp »

Hi all,

I want to have periodic triggering of a timer interrupt in all the CPU cores (including BSP), i.e. what I want is the interrupt to occur at the same time interval in all CPU cores.

The way I see it, there are two ways for achieving this:

1) Try to start all processors' LAPIC timers (which will be already configured for periodic interrupt execution) at the same time. I know that by doing this the best I can achieve is to have the LAPIC timers in-phase (as all the LAPIC timers are driven by the same source)) but not in-sync. Another negative is that each LAPIC timer tends to behave (drift) differently so I don't know how big drifting problems would I have.

2) Configure a timer (e.g. HPET) for periodic interrupts and set the I/O APIC to broadcast the interrupt to all CPUs (including self). I don't know the delivery delay that I am going to have in this case, but I guess I will have more jitter than in the 1st method.

Which one of the above do you guys think is the best way of doing this? Do you suggest a better way than the ones mentioned?

Thanks in advance for you time.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Best way of having synchronised periodic interrupts in S

Post by Combuster »

Do you suggest a better way than the ones mentioned?
Not performing such synchronization at all. If you truly need synchronisation, the delay you get is the longest time spent in a core to answer the interrupt plus the overhead of actually checking in each core. That means that if you get twice as many cores, you spend more than twice as many time on the overhead. A good design will only do system-wide core synchronisation a fixed number of times between boots: my OS only needs two of such synchronisation points in its lifetime.


That said, the number of clocks needed to communicate an interrupt across the apic bus, and the number of clocks in difference between apic timers are started not much different, and both are small compared to actually invoking the interrupt handler and any other code operating under an interrupts-disabled lock. An interrupt broadcast is therefore not observably inferior to lockstepped timers. A broadcast however has the advantage of lacking the need for any cross-core synchronisation, making it an order of magnitude simpler, and it uses only one time source rather than all but a few sources. The per-cpuness of the local apic timer has much more practical uses.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Best way of having synchronised periodic interrupts in S

Post by Brendan »

Hi,

I'm relatively intelligent. For example, I'm a lot smarter than a dog.

I don't think I'm more attractive than a cat; but maybe I'm just crazy.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Best way of having synchronised periodic interrupts in S

Post by bluemoon »

I agree except for the cat thing :shock:
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: Best way of having synchronised periodic interrupts in S

Post by Brynet-Inc »

I've had my suspicions about Brendan before, confirmed. He's crazy.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Best way of having synchronised periodic interrupts in S

Post by Brendan »

Hi,
bluemoon wrote:
Brendan wrote:I'm relatively intelligent. For example, I'm a lot smarter than a dog.

I don't think I'm more attractive than a cat; but maybe I'm just crazy.
I agree except for the cat thing :shock:
Brynet-Inc wrote:I've had my suspicions about Brendan before, confirmed. He's crazy.
Thanks for your support guys - it means a lot to me.

However I probably should stop posting off-topic stuff; and say something about when things appear to have occurred being far more important than when things actually occur..

Imagine if I go to the local pet shop and buy 2 cats (from the pet shop owner, a mysterious Mr Schrodinger). I put each cat into a separate box, and connect each cat to a device that kills the cat as soon as a (cheap, inaccurate) timer expires. I set both of the timers to "about 10 minutes" and seal the boxes. After a few hours of torturing puppies in another room I come back and I open both of the boxes and find 2 dead cats. As far as I know, the cats died at exactly the same time - how could I possibly know they didn't?

As long as things appear to have occurred at the same time to an observer, whether or not they actually did occur at the same time is irrelevant.

What you want is to have timer interrupts that appear to occur at the same time as far as other software can tell. So, how can other software possibly know that the timer IRQs on different CPUs didn't occur at exactly the same time?


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Best way of having synchronised periodic interrupts in S

Post by Owen »

And this thread has become shaped like itself.

Well played, sir, well played.
Post Reply