Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

Eventloop very efficient?

已查看 183 次
跳至第一个未读帖子

Cecil Westerhof

未读,
2018年1月1日 17:28:062018/1/1
收件人
I had a program running under systemd which saved every minute some
system info and every hour some other info. I did this in a while
loop, where I used an after to get to the start of the next minute.
And in this while loop I checked if it was the beginning of the hour
and saved the extra information.

I really did not like this, because you need the logic in your loop
then. With two pieces of data, this is not really a problem, but if it
increases it can become messy. So I rewrote it using an eventloop.

The funny thing is: it looks like it uses about half the CPU time as
the previous one. Is this a known result, or is it just accidental?


I wrote two simple scripts to test this. And these seem to give an
even bigger difference, but I want to run them a bit longer before I
give it any significance.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Arjen Markus

未读,
2018年1月2日 04:34:572018/1/2
收件人
Can you show us these scripts? That would help understanding the issue and giving you informed advice.

Regards,

Arjen

Cecil Westerhof

未读,
2018年1月2日 05:14:062018/1/2
收件人
I have written a script that notifies me. Originally the main was:
while {True} {
waitMinutes ${sleepMins}
if {([clock format [clock seconds] -format %H] eq "00") && ${signalHour}} {
giveSignal ${hourDuration}
} else {
giveSignal ${normalDuration}
}
}

For the eventloop version I changed this to:
after [getWaitMinutesTicks ${sleepMins}] giveSignalLoop
vwait forever

And the proc is:
proc giveSignalLoop {} {
if {([clock format [clock seconds] -format %H] eq "00") && ${::signalHour}} {
giveSignal ${::hourDuration}
} else {
giveSignal ${::normalDuration}
}
after [getWaitMinutesTicks ${::sleepMins}] giveSignalLoop
}

The proc waitMinutes does an after until there are a multiply of the
minutes given. (In my case it is 30, so the signal is given at 00 and
30.

The proc getWaitMinuteTicks returns the ticks that waitMinutes would
wait.

Both are now running for 44 hours. The first one has used 9 seconds
CPU time and the second is still standing on zero. So that is a very
significant difference.


The procs used:
# Usage: waitMinutes INTERVAL
# Waits until minutes is a multiply of INTERVAL
# 60 % INTERVAL should be 0
proc waitMinutes {interval} {
if {[expr {(60 % ${interval}) != 0}]} {
error "[getProcName]: 60 is not a multiply of ${interval}"
}
after [getWaitMinutesTicks ${interval}]
}

# Usage: waitSeconds INTERVAL
# Waits until seconds is a multiply of INTERVAL
# 3600 % INTERVAL should be 0
proc waitSeconds {interval} {
if {[expr {(3600 % ${interval}) != 0}]} {
error "[getProcName]: 3600 is not a multiply of ${interval}"
}
after [getWaitSecondsTicks ${interval}]
}

And in the second case:
# Usage: getWaitMinutes INTERVAL
# Get ticks to wait until minutes is a multiply of INTERVAL
# 60 % INTERVAL should be 0
proc getWaitMinutesTicks {interval} {
if {[expr {(60 % ${interval}) != 0}]} {
error "[getProcName]: 60 is not a multiply of ${interval}"
}
getWaitSecondsTicks [expr {${interval} * 60}]
}

# Usage: getWaitSeconds INTERVAL
# Get ticks to wait until seconds is a multiply of INTERVAL
# 3600 % INTERVAL should be 0
proc getWaitSecondsTicks {interval} {
if {[expr {(3600 % ${interval}) != 0}]} {
error "[getProcName]: 3600 is not a multiply of ${interval}"
}
set secondsInHour [expr {[clock seconds] % 3600}]
set secondsToWait [expr {${interval} - (${secondsInHour} % ${interval})}]
expr {1000 * ${secondsToWait}}

Cecil Westerhof

未读,
2018年1月3日 09:28:072018/1/3
收件人
They have been running for three days now. The one with the while loop
has used 15 seconds of CPU time, while the eventloop version still
stands on zero. So it looks like that a program that is mostly waiting
surely wins by using an eventloop.

Cecil Westerhof

未读,
2018年1月31日 05:44:062018/1/31
收件人
More info. The program that uses the while loop uses about 5 seconds a
day. With the event loop it took 18 days before it took a second. So
you can say it uses about 1% of the while loop.

It is of-course a bit contrived, but combined with that it makes
things simpler and more logical it is a good idea to use an event loop
when possible.

Ralf Fassel

未读,
2018年1月31日 08:03:282018/1/31
收件人
* Cecil Westerhof <Ce...@decebal.nl>
| [after MS ; cmd ] vs [after MS cmd]
| >> Both are now running for 44 hours. The first one has used 9 seconds
| >> CPU time and the second is still standing on zero. So that is a very
| >> significant difference.
| >
| > They have been running for three days now. The one with the while loop
| > has used 15 seconds of CPU time, while the eventloop version still
| > stands on zero. So it looks like that a program that is mostly waiting
| > surely wins by using an eventloop.
>
| More info. The program that uses the while loop uses about 5 seconds a
| day. With the event loop it took 18 days before it took a second. So
| you can say it uses about 1% of the while loop.

Looking at the source code (generic/tclTimer.c, static int
AfterDelay()), it seems that [after ms] splits up the interval into
500ms chunks and does some some checks in between. This obviously
uses more CPU than the event loop waiting for events...

R'

Cecil Westerhof

未读,
2018年1月31日 09:14:062018/1/31
收件人
Are they necessary, or could it be optimised?

Ralf Fassel

未读,
2018年2月1日 05:12:182018/2/1
收件人
* Cecil Westerhof <Ce...@decebal.nl>
| Ralf Fassel <ral...@gmx.de> writes:
| > Looking at the source code (generic/tclTimer.c, static int
| > AfterDelay()), it seems that [after ms] splits up the interval into
| > 500ms chunks and does some some checks in between. This obviously
| > uses more CPU than the event loop waiting for events...
>
| Are they necessary, or could it be optimised?

The comments seem to indicate it is necessary...

* AfterDelay --
*
* Implements the blocking delay behaviour of [after $time]. Tricky
* because it has to take into account any time limit that has been set.

Haven't checked the details...

R'

Andreas Kupries

未读,
2018年3月28日 01:38:072018/3/28
收件人
Cecil Westerhof <Ce...@decebal.nl> writes:

> I have written a script that notifies me. Originally the main was:
> while {True} {
> waitMinutes ${sleepMins}
> if {([clock format [clock seconds] -format %H] eq "00") && ${signalHour}} {

Feels a bit complicated.

if {$signalHour && (([clock seconds] % 3600) == 0)} { ...

Placing $signalHour as the first term short circuits calculation of
the second term when $signalHour is false. The second term computes
hourness directly from the seconds.


> if {[expr {(60 % ${interval}) != 0}]} {

The 1st argument to [if] is an expression.
No need to nest an [expr] into it.

if {(60 % ${interval}) != 0} {

--
See you,
Andreas Kupries <akup...@shaw.ca>
<http://core.tcl.tk/akupries/>
Developer @ SUSE (MicroFocus Canada LLC)
<andreas...@suse.com>

EuroTcl 2018, Jul 7-8, Munich/DE, http://eurotcl.eu/
Tcl'2018, Oct 15-19, Houston, TX, USA. https://www.tcl.tk/community/tcl2018/
-------------------------------------------------------------------------------
0 个新帖子