From: ···········@gmail.com
Subject: (sleep)ing for a very short time.
Date: 
Message-ID: <1117172440.033779.225960@o13g2000cwo.googlegroups.com>
Hi All! I'm hoping someone can help me out here.

I'm using Allegro CL and I want a process to sleep (with little to no
CPU usage) for a very brief period of time (say for 0.05 seconds)

Unfortunately this doesn't seem to work. For example:
> (time (dotimes (n 100)
        (sleep 0.1)))
; cpu time (non-gc) 10,094 msec user, 0 msec system
; cpu time (gc)     0 msec user, 0 msec system
; cpu time (total)  10,094 msec user, 0 msec system
; real time  10,093 msec
; space allocation:
;  4,975 cons cells, 0 symbols, 6,304 other bytes, 2672 static bytes
NIL
> (time (dotimes (n 100)
        (sleep 0.05)))
; cpu time (non-gc) 0 msec user, 0 msec system
; cpu time (gc)     0 msec user, 0 msec system
; cpu time (total)  0 msec user, 0 msec system
; real time  0 msec
; space allocation:
;  1,304 cons cells, 0 symbols, 3,200 other bytes, 0 static bytes
NIL
>

Note that in the first case, it's all good and the time taken is 10
seconds (what you'd expect for 100 sleeps of 0.1 seconds each) and CPU
usage stays near zero. But in the second case it takes no time. Even
worse, if you increase the number of iterations to say 100000, you find
that it's also sucking up 100% CPU time to do nothing.

I'm guessing I'm hitting some sort of granularity problem with the
clock that ACL is using. Does anyone know how I might solve this
problem with ACL?

Thanks!
Toron

From: Kenny Tilton
Subject: Re: (sleep)ing for a very short time.
Date: 
Message-ID: <NyAle.583$jU5.685817@twister.nyc.rr.com>
···········@gmail.com wrote:
> Hi All! I'm hoping someone can help me out here.
> 
> I'm using Allegro CL and I want a process to sleep (with little to no
> CPU usage) for a very brief period of time (say for 0.05 seconds)
> 
> Unfortunately this doesn't seem to work. For example:
> 
>>(time (dotimes (n 100)
> 
>         (sleep 0.1)))
> ; cpu time (non-gc) 10,094 msec user, 0 msec system
> ; cpu time (gc)     0 msec user, 0 msec system
> ; cpu time (total)  10,094 msec user, 0 msec system
> ; real time  10,093 msec
> ; space allocation:
> ;  4,975 cons cells, 0 symbols, 6,304 other bytes, 2672 static bytes
> NIL
> 
>>(time (dotimes (n 100)
> 
>         (sleep 0.05)))
> ; cpu time (non-gc) 0 msec user, 0 msec system
> ; cpu time (gc)     0 msec user, 0 msec system
> ; cpu time (total)  0 msec user, 0 msec system
> ; real time  0 msec
> ; space allocation:
> ;  1,304 cons cells, 0 symbols, 3,200 other bytes, 0 static bytes
> NIL
> 
> 
> Note that in the first case, it's all good and the time taken is 10
> seconds (what you'd expect for 100 sleeps of 0.1 seconds each) and CPU
> usage stays near zero.

No, you are misreading the TIME output. Look at what it says: "cpu 
time", "non-gc" and "gc". Then the total of those two. GC means time 
spent doing GC, non-GC is the time spent executing your code. Right, ACL 
burns CPU cycles when you sleep. Right again: I think any sleep under 
0.07 does not actually sleep. type "sleep" and then hit F1 to read the 
hyperspec. I think you'll see that not much is guaranteed by the sleep doc.

  But in the second case it takes no time. Even
> worse, if you increase the number of iterations to say 100000, you find
> that it's also sucking up 100% CPU time to do nothing.
> 
> I'm guessing I'm hitting some sort of granularity problem with the
> clock that ACL is using.

Good guess. Well, the spec says sleep is "affected by" the granularity 
of the scheduler.

  Does anyone know how I might solve this
> problem with ACL?

Why is it a problem? Why do you want to pause the app? When should the 
app resume? Just fishing...

kenny

-- 
Cells? : http://www.common-lisp.net/project/cells/
Cello? : http://www.common-lisp.net/project/cello/
Cells-Gtk? : http://www.common-lisp.net/project/cells-gtk/
Why Lisp? http://lisp.tech.coop/RtL%20Highlight%20Film

"Doctor, I wrestled with reality for forty years, and I am happy to 
state that I finally won out over it." -- Elwood P. Dowd
From: ···········@gmail.com
Subject: Re: (sleep)ing for a very short time.
Date: 
Message-ID: <1117252954.823243.172290@g49g2000cwa.googlegroups.com>
> No, you are misreading the TIME output. Look at what it says: "cpu
> time", "non-gc" and "gc". Then the total of those two. GC means time
> spent doing GC, non-GC is the time spent executing your code. Right, ACL
> burns CPU cycles when you sleep.

Actually, I was looking at "; real time  10,093 msec "
The "; cpu time (non-gc) 10,094 msec user, 0 msec system " is
incorrect, and an apparent problem in ACL 5.0.1. In later versions of
ACL (7.0 anyways), doing this same call will result in "; cpu time
(non-gc) 0 msec user, 0 msec system " but real time still shows around
10000msec.
When I say it takes no CPU time, I'm also looking at Windows' task
manager.
The second call, where i'm sleeping for 0.05 seconds, results in a CPU
spike to 100% until it's done.

> Why is it a problem? Why do you want to pause the app? When should the
> app resume? Just fishing...

Fishing's good. The reason it's a problem is two-fold. For one, I want
my program to continue to loop at roughly the same speed even as
computer power increases. The loop may execute the code faster (the
time is already negligable), but the overall program will still tick
along at the same pace.
For two, I don't want the CPU hitting 100% because I have a UI to draw,
and Windows doesn't seem to update the UI very well when Lisp is
continuously sucking up all the CPU time.

Also, to Espen:
I looked into lisp-sleep, but it's effectively the same as sleep, so I
suspect it has the same problem. And besides that, I have other
processes running that I didn't want to sleep. Drat. :/

I've been talking to others about it and I hear there is two timers in
Windows, one has a much finer granularity. So I'm guessing that the
sleep command is hooked into the less-fine timer and can only go as low
as about 0.07secs. My next thought is to try and hook into the windows
timer directly providing there is one that is finer granularity than
0.07secs.
Hrmmm.

Frustrating this one is...
Toron
From: Frank Buss
Subject: Re: (sleep)ing for a very short time.
Date: 
Message-ID: <tvkhwidxou3j.1hvh63uc259in$.dlg@40tude.net>
···········@gmail.com wrote:

> My next thought is to try and hook into the windows
> timer directly providing there is one that is finer granularity than
> 0.07secs.

perhaps you should ask this in a Microsoft newsgroup, if you want to call
some Windows functions, but looks like this could interesting:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_about_multimedia_timers.asp

Searching Google groups shows a message, where the timeGetDevCaps fails on
2 out of 1000 computers, if you request a resolution of 1 ms.

-- 
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: André Thieme
Subject: Re: (sleep)ing for a very short time.
Date: 
Message-ID: <d76o70$ju8$1@ulric.tng.de>
···········@gmail.com schrieb:
> Hi All! I'm hoping someone can help me out here.
> 
> I'm using Allegro CL and I want a process to sleep (with little to no
> CPU usage) for a very brief period of time (say for 0.05 seconds)
> 
> Unfortunately this doesn't seem to work. For example:
> 
>>(time (dotimes (n 100)
> 
>         (sleep 0.1)))
> ; cpu time (non-gc) 10,094 msec user, 0 msec system
> ; cpu time (gc)     0 msec user, 0 msec system
> ; cpu time (total)  10,094 msec user, 0 msec system
> ; real time  10,093 msec
> ; space allocation:
> ;  4,975 cons cells, 0 symbols, 6,304 other bytes, 2672 static bytes
> NIL
 >
> Note that in the first case, it's all good and the time taken is 10
> seconds (what you'd expect for 100 sleeps of 0.1 seconds each) and CPU
> usage stays near zero.

This should run ten seconds, like you said (and on clisp it does for me)
but in the output I do read "msec" in this output, which sounds for me
like "milliseconds"...


Andr�
-- 
From: Edi Weitz
Subject: Re: (sleep)ing for a very short time.
Date: 
Message-ID: <usm092eui.fsf@agharta.de>
On Fri, 27 May 2005 11:12:26 +0200, Andr� Thieme <······························@justmail.de> wrote:

> This should run ten seconds, like you said (and on clisp it does for
> me) but in the output I do read "msec" in this output, which sounds
> for me like "milliseconds"...

But 10,093 msec /are/ ten seconds, aren't they?

Edi.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Espen Vestre
Subject: Re: (sleep)ing for a very short time.
Date: 
Message-ID: <kw7jhlov7f.fsf@merced.netfonds.no>
Edi Weitz <········@agharta.de> writes:

>> This should run ten seconds, like you said (and on clisp it does for
>> me) but in the output I do read "msec" in this output, which sounds
>> for me like "milliseconds"...
>
> But 10,093 msec /are/ ten seconds, aren't they?

That depends on your number formatting conventions...

I experiented a little with LispWorks (4.4.5, mac) and got the right
results when the sleep time was >= 10 milliseconds.  

ACL has a function lisp-sleep that makes the whole thing sleep (while
sleep is equivalent to process-sleep), maybe that function - if it's
feasible to use it - allows for finer granularity than ACLs
process-sleep?
-- 
  (espen)
From: André Thieme
Subject: Re: (sleep)ing for a very short time.
Date: 
Message-ID: <d76qt4$m1l$1@ulric.tng.de>
Edi Weitz schrieb:
> On Fri, 27 May 2005 11:12:26 +0200, Andr� Thieme <······························@justmail.de> wrote:
> 
> 
>>This should run ten seconds, like you said (and on clisp it does for
>>me) but in the output I do read "msec" in this output, which sounds
>>for me like "milliseconds"...
> 
> 
> But 10,093 msec /are/ ten seconds, aren't they?

Yes, you are right. I was thinking confused by the comma, forgot that in
some countries the meaning of "," and "." which I am used to are
interchanged.

Sorry for the confusion.


Andr�
--