說(shuō)說(shuō)Android的廣播(5) - 廣播的歷史

說(shuō)說(shuō)Android的廣播(5) - 廣播的歷史

廣播的歷史

AMS中的歷史信息

處理完廣播之后,BroadcastQueue會(huì)記錄一段歷史用于調(diào)試:

  • mBroadcastHistory記錄最近的BroadcastRecord
  • mBroadcastSummaryHistory記錄最近的Intent
  • mSummaryHistoryEnqueueTime記錄最近的enqueueTime
  • mSummaryHistoryDispatchTime記錄最近的dispatchTime
  • mSummaryHistoryFinishTime記錄最近的finishTime

具體的定義如下:

/**
 * Historical data of past broadcasts, for debugging.  This is a ring buffer
 * whose last element is at mHistoryNext.
 */
final BroadcastRecord[] mBroadcastHistory = new BroadcastRecord[MAX_BROADCAST_HISTORY];
int mHistoryNext = 0;

/**
 * Summary of historical data of past broadcasts, for debugging.  This is a
 * ring buffer whose last element is at mSummaryHistoryNext.
 */
final Intent[] mBroadcastSummaryHistory = new Intent[MAX_BROADCAST_SUMMARY_HISTORY];
int mSummaryHistoryNext = 0;

/**
 * Various milestone timestamps of entries in the mBroadcastSummaryHistory ring
 * buffer, also tracked via the mSummaryHistoryNext index.  These are all in wall
 * clock time, not elapsed.
 */
final long[] mSummaryHistoryEnqueueTime = new  long[MAX_BROADCAST_SUMMARY_HISTORY];
final long[] mSummaryHistoryDispatchTime = new  long[MAX_BROADCAST_SUMMARY_HISTORY];
final long[] mSummaryHistoryFinishTime = new  long[MAX_BROADCAST_SUMMARY_HISTORY];

BroadcastRecord中的歷史信息

BroadcastRecord中,除了前面我們講的這幾個(gè)時(shí)間:

long enqueueClockTime;  // the clock time the broadcast was enqueued
long dispatchTime;      // when dispatch started on this set of receivers
long dispatchClockTime; // the clock time the dispatch started
long receiverTime;      // when current receiver started for timeouts.
long finishTime;        // when we finished the broadcast.

還有記錄的ANR的次數(shù):

int anrCount;           // has this broadcast record hit any ANRs?

可惜這兩部分信息只是被打日志調(diào)試時(shí)用到。

BroadcastQueue中記錄歷史的過(guò)程

BroadcastQueue中提供了addBroadcastToHistoryLocked方法來(lái)記錄歷史信息。

1221    private final void addBroadcastToHistoryLocked(BroadcastRecord r) {
1222        if (r.callingUid < 0) {
1223            // This was from a registerReceiver() call; ignore it.
1224            return;
1225        }
1226        r.finishTime = SystemClock.uptimeMillis();
1227
1228        mBroadcastHistory[mHistoryNext] = r;
1229        mHistoryNext = ringAdvance(mHistoryNext, 1, MAX_BROADCAST_HISTORY);
1230
1231        mBroadcastSummaryHistory[mSummaryHistoryNext] = r.intent;
1232        mSummaryHistoryEnqueueTime[mSummaryHistoryNext] = r.enqueueClockTime;
1233        mSummaryHistoryDispatchTime[mSummaryHistoryNext] = r.dispatchClockTime;
1234        mSummaryHistoryFinishTime[mSummaryHistoryNext] = System.currentTimeMillis();
1235        mSummaryHistoryNext = ringAdvance(mSummaryHistoryNext, 1, MAX_BROADCAST_SUMMARY_HISTORY);
1236    }

其中,ringAdvance的實(shí)現(xiàn)是這樣的。從循環(huán)計(jì)數(shù)的角度來(lái)講,Android這段寫得并不好。只在increment為+1或者-1的情況下還算工作正常吧。

1214    private final int ringAdvance(int x, final int increment, final int ringSize) {
1215        x += increment;
1216        if (x < 0) return (ringSize - 1);
1217        else if (x >= ringSize) return 0;
1218        else return x;
1219    }

addBroadcastToHistoryLocked一共被調(diào)用兩次,全部都在processNextBroadcast函數(shù)中。一處是在并發(fā)隊(duì)列處理完之后,另一個(gè)是在有序隊(duì)列返回值以后。下節(jié)我們分析processNextBroadcast的時(shí)候,會(huì)把這些都串起來(lái)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容