Merge "Revert to using nano protos for shell side tracing" into udc-dev

This commit is contained in:
Winson Chung
2023-05-02 23:25:21 +00:00
committed by Android (Google) Code Review
4 changed files with 70 additions and 95 deletions

View File

@@ -125,34 +125,6 @@ prebuilt_etc {
// End ProtoLog // End ProtoLog
gensrcs {
name: "wm-shell-protos",
tools: [
"aprotoc",
"protoc-gen-javastream",
"soong_zip",
],
tool_files: [
":libprotobuf-internal-protos",
],
cmd: "mkdir -p $(genDir)/$(in) " +
"&& $(location aprotoc) " +
" --plugin=$(location protoc-gen-javastream) " +
" --javastream_out=$(genDir)/$(in) " +
" -Iexternal/protobuf/src " +
" -I . " +
" $(in) " +
"&& $(location soong_zip) -jar -o $(out) -C $(genDir)/$(in) -D $(genDir)/$(in)",
srcs: [
"proto/**/*.proto",
],
output_extension: "srcjar",
}
java_library { java_library {
name: "WindowManager-Shell-proto", name: "WindowManager-Shell-proto",
@@ -170,7 +142,6 @@ android_library {
// TODO(b/168581922) protologtool do not support kotlin(*.kt) // TODO(b/168581922) protologtool do not support kotlin(*.kt)
":wm_shell-sources-kt", ":wm_shell-sources-kt",
":wm_shell-aidls", ":wm_shell-aidls",
":wm-shell-protos",
], ],
resource_dirs: [ resource_dirs: [
"res", "res",

View File

@@ -18,25 +18,29 @@ package com.android.wm.shell.transition;
import static android.os.Build.IS_USER; import static android.os.Build.IS_USER;
import static com.android.wm.shell.WmShellTransitionTraceProto.MAGIC_NUMBER; import static com.android.wm.shell.nano.WmShellTransitionTraceProto.MAGIC_NUMBER_H;
import static com.android.wm.shell.WmShellTransitionTraceProto.MAGIC_NUMBER_H; import static com.android.wm.shell.nano.WmShellTransitionTraceProto.MAGIC_NUMBER_L;
import static com.android.wm.shell.WmShellTransitionTraceProto.MAGIC_NUMBER_L;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.os.SystemClock; import android.os.SystemClock;
import android.os.Trace; import android.os.Trace;
import android.util.Log; import android.util.Log;
import android.util.proto.ProtoOutputStream;
import com.android.internal.util.TraceBuffer; import com.android.internal.util.TraceBuffer;
import com.android.wm.shell.nano.HandlerMapping;
import com.android.wm.shell.sysui.ShellCommandHandler; import com.android.wm.shell.sysui.ShellCommandHandler;
import com.google.protobuf.nano.MessageNano;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Queue;
/** /**
* Helper class to collect and dump transition traces. * Helper class to collect and dump transition traces.
@@ -54,8 +58,35 @@ public class Tracer implements ShellCommandHandler.ShellCommandActionHandler {
private final Object mEnabledLock = new Object(); private final Object mEnabledLock = new Object();
private boolean mActiveTracingEnabled = false; private boolean mActiveTracingEnabled = false;
private final TraceBuffer mTraceBuffer = new TraceBuffer(ALWAYS_ON_TRACING_CAPACITY, private final TraceBuffer.ProtoProvider mProtoProvider =
(proto) -> handleOnEntryRemovedFromTrace(proto)); new TraceBuffer.ProtoProvider<MessageNano,
com.android.wm.shell.nano.WmShellTransitionTraceProto,
com.android.wm.shell.nano.Transition>() {
@Override
public int getItemSize(MessageNano proto) {
return proto.getCachedSize();
}
@Override
public byte[] getBytes(MessageNano proto) {
return MessageNano.toByteArray(proto);
}
@Override
public void write(
com.android.wm.shell.nano.WmShellTransitionTraceProto encapsulatingProto,
Queue<com.android.wm.shell.nano.Transition> buffer, OutputStream os)
throws IOException {
encapsulatingProto.transitions = buffer.toArray(
new com.android.wm.shell.nano.Transition[0]);
os.write(getBytes(encapsulatingProto));
}
};
private final TraceBuffer<MessageNano,
com.android.wm.shell.nano.WmShellTransitionTraceProto,
com.android.wm.shell.nano.Transition> mTraceBuffer
= new TraceBuffer(ALWAYS_ON_TRACING_CAPACITY, mProtoProvider,
(proto) -> handleOnEntryRemovedFromTrace(proto));
private final Map<Object, Runnable> mRemovedFromTraceCallbacks = new HashMap<>(); private final Map<Object, Runnable> mRemovedFromTraceCallbacks = new HashMap<>();
private final Map<Transitions.TransitionHandler, Integer> mHandlerIds = new HashMap<>(); private final Map<Transitions.TransitionHandler, Integer> mHandlerIds = new HashMap<>();
@@ -78,26 +109,20 @@ public class Tracer implements ShellCommandHandler.ShellCommandActionHandler {
mHandlerIds.put(handler, handlerId); mHandlerIds.put(handler, handlerId);
} }
ProtoOutputStream outputStream = new ProtoOutputStream(); com.android.wm.shell.nano.Transition proto = new com.android.wm.shell.nano.Transition();
final long protoToken = proto.id = transitionId;
outputStream.start(com.android.wm.shell.WmShellTransitionTraceProto.TRANSITIONS); proto.dispatchTimeNs = SystemClock.elapsedRealtimeNanos();
proto.handler = handlerId;
outputStream.write(com.android.wm.shell.Transition.ID, transitionId);
outputStream.write(com.android.wm.shell.Transition.DISPATCH_TIME_NS,
SystemClock.elapsedRealtimeNanos());
outputStream.write(com.android.wm.shell.Transition.HANDLER, handlerId);
outputStream.end(protoToken);
final int useCountAfterAdd = mHandlerUseCountInTrace.getOrDefault(handler, 0) + 1; final int useCountAfterAdd = mHandlerUseCountInTrace.getOrDefault(handler, 0) + 1;
mHandlerUseCountInTrace.put(handler, useCountAfterAdd); mHandlerUseCountInTrace.put(handler, useCountAfterAdd);
mRemovedFromTraceCallbacks.put(outputStream, () -> { mRemovedFromTraceCallbacks.put(proto, () -> {
final int useCountAfterRemove = mHandlerUseCountInTrace.get(handler) - 1; final int useCountAfterRemove = mHandlerUseCountInTrace.get(handler) - 1;
mHandlerUseCountInTrace.put(handler, useCountAfterRemove); mHandlerUseCountInTrace.put(handler, useCountAfterRemove);
}); });
mTraceBuffer.add(outputStream); mTraceBuffer.add(proto);
} }
/** /**
@@ -107,18 +132,12 @@ public class Tracer implements ShellCommandHandler.ShellCommandActionHandler {
* @param playingTransitionId The id of the transition we was to merge the transition into. * @param playingTransitionId The id of the transition we was to merge the transition into.
*/ */
public void logMergeRequested(int mergeRequestedTransitionId, int playingTransitionId) { public void logMergeRequested(int mergeRequestedTransitionId, int playingTransitionId) {
ProtoOutputStream outputStream = new ProtoOutputStream(); com.android.wm.shell.nano.Transition proto = new com.android.wm.shell.nano.Transition();
final long protoToken = proto.id = mergeRequestedTransitionId;
outputStream.start(com.android.wm.shell.WmShellTransitionTraceProto.TRANSITIONS); proto.mergeRequestTimeNs = SystemClock.elapsedRealtimeNanos();
proto.mergedInto = playingTransitionId;
outputStream.write(com.android.wm.shell.Transition.ID, mergeRequestedTransitionId); mTraceBuffer.add(proto);
outputStream.write(com.android.wm.shell.Transition.MERGE_REQUEST_TIME_NS,
SystemClock.elapsedRealtimeNanos());
outputStream.write(com.android.wm.shell.Transition.MERGED_INTO, playingTransitionId);
outputStream.end(protoToken);
mTraceBuffer.add(outputStream);
} }
/** /**
@@ -128,18 +147,12 @@ public class Tracer implements ShellCommandHandler.ShellCommandActionHandler {
* @param playingTransitionId The id of the transition the transition was merged into. * @param playingTransitionId The id of the transition the transition was merged into.
*/ */
public void logMerged(int mergedTransitionId, int playingTransitionId) { public void logMerged(int mergedTransitionId, int playingTransitionId) {
ProtoOutputStream outputStream = new ProtoOutputStream(); com.android.wm.shell.nano.Transition proto = new com.android.wm.shell.nano.Transition();
final long protoToken = proto.id = mergedTransitionId;
outputStream.start(com.android.wm.shell.WmShellTransitionTraceProto.TRANSITIONS); proto.mergeTimeNs = SystemClock.elapsedRealtimeNanos();
proto.mergedInto = playingTransitionId;
outputStream.write(com.android.wm.shell.Transition.ID, mergedTransitionId); mTraceBuffer.add(proto);
outputStream.write(
com.android.wm.shell.Transition.MERGE_TIME_NS, SystemClock.elapsedRealtimeNanos());
outputStream.write(com.android.wm.shell.Transition.MERGED_INTO, playingTransitionId);
outputStream.end(protoToken);
mTraceBuffer.add(outputStream);
} }
/** /**
@@ -148,17 +161,11 @@ public class Tracer implements ShellCommandHandler.ShellCommandActionHandler {
* @param transitionId The id of the transition that was aborted. * @param transitionId The id of the transition that was aborted.
*/ */
public void logAborted(int transitionId) { public void logAborted(int transitionId) {
ProtoOutputStream outputStream = new ProtoOutputStream(); com.android.wm.shell.nano.Transition proto = new com.android.wm.shell.nano.Transition();
final long protoToken = proto.id = transitionId;
outputStream.start(com.android.wm.shell.WmShellTransitionTraceProto.TRANSITIONS); proto.abortTimeNs = SystemClock.elapsedRealtimeNanos();
outputStream.write(com.android.wm.shell.Transition.ID, transitionId); mTraceBuffer.add(proto);
outputStream.write(
com.android.wm.shell.Transition.ABORT_TIME_NS, SystemClock.elapsedRealtimeNanos());
outputStream.end(protoToken);
mTraceBuffer.add(outputStream);
} }
/** /**
@@ -230,8 +237,9 @@ public class Tracer implements ShellCommandHandler.ShellCommandActionHandler {
private void writeTraceToFileLocked(@Nullable PrintWriter pw, File file) { private void writeTraceToFileLocked(@Nullable PrintWriter pw, File file) {
Trace.beginSection("TransitionTracer#writeTraceToFileLocked"); Trace.beginSection("TransitionTracer#writeTraceToFileLocked");
try { try {
ProtoOutputStream proto = new ProtoOutputStream(); com.android.wm.shell.nano.WmShellTransitionTraceProto proto =
proto.write(MAGIC_NUMBER, MAGIC_NUMBER_VALUE); new com.android.wm.shell.nano.WmShellTransitionTraceProto();
proto.magicNumber = MAGIC_NUMBER_VALUE;
writeHandlerMappingToProto(proto); writeHandlerMappingToProto(proto);
int pid = android.os.Process.myPid(); int pid = android.os.Process.myPid();
LogAndPrintln.i(pw, "Writing file to " + file.getAbsolutePath() LogAndPrintln.i(pw, "Writing file to " + file.getAbsolutePath()
@@ -243,19 +251,21 @@ public class Tracer implements ShellCommandHandler.ShellCommandActionHandler {
Trace.endSection(); Trace.endSection();
} }
private void writeHandlerMappingToProto(ProtoOutputStream outputStream) { private void writeHandlerMappingToProto(
com.android.wm.shell.nano.WmShellTransitionTraceProto proto) {
ArrayList<com.android.wm.shell.nano.HandlerMapping> handlerMappings = new ArrayList<>();
for (Transitions.TransitionHandler handler : mHandlerUseCountInTrace.keySet()) { for (Transitions.TransitionHandler handler : mHandlerUseCountInTrace.keySet()) {
final int count = mHandlerUseCountInTrace.get(handler); final int count = mHandlerUseCountInTrace.get(handler);
if (count > 0) { if (count > 0) {
final long protoToken = outputStream.start( com.android.wm.shell.nano.HandlerMapping mapping =
com.android.wm.shell.WmShellTransitionTraceProto.HANDLER_MAPPINGS); new com.android.wm.shell.nano.HandlerMapping();
outputStream.write(com.android.wm.shell.HandlerMapping.ID, mapping.id = mHandlerIds.get(handler);
mHandlerIds.get(handler)); mapping.name = handler.getClass().getName();
outputStream.write(com.android.wm.shell.HandlerMapping.NAME, handlerMappings.add(mapping);
handler.getClass().getName());
outputStream.end(protoToken);
} }
} }
proto.handlerMappings = handlerMappings.toArray(
new com.android.wm.shell.nano.HandlerMapping[0]);
} }
private void handleOnEntryRemovedFromTrace(Object proto) { private void handleOnEntryRemovedFromTrace(Object proto) {

View File

@@ -16,8 +16,6 @@
syntax = "proto2"; syntax = "proto2";
import "frameworks/base/libs/WindowManager/Shell/proto/wm_shell_trace.proto";
package com.android.systemui.tracing; package com.android.systemui.tracing;
option java_multiple_files = true; option java_multiple_files = true;
@@ -25,7 +23,6 @@ option java_multiple_files = true;
message SystemUiTraceProto { message SystemUiTraceProto {
optional EdgeBackGestureHandlerProto edge_back_gesture_handler = 1; optional EdgeBackGestureHandlerProto edge_back_gesture_handler = 1;
optional com.android.wm.shell.WmShellTraceProto wm_shell = 2;
} }
message EdgeBackGestureHandlerProto { message EdgeBackGestureHandlerProto {

View File

@@ -362,9 +362,6 @@ public final class WMShell implements
@Override @Override
public void writeToProto(SystemUiTraceProto proto) { public void writeToProto(SystemUiTraceProto proto) {
if (proto.wmShell == null) {
proto.wmShell = new WmShellTraceProto();
}
// Dump to WMShell proto here // Dump to WMShell proto here
// TODO: Figure out how we want to synchronize while dumping to proto // TODO: Figure out how we want to synchronize while dumping to proto
} }