hotspot & hotswapwho and who are best friends<br />xuanxi@taobao.com<br />
why we need hotswap?<br />主管: 你在偷懒?<br />汇编开发人员:我在编译。<br />C++开发人员: 我在编译打包部署。<br />java 开发人员: 我在打包部署。<br />Jsp开发人员:我在部署。<b...
why we need hotswap?<br />线上紧急修改。(方法体的修改、增加和减少方法,字段,类,接口,更改类继承拓扑结构)<br />
JVM 1.2 以后支持了hotswap功能<br />JVMTM Tool Interface<br />typedefstruct {<br />jclassklass;<br />jintclass_byte_count;<br />  ...
JVM 1.2 以后支持了hotswap功能<br />JavaTM Virtual Machine Debug Interface Reference<br />typedefstruct {<br />jclassclazz;       ...
Java dev除打包部署外还干嘛?<br />用IDE(eclipse)调试<br />
Sounds good,what's the problem?<br />Unfortunately, this redefinition is limited only to changing method bodies — it canno...
So, Here we go!<br />Discarding classloaders<br />java.lang.instrument<br />Hacking changes into the JVM itself feels a fr...
Discarding classloaders<br />
Discarding classloaders can help, but<br /> Not particularly granular<br /> Instance state is lost<br /> Can run into memo...
java.lang.instrument<br /> a JRuby object is not much more than a Map from method names to their implementations and from ...
java.lang.instrument<br />java.lang.instrument.ClassFileTransformer. This class can do two things:<br />replace method bod...
java.lang.instrument<br />class Class1{  protected static int field;    public static Object __REDEFINED_STATIC_METHOD_(in...
java.lang.instrument<br />public class Proxy1{  public void setField(intparam)  {    Object[] params = new Object[1];    p...
Sounds well,but…<br />Perfomance. subject to indirection & Pem memory<br />Java SDK classes. The native code<br />Compatib...
fakereplace<br />Google Code project<br />Usual structure changes supported<br />Methods, fields, constructors<br /> Annot...
Fakereplace demo<br />
Jrebel(not free)<br />one master class + several anonymous<br />Solve problem<br />direction  and lightweight<br />Avoids ...
Jrebel Demo<br />
Sounds perfect<br />
            国家队( nation team )<br />Hotswap.patch<br />
hotspot and hotswap<br />Arbitrary changes possible (including changes to subtype relationships)<br />No performance penal...
HotswapStatus<br />
Transformer Methods<br />class A<br />{<br />int x; <br />intdoubleX; <br />	static void $staticTransformer()  //class tra...
Hotswap.patch demo<br />
Hotswap.patchAlgorithm<br /><ul><li>Loading the New Classes
Updating the Data Structures and Pointers</li></li></ul><li>Loading the New Classes<br />step1:Find all affected classes.<...
Loading the New Classes<br />step2:Sort the classes topologically.<br /> (VM_RedefineClasses::TopologicalClassSorting)<br ...
Updating the Data Structures and Pointers<br />step1:Flush dependent code .<br />Currently all compiled methods are deopti...
Updating the Data Structures and Pointers<br />step2:Update constant pool cache entries.<br /> (VM_RedefineClasses::adjust...
Updating the Data Structures and Pointers<br />step3: Swap pointers.<br />利用gc的特性 ,gc 复制一个对象的后 会调整指向该对象的pointer<br /> (Mar...
Updating the Data Structures and Pointers<br />step4: Update instance fields.<br />all new fields are initialized to zero....
Fantastic,but…<br />
Hotswap@taobao.com<br /><ul><li>HSF_JETTY plugin自动集成hotswap.patch。
你也可以自己打patch。</li></li></ul><li>Question time<br />
Party time谢谢!xuanxi@taobao.com<br />
Upcoming SlideShare
Loading in …5
×

Hotspot & hotswap, who and who are best freinds

6,172 views

Published on

3 Comments
17 Likes
Statistics
Notes
No Downloads
Views
Total views
6,172
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
180
Comments
3
Likes
17
Embeds 0
No embeds

No notes for slide

Hotspot & hotswap, who and who are best freinds

  1. 1. hotspot & hotswapwho and who are best friends<br />xuanxi@taobao.com<br />
  2. 2. why we need hotswap?<br />主管: 你在偷懒?<br />汇编开发人员:我在编译。<br />C++开发人员: 我在编译打包部署。<br />java 开发人员: 我在打包部署。<br />Jsp开发人员:我在部署。<br />Python开发人员:………<br />Ruby开发人员:………<br />
  3. 3. why we need hotswap?<br />线上紧急修改。(方法体的修改、增加和减少方法,字段,类,接口,更改类继承拓扑结构)<br />
  4. 4. JVM 1.2 以后支持了hotswap功能<br />JVMTM Tool Interface<br />typedefstruct {<br />jclassklass;<br />jintclass_byte_count;<br /> const unsigned char* class_bytes;<br />} jvmtiClassDefinition;<br />jvmtiError<br />RedefineClasses(jvmtiEnv* env,<br />jintclass_count,<br /> const jvmtiClassDefinition* class_definitions)<br />
  5. 5. JVM 1.2 以后支持了hotswap功能<br />JavaTM Virtual Machine Debug Interface Reference<br />typedefstruct {<br />jclassclazz; /* Class to be redefined */<br />jintclass_byte_count; /* number of bytes defining class (below) */<br />jbyte *class_bytes; /* bytes defining class */<br /> /* (in Class File Format of JVM spec) */<br />} JVMDI_class_definition;<br /> <br />jvmdiError<br />RedefineClasses(jintclassCount, JVMDI_class_definition *classDefs)<br />
  6. 6. Java dev除打包部署外还干嘛?<br />用IDE(eclipse)调试<br />
  7. 7. Sounds good,what's the problem?<br />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.<br />
  8. 8. So, Here we go!<br />Discarding classloaders<br />java.lang.instrument<br />Hacking changes into the JVM itself feels a fragile approach<br />
  9. 9. Discarding classloaders<br />
  10. 10. Discarding classloaders can help, but<br /> Not particularly granular<br /> Instance state is lost<br /> Can run into memory problems<br />Demo(discarding.classloaders.hotswap) by ken.wug@gmail.com<br />
  11. 11. java.lang.instrument<br /> a JRuby object is not much more than a Map from method names to their implementations and from field names to their values.<br />byte[] transform(ClassLoader loader,<br /> String className,<br /> Class<?> classBeingRedefined,<br />ProtectionDomainprotectionDomain,<br /> byte[] classfileBuffer)<br /> throws IllegalClassFormatException<br />
  12. 12. java.lang.instrument<br />java.lang.instrument.ClassFileTransformer. This class can do two things:<br />replace method bodies of existing classes (but nothing else, it is essentially the same as hotswap).<br />modify classes before they get loaded into the java virtual machine<br />class Class1<br />{ protected static int field;<br />}<br />
  13. 13. java.lang.instrument<br />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;  }}<br />
  14. 14. java.lang.instrument<br />public class Proxy1{  public void setField(intparam)  {    Object[] params = new Object[1];    params[0] = new Integer(param);    Class1.__REDEFINED_STATIC_METHOD_(1,params);  }}<br />
  15. 15. Sounds well,but…<br />Perfomance. subject to indirection & Pem memory<br />Java SDK classes. The native code<br />Compatibility.  A.class.getMethods()<br />
  16. 16. fakereplace<br />Google Code project<br />Usual structure changes supported<br />Methods, fields, constructors<br /> Annotations<br />Reflection instrumentation<br />Integrates support directly for some frameworks<br />
  17. 17. Fakereplace demo<br />
  18. 18. Jrebel(not free)<br />one master class + several anonymous<br />Solve problem<br />direction and lightweight<br />Avoids instrumenting the Java<br />Tweaks the results of the Reflection API, so that we can correctly include the added/removed members in these results<br />
  19. 19. Jrebel Demo<br />
  20. 20. Sounds perfect<br />
  21. 21. 国家队( nation team )<br />Hotswap.patch<br />
  22. 22. hotspot and hotswap<br />Arbitrary changes possible (including changes to subtype relationships)<br />No performance penalty before or after the change<br />No introduced indirections<br />Continued execution of old active methods<br />Only simple, comprehensible strategies for field matching or method transitions<br />Change possible at any point when the VM is suspended<br />Graceful handling of accesses to deleted fields or calls of deleted methods<br />
  23. 23. HotswapStatus<br />
  24. 24. Transformer Methods<br />class A<br />{<br />int x; <br />intdoubleX; <br /> static void $staticTransformer() //class transformer<br /> { <br />System.out.println("Class A has a new version"); <br /> } <br /> void $transformer() { doubleX = 2 * x; } //instance’s <br />}<br />
  25. 25. Hotswap.patch demo<br />
  26. 26. Hotswap.patchAlgorithm<br /><ul><li>Loading the New Classes
  27. 27. Updating the Data Structures and Pointers</li></li></ul><li>Loading the New Classes<br />step1:Find all affected classes.<br /> (VM_RedefineClasses::FindAffectedKlassesClosure)<br />Redefining D and B<br />Find all affected classes<br />
  28. 28. Loading the New Classes<br />step2:Sort the classes topologically.<br /> (VM_RedefineClasses::TopologicalClassSorting)<br />Topological order.<br />
  29. 29. Updating the Data Structures and Pointers<br />step1:Flush dependent code .<br />Currently all compiled methods are deoptimized<br /> (Classes::flush_dependent_code)<br />
  30. 30. Updating the Data Structures and Pointers<br />step2:Update constant pool cache entries.<br /> (VM_RedefineClasses::adjust_cpool_cache)<br />
  31. 31. Updating the Data Structures and Pointers<br />step3: Swap pointers.<br />利用gc的特性 ,gc 复制一个对象的后 会调整指向该对象的pointer<br /> (MarkSweep::adjust_pointer)<br />
  32. 32. Updating the Data Structures and Pointers<br />step4: Update instance fields.<br />all new fields are initialized to zero.(新建)<br />instanceKlass::do_fields_evolution(标记)<br />MarkSweep::update_fields(拷贝)<br />
  33. 33. Fantastic,but…<br />
  34. 34. Hotswap@taobao.com<br /><ul><li>HSF_JETTY plugin自动集成hotswap.patch。
  35. 35. 你也可以自己打patch。</li></li></ul><li>Question time<br />
  36. 36. Party time谢谢!xuanxi@taobao.com<br />

×