Skip to content

Commit 05cc380

Browse files
committed
New events work
1 parent 1ccf397 commit 05cc380

9 files changed

+303
-66
lines changed

src/monodroid/jni/android-system.hh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef __ANDROID_SYSTEM_H
33
#define __ANDROID_SYSTEM_H
44

5+
#include <string_view>
56
#include <stdint.h>
67
#include <stdlib.h>
78
#include <pthread.h>
@@ -56,7 +57,19 @@ namespace xamarin::android::internal
5657
void setup_process_args (jstring_array_wrapper &runtimeApks);
5758
void create_update_dir (char *override_dir);
5859
int monodroid_get_system_property (const char *name, char **value);
60+
61+
int monodroid_get_system_property (std::string_view const& name, char **value) noexcept
62+
{
63+
return monodroid_get_system_property (name.data (), value);
64+
}
65+
5966
int monodroid_get_system_property (const char *name, dynamic_local_string<PROPERTY_VALUE_BUFFER_LEN>& value);
67+
68+
int monodroid_get_system_property (std::string_view const& name, dynamic_local_string<PROPERTY_VALUE_BUFFER_LEN>& value) noexcept
69+
{
70+
return monodroid_get_system_property (name.data (), value);
71+
}
72+
6073
size_t monodroid_get_system_property_from_overrides (const char *name, char ** value);
6174
char* get_bundled_app (JNIEnv *env, jstring dir);
6275
int count_override_assemblies ();

src/monodroid/jni/debug-constants.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ using namespace xamarin::android;
44

55
extern "C" const char *__get_debug_mono_log_property (void)
66
{
7-
return static_cast<const char*> (Debug::DEBUG_MONO_LOG_PROPERTY);
7+
return Debug::DEBUG_MONO_LOG_PROPERTY.data ();
88
}

src/monodroid/jni/debug.hh

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define __MONODROID_DEBUG_H__
44

55
#include <cstdint>
6+
#include <string_view>
67
#include <pthread.h>
78
#include <sys/time.h>
89

@@ -29,20 +30,20 @@ namespace xamarin::android
2930

3031
public:
3132
/* Android property containing connection information, set by XS */
32-
static inline const char DEBUG_MONO_CONNECT_PROPERTY[] = "debug.mono.connect";
33-
static inline const char DEBUG_MONO_DEBUG_PROPERTY[] = "debug.mono.debug";
34-
static inline const char DEBUG_MONO_ENV_PROPERTY[] = "debug.mono.env";
35-
static inline const char DEBUG_MONO_EXTRA_PROPERTY[] = "debug.mono.extra";
36-
static inline const char DEBUG_MONO_GC_PROPERTY[] = "debug.mono.gc";
37-
static inline const char DEBUG_MONO_GDB_PROPERTY[] = "debug.mono.gdb";
38-
static inline const char DEBUG_MONO_LOG_PROPERTY[] = "debug.mono.log";
39-
static inline const char DEBUG_MONO_MAX_GREFC[] = "debug.mono.max_grefc";
40-
static inline const char DEBUG_MONO_PROFILE_PROPERTY[] = "debug.mono.profile";
41-
static inline const char DEBUG_MONO_RUNTIME_ARGS_PROPERTY[] = "debug.mono.runtime_args";
42-
static inline const char DEBUG_MONO_SOFT_BREAKPOINTS[] = "debug.mono.soft_breakpoints";
43-
static inline const char DEBUG_MONO_TIMING[] = "debug.mono.timing";
44-
static inline const char DEBUG_MONO_TRACE_PROPERTY[] = "debug.mono.trace";
45-
static inline const char DEBUG_MONO_WREF_PROPERTY[] = "debug.mono.wref";
33+
static constexpr std::string_view DEBUG_MONO_CONNECT_PROPERTY { "debug.mono.connect" };
34+
static constexpr std::string_view DEBUG_MONO_DEBUG_PROPERTY { "debug.mono.debug" };
35+
static constexpr std::string_view DEBUG_MONO_ENV_PROPERTY { "debug.mono.env" };
36+
static constexpr std::string_view DEBUG_MONO_EXTRA_PROPERTY { "debug.mono.extra" };
37+
static constexpr std::string_view DEBUG_MONO_GC_PROPERTY { "debug.mono.gc" };
38+
static constexpr std::string_view DEBUG_MONO_GDB_PROPERTY { "debug.mono.gdb" };
39+
static constexpr std::string_view DEBUG_MONO_LOG_PROPERTY { "debug.mono.log" };
40+
static constexpr std::string_view DEBUG_MONO_MAX_GREFC { "debug.mono.max_grefc" };
41+
static constexpr std::string_view DEBUG_MONO_PROFILE_PROPERTY { "debug.mono.profile" };
42+
static constexpr std::string_view DEBUG_MONO_RUNTIME_ARGS_PROPERTY { "debug.mono.runtime_args" };
43+
static constexpr std::string_view DEBUG_MONO_SOFT_BREAKPOINTS { "debug.mono.soft_breakpoints" };
44+
static constexpr std::string_view DEBUG_MONO_TIMING { "debug.mono.timing" };
45+
static constexpr std::string_view DEBUG_MONO_TRACE_PROPERTY { "debug.mono.trace" };
46+
static constexpr std::string_view DEBUG_MONO_WREF_PROPERTY { "debug.mono.wref" };
4647

4748
public:
4849
explicit Debug ()

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,36 @@ namespace xamarin::android::internal
8888
true
8989
>;
9090

91+
using timing_sequence_map_t = tsl::robin_map<void*, size_t>;
9192
using load_assemblies_context_type = MonoAssemblyLoadContextGCHandle;
93+
94+
class TimingProfilerState
95+
{
96+
public:
97+
template<typename T>
98+
void add_sequence (T* ptr, size_t sequence_number)
99+
{
100+
std::lock_guard lock (map_lock);
101+
seq_map[ptr] = sequence_number;
102+
}
103+
104+
template<typename T>
105+
size_t get_sequence (T* ptr)
106+
{
107+
std::lock_guard lock (map_lock);
108+
auto iter = seq_map.find (ptr);
109+
if (iter == seq_map.end ()) {
110+
return 0;
111+
}
112+
113+
return iter->second;
114+
}
115+
116+
private:
117+
timing_sequence_map_t seq_map;
118+
std::mutex map_lock;
119+
};
120+
92121
static constexpr pinvoke_library_map::size_type LIBRARY_MAP_INITIAL_BUCKET_COUNT = 1;
93122
#else // def NET
94123
using load_assemblies_context_type = MonoDomain*;
@@ -300,6 +329,11 @@ namespace xamarin::android::internal
300329
void set_debug_options ();
301330
void parse_gdb_options ();
302331
void mono_runtime_init (JNIEnv *env, dynamic_local_string<PROPERTY_VALUE_BUFFER_LEN>& runtime_args);
332+
void timing_init () noexcept;
333+
void timing_ensure_state () noexcept;
334+
void timing_init_extended () noexcept;
335+
void timing_init_verbose () noexcept;
336+
303337
#if defined (NET)
304338
void init_android_runtime (JNIEnv *env, jclass runtimeClass, jobject loader);
305339
#else //def NET
@@ -341,6 +375,13 @@ namespace xamarin::android::internal
341375
static void jit_done (MonoProfiler *prof, MonoMethod *method, MonoJitInfo* jinfo);
342376
static void thread_start (MonoProfiler *prof, uintptr_t tid);
343377
static void thread_end (MonoProfiler *prof, uintptr_t tid);
378+
static void prof_assembly_loading (MonoProfiler *prof, MonoAssembly *assembly) noexcept;
379+
static void prof_assembly_loaded (MonoProfiler *prof, MonoAssembly *assembly) noexcept;
380+
static void prof_image_loading (MonoProfiler *prof, MonoImage *assembly) noexcept;
381+
static void prof_image_loaded (MonoProfiler *prof, MonoImage *assembly) noexcept;
382+
static void prof_class_loading (MonoProfiler *prof, MonoClass *klass) noexcept;
383+
static void prof_class_loaded (MonoProfiler *prof, MonoClass *klass) noexcept;
384+
344385
#if !defined (RELEASE) || !defined (ANDROID)
345386
static MonoReflectionType* typemap_java_to_managed (MonoString *java_type_name) noexcept;
346387
static const char* typemap_managed_to_java (MonoReflectionType *type, const uint8_t *mvid) noexcept;
@@ -416,6 +457,8 @@ namespace xamarin::android::internal
416457
static void *system_native_library_handle;
417458
static void *system_security_cryptography_native_android_library_handle;
418459
static void *system_io_compression_native_library_handle;
460+
461+
static inline TimingProfilerState* timing_profiler_state = nullptr;
419462
#else // def NET
420463
static std::mutex api_init_lock;
421464
static void *api_dso_handle;

src/monodroid/jni/monodroid-glue.cc

Lines changed: 114 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,119 @@ MonodroidRuntime::set_debug_options (void)
637637
mono_debug_init (MONO_DEBUG_FORMAT_MONO);
638638
}
639639

640+
void
641+
MonodroidRuntime::prof_assembly_loading ([[maybe_unused]] MonoProfiler *prof, MonoAssembly *assembly) noexcept
642+
{
643+
size_t seq = internal_timing->start_event (TimingEventKind::MonoAssemblyLoad);
644+
// This unfortunately has some overhead
645+
timing_profiler_state->add_sequence (assembly, seq);
646+
}
647+
648+
void
649+
MonodroidRuntime::prof_assembly_loaded ([[maybe_unused]] MonoProfiler *prof, MonoAssembly *assembly) noexcept
650+
{
651+
// This unfortunately has some overhead
652+
size_t seq = timing_profiler_state->get_sequence (assembly);
653+
internal_timing->end_event (seq, true /* uses_more_info */);
654+
internal_timing->add_more_info (seq, utils.strdup_new (mono_assembly_name_get_name (mono_assembly_get_name (assembly))));
655+
}
656+
657+
void
658+
MonodroidRuntime::prof_image_loading ([[maybe_unused]] MonoProfiler *prof, MonoImage *image) noexcept
659+
{
660+
size_t seq = internal_timing->start_event (TimingEventKind::MonoImageLoad);
661+
// This unfortunately has some overhead
662+
timing_profiler_state->add_sequence (image, seq);
663+
}
664+
665+
void
666+
MonodroidRuntime::prof_image_loaded ([[maybe_unused]] MonoProfiler *prof, MonoImage *image) noexcept
667+
{
668+
// This unfortunately has some overhead
669+
size_t seq = timing_profiler_state->get_sequence (image);
670+
internal_timing->end_event (seq, true /* uses_more_info */);
671+
internal_timing->add_more_info (seq, utils.strdup_new (mono_image_get_name (image)));
672+
}
673+
674+
void
675+
MonodroidRuntime::prof_class_loading ([[maybe_unused]] MonoProfiler *prof, MonoClass *klass) noexcept
676+
{
677+
size_t seq = internal_timing->start_event (TimingEventKind::MonoClassLoad);
678+
// This unfortunately has some overhead
679+
timing_profiler_state->add_sequence (klass, seq);
680+
}
681+
682+
void
683+
MonodroidRuntime::prof_class_loaded ([[maybe_unused]] MonoProfiler *prof, MonoClass *klass) noexcept
684+
{
685+
// This unfortunately has some overhead
686+
size_t seq = timing_profiler_state->get_sequence (klass);
687+
internal_timing->end_event (seq, true /* uses_more_info */);
688+
internal_timing->add_more_info (seq, utils.strdup_new (mono_class_get_name (klass)));
689+
}
690+
691+
force_inline
692+
void
693+
MonodroidRuntime::timing_ensure_state () noexcept
694+
{
695+
if (timing_profiler_state != nullptr) {
696+
return;
697+
}
698+
699+
timing_profiler_state = new TimingProfilerState;
700+
}
701+
702+
force_inline void
703+
MonodroidRuntime::timing_init_extended () noexcept
704+
{
705+
timing_ensure_state ();
706+
707+
mono_profiler_set_assembly_loading_callback (profiler_handle, prof_assembly_loading);
708+
mono_profiler_set_assembly_loaded_callback (profiler_handle, prof_assembly_loaded);
709+
mono_profiler_set_image_loading_callback (profiler_handle, prof_image_loading);
710+
mono_profiler_set_image_loaded_callback (profiler_handle, prof_image_loaded);
711+
}
712+
713+
force_inline void
714+
MonodroidRuntime::timing_init_verbose () noexcept
715+
{
716+
timing_ensure_state ();
717+
718+
std::unique_ptr<char> jit_log_path {utils.path_combine (androidSystem.get_override_dir (0), "methods.txt")};
719+
jit_log = utils.monodroid_fopen (jit_log_path.get (), "a");
720+
utils.set_world_accessable (jit_log_path.get ());
721+
jit_time.mark_start ();
722+
mono_profiler_set_jit_begin_callback (profiler_handle, jit_begin);
723+
mono_profiler_set_jit_done_callback (profiler_handle, jit_done);
724+
mono_profiler_set_jit_failed_callback (profiler_handle, jit_failed);
725+
726+
mono_profiler_set_class_loading_callback (profiler_handle, prof_class_loading);
727+
mono_profiler_set_class_loaded_callback (profiler_handle, prof_class_loaded);
728+
}
729+
730+
force_inline void
731+
MonodroidRuntime::timing_init () noexcept
732+
{
733+
if (!FastTiming::enabled () || FastTiming::mode () == TimingMode::Bare) {
734+
return;
735+
}
736+
737+
// TODO: time this, so that we can subtract it from the cumulative results
738+
switch (FastTiming::mode ()) {
739+
case TimingMode::Verbose:
740+
timing_init_verbose ();
741+
[[fallthrough]];
742+
743+
case TimingMode::Extended:
744+
timing_init_extended ();
745+
break;
746+
747+
default:
748+
// ignored
749+
break;
750+
}
751+
}
752+
640753
void
641754
MonodroidRuntime::mono_runtime_init ([[maybe_unused]] JNIEnv *env, [[maybe_unused]] dynamic_local_string<PROPERTY_VALUE_BUFFER_LEN>& runtime_args)
642755
{
@@ -745,41 +858,11 @@ MonodroidRuntime::mono_runtime_init ([[maybe_unused]] JNIEnv *env, [[maybe_unuse
745858

746859
// TESTING UBSAN: integer overflow
747860
//log_warn (LOG_DEFAULT, "Let us have an overflow: %d", INT_MAX + 1);
748-
749-
bool log_methods = FastTiming::enabled () && !FastTiming::is_bare_mode ();
750-
if (XA_UNLIKELY (log_methods)) {
751-
std::unique_ptr<char> jit_log_path {utils.path_combine (androidSystem.get_override_dir (0), "methods.txt")};
752-
jit_log = utils.monodroid_fopen (jit_log_path.get (), "a");
753-
utils.set_world_accessable (jit_log_path.get ());
754-
}
755-
756861
profiler_handle = mono_profiler_create (nullptr);
757862
mono_profiler_set_thread_started_callback (profiler_handle, thread_start);
758863
mono_profiler_set_thread_stopped_callback (profiler_handle, thread_end);
759864

760-
if (XA_UNLIKELY (log_methods)) { // TODO: migrate to the new timing logger
761-
jit_time.mark_start ();
762-
mono_profiler_set_jit_begin_callback (profiler_handle, jit_begin);
763-
mono_profiler_set_jit_done_callback (profiler_handle, jit_done);
764-
mono_profiler_set_jit_failed_callback (profiler_handle, jit_failed);
765-
}
766-
767-
if (FastTiming::enabled () && FastTiming::mode () != TimingMode::Bare) {
768-
switch (FastTiming::mode ()) {
769-
case TimingMode::Extended:
770-
// TODO: implement;
771-
break;
772-
773-
case TimingMode::Verbose:
774-
// TODO: implement
775-
break;
776-
777-
default:
778-
// ignored
779-
break;
780-
}
781-
}
782-
865+
timing_init ();
783866
parse_gdb_options ();
784867

785868
if (wait_for_gdb) {

src/monodroid/jni/robin-map.hh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Dear Emacs, this is a -*- C++ -*- header
2+
#if !defined (__ROBIN_MAP_HH)
3+
#define __ROBIN_MAP_HH
4+
5+
// NDEBUG causes robin_map.h not to include <iostream> which, in turn, prevents indirect inclusion of <mutex>. <mutex>
6+
// conflicts with our std::mutex definition in cppcompat.hh
7+
#if !defined (NDEBUG)
8+
#define NDEBUG
9+
#define NDEBUG_UNDEFINE
10+
#endif
11+
12+
// hush some compiler warnings
13+
#if defined (__clang__)
14+
#pragma clang diagnostic push
15+
#pragma clang diagnostic ignored "-Wunused-parameter"
16+
#endif // __clang__
17+
18+
#include <tsl/robin_map.h>
19+
20+
#if defined (__clang__)
21+
#pragma clang diagnostic pop
22+
#endif // __clang__
23+
24+
#if defined (NDEBUG_UNDEFINE)
25+
#undef NDEBUG
26+
#undef NDEBUG_UNDEFINE
27+
#endif
28+
29+
#endif // ndef __ROBIN_MAP_HH

src/monodroid/jni/strings.hh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,23 @@ namespace xamarin::android::internal
8383

8484
force_inline bool equal (size_t start_index, std::string_view const& s) const noexcept
8585
{
86-
if (s == nullptr) {
86+
if (s.empty ()) {
8787
return false;
8888
}
8989

90-
if (!can_access (s.length ())) {
90+
if (!can_access (s.length () + start_index)) {
9191
return false;
9292
}
9393

94-
if (s.length () != length () + start_index) {
94+
if (s.length () != length () - start_index) {
9595
return false;
9696
}
9797

9898
if (length () == 0) {
9999
return true;
100100
}
101101

102-
return memcmp (_start + start_index, s.data (), s.length ());
102+
return memcmp (_start + start_index, s.data (), length () - start_index) == 0;
103103
}
104104

105105
template<size_t Size>
@@ -476,6 +476,15 @@ namespace xamarin::android::internal
476476
return *this;
477477
}
478478

479+
force_inline string_base& append (std::basic_string_view<TChar> const& s) noexcept
480+
{
481+
if (s.empty ()) {
482+
return *this;
483+
}
484+
485+
return append (s.data (), s.length ());
486+
}
487+
479488
template<size_t LocalMaxStackSize, typename LocalTStorage, typename LocalTChar = char>
480489
force_inline string_base& append (internal::string_base<LocalMaxStackSize, LocalTStorage, LocalTChar> const& str) noexcept
481490
{

0 commit comments

Comments
 (0)