Skip to content

Commit 82cfb8a

Browse files
committed
Moving on: logging + timing
1 parent dc9f730 commit 82cfb8a

15 files changed

+1184
-0
lines changed

src/native-clr/host/host.cc

+18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
#include <host/host.hh>
22
#include <host/host-jni.hh>
33
#include <runtime-base/android-system.hh>
4+
#include <runtime-base/logger.hh>
5+
#include <runtime-base/timing-internal.hh>
46
#include <shared/log_types.hh>
57

68
using namespace xamarin::android;
79

10+
void Host::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass klass, jstring lang, jobjectArray runtimeApksJava,
11+
jstring runtimeNativeLibDir, jobjectArray appDirs, jint localDateTimeOffset, jobject loader,
12+
jobjectArray assembliesJava, jboolean isEmulator, jboolean haveSplitApks)
13+
{
14+
Logger::init_logging_categories ();
15+
16+
// If fast logging is disabled, log messages immediately
17+
FastTiming::initialize ((Logger::log_timing_categories() & LogTimingCategories::FastBare) != LogTimingCategories::FastBare);
18+
19+
size_t total_time_index;
20+
if (FastTiming::enabled ()) [[unlikely]] {
21+
_timing = std::make_unique<Timing> ();
22+
total_time_index = internal_timing->start_event (TimingEventKind::TotalRuntimeInit);
23+
}
24+
}
25+
826
auto Host::Java_JNI_OnLoad (JavaVM *vm, [[maybe_unused]] void *reserved) noexcept -> jint
927
{
1028
log_write (LOG_DEFAULT, LogLevel::Info, "Host init");

src/native-clr/include/constants.hh

+3
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,8 @@ namespace xamarin::android {
9292
// 64-bit unsigned or 64-bit signed with sign
9393
static constexpr size_t MAX_INTEGER_DIGIT_COUNT_BASE10 = 21uz;
9494
static constexpr size_t INTEGER_BASE10_BUFFER_SIZE = MAX_INTEGER_DIGIT_COUNT_BASE10 + 1uz;
95+
96+
// Documented in NDK's <android/log.h> comments
97+
static constexpr size_t MAX_LOGCAT_MESSAGE_LENGTH = 1023uz;
9598
};
9699
}

src/native-clr/include/host/host.hh

+14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
#pragma once
22

3+
#include <memory>
4+
35
#include <jni.h>
46

7+
#include "../runtime-base/timing.hh"
58
#include "../shared/log_types.hh"
69

710
namespace xamarin::android {
811
class Host
912
{
1013
public:
1114
static auto Java_JNI_OnLoad (JavaVM *vm, void *reserved) noexcept -> jint;
15+
static void Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass klass, jstring lang, jobjectArray runtimeApksJava,
16+
jstring runtimeNativeLibDir, jobjectArray appDirs, jint localDateTimeOffset, jobject loader,
17+
jobjectArray assembliesJava, jboolean isEmulator, jboolean haveSplitApks);
18+
19+
static auto get_timing () -> Timing*
20+
{
21+
return _timing.get ();
22+
}
23+
24+
private:
25+
static inline std::unique_ptr<Timing> _timing{};
1226
};
1327
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#pragma once
2+
3+
#include <cstdio>
4+
5+
#include <string_view>
6+
7+
#include <shared/log_types.hh>
8+
#include "strings.hh"
9+
10+
namespace xamarin::android {
11+
class Logger
12+
{
13+
public:
14+
static void init_logging_categories () noexcept;
15+
static void init_reference_logging (std::string_view const& override_dir) noexcept;
16+
17+
static auto log_timing_categories () noexcept -> LogTimingCategories
18+
{
19+
return _log_timing_categories;
20+
}
21+
22+
static void set_gc_spew_enabled (bool yesno) noexcept
23+
{
24+
_gc_spew_enabled = yesno;
25+
}
26+
27+
static auto gc_spew_enabled () noexcept -> bool
28+
{
29+
return _gc_spew_enabled;
30+
}
31+
32+
static auto gref_log () -> FILE*
33+
{
34+
return _gref_log;
35+
}
36+
37+
static auto lref_log () -> FILE*
38+
{
39+
return _lref_log;
40+
}
41+
42+
static auto gref_to_logcat () -> bool
43+
{
44+
return _gref_to_logcat;
45+
}
46+
47+
static auto lref_to_logcat () -> bool
48+
{
49+
return _lref_to_logcat;
50+
}
51+
52+
private:
53+
static bool set_category (std::string_view const& name, string_segment& arg, unsigned int entry, bool arg_starts_with_name = false) noexcept;
54+
55+
private:
56+
static inline LogTimingCategories _log_timing_categories;
57+
static inline bool _gc_spew_enabled = false;
58+
static inline FILE *_gref_log = nullptr;
59+
static inline FILE *_lref_log = nullptr;
60+
static inline bool _gref_to_logcat = false;
61+
static inline bool _lref_to_logcat = false;
62+
};
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
namespace xamarin::android
4+
{
5+
class MonodroidState
6+
{
7+
public:
8+
static auto is_startup_in_progress () noexcept -> bool
9+
{
10+
return startup_in_progress;
11+
}
12+
13+
static void mark_startup_done () noexcept
14+
{
15+
startup_in_progress = false;
16+
}
17+
18+
private:
19+
inline static bool startup_in_progress = true;
20+
};
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include <mutex>
4+
5+
#include "monodroid-state.hh"
6+
7+
namespace xamarin::android
8+
{
9+
class StartupAwareLock final
10+
{
11+
public:
12+
explicit StartupAwareLock (std::mutex &m)
13+
: lock (m)
14+
{
15+
if (MonodroidState::is_startup_in_progress ()) {
16+
// During startup we run without threads, do nothing
17+
return;
18+
}
19+
20+
lock.lock ();
21+
}
22+
23+
~StartupAwareLock ()
24+
{
25+
if (MonodroidState::is_startup_in_progress ()) {
26+
return;
27+
}
28+
29+
lock.unlock ();
30+
}
31+
32+
StartupAwareLock (StartupAwareLock const&) = delete;
33+
StartupAwareLock (StartupAwareLock const&&) = delete;
34+
35+
StartupAwareLock& operator= (StartupAwareLock const&) = delete;
36+
37+
private:
38+
std::mutex& lock;
39+
};
40+
}

src/native-clr/include/runtime-base/strings.hh

+29
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <array>
44
#include <cstring>
55
#include <cerrno>
6+
#include <expected>
67
#include <limits>
78
#include <string_view>
89
#include <type_traits>
@@ -21,6 +22,24 @@ namespace xamarin::android {
2122
static constexpr bool BoundsCheck = false;
2223
#endif
2324

25+
enum class string_segment_error
26+
{
27+
index_out_of_range,
28+
};
29+
30+
static inline auto to_string (string_segment_error error) -> std::string_view const
31+
{
32+
using std::operator""sv;
33+
34+
switch (error) {
35+
case string_segment_error::index_out_of_range:
36+
return "Index out of range"sv;
37+
38+
default:
39+
return "Unknown error"sv;
40+
}
41+
}
42+
2443
class string_segment
2544
{
2645
public:
@@ -36,6 +55,16 @@ namespace xamarin::android {
3655
return _start;
3756
}
3857

58+
[[gnu::always_inline]]
59+
auto at (size_t offset) const noexcept -> std::expected<const char*, string_segment_error>
60+
{
61+
if (offset >= length ()) {
62+
return std::unexpected (string_segment_error::index_out_of_range);
63+
}
64+
65+
return _start + offset;
66+
}
67+
3968
[[gnu::always_inline]]
4069
auto length () const noexcept -> size_t
4170
{

0 commit comments

Comments
 (0)