From 1ed50300c4c585b8953ef148318daab61d18803a Mon Sep 17 00:00:00 2001 From: james Date: Thu, 18 Dec 2025 21:28:09 +1030 Subject: [PATCH] Track total module access times and add ScopedMemoryAccess registration --- crates/core/src/bimage.rs | 21 ++++++++++++++------- crates/core/src/class_loader.rs | 5 ++++- crates/core/src/main.rs | 2 ++ crates/core/src/native/jni.rs | 10 ++++++---- crates/core/src/thread.rs | 10 +++++----- crates/roast-vm-sys/src/lib.rs | 5 +++-- crates/roast-vm-sys/src/memaccess.rs | 10 ++++++++++ 7 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 crates/roast-vm-sys/src/memaccess.rs diff --git a/crates/core/src/bimage.rs b/crates/core/src/bimage.rs index 7befd70..070a268 100644 --- a/crates/core/src/bimage.rs +++ b/crates/core/src/bimage.rs @@ -2,6 +2,8 @@ use log::trace; use sevenz_rust2::ArchiveReader; use std::collections::{HashMap, HashSet}; use std::fs::File; +use std::ops::{Add, AddAssign}; +use std::time::{Duration, Instant}; const DEFAULT_LOCATION: &str = "./lib/modules"; @@ -11,6 +13,7 @@ pub struct Bimage { // inversion, // eg. packages: HashMap, + pub total_access_time: Duration, } impl Default for Bimage { @@ -49,6 +52,7 @@ impl Default for Bimage { image: reader, modules, packages, + total_access_time: Default::default(), } } } @@ -81,13 +85,16 @@ impl Bimage { pub fn get_class(&mut self, module: &str, class: &str) -> Result, String> { // trace!("Modules{:#?}", self.modules); - + if class.contains("ScopedMemoryAccess") { + println!("Time to scoped: {:?}", self.total_access_time) + } + let start = Instant::now(); let path = Self::resolve_path(module, class); - self.image - .read_file(&path) - .map_err(|e| { - log::trace!("Class not found {}", path); - e.to_string() - }) + let res = self.image.read_file(&path).map_err(|e| { + log::trace!("Class not found {}", path); + e.to_string() + }); + self.total_access_time.add_assign(start.elapsed()); + res } } diff --git a/crates/core/src/class_loader.rs b/crates/core/src/class_loader.rs index 5f9057d..fd66dc0 100644 --- a/crates/core/src/class_loader.rs +++ b/crates/core/src/class_loader.rs @@ -14,9 +14,9 @@ use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; use std::sync::{Arc, OnceLock}; +use std::time::Duration; pub type LoaderRef = Arc>; - #[deprecated(note = "This method is deprecated and will be removed in future versions")] pub fn resolve_path(what: &str) -> Result<(PathBuf, String), String> { let (module, fqn) = what.split_once("/").unwrap_or(("", what)); @@ -92,6 +92,9 @@ pub struct ClassLoader { type LoaderId = Option; impl ClassLoader { + pub fn access_time(&self) -> Duration { + self.bimage.total_access_time + } pub fn new() -> Result { let loader = Self::default(); // for entry in VM_CLASSES { diff --git a/crates/core/src/main.rs b/crates/core/src/main.rs index 96d2ed1..7bc6088 100644 --- a/crates/core/src/main.rs +++ b/crates/core/src/main.rs @@ -37,6 +37,8 @@ fn run() { stack_trace, }) => { println!("took {:?}", start.elapsed()); + let access = vm.loader.lock().access_time(); + println!("total module access time was {:?}", access); let thread = vm.threads.get(&vm.main_thread_id).unwrap(); let objs = thread .gc diff --git a/crates/core/src/native/jni.rs b/crates/core/src/native/jni.rs index db65c5b..60be2f5 100644 --- a/crates/core/src/native/jni.rs +++ b/crates/core/src/native/jni.rs @@ -1652,7 +1652,7 @@ unsafe extern "system" fn release_string_chars( str: jstring, chars: *const jchar, ) { - todo!("release_string_chars") + warn!("release_string_chars") } unsafe extern "system" fn new_string_utf(env: *mut JNIEnv, utf: *const c_char) -> jstring { @@ -1697,11 +1697,13 @@ unsafe extern "system" fn get_string_utf_length(env: *mut JNIEnv, str: jstring) } unsafe extern "system" fn release_string_utf_chars( - env: *mut JNIEnv, - str: jstring, + _env: *mut JNIEnv, + _str: jstring, chars: *const c_char, ) { - todo!("release_string_utf_chars") + if !chars.is_null() { + drop(CString::from_raw(chars as *mut c_char)); + } } unsafe extern "system" fn get_array_length(env: *mut JNIEnv, array: jarray) -> jsize { diff --git a/crates/core/src/thread.rs b/crates/core/src/thread.rs index 35718d4..c86fd2a 100644 --- a/crates/core/src/thread.rs +++ b/crates/core/src/thread.rs @@ -320,11 +320,11 @@ impl VmThread { pub fn invoke_native(&self, method: &MethodRef, mut args: Vec) -> MethodCallResult { let symbol_name = generate_jni_method_name(method, false); - // if symbol_name.contains("Java_jdk_internal_reflect_Reflection_getClassAccessFlags") { - // return Err(VmError::Debug( - // "RoastVM specific implementation required for Java_jdk_internal_reflect_Reflection_getClassAccessFlags", - // )); - // } + if symbol_name.contains("Java_jdk_internal_misc_Signal_handle0") { + return Err(VmError::Debug( + "RoastVM specific implementation required for Java_jdk_internal_misc_Signal_handle0", + )); + } if symbol_name.contains("Java_java_lang_Class_desiredAssertionStatus0") { warn!("MAJOR HACK, figure out what is wrong with desiredAssertionStatus0"); diff --git a/crates/roast-vm-sys/src/lib.rs b/crates/roast-vm-sys/src/lib.rs index c3a3669..06b359e 100644 --- a/crates/roast-vm-sys/src/lib.rs +++ b/crates/roast-vm-sys/src/lib.rs @@ -2,6 +2,7 @@ mod CDS; mod class; +mod memaccess; mod misc_unsafe; mod object; mod reflection; @@ -12,10 +13,10 @@ use jni::objects::{JClass, JObject, JString}; use jni::strings::JNIString; use jni::sys::{jclass, jlong, jobject, jobjectArray}; use jni::{JNIEnv, NativeMethod}; +use roast_vm_core::VmThread; +use roast_vm_core::objects::ReferenceKind; use roast_vm_core::objects::array::ArrayReference; use roast_vm_core::objects::object::ObjectReference; -use roast_vm_core::objects::ReferenceKind; -use roast_vm_core::VmThread; use std::ffi::c_void; use std::io::Write; use std::time::{SystemTime, UNIX_EPOCH}; diff --git a/crates/roast-vm-sys/src/memaccess.rs b/crates/roast-vm-sys/src/memaccess.rs new file mode 100644 index 0000000..4d3ddcf --- /dev/null +++ b/crates/roast-vm-sys/src/memaccess.rs @@ -0,0 +1,10 @@ +use jni::sys::{JNIEnv, jclass}; + +#[unsafe(no_mangle)] +pub unsafe extern "system" fn Java_jdk_internal_misc_ScopedMemoryAccess_registerNatives( + env: *mut JNIEnv, + _class: jclass, +) { + // noop + () +}