Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - A timer with pulse (continued): (8 Items)
   
A timer with pulse (continued)  
By Brian Stecher:
> We only expire a maximum of 50 timers on each clock tick (to minimize
> latency), so we should still have plenty of critical heap.

So, this bears an interesting question. Consider you have more than 50 timers scheduled for the same expiration time and
 you expire only first 50 and leave few more for the next tick. Now, before the next tick I call TimerSetTime() for one 
of those few delayed timers. What do I get in oitime parameter? Arithmetically I should get zero (or even a negative 
value) but, accordingly to documentation, zero is an indicator that the timer has already expired and a signal or pulse 
has already been queued for the recipient.  This, obviously is not the case and the event will not be queued at all 
because I change timer expiration time with my current call. Obviously it will lead to malfunction in the program logic.

Any comments on this situation?
Re: A timer with pulse (continued)  
By Brian Stecher:
> We only expire a maximum of 50 timers on each clock tick (to minimize
> latency), so we should still have plenty of critical heap.

Another funny thing is that it's enough to write a simple program that will schedule 50 timers periodically for each 
tick with event handlers just doing nothing and this will break down the system completely as noone else will have their
 timer working. :)
Re: A timer with pulse (continued)  
On Tue, Apr 21, 2009 at 10:02:19AM -0400, Oleh Derevenko wrote:
> By Brian Stecher:
> > We only expire a maximum of 50 timers on each clock tick (to minimize
> > latency), so we should still have plenty of critical heap.
> 
> Another funny thing is that it's enough to write a simple program that will schedule 50 timers periodically for each 
tick with event handlers just doing nothing and this will break down the system completely as noone else will have their
 timer working. :)

Actually no. After the timers fire, they'll be placed back on the active
list *after* the timers that got skipped the first time since they now
have a later firing time. The next clock tick will process the delayed
timers before getting back to your malicious 50.

-- 
Brian Stecher (bstecher@qnx.com)        QNX Software Systems
phone: +1 (613) 591-0931 (voice)        175 Terence Matthews Cr.
       +1 (613) 591-3579 (fax)          Kanata, Ontario, Canada K2M 1W8
Re: A timer with pulse (continued)  
If the timeout is pending at timer_settime then the processing of the event delivery is completed before
the settime is performed.

Also, 50 is just the limit of timers to be delivered in the clock interrupt - further timers are processed by
adding an interrupt event such that any left-over timer firings are handled as the kernel exits, but before
user threads run.

Oleh Derevenko wrote:
> By Brian Stecher:
>> We only expire a maximum of 50 timers on each clock tick (to minimize
>> latency), so we should still have plenty of critical heap.
> 
> So, this bears an interesting question. Consider you have more than 50 timers scheduled for the same expiration time 
and you expire only first 50 and leave few more for the next tick. Now, before the next tick I call TimerSetTime() for 
one of those few delayed timers. What do I get in oitime parameter? Arithmetically I should get zero (or even a negative
 value) but, accordingly to documentation, zero is an indicator that the timer has already expired and a signal or pulse
 has already been queued for the recipient.  This, obviously is not the case and the event will not be queued at all 
because I change timer expiration time with my current call. Obviously it will lead to malfunction in the program logic.

> Any comments on this situation?
> 
> _______________________________________________
> OSTech
> http://community.qnx.com/sf/go/post27609
> 

-- 
cburgess@qnx.com
Re: A timer with pulse (continued)  
Aha! That returns us to previous topic and indicates that the statement "...so we should still have plenty of critical 
heap" is incorrect at all. There can be lots of processes and they can scheduled timers in a manner that critical heap 
ends even though each of them is handling events of its own and does not do anything illegal.

> If the timeout is pending at timer_settime then the processing of the event 
> delivery is completed before
> the settime is performed.
> 
> Also, 50 is just the limit of timers to be delivered in the clock interrupt - 
> further timers are processed by
> adding an interrupt event such that any left-over timer firings are handled as
>  the kernel exits, but before
> user threads run.
> 
> Oleh Derevenko wrote:
> > By Brian Stecher:
> >> We only expire a maximum of 50 timers on each clock tick (to minimize
> >> latency), so we should still have plenty of critical heap.
> > 
> > So, this bears an interesting question. Consider you have more than 50 
> timers scheduled for the same expiration time and you expire only first 50 and
>  leave few more for the next tick. Now, before the next tick I call 
> TimerSetTime() for one of those few delayed timers. What do I get in oitime 
> parameter? Arithmetically I should get zero (or even a negative value) but, 
> accordingly to documentation, zero is an indicator that the timer has already 
> expired and a signal or pulse has already been queued for the recipient.  This
> , obviously is not the case and the event will not be queued at all because I 
> change timer expiration time with my current call. Obviously it will lead to 
> malfunction in the program logic.
> > Any comments on this situation?
Re: A timer with pulse (continued)  
I spoke too soon, timer_pending() does not 'fire' the timers, rather it handles processing
timers which have 'fired', but couldn't be delivered due to the kernel manipulating the
timer lists at the time that the clock tick went off.

If the timer was still 'active' (ie expired but not 'fired') at the time of the timer_settime, then
the oitime.nsec is set to 1

Oleh Derevenko wrote:
> Aha! That returns us to previous topic and indicates that the statement "...so we should still have plenty of critical
 heap" is incorrect at all. There can be lots of processes and they can scheduled timers in a manner that critical heap 
ends even though each of them is handling events of its own and does not do anything illegal.
> 
>> If the timeout is pending at timer_settime then the processing of the event 
>> delivery is completed before
>> the settime is performed.
>>
>> Also, 50 is just the limit of timers to be delivered in the clock interrupt - 
>> further timers are processed by
>> adding an interrupt event such that any left-over timer firings are handled as
>>  the kernel exits, but before
>> user threads run.
>>
>> Oleh Derevenko wrote:
>>> By Brian Stecher:
>>>> We only expire a maximum of 50 timers on each clock tick (to minimize
>>>> latency), so we should still have plenty of critical heap.
>>> So, this bears an interesting question. Consider you have more than 50 
>> timers scheduled for the same expiration time and you expire only first 50 and
>>  leave few more for the next tick. Now, before the next tick I call 
>> TimerSetTime() for one of those few delayed timers. What do I get in oitime 
>> parameter? Arithmetically I should get zero (or even a negative value) but, 
>> accordingly to documentation, zero is an indicator that the timer has already 
>> expired and a signal or pulse has already been queued for the recipient.  This
>> , obviously is not the case and the event will not be queued at all because I 
>> change timer expiration time with my current call. Obviously it will lead to 
>> malfunction in the program logic.
>>> Any comments on this situation?
> 
> 
> _______________________________________________
> OSTech
> http://community.qnx.com/sf/go/post27621
> 

-- 
cburgess@qnx.com
Re: A timer with pulse (continued)  
OK. This time you made it out. :)
But still, the limitation of 50 is quite an artificial. I would have chosen 64 instead.

Just kidding. :) With growth of CPU performance such things should be made customizable or at least, automatically 
adjustable just like default clock period.

> I spoke too soon, timer_pending() does not 'fire' the timers, rather it 
> handles processing
> timers which have 'fired', but couldn't be delivered due to the kernel 
> manipulating the
> timer lists at the time that the clock tick went off.
> 
> If the timer was still 'active' (ie expired but not 'fired') at the time of 
> the timer_settime, then
> the oitime.nsec is set to 1
> 
Re: A timer with pulse (continued)  
On Tue, Apr 21, 2009 at 10:38:16AM -0400, Colin Burgess wrote:
> I spoke too soon, timer_pending() does not 'fire' the timers, rather it handles processing
> timers which have 'fired', but couldn't be delivered due to the kernel manipulating the
> timer lists at the time that the clock tick went off.

Actually, the event does get delivered right away - what timer_pending()
does is take the timer off the active list and, in the case of periodic
timer, puts it back on in the proper position for the next firing.

-- 
Brian Stecher (bstecher@qnx.com)        QNX Software Systems
phone: +1 (613) 591-0931 (voice)        175 Terence Matthews Cr.
       +1 (613) 591-3579 (fax)          Kanata, Ontario, Canada K2M 1W8