From 8a572879b9109745e945ecb7c5c5dd44459b900e Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 7 Nov 2024 15:10:51 +0100 Subject: [PATCH 1/3] [GR-58225] Update to latest graal and JDK to get a libgraal fix --- common.json | 16 ++++++++-------- mx.truffleruby/suite.py | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/common.json b/common.json index c867b8da1b68..b1ff34e7c501 100644 --- a/common.json +++ b/common.json @@ -8,7 +8,7 @@ "COMMENT.jdks": "When adding or removing JDKs keep in sync with JDKs in ci/common.jsonnet", "jdks": { - "galahad-jdk": {"name": "jpg-jdk", "version": "24", "build_id": "jdk-24+22-2651", "platformspecific": true, "extrabundles": ["static-libs"]}, + "galahad-jdk": {"name": "jpg-jdk", "version": "24", "build_id": "jdk-24+23-2783", "platformspecific": true, "extrabundles": ["static-libs"]}, "oraclejdk17": {"name": "jpg-jdk", "version": "17.0.7", "build_id": "jdk-17.0.7+8", "platformspecific": true, "extrabundles": ["static-libs"]}, "labsjdk-ce-17": {"name": "labsjdk", "version": "ce-17.0.7+4-jvmci-23.1-b02", "platformspecific": true }, @@ -45,13 +45,13 @@ "oraclejdk23": {"name": "jpg-jdk", "version": "23", "build_id": "jdk-23+37", "platformspecific": true, "extrabundles": ["static-libs"]}, - "oraclejdk-latest": {"name": "jpg-jdk", "version": "24", "build_id": "jdk-24+22", "platformspecific": true, "extrabundles": ["static-libs"]}, - "labsjdk-ce-latest": {"name": "labsjdk", "version": "ce-24+22-jvmci-b01", "platformspecific": true }, - "labsjdk-ce-latestDebug": {"name": "labsjdk", "version": "ce-24+22-jvmci-b01-debug", "platformspecific": true }, - "labsjdk-ce-latest-llvm": {"name": "labsjdk", "version": "ce-24+22-jvmci-b01-sulong", "platformspecific": true }, - "labsjdk-ee-latest": {"name": "labsjdk", "version": "ee-24+22-jvmci-b01", "platformspecific": true }, - "labsjdk-ee-latestDebug": {"name": "labsjdk", "version": "ee-24+22-jvmci-b01-debug", "platformspecific": true }, - "labsjdk-ee-latest-llvm": {"name": "labsjdk", "version": "ee-24+22-jvmci-b01-sulong", "platformspecific": true } + "oraclejdk-latest": {"name": "jpg-jdk", "version": "24", "build_id": "jdk-24+23", "platformspecific": true, "extrabundles": ["static-libs"]}, + "labsjdk-ce-latest": {"name": "labsjdk", "version": "ce-24+23-jvmci-b01", "platformspecific": true }, + "labsjdk-ce-latestDebug": {"name": "labsjdk", "version": "ce-24+23-jvmci-b01-debug", "platformspecific": true }, + "labsjdk-ce-latest-llvm": {"name": "labsjdk", "version": "ce-24+23-jvmci-b01-sulong", "platformspecific": true }, + "labsjdk-ee-latest": {"name": "labsjdk", "version": "ee-24+23-jvmci-b01", "platformspecific": true }, + "labsjdk-ee-latestDebug": {"name": "labsjdk", "version": "ee-24+23-jvmci-b01-debug", "platformspecific": true }, + "labsjdk-ee-latest-llvm": {"name": "labsjdk", "version": "ee-24+23-jvmci-b01-sulong", "platformspecific": true } }, "eclipse": { diff --git a/mx.truffleruby/suite.py b/mx.truffleruby/suite.py index 7ce224f8c975..4fcbde241203 100644 --- a/mx.truffleruby/suite.py +++ b/mx.truffleruby/suite.py @@ -22,7 +22,7 @@ { "name": "regex", "subdir": True, - "version": "e38604e7078ca4f8c66fdd3f9c3fbf7ec835d894", + "version": "367e521345b28b954a071924b3b129ad6546e7ed", "urls": [ {"url": "https://github.com/oracle/graal.git", "kind": "git"}, {"url": "https://curio.ssw.jku.at/nexus/content/repositories/snapshots", "kind": "binary"}, @@ -31,7 +31,7 @@ { "name": "sulong", "subdir": True, - "version": "e38604e7078ca4f8c66fdd3f9c3fbf7ec835d894", + "version": "367e521345b28b954a071924b3b129ad6546e7ed", "urls": [ {"url": "https://github.com/oracle/graal.git", "kind": "git"}, {"url": "https://curio.ssw.jku.at/nexus/content/repositories/snapshots", "kind": "binary"}, From 95b122313d7a5602769ce780d0b6ba3173d72180 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 7 Nov 2024 15:15:22 +0100 Subject: [PATCH 2/3] [GR-52676] Enable the Panama NFI backend by default --- CHANGELOG.md | 1 + src/main/java/org/truffleruby/options/Options.java | 2 +- src/options.yml | 2 +- .../java/org/truffleruby/shared/options/OptionsCatalog.java | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 197a39f11ef6..08fbdb24ca94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ Compatibility: Performance: +* Speedup some C extensions like `sqlite3`, `trilogy` and `json` by 2 to 3 times by using the Panama NFI backend for faster upcalls in JVM mode (@eregon). * Optimize encoding negotiation for ASCII-compatible encodings (@eregon, @andrykonchin). Changes: diff --git a/src/main/java/org/truffleruby/options/Options.java b/src/main/java/org/truffleruby/options/Options.java index 78f9f56ed765..072ffc79235c 100644 --- a/src/main/java/org/truffleruby/options/Options.java +++ b/src/main/java/org/truffleruby/options/Options.java @@ -107,7 +107,7 @@ public final class Options { public final boolean CEXTS; /** --cexts-lock=true */ public final boolean CEXT_LOCK; - /** --cexts-panama=false */ + /** --cexts-panama=true */ public final boolean CEXTS_PANAMA; /** --options-log=false */ public final boolean OPTIONS_LOG; diff --git a/src/options.yml b/src/options.yml index f9c2f78a0c25..82dbfb693d09 100644 --- a/src/options.yml +++ b/src/options.yml @@ -132,7 +132,7 @@ EXPERT: # C extension options CEXTS: [cexts, boolean, true, Enable use of C extensions] CEXT_LOCK: [cexts-lock, boolean, true, Use a Global Lock when running C extensions] - CEXTS_PANAMA: [cexts-panama, boolean, false, 'Use Panama for native to Ruby calls in C extensions. Only available in --jvm mode on JDK 22+.'] + CEXTS_PANAMA: [cexts-panama, boolean, true, 'Use Panama for native to Ruby calls in C extensions. Only available in --jvm mode on JDK 22+.'] # Debugging the values of options OPTIONS_LOG: [options-log, boolean, false, Log the final value of all options] diff --git a/src/shared/java/org/truffleruby/shared/options/OptionsCatalog.java b/src/shared/java/org/truffleruby/shared/options/OptionsCatalog.java index 4e008eb24fc7..a584c51319ef 100644 --- a/src/shared/java/org/truffleruby/shared/options/OptionsCatalog.java +++ b/src/shared/java/org/truffleruby/shared/options/OptionsCatalog.java @@ -71,7 +71,7 @@ public final class OptionsCatalog { public static final OptionKey BACKTRACE_ON_NEW_FIBER_KEY = new OptionKey<>(false); public static final OptionKey CEXTS_KEY = new OptionKey<>(true); public static final OptionKey CEXT_LOCK_KEY = new OptionKey<>(true); - public static final OptionKey CEXTS_PANAMA_KEY = new OptionKey<>(false); + public static final OptionKey CEXTS_PANAMA_KEY = new OptionKey<>(true); public static final OptionKey OPTIONS_LOG_KEY = new OptionKey<>(false); public static final OptionKey LOG_LOAD_KEY = new OptionKey<>(false); public static final OptionKey LOG_AUTOLOAD_KEY = new OptionKey<>(false); From aaf0238045b755d43964a39e0167c7e81ab0f39f Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 10 Nov 2024 16:07:22 +0100 Subject: [PATCH 3/3] [GR-52676] Handle the Panama Truffle NFI backend not being available when embedded * We cannot just add it as a dependency of TruffleRuby because the Panama backend is 22+ but Truffle 24.2 will still support JDK 21. We can add it as a dependency once Truffle drops supports for JDK 21. --- lib/truffle/truffle/cext.rb | 15 ++++++++++++++- .../test/embedding/ContextPermissionsTest.java | 7 +++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/truffle/truffle/cext.rb b/lib/truffle/truffle/cext.rb index 7f90b07da5a7..f089c9a1a257 100644 --- a/lib/truffle/truffle/cext.rb +++ b/lib/truffle/truffle/cext.rb @@ -190,7 +190,20 @@ def self.init_libtrufflerubytrampoline(libtrampoline) init_functions = libtrampoline[:rb_tr_trampoline_init_functions] init_functions = Primitive.interop_eval_nfi('(env,(string):pointer):void').bind(init_functions) - if Truffle::Boot.get_option 'cexts-panama' and Primitive.vm_java_version >= 22 and !TruffleRuby.native? + + panama = Truffle::Boot.get_option('cexts-panama') && Primitive.vm_java_version >= 22 && !TruffleRuby.native? + if panama + # Check if the Panama backend is available, it might not be when embedding TruffleRuby + rb_tr_invoke = LIBTRUFFLERUBY['rb_tr_invoke'] + begin + keep_alive << rb_tr_invoke.createNativeClosure('panama') + rescue Polyglot::ForeignException => e + warn "warning: the Panama Truffle NFI backend for running C extensions faster is not available (#{e.message}). Add 'org.graalvm.truffle:truffle-nfi-panama' to Maven dependencies to resolve or use '--ruby.cexts-panama=false' to ignore." + panama = false + end + end + + if panama init_functions.call(-> name { closure = LIBTRUFFLERUBY[name].createNativeClosure('panama') keep_alive << closure diff --git a/src/test-embedding/java/org/truffleruby/test/embedding/ContextPermissionsTest.java b/src/test-embedding/java/org/truffleruby/test/embedding/ContextPermissionsTest.java index e1cf0e6b238b..829d481d7412 100644 --- a/src/test-embedding/java/org/truffleruby/test/embedding/ContextPermissionsTest.java +++ b/src/test-embedding/java/org/truffleruby/test/embedding/ContextPermissionsTest.java @@ -77,6 +77,13 @@ public void testRequireGem() { } } + @Test + public void testRequireCExtension() { + try (Context context = Context.newBuilder("ruby").allowIO(IOAccess.ALL).allowNativeAccess(true).build()) { + Assert.assertEquals("Etc", context.eval("ruby", "require 'etc'; Etc.to_s").asString()); + } + } + @Test public void testThreadsNoNative() throws Throwable { // The ruby.single_threaded option needs to be set because --single-threaded defaults to --embedded.