Merge "Add test api to gc system and sysui" into sc-dev

This commit is contained in:
Winson Chung
2021-05-11 05:02:35 +00:00
committed by Android (Google) Code Review
5 changed files with 109 additions and 0 deletions

View File

@@ -271,4 +271,9 @@ oneway interface IStatusBar
* @param enable {@code true} if enable, otherwise set to {@code false}.
*/
void setNavigationBarLumaSamplingEnabled(int displayId, boolean enable);
/**
* Triggers a GC in the system and status bar.
*/
void runGcForTest();
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.internal.util;
import android.util.Slog;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* A helper class to handle gc'ing a process, mainly used for testing.
*
* @hide
*/
public final class GcUtils {
private static final String TAG = GcUtils.class.getSimpleName();
/**
* Runs a GC and attempts to wait for finalization.
*/
public static void runGcAndFinalizersSync() {
Runtime.getRuntime().gc();
Runtime.getRuntime().runFinalization();
final CountDownLatch fence = new CountDownLatch(1);
createFinalizationObserver(fence);
try {
do {
Runtime.getRuntime().gc();
Runtime.getRuntime().runFinalization();
} while (!fence.await(100, TimeUnit.MILLISECONDS));
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
Slog.v(TAG, "Running gc and finalizers");
}
/**
* Create the observer in the scope of a method to minimize the chance that
* it remains live in a DEX/machine register at the point of the fence guard.
* This must be kept to avoid R8 inlining it.
*/
private static void createFinalizationObserver(CountDownLatch fence) {
new Object() {
@Override
protected void finalize() throws Throwable {
try {
fence.countDown();
} finally {
super.finalize();
}
}
};
}
// Uninstantiable
private GcUtils() {}
}

View File

@@ -57,6 +57,7 @@ import androidx.annotation.NonNull;
import com.android.internal.os.SomeArgs;
import com.android.internal.statusbar.IStatusBar;
import com.android.internal.statusbar.StatusBarIcon;
import com.android.internal.util.GcUtils;
import com.android.internal.view.AppearanceRegion;
import com.android.systemui.statusbar.CommandQueue.Callbacks;
import com.android.systemui.statusbar.commandline.CommandRegistry;
@@ -1086,6 +1087,12 @@ public class CommandQueue extends IStatusBar.Stub implements CallbackController<
thr.start();
}
@Override
public void runGcForTest() {
// Gc sysui
GcUtils.runGcAndFinalizersSync();
}
private final class H extends Handler {
private H(Looper l) {
super(l);

View File

@@ -71,6 +71,7 @@ import com.android.internal.statusbar.NotificationVisibility;
import com.android.internal.statusbar.RegisterStatusBarResult;
import com.android.internal.statusbar.StatusBarIcon;
import com.android.internal.util.DumpUtils;
import com.android.internal.util.GcUtils;
import com.android.internal.view.AppearanceRegion;
import com.android.server.LocalServices;
import com.android.server.UiThread;
@@ -649,6 +650,7 @@ public class StatusBarManagerService extends IStatusBarService.Stub implements D
// ================================================================================
// From IStatusBarService
// ================================================================================
@Override
public void expandNotificationsPanel() {
enforceExpandStatusBar();
@@ -971,6 +973,22 @@ public class StatusBarManagerService extends IStatusBarService.Stub implements D
return new int[] {disable1, disable2};
}
void runGcForTest() {
if (!Build.IS_DEBUGGABLE) {
throw new SecurityException("runGcForTest requires a debuggable build");
}
// Gc the system along the way
GcUtils.runGcAndFinalizersSync();
if (mBar != null) {
try {
mBar.runGcForTest();
} catch (RemoteException ex) {
}
}
}
@Override
public void setIcon(String slot, String iconPackage, int iconId, int iconLevel,
String contentDescription) {

View File

@@ -75,6 +75,8 @@ public class StatusBarShellCommand extends ShellCommand {
return runSendDisableFlag();
case "tracing":
return runTracing();
case "run-gc":
return runGc();
// Handle everything that would be handled in `handleDefaultCommand()` explicitly,
// so the default can be to pass all args to StatusBar
case "-h":
@@ -213,6 +215,11 @@ public class StatusBarShellCommand extends ShellCommand {
return 0;
}
private int runGc() {
mInterface.runGcForTest();
return 0;
}
@Override
public void onHelp() {
final PrintWriter pw = getOutPrintWriter();