SlideShare a Scribd company logo
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

More Related Content

What's hot

Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
Cheng Ta Yeh
 
Java Basics
Java BasicsJava Basics
Java Basics
Brandon Black
 
Java Introduction
Java IntroductionJava Introduction
Java Introduction
sunmitraeducation
 
Core java slides
Core java slidesCore java slides
Core java slides
Abhilash Nair
 
Java Basics
Java BasicsJava Basics
Java Basics
shivamgarg_nitj
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
Vishnu Suresh
 
Spring boot Under Da Hood
Spring boot Under Da HoodSpring boot Under Da Hood
Spring boot Under Da Hood
Michel Schudel
 
Spring Boot
Spring BootSpring Boot
Spring Boot
Jiayun Zhou
 
SOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principles
Sergey Karpushin
 
PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL
PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQLPL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL
PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL
Reactive.IO
 
Clean Code
Clean CodeClean Code
Clean Code
Victor Rentea
 
Core java
Core javaCore java
Core java
Shivaraj R
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
Jafar Nesargi
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
R. Sosa
 
Spring annotation
Spring annotationSpring annotation
Spring annotation
Rajiv Srivastava
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
arnold 7490
 
JDBC
JDBCJDBC
Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentals
javaease
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
Ram132
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
Mahika Tutorials
 

What's hot (20)

Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java Introduction
Java IntroductionJava Introduction
Java Introduction
 
Core java slides
Core java slidesCore java slides
Core java slides
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Spring boot Under Da Hood
Spring boot Under Da HoodSpring boot Under Da Hood
Spring boot Under Da Hood
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
SOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principles
 
PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL
PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQLPL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL
PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL
 
Clean Code
Clean CodeClean Code
Clean Code
 
Core java
Core javaCore java
Core java
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
 
Spring annotation
Spring annotationSpring annotation
Spring annotation
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
JDBC
JDBCJDBC
JDBC
 
Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentals
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
 

Similar to Hotspot & hotswap, who and who are best freinds

Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
Hamid Ghorbani
 
First fare 2010 java-introduction
First fare 2010 java-introductionFirst fare 2010 java-introduction
First fare 2010 java-introduction
Oregon FIRST Robotics
 
Владимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpotВладимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpot
Volha Banadyseva
 
JAVA(module1).pptx
JAVA(module1).pptxJAVA(module1).pptx
JAVA(module1).pptx
SRKCREATIONS
 
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
Igor 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, Tuning
Carol McDonald
 
1- java
1- java1- 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
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. .pptx
Poonam60376
 
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
AVINASH KUMAR
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
Satya Johnny
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
Jyothsna Sree
 
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 8Introduction to new features in java 8
Introduction to new features in java 8
Raffi 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 Interfaces
Raffi Khatchadourian
 

Similar to 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.