Merge "Avoid too long trace section name to crash FrameTracker" into tm-qpr-dev am: 242b670b62 am: 979765606d

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/19429366

Change-Id: I465d6b2dfc0c525cd28dd1a827fb368236916943
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
TreeHugger Robot
2022-08-05 20:50:06 +00:00
committed by Automerger Merge Worker
3 changed files with 200 additions and 4 deletions

View File

@@ -66,6 +66,8 @@ public class FrameTracker extends SurfaceControl.OnJankDataListener
private static final long INVALID_ID = -1;
public static final int NANOS_IN_MILLISECOND = 1_000_000;
private static final int MAX_LENGTH_EVENT_DESC = 20;
static final int REASON_END_UNKNOWN = -1;
static final int REASON_END_NORMAL = 0;
static final int REASON_END_SURFACE_DESTROYED = 1;
@@ -394,7 +396,18 @@ public class FrameTracker extends SurfaceControl.OnJankDataListener
return true;
}
private void markEvent(String desc) {
/**
* Mark the FrameTracker events in the trace.
*
* @param desc The description of the trace event,
* shouldn't exceed {@link #MAX_LENGTH_EVENT_DESC}.
*/
private void markEvent(@NonNull String desc) {
if (desc.length() > MAX_LENGTH_EVENT_DESC) {
throw new IllegalArgumentException(TextUtils.formatSimple(
"The length of the trace event description <%s> exceeds %d",
desc, MAX_LENGTH_EVENT_DESC));
}
Trace.beginSection(TextUtils.formatSimple("%s#%s", mSession.getName(), desc));
Trace.endSection();
}

View File

@@ -149,6 +149,10 @@ public class InteractionJankMonitor {
private static final int DEFAULT_TRACE_THRESHOLD_MISSED_FRAMES = 3;
private static final int DEFAULT_TRACE_THRESHOLD_FRAME_TIME_MILLIS = 64;
@VisibleForTesting
public static final int MAX_LENGTH_OF_CUJ_NAME = 80;
private static final int MAX_LENGTH_SESSION_NAME = 100;
public static final String ACTION_SESSION_END = ACTION_PREFIX + ".ACTION_SESSION_END";
public static final String ACTION_SESSION_CANCEL = ACTION_PREFIX + ".ACTION_SESSION_CANCEL";
@@ -732,6 +736,9 @@ public class InteractionJankMonitor {
* @return the name of the cuj type
*/
public static String getNameOfCuj(int cujType) {
// Please note:
// 1. The length of the returned string shouldn't exceed MAX_LENGTH_OF_CUJ_NAME.
// 2. The returned string should be the same with the name defined in atoms.proto.
switch (cujType) {
case CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE:
return "SHADE_EXPAND_COLLAPSE";
@@ -1106,9 +1113,27 @@ public class InteractionJankMonitor {
public Session(@CujType int cujType, @NonNull String postfix) {
mCujType = cujType;
mTimeStamp = System.nanoTime();
mName = TextUtils.isEmpty(postfix)
? String.format("J<%s>", getNameOfCuj(mCujType))
: String.format("J<%s::%s>", getNameOfCuj(mCujType), postfix);
mName = generateSessionName(getNameOfCuj(cujType), postfix);
}
private String generateSessionName(@NonNull String cujName, @NonNull String cujPostfix) {
final boolean hasPostfix = !TextUtils.isEmpty(cujPostfix);
// We assert that the cujName shouldn't exceed MAX_LENGTH_OF_CUJ_NAME.
if (cujName.length() > MAX_LENGTH_OF_CUJ_NAME) {
throw new IllegalArgumentException(TextUtils.formatSimple(
"The length of cuj name <%s> exceeds %d", cujName, MAX_LENGTH_OF_CUJ_NAME));
}
if (hasPostfix) {
final int remaining = MAX_LENGTH_SESSION_NAME - cujName.length();
if (cujPostfix.length() > remaining) {
cujPostfix = cujPostfix.substring(0, remaining - 3).concat("...");
}
}
// The max length of the whole string should be:
// 105 with postfix, 83 without postfix
return hasPostfix
? TextUtils.formatSimple("J<%s::%s>", cujName, cujPostfix)
: TextUtils.formatSimple("J<%s>", cujName);
}
@CujType