hotspotコトハジメ

Post on 16-Jul-2015

2.222 Views

Category:

Software

8 Downloads

Preview:

Click to see full reader

TRANSCRIPT

末永 恭正 @YaSuenag

今日はGCの話をあまりしません!他の方の資料等をご覧ください…

OracleとJavaは、Oracle Corporation及びその子会社、関連会社の米国及びその他の国における登録商標です。文中の社名、商品名等は各社の商標または登録商標である場合があります。本資料の実装確認はOpenJDK8 8u-dev(HotSpot rev. 7164:bf68e15dc8fe)で行っています。

ハードウェアをちょっと気にしてHotSpotを気持ちよく動かしてあげよう!

※AMD64 Linuxを中心にお話しします

http://icedtea.classpath.org/wiki/HeapStats/jp

$ java -versionopenjdk version "1.8.0_31"OpenJDK Runtime Environment (build 1.8.0_31-b13)OpenJDK 64-Bit Server VM (build 25.31-b07, mixed mode)

$ java -versionjava version "1.8.0"Java(TM) SE Runtime Environment (build 1.8.0-b132)Java HotSpot(TM) Client VM (build 25.0-b70, mixed mode)

• http://docs.oracle.com/javase/jp/8/technotes/guides/vm/index.html

• http://otndnld.oracle.co.jp/ondemand/javaday2014/pdf/C

2-JavaDay-304328.pdf

• http://docs.oracle.com/javase/jp/8/technotes/guides/vm/s

erver-class.html

注: Java SE 6の場合、server-classマシンとは、少なくとも2つのCPU

と、最少でも2Gバイトの物理メモリーを備えているマシンを指します。

• MetaspaceはGC対象ではありません!

ちょっとだけ

• http://www.slideshare.net/YujiKubota/concurrent-

marksweep-garbage-collection

• http://www.slideshare.net/YaSuenag/metaspace

• CodeCacheはGCでクリアされません!

ちょっとだけ

Java HotSpot(TM) 64-Bit Server VM warning: CodeCache is full. Compiler has been disabled.Java HotSpot(TM) 64-Bit Server VM warning: Try increasing the code cache size using -XX:ReservedCodeCacheSize=CodeCache: size=2496Kb used=1955Kb max_used=1969Kb free=540Kbbounds [0x00007f50c844b000, 0x00007f50c86bb000, 0x00007f50c86bb000]total_blobs=578 nmethods=353 adapters=139compilation: disabled (not enough contiguous free space left)

• http://www.oracle.com/webfolder/technetwork/jp/javama

gazine/Java-JA13-Architect-evans.pdf

$ ulimit -v 102400$ /usr/local/jdk1.8.0/bin/java -version## There is insufficient memory for the Java Runtime Environment to continue.# Native memory allocation (malloc) failed to allocate 80 bytes for AllocateHeap# An error report file with more information is saved as:# /home/xxx/hs_err_pid8664.log

• http://www.oracle.com/technetwork/jp/articles/java/compressedoops-

427542-ja.html

# cat /sys/kernel/mm/transparent_hugepage/enabled

always [madvise] never

import java.nio.*;

import java.nio.file.*;

import javax.crypto.*;

import javax.crypto.spec.*;

import java.security.*;

public class AESTest{

public static void main(String[] args) throws Exception{

Key key = new SecretKeySpec("passwordpassword".getBytes(), "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");

cipher.init(Cipher.ENCRYPT_MODE, key);

byte[] plain = Files.readAllBytes(Paths.get(args[0]));

long start = System.currentTimeMillis();

cipher.doFinal(plain);

long end = System.currentTimeMillis();

long elapsedTime = end - start;

System.out.println("Elapsed time: " + elapsedTime + " ms");

}

}

$ java -Xms2g -Xmx2g AESTest /tmp/test.dat

Elapsed time: 1654 ms$ java -Xms2g -Xmx2g -XX:+UseTransparentHugePages AESTest /tmp/test.dat

Elapsed time: 1613 ms

約40msの差!

ソフトウェア環境:• Fedora21 x86_64

• kernel-3.18.7-200.fc21.x86_64• glibc-2.20-8.fc21.x86_64

• java-1.8.0-openjdk-1.8.0.31-5.b13.fc21.x86_64

ハードウェア環境:• CPU:Intel Core i5 2500T• MB:GIGABYTE GA-H67A-UD3H-B3• メモリ:DDR3 1333MHz 2GB×2

テストデータ:• /tmp/test.dat

• /dev/urandomから500MBを/tmp配下にコピー• /tmpはtmpfs

$ perf stat -e dTLB-load-misses,dTLB-store-misses java -Xms2g -Xmx2g AESTest /tmp/test.datElapsed time: 1654 ms

Performance counter stats for 'java -Xms2g -Xmx2g AESTest /tmp/test.dat':

826,055 dTLB-load-misses1,648,456 dTLB-store-misses

2.300445236 seconds time elapsed

$ perf stat -e dTLB-load-misses,dTLB-store-misses java -Xms2g -Xmx2g -XX:+UseTransparentHugePages AESTest /tmp/test.datElapsed time: 1613 ms

Performance counter stats for 'java -Xms2g -Xmx2g -XX:+UseTransparentHugePages AESTest /tmp/test.dat':

623,795 dTLB-load-misses576,097 dTLB-store-misses

2.165985041 seconds time elapsed

TLBミスヒットが激減!

論理プロセッサ数が8以下のとき

論理プロセッサ数が9以上のとき

論理プロセッサ数と同数

8 +5 <論理プロセッサ数 > −8

8

• http://openjdk.java.net/jeps/157

• http://openjdk.java.net/projects/code-tools/jol/

import sun.misc.Contended;

@Contended

public class Test{

public int val1;

public int val2;

public int val3;

public int val4;

public int val5;

}

$ java -cp jol-cli-0.3.1-full.jar:. \org.openjdk.jol.Main internals Test

:Test object internals:OFFSET SIZE TYPE DESCRIPTION VALUE

:12 4 int Test.val1 016 4 int Test.val2 020 4 int Test.val3 024 4 int Test.val4 028 4 int Test.val5 0

Instance size: 32 bytes (reported by Instrumentation API)Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

$ java -XX:-RestrictContended -cp jol-cli-0.3.1-full.jar:. \org.openjdk.jol.Main internals Test

:Test object internals:OFFSET SIZE TYPE DESCRIPTION VALUE

12 128 (alignment/padding gap) N/A140 4 int Test.val1 0144 4 int Test.val2 0148 4 int Test.val3 0152 4 int Test.val4 0156 4 int Test.val5 0

160 128 (loss due to the next object alignment)

Instance size: 288 bytes (reported by Instrumentation API)Space losses: 128 bytes internal + 128 bytes external = 256 bytes total

@Contended無効 @Contended有効

ソフトウェア環境:• Fedora21 x86_64

• kernel-3.18.7-200.fc21.x86_64• glibc-2.20-8.fc21.x86_64

• java-1.8.0-openjdk-1.8.0.31-5.b13.fc21.x86_64

import sun.misc.Contended;

public class Test2{

@Contended("group")

public int val1;

@Contended("group")

public int val2;

public int val3;

public int val4;

@Contended

public int val5;

}

$ java -cp jol-cli-0.3.1-full.jar:. \org.openjdk.jol.Main internals Test2

:Test2 object internals:OFFSET SIZE TYPE DESCRIPTION VALUE

:12 4 int Test2.val1 016 4 int Test2.val2 020 4 int Test2.val3 024 4 int Test2.val4 028 4 int Test2.val5 0

Instance size: 32 bytes (reported by Instrumentation API)Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

$ java -XX:-RestrictContended -cp jol-cli-0.3.1-full.jar:. \org.openjdk.jol.Main internals Test2

:Test2 object internals:OFFSET SIZE TYPE DESCRIPTION VALUE

:12 4 int Test2.val3 016 4 int Test2.val4 0

20 128 (alignment/padding gap) N/A148 4 int Test2.val5 0152 128 (alignment/padding gap) N/A280 4 int Test2.val1 0284 4 int Test2.val2 0288 128 (loss due to the next object alignment)

Instance size: 416 bytes (reported by Instrumentation API)Space losses: 256 bytes internal + 128 bytes external = 384 bytes total

@Contended無効 @Contended有効

ソフトウェア環境:• Fedora21 x86_64

• kernel-3.18.7-200.fc21.x86_64• glibc-2.20-8.fc21.x86_64

• java-1.8.0-openjdk-1.8.0.31-5.b13.fc21.x86_64

• http://www.oracle.com/technetwork/java/faq-sun-packages-

142232.html

sun.miscパッケージです!

UseSSE

UseAVX

UseAES

UseCLMUL

UseXmmLoadAndClearUpper UseXmmRegToRegMoveAll

UseXMMForArrayCopy

UseUnalignedLoadStores

UseCountLeadingZerosInstruction

UseCountTrailingZerosInstruction

UseBMI1Instructions

UseBMI2Instructions

UsePopCountInstructionUseFastStosb

AllocatePrefetchInstr

UseRTMLocking

UseRTMForStackLocks

UseSSE42Intrinsics

UseRTMXendForLockBusy

import java.nio.*;

import java.nio.file.*;

import javax.crypto.*;

import javax.crypto.spec.*;

import java.security.*;

public class AESTest{

public static void main(String[] args) throws Exception{

Key key = new SecretKeySpec("passwordpassword".getBytes(), "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");

cipher.init(Cipher.ENCRYPT_MODE, key);

byte[] plain = Files.readAllBytes(Paths.get(args[0]));

long start = System.currentTimeMillis();

cipher.doFinal(plain);

long end = System.currentTimeMillis();

long elapsedTime = end - start;

System.out.println("Elapsed time: " + elapsedTime + " ms");

}

}

$ java -Xms2g -Xmx2g AESTest /tmp/test.dat

Elapsed time: 1669 ms$ java -Xms2g -Xmx2g -XX:-UseAES AESTest /tmp/test.dat

Elapsed time: 4651 ms

ソフトウェア環境:• Fedora21 x86_64

• kernel-3.18.7-200.fc21.x86_64• glibc-2.20-8.fc21.x86_64

• java-1.8.0-openjdk-1.8.0.31-5.b13.fc21.x86_64

約3秒差!

テストデータ:• /tmp/test.dat

• /dev/urandomから500MBを/tmp配下にコピー• /tmpはtmpfs

ハードウェア環境:• CPU:Intel Core i5 2500T• MB:GIGABYTE GA-H67A-UD3H-B3• メモリ:DDR3 1333MHz 2GB×2

$ java -XX:+UnlockDiagnosticVMOptions -XX:+PrintIntrinsics AESTest /tmp/test.dat@ 64 com.sun.crypto.provider.AESCrypt::encryptBlock (953 bytes) (intrinsic)@ 80 java.lang.System::arraycopy (0 bytes) (intrinsic)

Elapsed time: 1669 ms

$ java -XX:+UnlockDiagnosticVMOptions -XX:+PrintIntrinsics -XX:-UseAES AESTest /tmp/test.dat@ 80 java.lang.System::arraycopy (0 bytes) (intrinsic)

Elapsed time: 4651 ms

• http://www.intel.co.jp/content/www/jp/ja/architecture-and-

technology/advanced-encryption-standard--aes-/data-protection-aes-

general-technology.html

→AES-NI命令セットを活用した機械語がJITで適用された!

• http://openjdk.java.net/projects/sumatra/

• https://wiki.openjdk.java.net/display/Sumatra/Main

https://wiki.openjdk.java.net/display/Sumatra/Standalone+Sumatra+Stream+API+Offload+Demo

final int length = 8;

int[] ina = new int[length];

int[] inb = new int[length];

int[] out = new int[length];

// Initialize the input arrays - this is offloadable

IntStream.range(0, length).parallel().forEach(p -> {

ina[p] = 1;

inb[p] = 2;

});

// Sum each pair of elements into out[] - this is offloadable

IntStream.range(0, length).parallel().forEach(p -> {

out[p] = ina[p] + inb[p];

});

We cannot speak for Project Sumatra or AMD. Project Graal decided to temporarily

remove the compiler's GPU support, because the original contributors

indicated to no longer maintain the code. We are still investigating outside

the main branch a new version of compilation support for heterogenous

architectures for Graal. If you want to compile and run a Graal version with GPU support in

the meantime, use this revision: http://hg.openjdk.java.net/graal/graal/rev/91fee1fab96d

<http://hg.openjdk.java.net/graal/graal/rev/91fee1fab96d>.

http://mail.openjdk.java.net/pipermail/sumatra-dev/2015-February/000308.html

HSA/PTXサポート削除

GPUを使いたければこのタグを使用→

• ツールの動作を知ることがHotSpotを知る一番の近道!

• http://www.slideshare.net/YaSuenag/serviceability-tools

• http://www.oracle.com/webfolder/technetwork/jp/javama

gazine/Java-JA12-architect-bajaj.pdf

HotSpotがご機嫌ナナメになったら取るべき情報は取っておこう!

• http://www.slideshare.net/agetsuma/java-34815905

• だいたい

• クラッシュ解析の最初の取っ掛かり!

Current thread (0x00007fb944008800): JavaThread "main" [_thread_in_native, id=10054, stack(0x00007fb9486b0000,0x00007fb9487b1000)]

siginfo: si_signo: 11 (SIGSEGV), si_code: 1 (SEGV_MAPERR), si_addr: 0x0000000000000000

Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)

C [libsegv.so+0x6a0] Java_NativeSEGV_doSEGV+0x18j NativeSEGV.doSEGV()V+0j NativeSEGV.main([Ljava/lang/String;)V+0v ~StubRoutines::call_stubV [libjvm.so+0x681a56] JavaCalls::call_helper(JavaValue*, methodHandle*, JavaCallArguments*, Thread*)+0x1056V [libjvm.so+0x6c36c2] jni_invoke_static(JNIEnv_*, JavaValue*, _jobject*, JNICallType, _jmethodID*, JNI_ArgumentPusher*, Thread*)+0x362V [libjvm.so+0x6e00ca] jni_CallStaticVoidMethod+0x17aC [libjli.so+0x7bcc] JavaMain+0x80cC [libpthread.so.0+0x752a]

• https://bugs.openjdk.java.net/

:compile java/lang/invoke/MethodHandle <clinit> ()V -1 4 inline 1 0 -1 java/lang/invoke/MethodHandle <clinit> ()V

Current CompileTask:C2: 13427 2 !b 4 java.lang.invoke.MethodHandle::<clinit> (75 bytes)

• https://github.com/YaSuenag/JVMLive

top related