溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android如何發(fā)現(xiàn)APP卡頓

發(fā)布時間:2020-11-02 14:57:52 來源:億速云 閱讀:220 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Android如何發(fā)現(xiàn)APP卡頓,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

常用的方式是使用 adb SurfaceFlinger 服務(wù)和 adb gfxinfo 功能,在自動化操作 app 的過程中,使用 adb 獲取數(shù)據(jù)來監(jiān)控 app 的流暢情況,發(fā)現(xiàn)出現(xiàn)出現(xiàn)卡頓的時間段,尋找出現(xiàn)卡頓的場景和操作。

方式1:adb shell dumpsysSurfaceFlinger

使用 ‘a(chǎn)db shell dumpsysSurfaceFlinger' 命令即可獲取最近 127 幀的數(shù)據(jù),通過定期執(zhí)行 adb 命令,獲取幀數(shù)來計(jì)算出幀率 FPS。

方式2:adb shell dumpsys gfxinfo

使用 ‘a(chǎn)db shell dumpsys gfxinfo' 命令即可獲取最新 128 幀的繪制信息,詳細(xì)包括每一幀繪制的 Draw,Process,Execute 三個過程的耗時,如果這三個時間總和超過 16.6ms 即認(rèn)為是發(fā)生了卡頓。

已有的兩種方案比較適合衡量回歸卡頓問題的修復(fù)效果和判斷某些特定場景下是否有卡頓情況,然而,這樣的方式有幾個明顯的不足:

  • 一般很難構(gòu)造實(shí)際用戶卡頓的環(huán)境來重現(xiàn);
  • 這種方式操作起來比較麻煩,需編寫自動化用例,無法覆蓋大量的可疑場景,測試重現(xiàn)耗時耗人力;
  • 無法衡量靜態(tài)頁面的卡頓情況;
  • 出現(xiàn)卡頓的時候app無法及時獲取運(yùn)行狀態(tài)和信息,開發(fā)定位困難。

隨著對Android 源碼的深入研究,也有了其他兩種比較方便的方式,并且這兩種方式侵入性小,占用內(nèi)存低,能夠更好的用在實(shí)際場景中:

  • 利用UI線程的Looper打印的日志匹配;
  • 使用Choreographer.FrameCallback

利用 UI 線程的 Looper 打印的日志匹配

Android 主線程更新 UI。如果界面1秒鐘刷新少于 60 次,即 FPS 小于 60,用戶就會產(chǎn)生卡頓感覺。簡單來說,Android 使用消息機(jī)制進(jìn)行 UI 更新,UI 線程有個 Looper,在其 loop方法中會不斷取出 message,調(diào)用其綁定的 Handler 在 UI 線程執(zhí)行。如果在 handler 的 dispatchMesaage 方法里有耗時操作,就會發(fā)生卡頓。

下面來看下 Looper.loop( ) 的源碼

public static void loop() {
    final Looper me = myLooper();
    if (me == null) {
      throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
    }
    final MessageQueue queue = me.mQueue;

    // Make sure the identity of this thread is that of the local process,
    // and keep track of what that identity token actually is.
    Binder.clearCallingIdentity();
    final long ident = Binder.clearCallingIdentity();

    // Allow overriding a threshold with a system prop. e.g.
    // adb shell 'setprop log.looper.1000.main.slow 1 && stop && start'
    final int thresholdOverride =
        SystemProperties.getInt("log.looper."
            + Process.myUid() + "."
            + Thread.currentThread().getName()
            + ".slow", 0);

    boolean slowDeliveryDetected = false;

    for (;;) {
      Message msg = queue.next(); // might block
      if (msg == null) {
        // No message indicates that the message queue is quitting.
        return;
      }

      // This must be in a local variable, in case a UI event sets the logger
      final Printer logging = me.mLogging;
      if (logging != null) {
        logging.println(">>>>> Dispatching to " + msg.target + " " +
            msg.callback + ": " + msg.what);
      }
      // Make sure the observer won't change while processing a transaction.
      final Observer observer = sObserver;

      final long traceTag = me.mTraceTag;
      long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;
      long slowDeliveryThresholdMs = me.mSlowDeliveryThresholdMs;
      if (thresholdOverride > 0) {
        slowDispatchThresholdMs = thresholdOverride;
        slowDeliveryThresholdMs = thresholdOverride;
      }
      final boolean logSlowDelivery = (slowDeliveryThresholdMs > 0) && (msg.when > 0);
      final boolean logSlowDispatch = (slowDispatchThresholdMs > 0);

      final boolean needStartTime = logSlowDelivery || logSlowDispatch;
      final boolean needEndTime = logSlowDispatch;

      if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
        Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
      }

      final long dispatchStart = needStartTime ? SystemClock.uptimeMillis() : 0;
      final long dispatchEnd;
      Object token = null;
      if (observer != null) {
        token = observer.messageDispatchStarting();
      }
      long origWorkSource = ThreadLocalWorkSource.setUid(msg.workSourceUid);
      try {
        msg.target.dispatchMessage(msg);
        if (observer != null) {
          observer.messageDispatched(token, msg);
        }
        dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
      } catch (Exception exception) {
        if (observer != null) {
          observer.dispatchingThrewException(token, msg, exception);
        }
        throw exception;
      } finally {
        ThreadLocalWorkSource.restore(origWorkSource);
        if (traceTag != 0) {
          Trace.traceEnd(traceTag);
        }
      }
      if (logSlowDelivery) {
        if (slowDeliveryDetected) {
          if ((dispatchStart - msg.when) <= 10) {
            Slog.w(TAG, "Drained");
            slowDeliveryDetected = false;
          }
        } else {
          if (showSlowLog(slowDeliveryThresholdMs, msg.when, dispatchStart, "delivery",
              msg)) {
            // Once we write a slow delivery log, suppress until the queue drains.
            slowDeliveryDetected = true;
          }
        }
      }
      if (logSlowDispatch) {
        showSlowLog(slowDispatchThresholdMs, dispatchStart, dispatchEnd, "dispatch", msg);
      }

      if (logging != null) {
        logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
      }

      // Make sure that during the course of dispatching the
      // identity of the thread wasn't corrupted.
      final long newIdent = Binder.clearCallingIdentity();
      if (ident != newIdent) {
        Log.wtf(TAG, "Thread identity changed from 0x"
            + Long.toHexString(ident) + " to 0x"
            + Long.toHexString(newIdent) + " while dispatching to "
            + msg.target.getClass().getName() + " "
            + msg.callback + " what=" + msg.what);
      }

      msg.recycleUnchecked();
    }
  }

代碼中兩處標(biāo)紅的地方,就是 msg.target.dispatchMessage(msg) 的執(zhí)行前后索打印的 log。通過測量處理時間就能檢測到部分UI線程是否有耗時的操作。注意到這行執(zhí)行代碼的前后,有兩個 logging.println 函數(shù),如果設(shè)置了logging,會分別打印出 ”>>>>> Dispatching to “ 和 ”<<<<< Finished to “ 這樣的日志,這樣我們就可以通過兩次log的時間差值,來計(jì)算 dispatchMessage 的執(zhí)行時間,從而設(shè)置閾值判斷是否發(fā)生了卡頓。

那么如何設(shè)置 logging 呢?

我們看下面的代碼:

/**
   * Control logging of messages as they are processed by this Looper. If
   * enabled, a log message will be written to <var>printer</var>
   * at the beginning and ending of each message dispatch, identifying the
   * target Handler and message contents.
   *
   * @param printer A Printer object that will receive log messages, or
   * null to disable message logging.
   */
public final class Looper { 
  private Printer mLogging; 
  public void setMessageLogging(@Nullable Printer printer) { 
    mLogging = printer; 
  } 
} 

public interface Printer { 
  void println(String x); 
}

Looper 的 mLogging 是私有的,并且提供了 setMessageLogging(@Nullable Printer printer) 方法,所以我們可以自己實(shí)現(xiàn)一個 Printer,在通過 setMessageLogging() 方法傳入即可,代碼如下:

public class BlockDetectByPrinter {
  
  public static void start() {
    Looper.getMainLooper().setMessageLogging(new Printer() {
      private static final String START = ">>>>> Dispatching";
      private static final String END = "<<<<< Finished";

      @Override
      public void println(String x) {
        if (x.startsWith(START)) {
          LogMonitor.getInstance().startMonitor();
        }
        if (x.startsWith(END)) {
          LogMonitor.getInstance().removeMonitor();
        }
      }
    });
  }
}

設(shè)置了logging后,loop方法會回調(diào) logging.println 打印出每次消息執(zhí)行的時間日志:”>>>>> Dispatching to “和”<<<<< Finished to “。BlockDetectByPrinter 的使用則在Application 的 onCreate 方法中調(diào)用 BlockDetectByPrinter.start() 即可。

我們可以簡單實(shí)現(xiàn)一個 LogMonitor 來記錄卡頓時候主線程的堆棧信息。當(dāng)匹配到 >>>>> Dispatching 時,執(zhí)行 startMonitor,會在 200ms(設(shè)定的卡頓閾值)后執(zhí)行任務(wù),這個任務(wù)負(fù)責(zé)在子線程(非UI線程)打印UI線程的堆棧信息。如果消息低于 200ms 內(nèi)執(zhí)行完成,就可以匹配到 <<<<< Finished 日志,那么在打印堆棧任務(wù)啟動前執(zhí)行 removeMonitor 取消了這個任務(wù),則認(rèn)為沒有卡頓的發(fā)生;如果消息超過 200ms 才執(zhí)行完畢,此時認(rèn)為發(fā)生了卡頓,并打印 UI 線程的堆棧信息。

LogMonitor如何實(shí)現(xiàn)?

public class LogMonitor {
  private static final String TAG = "LogMonitor";
  private static LogMonitor sInstance = new LogMonitor();
  private HandlerThread mLogThread = new HandlerThread("log");
  private Handler mIoHandler;
  private static final long TIME_BLOCK = 200L;

  private LogMonitor() {
    mLogThread.start();
    mIoHandler = new Handler(mLogThread.getLooper());
  }

  private static Runnable mLogRunnable = new Runnable() {
    @Override
    public void run() {
      StringBuilder sb = new StringBuilder();
      StackTraceElement[] stackTrace = Looper.getMainLooper().getThread().getStackTrace();
      for (StackTraceElement s : stackTrace) {
        sb.append(s.toString() + "\n");
      }
      Log.e(TAG, sb.toString());
    }
  };

  public static LogMonitor getInstance() {
    return sInstance;
  }

  public boolean isMonitor() {
    return mIoHandler.hasCallbacks(mLogRunnable);
  }

  public void startMonitor() {
    mIoHandler.postDelayed(mLogRunnable, TIME_BLOCK);
  }

  public void removeMonitor() {
    mIoHandler.removeCallbacks(mLogRunnable);
  }
}

這里我們使用 HandlerThread 來構(gòu)造一個 Handler,HandlerThread 繼承自 Thread,實(shí)際上就一個 Thread,只不過比普通的 Thread 多了一個 Looper,對外提供自己這個 Looper 對象的 getLooper 方法,然后創(chuàng)建 Handler 時將 HandlerThread 中的 looper 對象傳入。這樣我們的 mIoHandler 對象就是與 HandlerThread 這個非 UI 線程綁定的了,它處理耗時操作將不會阻塞UI。如果UI線程阻塞超過 200ms,就會在子線程中執(zhí)行 mLogRunnable,打印出 UI 線程當(dāng)前的堆棧信息,如果處理消息沒有超過 1000ms,則會實(shí)時的 remove 掉這個mLogRunnable 任務(wù)。

發(fā)生卡頓時打印出堆棧信息的大致內(nèi)容如下,開發(fā)可以通過 log 定位耗時的地方。

2020-10-30 14:26:13.823 30359-30415/com.example.myproxyplugin E/LogMonitor: java.lang.Thread.sleep(Native Method)
    java.lang.Thread.sleep(Thread.java:443)
    java.lang.Thread.sleep(Thread.java:359)
    com.example.myproxyplugin.MainActivity$1.run(MainActivity.java:22)
    android.os.Handler.handleCallback(Handler.java:900)
    android.os.Handler.dispatchMessage(Handler.java:103)
    android.os.Looper.loop(Looper.java:219)
    android.app.ActivityThread.main(ActivityThread.java:8347)
    java.lang.reflect.Method.invoke(Native Method)
    com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)

優(yōu)點(diǎn):用戶使用 app 或者測試過程中都能從app層面來監(jiān)控卡頓情況,一旦出現(xiàn)卡頓能記錄 app 狀態(tài)和信息, 只要dispatchMesaage執(zhí)行耗時過大都會記錄下來,不再有前面兩種adb方式面臨的問題與不足。

缺點(diǎn):需另開子線程獲取堆棧信息,會消耗少量系統(tǒng)資源。

在實(shí)際實(shí)現(xiàn)中,不同手機(jī)不同 Android  系統(tǒng)甚至是不同的 ROM 版本,Loop 函數(shù)不一定都能打印出 ”>>>>> Dispatching to “ 和 ”<<<<< Finished to “ 這樣的日志,導(dǎo)致該方式無法進(jìn)行。

優(yōu)化的策略:我們知道 Loop 函數(shù)開始和結(jié)束必會執(zhí)行 println 打印日志,所以優(yōu)化版本將卡頓的判斷改為,Loop輸出第一句 log 時當(dāng)作 startMonitor,輸出下一句log時當(dāng)作end時刻來解決這個問題。

其實(shí) Looper 中有個 Observer 接口可以很好的完成這個任務(wù),只是因?yàn)楸粯?biāo)記為 hide 了,所以我們不能使用,不過可以知道下。

Observer 接口提供了三個方法,分別是監(jiān)聽任務(wù)開始,結(jié)束,發(fā)生錯誤的回調(diào)。

  /** {@hide} */
  public interface Observer {
    /**
     * Called right before a message is dispatched.
     *
     * <p> The token type is not specified to allow the implementation to specify its own type.
     *
     * @return a token used for collecting telemetry when dispatching a single message.
     *     The token token must be passed back exactly once to either
     *     {@link Observer#messageDispatched} or {@link Observer#dispatchingThrewException}
     *     and must not be reused again.
     *
     */
    Object messageDispatchStarting();

    /**
     * Called when a message was processed by a Handler.
     *
     * @param token Token obtained by previously calling
     *       {@link Observer#messageDispatchStarting} on the same Observer instance.
     * @param msg The message that was dispatched.
     */
    void messageDispatched(Object token, Message msg);

    /**
     * Called when an exception was thrown while processing a message.
     *
     * @param token Token obtained by previously calling
     *       {@link Observer#messageDispatchStarting} on the same Observer instance.
     * @param msg The message that was dispatched and caused an exception.
     * @param exception The exception that was thrown.
     */
    void dispatchingThrewException(Object token, Message msg, Exception exception);
  }

利用Choreographer.FrameCallback監(jiān)控卡頓

Choreographer.FrameCallback 官方文檔鏈接(https://developer.android.com/reference/android/view/Choreographer.FrameCallback.html)

我們知道, Android 系統(tǒng)每隔 16ms 發(fā)出 VSYNC 信號,來通知界面進(jìn)行重繪、渲染,每一次同步的周期為16.6ms,代表一幀的刷新頻率。SDK 中包含了一個相關(guān)類,以及相關(guān)回調(diào)。理論上來說兩次回調(diào)的時間周期應(yīng)該在 16ms,如果超過了 16ms 我們則認(rèn)為發(fā)生了卡頓,利用兩次回調(diào)間的時間周期來判斷是否發(fā)生卡頓(這個方案是 Android 4.1 API 16 以上才支持)。

這個方案的原理主要是通過 Choreographer 類設(shè)置它的 FrameCallback 函數(shù),當(dāng)每一幀被渲染時會觸發(fā)回調(diào) FrameCallback, FrameCallback 回調(diào) void doFrame (long frameTimeNanos) 函數(shù)。一次界面渲染會回調(diào) doFrame 方法,如果兩次 doFrame 之間的間隔大于 16.6ms 說明發(fā)生了卡頓。

public class FPSFrameCallback implements Choreographer.FrameCallback {

  private static final String TAG = "FPS_TEST";
  private long mLastFrameTimeNanos = 0;
  private long mFrameIntervalNanos;

  public FPSFrameCallback(long lastFrameTimeNanos) {
    mLastFrameTimeNanos = lastFrameTimeNanos;
    // 1s 60 幀
    mFrameIntervalNanos = (long) (1000000000 / 60.0);
  }

  @Override
  public void doFrame(long frameTimeNanos) {

    //初始化時間
    if (mLastFrameTimeNanos == 0) {
      mLastFrameTimeNanos = frameTimeNanos;
    }
    final long jitterNanos = frameTimeNanos - mLastFrameTimeNanos;
    if (jitterNanos >= mFrameIntervalNanos) {
      final long skippedFrames = jitterNanos / mFrameIntervalNanos;
      if (skippedFrames > 30) {
        Log.i(TAG, "Skipped " + skippedFrames + " frames! "
            + "The application may be doing too much work on its main thread.");
      }
    }
    mLastFrameTimeNanos = frameTimeNanos;
    //注冊下一幀回調(diào)
    Choreographer.getInstance().postFrameCallback(this);
  }
}

本質(zhì)和 log 沒太多區(qū)別,但是這個更加通用些,不會因?yàn)闄C(jī)型系統(tǒng)原因出現(xiàn)不可用的問題。

示例

下面進(jìn)入實(shí)戰(zhàn),看看代碼層面是如何實(shí)現(xiàn)的。

MainActivity 代碼如下:

public class MainActivity extends AppCompatActivity {
  Handler handler = new Handler(Looper.getMainLooper());

  private final Runnable runnable = new Runnable() {
    @Override
    public void run() {
      try {
        Thread.sleep(600);
        handler.postDelayed(runnable, 500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Choreographer.getInstance().postFrameCallback(new FPSFrameCallback(System.nanoTime()));
    BlockDetectByPrinter.start();
  }

  @Override
  protected void onResume() {
    super.onResume();
    handler.postDelayed(runnable, 500);
  }

}

收集到的堆棧信息如下:

2020-10-30 14:26:13.823 30359-30415/com.example.myproxyplugin E/LogMonitor: java.lang.Thread.sleep(Native Method)
    java.lang.Thread.sleep(Thread.java:443)
    java.lang.Thread.sleep(Thread.java:359)
    com.example.myproxyplugin.MainActivity$1.run(MainActivity.java:22)
    android.os.Handler.handleCallback(Handler.java:900)
    android.os.Handler.dispatchMessage(Handler.java:103)
    android.os.Looper.loop(Looper.java:219)
    android.app.ActivityThread.main(ActivityThread.java:8347)
    java.lang.reflect.Method.invoke(Native Method)
    com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)

對于 FPS log 可以看到如下信息:

     I/Choreographer: Skipped 64 frames!  The application may be doing too much work on its main thread.
     I/FPS_TEST: Skipped 65 frames!  The application may be doing too much work on its main thread.

上述就是小編為大家分享的Android如何發(fā)現(xiàn)APP卡頓了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI