GraalVM etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
GraalVM etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

17 Şubat 2022 Perşembe

GraalVM Truffle - Polyglot Runtime

Giriş
Bu bileşen aynı zamanda Espresso Project olarak ta biliniyor. Açıklaması şöyle. Yani GraalVM iki şekilde kullanılabiliyordu. 
1. Ya Native Image oluşturuluyor 
2. Ta da kendi içinde barındırdığı Java HotSpot VM için JIT yapıyordu. 

Artık JVM olarak ta kullanılabilir
Up until now, GraalVM has offered two ways to run Java programs: using the Java HotSpot VM with the GraalVM JIT (just-in-time) compiler, and compiled to a native executable using GraalVM Native Image.

Today, we’re happy to announce a new way to run Java on GraalVM. GraalVM 21.0 introduces a new installable component, named espresso, which provides a JVM implementation written in Java.
Açıklama şöyle. Yani Truffle diğer dilleri de çalıştırabiliyor
One of the newer VMs is GraalVM. I'm not sure it's completely correct to call it a JVM because it can (supposedly) also run other languages such as Python, Ruby, C, and C++.
Espresso bileşeni "Truffle Language Implementation Framework" kullanılarak yazılmış bir JVM. Şeklen şöyle
Şeklen şöyle


Bazı yeni özellikler şöyle
Java on Truffle enables some interesting use cases. You can embed Java bytecode of a different version in your main Java application (given you have both distributions), dynamically execute Java code in native executable built with Native Image, use it for advanced class redefinition, get access to all Truffle tools and interoperability, and more.

Over the year we’ve done a lot of work on Java on Truffle in terms of both features, like the new Hotswap Plugin API, which allows reloading the code without the need for restarting a running application, and performance — most recently on startup performance in a wide range of benchmarks. Stay tuned for more updates in 2022!
Truffle Artık Ayrıca İndirilebiliyor
Açıklaması şöyle
GraalVM’s polyglot language runtime, Truffle, and language runtimes are now decoupled from the GraalVM JDK. Standalone language runtimes are available for download in native and JVM standalone versions. For embedding in Java applications, you can download all artifacts from Maven Central, which will work for GraalVM JDK and any other compatible JDK.
Polyglot Embedding
Açıklaması şöyle
Previously, to embed GraalVM languages in Java applications, you had to install them using the gu tool. Now all languages are available as Maven dependencies. 
Örnek
Java içinde Python kullanmak için şöyle yaparız
<dependency>
  <groupId>org.graalvm.polyglot</groupId>
  <artifactId>polyglot</artifactId>
  <version>23.1.0</version>
</dependency>
<dependency>
  <groupId>org.graalvm.polyglot</groupId>
  <artifactId>python</artifactId>
  <version>23.1.0</version>
  <type>pom</type>
</dependency>
Örnek
Java içinde Javascript kullanmak için şöyle yaparız
<dependency>
  <groupId>org.graalvm.polyglot</groupId>
  <artifactId>js</artifactId>
  <version>23.1.0</version>
  <type>pom</type>
</dependency>


10 Mart 2021 Çarşamba

GraalVM native-image komutu - Compile To Native Executable

Giriş
Compile To Native Executable içindir. Bu komutu kullanabilmek için için önce native-image bileşenini indirip kurmak gerekiyor. Kurulum için şöyle yaparız
<graalvm-home>/bin/gu install native-image
Açıklaması şöyle
Short hint for Windows users: Native Image builds are platform dependent. This means that will only work in the platform specific command line (e.g. it will not work in Git Bash).
Bütün seçenekleri güzelce açıklayan bir yazı burada.
Bir başka bir yazı burada.

Ne İşe Yarar
.class dosyalarını derler ve bir native image oluşturur

Örnek
Şöyle yaparız
> javac  HelloWorld.java
> native-image  HelloWorld

Maven Plugin
native plugin yazısına taşıdım

Gradle Plugin
Şöyle yaparız
plugins {
    id 'application'
    id 'java'
    id 'org.graalvm.buildtools.native' version '0.9.16'
}


group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

application {
    mainClass = 'org.example.Main'
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
    useJUnitPlatform()
}
IntelliJ ile nativeCompile seçeneğine çift tıklarız. Şeklen şöyle


-jar seçeneği
Örnek
Şöyle yaparız
native-image -jar MyApp.jar ./myApp
--native-image seçeneği
Örnek
Açıklaması şöyle
To check the building environment, native-toolchain information, and settings applied while building an image, use the --native-image-info option...
Şöyle yaparız
native-image --native-image-info -jar App.jar
-cp seçeneği
Örnek
Şöyle yaparız
native-image [options] -cp jar:com/package/**/myApp.jar MyApp [imagename]

17 Şubat 2021 Çarşamba

GraalVM gu komutu - Package Manager

Giriş
Açıklaması şöyle
Its package manager, called gu, lets users install other languages.
available seçeneği
Kurulabilecek bileşenleri gösterir

Örnek
Şöyle yaparız
$ gu available
Downloading: Component catalog from www.graalvm.org
ComponentId              Version             Component name                Stability                     Origin
---------------------------------------------------------------------------------------------------------------------------------
espresso                 22.3.0              Java on Truffle               Experimental                  github.com
espresso-llvm            22.3.0              Java on Truffle LLVM Java librExperimental                  github.com
js                       22.3.0              Graal.js                      Supported                     github.com
llvm                     22.3.0              LLVM Runtime Core             Supported                     github.com
llvm-toolchain           22.3.0              LLVM.org toolchain            Supported                     github.com
native-image             22.3.0              Native Image                  Early adopter                 github.com
native-image-llvm-backend22.3.0              Native Image LLVM Backend     Early adopter (experimental)  github.com
nodejs                   22.3.0              Graal.nodejs                  Supported                     github.com
python                   22.3.0              GraalVM Python                Experimental                  github.com
R                        22.3.0              FastR                         Experimental                  github.com
ruby                     22.3.0              TruffleRuby                   Experimental                  github.com
visualvm                 22.3.0              VisualVM                      Experimental                  github.com
wasm                     22.3.0              GraalWasm                     Experimental                  github.com

list seçeneği
Kurulu bileşenleri gösterir
Örnek
Şöyle yaparız
$ gu list
ComponentId   Version      Component name         Stability            Origin
----------------------------------------------------------------------------------------
graalvm       22.3.0       GraalVM Core           Supported
install seçeneği
Örnek
native-image bileşenini kurmak için şöyle yaparız
<graalvm-home>/bin/gu install native-image
Örnek
Şöyle yaparız
gu install espresso
gu install -L espresso-installable.jar # if downloaded manually

16 Kasım 2020 Pazartesi

GraalVM - Oracle Tarafından Geliştiriliyor

Giriş
GraalVM Oracle tarafından geliştiriliyor. 2018 yılının Nisan ayında duyuruldu. Ancak tarihçesi şöyle
The history of Graal dates back to the research works on MaxineVM, also known as a meta-circular virtual machine. If this sounds complicated then all you really have to understand is that MaxineVM is a Java virtual machine written in… Java (hence it is meta-circular).
Mayıs 2019'dan beri de ticari kullanıma hazır. Açıklaması şöyle
GraalVM is an Oracle project and has been in production-ready status since May 2019. This means that it has been extensively tested and is considered stable enough for use in production environments.
Graal Ne Demek?
Açıklaması şöyle
The word “Graal” comes from old French for “Grail.” The “VM” in “GraalVM” comes from the fact that it runs inside the JVM.
GraalVM Kurulum
Açıklaması şöyle
You can install GraalVM on your machine in several ways.

- You can download the official releases from their GitHub page. They support several platforms (including Apple Silicon!) and two versions of Java. After that, you can follow this guide for your respective platform.

- Another option if you have a UNIX-like system, like Linux or MacOS, is to use SDKMan to manage your installed SKDs. It’s a great tool to manage multiple java installations and switch between them with ease. Think of this tool as NVM for Java. Follow this link to learn more about SDKMan and about how to install it.
Örnek - MacOS
Şöyle yaparız
sdk install java 22.3.r17-grl
Kurulumu test etmek için şöyle yaparız
$ java -version
openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment GraalVM CE 22.3.0 (build 17.0.5+8-jvmci-22.3-b08)
OpenJDK 64-Bit Server VM GraalVM CE 22.3.0 (build 17.0.5+8-jvmci-22.3-b08, 
mixed mode, sharing)
JAVA_HOME ve PATH değişkenleri için şöyle yaparız
# Linux
export JAVA_HOME=<graalvm>

# macOS
export JAVA_HOME=/Library/Java/JavaVirtualMachines/<graalvm>/Contents/Home

# Windows
setx /M JAVA_HOME "C:\Progra~1\Java\<graalvm>"

# Linux
export PATH=<graalvm>/bin:$PATH

# macOS
export PATH=/Library/Java/JavaVirtualMachines/<graalvm>/Contents/Home/bin:$PATH

# Windows
setx /M PATH "C:\Progra~1\Java\<graalvm>\bin;%PATH%"
Sürümleri
İki sürüm var. Bunlar şöyle 
1. Community Edition 
2. Enterprise Edition

Not : Yeni paket kurmak için GraalVM gu komutu yazısına bakabilirsiniz.

1. Internal JDK
Açıklaması şöyle. Yani GraalVM'i aynen JDK gibi kullanmak mümkün, çünkü GraalVM kendi içerisinde bir HotSpot  JVM barındırıyor. 
Foremost, it’s good to remember that the GraalVM distributions contain a JDK with all the usual JDK tools, meaning you can use GraalVM as your JDK. 
...
When running Java applications just-in-time with GraalVM, the underlying JVM is the usual Java Hotspot (tm) VM, which means that most of the configuration options are equivalent,...
Bu kullanımda HotSpot VM'deki C2 derleyici yani JIT yerine Graal derleyici kullanılıyor.. Şeklen şöyle

Açıklaması şöyle
GraalVM includes two compilers — the GraalVM compiler and the GraalVM Native Image. The GraalVM compiler is an advanced JIT optimizing compiler that replaces the old C++ written HotSpot Java Virtual Machine. It is written in Java itself and designed to be modular, maintainable, and extendable. The GraalVM compiler generates faster and leaner code that requires fewer compute resources.
Truffle
Truffle ile GraalVM de kendi içinde barındırdığı Java HotSpot VM'e başka alternatifler de sunmaya başlamış. GraalVM Truffle yazısına bakabilirsiniz. 

2. Compile To Native Executable
Açıklaması şöyle. Böylece artık JVM'e gerek kalmadan uygulama çalıştırılabiliyor Buna aynı zamanda ahead-of-time compilation da deniliyor.
What excites people so much about GraalVM is a sub-project called SubstrateVM (SVM), and that compiles JVM applications to native executables. This is also called a ahead-of-time compiler.

Once you compile a JVM application into a native executable, it can run without a JVM. In fact, what you get is a self-contained executable just like you would get with Go.
Native Executable Dezavantajları
1. Çok büyük bellek kullanan uygulamalar için iyi değil.
2. Artık Java Agents, Java Management Extensions (JMX), and JDK Flight Recorder (JFR) gibi bir çok araç olmuyor
3. GC olarak SerialGC var. Açıklaması şöyle
Additionally, it has a much simpler garbage collection (SerialGC) to start with and generates Image Heap during the native-image build, resulting in lower memory footprint. But we got to select a GC as we prefer. However, it’s worth noting that some GC options, like G1GC, are only available in the Enterprise version of GraalVM at the time of writing.
Substrate VM
Native Image olarak derlenen uygulamalar halen bir runtime katmanına ihtiyaç duyuyor. Bunu da  SubstrateVM sağlıyor.  Açıklaması şöyle
The GraalVM native image compiler compiles Java applications into standalone native executable that include the application classes, classes from its dependencies, runtime library classes, and statically linked native code from JDK. These executable do not run on the JVM; instead, they use a different runtime system called the “Substrate VM.”

This runtime system includes all necessary components like memory management, thread scheduling, and more. As a result, the resulting program has a faster startup time and lower runtime memory overhead compared to a JVM, making it ideal for container and serverless applications.
Şeklen şöyle


Debugging
Açıklaması şöyle
However, the minimalist VM, SubstrateVM, used in the native executable does not support JVM-TI, and thus Java debuggers cannot be used for native executable debugging.

To debug the native executable, developers need to use an OS-level system debugger such as GDB.


Pluginler
Bir başka açıklama şöyle. Yani Maven veya Gradle için pluginler var. Ayrıca gereksiz kodları native executable dosyasına dahil etmiyor.
  • Compiles Java code ahead-of-time into a native executable file.
  • Only the code that is required at run time by the application (a fraction of the resources required by the JVM) is included in the executable file.
  • While the AOT compiler compiles Java code into a native executable, it will analyze its dependencies, dependent JDK libraries and VM components and removes all unused classes, method, and fields from your application and libraries.
  • This process makes reverse engineering difficult by converting Java bytecode into native machine code. Potential attack surfaces are also minimized.
  • Supported by many microservice frameworks: Micronaut, Spring, Helidon, Quarkus, etc.
  • Maven and Gradle plugins for Native Image so you can easily build, test (there is also JUnit testing support), and run Java applications as executable files.
  • Costs less to run
  • Fast startup, no warmup
  • Ideal for containers (can be packaged into a lightweight container image for faster and more efficient deployment)
Detayları native-image komutu yazısına taşıdım.

3. Build a Shared Library
Açıklaması şöyle
If instead of the executable you would like to build a shared library, you can do that passing the --shared option. You need to mark the methods to be exposed with the @CEntryPoint annotation,..
4. Build Statically Linked Binaries
Açıklaması şöyle
Another very useful possibility is to build statically linked binaries where the OS libraries like libc are linked into the executable. It’s even possible to pick which libc implementation to use. glibc is used by default, ...