Skip to content

Commit 7060c43

Browse files
committed
Add some configuration options
Still can't figure out why some events show up in the trace only once, during first session after device reboot.
1 parent acd70e3 commit 7060c43

File tree

8 files changed

+196
-36
lines changed

8 files changed

+196
-36
lines changed

src/native/monodroid/monodroid-glue-internal.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ namespace xamarin::android::internal
268268
void set_debug_options ();
269269
void parse_gdb_options ();
270270
void mono_runtime_init (JNIEnv *env, dynamic_local_string<PROPERTY_VALUE_BUFFER_LEN>& runtime_args);
271+
void timing_init () noexcept;
272+
void timing_init_extended () noexcept;
273+
void timing_init_verbose () noexcept;
274+
void timing_init_extreme () noexcept;
271275
void init_android_runtime (JNIEnv *env, jclass runtimeClass, jobject loader);
272276
void set_environment_variable_for_directory (const char *name, jstring_wrapper &value, bool createDirectory, mode_t mode);
273277

src/native/monodroid/monodroid-glue.cc

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,17 @@
6161
#include "embedded-assemblies.hh"
6262
#include "monodroid-glue.hh"
6363
#include "monodroid-glue-internal.hh"
64+
#include "monodroid-profiling.hh"
6465
#include "globals.hh"
6566
#include "xamarin-app.hh"
6667
#include "timing.hh"
67-
//#include "xa-internal-api-impl.hh"
6868
#include "build-info.hh"
6969
#include "monovm-properties.hh"
7070
#include "startup-aware-lock.hh"
7171
#include "timing-internal.hh"
7272
#include "search.hh"
7373
#include "runtime-util.hh"
7474

75-
//#include "xamarin_getifaddrs.h"
76-
7775
#include "cpp-util.hh"
7876
#include "strings.hh"
7977

@@ -547,7 +545,7 @@ MonodroidRuntime::mono_runtime_init ([[maybe_unused]] JNIEnv *env, [[maybe_unuse
547545
{
548546
#if defined(PERFETTO_ENABLED)
549547
log_info (LOG_DEFAULT, "Trace event for mono_runtime_init");
550-
TRACE_EVENT(
548+
TRACE_EVENT_BEGIN(
551549
PerfettoConstants::MonoRuntimeCategory.data (),
552550
"mono_runtime_init",
553551
PerfettoSupport::get_name_annotated_thread_track<PerfettoTrackId::MonodroidRuntime> ()
@@ -667,12 +665,6 @@ MonodroidRuntime::mono_runtime_init ([[maybe_unused]] JNIEnv *env, [[maybe_unuse
667665
Util::set_world_accessable (jit_log_path.get ());
668666
}
669667

670-
profiler_handle = mono_profiler_create (nullptr);
671-
mono_profiler_set_thread_started_callback (profiler_handle, thread_start);
672-
mono_profiler_set_thread_stopped_callback (profiler_handle, thread_end);
673-
#if defined (PERFETTO_ENABLED)
674-
perfetto_hook_mono_events ();
675-
#endif
676668
if (log_methods) [[unlikely]]{
677669
jit_time.mark_start ();
678670
mono_profiler_set_jit_begin_callback (profiler_handle, jit_begin);
@@ -729,6 +721,13 @@ MonodroidRuntime::mono_runtime_init ([[maybe_unused]] JNIEnv *env, [[maybe_unuse
729721
xamarin_app_init (env, get_function_pointer_at_startup);
730722
}
731723
#endif // def RELEASE && def ANDROID && def NET
724+
725+
#if defined(PERFETTO_ENABLED)
726+
TRACE_EVENT_END(
727+
PerfettoConstants::MonoRuntimeCategory.data (),
728+
PerfettoSupport::get_name_annotated_thread_track<PerfettoTrackId::MonodroidRuntime> ()
729+
);
730+
#endif
732731
}
733732

734733
void
@@ -1584,41 +1583,77 @@ MonodroidRuntime::install_logging_handlers ()
15841583
void
15851584
MonodroidRuntime::perfetto_init () noexcept
15861585
{
1587-
log_warn (LOG_TIMING, "INIT perfetto");
15881586
perfetto::TracingInitArgs args;
15891587
args.backends = perfetto::kSystemBackend;
15901588

15911589
perfetto::Tracing::Initialize (args);
15921590
perfetto::TrackEvent::Register ();
15931591
}
1592+
#endif // def PERFETTO_ENABLED
15941593

15951594
void
1596-
MonodroidRuntime::perfetto_hook_mono_events () noexcept
1595+
MonodroidRuntime::timing_init_extended () noexcept
15971596
{
1598-
if (profiler_handle == nullptr) {
1599-
return;
1600-
}
1601-
1602-
log_warn (LOG_TIMING, "HOOK perfetto");
16031597
mono_profiler_set_assembly_loading_callback (profiler_handle, prof_assembly_loading);
16041598
mono_profiler_set_assembly_loaded_callback (profiler_handle, prof_assembly_loaded);
1605-
mono_profiler_set_image_loading_callback (profiler_handle, prof_image_loading);
1606-
mono_profiler_set_image_loaded_callback (profiler_handle, prof_image_loaded);
16071599

16081600
mono_profiler_set_class_loading_callback (profiler_handle, prof_class_loading);
16091601
mono_profiler_set_class_loaded_callback (profiler_handle, prof_class_loaded);
1610-
// mono_profiler_set_vtable_loading_callback (profiler_handle, prof_vtable_loading);
1611-
// mono_profiler_set_vtable_loaded_callback (profiler_handle, prof_vtable_loaded);
1612-
mono_profiler_set_monitor_contention_callback (profiler_handle, prof_monitor_contention);
1613-
mono_profiler_set_monitor_acquired_callback (profiler_handle, prof_monitor_acquired);
16141602

16151603
mono_profiler_set_method_begin_invoke_callback (profiler_handle, prof_method_begin_invoke);
16161604
mono_profiler_set_method_end_invoke_callback (profiler_handle, prof_method_end_invoke);
1617-
mono_profiler_set_method_enter_callback (profiler_handle, prof_method_enter);
1618-
mono_profiler_set_method_leave_callback (profiler_handle, prof_method_leave);
1605+
}
1606+
1607+
void
1608+
MonodroidRuntime::timing_init_verbose () noexcept
1609+
{
1610+
mono_profiler_set_jit_begin_callback (profiler_handle, jit_begin);
1611+
mono_profiler_set_jit_done_callback (profiler_handle, jit_done);
1612+
mono_profiler_set_jit_failed_callback (profiler_handle, jit_failed);
16191613

1614+
mono_profiler_set_monitor_contention_callback (profiler_handle, prof_monitor_contention);
1615+
mono_profiler_set_monitor_acquired_callback (profiler_handle, prof_monitor_acquired);
16201616
}
1617+
1618+
void
1619+
MonodroidRuntime::timing_init_extreme () noexcept
1620+
{
1621+
mono_profiler_set_vtable_loading_callback (profiler_handle, prof_vtable_loading);
1622+
mono_profiler_set_vtable_loaded_callback (profiler_handle, prof_vtable_loaded);
1623+
1624+
mono_profiler_set_image_loading_callback (profiler_handle, prof_image_loading);
1625+
mono_profiler_set_image_loaded_callback (profiler_handle, prof_image_loaded);
1626+
}
1627+
1628+
[[gnu::flatten]] void
1629+
MonodroidRuntime::timing_init () noexcept
1630+
{
1631+
#if !defined(PERFETTO_ENABLED)
1632+
if (!FastTiming::enabled () || FastTiming::mode () == ProfilingMode::Bare) {
1633+
return;
1634+
}
1635+
#endif
1636+
switch (FastTiming::mode ()) {
1637+
case ProfilingMode::Extreme:
1638+
timing_init_extreme ();
1639+
[[fallthrough]];
1640+
1641+
case ProfilingMode::Verbose:
1642+
timing_init_verbose ();
1643+
[[fallthrough]];
1644+
1645+
#if defined(PERFETTO_ENABLED)
1646+
case ProfilingMode::Bare:
16211647
#endif
1648+
case ProfilingMode::Extended:
1649+
timing_init_extended ();
1650+
break;
1651+
1652+
default:
1653+
// ignored
1654+
break;
1655+
}
1656+
}
16221657

16231658
inline void
16241659
MonodroidRuntime::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass klass, jstring lang, jobjectArray runtimeApksJava,
@@ -1627,7 +1662,7 @@ MonodroidRuntime::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass kl
16271662
jboolean haveSplitApks)
16281663
{
16291664
#if defined(PERFETTO_ENABLED)
1630-
TRACE_EVENT(
1665+
TRACE_EVENT_BEGIN(
16311666
PerfettoConstants::MonoRuntimeCategory.data (),
16321667
"initInternal",
16331668
PerfettoSupport::get_name_annotated_thread_track<PerfettoTrackId::MonodroidRuntime> ()
@@ -1644,6 +1679,11 @@ MonodroidRuntime::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass kl
16441679
// If fast logging is disabled, log messages immediately
16451680
FastTiming::initialize ((Logger::log_timing_categories() & LogTimingCategories::FastBare) != LogTimingCategories::FastBare);
16461681

1682+
profiler_handle = mono_profiler_create (nullptr);
1683+
mono_profiler_set_thread_started_callback (profiler_handle, thread_start);
1684+
mono_profiler_set_thread_stopped_callback (profiler_handle, thread_end);
1685+
timing_init ();
1686+
16471687
size_t total_time_index;
16481688
if (FastTiming::enabled ()) [[unlikely]] {
16491689
timing = new Timing ();
@@ -1759,13 +1799,7 @@ MonodroidRuntime::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass kl
17591799
}
17601800

17611801
mono_runtime_init (env, runtime_args);
1762-
#if defined(PERFETTO_ENABLED)
1763-
TRACE_EVENT(
1764-
PerfettoConstants::MonoRuntimeCategory.data (),
1765-
"initInternal_2",
1766-
PerfettoSupport::get_name_annotated_thread_track<PerfettoTrackId::MonodroidRuntime> ()
1767-
);
1768-
#endif
1802+
17691803
if (FastTiming::enabled ()) [[unlikely]] {
17701804
internal_timing->end_event (mono_runtime_init_index);
17711805
}
@@ -1807,6 +1841,10 @@ MonodroidRuntime::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass kl
18071841
startup_in_progress = false;
18081842

18091843
#if defined(PERFETTO_ENABLED)
1844+
TRACE_EVENT_END(
1845+
PerfettoConstants::MonoRuntimeCategory.data (),
1846+
PerfettoSupport::get_name_annotated_thread_track<PerfettoTrackId::MonodroidRuntime> ()
1847+
);
18101848
perfetto::TrackEvent::Flush ();
18111849
#endif
18121850
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#if !defined(MONODROID_PROFILING_HH)
2+
#define MONODROID_PROFILING_HH
3+
4+
namespace xamarin::android {
5+
// Keep the values ordered in the order of increasing verbosity
6+
enum class ProfilingMode
7+
{
8+
Bare,
9+
Extended,
10+
Verbose,
11+
Extreme,
12+
};
13+
}
14+
#endif // MONODROID_PROFILING_HH

src/native/monodroid/timing-internal.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,65 @@ bool FastTiming::is_enabled = false;
1212
bool FastTiming::immediate_logging = false;
1313
TimingEvent FastTiming::init_time {};
1414

15+
void
16+
FastTiming::parse_options (dynamic_local_string<PROPERTY_VALUE_BUFFER_LEN> const& value) noexcept
17+
{
18+
if (value.empty ()) {
19+
return;
20+
}
21+
22+
string_segment param;
23+
while (value.next_token (',', param)) {
24+
if (param.starts_with (OPT_MODE)) {
25+
if (param.equal (OPT_MODE.length (), OPT_MODE_BARE)) {
26+
profiling_mode = ProfilingMode::Bare;
27+
continue;
28+
}
29+
30+
if (param.equal (OPT_MODE.length (), OPT_MODE_EXTENDED)) {
31+
profiling_mode = ProfilingMode::Extended;
32+
continue;
33+
}
34+
35+
if (param.equal (OPT_MODE.length (), OPT_MODE_VERBOSE)) {
36+
profiling_mode = ProfilingMode::Verbose;
37+
continue;
38+
}
39+
40+
if (param.equal (OPT_MODE.length (), OPT_MODE_EXTREME)) {
41+
profiling_mode = ProfilingMode::Extreme;
42+
continue;
43+
}
44+
}
45+
}
46+
47+
#if defined(PERFETTO_ENABLED)
48+
// Clamp the profiling mode to extended, otherwise using Perfetto makes no sense
49+
if (profiling_mode < ProfilingMode::Extended) {
50+
profiling_mode = ProfilingMode::Extended;
51+
}
52+
#endif
53+
}
54+
1555
void
1656
FastTiming::really_initialize (bool log_immediately) noexcept
1757
{
1858
internal_timing = new FastTiming ();
1959
is_enabled = true;
2060
immediate_logging = log_immediately;
2161

62+
dynamic_local_string<PROPERTY_VALUE_BUFFER_LEN> value;
63+
int prop_len = 0;
64+
65+
if constexpr (SharedConstants::PerfettoEnabled) {
66+
prop_len = AndroidSystem::monodroid_get_system_property (SharedConstants::DEBUG_MONO_PERFETTO, value);
67+
}
68+
69+
if (prop_len <= 0) {
70+
AndroidSystem::monodroid_get_system_property (SharedConstants::DEBUG_MONO_TIMING, value);
71+
}
72+
parse_options (value);
73+
2274
if (immediate_logging) {
2375
return;
2476
}

src/native/monodroid/timing-internal.hh

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
#include <atomic>
55
#include <concepts>
66
#include <ctime>
7+
#include <string_view>
78
#include <vector>
89

910
#include "cpp-util.hh"
1011
#include "logger.hh"
1112
#include "startup-aware-lock.hh"
1213
#include "strings.hh"
1314
#include "util.hh"
15+
#include "monodroid-profiling.hh"
1416
#include "shared-constants.hh"
1517

1618
namespace xamarin::android::internal
@@ -37,7 +39,7 @@ namespace xamarin::android::internal
3739
RuntimeConfigBlob = 9,
3840
RuntimeRegister = 10,
3941
TotalRuntimeInit = 11,
40-
Unspecified = 12,
42+
Unspecified = std::numeric_limits<std::underlying_type_t<TimingEventKind>>::max (),
4143
};
4244

4345
struct TimingEventPoint
@@ -86,13 +88,31 @@ namespace xamarin::android::internal
8688
static constexpr uint32_t ms_in_second = 1000;
8789
static constexpr uint32_t ns_in_second = ms_in_second * ns_in_millisecond;
8890

91+
// Defaults
92+
#if defined(PERFETTO_ENABLED)
93+
static constexpr ProfilingMode default_profiling_mode = ProfilingMode::Extended;
94+
#else
95+
static constexpr ProfilingMode default_profiling_mode = ProfilingMode::Bare;
96+
#endif
97+
// Parameters in the debug.mono.{timing,perfetto} properties
98+
static constexpr std::string_view OPT_MODE { "mode=" };
99+
static constexpr std::string_view OPT_MODE_BARE { "bare" };
100+
static constexpr std::string_view OPT_MODE_EXTENDED { "extended" };
101+
static constexpr std::string_view OPT_MODE_VERBOSE { "verbose" };
102+
static constexpr std::string_view OPT_MODE_EXTREME { "extreme" };
103+
89104
protected:
90105
FastTiming () noexcept
91106
{
92107
events.reserve (INITIAL_EVENT_VECTOR_SIZE);
93108
}
94109

95110
public:
111+
force_inline static ProfilingMode mode () noexcept
112+
{
113+
return profiling_mode;
114+
}
115+
96116
force_inline static bool enabled () noexcept
97117
{
98118
return is_enabled;
@@ -235,6 +255,7 @@ namespace xamarin::android::internal
235255

236256
private:
237257
static void really_initialize (bool log_immediately) noexcept;
258+
static void parse_options (dynamic_local_string<PROPERTY_VALUE_BUFFER_LEN> const& value) noexcept;
238259
static void* timing_signal_thread (void *arg) noexcept;
239260

240261
force_inline static void mark (TimingEventPoint &point) noexcept
@@ -446,6 +467,7 @@ namespace xamarin::android::internal
446467
static TimingEvent init_time;
447468
static bool is_enabled;
448469
static bool immediate_logging;
470+
static inline ProfilingMode profiling_mode = default_profiling_mode;
449471
};
450472

451473
extern FastTiming *internal_timing;

src/native/runtime-base/shared-constants.hh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ namespace xamarin::android::internal
1818
class SharedConstants
1919
{
2020
public:
21+
#if defined(PERFETTO_ENABLED)
22+
static constexpr bool PerfettoEnabled = true;
23+
#else
24+
static constexpr bool PerfettoEnabled = false;
25+
#endif
26+
2127
// These three MUST be the same as like-named constants in src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Basic.cs
2228
static constexpr std::string_view MANGLED_ASSEMBLY_NAME_EXT { ".so" };
2329
static constexpr std::string_view MANGLED_ASSEMBLY_REGULAR_ASSEMBLY_MARKER { "lib_" };
@@ -55,9 +61,13 @@ namespace xamarin::android::internal
5561
static inline constexpr std::string_view DEBUG_MONO_GDB_PROPERTY { "debug.mono.gdb" };
5662
static inline constexpr std::string_view DEBUG_MONO_LOG_PROPERTY { "debug.mono.log" };
5763
static inline constexpr std::string_view DEBUG_MONO_MAX_GREFC { "debug.mono.max_grefc" };
64+
65+
// An alias to `debug.mono.timing`
66+
static inline constexpr std::string_view DEBUG_MONO_PERFETTO { "debug.mono.perfetto" };
5867
static inline constexpr std::string_view DEBUG_MONO_PROFILE_PROPERTY { "debug.mono.profile" };
5968
static inline constexpr std::string_view DEBUG_MONO_RUNTIME_ARGS_PROPERTY { "debug.mono.runtime_args" };
6069
static inline constexpr std::string_view DEBUG_MONO_SOFT_BREAKPOINTS { "debug.mono.soft_breakpoints" };
70+
static inline constexpr std::string_view DEBUG_MONO_TIMING { "debug.mono.timing" };
6171
static inline constexpr std::string_view DEBUG_MONO_TRACE_PROPERTY { "debug.mono.trace" };
6272
static inline constexpr std::string_view DEBUG_MONO_WREF_PROPERTY { "debug.mono.wref" };
6373

0 commit comments

Comments
 (0)