-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[Android][libs] Introduce GetLocalUtcOffset temporary fast result #74459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
a3f0e9f
a44084a
1edaeee
0712f57
96bf37f
582c359
9a2a8a2
f352b3a
f7dc8e9
32451c8
1a55a0c
61af7b7
50ff121
b1531b6
533ad1c
ef7729e
55fcccc
1cfc352
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,7 +107,7 @@ private static int ParseGMTNumericZone(string name) | |
| { | ||
| return new TimeZoneInfo(id, TimeSpan.FromSeconds(0), id, name, name, null, disableDaylightSavingTime:true); | ||
| } | ||
| if (name.StartsWith("GMT", StringComparison.Ordinal)) | ||
| if (name[0] == 'G' && name[1] == 'M' && name[2] == 'T') | ||
| { | ||
| return new TimeZoneInfo(id, TimeSpan.FromSeconds(ParseGMTNumericZone(name)), id, name, name, null, disableDaylightSavingTime:true); | ||
| } | ||
|
|
@@ -142,6 +142,56 @@ private static TimeZoneInfo GetLocalTimeZoneCore() | |
| return Utc; | ||
| } | ||
|
|
||
| private static TimeSpan GetCacheLocalUtcOffset(DateTime dateTime, TimeZoneInfoOptions flags) | ||
| { | ||
| CachedData cachedData = s_cachedData; | ||
| return cachedData.Local.GetUtcOffset(dateTime, flags, cachedData); | ||
mdh1418 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private static TimeSpan? _localUtcOffset; | ||
mdh1418 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private static object _localUtcOffsetLock = new(); | ||
| private static Thread? _loadAndroidTZData; | ||
mdh1418 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Shortcut for TimeZoneInfo.Local.GetUtcOffset | ||
| // On Android, loading AndroidTZData while obtaining cachedData.Local is expensive for startup. | ||
| // We introduce a fast result for GetLocalUtcOffset that relies on the date time offset being | ||
| // passed into monovm_initialize(_preparsed) from Java in seconds as LOCAL_DATE_TIME_OFFSET. | ||
| // However, to handle timezone changes during the app lifetime, AndroidTZData needs to be loaded. | ||
| // The fast path is initially used, and we start a background thread to get cachedData.Local | ||
| internal static TimeSpan GetLocalUtcOffset(DateTime dateTime, TimeZoneInfoOptions flags) | ||
| { | ||
| if (_localUtcOffset != null) // The background thread finished, the cache is loaded. | ||
| return GetCacheLocalUtcOffset(dateTime, flags); | ||
|
|
||
| if (_localUtcOffset == null && _loadAndroidTZData == null) // The cache isn't loaded and no background thread has been created | ||
| { | ||
| lock (_localUtcOffsetLock) | ||
| { | ||
| // GetLocalUtcOffset may be called multiple times before a cache is loaded and a background thread is running, | ||
| // once the lock is available, check for a cache and background thread. | ||
| if (_localUtcOffset != null) | ||
| return GetCacheLocalUtcOffset(dateTime, flags); | ||
|
|
||
| if (_loadAndroidTZData == null) | ||
| { | ||
| _loadAndroidTZData = new Thread(() => { | ||
| Thread.Sleep(1000); | ||
|
||
| _localUtcOffset = GetCacheLocalUtcOffset(dateTime, flags); | ||
| }); | ||
| _loadAndroidTZData.IsBackground = true; | ||
| _loadAndroidTZData.Start(); | ||
mdh1418 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| object? localDateTimeOffset = AppContext.GetData("System.TimeZoneInfo.LocalDateTimeOffset"); | ||
| if (localDateTimeOffset == null) | ||
| return GetCacheLocalUtcOffset(dateTime, flags); // If no offset property provided through monovm app context, default | ||
|
|
||
| long localDateTimeOffsetSeconds = Convert.ToInt32(localDateTimeOffset); | ||
mdh1418 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| TimeSpan offset = TimeSpan.FromSeconds(localDateTimeOffsetSeconds); | ||
| return offset; | ||
| } | ||
|
|
||
| private static TimeZoneInfoResult TryGetTimeZoneFromLocalMachineCore(string id, out TimeZoneInfo? value, out Exception? e) | ||
| { | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -203,7 +203,7 @@ cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) | |
| } | ||
|
|
||
| int | ||
| mono_droid_runtime_init (const char* executable, int managed_argc, char* managed_argv[]) | ||
| mono_droid_runtime_init (const char* executable, int managed_argc, char* managed_argv[], int local_date_time_offset) | ||
| { | ||
| // NOTE: these options can be set via command line args for adb or xharness, see AndroidSampleApp.csproj | ||
|
|
||
|
|
@@ -225,13 +225,24 @@ mono_droid_runtime_init (const char* executable, int managed_argc, char* managed | |
|
|
||
| // TODO: set TRUSTED_PLATFORM_ASSEMBLIES, APP_PATHS and NATIVE_DLL_SEARCH_DIRECTORIES | ||
|
|
||
| const char* appctx_keys[2]; | ||
| const char* appctx_keys[3]; | ||
| appctx_keys[0] = "RUNTIME_IDENTIFIER"; | ||
| appctx_keys[1] = "APP_CONTEXT_BASE_DIRECTORY"; | ||
| appctx_keys[2] = "System.TimeZoneInfo.LocalDateTimeOffset"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we considering this as "internal-use only"? Or would we expect developers to get this AppContext setting? With its current name, it kind of seems like an "official" setting. I wonder if we should rename it to better indicate that we don't expect others to use it. (and quite possibly we could remove it in the future if we refactor this code.)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we want to set the MonoVMProperty on Xamarin-Android at all times. And to address #74459 (comment), I believe we would want to maintain alignment of AndroidAppBuilder in I think the only purpose for this property is to get the fast path local date time offset, but there was a concern whether we needed this to follow runtime config naming #74459 (comment). |
||
|
|
||
| const char* appctx_values[2]; | ||
| const char* appctx_values[3]; | ||
| appctx_values[0] = ANDROID_RUNTIME_IDENTIFIER; | ||
| appctx_values[1] = bundle_path; | ||
| char local_date_time_offset_buffer[32]; | ||
| snprintf (local_date_time_offset_buffer, sizeof(local_date_time_offset_buffer), "%d", local_date_time_offset); | ||
| appctx_values[2] = strdup (local_date_time_offset_buffer); | ||
|
|
||
| MonoCoreLocalTime mclt = { | ||
| local_date_time_offset, | ||
| }; | ||
| MonoCoreRuntimeProperties mcrp = { | ||
| &mclt, | ||
| }; | ||
|
|
||
| char *file_name = RUNTIMECONFIG_BIN_FILE; | ||
| int str_len = strlen (bundle_path) + strlen (file_name) + 1; // +1 is for the "/" | ||
|
|
@@ -251,7 +262,7 @@ mono_droid_runtime_init (const char* executable, int managed_argc, char* managed | |
| free (file_path); | ||
| } | ||
|
|
||
| monovm_initialize(2, appctx_keys, appctx_values); | ||
| monovm_initialize_preparsed(&mcrp, 3, appctx_keys, appctx_values); | ||
|
|
||
| mono_debug_init (MONO_DEBUG_FORMAT_MONO); | ||
| mono_install_assembly_preload_hook (mono_droid_assembly_preload_hook, NULL); | ||
|
|
@@ -318,7 +329,7 @@ Java_net_dot_MonoRunner_setEnv (JNIEnv* env, jobject thiz, jstring j_key, jstrin | |
| } | ||
|
|
||
| int | ||
| Java_net_dot_MonoRunner_initRuntime (JNIEnv* env, jobject thiz, jstring j_files_dir, jstring j_cache_dir, jstring j_testresults_dir, jstring j_entryPointLibName, jobjectArray j_args) | ||
| Java_net_dot_MonoRunner_initRuntime (JNIEnv* env, jobject thiz, jstring j_files_dir, jstring j_cache_dir, jstring j_testresults_dir, jstring j_entryPointLibName, jobjectArray j_args, long current_local_time) | ||
| { | ||
| char file_dir[2048]; | ||
| char cache_dir[2048]; | ||
|
|
@@ -347,7 +358,7 @@ Java_net_dot_MonoRunner_initRuntime (JNIEnv* env, jobject thiz, jstring j_files_ | |
| managed_argv[i + 1] = (*env)->GetStringUTFChars(env, j_arg, NULL); | ||
| } | ||
|
|
||
| int res = mono_droid_runtime_init (executable, managed_argc, managed_argv); | ||
| int res = mono_droid_runtime_init (executable, managed_argc, managed_argv, current_local_time); | ||
|
|
||
| for (int i = 0; i < args_len; ++i) | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.