Hotspot & hotswap, who and who are best freinds
Upcoming SlideShare
Loading in...5
×
 

Hotspot & hotswap, who and who are best freinds

on

  • 5,848 views

 

Statistics

Views

Total Views
5,848
Views on SlideShare
5,846
Embed Views
2

Actions

Likes
15
Downloads
175
Comments
3

2 Embeds 2

http://twitter.com 1
http://www.techgig.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Hotspot & hotswap, who and who are best freinds Hotspot & hotswap, who and who are best freinds Presentation Transcript

  • hotspot & hotswapwho and who are best friends
    xuanxi@taobao.com
  • why we need hotswap?
    主管: 你在偷懒?
    汇编开发人员:我在编译。
    C++开发人员: 我在编译打包部署。
    java 开发人员: 我在打包部署。
    Jsp开发人员:我在部署。
    Python开发人员:………
    Ruby开发人员:………
  • why we need hotswap?
    线上紧急修改。(方法体的修改、增加和减少方法,字段,类,接口,更改类继承拓扑结构)
    View slide
  • JVM 1.2 以后支持了hotswap功能
    JVMTM Tool Interface
    typedefstruct {
    jclassklass;
    jintclass_byte_count;
    const unsigned char* class_bytes;
    } jvmtiClassDefinition;
    jvmtiError
    RedefineClasses(jvmtiEnv* env,
    jintclass_count,
    const jvmtiClassDefinition* class_definitions)
    View slide
  • 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;
     
    jvmdiError
    RedefineClasses(jintclassCount, JVMDI_class_definition *classDefs)
  • Java dev除打包部署外还干嘛?
    用IDE(eclipse)调试
  • 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.
  • So, Here we go!
    Discarding classloaders
    java.lang.instrument
    Hacking changes into the JVM itself feels a fragile approach
  • Discarding classloaders
  • Discarding classloaders can help, but
    Not particularly granular
    Instance state is lost
    Can run into memory problems
    Demo(discarding.classloaders.hotswap) by ken.wug@gmail.com
  • java.lang.instrument
     a JRuby object 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,
    ProtectionDomainprotectionDomain,
    byte[] classfileBuffer)
    throws IllegalClassFormatException
  • 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;
    }
  • 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;  }}
  • 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);  }}
  • Sounds well,but…
    Perfomance. subject to indirection & Pem memory
    Java SDK classes. The native code
    Compatibility.  A.class.getMethods()
  • fakereplace
    Google Code project
    Usual structure changes supported
    Methods, fields, constructors
    Annotations
    Reflection instrumentation
    Integrates support directly for some frameworks
  • Fakereplace demo
  • Jrebel(not 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
  • Jrebel Demo
  • Sounds perfect
  • 国家队( nation team )
    Hotswap.patch
  • 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
  • HotswapStatus
  • Transformer Methods
    class A
    {
    int x;
    intdoubleX;
    static void $staticTransformer() //class transformer
    {
    System.out.println("Class A has a new version");
    }
    void $transformer() { doubleX = 2 * x; } //instance’s
    }
  • Hotswap.patch demo
  • Hotswap.patchAlgorithm
    • Loading the New Classes
    • Updating the Data Structures and Pointers
  • Loading the New Classes
    step1:Find all affected classes.
    (VM_RedefineClasses::FindAffectedKlassesClosure)
    Redefining D and B
    Find all affected classes
  • Loading the New Classes
    step2:Sort the classes topologically.
    (VM_RedefineClasses::TopologicalClassSorting)
    Topological order.
  • Updating the Data Structures and Pointers
    step1:Flush dependent code .
    Currently all compiled methods are deoptimized
    (Classes::flush_dependent_code)
  • Updating the Data Structures and Pointers
    step2:Update constant pool cache entries.
    (VM_RedefineClasses::adjust_cpool_cache)
  • Updating the Data Structures and Pointers
    step3: Swap pointers.
    利用gc的特性 ,gc 复制一个对象的后 会调整指向该对象的pointer
    (MarkSweep::adjust_pointer)
  • Updating the Data Structures and Pointers
    step4: Update instance fields.
    all new fields are initialized to zero.(新建)
    instanceKlass::do_fields_evolution(标记)
    MarkSweep::update_fields(拷贝)
  • Fantastic,but…
  • Hotswap@taobao.com
    • HSF_JETTY plugin自动集成hotswap.patch。
    • 你也可以自己打patch。
  • Question time
  • Party time谢谢!xuanxi@taobao.com