<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 11, 2017 at 4:54 AM, Eliot Miranda <span dir="ltr"><<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <br><div dir="ltr">Hi Denis,<div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 10, 2017 at 12:08 PM, Denis Kudriashov <span dir="ltr"><<a href="mailto:dionisiydk@gmail.com" target="_blank">dionisiydk@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <br><div dir="ltr">Hi.<div><br></div><div>Will event-driven VM fix this problem completely? Or heartbeat will be needed anyway?</div></div></blockquote><div><br></div><div>As I understand it, the heartbeat us always needed.  In an event-driven VM it may be that the Smalltalk executive is called form the event loop, but the Smalltalk executive still has to break out of executing Smalltalk to return to the event loop to receive new events.</div><div><br></div><div>The thing to understand about the JIT VM is that Smalltalk is executing in machine code just like simple code in some low level language.  That code can respond to Smalltalk events such as attempting to wait on a Semaphore with no outstanding signals, which may cause it to switch processes.  But it cannot respond to external asynchronous events unless it is informed of those events.  And it is very difficult to construct a VM that can accept interrupts at arbitrary times that activate Smalltalk code (imagine receiving an interrupt in the middle of a GC, or mid-way through looking up a send not found in the cache, etc, etc; essentially interrupts can only be accepted at limited times).  So the VM needs to check for events (and indeed interrupts) at safe points.  The efficient implementation of safe points is checking on the next frame-building send.  But if every frame-building send checked for interrupts and/or events, frame build would be very slow and the entire VM would crawl.  The function of the heart beat is to cause frame building sends to check for interrupts and/or events at regular, but (relative to frame-builling send frequency) infrequent occasions.</div><div><br></div><div>Compare that to interrupts in a real processor.  The processor /also/ only tests e.g. the interrupt request pin at a safe point (perhaps at the end of each interaction decode cycle), and also provides means to disable interrupts for critical sections of code.  It's just that the quanta are much smaller than in a Smalltalk vm.</div><div><br></div><div>HTH</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="gmail_extra"><div class="gmail_quote">2017-01-06 20:23 GMT+01:00 Eliot Miranda <span dir="ltr"><<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <br><div dir="ltr">Hi Fabio, Hi Guille,<div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 6, 2017 at 9:44 AM, Fabio Niephaus <span dir="ltr"><<a href="mailto:lists@fniephaus.com" target="_blank">lists@fniephaus.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <br><div dir="ltr"><div class="gmail_quote"><div class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969GmSign">On Fri, Jan 6, 2017 at 6:33 PM Eliot Miranda <<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
Hi Guille,<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
> On Jan 6, 2017, at 6:44 AM, Guillermo Polito <<a href="mailto:guillermopolito@gmail.com" class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg" target="_blank">guillermopolito@gmail.com</a>> wrote:<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
><br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
> Hi,<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
><br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
> I was checking the code in sqUnixHeartbeat.c to see how the heartbeat thread/itimer worked. It somehow bothers me that there are different compiled artifacts, one per option.<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
><br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
> What do you think about having a VM that manages that as an argument provided when we launch the VM? This would add some flexibility that we don't have right now because we make the decision at compile time.<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
I think it's a fine idea but it isn't really the issue.  The issue is that the itimer mechanism is problematic, especially for foreign code, and is therefore a stop gap.  The itimer interrupts long-running system calls, which means that things like sound libraries break (at Qwaq I had to fix ALSA to get it to work with the itimer heartbeat).  Since Pharo is becoming more reliant on external code it may impact us more going forward.<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
The real issue is that linux's requirement that thread priorities be set in per-application file in /etc/security/limits.d (IIRC) is a big.  Neither Windows nor Mac OS X requires such nonsense, and a threaded heartbeat is used on those systems without any issue at all.  Why linux erected this mess in the first place is something I don't understand.<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
I had to implement the itimer heartbeat to get Qwaq forums running on Linux running pre 2.6 kernels, but had many other problems to solve as a result (ALSA, database connects).<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg">
Were it that the vm merely had to detect whether it could use the threaded heartbeat then things would be easy.  Instead one can only use the thing if one has superuser permissions to install a file in /etc, just to use a thread of higher priority than the main one.<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg"></blockquote><div><br></div><div>Thanks for the explanation, Eliot. I had no idea how bad the issues are with the itimer, but I'm glad you also see the user-facing issue with the heartbeat.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">An alternative might be to lower the priority of the main thread.  Then the file installation would be unnecessary.<br class="gmail-m_-2455799944368928351m_5056426692792882515m_8857803480512424406gmail-m_3886557463388821969gmail_msg"></blockquote><div><br></div><div>Could you elaborate a little bit more on this idea? How could this impact the vm? What could be the drawbacks here?</div></div></div></blockquote><div><br></div><div>First of all, for the heartbeat thread to work reliably it must run at higher priority than the thread running Smalltalk code.  This is because its job is to cause Smalltalk code to break out at regular intervals to check for events.  If the Smalltalk code is compute-intensive then it will prevent the heartbeat thread from running unless the heartbeat thread is running at a higher priority, and so it will be impossible to receive input keys, etc. (Note that if event collection was in a separate thread it would suffer the same issue; compute intensive code would block the event collection thread unless it was running at higher priority).</div><div><br></div><div>Right now, Linux restricts creating threads with priority higher than the default to those programs that have a /etc/security/limits.d/progr<wbr>am.conf file that specifies the highest priority thread the program can create.  And prior to the 2.6.12 kernel only superuser processes could create higher-priority threads.  I do know that prior to 2.6.12 one couldn't create threads of *lower* priority than the default either (I would have used this if I could).</div><div><br></div><div>If 2.6.12 allows a program to create threads with lower priorities *without* needing a /etc/security/limits.d/program<wbr>.conf, or more conveniently to allow a thread's priority to be lowered, then the idea is:</div><div>1. at start-up create a heartbeat thread at the normal priority</div><div>2. lower the priority of the main VM thread below the heartbeat thread.</div><div>Alternatively, one could spawn a new lower-priority thread to run Smalltalk code, but this may be be much more work.</div><div><br></div><div>The draw-back is that running Smalltalk in a thread whose priority is lower than the default *might* impact performance with lots of other processes running.  This depends on whether the scheduler conflates thread priorities with process priorities (which was the default with old linux threads, which were akin to processes).</div><div><br></div><div>Sop there are some tests to perform:</div><div><br></div><div>a) see if one can lower the priority of a thread without having a /etc/security/limits.d/program<wbr>.conf in place</div><div>b) write a simple performance test (nfib?) in a program that can be run either with its thread having normal or lower priority, and run two instances of the program at the same time and see if they take significantly different times to compute their result</div><div><br></div><div>If a) is possible and b) shows no significant difference in the wall-times of the two programs then we can modify the linux heartbeat code to *lower* the priority of the main Smalltalk thread if it finds it can't create a heartbeat thread with higher priority.</div><div><br></div></div></div></div></blockquote></div></div></blockquote></div></div></div></blockquote><div><br></div><div>With recent Pharo discussion on heatbeat  itimer versus threaded </div><div>   <a href="http://forum.world.st/Esteban-s-ChangeLog-week-of-6-March-2017-td4938349.html">http://forum.world.st/Esteban-s-ChangeLog-week-of-6-March-2017-td4938349.html</a> </div><div>I got curious....</div><div><br></div><div>$ vi test.c</div><div><div>#include <stdio.h></div><div>#include <sys/time.h></div><div>#include <sys/resource.h></div><div>#include <stdlib.h></div><div>#include <time.h></div><div><br></div><div>void fib(int n, int pid)</div><div>{</div><div>   int first = 0, second = 1, next, c;</div><div>   for ( c = 0 ; c < n ; c++ )</div><div>   {</div><div>      if ( c <= 1 )</div><div>         next = c;</div><div>      else</div><div>      {</div><div>         next = first + second;</div><div>         first = second;</div><div>         second = next;</div><div>      }</div><div>      //printf("%d - fib=%d\n", pid, next);</div><div>   }</div><div>}</div><div><br></div><div><br></div><div>struct timespec timediff(struct timespec start, struct timespec end)</div><div>{</div><div>        struct timespec temp;</div><div>        temp.tv_sec = end.tv_sec-start.tv_sec;</div><div>        temp.tv_nsec = end.tv_nsec-start.tv_nsec;</div><div>        if (temp.tv_nsec < 0)</div><div>        {       temp.tv_nsec += 1000000000;</div><div>                temp.tv_sec -= 1;</div><div>        }</div><div>        return temp;</div><div>}</div></div><div><br></div><div><div>int main(int argc, char *argv[])</div><div>{</div><div>        int which = PRIO_PROCESS;</div><div>        id_t pid;</div><div>        int priority, fibN, ret;</div><div>        struct timespec start, stop, diff;</div><div><br></div><div>        if(argc<2)</div><div>        {       printf("Usage: %s ProcessPriority FibN\n", argv[0]);</div><div>                exit(1);</div><div>        }</div><div>        priority = atoi(argv[1]);</div><div>        fibN = atoi(argv[2]);</div><div><br></div><div>        pid = getpid();</div><div>        printf("%d\t ==> %d original priority\n", pid, getpriority(which, pid));</div><div><br></div><div>        setpriority(which, pid, priority);</div><div>        priority = getpriority(which, pid);</div><div>        printf("%d\t ==> %d new priority\n", pid, priority);</div><div><br></div><div>        clock_gettime( CLOCK_REALTIME, &start);</div><div>        sleep(1); // allow all threads to be scheduled</div><div>        fib(fibN, pid);</div><div>        clock_gettime( CLOCK_REALTIME, &stop);</div><div>        diff = timediff(start, stop);</div><div>        printf("\n%d @ %d\t ==> execution time %d:%d\n", pid, priority, diff.tv_sec - 1, diff.tv_nsec);</div><div>}</div></div><div><br></div><div>///////////////////////</div><div><br></div><div>$ gcc test.c</div><div><br></div><div>$ uname -a</div><div><div>Linux dom0 3.16.0-4-686-pae #1 SMP Debian 3.16.7-ckt25-2 (2016-04-08) i686 GNU/Linux</div></div><div><br></div><div>$ nproc --all</div><div>4</div><div><br></div><div>$ N=1000000000 ; for NPROC in 1 ; do (./a.out 19 $N &) && (./a.out 1 $N &) && (./a.out 0 $N &) ; done</div><div><div>28175 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre">    </span> ==> execution time 5:137392274</div><div>28171 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">   </span> ==> execution time 5:493271222</div><div>28173 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre">    </span> ==> execution time 5:678498982</div></div><div><br></div><div>...for NPROC in 1 2 ; do...</div><div><div>28339 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre">  </span> ==> execution time 5:891516242</div><div>28333 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre">    </span> ==> execution time 6:101871486</div><div>28331 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre">    </span> ==> execution time 6:197583303</div><div>28337 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre">    </span> ==> execution time 6:473926938</div><div>28335 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">   </span> ==> execution time 11:19093473</div><div>28329 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">   </span> ==> execution time 11:109494611</div></div><div><br></div><div>...for NPROC in 1 2 3 ; do...<br></div><div><div>28370 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre">     </span> ==> execution time 8:286661748</div><div>28364 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre">    </span> ==> execution time 8:346971535</div><div>28376 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre">    </span> ==> execution time 8:919760746</div><div>28362 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre">    </span> ==> execution time 9:943310436</div><div>28368 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre">    </span> ==> execution time 10:43977329</div><div>28374 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre">    </span> ==> execution time 10:251189507</div><div>28372 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">  </span> ==> execution time 14:807238482</div><div>28360 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">  </span> ==> execution time 15:48684466</div><div>28366 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">   </span> ==> execution time 15:392610447</div></div><div><br></div><div>...for NPROC in 1 2 3 4 ; do...<br></div><div><div>28401 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre">   </span> ==> execution time 10:808863373</div><div>28407 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre">   </span> ==> execution time 11:144571568<br></div><div>28395 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre"> </span> ==> execution time 11:311897577<br></div><div>28389 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre"> </span> ==> execution time 12:49899167<br></div><div>28399 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre">  </span> ==> execution time 12:391939682<br></div><div>28387 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre"> </span> ==> execution time 12:922309497<br></div><div>28393 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre"> </span> ==> execution time 12:997908723<br></div><div>28405 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre"> </span> ==> execution time 13:116623935<br></div><div>28385 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">        </span> ==> execution time 18:710195627<br></div><div>28391 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">        </span> ==> execution time 19:160867082</div><div>28397 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">  </span> ==> execution time 19:306339215</div><div>28403 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">  </span> ==> execution time 19:340283641</div></div><div><br></div><div>...for NPROC in 1 2 3 4 5; do...<br></div><div><div>28431 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre">  </span> ==> execution time 13:512767819</div><div>28437 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre">   </span> ==> execution time 13:682814129<br></div><div>28449 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre"> </span> ==> execution time 13:695500843<br></div><div>28443 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre"> </span> ==> execution time 14:212788768<br></div><div>28425 @ 0<span class="gmail-Apple-tab-span" style="white-space:pre"> </span> ==> execution time 14:384973010<br></div><div>28435 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre"> </span> ==> execution time 15:356897222<br></div><div>28423 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre"> </span> ==> execution time 15:638388578<br></div><div>28441 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre"> </span> ==> execution time 15:692913828<br></div><div>28429 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre"> </span> ==> execution time 15:822047741<br></div><div>28447 @ 1<span class="gmail-Apple-tab-span" style="white-space:pre"> </span> ==> execution time 15:845915440<br></div><div>28445 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">        </span> ==> execution time 23:221173838<br></div><div>28433 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">        </span> ==> execution time 23:349862424<br></div><div>28421 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">        </span> ==> execution time 23:388930822<br></div><div>28439 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">        </span> ==> execution time 23:374234661<br></div><div>28427 @ 19<span class="gmail-Apple-tab-span" style="white-space:pre">        </span> ==> execution time 23:517771887<br></div></div><div><br></div><div>cheers -ben</div></div><br></div></div>