[Vm-dev] Re: [Pharo-dev] what is exactly flushCache?

Eliot Miranda eliot.miranda at gmail.com
Wed Nov 25 17:25:01 UTC 2015


Hi Stef,

> On Nov 24, 2015, at 12:00 PM, stepharo <stepharo at free.fr> wrote:
> 
> Thanks for this great explanation and yes I know about this cache but I do not why
> I was wondering that we did not need to update it from the image. 
> Now I would like to add such comment inside the system because I like self explanation. 
> 
> "See Object documentation whatIsAPrimitive." gets frustrates me. 

It's still appropriate to explain to newbies what primitives are and how they are invoked.  Interesting to consider what the minimum knowledge programmers have when first using the system.  Do they know, for example, that methods contain bytecode for a stack machine, or that underneath the system is a virtual machine containing a bytecode execution engine, a set of primitives and (a) garbage collector(s)?

> So I imagine that is Behavior flushCache flushing for the class in the class,selector pair.

Again because of inheritance the VM can't just flush entries for a specific class, so the VM responds to Behavior>>flushCache flushes the entire cache.

> What would be a good place to add such comment? MethodDictionary. It sounds reasonable to me but tell me.

If the comments in the various flushCache implementations are correct and reference each other then I think that's enough.

> TBehavior >> flushCache
>     "Tell the interpreter to remove the contents of its method lookup cache, if it has 
>     one.  Essential.  See Object documentation whatIsAPrimitive."
> 
>     <primitive: 89>
>     self primitiveFailed

I would rewrite this to say

"Tell the virtual machine to remove the contents of its method lookup caches, if it has any.  This must be done when the system modifies the class hierarchy so that message lookups reflect the revised organization.  c.f. Symbol>>flushCache & CompiledMethod>>flushCache.  Essential.  See Object documentation whatIsAPrimitive."

> Symbol >> flushCache
>     "Tell the interpreter to remove all entries with this symbol as a selector from its method lookup cache, if it has one.  This primitive must be called whenever a method is redefined or removed.
>     NOTE:  Only one of the two selective flush methods (Symbol or CompiledMethod) needs to be used."
> 
>     <primitive: 119>

I would rewrite this to say

"Tell the virtual machine to remove all entries with this symbol as a selector from its method lookup caches, if it has any.  This must be done whenever a method is added, redefined or removed, so that message lookups reflect the revised organization.  c.f. Behavior>>flushCache & CompiledMethod>>flushCache.  Essential.  See Object documentation whatIsAPrimitive."


> CompiledMethod >> flushCache
>     "Tell the interpreter to remove all references to this method from its method lookup cache, if it has one. This primitive must be called whenever a method is redefined or removed.
>     NOTE:  Only one of two selective flush methods (Symbol or CompiledMethod) needs to be used."
> 
>     <primitive: 116>

I would rewrite this to say

"Tell the virtual machine to remove all references to this method from its method lookup caches, and to discard any optimized version of the method, if it has any of these.  This must be done whenever a method is modified in place, such as modifying its literals or machine code, to reflect the revised code.  c.f. Behavior>>flushCache & Symbol>>flushCache.  Essential.  See Object documentation whatIsAPrimitive." 


> Le 24/11/15 19:10, Eliot Miranda a écrit :
>> Hi Stephane,
>> 
>>     the VM caches method lookups in a lookup cache from class,selector pairs to method,primitive pairs, where primitive may be null.  This is called the first-level method lookup cache.  The JIT VM caches message lookups in machine code, so that a particular piece of machine code exists in a state that invokes a method for a specific class very fast by embedding the class reference in a register load and the target method in a call instruction, and having the target method verify this "cache probe" (this is an "in-line cache).  The JIT also caches the translation of a byte coded method to machine code, by hiding a reference to a machine code method in a byte coded method.
>> 
>> These caches can be invalidated in several circumstances:
>> 
>> 1a. if one adds or removes a method from a class's method dictionary it may change the correct results of a lookup not merely of the class whose dictionary was updated but also subclasses of the class.
>> 1b. if one replaces a method in a method dictionary this changes the target method for a lookup of the selector for the class and subclasses
>> 
>> 2. if one wants to rewrite the byte code or literals of a method, for example because a Slot definition has changed, then if the method has been compiled to machine code, the machine code must be discarded before the new code may be executed
>> 
>> 1a & 1b are done via Symbol>>flushCache.  In response the normal VM flushes its first-level method lookup cache, and the JIT also scans all of machine code looking for inline caches with that selector, and voiding them, reverting each send site for that selector to the "unlinked" state.
>> 
>> There used to be confusion in Squeak, which Pharo inherited, that using CompiledMethod>>flushCache was somehow the right way to void caches when updating method dictionaries, flushing the old method in the dictionary, if any, and the new method.  It isn't, precisely because adding or removing methods affects the visibility of inherited methods with the same selector.  So MethodDictionary code should use Symbol>>flushCache, and only once, on each update of a method dictionary.  As a result, the VM will ensure that the necessary send caches are flushed for that           selector.
>> 
>> 2. is done via CompiledMethod>>flushCache.  In response the VM searches the first-level method lookup cache and removes all entries whose target is the method.  In addition the JIT discards the machine code for the method, and searches for all send sites with that method's machine code as the target and voids them, reverting them to the unlinked state.
>> 
>> The VM must be told to flush the cached state for a compiled method via CompiledMethod>>flushCache and will /try/ and void the state for that method.  But it can't always deal with existing activations of that method, because if there are activations running the machine code, that machine code can't merely be thrown away, and can't be replaced because its length may change, depending on literals or byte codes.  So this kind of byte coded method manipulation needs to be done with case and some understanding of the total system state.
>> 
>> HTH
>> 
>> 
>>> On Tue, Nov 24, 2015 at 9:37 AM, stepharo <stepharo at free.fr> wrote:
>>> Clement
>>> 
>>> do you know what is flushCache and why we need to invoke it?
>>> 
>>> Stef
>> 
>> _,,,^..^,,,_
>> best, Eliot

_,,,^..^,,,_ (phone)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/vm-dev/attachments/20151125/364171f4/attachment-0001.htm


More information about the Vm-dev mailing list