From 77f6bbff3c527907c8aecf568d541b70e1c6bd7c Mon Sep 17 00:00:00 2001 From: Nikita Iashchenko Date: Mon, 26 Apr 2021 15:38:54 +0100 Subject: [PATCH] Refactor ddm ChunkHandler utility methods from libcore to framework As a part of internal libcore cleanup three utility methods for ddm chunks (de)serialization were removed from the CorePlatformApi set and moved to framework. As a part of API council review made following changes: * Renamed connected/disconnected callback methods to onConnected/onDisconnected, respectively * Made unregisterHandler method a part of core api * Renamed threadNotify/enableRecentAllocations setters Bug: 154796679 Bug: 184654804 Test: m droid Change-Id: I1d43f12aaf70c7c079577d3f3248248ed39f9865 --- core/java/android/app/ActivityThread.java | 2 +- core/java/android/ddm/DdmHandle.java | 51 +++++++++++++++++++ core/java/android/ddm/DdmHandleAppName.java | 8 +-- core/java/android/ddm/DdmHandleExit.java | 12 +++-- core/java/android/ddm/DdmHandleHeap.java | 18 +++---- core/java/android/ddm/DdmHandleHello.java | 18 +++---- .../java/android/ddm/DdmHandleNativeHeap.java | 14 ++--- core/java/android/ddm/DdmHandleProfiling.java | 30 ++++++----- core/java/android/ddm/DdmHandleViewDebug.java | 14 ++--- 9 files changed, 108 insertions(+), 59 deletions(-) create mode 100644 core/java/android/ddm/DdmHandle.java diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index 65f2c02faa858..71cc11b19bc62 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -6381,7 +6381,7 @@ public final class ActivityThread extends ClientTransactionHandler { VMDebug.setAllocTrackerStackDepth(Integer.parseInt(property)); } if (data.trackAllocation) { - DdmVmInternal.enableRecentAllocations(true); + DdmVmInternal.setRecentAllocationsTrackingEnabled(true); } // Note when this process has started. Process.setStartTimes(SystemClock.elapsedRealtime(), SystemClock.uptimeMillis()); diff --git a/core/java/android/ddm/DdmHandle.java b/core/java/android/ddm/DdmHandle.java new file mode 100644 index 0000000000000..0505feea9081b --- /dev/null +++ b/core/java/android/ddm/DdmHandle.java @@ -0,0 +1,51 @@ +/* + * 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 android.ddm; + +import org.apache.harmony.dalvik.ddmc.ChunkHandler; + +import java.nio.ByteBuffer; + +/** + * Contains utility methods for chunk serialization and deserialization. + */ +public abstract class DdmHandle extends ChunkHandler { + + /** + * Utility function to copy a String out of a ByteBuffer. + * + * This is here because multiple chunk handlers can make use of it, + * and there's nowhere better to put it. + */ + public static String getString(ByteBuffer buf, int len) { + char[] data = new char[len]; + for (int i = 0; i < len; i++) { + data[i] = buf.getChar(); + } + return new String(data); + } + + /** + * Utility function to copy a String into a ByteBuffer. + */ + public static void putString(ByteBuffer buf, String str) { + int len = str.length(); + for (int i = 0; i < len; i++) { + buf.putChar(str.charAt(i)); + } + } + +} diff --git a/core/java/android/ddm/DdmHandleAppName.java b/core/java/android/ddm/DdmHandleAppName.java index 35da062329b3c..e19f19f8fe8c6 100644 --- a/core/java/android/ddm/DdmHandleAppName.java +++ b/core/java/android/ddm/DdmHandleAppName.java @@ -30,9 +30,9 @@ import java.nio.ByteBuffer; /** * Track our app name. We don't (currently) handle any inbound packets. */ -public class DdmHandleAppName extends ChunkHandler { +public class DdmHandleAppName extends DdmHandle { - public static final int CHUNK_APNM = type("APNM"); + public static final int CHUNK_APNM = ChunkHandler.type("APNM"); private static volatile Names sNames = new Names("", ""); @@ -51,13 +51,13 @@ public class DdmHandleAppName extends ChunkHandler { * Called when the DDM server connects. The handler is allowed to * send messages to the server. */ - public void connected() {} + public void onConnected() {} /** * Called when the DDM server disconnects. Can be used to disable * periodic transmissions or clean up saved state. */ - public void disconnected() {} + public void onDisconnected() {} /** * Handle a chunk of data. diff --git a/core/java/android/ddm/DdmHandleExit.java b/core/java/android/ddm/DdmHandleExit.java index 74ae37a42f568..a9e3d16077290 100644 --- a/core/java/android/ddm/DdmHandleExit.java +++ b/core/java/android/ddm/DdmHandleExit.java @@ -16,18 +16,20 @@ package android.ddm; +import android.util.Log; + import org.apache.harmony.dalvik.ddmc.Chunk; import org.apache.harmony.dalvik.ddmc.ChunkHandler; import org.apache.harmony.dalvik.ddmc.DdmServer; -import android.util.Log; + import java.nio.ByteBuffer; /** * Handle an EXIT chunk. */ -public class DdmHandleExit extends ChunkHandler { +public class DdmHandleExit extends DdmHandle { - public static final int CHUNK_EXIT = type("EXIT"); + public static final int CHUNK_EXIT = ChunkHandler.type("EXIT"); private static DdmHandleExit mInstance = new DdmHandleExit(); @@ -46,13 +48,13 @@ public class DdmHandleExit extends ChunkHandler { * Called when the DDM server connects. The handler is allowed to * send messages to the server. */ - public void connected() {} + public void onConnected() {} /** * Called when the DDM server disconnects. Can be used to disable * periodic transmissions or clean up saved state. */ - public void disconnected() {} + public void onDisconnected() {} /** * Handle a chunk of data. We're only registered for "EXIT". diff --git a/core/java/android/ddm/DdmHandleHeap.java b/core/java/android/ddm/DdmHandleHeap.java index 8fa2352944518..2edb5c70cdf29 100644 --- a/core/java/android/ddm/DdmHandleHeap.java +++ b/core/java/android/ddm/DdmHandleHeap.java @@ -16,21 +16,18 @@ package android.ddm; +import android.util.Log; + import org.apache.harmony.dalvik.ddmc.Chunk; import org.apache.harmony.dalvik.ddmc.ChunkHandler; import org.apache.harmony.dalvik.ddmc.DdmServer; -import org.apache.harmony.dalvik.ddmc.DdmVmInternal; -import android.os.Debug; -import android.util.Log; -import java.io.IOException; -import java.nio.ByteBuffer; /** * Handle native and virtual heap requests. */ -public class DdmHandleHeap extends ChunkHandler { +public class DdmHandleHeap extends DdmHandle { - public static final int CHUNK_HPGC = type("HPGC"); + public static final int CHUNK_HPGC = ChunkHandler.type("HPGC"); private static DdmHandleHeap mInstance = new DdmHandleHeap(); @@ -49,13 +46,13 @@ public class DdmHandleHeap extends ChunkHandler { * Called when the DDM server connects. The handler is allowed to * send messages to the server. */ - public void connected() {} + public void onConnected() {} /** * Called when the DDM server disconnects. Can be used to disable * periodic transmissions or clean up saved state. */ - public void disconnected() {} + public void onDisconnected() {} /** * Handle a chunk of data. @@ -68,8 +65,7 @@ public class DdmHandleHeap extends ChunkHandler { if (type == CHUNK_HPGC) { return handleHPGC(request); } else { - throw new RuntimeException("Unknown packet " - + ChunkHandler.name(type)); + throw new RuntimeException("Unknown packet " + name(type)); } } diff --git a/core/java/android/ddm/DdmHandleHello.java b/core/java/android/ddm/DdmHandleHello.java index 60dfc8d7ee7b4..41600294bfb54 100644 --- a/core/java/android/ddm/DdmHandleHello.java +++ b/core/java/android/ddm/DdmHandleHello.java @@ -31,11 +31,11 @@ import java.nio.ByteBuffer; /** * Handle "hello" messages and feature discovery. */ -public class DdmHandleHello extends ChunkHandler { +public class DdmHandleHello extends DdmHandle { - public static final int CHUNK_HELO = type("HELO"); - public static final int CHUNK_WAIT = type("WAIT"); - public static final int CHUNK_FEAT = type("FEAT"); + public static final int CHUNK_HELO = ChunkHandler.type("HELO"); + public static final int CHUNK_WAIT = ChunkHandler.type("WAIT"); + public static final int CHUNK_FEAT = ChunkHandler.type("FEAT"); private static final int CLIENT_PROTOCOL_VERSION = 1; @@ -61,15 +61,14 @@ public class DdmHandleHello extends ChunkHandler { * Called when the DDM server connects. The handler is allowed to * send messages to the server. */ - public void connected() { + public void onConnected() { if (false) Log.v("ddm-hello", "Connected!"); if (false) { /* test spontaneous transmission */ byte[] data = new byte[] { 0, 1, 2, 3, 4, -4, -3, -2, -1, 127 }; - Chunk testChunk = - new Chunk(ChunkHandler.type("TEST"), data, 1, data.length-2); + Chunk testChunk = new Chunk(ChunkHandler.type("TEST"), data, 1, data.length - 2); DdmServer.sendChunk(testChunk); } } @@ -78,7 +77,7 @@ public class DdmHandleHello extends ChunkHandler { * Called when the DDM server disconnects. Can be used to disable * periodic transmissions or clean up saved state. */ - public void disconnected() { + public void onDisconnected() { if (false) Log.v("ddm-hello", "Disconnected!"); } @@ -96,8 +95,7 @@ public class DdmHandleHello extends ChunkHandler { } else if (type == CHUNK_FEAT) { return handleFEAT(request); } else { - throw new RuntimeException("Unknown packet " - + ChunkHandler.name(type)); + throw new RuntimeException("Unknown packet " + name(type)); } } diff --git a/core/java/android/ddm/DdmHandleNativeHeap.java b/core/java/android/ddm/DdmHandleNativeHeap.java index 775c57027f78e..dfd451c3ab4e8 100644 --- a/core/java/android/ddm/DdmHandleNativeHeap.java +++ b/core/java/android/ddm/DdmHandleNativeHeap.java @@ -16,17 +16,18 @@ package android.ddm; +import android.util.Log; + import org.apache.harmony.dalvik.ddmc.Chunk; import org.apache.harmony.dalvik.ddmc.ChunkHandler; import org.apache.harmony.dalvik.ddmc.DdmServer; -import android.util.Log; /** * Handle thread-related traffic. */ -public class DdmHandleNativeHeap extends ChunkHandler { +public class DdmHandleNativeHeap extends DdmHandle { - public static final int CHUNK_NHGT = type("NHGT"); + public static final int CHUNK_NHGT = ChunkHandler.type("NHGT"); private static DdmHandleNativeHeap mInstance = new DdmHandleNativeHeap(); @@ -45,13 +46,13 @@ public class DdmHandleNativeHeap extends ChunkHandler { * Called when the DDM server connects. The handler is allowed to * send messages to the server. */ - public void connected() {} + public void onConnected() {} /** * Called when the DDM server disconnects. Can be used to disable * periodic transmissions or clean up saved state. */ - public void disconnected() {} + public void onDisconnected() {} /** * Handle a chunk of data. @@ -63,8 +64,7 @@ public class DdmHandleNativeHeap extends ChunkHandler { if (type == CHUNK_NHGT) { return handleNHGT(request); } else { - throw new RuntimeException("Unknown packet " - + ChunkHandler.name(type)); + throw new RuntimeException("Unknown packet " + name(type)); } } diff --git a/core/java/android/ddm/DdmHandleProfiling.java b/core/java/android/ddm/DdmHandleProfiling.java index cce4dd2005a8a..806e4bd96f9c5 100644 --- a/core/java/android/ddm/DdmHandleProfiling.java +++ b/core/java/android/ddm/DdmHandleProfiling.java @@ -16,25 +16,28 @@ package android.ddm; + +import android.os.Debug; +import android.util.Log; + import org.apache.harmony.dalvik.ddmc.Chunk; import org.apache.harmony.dalvik.ddmc.ChunkHandler; import org.apache.harmony.dalvik.ddmc.DdmServer; -import android.os.Debug; -import android.util.Log; + import java.nio.ByteBuffer; /** * Handle profiling requests. */ -public class DdmHandleProfiling extends ChunkHandler { +public class DdmHandleProfiling extends DdmHandle { - public static final int CHUNK_MPRS = type("MPRS"); - public static final int CHUNK_MPRE = type("MPRE"); - public static final int CHUNK_MPSS = type("MPSS"); - public static final int CHUNK_MPSE = type("MPSE"); - public static final int CHUNK_MPRQ = type("MPRQ"); - public static final int CHUNK_SPSS = type("SPSS"); - public static final int CHUNK_SPSE = type("SPSE"); + public static final int CHUNK_MPRS = ChunkHandler.type("MPRS"); + public static final int CHUNK_MPRE = ChunkHandler.type("MPRE"); + public static final int CHUNK_MPSS = ChunkHandler.type("MPSS"); + public static final int CHUNK_MPSE = ChunkHandler.type("MPSE"); + public static final int CHUNK_MPRQ = ChunkHandler.type("MPRQ"); + public static final int CHUNK_SPSS = ChunkHandler.type("SPSS"); + public static final int CHUNK_SPSE = ChunkHandler.type("SPSE"); private static final boolean DEBUG = false; private static DdmHandleProfiling mInstance = new DdmHandleProfiling(); @@ -60,13 +63,13 @@ public class DdmHandleProfiling extends ChunkHandler { * Called when the DDM server connects. The handler is allowed to * send messages to the server. */ - public void connected() {} + public void onConnected() {} /** * Called when the DDM server disconnects. Can be used to disable * periodic transmissions or clean up saved state. */ - public void disconnected() {} + public void onDisconnected() {} /** * Handle a chunk of data. @@ -91,8 +94,7 @@ public class DdmHandleProfiling extends ChunkHandler { } else if (type == CHUNK_SPSE) { return handleMPSEOrSPSE(request, "Sample"); } else { - throw new RuntimeException("Unknown packet " - + ChunkHandler.name(type)); + throw new RuntimeException("Unknown packet " + name(type)); } } diff --git a/core/java/android/ddm/DdmHandleViewDebug.java b/core/java/android/ddm/DdmHandleViewDebug.java index 5539dc92f82af..6b0f78f2c1c3a 100644 --- a/core/java/android/ddm/DdmHandleViewDebug.java +++ b/core/java/android/ddm/DdmHandleViewDebug.java @@ -39,12 +39,12 @@ import java.nio.ByteBuffer; * Handle various requests related to profiling / debugging of the view system. * Support for these features are advertised via {@link DdmHandleHello}. */ -public class DdmHandleViewDebug extends ChunkHandler { +public class DdmHandleViewDebug extends DdmHandle { /** List {@link ViewRootImpl}'s of this process. */ - private static final int CHUNK_VULW = type("VULW"); + private static final int CHUNK_VULW = ChunkHandler.type("VULW"); /** Operation on view root, first parameter in packet should be one of VURT_* constants */ - private static final int CHUNK_VURT = type("VURT"); + private static final int CHUNK_VURT = ChunkHandler.type("VURT"); /** Dump view hierarchy. */ private static final int VURT_DUMP_HIERARCHY = 1; @@ -59,7 +59,7 @@ public class DdmHandleViewDebug extends ChunkHandler { * Generic View Operation, first parameter in the packet should be one of the * VUOP_* constants below. */ - private static final int CHUNK_VUOP = type("VUOP"); + private static final int CHUNK_VUOP = ChunkHandler.type("VUOP"); /** Capture View. */ private static final int VUOP_CAPTURE_VIEW = 1; @@ -99,11 +99,11 @@ public class DdmHandleViewDebug extends ChunkHandler { } @Override - public void connected() { + public void onConnected() { } @Override - public void disconnected() { + public void onDisconnected() { } @Override @@ -154,7 +154,7 @@ public class DdmHandleViewDebug extends ChunkHandler { return createFailChunk(ERR_INVALID_OP, "Unknown view operation: " + op); } } else { - throw new RuntimeException("Unknown packet " + ChunkHandler.name(type)); + throw new RuntimeException("Unknown packet " + name(type)); } }