diff options
| author | 2024-01-18 14:32:50 +0000 | |
|---|---|---|
| committer | 2024-01-23 11:38:19 +0000 | |
| commit | 0c36cea9ffc17d0fbdb3797917b095818ca15973 (patch) | |
| tree | 23511261a6ba9a97cd498a9266e289c7960ce9a4 | |
| parent | b25dd2d4f93f4925551f469a9535e3abc79081a7 (diff) | |
Add the current GC in the dumpsys output
This CL surfaces the Garbage Collector currently in use in the Android
Runtime in the output for the 'adb dumpsys package dexopt' command, for
debugging purposes.
Bug: 320283802
Test: atest art_standalone_libartservice_tests
Test: atest ArtServiceTests
Test: adb shell dumpsys package dexopt (to contain the current GC)
Test: adb shell dumpsys package com.android.bluetooth (to not contain the current GC)
Test: adb shell pm art dump (to contain the current GC)
Test: adb shell pm art dump com.android.bluetooth (to not contain the current GC)
Change-Id: Idce3e11c79c8e28ea1c17c184756c052c69a1fe5
| -rw-r--r-- | libartservice/service/java/com/android/server/art/ArtJni.java | 9 | ||||
| -rw-r--r-- | libartservice/service/java/com/android/server/art/DumpHelper.java | 1 | ||||
| -rw-r--r-- | libartservice/service/javatests/com/android/server/art/DumpHelperTest.java | 8 | ||||
| -rw-r--r-- | libartservice/service/native/service.cc | 11 | ||||
| -rw-r--r-- | libartservice/service/native/service.h | 2 | ||||
| -rw-r--r-- | libartservice/service/native/service_test.cc | 7 | ||||
| -rw-r--r-- | runtime/gc/heap.cc | 24 | ||||
| -rw-r--r-- | runtime/gc/heap.h | 2 |
8 files changed, 54 insertions, 10 deletions
diff --git a/libartservice/service/java/com/android/server/art/ArtJni.java b/libartservice/service/java/com/android/server/art/ArtJni.java index 2cc9ea8911..f2868eb0d2 100644 --- a/libartservice/service/java/com/android/server/art/ArtJni.java +++ b/libartservice/service/java/com/android/server/art/ArtJni.java @@ -56,8 +56,17 @@ public class ArtJni { return validateClassLoaderContextNative(dexPath, classLoaderContext); } + /** + * Returns the name of the Garbage Collector currently in use in the Android Runtime. + */ + @NonNull + public static String getGarbageCollector() { + return getGarbageCollectorNative(); + } + @Nullable private static native String validateDexPathNative(@NonNull String dexPath); @Nullable private static native String validateClassLoaderContextNative( @NonNull String dexPath, @NonNull String classLoaderContext); + @NonNull private static native String getGarbageCollectorNative(); } diff --git a/libartservice/service/java/com/android/server/art/DumpHelper.java b/libartservice/service/java/com/android/server/art/DumpHelper.java index d9523b9fae..4d79c30344 100644 --- a/libartservice/service/java/com/android/server/art/DumpHelper.java +++ b/libartservice/service/java/com/android/server/art/DumpHelper.java @@ -75,6 +75,7 @@ public class DumpHelper { .stream() .sorted(Comparator.comparing(PackageState::getPackageName)) .forEach(pkgState -> dumpPackage(pw, snapshot, pkgState)); + pw.printf("\nCurrent GC: %s\n", ArtJni.getGarbageCollector()); } /** diff --git a/libartservice/service/javatests/com/android/server/art/DumpHelperTest.java b/libartservice/service/javatests/com/android/server/art/DumpHelperTest.java index be2887d20f..520cda0835 100644 --- a/libartservice/service/javatests/com/android/server/art/DumpHelperTest.java +++ b/libartservice/service/javatests/com/android/server/art/DumpHelperTest.java @@ -63,7 +63,7 @@ public class DumpHelperTest { @Rule public StaticMockitoRule mockitoRule = - new StaticMockitoRule(SystemProperties.class, Constants.class); + new StaticMockitoRule(SystemProperties.class, Constants.class, ArtJni.class); @Mock private DumpHelper.Injector mInjector; @Mock private ArtManagerLocal mArtManagerLocal; @@ -83,6 +83,8 @@ public class DumpHelperTest { .when(SystemProperties.get(argThat(arg -> arg.startsWith("ro.dalvik.vm.isa.")))) .thenReturn(""); + lenient().when(ArtJni.getGarbageCollector()).thenReturn("CollectorTypeCMC"); + lenient().when(mInjector.getArtManagerLocal()).thenReturn(mArtManagerLocal); lenient().when(mInjector.getDexUseManager()).thenReturn(mDexUseManagerLocal); @@ -131,7 +133,9 @@ public class DumpHelperTest { + " arm: [status=verify] [reason=install] [primary-abi]\n" + " [location is /somewhere/app/bar/oat/arm/base.odex]\n" + " arm64: [status=verify] [reason=install]\n" - + " [location is /somewhere/app/bar/oat/arm64/base.odex]\n"; + + " [location is /somewhere/app/bar/oat/arm64/base.odex]\n" + + "\n" + + "Current GC: CollectorTypeCMC\n"; var stringWriter = new StringWriter(); mDumpHelper.dump(new PrintWriter(stringWriter), mSnapshot); diff --git a/libartservice/service/native/service.cc b/libartservice/service/native/service.cc index 7d223e5c35..5901efd9f6 100644 --- a/libartservice/service/native/service.cc +++ b/libartservice/service/native/service.cc @@ -24,7 +24,9 @@ #include "android-base/file.h" #include "android-base/result.h" #include "class_loader_context.h" +#include "gc/heap.h" #include "nativehelper/utils.h" +#include "runtime.h" namespace art { namespace service { @@ -76,6 +78,10 @@ Result<void> ValidateDexPath(const std::string& dex_path) { return {}; } +std::string GetGarbageCollector() { + return Runtime::Current()->GetHeap()->GetForegroundCollectorName(); +} + extern "C" JNIEXPORT jstring JNICALL Java_com_android_server_art_ArtJni_validateDexPathNative(JNIEnv* env, jobject, jstring j_dex_path) { std::string dex_path(GET_UTF_OR_RETURN(env, j_dex_path)); @@ -116,5 +122,10 @@ Java_com_android_server_art_ArtJni_validateClassLoaderContextNative( return nullptr; } +extern "C" JNIEXPORT jstring JNICALL +Java_com_android_server_art_ArtJni_getGarbageCollectorNative(JNIEnv* env, jobject) { + return CREATE_UTF_OR_RETURN(env, GetGarbageCollector()).release(); +} + } // namespace service } // namespace art diff --git a/libartservice/service/native/service.h b/libartservice/service/native/service.h index 7f43f17f0b..85c7dcd39f 100644 --- a/libartservice/service/native/service.h +++ b/libartservice/service/native/service.h @@ -34,6 +34,8 @@ android::base::Result<void> ValidatePathElement(const std::string& path_element, android::base::Result<void> ValidateDexPath(const std::string& dex_path); +std::string GetGarbageCollector(); + } // namespace service } // namespace art diff --git a/libartservice/service/native/service_test.cc b/libartservice/service/native/service_test.cc index 8300bf5e76..d1d429edd7 100644 --- a/libartservice/service/native/service_test.cc +++ b/libartservice/service/native/service_test.cc @@ -17,6 +17,7 @@ #include "service.h" #include "android-base/result-gmock.h" +#include "common_runtime_test.h" #include "gtest/gtest.h" namespace art { @@ -106,6 +107,12 @@ TEST_F(ArtServiceTest, ValidateDexPathNul) { HasError(WithMessage("Path '/a/\0/b.apk' has invalid character '\\0'"s))); } +class ArtServiceGcTest : public CommonRuntimeTest {}; + +TEST_F(ArtServiceGcTest, GetGarbageCollector) { + EXPECT_THAT(GetGarbageCollector(), testing::HasSubstr("CollectorType")); +} + } // namespace } // namespace service } // namespace art diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc index 9c23423276..c1505dd7f5 100644 --- a/runtime/gc/heap.cc +++ b/runtime/gc/heap.cc @@ -16,20 +16,18 @@ #include "heap.h" +#include <sys/types.h> +#include <unistd.h> + #include <limits> -#include "android-base/thread_annotations.h" -#if defined(__BIONIC__) || defined(__GLIBC__) || defined(ANDROID_HOST_MUSL) -#include <malloc.h> // For mallinfo() -#endif #include <memory> #include <random> -#include <unistd.h> -#include <sys/types.h> +#include <sstream> #include <vector> -#include "android-base/stringprintf.h" - #include "allocation_listener.h" +#include "android-base/stringprintf.h" +#include "android-base/thread_annotations.h" #include "art_field-inl.h" #include "backtrace_helper.h" #include "base/allocator.h" @@ -108,6 +106,10 @@ #include "verify_object-inl.h" #include "well_known_classes.h" +#if defined(__BIONIC__) || defined(__GLIBC__) || defined(ANDROID_HOST_MUSL) +#include <malloc.h> // For mallinfo() +#endif + namespace art HIDDEN { #ifdef ART_TARGET_ANDROID @@ -4762,5 +4764,11 @@ bool Heap::AddHeapTask(gc::HeapTask* task) { return true; } +std::string Heap::GetForegroundCollectorName() { + std::ostringstream oss; + oss << foreground_collector_type_; + return oss.str(); +} + } // namespace gc } // namespace art diff --git a/runtime/gc/heap.h b/runtime/gc/heap.h index d0944e0f55..1243f4f59a 100644 --- a/runtime/gc/heap.h +++ b/runtime/gc/heap.h @@ -861,6 +861,8 @@ class Heap { bool IsMovingGc() const { return IsMovingGc(CurrentCollectorType()); } CollectorType GetForegroundCollectorType() const { return foreground_collector_type_; } + // EXPORT is needed to make this method visible for libartservice. + EXPORT std::string GetForegroundCollectorName(); bool IsGcConcurrentAndMoving() const { if (IsGcConcurrent() && IsMovingGc(collector_type_)) { |