Skip to content

Commit

Permalink
Moving on: logging + timing
Browse files Browse the repository at this point in the history
  • Loading branch information
grendello committed Dec 4, 2024
1 parent dc9f730 commit 82cfb8a
Show file tree
Hide file tree
Showing 15 changed files with 1,184 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/native-clr/host/host.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
#include <host/host.hh>
#include <host/host-jni.hh>
#include <runtime-base/android-system.hh>
#include <runtime-base/logger.hh>
#include <runtime-base/timing-internal.hh>
#include <shared/log_types.hh>

using namespace xamarin::android;

void Host::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass klass, jstring lang, jobjectArray runtimeApksJava,
jstring runtimeNativeLibDir, jobjectArray appDirs, jint localDateTimeOffset, jobject loader,
jobjectArray assembliesJava, jboolean isEmulator, jboolean haveSplitApks)
{
Logger::init_logging_categories ();

// If fast logging is disabled, log messages immediately
FastTiming::initialize ((Logger::log_timing_categories() & LogTimingCategories::FastBare) != LogTimingCategories::FastBare);

size_t total_time_index;
if (FastTiming::enabled ()) [[unlikely]] {
_timing = std::make_unique<Timing> ();
total_time_index = internal_timing->start_event (TimingEventKind::TotalRuntimeInit);
}
}

auto Host::Java_JNI_OnLoad (JavaVM *vm, [[maybe_unused]] void *reserved) noexcept -> jint
{
log_write (LOG_DEFAULT, LogLevel::Info, "Host init");
Expand Down
3 changes: 3 additions & 0 deletions src/native-clr/include/constants.hh
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,8 @@ namespace xamarin::android {
// 64-bit unsigned or 64-bit signed with sign
static constexpr size_t MAX_INTEGER_DIGIT_COUNT_BASE10 = 21uz;
static constexpr size_t INTEGER_BASE10_BUFFER_SIZE = MAX_INTEGER_DIGIT_COUNT_BASE10 + 1uz;

// Documented in NDK's <android/log.h> comments
static constexpr size_t MAX_LOGCAT_MESSAGE_LENGTH = 1023uz;
};
}
14 changes: 14 additions & 0 deletions src/native-clr/include/host/host.hh
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
#pragma once

#include <memory>

#include <jni.h>

#include "../runtime-base/timing.hh"
#include "../shared/log_types.hh"

namespace xamarin::android {
class Host
{
public:
static auto Java_JNI_OnLoad (JavaVM *vm, void *reserved) noexcept -> jint;
static void Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass klass, jstring lang, jobjectArray runtimeApksJava,
jstring runtimeNativeLibDir, jobjectArray appDirs, jint localDateTimeOffset, jobject loader,
jobjectArray assembliesJava, jboolean isEmulator, jboolean haveSplitApks);

static auto get_timing () -> Timing*
{
return _timing.get ();
}

private:
static inline std::unique_ptr<Timing> _timing{};
};
}
63 changes: 63 additions & 0 deletions src/native-clr/include/runtime-base/logger.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include <cstdio>

#include <string_view>

#include <shared/log_types.hh>
#include "strings.hh"

namespace xamarin::android {
class Logger
{
public:
static void init_logging_categories () noexcept;
static void init_reference_logging (std::string_view const& override_dir) noexcept;

static auto log_timing_categories () noexcept -> LogTimingCategories
{
return _log_timing_categories;
}

static void set_gc_spew_enabled (bool yesno) noexcept
{
_gc_spew_enabled = yesno;
}

static auto gc_spew_enabled () noexcept -> bool
{
return _gc_spew_enabled;
}

static auto gref_log () -> FILE*
{
return _gref_log;
}

static auto lref_log () -> FILE*
{
return _lref_log;
}

static auto gref_to_logcat () -> bool
{
return _gref_to_logcat;
}

static auto lref_to_logcat () -> bool
{
return _lref_to_logcat;
}

private:
static bool set_category (std::string_view const& name, string_segment& arg, unsigned int entry, bool arg_starts_with_name = false) noexcept;

private:
static inline LogTimingCategories _log_timing_categories;
static inline bool _gc_spew_enabled = false;
static inline FILE *_gref_log = nullptr;
static inline FILE *_lref_log = nullptr;
static inline bool _gref_to_logcat = false;
static inline bool _lref_to_logcat = false;
};
}
21 changes: 21 additions & 0 deletions src/native-clr/include/runtime-base/monodroid-state.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

namespace xamarin::android
{
class MonodroidState
{
public:
static auto is_startup_in_progress () noexcept -> bool
{
return startup_in_progress;
}

static void mark_startup_done () noexcept
{
startup_in_progress = false;
}

private:
inline static bool startup_in_progress = true;
};
}
40 changes: 40 additions & 0 deletions src/native-clr/include/runtime-base/startup-aware-lock.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <mutex>

#include "monodroid-state.hh"

namespace xamarin::android
{
class StartupAwareLock final
{
public:
explicit StartupAwareLock (std::mutex &m)
: lock (m)
{
if (MonodroidState::is_startup_in_progress ()) {
// During startup we run without threads, do nothing
return;
}

lock.lock ();
}

~StartupAwareLock ()
{
if (MonodroidState::is_startup_in_progress ()) {
return;
}

lock.unlock ();
}

StartupAwareLock (StartupAwareLock const&) = delete;
StartupAwareLock (StartupAwareLock const&&) = delete;

StartupAwareLock& operator= (StartupAwareLock const&) = delete;

private:
std::mutex& lock;
};
}
29 changes: 29 additions & 0 deletions src/native-clr/include/runtime-base/strings.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <array>
#include <cstring>
#include <cerrno>
#include <expected>
#include <limits>
#include <string_view>
#include <type_traits>
Expand All @@ -21,6 +22,24 @@ namespace xamarin::android {
static constexpr bool BoundsCheck = false;
#endif

enum class string_segment_error
{
index_out_of_range,
};

static inline auto to_string (string_segment_error error) -> std::string_view const
{
using std::operator""sv;

switch (error) {
case string_segment_error::index_out_of_range:
return "Index out of range"sv;

default:
return "Unknown error"sv;
}
}

class string_segment
{
public:
Expand All @@ -36,6 +55,16 @@ namespace xamarin::android {
return _start;
}

[[gnu::always_inline]]
auto at (size_t offset) const noexcept -> std::expected<const char*, string_segment_error>
{
if (offset >= length ()) {
return std::unexpected (string_segment_error::index_out_of_range);
}

return _start + offset;
}

[[gnu::always_inline]]
auto length () const noexcept -> size_t
{
Expand Down
Loading

0 comments on commit 82cfb8a

Please sign in to comment.