Hotspot & hotswap, who and who are best freinds

Download Hotspot & hotswap, who and who are best freinds

Post on 22-Apr-2015

5.913 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

<ul><li> 1. hotspot &amp; hotswapwho and who are best friends<br />xuanxi@taobao.com<br /></li> <li> 2. why we need hotswap<br />: <br /><br />C++: <br />java <br />Jsp<br />Python:<br />Ruby:<br /></li> <li> 3. why we need hotswap?<br />(,)<br /></li> <li> 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 /></li> <li> 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 /></li> <li> 6. Java dev<br />IDE(eclipse)<br /></li> <li> 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 /></li> <li> 8. So, Here we go!<br />Discarding classloaders<br />java.lang.instrument<br />Hacking changes into the JVM itself feels a fragile approach<br /></li> <li> 9. Discarding classloaders<br /></li> <li> 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 /></li> <li> 11. java.lang.instrument<br />aJRubyobject 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 /></li> <li> 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 /></li> <li> 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 /></li> <li> 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 /></li> <li> 15. Sounds well,but<br />Perfomance. subject to indirection &amp; Pem memory<br />Java SDK classes.The native code<br />Compatibility. A.class.getMethods()<br /></li> <li> 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 /></li> <li> 17. Fakereplace demo<br /></li> <li> 18. Jrebelnot 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 /></li> <li> 19. Jrebel Demo<br /></li> <li> 20. Sounds perfect<br /></li> <li> 21. nation team <br />Hotswap.patch<br /></li> <li> 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 /></li> <li> 23. HotswapStatus<br /></li> <li> 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; } //instances <br />}<br /></li> <li> 25. Hotswap.patch demo<br /></li> <li> 26. Hotswap.patchAlgorithm<br /><ul><li>Loading the New Classes </li> <li> 27. Updating the Data Structures and Pointers</li></ul></li></ul>