More Related Content
Similar to Ahead-of-Time Compilation with JDK 9 [Java Day Tokyo 2017 D1-A1] (20)
More from David Buck (20)
Ahead-of-Time Compilation with JDK 9 [Java Day Tokyo 2017 D1-A1]
- 1. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Ahead-of-Time Compilation with JDK 9
日本オラクル株式会社
Java SE Sustaining Engineering
バック デイビッド
Java Day Tokyo 2017
2017年5月17日
- 2. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
2
Safe Harbor Statement
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 © 2017, Oracle and/or its affiliates. All rights reserved. |
3
• JVM Sustaining Engineer
• OpenJDK 8 Update Project
Maintainer
• JavaOne Rock Star
• Co-author of Oracle WebLogic
Server 11g 構築・運用ガイド
• @DavidBuckJP
• https://blogs.oracle.com/buck/
Hi There!
- 4. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
4
Program Agenda
注意点
AoT のいろは
似ている機能
使い方
1
2
3
4
- 6. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
6
要注意
• AoT は実験的なものであり、正式にはサポートされていない
- 7. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
7
要注意
• AoT は実験的なものであり、正式にはサポートされていない
• 現時点では、AMD64 の Linux しかサポートされていない
- 8. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
8
要注意
• AoT は実験的なものであり、正式にはサポートされていない
• 現時点では、AMD64 の Linux しかサポートされていない
• 正式なドキュメントはない(JEP のみ)
- 9. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
9
要注意
• AoT は実験的なものであり、正式にはサポートされていない
• 現時点では、AMD64 の Linux しかサポートされていない
• 正式なドキュメントはない(JEP のみ)
• 現時点では、将来的にサポートされる予定はない
- 10. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
10
AoT のいろは
- 11. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
11
AoT を勉強する価値
- 12. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
12
AoT を勉強する価値
• 将来にサポートされるようになる可能性がある
- 13. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
13
AoT を勉強する価値
• 将来にサポートされるようになる可能性がある
• HotSpot の他の機能を勉強するきっかけである
- 14. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
14
AoT を勉強する価値
• 将来にサポートされるようになる可能性がある
• HotSpot の他の機能を勉強するきっかけである
• やはり、面白い!
- 15. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
15
Java Build / Execution Model (Interpreter)
MyClass.java MyClass.class java
(HotSpot)
javac
- 16. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
16
Java Build / Execution Model (JIT)
MyClass.java MyClass.class java
(HotSpot)
javac
マシン語
(メモリ上)
- 17. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
17
Just in Time Compilation (JIT)
- 18. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
18
Just in Time Compilation (JIT)
• メソッドがコンパイルされる
- 19. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
19
Just in Time Compilation (JIT)
• メソッドがコンパイルされる
• コンパイル処理は重い
- 20. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
20
Just in Time Compilation (JIT)
• メソッドがコンパイルされる
• コンパイル処理は重い
• マシン語のバージョンは早い
- 21. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
21
Just in Time Compilation (JIT)
• メソッドがコンパイルされる
• コンパイル処理は重い
• マシン語のバージョンは早い
• パフォーマンスは interpreter よりいい?
- 22. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
22
Just in Time Compilation (JIT)
- 23. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
23
Just in Time Compilation (JIT)
- 24. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
24
Just in Time Compilation (JIT)
パフォーマンスは interpreter よりいい?
- 25. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
25
Just in Time Compilation (JIT)
パフォーマンスは interpreter よりいい?
パフォーマンスの定義次第
- 26. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
26
$ time ./hello_jdt
Hello JavaDay Tokyo!
real 0m0.002s
user 0m0.001s
$ time java HelloJDT
Hello JavaDay Tokyo!
real 0m0.104s
user 0m0.077s
sys 0m0.020s
Just in Time Compilation (JIT)
- 27. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
27
Just in Time Compilation (JIT)
弱点
• コンパイル処理が重い
– メソッドを実行出来るようになるまで時間かかる
– メモリの使用量が大きい
• Hot ではないメソッドの場合、interpreter のほうが適切
- 28. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
28
Just in Time Compilation (JIT)
(HotSpot の実装)
• まず、 すべてのメソッドは interpreter のみで実行
• サンプリングで Hot なメソッドを検出
• Hot のメソッドだけをコンパイル
• Hot ではないメソッドは interpreter で実行
- 29. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
29
HotSpot のJIT でも
• アプリの起動が時間がかかる
• 起動しても、最高のパフォーマンスまで時間がかかる
• コンパイル処理がメモリを消費する
• 実行時にマシン語を生成出来ないプラットフォームもある
ネイティブ(C/C++ などと比べると)
- 30. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
30
Just in Time Compilation (JIT) (HotSpot の実装)
2コンパイラ物語
• C2
– Server 用
– 最適化を頑張る
– Interpreter が収集するプロファイリングデータを参考する
– デフォルト CompileThreshold : 10000
• C1
– Client 用
– 最適化はあまりしない
– プロファイリングデータをあまり利用しない
– デフォルト CompileThreshold : 1500
- 31. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
31
Ahead of Time (AoT) の戦略
• 実行の前にマシン語を生成しておく
• 実行時に既存のマシン語をロードして実行する
• 実行時にマシン語を生成する必要はなくなる
- 32. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
32
Java Build / Execution Model (AoT)
MyClass.java MyClass.class java
(HotSpot)
javac
マシン語
(メモリ上)
- 33. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
33
Java Build / Execution Model (AoT)
MyClass.java MyClass.class java
(HotSpot)
javac
my_class.so
jaotc
- 34. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
34
Java Build / Execution Model
java
(HotSpot)
マシン語
(メモリ上)
java
(HotSpot)
マシン語
(メモリ上)
java
(HotSpot)
マシン語
(メモリ上)
java
(HotSpot)
マシン語
(メモリ上)
- 35. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
35
Java Build / Execution Model
java
(HotSpot)
java
(HotSpot)
java
(HotSpot)
java
(HotSpot)
my_class.so
- 36. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
36
Ahead of Time (AoT) の戦略
- 37. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
37
Ahead of Time (AoT) の戦略
• アプリの起動が早くなる ★
- 38. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
38
Ahead of Time (AoT) の戦略
• アプリの起動が早くなる ★
• peak performance / steady state まですぐ達成する ★
- 39. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
39
Ahead of Time (AoT) の戦略
• アプリの起動が早くなる ★
• peak performance / steady state まですぐ達成する ★
• コンパイルの負荷( CPU 使用 / メモリの消費)を減らす ★
- 40. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
40
Ahead of Time (AoT) の戦略
• アプリの起動が早くなる ★
• peak performance / steady state まですぐ達成する ★
• コンパイルの負荷( CPU 使用 / メモリの消費)を減らす ★
• 同じマシン語のデータを共有することが出来る ★
- 41. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
41
Ahead of Time (AoT) の戦略
• アプリの起動が早くなる ★
• peak performance / steady state まですぐ達成する ★
• コンパイルの負荷( CPU 使用 / メモリの消費)を減らす ★
• 同じマシン語のデータを共有することが出来る ★
• 実行時にマシン語を生成出来ないプラットフォームでも利用できる ★
- 42. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
42
Ahead of Time (AoT) の戦略
• アプリの起動が早くなる ★
• peak performance / steady state まですぐ達成する ★
• コンパイルの負荷( CPU 使用 / メモリの消費)を減らす ★
• 同じマシン語のデータを共有することが出来る ★
• 実行時にマシン語を生成出来ないプラットフォームでも利用できる ★
★ 場合がある
- 45. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
45
少し似ている技術
• Tiered Compilation
• Class Data Sharing(CDS) / AppCDS
- 46. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
46
Tiered Compilation
• 従来の server JVM
– インタプリタ(プロファイリング)
– C2
• Tiered Compilation
– インタプリタ(プロファイリング)
– プロファイリング付きの C1
– C2
- 47. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
47
No Class Data Sharing
java
(HotSpot)
クラスデータ
(メモリ上)
java
(HotSpot)
クラスデータ
(メモリ上)
java
(HotSpot)
クラスデータ
(メモリ上)
java
(HotSpot)
クラスデータ
(メモリ上)
- 48. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
48
Class Data Sharing
java
(HotSpot)
java
(HotSpot)
java
(HotSpot)
java
(HotSpot)
クラスデータ
classes.jsa
- 49. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
49
• Class Data Sharing
– Java SE のクラスライブラリ
• AppCDS
– アプリケーションのクラスも含む
- 50. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
50
AoT との共通点
• Tiered Compilation
– Warm up を早くしたい
• Class Data Sharing(CDS) / AppCDS
– Warm up を早くしたい
– メモリの使用量を減らしたい
AoT と異なる点
• マシン語を保存し、共用する機能
はない
少し似ている技術
- 51. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
51
Corporate Photography Collection
- 53. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
53
新しいツール: jaotc
MyClass.class my_class.sojaotc
- 54. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
54
$ jaotc --output libHelloAOT.so HelloAoT.class
jaotc
- 55. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
55
$ jaotc --help
Usage: jaotc <options> list
list A : separated list of class names, modules, jar files
or directories which contain class files.
where options include:
--output <file> Output file name
--class-name <class names> List of classes to compile
--jar <jarfiles> List of jar files to compile
--module <modules> List of modules to compile
--directory <dirs> List of directories where to search for files to compile
--search-path <dirs> List of directories where to search for specified files
--compile-commands <file> Name of file with compile commands
--compile-for-tiered Generate profiling code for tiered compilation
--compile-with-assertions Compile with java assertions
--compile-threads <number> Number of compilation threads to be used
--ignore-errors Ignores all exceptions thrown during class loading
--exit-on-error Exit on compilation errors
--info Print information during compilation
--verbose Print verbose information
--debug Print debug information
--help Print this usage message
--version Version information
-J<flag> Pass <flag> directly to the runtime system
jaotc (on-line help)
- 56. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
56
$ jaotc --info --output libHelloAOT.so HelloAoT.class
Compiling libHelloAOT...
1 classes found (30 ms)
2 methods total, 2 methods to compile (4 ms)
Compiling with 8 threads
.
2 methods compiled, 0 methods failed (409 ms)
Parsing compiled code (1 ms)
Processing metadata (15 ms)
Preparing stubs binary (1 ms)
Preparing compiled binary (1 ms)
Creating binary: libHelloAOT.o (8 ms)
Creating shared library: libHelloAOT.so (13 ms)
Total time: 912 ms
jaotc (--info flag)
- 57. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
57
$ jaotc --info --compile-with-assertions --output libHelloAOT.so HelloAoT.class
jaotc (--compile-with-assertions flag)
- 58. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
58
$ jaotc --compile-commands cc.txt --output libHelloAOT.so HelloAoT.class
$ cat cc.txt
exclude org.sample.code.MyClass.method*
exclude org.sample.code.MyClass.<init>
jaotc (--compile-commands flag)
- 59. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
59
$ jaotc --output libjava.base.so --module java.base
jaotc (module)
- 60. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
60
$ jaotc --output libjava.base.so --jar my-spiffy-app.jar
jaotc (jar file)
- 61. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
61
$ jaotc -J-XX:-UseCompressedOop --output libHelloAOT.so HelloAoT.class
意外に大事なオプション!(後ほど説明を。。。)
jaotc (VM options)
- 62. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
62
$ java -XX:AOTLibrary=./libHelloAoT.so HelloAoT
実行時
- 63. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
63
$ java -XX:AOTLibrary=./libHelloAoT.so HelloAoT
Hello AoT!
$
実行時
- 64. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
64
$ java -XX:+PrintAOT -XX:AOTLibrary=./libHelloAoT.so HelloAoT
実行時 (-XX:+/-PrintAOT)
- 65. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
65
$ builds/jdk-9/bin/java -XX:+PrintAOT -XX:AOTLibrary=./libHelloAoT.so HelloAoT
13 1 loaded ./libHelloAoT.so aot library
76 1 aot[ 1] HelloAoT.<init>()V
76 2 aot[ 1] HelloAoT.main([Ljava/lang/String;)V
Hello AoT!
$
実行時 (-XX:+/-PrintAOT)
- 66. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
66
$ java -XX:-UseCompressedOops -XX:+PrintAOT -XX:AOTLibrary=./libHelloAoT.so HelloAoT
実行時 (圧縮 OOP を無効)
- 67. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
67
$ java -XX:-UseCompressedOops -XX:+PrintAOT -XX:AOTLibrary=./libHelloAoT.so HelloAoT
Shared file ./libHelloAoT.so error: UseCompressedOops has different value 'true' from
current 'false'
7 1 skipped ./libHelloAoT.so aot library
Hello AoT!
$
実行時 (圧縮 OOP を無効)
- 68. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
68
$ builds/jdk-9/bin/java -XX:-UseCompressedOops -XX:AOTLibrary=./libHelloAoT.so HelloAoT
Hello AoT!
$
実行時 (圧縮 OOP を無効)
- 69. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
69
$ jaotc -J-XX:-UseCompressedOop --output libHelloAOT.so HelloAoT.class
jaotc (VM options を合わせましょう)
圧縮されたOOP 普通の OOP
G1 -J-XX:+UseCompressedOops
-J-XX:+UseG1GC
-J-XX:-UseCompressedOops
-J-XX:+UseG1GC
ParallelGC -J-XX:+UseCompressedOops
-J-XX:+UseParallelGC
-J-XX:-UseCompressedOops
-J-XX:+UseParallelGC
- 70. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
70
まとめ
• 価値
– Peak performance までの時間を短縮 ★
– 複数の JVM のメモリ使用量を減らす ★
– JITを使えないプラットフォームでも利用できる ★
• 注意点
– 実験的なもので、サポートされていない
– 正式なドキュメントはない
– AMD64 の Linux しか動かない
– Invoke dynamic や カスタムクラスローダーをサポートしない
- 71. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
71
まとめ
• 価値
– Peak performance までの時間を短縮 ★
– 複数の JVM のメモリ使用量を減らす ★
– JITを使えないプラットフォームでも利用できる ★
• 注意点
– 実験的なもので、サポートされていない
– 正式なドキュメントはない
– AMD64 の Linux しか動かない
– Invoke dynamic や カスタムクラスローダーをサポートしない
★ 場合があります。