# 2020q3 專題: Java 虛擬機器 ## 目標 1. 理解 Java 虛擬機器運作原理 2. 擴充[給定的 Java 虛擬機器](https://github.com/jserv/pitifulvm),得以執行 [Dhrystone](https://en.wikipedia.org/wiki/Dhrystone) 一類的工具 3. 研究其他開放原始碼 JVM 實作 ## 背景知識 * [Java Virtual Machine (JVM) & its Architecture](https://www.guru99.com/java-virtual-machine-jvm.html) * [Learn about JVM internals - what does the JVM do?](https://youtu.be/UwB0OSmkOtQ) ## Dhrystone for Java 1. 安裝 OpenJDK ```shell $ sudo apt install openjdk-8-jdk ``` 2. 取得 Dhrystone for Java 原始程式碼並編譯 ```shell $ wget http://www.okayan.jp/DhrystoneApplet/dhry_src.jar $ unzip dhry_src.jar $ javac dhry.java ``` 3. 執行 Dhrystone for Java,當看到 `Please give the number of runs through the benchmark` 字樣時,輸入 1000 (或更大的數值) ## [simple-jvm/dvm](https://github.com/jserv/simple-dvm) 1. 取得原始程式碼並編譯 ```shell git clone https://github.com/jserv/simple-dvm cd simple-dvm/jvm make ``` 2. 測試 ```shell ./jvm ../tests/Foo1.class ``` 3. 預期可見以下輸出 ``` HelloWorld -------------------- initial value random number x : 36 x = 36 y = 6345 c = 0 d = 23456 f = 0 -------------------- c = x + y = 36 + 6345 = 6381 d = d + c = 29837 x = c/2 = 3190 c = x * y = 3190 * 6345 = 20240550 d = d + c = 20270387 x = c/2 = 10120275 c = x - y = 10120275 - 6345 = 10113930 d = d + c = 30384317 x = c/2 = 5056965 c = x / y = 5056965 / 6345 = 797 d = d + c = 30385114 f = 30385114 + 5056965 + 6345 + 797 = 35449221 Foo Test By WJY ``` threaded interpreter > 約可改善 20% 效能 TODO: 1. 描述[給定的 Java 虛擬機器](https://github.com/jserv/pitifulvm) 欠缺哪些功能/特徵/元素 - Constant Pool - CONSTANT_Float_info - CONSTANT_Long_info - CONSTANT_Double_info - CONSTANT_String_info - CONSTANT_Methodref_info - CONSTANT_InterfaceMethodref_info - Bytecode - object related instruction - invoke (e.g. `invokevirtual`, `invokeinterface`) - field (e.g. `getstatic`, `getfield`) - create (`new`) - array related instruction (e.g. `anewarray`, `arraylength`) - stack related instruction (`pop`, `dup`, `swap`) - other data type (float, double, long) instruction (e.g. `fload`, `dadd`) - bitwise instruction (e.g. `ior`, `iand`) - convert instruction (e.g. `i2b`, `i2d`) - exception (`athrow`) - Class Loader - Bootstrap Class Loader - Extension Class Loader - System Class Loader - Bytecode Verifier - JIT Compiler - Code Optimizations - Garbage Collection - Runtime Data Area - Native Method Stack - Heap - Method Area 2. [Learn about JVM internals - what does the JVM do?](https://youtu.be/UwB0OSmkOtQ) 紀錄不理解的部分 * [閱讀筆記](https://hackmd.io/@hankluo6/S1NV6DwnP) :::danger 關於欠缺的功能/特徵,需要描述更細緻,例如缺少的 Java bytecode, JNI, class field 解析等等。一旦完整列出,才能分別給予不同的優先權,隨後再著手改進。 :notes: jserv ::: Dhrystone: https://en.wikipedia.org/wiki/Dhrystone --- ### [開發紀錄](https://hackmd.io/@hankluo6/pitifulVM)