SlideShare uma empresa Scribd logo
1 de 36
hotspot & hotswapwho and who are best friends xuanxi@taobao.com
why we need hotswap? 主管: 你在偷懒? 汇编开发人员:我在编译。 C++开发人员: 我在编译打包部署。 java 开发人员: 我在打包部署。 Jsp开发人员:我在部署。 Python开发人员:……… Ruby开发人员:………
why we need hotswap? 线上紧急修改。(方法体的修改、增加和减少方法,字段,类,接口,更改类继承拓扑结构)
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)
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 ,[object Object]
Updating the Data Structures and Pointers,[object Object]
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 ,[object Object]
你也可以自己打patch。,[object Object]
Party time谢谢!xuanxi@taobao.com

Mais conteúdo relacionado

Mais procurados

Performance testing with jmeter
Performance testing with jmeter Performance testing with jmeter
Performance testing with jmeter Knoldus Inc.
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
Performance testing presentation
Performance testing presentationPerformance testing presentation
Performance testing presentationBelatrix Software
 
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScript
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScriptJS Fest 2019. Ryan Dahl. Deno, a new way to JavaScript
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScriptJSFestUA
 
Test Design and Automation for REST API
Test Design and Automation for REST APITest Design and Automation for REST API
Test Design and Automation for REST APIIvan Katunou
 
PUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBootPUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBootJosué Neis
 
Unit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma introUnit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma introMaurice De Beijer [MVP]
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PBAbhishek Yadav
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practicesnickokiss
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Prashanth Kumar
 
JMeter - Performance testing your webapp
JMeter - Performance testing your webappJMeter - Performance testing your webapp
JMeter - Performance testing your webappAmit Solanki
 

Mais procurados (20)

TestNG with selenium
TestNG with seleniumTestNG with selenium
TestNG with selenium
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
Performance testing with jmeter
Performance testing with jmeter Performance testing with jmeter
Performance testing with jmeter
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Soap and Rest
Soap and RestSoap and Rest
Soap and Rest
 
Performance testing presentation
Performance testing presentationPerformance testing presentation
Performance testing presentation
 
Maven tutorial
Maven tutorialMaven tutorial
Maven tutorial
 
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScript
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScriptJS Fest 2019. Ryan Dahl. Deno, a new way to JavaScript
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScript
 
Core java slides
Core java slidesCore java slides
Core java slides
 
Test ng
Test ngTest ng
Test ng
 
Java basics mind map
Java basics mind mapJava basics mind map
Java basics mind map
 
Test Design and Automation for REST API
Test Design and Automation for REST APITest Design and Automation for REST API
Test Design and Automation for REST API
 
Testing Services
Testing ServicesTesting Services
Testing Services
 
PUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBootPUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBoot
 
Unit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma introUnit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma intro
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PB
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)
 
De Java 8 a Java 17
De Java 8 a Java 17De Java 8 a Java 17
De Java 8 a Java 17
 
JMeter - Performance testing your webapp
JMeter - Performance testing your webappJMeter - Performance testing your webapp
JMeter - Performance testing your webapp
 

Semelhante a Hotspot & hotswap, who and who are best freinds

Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java programming basics
Java programming basicsJava programming basics
Java programming basicsHamid Ghorbani
 
Владимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpotВладимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpotVolha Banadyseva
 
JAVA(module1).pptx
JAVA(module1).pptxJAVA(module1).pptx
JAVA(module1).pptxSRKCREATIONS
 
The craft of meta programming on JVM
The craft of meta programming on JVMThe craft of meta programming on JVM
The craft of meta programming on JVMIgor Khotin
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction AKR Education
 
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningCarol McDonald
 
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov Nayden Gochev
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxPoonam60376
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAVINASH KUMAR
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satyaSatya Johnny
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8Raffi Khatchadourian
 
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신Sungchul Park
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesRaffi Khatchadourian
 

Semelhante a Hotspot & hotswap, who and who are best freinds (20)

Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
 
First fare 2010 java-introduction
First fare 2010 java-introductionFirst fare 2010 java-introduction
First fare 2010 java-introduction
 
Владимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpotВладимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpot
 
JAVA(module1).pptx
JAVA(module1).pptxJAVA(module1).pptx
JAVA(module1).pptx
 
The craft of meta programming on JVM
The craft of meta programming on JVMThe craft of meta programming on JVM
The craft of meta programming on JVM
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction
 
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
 
1- java
1- java1- java
1- java
 
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptx
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
 
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to Interfaces
 

Hotspot & hotswap, who and who are best freinds

  • 1. hotspot & hotswapwho and who are best friends xuanxi@taobao.com
  • 2. why we need hotswap? 主管: 你在偷懒? 汇编开发人员:我在编译。 C++开发人员: 我在编译打包部署。 java 开发人员: 我在打包部署。 Jsp开发人员:我在部署。 Python开发人员:……… Ruby开发人员:………
  • 3. why we need hotswap? 线上紧急修改。(方法体的修改、增加和减少方法,字段,类,接口,更改类继承拓扑结构)
  • 4. 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)
  • 5. 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)
  • 7. 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.
  • 8. So, Here we go! Discarding classloaders java.lang.instrument Hacking changes into the JVM itself feels a fragile approach
  • 10. 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
  • 11. 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
  • 12. 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; }
  • 13. 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;  }}
  • 14. 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);  }}
  • 15. Sounds well,but… Perfomance. subject to indirection & Pem memory Java SDK classes. The native code Compatibility.  A.class.getMethods()
  • 16. fakereplace Google Code project Usual structure changes supported Methods, fields, constructors Annotations Reflection instrumentation Integrates support directly for some frameworks
  • 18. 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
  • 21. 国家队( nation team ) Hotswap.patch
  • 22. 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
  • 24. 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 }
  • 26.
  • 27.
  • 28. Loading the New Classes step2:Sort the classes topologically. (VM_RedefineClasses::TopologicalClassSorting) Topological order.
  • 29. Updating the Data Structures and Pointers step1:Flush dependent code . Currently all compiled methods are deoptimized (Classes::flush_dependent_code)
  • 30. Updating the Data Structures and Pointers step2:Update constant pool cache entries. (VM_RedefineClasses::adjust_cpool_cache)
  • 31. Updating the Data Structures and Pointers step3: Swap pointers. 利用gc的特性 ,gc 复制一个对象的后 会调整指向该对象的pointer (MarkSweep::adjust_pointer)
  • 32. Updating the Data Structures and Pointers step4: Update instance fields. all new fields are initialized to zero.(新建) instanceKlass::do_fields_evolution(标记) MarkSweep::update_fields(拷贝)
  • 34.
  • 35.