Hotspot hotswap, who and who are best freinds
Hotspot&hotswap,whoandwhoarebestfreinds(點擊這里查看ppt)
java動態替換的ppt。
hotspot&hotswapwhoandwhoarebestfriends
xuanxi@taobao.com
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)byken.wug@gmail.com
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
FindallaffectedclassesLoadingtheNewClasses
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…
Hotswap@taobao.com
HSF_JETTYplugin自動集成hotswap.patch。
你也可以自己打patch。
Questiontime
Partytime謝謝!xuanxi@taobao.com
轉載于:https://blog.51cto.com/aliapp/1327693
總結
以上是生活随笔為你收集整理的Hotspot hotswap, who and who are best freinds的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《菜菜的机器学习sklearn课堂,to
- 下一篇: 陕理工高级语言程序设计实验 (C)答案,