Support configuration as parameter while beginning the instrumentation

Support a custom configuration while beginning the instrumentation, also
support a custom tag for getting fine-grained CUJ in the trace.

Bug: 187495856
Test: take the trace, see the cuj section name.
Change-Id: Ife2554e0d7dbe4c01b7e4381a12a5f595e2f4364
This commit is contained in:
Ahan Wu
2021-05-07 19:46:05 +08:00
parent cb3e9c2bc8
commit c45abcf9db
7 changed files with 192 additions and 49 deletions

View File

@@ -208,7 +208,6 @@ public class FrameTracker extends SurfaceControl.OnJankDataListener
*/
public synchronized void begin() {
mBeginVsyncId = mChoreographer.getVsyncId() + 1;
mSession.setTimeStamp(System.nanoTime());
if (mSurfaceControl != null) {
postTraceStartMarker();
}
@@ -224,7 +223,11 @@ public class FrameTracker extends SurfaceControl.OnJankDataListener
}
}
private void postTraceStartMarker() {
/**
* Start trace section at appropriate time.
*/
@VisibleForTesting
public void postTraceStartMarker() {
mChoreographer.mChoreographer.postCallback(Choreographer.CALLBACK_INPUT, () -> {
synchronized (FrameTracker.this) {
if (mCancelled || mEndVsyncId != INVALID_ID) {

View File

@@ -62,6 +62,7 @@ import android.os.HandlerExecutor;
import android.os.HandlerThread;
import android.os.SystemProperties;
import android.provider.DeviceConfig;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.Choreographer;
@@ -91,6 +92,7 @@ import java.util.concurrent.TimeUnit;
*/
public class InteractionJankMonitor {
private static final String TAG = InteractionJankMonitor.class.getSimpleName();
private static final boolean DEBUG = false;
private static final String ACTION_PREFIX = InteractionJankMonitor.class.getCanonicalName();
private static final String DEFAULT_WORKER_NAME = TAG + "-Worker";
@@ -289,14 +291,17 @@ public class InteractionJankMonitor {
* @return instance of the FrameTracker
*/
@VisibleForTesting
public FrameTracker createFrameTracker(View v, Session session) {
public FrameTracker createFrameTracker(Configuration conf, Session session) {
final View v = conf.mView;
final Context c = v.getContext().getApplicationContext();
final ThreadedRendererWrapper r = new ThreadedRendererWrapper(v.getThreadedRenderer());
final ViewRootWrapper vr = new ViewRootWrapper(v.getViewRootImpl());
final SurfaceControlWrapper sc = new SurfaceControlWrapper();
final ChoreographerWrapper cg = new ChoreographerWrapper(Choreographer.getInstance());
synchronized (this) {
FrameTrackerListener eventsListener = (s, act) -> handleCujEvents(c, act, s);
return new FrameTracker(session, mWorker.getThreadHandler(),
new ThreadedRendererWrapper(v.getThreadedRenderer()),
new ViewRootWrapper(v.getViewRootImpl()), new SurfaceControlWrapper(),
new ChoreographerWrapper(Choreographer.getInstance()), mMetrics,
return new FrameTracker(session, mWorker.getThreadHandler(), r, vr, sc, cg, mMetrics,
mTraceThresholdMissedFrames, mTraceThresholdFrameTimeMillis, eventsListener);
}
}
@@ -348,30 +353,47 @@ public class InteractionJankMonitor {
/**
* Begin a trace session.
*
* @param v an attached view.
* @param cujType the specific {@link InteractionJankMonitor.CujType}.
* @return boolean true if the tracker is started successfully, false otherwise.
*/
public boolean begin(View v, @CujType int cujType) {
synchronized (this) {
return begin(v, cujType, DEFAULT_TIMEOUT_MS);
try {
return beginInternal(
new Configuration.Builder(cujType)
.setView(v)
.build());
} catch (IllegalArgumentException ex) {
Log.d(TAG, "Build configuration failed!", ex);
return false;
}
}
/**
* Begin a trace session.
*
* @param cujType the specific {@link InteractionJankMonitor.CujType}.
* @param timeout the elapsed time in ms until firing the timeout action.
* @param builder the builder of the configurations for instrumenting the CUJ.
* @return boolean true if the tracker is started successfully, false otherwise.
*/
public boolean begin(View v, @CujType int cujType, long timeout) {
public boolean begin(@NonNull Configuration.Builder builder) {
try {
return beginInternal(builder.build());
} catch (IllegalArgumentException ex) {
Log.d(TAG, "Build configuration failed!", ex);
return false;
}
}
private boolean beginInternal(@NonNull Configuration conf) {
synchronized (this) {
if (!v.isAttachedToWindow()) {
Log.d(TAG, "View not attached!", new Throwable());
return false;
}
int cujType = conf.mCujType;
boolean shouldSample = ThreadLocalRandom.current().nextInt() % mSamplingInterval == 0;
if (!mEnabled || !shouldSample) {
if (DEBUG) {
Log.d(TAG, "Skip monitoring cuj: " + getNameOfCuj(cujType)
+ ", enable=" + mEnabled + ", debuggable=" + DEFAULT_ENABLED
+ ", sample=" + shouldSample + ", interval=" + mSamplingInterval);
}
return false;
}
FrameTracker tracker = getTracker(cujType);
@@ -379,14 +401,14 @@ public class InteractionJankMonitor {
if (tracker != null) return false;
// begin a new trace session.
tracker = createFrameTracker(v, new Session(cujType));
tracker = createFrameTracker(conf, new Session(cujType, conf.mTag));
mRunningTrackers.put(cujType, tracker);
tracker.begin();
// Cancel the trace if we don't get an end() call in specified duration.
Runnable timeoutAction = () -> cancel(cujType);
mTimeoutActions.put(cujType, timeoutAction);
mWorker.getThreadHandler().postDelayed(timeoutAction, timeout);
mWorker.getThreadHandler().postDelayed(timeoutAction, conf.mTimeout);
return true;
}
}
@@ -553,20 +575,121 @@ public class InteractionJankMonitor {
return "UNKNOWN";
}
/**
* Configurations used while instrumenting the CUJ. <br/>
* <b>It may refer to an attached view, don't use static reference for any purpose.</b>
*/
public static class Configuration {
private final View mView;
private final long mTimeout;
private final String mTag;
private final @CujType int mCujType;
/**
* A builder for building Configuration. <br/>
* <b>It may refer to an attached view, don't use static reference for any purpose.</b>
*/
public static class Builder {
private View mAttrView = null;
private long mAttrTimeout = DEFAULT_TIMEOUT_MS;
private String mAttrTag = "";
private @CujType int mAttrCujType;
/**
* @param cuj The enum defined in {@link InteractionJankMonitor.CujType}.
*/
public Builder(@CujType int cuj) {
mAttrCujType = cuj;
}
/**
* @param view an attached view
* @return builder
*/
public Builder setView(@NonNull View view) {
mAttrView = view;
return this;
}
/**
* @param timeout duration to cancel the instrumentation in ms
* @return builder
*/
public Builder setTimeout(long timeout) {
mAttrTimeout = timeout;
return this;
}
/**
* @param tag The postfix of the CUJ in the output trace.
* It provides a brief description for the CUJ like the concrete class
* who is dealing with the CUJ or the important state with the CUJ, etc.
* @return builder
*/
public Builder setTag(@NonNull String tag) {
mAttrTag = tag;
return this;
}
/**
* Build the {@link Configuration} instance
* @return the instance of {@link Configuration}
* @throws IllegalArgumentException if any invalid attribute is set
*/
public Configuration build() throws IllegalArgumentException {
return new Configuration(mAttrCujType, mAttrView, mAttrTag, mAttrTimeout);
}
}
private Configuration(@CujType int cuj, View view, String tag, long timeout) {
mCujType = cuj;
mTag = tag;
mTimeout = timeout;
mView = view;
validate();
}
private void validate() {
boolean shouldThrow = false;
final StringBuilder msg = new StringBuilder();
if (mTag == null) {
shouldThrow = true;
msg.append("Invalid tag; ");
}
if (mTimeout < 0) {
shouldThrow = true;
msg.append("Invalid timeout value; ");
}
if (mView == null || !mView.isAttachedToWindow()) {
shouldThrow = true;
msg.append("Null view or view is not attached yet; ");
}
if (shouldThrow) {
throw new IllegalArgumentException(msg.toString());
}
}
}
/**
* A class to represent a session.
*/
public static class Session {
@CujType
private int mCujType;
private long mTimeStamp;
private final int mCujType;
private final long mTimeStamp;
@FrameTracker.Reasons
private int mReason = FrameTracker.REASON_END_UNKNOWN;
private boolean mShouldNotify;
private final boolean mShouldNotify;
private final String mName;
public Session(@CujType int cujType) {
public Session(@CujType int cujType, @NonNull String postfix) {
mCujType = cujType;
mTimeStamp = System.nanoTime();
mShouldNotify = SystemProperties.getBoolean(PROP_NOTIFY_CUJ_EVENT, false);
mName = TextUtils.isEmpty(postfix)
? String.format("J<%s>", getNameOfCuj(mCujType))
: String.format("J<%s::%s>", getNameOfCuj(mCujType), postfix);
}
@CujType
@@ -588,11 +711,7 @@ public class InteractionJankMonitor {
}
public String getName() {
return "J<" + getNameOfCuj(mCujType) + ">";
}
public void setTimeStamp(long timeStamp) {
mTimeStamp = timeStamp;
return mName;
}
public long getTimeStamp() {

View File

@@ -62,6 +62,7 @@ import java.util.concurrent.TimeUnit;
@SmallTest
public class FrameTrackerTest {
private static final String CUJ_POSTFIX = "";
private ViewAttachTestActivity mActivity;
@Rule
@@ -100,15 +101,17 @@ public class FrameTrackerTest {
mListenerCapture.capture(), any());
doNothing().when(mSurfaceControlWrapper).removeJankStatsListener(
mListenerCapture.capture());
mChoreographer = mock(ChoreographerWrapper.class);
Session session = new Session(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
Session session = new Session(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE, CUJ_POSTFIX);
mTracker = Mockito.spy(
new FrameTracker(session, handler, mRenderer, mViewRootWrapper,
mSurfaceControlWrapper, mChoreographer, mWrapper,
/*traceThresholdMissedFrames=*/ 1, /*traceThresholdFrameTimeMillis=*/ -1,
null));
doNothing().when(mTracker).triggerPerfetto();
doNothing().when(mTracker).postTraceStartMarker();
}
@Test

View File

@@ -62,6 +62,7 @@ import java.util.stream.Collectors;
@SmallTest
public class InteractionJankMonitorTest {
private static final String CUJ_POSTFIX = "";
private ViewAttachTestActivity mActivity;
private View mView;
private HandlerThread mWorker;
@@ -90,7 +91,7 @@ public class InteractionJankMonitorTest {
InteractionJankMonitor monitor = spy(new InteractionJankMonitor(mWorker));
verify(mWorker).start();
Session session = new Session(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
Session session = new Session(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE, CUJ_POSTFIX);
FrameTracker tracker = spy(new FrameTracker(session, mWorker.getThreadHandler(),
new ThreadedRendererWrapper(mView.getThreadedRenderer()),
new ViewRootWrapper(mView.getViewRootImpl()), new SurfaceControlWrapper(),
@@ -137,7 +138,7 @@ public class InteractionJankMonitorTest {
ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
Session session = new Session(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
Session session = new Session(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE, CUJ_POSTFIX);
FrameTracker tracker = spy(new FrameTracker(session, mWorker.getThreadHandler(),
new ThreadedRendererWrapper(mView.getThreadedRenderer()),
new ViewRootWrapper(mView.getViewRootImpl()), new SurfaceControlWrapper(),

View File

@@ -20,11 +20,14 @@ import android.annotation.IntDef;
import android.view.View;
import com.android.internal.jank.InteractionJankMonitor;
import com.android.internal.jank.InteractionJankMonitor.Configuration;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
public final class InteractionJankMonitorWrapper {
private static final String TAG = "JankMonitorWrapper";
// Launcher journeys.
public static final int CUJ_APP_LAUNCH_FROM_RECENTS =
InteractionJankMonitor.CUJ_LAUNCHER_APP_LAUNCH_FROM_RECENTS;
@@ -60,7 +63,11 @@ public final class InteractionJankMonitorWrapper {
}
public static boolean begin(View v, @CujType int cujType, long timeout) {
return InteractionJankMonitor.getInstance().begin(v, cujType, timeout);
Configuration.Builder builder =
new Configuration.Builder(cujType)
.setView(v)
.setTimeout(timeout);
return InteractionJankMonitor.getInstance().begin(builder);
}
public static boolean end(@CujType int cujType) {

View File

@@ -32,6 +32,7 @@ import android.view.animation.Interpolator;
import android.view.animation.PathInterpolator;
import com.android.internal.jank.InteractionJankMonitor;
import com.android.internal.jank.InteractionJankMonitor.Configuration;
import com.android.settingslib.Utils;
import com.android.systemui.Gefingerpoken;
import com.android.systemui.R;
@@ -542,8 +543,9 @@ public abstract class ActivatableNotificationView extends ExpandableOutlineView
@Override
public void onAnimationStart(Animator animation) {
mWasCancelled = false;
InteractionJankMonitor.getInstance().begin(ActivatableNotificationView.this,
getCujType(isAppearing));
Configuration.Builder builder = new Configuration.Builder(getCujType(isAppearing))
.setView(ActivatableNotificationView.this);
InteractionJankMonitor.getInstance().begin(builder);
}
@Override

View File

@@ -26,7 +26,6 @@ import static java.lang.Float.isNaN;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.res.Configuration;
import android.content.res.Resources;
@@ -42,7 +41,6 @@ import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Interpolator;
import android.view.animation.PathInterpolator;
import com.android.internal.jank.InteractionJankMonitor;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -336,8 +334,7 @@ public abstract class PanelViewController {
protected void startExpandMotion(float newX, float newY, boolean startTracking,
float expandedHeight) {
if (!mHandlingPointerUp) {
InteractionJankMonitor.getInstance().begin(mView,
CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
beginJankMonitoring(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
}
mInitialOffsetOnTouch = expandedHeight;
mInitialTouchY = newY;
@@ -533,7 +530,7 @@ public abstract class PanelViewController {
protected void flingToHeight(float vel, boolean expand, float target,
float collapseSpeedUpFactor, boolean expandBecauseOfFalsing) {
if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) {
InteractionJankMonitor.getInstance().end(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
endJankMonitoring(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
mKeyguardStateController.notifyPanelFlingEnd();
notifyExpandingFinished();
return;
@@ -579,8 +576,7 @@ public abstract class PanelViewController {
@Override
public void onAnimationStart(Animator animation) {
InteractionJankMonitor.getInstance()
.begin(mView, CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
beginJankMonitoring(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
}
@Override
@@ -632,12 +628,10 @@ public abstract class PanelViewController {
setAnimator(null);
mKeyguardStateController.notifyPanelFlingEnd();
if (!cancelled) {
InteractionJankMonitor.getInstance()
.end(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
endJankMonitoring(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
notifyExpandingFinished();
} else {
InteractionJankMonitor.getInstance()
.cancel(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
cancelJankMonitoring(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
}
notifyBarPanelExpansionChanged();
}
@@ -847,8 +841,7 @@ public abstract class PanelViewController {
mView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
if (mAnimateAfterExpanding) {
notifyExpandingStarted();
InteractionJankMonitor.getInstance().begin(mView,
CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
beginJankMonitoring(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
fling(0, true /* expand */);
} else {
setExpandedFraction(1f);
@@ -1304,11 +1297,10 @@ public abstract class PanelViewController {
endMotionEvent(event, x, y, false /* forceCancel */);
// mHeightAnimator is null, there is no remaining frame, ends instrumenting.
if (mHeightAnimator == null) {
InteractionJankMonitor monitor = InteractionJankMonitor.getInstance();
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
monitor.end(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
endJankMonitoring(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
} else {
monitor.cancel(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
cancelJankMonitoring(CUJ_NOTIFICATION_SHADE_EXPAND_COLLAPSE);
}
}
break;
@@ -1338,4 +1330,20 @@ public abstract class PanelViewController {
loadDimens();
}
}
private void beginJankMonitoring(int cuj) {
InteractionJankMonitor.Configuration.Builder builder =
new InteractionJankMonitor.Configuration.Builder(cuj)
.setView(mView)
.setTag(isFullyCollapsed() ? "Expand" : "Collapse");
InteractionJankMonitor.getInstance().begin(builder);
}
private void endJankMonitoring(int cuj) {
InteractionJankMonitor.getInstance().end(cuj);
}
private void cancelJankMonitoring(int cuj) {
InteractionJankMonitor.getInstance().cancel(cuj);
}
}