Hotspot&hotswap,whoandwhoarebestfreinds(点击这里查看ppt)
hotspot&hotswapwhoandwhoarebestfriends
[email protected]
whyweneedhotswap?
主管:你在偷懒?
汇编开发人员:我在编译。
C++开发人员:我在编译打包部署。
java开发人员:我在打包部署。
Jsp开发人员:我在部署。
Python开发人员:………
Ruby开发人员:………
whyweneedhotswap?
线上紧急修改。(方法体的修改、增加和减少方法,字段,类,接口,更改类继承拓扑结构)
JVM1.2以后支持了hotswap功能
JVMTMToolInterface
typedefstruct { jclassklass; jintclass_byte_count; const unsigned char* class_bytes; } jvmtiClassDefinition; jvmtiError RedefineClasses(jvmtiEnv* env, jintclass_count, const jvmtiClassDefinition* class_definitions)
JVM1.2以后支持了hotswap功能
JavaTMVirtualMachineDebugInterfaceReference
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)
Javadev除打包部署外还干嘛?
用IDE(eclipse)调试
Soundsgood,what’stheproblem?
Unfortunately,thisredefinitionislimitedonlytochangingmethodbodies―itcannoteitheraddmethodsorfieldsorotherwisechangeanythingelse,exceptforthemethodbodies.
So,Herewego!
Discardingclassloaders
java.lang.instrument
HackingchangesintotheJVMitselffeelsafragileapproach
Discardingclassloaders
Discardingclassloaderscanhelp,but
Notparticularlygranular
Instancestateislost
Canrunintomemoryproblems
Demo(discarding.classloaders.hotswap)[email protected]
java.lang.instrument
aJRubyobjectisnotmuchmorethanaMapfrommethodnamestotheirimplementationsandfromfieldnamestotheirvalues.
byte[]transform(ClassLoaderloader,
StringclassName,
Class<?>classBeingRedefined,
ProtectionDomainprotectionDomain,
byte[]classfileBuffer)
throwsIllegalClassFormatException
java.lang.instrument
java.lang.instrument.ClassFileTransformer.Thisclasscandotwothings:
replacemethodbodiesofexistingclasses(butnothingelse,itisessentiallythesameashotswap).
modifyclassesbeforetheygetloadedintothejavavirtualmachine
classClass1
{protectedstaticintfield;
}
java.lang.instrument
classClass1{protectedstaticintfield;publicstaticObject__REDEFINED_STATIC_METHOD_(intmethodNo,Object[]parameters){if(methodNo==0){returnfield;}if(methodNo==1){Integerp0=(Integer)parameters[0];intunboxedP0=p0.intValue();field=unboxedP0;returnnull;}returnnull;}}
java.lang.instrument
publicclassProxy1{publicvoidsetField(intparam){Object[]params=newObject[1];params[0]=newInteger(param);Class1.__REDEFINED_STATIC_METHOD_(1,params);}}
Soundswell,but…
Perfomance.subjecttoindirection&Pemmemory
JavaSDKclasses.Thenativecode
Compatibility.A.class.getMethods()
fakereplace
GoogleCodeproject
Usualstructurechangessupported
Methods,fields,constructors
Annotations
Reflectioninstrumentation
Integratessupportdirectlyforsomeframeworks
Fakereplacedemo
Jrebel(notfree)
onemasterclass+severalanonymous
Solveproblem
directionandlightweight
AvoidsinstrumentingtheJava
TweakstheresultsoftheReflectionAPI,sothatwecancorrectlyincludetheadded/removedmembersintheseresults
JrebelDemo
Soundsperfect
国家队(nationteam)
Hotswap.patch
hotspotandhotswap
Arbitrarychangespossible(includingchangestosubtyperelationships)
Noperformancepenaltybeforeorafterthechange
Nointroducedindirections
Continuedexecutionofoldactivemethods
Onlysimple,comprehensiblestrategiesforfieldmatchingormethodtransitions
ChangepossibleatanypointwhentheVMissuspended
Gracefulhandlingofaccessestodeletedfieldsorcallsofdeletedmethods
HotswapStatus
TransformerMethods
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.patchdemo
Hotswap.patchAlgorithm
LoadingtheNewClasses
UpdatingtheDataStructuresandPointers
LoadingtheNewClasses
step1:Findallaffectedclasses.
(VM_RedefineClasses::FindAffectedKlassesClosure)
RedefiningDandB
Findallaffectedclasses
LoadingtheNewClasses
step2:Sorttheclassestopologically.
(VM_RedefineClasses::TopologicalClassSorting)
Topologicalorder.
UpdatingtheDataStructuresandPointers
step1:Flushdependentcode.
Currentlyallcompiledmethodsaredeoptimized
(Classes::flush_dependent_code)
UpdatingtheDataStructuresandPointers
step2:Updateconstantpoolcacheentries.
(VM_RedefineClasses::adjust_cpool_cache)
UpdatingtheDataStructuresandPointers
step3:Swappointers.
利用gc的特性,gc复制一个对象的后会调整指向该对象的pointer
(MarkSweep::adjust_pointer)
UpdatingtheDataStructuresandPointers
step4:Updateinstancefields.
allnewfieldsareinitializedtozero.(新建)
instanceKlass::do_fields_evolution(标记)
MarkSweep::update_fields(拷贝)
Fantastic,but…
HSF_JETTYplugin自动集成hotswap.patch。
你也可以自己打patch。
Questiontime
Partytime谢谢[email protected]