Skip to content

Commit

Permalink
Merge pull request #9 from fourlastor-alexandria/zgc-support
Browse files Browse the repository at this point in the history
Add support for zgc
  • Loading branch information
fourlastor committed Nov 24, 2023
2 parents e6cae96 + 3d78105 commit 25a3df1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ java-locator = "0.1.5"
jni = { version = "0.21.1", features = ["invocation"] }
serde = { version = "1.0.190", features = ["derive"] }
serde_json = "1.0.108"
windows-version = "0.1.0"

[package.metadata.cross.target.x86_64-pc-windows-msvc]
image = "ghcr.io/cross-rs/x86_64-pc-windows-msvc-cross:local"
Expand Down
30 changes: 26 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#![cfg_attr(not(feature = "win_console"), windows_subsystem = "windows") ]
#![cfg_attr(not(feature = "win_console"), windows_subsystem = "windows")]
use jni::{objects::JString, InitArgsBuilder, JNIVersion, JavaVM};
use serde::Deserialize;
use std::{env, fs, path::{PathBuf, Path}};
use std::{
env, fs,
path::{Path, PathBuf},
};

#[allow(non_snake_case)]
#[derive(Deserialize)]
struct Config {
classPath: Vec<String>,
mainClass: String,
vmArgs: Vec<String>,
useZgcIfSupportedOs: bool,
}

// Picks discrete GPU on Windows, if possible
Expand Down Expand Up @@ -39,6 +43,7 @@ fn start_jvm(
class_path: Vec<String>,
main_class: &str,
vm_args: Vec<String>,
use_zgc_if_supported: bool,
args: Vec<String>,
) {
let mut args_builder = InitArgsBuilder::new()
Expand All @@ -53,13 +58,18 @@ fn start_jvm(
args_builder = args_builder.option(arg);
}

if use_zgc_if_supported && is_zgc_supported() {
args_builder = args_builder
.option("-XX:+UnlockExperimentalVMOptions")
.option("-XX:+UseZGC")
}

// Build the VM properties
let jvm_args = args_builder.build().expect("Failed to buid VM properties");

append_library_paths(jvm_location);
// Create a new VM
let jvm = JavaVM::new(jvm_args)
.expect("Failed to create a new JavaVM");
let jvm = JavaVM::new(jvm_args).expect("Failed to create a new JavaVM");

let mut env = jvm
.attach_current_thread()
Expand Down Expand Up @@ -137,7 +147,18 @@ fn append_library_paths_os(jvm_location: &str) {
}
}

#[cfg(target_os = "windows")]
fn is_zgc_supported() -> bool {
// Windows 10 1803 is required for ZGC, see https://wiki.openjdk.java.net/display/zgc/Main#Main-SupportedPlatforms
// Windows 10 1803 is build 17134.
use windows_version::OsVersion;
return OsVersion::current() >= OsVersion::new(10, 0, 0, 17134);
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
fn is_zgc_supported() -> bool {
return true;
}

fn main() {
let args: Vec<String> = env::args().collect();
Expand All @@ -153,6 +174,7 @@ fn main() {
class_path,
&config.mainClass.replace(".", "/"),
config.vmArgs,
config.useZgcIfSupportedOs,
args,
);
}

0 comments on commit 25a3df1

Please sign in to comment.