Intrinsic Methods in HotSpot VM

9,272 views
8,852 views

Published on

A presentation I did in a GreenTeaJUG event on HotSpot intrinsics. (Feb 23, 2013) http://rednaxelafx.iteye.com/blog/1814180

Published in: Technology
0 Comments
25 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
9,272
On SlideShare
0
From Embeds
0
Number of Embeds
49
Actions
Shares
0
Downloads
163
Comments
0
Likes
25
Embeds 0
No embeds

No notes for slide

Intrinsic Methods in HotSpot VM

  1. 1. 1 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  2. 2. Intrinsic Methods in HotSpotVMKrystal MoMember of Technical StaffHotSpot JVM Compiler Team2 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  3. 3. The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.3 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  4. 4. Agenda  Background  Intrinsic Methods in HotSpot VM  Intrinsic Methods added in TaobaoJDK  Experiment: Implement Your Own Intrinsic  Further Experiment4 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  5. 5. Agenda  Background  Intrinsic Methods in HotSpot VM  Intrinsic Methods added in TaobaoJDK  Experiment: Implement Your Own Intrinsic  Further Experiment5 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  6. 6. What is a Intrinsic Method?  In compiler theory, an intrinsic function is a function available for use in a given programming language whose implementation is handled specially by the compiler. Typically, it substitutes a sequence of automatically generated instructions for the original function call, similar to an inline function. Unlike an inline function though, the compiler has an intimate knowledge of the intrinsic function and can therefore better integrate it and optimize it for the situation. This is also called builtin function in many languages.  (via Wikipedia)6 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  7. 7. Intrinsic Functions in Native Compilers Examples  Microsoft Visual C/C++ Compiler – __debugbreak()  inserts an “int 3” instruction for breaking into debugger  GCC – __builtin_ia32_pause()  inserts a “pause” instruction and necessary compiler barriers to prevent the instruction from floating around7 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  8. 8. Intrinsic Methods in JVMs  Specific to JVM implementations – can’t assume a method is intrinsic across JVMs in general – mostly implemented in JVM compilers8 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  9. 9. Intrinsic Methods in JVMs  Compatible – appear to be no different from normal Java methods on Java source level; all special handling is done within JVM – can fallback to normal (non-intrinsic) version on JVMs that don’t intrinsify them9 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  10. 10. Intrinsic Methods in JVMs  Reliable – are good “anchor” points for JVM optimizations for common code patterns – e.g. java.lang.System.arraycopy()  easier to recognize than matching an explicit array-copying loop10 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  11. 11. Intrinsic Methods in JVMs  Extend Java semantics – the same way native methods do, but usually more performant  reliably inlined  no JNI overhead – e.g. sun.misc.Unsafe.compareAndSwapInt() on x86/AMD64  no Java bytecode can express its semantics  without intrinsics: implemented in native via JNI (Unsafe_CompareAndSwapInt())  with intrinsics: a direct cmpxchg instruction, inlined to caller11 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  12. 12. Agenda  Background  Intrinsic Methods in HotSpot VM  Intrinsic Methods added in TaobaoJDK  Experiment: Implement Your Own Intrinsic  Further Experiment12 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  13. 13. Intrinsic Methods in HotSpot VM What to intrinsify?  Commonly used public APIs in JDK core library – to speed up common code patterns – so use JDK core library methods whenever you can  they may be better optimized!  Special internal methods – to implement special semantics – the “secret sauce” to allow more parts of the Java core library be implemented in Java – may be exposed indirectly via ease-of-use APIs, e.g. j.u.c.atomic.*13 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  14. 14. Intrinsic Methods in HotSpot VM How to intrinsify?  Declare intrinsic methods in vmSymbols  Implement intrinsic methods in – Interpreter  currently only implements intrinsics for – some math functions – a few MethodHandle internals for bootstrapping – Client Compiler (C1) – Server Compiler (C2)14 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  15. 15. Intrinsic Methods in HotSpot VM Example: java.lang.System.currentTimeMillis() on Linux  Interpreter – not intrinsified – java.lang.System.currentTimeMillis() (Java / core library)  (-> through normal JNI call path)  JVM_CurrentTimeMillis() (C++ / HotSpot VM)  os::javaTimeMillis() (C++ / HotSpot VM)  gettimeofday() (C / Linux)15 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  16. 16. Intrinsic Methods in HotSpot VM Example: java.lang.System.currentTimeMillis() on Linux  C1 – intrinsified – inlined to caller as a direct call to os::javaTimeMillis()  C2 – same as in C1  (Intrinsification eliminates JNI overhead in compiled code)16 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  17. 17. Intrinsic Methods in HotSpot VM Example: sun.misc.Unsafe.compareAndSwapInt() on x86/AMD64  Interpreter – not intrinsified – sun.misc.Unsafe.compareAndSwapInt() (Java / core library)  (-> through normal JNI call path)  Unsafe_CompareAndSwapInt() (C++ / HotSpot VM)  Atomic::cmpxchg() (C++ inline asm / HotSpot VM)  lock cmpxchgl17 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  18. 18. Intrinsic Methods in HotSpot VM Example: sun.misc.Unsafe.compareAndSwapInt() on x86/AMD64  C1 – intrinsified – inlined into caller as a plain “lock cmpxchgl” instruction  C2 – same as in C1  (Intrinsification easily leverages special hardware instructions)18 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  19. 19. Intrinsic Methods in HotSpot VM Example: java.lang.Math.log() on x86/AMD64  Interpreter – intrinsified – java.lang.Math.log() (Java / core library)  (-> through special interpreter method entry)  “flog” x87 instruction19 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  20. 20. Intrinsic Methods in HotSpot VM Example: java.lang.Math.log() on x86/AMD64  C1 and C2 – intrinsified – inlined into caller as “flog” x87 instruction  (Intrinsification ignores Java-level implementation) – java.lang.Math.log() is implemented in pure Java in JDK core library – HotSpot VM ignores that implementation on platforms where hardware floating point instructions are available20 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  21. 21. Intrinsic Methods in HotSpot VM Other intrinsic methods of interest (on AMD64)  java.lang.Thread.currentThread() – mov reg, [r15 + java_thread_offset]  java.lang.String.indexOf()/compareTo()/equals() – use STTNI instructions in SSE4.2  com.sun.crypto.provider.AESCrypt.encryptBlock()/decryptBlock() – use AES instructions21 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  22. 22. Intrinsic Methods in HotSpot VM Find out if you’re using intrinsics in compiled code  -XX:+PrintCompilation -XX:+PrintInlining – (prepend -XX:+UnlockDiagnosticVMOptions when running on product VM)22 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  23. 23. Intrinsic Methods in HotSpot VM Find out if you’re using intrinsics in compiled code: example public class Foo { private static Object bar() { return Thread.currentThread(); } public static void main(String[] args) { while (true) { bar(); } } }23 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  24. 24. Intrinsic Methods in HotSpot VM Find out if you’re using intrinsics in compiled code: example$ java -XX:+UnlockDiagnosticVMOptions -XX:CompileCommand=exclude,Foo,main -XX:+PrintCompilation -XX:+PrintInlining FooCompilerOracle: exclude Foo.main 50 1 n java.lang.Thread::currentThread (0 bytes) (static)### Excluding compile: static Foo::main 50 2 Foo::bar (4 bytes) @ 0 java.lang.Thread::currentThread (0 bytes) (intrinsic)24 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  25. 25. Intrinsic Methods in HotSpot VM Find out what intrinsic compiled into  -XX:+PrintAssembly – (prepend -XX:+UnlockDiagnosticVMOptions when running on product VM) – requires hsdis disassembler plugin25 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  26. 26. Intrinsic Methods in HotSpot VM Find out what intrinsic compiled into: example $ java -XX:+UnlockDiagnosticVMOptions -XX:CompileCommand=exclude,Foo,main -XX:+PrintAssembly Foo ... # {method} bar ()Ljava/lang/Object; in Foo # [sp+0x20] (sp of caller) 0x00007f91cd061bc0: sub $0x18,%rsp 0x00007f91cd061bc7: mov %rbp,0x10(%rsp) ;*synchronization entry ; - Foo::bar@-1 (line 3) 0x00007f91cd061bcc: mov 0x1b0(%r15),%rax ;*invokestatic currentThread ; - Foo::bar@0 (line 3) 0x00007f91cd061bd3: add $0x10,%rsp 0x00007f91cd061bd7: pop %rbp 0x00007f91cd061bd8: test %eax,0xb7ed422(%rip) # 0x00007f91d884f000 ; {poll_return} 0x00007f91cd061bde: retq 0x00007f91cd061bdf: hlt26 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  27. 27. Agenda  Background  Intrinsic Methods in HotSpot VM  Intrinsic Methods added in TaobaoJDK  Experiment: Implement Your Own Intrinsic  Further Experiment27 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  28. 28. Instrinsic Methods Added in TaobaoJDK Why make your own custom intrinsic method?  Make better use of new instructions available on new hardware – In Taobao’s case, new instructions on Westmere/Sandy Bridge  Eliminate JNI overhead when having to invoke hot native methods – Instead of calling a native method through normal JNI, implement it as an intrinsic method (this section is material from Taobao; TaobaoJDK is a custom version of OpenJDK from Taobao)28 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  29. 29. Instrinsic Methods Added in TaobaoJDK A few examples  TCrc32.xxx – crc32c instruction in SSE4.2  Unsafe.byteArrayCompare() – byte array comparison via packed compare instructions  Unsafe.pause() – insert “pause” instruction before backedge of spinlock-like loop …29 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  30. 30. Instrinsic Methods Added in TaobaoJDK crc32c  Used in Hadoop – throughput in TestDFSIO benchmark increased by 40%-180%30 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  31. 31. JVM Instrinsics Added in TaobaoJDK crc32c31 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  32. 32. Agenda  Background  Intrinsic Methods in HotSpot VM  Intrinsic Methods added in TaobaoJDK  Experiment: Implement Your Own Intrinsic  Further Experiment32 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  33. 33. Implementing an Intrinsic in C1 Example  Implement java.lang.Class.isInstance() intrinsic in C1 – https://gist.github.com/rednaxelafx/2830194  Note: starting point at GraphBuilder::try_inline_intrinsics()33 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  34. 34. Implementing an Intrinsic in C2 Example  Implement a simple intrinsic demo in C2 – https://gist.github.com/rednaxelafx/1986224  Implement a prototype for java.lang.Math.addExact() intrinsic in C2 – https://gist.github.com/rednaxelafx/db03ab15ef8b76246b84  Note: starting point at LibraryCallKit::try_to_inline()34 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  35. 35. Agenda  Background  Intrinsic Methods in HotSpot VM  Intrinsic Methods added in TaobaoJDK  Experiment: Implement Your Own Intrinsic  Further Experiment35 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  36. 36. Further Experiment Introducing Graal  Experiment with implementing your own language-/library-specific intrinsic in HotSpot VM, but don’t want to write C++ or build HotSpot VM? – Graal (from Oracle Labs) is the answer to your call!  bytecode-to-native compiler implemented in Java, can be plugged into HotSpot VM  OpenJDK project page  Graal introduction (from JVM Language Summit 2011)36 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  37. 37. Q&A37 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013
  38. 38. 38 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 02/22/2013

×