<br><br><div class="gmail_quote">On Wed, Nov 11, 2009 at 10:20 AM, Igor Stasenko <span dir="ltr">&lt;<a href="mailto:siguctua@gmail.com">siguctua@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5"><br>
2009/11/11 Eliot Miranda &lt;<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>&gt;:<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; On Tue, Nov 10, 2009 at 9:59 PM, Igor Stasenko &lt;<a href="mailto:siguctua@gmail.com">siguctua@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; 2009/11/11 Eliot Miranda &lt;<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>&gt;:<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; On Tue, Nov 10, 2009 at 6:45 PM, John M McIntosh &lt;<a href="mailto:johnmci@smalltalkconsulting.com">johnmci@smalltalkconsulting.com</a>&gt; wrote:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; On 2009-11-10, at 6:17 PM, Eliot Miranda wrote:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; With the threaded Squeak VM I&#39;m working on one can go one better and have a number of image-level processes that block in the FFI and a number of worker threads in the VM that block on OS semaphores waiting for the VM to give them something to do.<br>

&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Obviously now you have to give a bit more details on this. Is it like the hydra VM? Or entirely different?<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Orthogonal, in that it might work well with Hydra.  The basic scheme is to have a natively multi-threaded VM that is not concurrent.  Multiple native threads share the Vm such that there is only one thread running Vm code at any one time.  This the VM can make non-blocking calls to the outside world but neither the VM nor the image need to be modified to handle true concurrency.  This is the same basic architecture as in the Strongtalk and V8 VMs and notably in David Simmons&#39; various Smalltalk VMs.<br>

&gt;&gt; &gt; The cool thing about the system is David&#39;s design.  He&#39;s been extremely generous in explaining to me his scheme, which is extremely efficient.  I&#39;ve merely implemented this scheme in the context of the Cog VM.  The idea is to arrange that a threaded callout is so cheap that any and all callouts can be threaded.  This is done by arranging that a callout does not switch to another thread, instead the thread merely &quot;disowns&quot; the VM.  It is the job of a background heartbeat thread to detect tat a callout is long-runnijng and that the VM has effectively blocked.  The heartbeat then activates a new thread to run the VM and the new thread attempts to take ownership and will run Smalltalk code if it succeeds.<br>

&gt;&gt; &gt; On return form a callout a thread must attempt to take ownership of the VM, and if it fails, add itself to a queue of threads waiting to take back the VM and then wait on an OS semaphore until the thread owning the VM decides to give up ownership to it.<br>

&gt;&gt; &gt; Every VM thread has a unique index.  The vmOwner variable holds the index of the owning thread or 0 if the VM is unowned.  To disown the VM all a thread has to do is zero vmOwner, while remembering the value of vmOwner in a temporary.  To take ownership a thread must use a low-level lock to gain exclusive access to vmOwner, and if vmOwner is zero, set it back to the thread&#39;s index, and release the lock.  If it finds vmOwner is non-zero it releases the lock and enters the wanting ownership queue.<br>

&gt;&gt; &gt; In the Cog VM the heartbeat beats at 1KHz, so any call that takes less than 0.5ms is likely to complete without the heartbeat detecting that the VM is blocked.  So any and all callouts can be threaded.  Quite brilliant.  All the work of changing the active process when switching between threads is deferred from callout time to when a different thread takes ownership of the VM, saving the VM state for the process that surrendered the VM and installing its own.<br>

&gt;&gt; &gt; The major wrinkle in this is that in David&#39;s VM he has a pinning garbage collector which arranges that any arguments passed out through the FFI are implicitly pinned.  We don&#39;t yet have a pinning garbage collector.  I do plan to do one.  But in the interim one quick hack, a neat idea of Andreas&#39;, is to fail calls that attempt to pass objects in new space, allowing only old objects to be passed, and to prevent the full garbage collector from running while any threaded calls are in progress.<br>

&gt;&gt; &gt; Having cheap non-blocking calls allows e.g.<br>
&gt;&gt; &gt; - the Hydra inter-VM channels to be implemented in Smalltalk code above the threaded FFI<br>
&gt;&gt; &gt; - socket calls to be blocking calls in the image<br>
&gt;&gt; &gt; - Smalltalk code to call select/poll/WaitForMultipleEvents<br>
&gt;&gt; &gt; There are still plenty of sticky issues to do with e.g. identifying threads that can do specific functions, such as the UI thread, and issuing OpenGL calls from the right thread, etc, etc.  But these are all doable, if potentially tricky to get right.  If this kind of code does migrate from the VM innards up to the image I think that&#39;s a really good thing (tm) but one will really have to know what one is doing to get it right.<br>

&gt;&gt; &gt; HTH<br>
&gt;&gt; &gt; eliot<br>
&gt;&gt;<br>
&gt;&gt; I used a mutex in Hydra (each interpreter has own mutex), so any<br>
&gt;&gt; operation, which requires synchronization should be performed<br>
&gt;&gt; only after obtaining the mutex ownership.<br>
&gt;&gt; And sure, if crafted carefully, one could release the mutex before<br>
&gt;&gt; doing an external call, and &quot;try&quot; get it back again after call<br>
&gt;&gt; completed.<br>
&gt;&gt; If use mutexes, provided by OS, then you don&#39;t need a heartbeat<br>
&gt;&gt; process, obviously because you can simply put wait on mutex. So, i<br>
&gt;&gt; suppose you introducing the heardbeat to minimize the overhead of<br>
&gt;&gt; using synchronization primitives provided by OS, and instead using a<br>
&gt;&gt; low-level assembly code.<br>
&gt;&gt;<br>
&gt;&gt; Just one minor thing - you mentioned the table of threads. What if<br>
&gt;&gt; some routine creating a new thread, which get unnoticed by VM, so its<br>
&gt;&gt; not registered in the VM &#39;threads&#39; table,  but then such thread<br>
&gt;&gt; attempts to obtain an ownership on interpreter somehow?<br>
&gt;<br>
&gt; This can only happen on a callback or other well-defined entry-point.  At these well-defined entry-points the VM checks whether there is a tag in thread-local storage (the thread&#39;s VM index).  If it is not set the VM allocates the necessary per-thread storage, assigns an index and allows the thread to continue.  On return from the entry-point the VM deallocates the storage, clears the thread-local storage and returns.<br>

&gt;<br>
<br>
</div></div>Yes. Just to make sure everything is ok with that :)<br>
<div class="im"><br>
&gt;&gt;<br>
&gt;&gt; About inter-image communication in Hydra. The main problem that you<br>
&gt;&gt; need to pass a buffer between heads, so you need to get a lock on a<br>
&gt;&gt; recepient, while still keeping a lock on sender interpreter. But this<br>
&gt;&gt; could lead to deadlock, if recepient in own turn attempts to do the<br>
&gt;&gt; same.<br>
&gt;&gt; So, the solution, unfortunately, is to copy buffer to C heap (using<br>
&gt;&gt; malloc().. yeah :( ), and pass an event with pointer to such buffer,<br>
&gt;&gt; which then could be handled by recepient as soon as it ready to do so,<br>
&gt;&gt; in event handling routine.<br>
&gt;<br>
&gt; But you could connect the two with a pair of pipes, right?  Then al that locking and buffer allocation is in the VM.  Or rather, once you have a non-blocking FFI you can just use an OS&#39;s native stream-based inter-process communications facilities.<br>

&gt;<br>
<br>
</div>of course i could. but the task is to minimize the overhead, possibly<br>
even without buffer copy overhead (that where pinning GC would be<br>
really helpfull). i don&#39;t think that OS facilities not copying data<br>
buffer to secure location before passing it between the sockets.<br>
Because once it releases the sender, while still waiting receiver to<br>
be ready to retrieve the data, it can&#39;t guarantee that given buffer<br>
will not be used for something else, hence it inevitable should either<br>
copy buffer contents to secure location or block the sender.<br></blockquote><div><br></div><div>OK, instead one can create a buffer from within Smalltalk (e.g. via Alien) and then create OS semaphores and use blocking calls to wait on those semaphores.  All I&#39;m really trying to say is that once you have a threaded FFI you can move lots of stuff up out of the VM.  The disadvantage is that one loses platform independence, but I&#39;ve long thought that Smalltalk class hierarchies are a much nicer way of creating cross-platform abstractions than great gobs iof platform-specific C code.</div>
<div><br></div><div>Andreas counters that implementing the abstractions in the VM keeps them well-defined and free from meddling.  But that runs counter to the philosophy of an open system and preventing inadvertent meddling is something Smalltalk has to do anyway  (e.g. &quot;Process should not be redefined, proceed to store over it&quot;).  The nice things about shooting oneself in the foot by meddling with a Smalltalk system are that a) it doesn&#39;t really do any harm and b) the debugging of it can be a great learning experience.</div>
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div class="h5">&gt;&gt;<br>
&gt;&gt; One more thing:<br>
&gt;&gt;  socket calls to be blocking calls in the image<br>
&gt;&gt;<br>
&gt;&gt; Assuming that VM use blocking sockets, then call will block the thread<br>
&gt;&gt; &amp; some of the image-side process.<br>
&gt;&gt; Then hearbeat thread at some point sees that VM has no owning thread<br>
&gt;&gt; and so, allows another thread, waiting in the queue to take ownership<br>
&gt;&gt; on VM.<br>
&gt;&gt; But what if there is no such thread? There is a choice: allocate new<br>
&gt;&gt; native thread and let it continue running VM, or just ignore &amp;  skip<br>
&gt;&gt; over for the next heat beat.<br>
&gt;&gt; I&#39;d like to hear what you choose. Because depending from direction<br>
&gt;&gt; taken, on server image, which simultaneously serves, say 100<br>
&gt;&gt; connections you may end up either with 100 + 1 native threads, or less<br>
&gt;&gt; (fixed) number of them but with risk to unable to run any VM code<br>
&gt;&gt; until some of the blocking calls completes.<br>
&gt;<br>
&gt;  There is a simple policy that is a cap on the total number of threads the VM will allocate.  below this a new thread is allocated.  At the limit the VM will block.  But note that the pool starts at 1 and only grows as necessary up to the cap.<br>

&gt;&gt;<br>
&gt;&gt; I&#39;d like to note that either of above alternatives having a quite bad<br>
&gt;&gt; scalability potential.<br>
&gt;&gt; I&#39;d prefer to have a pool of threads, each of them serving N<br>
&gt;&gt; connections. The size of threads pool should be 2x-3x number of<br>
&gt;&gt; processor cores on host, because making more than that will not make<br>
&gt;&gt; any real difference, since single core can serve only single native<br>
&gt;&gt; thread while others will just consume the memory resources, like<br>
&gt;&gt; address space etc.<br>
&gt;<br>
&gt; That&#39;s very similar to my numbers too.  My current default is at least two threads and no more than 32, and 2 x num processors/cores in between.  But these numbers should be configurable.  This is just to get started.<br>

<br>
</div></div>Yes, but blocking sockets won&#39;t allow you to distribute load evenly<br>
when number of threads less than number of active sockets. All active<br>
connections should be distributed evenly among worker threads, that<br>
will guarantee that you consuming computing resources optimally.<br></blockquote><div><br></div><div>So in that case one needs more threads, and to support them one needs more memory.  But it shouldn&#39;t be a surprise that one needs more resources to support a higher workload. Yer takes yer choice and yer pays yer price.  And of course one can providing settings to control the per-thread stack space etc.</div>
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">And what about scheduling? Have you considered my idea to move<br>
scheduling to language side, while on VM side, leave<br>
very small portion (in amount of code &amp; logic) for switching the<br>
active processes?<br>
I think that with introduction of JIT the overhead of language-side<br>
sheduling will be quite small and quite acceptable given that it<br>
allows us to change things whenever we want, without touching VM.<br></blockquote><div><br></div><div>No I haven&#39;t considered this because at least in my implementation, the scheduler and the thread manager are intimately connected.  Once a process is in a callout on a particular thread it is bound to that thread and if it calls back it&#39;ll run on that thread and only on that thread until the callout unwinds.  Further, one will be able to bind processes to specific threads to ensure that certain activities happen from a given native thread (e.g. OpenGL and the Windows debugging API both require that calls are issued from a single thread).  So the thread manager and scheduler cooperate to bind processes to threads an to arrange that the right thread switches occur when process switches require them.  Figuring out how to do that with a Smalltalk-level scheduler is more than I can manage right now :)</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><br>
&gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; --<br>
&gt;&gt; &gt;&gt; ===========================================================================<br>
&gt;&gt; &gt;&gt; John M. McIntosh &lt;<a href="mailto:johnmci@smalltalkconsulting.com">johnmci@smalltalkconsulting.com</a>&gt;   Twitter:  squeaker68882<br>
&gt;&gt; &gt;&gt; Corporate Smalltalk Consulting Ltd.  <a href="http://www.smalltalkconsulting.com" target="_blank">http://www.smalltalkconsulting.com</a><br>
&gt;&gt; &gt;&gt; ===========================================================================<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; --<br>
&gt;&gt; Best regards,<br>
&gt;&gt; Igor Stasenko AKA sig.<br>
&gt;<br>
&gt;<br>
&gt;<br>
<br>
<br>
<br>
</div>--<br>
<div><div></div><div class="h5">Best regards,<br>
Igor Stasenko AKA sig.<br>
</div></div></blockquote></div><br>