Event log的各 TAG流程分析

Event log中有很多TAG通过他们可以分析一些运行是耗时在哪个区间。
在终端打印adb shell -b events ,Event log常见的启动应用打印log:
R版本正常一个应用冷启动的创建流程:
wm_task_created--》wm_stack_created--》wm_create_task--》wm_create_activity--》
am_proc_start--》am_proc_bound--》wm_restart_activity--》
wm_set_resumed_activity--》wm_on_create_called--》wm_on_start_called--》
wm_on_resume_called
# Application process has been started
am_proc_start 表示创建进程成功;
ActivityManagerService.startProcess -->
ActivityManagerService.startProcessLocked -->
ProcessList.startProcessLocked -->
ProcessList.handleProcessStartedLocked
EventLog.writeEvent(EventLogTags.AM_PROC_START,UserHandle.getUserId(app.startUid), pid, app.startUid,app.processName, app.hostingRecord.getType(),app.hostingRecord.getName() != null ? app.hostingRecord.getName() : "");

# Application process bound to work
am_proc_bound 表示与ActivityThread连接 attachAppicaiton成功;
ActivityThread.main-->ActivityThread.attach-->AMS.attachApplication-->AMS.attachApplicationLocked(am_proc_bound)

wm_task_to_front: [0,14]
ActivityTask. moveTaskToFront-->
EventLogTags.writeWmTaskToFront(tr.mUserId, tr.mTaskId);

R版本之后将如下都从am移到了wm目录
# A new activity is being created in an existing task:
Android R变更为:wm_create_activity
am_create_activity 表示调用了startActivity;
ActivityStarter.startActivity-->ActivityStarter.startActivityUnchecked(am_create_activity)-->ActivityStackSupervisor.resumeFocusedStackTopActivityLocked

# An activity has been resumed into the foreground but was not already running:
Android R变更为:wm_restart_activity
am_restart_activity 表示AMS发起handleLaunchActivity的调用, Activity restart,有2种可能,一种是从Recent中启动,另一种是热启动,如果设置了and Resume,将会调用ActivityThread的resume流程。
1.ActivityTask.resumeTopActivityInnerLocked-->ActivityStackSupervisor.startSpecificActivity-->
2.RootWindowContainer.attachApplication-->startActivityForAttachedApplicationIfNeeded-->
ActivityStackSupervisor.realStartActivityLocked
{
EventLogTags.writeWmRestartActivity
}
 EventLogTags.writeWmRestartActivity(r.mUserId, System.identityHashCode(r),task.mTaskId, r.shortComponentName);

#Activity set to resumed
 Android R变更为:wm_set_resumed_activity
am_set_resumed_activity 表示AMS服务端ActivityRecord设置为resumed,该Log就是
AMS.setResumedActivityUncheckLocked函数被调用,
如果存在am_restart_activity Log时将会在它后面执行,中间会执行ActivityThread的调用
(从AMS调到IApplicationThread里的方法都是加了oneWay的,因此不会阻塞在transact,此后ActivityThread将对应的通过Handler方法Post到MainThread执行)
ActivityStackSupervisor.realStartActivityLocked{EventLogTags.writeWmRestartActivity}-->ActivityTaskManagerService.setResumedActivityUncheckLocked
冷启动:执行minimalResumeActivityLocked
ActivityStack.minimalResumeActivityLocked{
   r.completeResumeLocked();
  mStackSupervisor.getLaunchTimeTracker().setLaunchTime(r);
}
在ActivityRecord的completeResumeLocked中setVisibility(true)在系统端置为可见状态。
wm_set_resumed_activity: [0,com.facebook.katana/.gdp.ProxyAuthDialog,minimalResumeActivityLocked]
相关调用:
ActivityThread.main  --> 
ActivityThread.attach -->
ActivityManagerService.attachApplication  -->
ActivityManagerService.attachApplicationLocked {EventLogTags.writeAmProcBound
里面做ActivityThread.bindApplication -->将数据封装为AppBindData通过H.BIND_APPLICATION发送-->handleBindApplication
Trace中打印bindApplication在handleBindApplication 中主要完成如下步骤:
1.将UI线程注册为运行时的敏感线程
2.创建上下文createAppContext
3.反射创建Instrumentation实例
4.获取ApplicationThread实例data.info.makeApplication,调用LoadedApk中makeApplication的创建应用中的Application Trace中打印makeApplication
5.调用Instrumentation.callApplicationOnCreate(Application的onCreate的生命周期)
}-->
RootWindowContainer.attachApplication  -->
WindowContainer.forAllActivities  -->
ActivityRecord.forAllActivities  -->
RootWindowContainer.startActivityForAttachedApplicationIfNeeded  -->
ActivityStackSupervisor.realStartActivityLocked{EventLogTags.writeWmRestartActivity} -->
ActivityStack.minimalResumeActivityLocked  -->
ActivityRecord.setState  -->
Task.onActivityStateChanged  -->
ActivityTaskManagerService.setResumedActivityUncheckLocked{EventLogTags.writeWmSetResumedActivity}
EventLogTags.writeWmSetResumedActivity(r == null ? -1 : r.mUserId,r == null ? "NULL" : r.shortComponentName,reason)
热启动:从前一个pause之后将stack中resumeTopActivityInnerLocked
 wm_set_resumed_activity: [0,com.xxxxx.camera/com.android.camera.CameraLauncher,resumeTopActivityInnerLocked]
相关调用流程:
ActivityTaskManagerService.activityPaused  -->
ActivityRecord.activityPaused  -->
ActivityStack.completePauseLocked  -->
RootWindowContainer.resumeFocusedStacksTopActivities  -->
ActivityStack.resumeTopActivityUncheckedLocked  -->
ActivityStack.resumeTopActivityInnerLocked{
EventLogTags.writeWmResumeActivity(next.mUserId, System.identityHashCode(next),next.getTask().mTaskId, next.shortComponentName);
例子:wm_resume_activity: [0,184161873,14,com.xxxxx.childmode/.LaunchActivity]
}  -->
ActivityRecord.setState  -->
Task.onActivityStateChanged  -->
ActivityTaskManagerService.setResumedActivityUncheckLocked

frameworks/base/core/java/android/app/Activity.java
ActivityThread$H.handleMessage(ActivityThread.java:2165) --->
TransactionExecutor.execute  --->
TransactionExecutor.executeCallbacks  --->
LaunchActivityItem.execute --->
ActivityThread.handleLaunchActivity --->
ActivityThread.performLaunchActivity --->
Instrumentation.callActivityOnCreate --->
Activity.performCreate --->
Activity.performCreate
am_on_create_called 表示Activity的 onCreate被调用完成,即APP中的onCreate调用完成.
Android R变更为:wm_on_create_called
EventLogTags.writeWmOnCreateCalled(mIdent, getComponentName().getClassName(),"performCreate");

am_on_resume_called 表示Activity的onResume被调用完成.
Android R变更为:wm_on_resume_called
ClientTransactionHandler.scheduleTransaction
ActivityThread.H.EXECUTE_TRANSACTION -->
TransactionExecutor.execute -->
TransactionExecutor.executeLifecycleState -->
TransactionExecutor.cycleToPath -->
TransactionExecutor.performLifecycleSequence -->
ActivityThread.handleResumeActivity -->
ActivityThread.performResumeActivity -->
Activity.performResume-->
 Activity.performResume --> EventLogTags.writeWmOnResumeCalled(mIdent, getComponentName().getClassName(), reason);

wm_on_paused_called表示Activity的onPause被调用完成,这个状态处于不可接收输入事件,但还处于可见状态,不能在里面在耗时的操作会导致下一个界面启动慢导致ANR。
wm_on_paused_called: [160823258,com.android.settings.Settings,performPause]
 EventLogTags.writeWmOnPausedCalled(mIdent, getComponentName().getClassName(), "performPause");

wm_on_stop_called 表示Activity的onResume被调用完成.
wm_on_stop_called: [160823258,com.android.settings.Settings,STOP_ACTIVITY_ITEM]
EventLogTags.writeWmOnStartCalled(mIdent, getComponentName().getClassName(), reason);

am_activity_launch_time 表示Activity界面启动完成.
Android R变更为:wm_activity_launch_time
EventLog.writeEvent(AM_ACTIVITY_LAUNCH_TIME,userId, System.identityHashCode(this), shortComponentName,thisTime, totalTime);
final long thisTime = curTime - displayStartTime;
final long totalTime = entry.mLaunchStartTime != 0? (curTime - entry.mLaunchStartTime) : thisTime;
在ActivityRecord.reportLaunchTimeLocked中计算thisTime和totalTime的时间.
curTime表示该函数调用的时间点.
displayStartTime表示一连串启动Activity中的最后一个Activity的启动时间点.
mLaunchStartTime表示一连串启动Activity中第一个Activity的启动时间点.

wm_pause_activity
# Attempting to pause the current activity
wm_pause_activity (User|1|5),(Token|1|5),(Component Name|3),(User Leaving|3)
入口:
1.ActivityStack.goToSleepIfPossible
2.ActivityStack.resumeTopActivityInnerLocked
3.WindowProcessController.updateTopResumingActivityInProcessIfNeeded
4.ActivityRecord.finishIfPossible
-->
ActivityStack.startPausingLocked(boolean userLeaving, boolean uiSleeping, ActivityRecord resuming) -->
 EventLogTags.writeWmPauseActivity(prev.mUserId, System.identityHashCode(prev),prev.shortComponentName, "userLeaving=" + userLeaving);
例子:wm_pause_activity: [0,160823258,com.android.settings/.Settings,userLeaving=true]

wm_add_to_stopping
EventLogTags.writeWmAddToStopping(mUserId, System.identityHashCode(this),shortComponentName, reason);
路径1:
* Ensure visibility with an option to also update the configuration of visible activities.
* @see ActivityStack#ensureActivitiesVisible(ActivityRecord, int, boolean)
* @see RootWindowContainer#ensureActivitiesVisible(ActivityRecord, int, boolean)
EnsureActivitiesVisibleHelper.process -->
EnsureActivitiesVisibleHelper.setActivityVisibilityState-->
ActivityRecord.makeInvisible{setVisibility(false)}-->addToStopping
wm_add_to_stopping: [0,160823258,com.android.settings/.Settings,makeInvisible]
路径2
ActivityRecord.completePauseLocked-->addToStopping
wm_add_to_stopping: [0,146011304,com.android.settings/.SubSettings,completePauseLocked]
路径3:
* Complete activity finish request that was initiated earlier. If the activity is still
* pausing we will wait for it to complete its transition. If the activity that should appear in
* place of this one is not visible yet - we'll wait for it first. Otherwise - activity can be
* destroyed right away.
ActivityRecord.completeFinishing-->addToStopping
wm_add_to_stopping:[0,146011304,com.android.settings/.SubSettings,completeFinishing]

RecentsAnimation.preloadRecentsActivity

wm_resume_activity
ActivityStack.resumeTopActivityInnerLocked
EventLogTags.writeWmResumeActivity(next.mUserId, System.identityHashCode(next),next.getTask().mTaskId, next.shortComponentName);
wm_resume_activity: [0,29845079,153,com.android.incallui/.InCallActivity]

主线程的Message 处于Idle状态执行
wm_stop_activity
wm_stop_activity: [0,160823258,com.android.settings/.Settings]
ActivityRecord.onAnimationFinished --->
ActivityStackSupervisor.scheduleProcessStoppingAndFinishingActivities-->
H.PROCESS_STOPPING_AND_FINISHING_MSG --->
* Processes the activities to be stopped or destroyed. This should be called when the resumed
* activities are idle or drawn.
ActivityStackSupervisor.processStoppingAndFinishingActivities--->
ActivityRecord.stopIfPossible -->
EventLogTags.writeWmStopActivity( mUserId, System.identityHashCode(this), shortComponentName);

wm_failed_to_pause
ActivityRecord.activityPaused-->writeWmFailedToPause
EventLogTags.writeWmFailedToPause(mUserId, System.identityHashCode(this),shortComponentName, stack.mPausingActivity != null? stack.mPausingActivity.shortComponentName : "(none)");

 wm_destroy_activity
ActivityRecord.completeFinishing-->ActivityRecord.destroyIfPossible -->ActivityRecord.destroyImmediately
EventLogTags.writeWmDestroyActivity(mUserId, System.identityHashCode(this),task.mTaskId, shortComponentName, reason);
wm_destroy_activity: [0,230265721,331,com.xxxxx.phonemaster/com.cyin.himgr.widget.activity.MainActivity,finish-imm:idle]

frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java 
// Called on the PowerManager's Notifier thread.
  finishedGoingToSleep --> EventLogTags.writeScreenToggled(0); 灭屏
  startedWakingUp ---> EventLogTags.writeScreenToggled(1);亮屏
Event log 中打印如何判断是否是亮灭屏由于是在子线程中打印与真实的亮灭屏时间存在偏差
screen_toggled: 1 亮屏
screen_toggled: 0 灭屏
power_screen_state: [1,0,0,0,233]亮屏
power_screen_state: [0,3,0,0,624]灭屏
power_screen_state (offOrOn|1|5),(becauseOfUser|1|5),(totalTouchDownTime|2|3),(touchCycles|1|1),(latency|1|3)


01-13 03:58:59.176 1173 1356 I am_low_memory: 6    记录当前Lru进程队列长度

30047 am_pss (Pid|1|5),(UID|1|5),(Process Name|3),(Pss|2|2),(Uss|2|2),(SwapPss|2|2),(Rss|2|2),(StatType|1|5),(ProcState|1|5),(TimeToCollect|2|2)
01-13 03:59:12.495 1173 1369 I am_pss : [4774,1000,com.transsion.overlaysuw,147921920,137489408,0,276503552,0,2,11]
查看第三个参数147921920即应用占用的内存147M

30046 am_meminfo (Cached|2|2),(Free|2|2),(Zram|2|2),(Kernel|2|2),(Native|2|2)
01-13 03:59:24.584 1173 1369 I am_meminfo: [666992640,35926016,4096,314904576,493999104]
查看分别是Cached,Free,Zram,Kernel,Native占用内存的大小

# Report changing memory conditions (Values are ProcessStats.ADJ_MEM_FACTOR* constants)
30050 am_mem_factor (Current|1|5),(Previous|1|5)
分为四个等级:
public static final int ADJ_MEM_FACTOR_NORMAL = 0;//正常等级
public static final int ADJ_MEM_FACTOR_MODERATE = 1;//中等等级
public static final int ADJ_MEM_FACTOR_LOW = 2;//存在低内存
public static final int ADJ_MEM_FACTOR_CRITICAL = 3;//严重低内存

12-23 11:46:21.522 1362 5856 I am_mem_factor: [1,0]
12-23 11:46:27.408 1362 5857 I am_mem_factor: [0,1]
12-23 11:47:01.640 1362 5855 I am_mem_factor: [3,0]
查看ANR前一段时间是否存在低内存状态

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值