<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Ok, since this is vaguely related...has there been any thought to trying to take advantage of GCD/libdispatch?<div class=""><br class=""></div><div class="">These days when writing iOS apps I am very careful to do the bare minimum of work on the main thread (event receipt/ui manipulation) and as much as possible on other queues.</div><div class=""><br class=""></div><div class="">I myself do not fully understand the way the VM works but my naive guess is it just does everything on the main thread.  Please correct me if that is wrong.</div><div class=""> <br class=""><div><blockquote type="cite" class=""><div class="">On Sep 5, 2017, at 8:45 PM, John McIntosh <<a href="mailto:johnmci@smalltalkconsulting.com" class="">johnmci@smalltalkconsulting.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><span style="font-size:12.800000190734863px" class="">self relinquishProcessorForMicrosec</span><wbr style="font-size:12.800000190734863px" class=""><span style="font-size:12.800000190734863px" class="">onds: 1000</span><br class=""><div class=""><span style="font-size:12.800000190734863px" class=""><br class=""></span></div>In ioRelinquishProcessorForMicroseconds it would call getNextWakeupUsecs() to get the time when the next Process had to wake up. This always would be the Morphic event loop. In Cuis Smalltalk this could be as much as 20 seconds btw. <br class=""><br class="">I noticed someone changed it to limit to the microseconds given, shrug. The version I have for testing on iOS 64 devices is like so. <div class=""><br class=""><div class=""><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">sqInt</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">ioRelinquishProcessorForMicroseconds(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">sqInt</span><span style="font-variant-ligatures:no-common-ligatures" class=""> microSeconds)</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">{</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">long</span><span style="font-variant-ligatures:no-common-ligatures" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">     </span>realTimeToWait;</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">   </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">extern</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(120,73,42)" class="">usqLong</span><span style="font-variant-ligatures:no-common-ligatures" class=""> getNextWakeupUsecs();</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">       </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(120,73,42)" class="">usqLong</span><span style="font-variant-ligatures:no-common-ligatures" class=""> nextWakeupUsecs = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)" class="">getNextWakeupUsecs</span><span style="font-variant-ligatures:no-common-ligatures" class="">();</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">      </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(120,73,42)" class="">usqLong</span><span style="font-variant-ligatures:no-common-ligatures" class=""> utcNow = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(120,73,42)" class="">get64</span><span style="font-variant-ligatures:no-common-ligatures" class="">(utcMicrosecondClock);</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; min-height: 15px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">if</span><span style="font-variant-ligatures:no-common-ligatures" class=""> (nextWakeupUsecs <= utcNow) {</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">               </span></span><span style="font-variant-ligatures:no-common-ligatures" class="">/* if nextWakeupUsecs is non-zero the next wakeup time has already</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">                </span> * passed and we should not wait.</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">          </span> */</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">if</span><span style="font-variant-ligatures:no-common-ligatures" class=""> (nextWakeupUsecs != </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">0</span><span style="font-variant-ligatures:no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">                   </span></span><span style="font-variant-ligatures:no-common-ligatures" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">0</span><span style="font-variant-ligatures: no-common-ligatures;" class="">;</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">            </span>realTimeToWait = microSeconds;</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">    }</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">else</span><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">        realTimeToWait = nextWakeupUsecs - utcNow;</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">               </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">if</span><span style="font-variant-ligatures:no-common-ligatures" class=""> (realTimeToWait < microSeconds)  // so wait 1000 microseconds or longer not just 1000 microseconds </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">                       </span>realTimeToWait = microSeconds;</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">    </span>}</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; min-height: 15px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">      </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)" class="">aioSleepForUsecs</span><span style="font-variant-ligatures:no-common-ligatures" class="">(realTimeToWait);</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; min-height: 15px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">    </span></span><span style="font-variant-ligatures:no-common-ligatures" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">0</span><span style="font-variant-ligatures: no-common-ligatures;" class="">;</span></div><p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">
























</span></p><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div>In talking to Eliot a few months back I also changed aioSleepForUsecs and just do the aioPoll as we thought the poll should complete if there is any form of interrupt pending from the UI.</div><div class=""><br class=""><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162);" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">long</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">aioSleepForUsecs(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">long</span><span style="font-variant-ligatures:no-common-ligatures" class=""> microSeconds)</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">{</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(120, 73, 42);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="gmail-Apple-tab-span" style="white-space:pre">       </span></span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)" class="">aioPoll</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(microSeconds);</span><br class=""></div><p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">


















</span></p><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div>With those changes a pharo image runs at about 1.6% <br class=""><br class=""><br class="">I'm sure I changed the Mac VM to signal the event semaphore on pending events about 15 years ago. <br class=""><br class=""><br class="">BTW also lost over the years was a check for the interrupt keyboard sequence that would fire the interrupt semaphore. That code I think got moved into the image event processing logic. Perhaps not workable if that logic is busy processing bad (or too much) data. </div></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, Aug 29, 2017 at 2:29 PM, Holger Freyther <span dir="ltr" class=""><<a href="mailto:holger@freyther.de" target="_blank" class="">holger@freyther.de</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br class="">
Hi,<br class="">
<br class="">
I have done some early prototype for the Unix VM end of 2015(?) and I have improved and repeated these for MacOS now and the thread based heartbeat (now that it is the universal default). I won't make it to ESUG this year but this might be something to play with?<br class="">
<br class="">
The motivations is simple: Polling increases the CPU usage which will reduce your battery life, takes away resources from other processes (e.g. more Pharo images) or these days increases your cloud computing bill. On top of that it might increase network latency (time from socket becoming readable to the time the semaphore is signaled).<br class="">
<br class="">
To complete the work we have work inside the Image and the VM and some of it is on the way and others might need more discussion.<br class="">
<br class="">
<br class="">
The idle process:<br class="">
<br class="">
ProcessorScheduler>># idleProcess<br class="">
        "A default background process which is invisible."<br class="">
<br class="">
        [true] whileTrue:<br class="">
                [self relinquishProcessorForMicrosec<wbr class="">onds: 1000]<br class="">
<br class="">
Let's please yield the CPU for more than a 1ms. Unless I am missing something an expired Delay or network IO would make us wake up earlier anyway?<br class="">
<br class="">
<br class="">
The delay scheduler:<br class="">
<br class="">
The VM supports that when the next wake-up time is set to 0, the VM can sleep indefinitely. There is a pending patch to sleep "0" in our Delay scheduler. Currently we force a wake-up earlier than that. I think we should trust the VM to do wake us up even if it is a second away.<br class="">
<br class="">
<br class="">
Morphic UI:<br class="">
<br class="">
I don't understand the WorldState>>#interCyclePause but then I never looked at Morphic. Do we really need to poll like that? Under which circumstances does the world update? We get an event (where we have the event semaphore), we get some I/O (where we have a semaphore) or we have a timeout (where we sleep on a semaphore). Did anyone ever look at removing the tick?<br class="">
<br class="">
<br class="">
VM I/O:<br class="">
<br class="">
Currently we receive a SIGIO but from what I can see (and I still need to write a benchmark) the processing might be delayed 20ms? My hack removes the usage of nextPollUsecs and instead checks a variable that is set by the SIGIO handler. Besides missing memory barriers this should work(tm).<br class="">
<br class="">
The biggest issue seems that for macos/ios the input is driven by polling. E.g. some wheel events seem to require to pump the event queue. Is this something we could trigger from the image in the future? I had hoped to get a fd to a machport we could get SIGIO for.. but that doesn't seem to exist. I have hacked out the honoring of the relinquish delay, added the polling into a iOS specific routine and thanks to the Morphic Delay we bump the event loop frequently enough.<br class="">
<br class="">
<br class="">
VM heartbeat thread:<br class="">
<br class="">
The process keeps ticking even if the VM doesn't run. E.g. sleeps and waits for an event. There is a cost in deciding when to halt the thread so there must be a cut-off for which delays we bother to disable the heartbeat thread. I think the current code would allow the hearbeat to drift so the new code might just make it a bit worse.<br class="">
<br class="">
<br class="">
Where are we now?<br class="">
<br class="">
I have pushed my changes to <a href="https://github.com/zecke/opensmalltalk-vm/tree/mac-use-less-cpu" rel="noreferrer" target="_blank" class="">https://github.com/zecke/<wbr class="">opensmalltalk-vm/tree/mac-use-<wbr class="">less-cpu</a> and would be happy to have people look at it, look at the memory synchronization, maybe run to see if they notice extra delays or such?<br class="">
<br class="">
<br class="">
I started the same image with the plain-vm and my hacked one and let it run for about 20min. The output is coming from top.<br class="">
<br class="">
COMMAND        %CPU                  TIME<br class="">
Pharo          4.3                   00:48.49<br class="">
Pharo          0.8                   00:10.20<br class="">
<br class="">
<br class="">
Looking for comments and feedback.<br class="">
<br class="">
regards<br class="">
<span class="HOEnZb"><font color="#888888" class="">        holger</font></span></blockquote></div><br class=""><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr" class=""><div class=""><div dir="ltr" class="">===========================================================================<br class="">John M. McIntosh. Corporate Smalltalk Consulting Ltd <a href="https://www.linkedin.com/in/smalltalk" target="_blank" class="">https://www.linkedin.com/in/smalltalk</a><br class="">===========================================================================<br class=""></div></div></div></div>
</div>
</div></blockquote></div><br class=""></div></body></html>