hotspot & hotswap, who and who are best freinds

Download Hotspot & hotswap, who and who are best freinds

Post on 22-Apr-2015




0 download

Embed Size (px)




  • 1. hotspot & hotswapwho and who are best friends
  • 2. why we need hotswap

  • 3. why we need hotswap?
  • 4. JVM 1.2 hotswap
    JVMTM Tool Interface
    typedefstruct {
    const unsigned char* class_bytes;
    } jvmtiClassDefinition;
    RedefineClasses(jvmtiEnv* env,
    const jvmtiClassDefinition* class_definitions)
  • 5. JVM 1.2 hotswap
    JavaTM Virtual Machine Debug Interface Reference
    typedefstruct {
    jclassclazz; /* Class to be redefined */
    jintclass_byte_count; /* number of bytes defining class (below) */
    jbyte *class_bytes; /* bytes defining class */
    /* (in Class File Format of JVM spec) */
    } JVMDI_class_definition;

    RedefineClasses(jintclassCount, JVMDI_class_definition *classDefs)
  • 6. Java dev
  • 7. Sounds good,what's the problem?
    Unfortunately, this redefinition is limited only to changing method bodies it cannot either add methods or fields or otherwise change anything else, except for the method bodies.
  • 8. So, Here we go!
    Discarding classloaders
    Hacking changes into the JVM itself feels a fragile approach
  • 9. Discarding classloaders
  • 10. Discarding classloaders can help, but
    Not particularly granular
    Instance state is lost
    Can run into memory problems
    Demo(discarding.classloaders.hotswap) by
  • 11. java.lang.instrument
    aJRubyobject is not much more than a Map from method names to their implementations and from field names to their values.
    byte[] transform(ClassLoader loader,
    String className,
    Class classBeingRedefined,
    byte[] classfileBuffer)
    throws IllegalClassFormatException
  • 12. java.lang.instrument
    java.lang.instrument.ClassFileTransformer. This class can do two things:
    replace method bodies of existing classes (but nothing else, it is essentially the same as hotswap).
    modify classes before they get loaded into the java virtual machine
    class Class1
    {protected static int field;
  • 13. java.lang.instrument
    class Class1{ protected static int field; public static Object __REDEFINED_STATIC_METHOD_(intmethodNo, Object[] parameters) { if(methodNo == 0) { return field; } if(methodNo == 1) { Integer p0 = (Integer)parameters[0]; int unboxedP0 = p0.intValue(); field = unboxedP0; return null; } return null; }}
  • 14. java.lang.instrument
    public class Proxy1{ public void setField(intparam) { Object[] params = new Object[1]; params[0] = new Integer(param); Class1.__REDEFINED_STATIC_METHOD_(1,params); }}
  • 15. Sounds well,but
    Perfomance. subject to indirection & Pem memory
    Java SDK classes.The native code
    Compatibility. A.class.getMethods()
  • 16. fakereplace
    Google Code project
    Usual structure changes supported
    Methods, fields, constructors
    Reflection instrumentation
    Integrates support directly for some frameworks
  • 17. Fakereplace demo
  • 18. Jrebelnot free
    one master class + several anonymous
    Solve problem
    direction and lightweight
    Avoids instrumenting the Java
    Tweaks the results of the Reflection API, so that we can correctly include the added/removed members in these results
  • 19. Jrebel Demo
  • 20. Sounds perfect
  • 21. nation team
  • 22. hotspot and hotswap
    Arbitrary changes possible (including changes to subtype relationships)
    No performance penalty before or after the change
    No introduced indirections
    Continued execution of old active methods
    Only simple, comprehensible strategies for field matching or method transitions
    Change possible at any point when the VM is suspended
    Graceful handling of accesses to deleted fields or calls of deleted methods
  • 23. HotswapStatus
  • 24. Transformer Methods
    class A
    int x;
    static void $staticTransformer() //class transformer
    System.out.println("Class A has a new version");
    void $transformer() { doubleX = 2 * x; } //instances
  • 25. Hotswap.patch demo
  • 26. Hotswap.patchAlgorithm
    • Loading the New Classes
    • 27. Updating the Data Structures and Pointers