From c74ba8e540a39f99659db477dacb53270cb2fb86 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Wed, 6 Nov 2024 21:45:34 +0100 Subject: [PATCH] Try as I might, can't get counts working without the interpreter Will probably require a custom build of the MonoVM runtime :( --- .../monodroid/monodroid-glue-internal.hh | 1 - src/native/monodroid/monodroid-glue.cc | 12 +----- src/native/monodroid/performance-methods.cc | 41 ++++++++++++++++--- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/native/monodroid/monodroid-glue-internal.hh b/src/native/monodroid/monodroid-glue-internal.hh index ea209b91b77..4a27a57bba8 100644 --- a/src/native/monodroid/monodroid-glue-internal.hh +++ b/src/native/monodroid/monodroid-glue-internal.hh @@ -268,7 +268,6 @@ namespace xamarin::android::internal jmethodID java_System_identityHashCode; jmethodID Class_getName; jclass java_TimeZone; - FILE *jit_log; MonoProfilerHandle profiler_handle; /* diff --git a/src/native/monodroid/monodroid-glue.cc b/src/native/monodroid/monodroid-glue.cc index dfd3f6c4cc2..5d6aabd263b 100644 --- a/src/native/monodroid/monodroid-glue.cc +++ b/src/native/monodroid/monodroid-glue.cc @@ -684,21 +684,11 @@ MonodroidRuntime::mono_runtime_init ([[maybe_unused]] JNIEnv *env, [[maybe_unuse // TESTING UBSAN: integer overflow //log_warn (LOG_DEFAULT, "Let us have an overflow: %d", INT_MAX + 1); - bool log_methods = FastTiming::enabled () && !FastTiming::is_bare_mode (); - if (log_methods) [[unlikely]] { - log_debug (LOG_ASSEMBLY, "Enabling method logging"); - std::unique_ptr jit_log_path {Util::path_combine (AndroidSystem::override_dirs [0], "methods.txt")}; - log_debug (LOG_ASSEMBLY, "JIT log path: %s", jit_log_path.get ()); - Util::create_directory (AndroidSystem::override_dirs [0], 0755); - jit_log = Util::monodroid_fopen (jit_log_path.get (), "w"); - Util::set_world_accessable (jit_log_path.get ()); - } - profiler_handle = mono_profiler_create (nullptr); mono_profiler_set_thread_started_callback (profiler_handle, thread_start); mono_profiler_set_thread_stopped_callback (profiler_handle, thread_end); - if (log_methods) [[unlikely]]{ + if (FastTiming::enabled () && !FastTiming::is_bare_mode ()) [[unlikely]]{ method_event_map_write_lock = std::make_unique (); method_event_map = std::make_unique (); diff --git a/src/native/monodroid/performance-methods.cc b/src/native/monodroid/performance-methods.cc index 0473639e8b9..89daa8d7e2f 100644 --- a/src/native/monodroid/performance-methods.cc +++ b/src/native/monodroid/performance-methods.cc @@ -1,6 +1,11 @@ #include +#include +#include + #include +#include +#include "android-system.hh" #include "cppcompat.hh" #include "logger.hh" #include "monodroid-glue-internal.hh" @@ -40,8 +45,34 @@ MonodroidRuntime::dump_method_events () mono_profiler_set_jit_begin_callback (profiler_handle, nullptr); mono_profiler_set_jit_done_callback (profiler_handle, nullptr); mono_profiler_set_jit_failed_callback (profiler_handle, nullptr); + mono_profiler_set_method_begin_invoke_callback (profiler_handle, nullptr); + mono_profiler_set_method_end_invoke_callback (profiler_handle, nullptr); + + switch (AndroidSystem::get_mono_aot_mode ()) { + case MonoAotMode::MONO_AOT_MODE_INTERP: + case MonoAotMode::MONO_AOT_MODE_INTERP_ONLY: + case MonoAotMode::MONO_AOT_MODE_INTERP_LLVMONLY: + case MonoAotMode::MONO_AOT_MODE_LLVMONLY_INTERP: + mono_profiler_set_call_instrumentation_filter_callback (profiler_handle, nullptr); + mono_profiler_set_method_enter_callback (profiler_handle, nullptr); + mono_profiler_set_method_leave_callback (profiler_handle, nullptr); + break; + + default: + // Other AOT modes are ignored + break; + } + + std::unique_ptr jit_log_path {Util::path_combine (AndroidSystem::override_dirs [0], "methods.xml")}; + Util::create_directory (AndroidSystem::override_dirs [0], 0755); + int jit_log = open (jit_log_path.get (), O_CREAT | O_WRONLY | O_TRUNC | O_SYNC, 0644); + if (jit_log < 0) { + log_error (LOG_DEFAULT, "Failed to open '%s' for writing: %s", jit_log_path.get (), strerror (errno)); + return; + } + Util::set_world_accessable (jit_log_path.get ()); - fprintf ( + dprintf ( jit_log, R"() @@ -54,7 +85,7 @@ MonodroidRuntime::dump_method_events () bool was_jited = (record.state & MethodEventRecord::JitStateStarted) == MethodEventRecord::JitStateStarted; timing_diff diff { record.jit_elapsed }; - fprintf ( + dprintf ( jit_log, R"( )", @@ -70,8 +101,6 @@ MonodroidRuntime::dump_method_events () } method_event_map->clear (); - fprintf (jit_log, "\n"); - fflush (jit_log); - fclose (jit_log); - jit_log = nullptr; + dprintf (jit_log, "\n"); + close (jit_log); }