目录

App Main Thread Blocking Faults

Introduction

This topic describes how to locate the app freeze (AppFreeze) issue caused by the main thread being blocked. According to the analysis of the main thread stack in the app freeze fault logs, the root causes of app freeze faults caused by main thread blocking are as follows:

For details about the AppFreeze detection principles and logs, see Application Freeze Detection.

Determining the Problem Type

For an AppFreeze problem, you can check whether it is caused by main thread blocking based on the AppFreeze logs and enhanced logs (sampling stack).

Check whether the main thread stacks of the alarm and freeze events are consistent. The two events include THREAD_BLOCK_3S and THREAD_BLOCK_6S, THREAD_BLOCK_3S and APP_INPUT_BLOCK, and LIFECYCLE_HALF_TIMEOUT and LIFECYCLE_TIMEOUT. If the stacks are consistent, the main thread is usually blocked. You are advised to determine the cause based on enhanced logs.

If the AppFreeze log contains only the single main thread stack of the APP_INPUT_BLOCK event, the main thread is not blocked.

Check whether the main thread stack in the AppFreeze log is consistent with the main thread sampling stack in the enhanced log. If they are consistent, the main thread is blocked.

In addition, you can determine whether the thread is running during the app freeze process by referring to the thread status description in Stack Information.

Root Causes

[h2]Lock Waiting

Description

The main thread of the app is blocked due to lock waiting.

Problem analysis

  1. Find the service shared library that holds the lock based on the stack, use the symbol or llvm-addr2line to parse the shared library to locate the code line, and determine the lock resource that is being waited for. Record the lock as lock A.
  2. In the process stack of the faulty app, search for the service shared library that is waiting for lock A, find the thread stack that contains the service shared library, and then locate the thread stack that holds lock A based on the code.
  3. If the root cause of the thread stack that holds lock A is not lock waiting, continue the analysis based on the corresponding root cause.
  4. If the root cause of the thread stack that holds lock A is also lock waiting, record the lock being waited for as lock B. In this case, analyze the problem again based on the analysis method in this section.

    If the thread that holds lock A is the main thread, or lock B that the thread holding lock A waits for is held by the main thread or another thread waiting for lock A, the problem is a deadlock.

Keywords

Check whether the shared library and symbol at the top of the stack of the main thread match the following keywords:

Shared library:

ld-musl-aarch64.so.1

Symbol:

Case analysis

Case 1: The main thread is blocked due to interlocking of the A and B locks between the child thread and main thread

Symptom

After a user triggers service code calling, the app does not respond. After a period of time, the app exits and an app freeze log file is generated.

Problem analysis

  1. Check the main thread stacks in THREAD_BLOCK_3S and THREAD_BLOCK_6S in the AppFreeze log. After confirming that they are consistent, analyze the stack content.

    Evidence 1: key stack frames of the main thread stack

    Tid:18728, Name:pfreezeanalysis
    state=S, utime=55, stime=15, priority=-54, nice=-10, clk=100
    #00 pc 00000000001de3f0 /system/lib/ld-musl-aarch64.so.1(__timedwait_cp+156)(ede7850a729ebc7f50c03216608d9277)
    #01 pc 00000000001e4488 /system/lib/ld-musl-aarch64.so.1(__pthread_mutex_timedlock_inner+536)(ede7850a729ebc7f50c03216608d9277)
    #02 pc 000000000000a6c8 /data/storage/el1/bundle/libs/arm64/libentry.so(f5f54ab64da94326c537007dfa9fbf43b7db6ea1)
    #03 pc 000000000007010c /system/lib64/platformsdk/libace_napi.z.so(panda::JSValueRef ArkNativeFunctionCallBack<true>(panda::JsiRuntimeCallInfo*)+284)(c41c28aa2f0cdd937fc9063a8b806514)
    #04 pc 0000000000e8ad70 /system/lib64/module/arkcompiler/stub.an(RTStub_AsmInterpreterEntry+484)
    #05 pc 0000000000e8abc8 /system/lib64/module/arkcompiler/stub.an(RTStub_AsmInterpreterEntry+60)

    #02 stack frame indicates that the service party waiting for the lock is libentry.so. Continue the analysis based on the code.

  2. In development and debugging scenarios, you can directly click the stack frame where libentry.so is located in DevEco Studio to go to the corresponding code line. In O&M scenarios, use llvm-addr2line to parse and find the corresponding code line.

    Evidence 2: code of the fault stack frame

    The code line points to pthread_mutex_lock(&g_deadlockMutexSecond), that is, the lock g_deadlockMutexSecond is waiting. Continue to analyze the lock status of g_deadlockMutexSecond.

  3. Search for g_deadlockMutexSecond in the code and confirm that it is used only in DeadlockOtherThread except TriggerDeadlockSync.

    Evidence 3: implementation code of DeadlockOtherThread

    DeadlockOtherThread is called in the child thread created by the main thread. After entering, the child thread first obtains the lock g_deadlockMutexSecond, waits for 1s, and then obtains the lock g_deadlockMutexFirst. Before the child thread is created, the main thread has obtained the lock g_deadlockMutexFirst. After the child thread is created, the main thread obtains the lock g_deadlockMutexSecond. A deadlock occurs between the main thread and the child thread, and the main thread is blocked while waiting for the lock.

Case 2: Two child threads are deadlocked, and the main thread is blocked because it cannot wait for the child threads to end

Symptom

After a user triggers service code calling, the app does not respond. After a period of time, the app exits and an app freeze log file is generated.

Problem analysis

  1. Check the main thread stacks in THREAD_BLOCK_3S and THREAD_BLOCK_6S in the AppFreeze log. After confirming that they are consistent, analyze the stack content.

    Evidence 1: key stack frames of the main thread stack

    Tid:30591, Name:pfreezeanalysis
    state=S, utime=21, stime=14, priority=-54, nice=-10, clk=100
    #00 pc 00000000001e3578 /system/lib/ld-musl-aarch64.so.1(pthread_join+136)(ede7850a729ebc7f50c03216608d9277)
    #01 pc 000000000000aae0 /data/storage/el1/bundle/libs/arm64/libentry.so(f5f54ab64da94326c537007dfa9fbf43b7db6ea1)
    #02 pc 000000000007010c /system/lib64/platformsdk/libace_napi.z.so(panda::JSValueRef ArkNativeFunctionCallBack<true>(panda::JsiRuntimeCallInfo*)+284)(c41c28aa2f0cdd937fc9063a8b806514)
    #03 pc 0000000000e8ad70 /system/lib64/module/arkcompiler/stub.an(RTStub_AsmInterpreterEntry+484)
    #04 pc 0000000000e8abc8 /system/lib64/module/arkcompiler/stub.an(RTStub_AsmInterpreterEntry+60)

    #01 stack frame indicates that the service party waiting for the lock is libentry.so. Continue the analysis based on the code.

  2. In development and debugging scenarios, you can directly click the stack frame where libentry.so is located in DevEco Studio to go to the corresponding code line. In O&M scenarios, use llvm-addr2line to parse and find the corresponding code line.

    Evidence 2: code of the fault stack frame

    The code line points to pthread_join(t1, nullptr), that is, waiting for the child thread to end. Continue to analyze the implementation of the child thread.

  3. Check the implementation code of the two child threads t1 and t2.

    Evidence 3: implementation code of the two child threads

    After entering, thread A first obtains the lock g_deadlockMutexFirst, waits for 1 ms, and then obtains the lock g_deadlockMutexSecond. Thread B does the opposite: it first obtains the lock g_deadlockMutexSecond and then waits for 1 ms to obtain the lock g_deadlockMutexFirst. In this way, after the two child threads enter, both of them obtain the lock that the other wants to obtain, resulting in a deadlock. Neither of the two child threads can end, causing the main thread to wait and be blocked.

Prevention suggestions

Optimize the lock usage policy to reduce lock contention. It is recommended that a lock-free data structure be used.

[h2]Synchronous Binder API Call Blocking

Description

The main thread of an app calls the synchronous Binder API for a system service or another process. The peer end does not return a response for a long time, causing a block.

Problem analysis

  1. Find the app-side service and service API that initiate the IPC based on the stack.
  2. Find the process ID and IPC thread ID of the peer service process in the Binder information based on the main thread ID.
  3. Find the stack of the peer service process based on the process ID, and then find the corresponding IPC thread stack.
  4. Determine the root cause of the IPC thread blocking, and then continue the analysis based on the analysis method for the corresponding root cause.

Keywords

Check whether the shared library and symbol of the main thread stack match the following keywords:

Shared library:

libipc_common.z.so

Symbol:

WriteBinder

Case analysis

Case: The main thread is blocked because the system audio service does not respond in a timely manner

Symptom

After a user triggers service code calling, the app does not respond. After a period of time, the app exits and an app freeze log file is generated.

Problem analysis

  1. Check the main thread stacks in THREAD_BLOCK_3S and THREAD_BLOCK_6S in the AppFreeze log. After confirming that they are consistent, analyze the stack content.

    Evidence 1: key stack frames of the main thread stack

    Tid:41777, Name:pfreezeanalysis
    state=S, utime=118, stime=49, priority=-14, nice=-10, clk=100
    #00 pc 0000000000197768 /system/lib/ld-musl-aarch64.so.1(ioctl+172)(8c2ca4e308a1de9ed1e6b4ccc14c7443)
    #01 pc 000000000000f0ac /system/lib64/platformsdk/libipc_common.z.so(OHOS::BinderConnector::WriteBinder(unsigned long, void*)+120)(21def93073c96c4161bfa61b948c2957)
    #02 pc 0000000000074934 /system/lib64/platformsdk/libipc_single.z.so(OHOS::BinderInvoker::TransactWithDriver(bool)+284)(5a12a4b7e05d71d18ea20b9f1e1aed8a)
    #03 pc 0000000000073154 /system/lib64/platformsdk/libipc_single.z.so(OHOS::BinderInvoker::WaitForCompletion(OHOS::MessageParcel*)+124)(5a12a4b7e05d71d18ea20b9f1e1aed8a)
    #04 pc 0000000000072624 /system/lib64/platformsdk/libipc_single.z.so(OHOS::BinderInvoker::SendRequest(int, unsigned int, OHOS::MessageParcel&, OHOS::MessageParcel&, OHOS::MessageOption&)+620)(5a12a4b7e05d71d18ea20b9f1e1aed8a)
    #05 pc 0000000000049d24 /system/lib64/platformsdk/libipc_single.z.so(OHOS::IPCObjectProxy::SendRequestInner(bool, unsigned int, OHOS::MessageParcel&, OHOS::MessageParcel&, OHOS::MessageOption&)+272)(5a12a4b7e05d71d18ea20b9f1e1aed8a)
    #06 pc 000000000004a73c /system/lib64/platformsdk/libipc_single.z.so(OHOS::IPCObjectProxy::SendRequest(unsigned int, OHOS::MessageParcel&, OHOS::MessageParcel&, OHOS::MessageOption&)+216)(5a12a4b7e05d71d18ea20b9f1e1aed8a)
    #07 pc 0000000000040dd4 /system/lib64/libaudio_policy_client_idl_interface.z.so(OHOS::AudioStandard::AudioPolicyProxy::GetAudioScene(int&)+320)(b95b8a9a8cca6ee70d60967fc1751cc5)
    #08 pc 000000000008cfec /system/lib64/platformsdk/libaudio_client.z.so(OHOS::AudioStandard::AudioSystemManager::GetAudioScene() const+44)(0e4839c7e3f20d5152f89c053c31d414)
    #09 pc 00000000000e0c44 /system/lib64/module/multimedia/libaudio.z.so(OHOS::AudioStandard::NapiAudioManager::GetAudioSceneSync(napi_env__*, napi_callback_info__*) (.cfi)+228)(3201d994d8d5f9e8fdf1c9aa5ed39509)
    #10 pc 0000000000067c90 /system/lib64/platformsdk/libace_napi.z.so(panda::JSValueRef ArkNativeFunctionCallBack<true>(panda::JsiRuntimeCallInfo*)+288)(76bc1ed675edcc5f429a976d6c9b955d)
    #11 pc 0000000000e179f4 /system/lib64/module/arkcompiler/stub.an(RTStub_PushCallArgsAndDispatchNative+40)
    #12 pc 000000000046b900 /system/lib64/module/arkcompiler/stub.an(BCStub_HandleCallthis0Imm8V8StwCopy+392)
    #13 at triggerBinderInterfaceBlockSync (entry|entry|1.0.0|src/main/ets/pages/page_second/page_third_appfreeze/appfreeze_threadblock.ts:147:43)

    The #13 stack frame of the stack indicates that the service function on the app side is triggerBinderInterfaceBlockSync, and the #09 stack frame indicates that the service calls the GetAudioSceneSync API provided by the system audio service. Because the system audio service does not respond in a timely manner, the main thread is blocked on the synchronous Binder communication. Because the peer process is the system audio service and does not belong to the service of the faulty app, you are advised to report the issue to the system side for optimization. If the peer process is the service of the faulty app, perform the following steps for further analysis.

  2. View the BinderCatcher information in the log and determine the process ID and IPC thread ID of the peer process based on to server_pid:server_tid.

    Evidence 2: BinderCatcher information

    41777:41777 to 784:801 code 1b wait:6.446522656 s frz_state:1,  ns:-1:-1 to -1:-1, debug:41777:41777 to 784:801, active_code:0, active_thread=0, pending_async_proc=0

    The BinderCatcher information indicates that the main thread has waited for the response from the peer process for more than 6 seconds. The process ID of the peer process is 784, and the IPC thread ID is 801.

  3. Find the information about the peer process based on the process ID, and then find the stack of the IPC thread based on the IPC thread ID.

    Evidence 3: key stack frames of the IPC thread stack of the peer process

    Tid:801, Name:OS_IPC_0_801
    state=S, utime=14, stime=9, priority=20, nice=0, clk=100
    #00 pc 0000000000197768 /system/lib/ld-musl-aarch64.so.1(ioctl+172)(8c2ca4e308a1de9ed1e6b4ccc14c7443)
    #01 pc 000000000000f0ac /system/lib64/platformsdk/libipc_common.z.so(OHOS::BinderConnector::WriteBinder(unsigned long, void*)+120)(21def93073c96c4161bfa61b948c2957)
    #02 pc 0000000000074934 /system/lib64/platformsdk/libipc_single.z.so(OHOS::BinderInvoker::TransactWithDriver(bool)+284)(5a12a4b7e05d71d18ea20b9f1e1aed8a)
    #03 pc 0000000000074d24 /system/lib64/platformsdk/libipc_single.z.so(OHOS::BinderInvoker::StartWorkLoop()+100)(5a12a4b7e05d71d18ea20b9f1e1aed8a)
    #04 pc 0000000000077958 /system/lib64/platformsdk/libipc_single.z.so(OHOS::BinderInvoker::JoinThread(bool)+80)(5a12a4b7e05d71d18ea20b9f1e1aed8a)
    #05 pc 000000000006c248 /system/lib64/platformsdk/libipc_single.z.so(OHOS::IPCWorkThread::ThreadHandler(void*)+1056)(5a12a4b7e05d71d18ea20b9f1e1aed8a)
    #06 pc 00000000001de740 /system/lib/ld-musl-aarch64.so.1(8c2ca4e308a1de9ed1e6b4ccc14c7443)
    #07 pc 00000000000b59ec /system/lib/ld-musl-aarch64.so.1(8c2ca4e308a1de9ed1e6b4ccc14c7443)

    If the stack of the IPC thread indicates that the root cause of the service unresponsiveness is also the blocking of the synchronous Binder API call, continue the analysis based on the analysis method in this section.

Prevention suggestions

Move the synchronous Binder call to the child thread, or use an asynchronous API.

[h2]Full Binder Thread on the Peer End

Description

The main thread of an app calls the synchronous Binder API for a system service or another process. The peer Binder thread is exhausted. As a result, the call does not return for a long time and is blocked.

Problem analysis

  1. Find the app-side service and service API that initiate the IPC based on the stack.
  2. Find the process ID and IPC thread ID of the peer service process in the Binder information based on the main thread ID.
  3. Find the stack of the peer service process based on the process ID, and then find the corresponding IPC thread stack.
  4. Reanalyze the IPC thread based on the root cause analysis approach for the fault type.

Keywords

Check whether the shared library and symbol of the main thread stack match the following keywords:

Shared library:

libipc_common.z.so

Symbol:

WriteBinder

Case analysis

Case: The main thread is blocked because the Binder thread of the system audio service is exhausted and does not respond in a timely manner

Symptom

After a user triggers service code calling, the app does not respond. After a period of time, the app exits and an app freeze log file is generated.

Problem analysis

  1. Check the main thread stacks in THREAD_BLOCK_3S and THREAD_BLOCK_6S in the AppFreeze log. After confirming that they are consistent, analyze the stack content.

    Evidence 1: key stack frames of the main thread stack

    Tid:17052, Name:pfreezeanalysis
    state=S, utime=203, stime=76, priority=-14, nice=-10, clk=100
    #00 pc 000000000019514c /system/lib/ld-musl-aarch64.so.1(ioctl+188)(9dcef83da65dee0250e2b6dbef45e66a)
    #01 pc 000000000000f030 /system/lib64/platformsdk/libipc_common.z.so(OHOS::BinderConnector::WriteBinder(unsigned long, void*)+120)(c3d820e943670c03802f3c6a6f6c02f7)
    #02 pc 0000000000071d64 /system/lib64/platformsdk/libipc_single.z.so(OHOS::BinderInvoker::TransactWithDriver(bool)+284)(725e65c2222991d0c1d55e2cf0cc2738)
    #03 pc 0000000000070764 /system/lib64/platformsdk/libipc_single.z.so(OHOS::BinderInvoker::WaitForCompletion(OHOS::MessageParcel*)+124)(725e65c2222991d0c1d55e2cf0cc2738)
    #04 pc 000000000006fc34 /system/lib64/platformsdk/libipc_single.z.so(OHOS::BinderInvoker::SendRequest(int, unsigned int, OHOS::MessageParcel&, OHOS::MessageParcel&, OHOS::MessageOption&)+620)(725e65c2222991d0c1d55e2cf0cc2738)
    #05 pc 0000000000048d14 /system/lib64/platformsdk/libipc_single.z.so(OHOS::IPCObjectProxy::SendRequestInner(bool, unsigned int, OHOS::MessageParcel&, OHOS::MessageParcel&, OHOS::MessageOption&)+272)(725e65c2222991d0c1d55e2cf0cc2738)
    #06 pc 000000000004972c /system/lib64/platformsdk/libipc_single.z.so(OHOS::IPCObjectProxy::SendRequest(unsigned int, OHOS::MessageParcel&, OHOS::MessageParcel&, OHOS::MessageOption&)+216)(725e65c2222991d0c1d55e2cf0cc2738)
    #07 pc 000000000003ee60 /system/lib64/libaudio_policy_client_idl_interface.z.so(OHOS::AudioStandard::AudioPolicyProxy::GetAudioScene(int&)+320)(16a5f13d157f85d0ce2574baf96e445a)
    #08 pc 00000000000896c8 /system/lib64/platformsdk/libaudio_client.z.so(OHOS::AudioStandard::AudioSystemManager::GetAudioScene() const+44)(fba17e35e3611dc6050105edd9d5bf7c)
    #09 pc 00000000000d3314 /system/lib64/module/multimedia/libaudio.z.so(OHOS::AudioStandard::NapiAudioManager::GetAudioSceneSync(napi_env__*, napi_callback_info__*) (.cfi)+228)(35129c60e8427727fa0ca4566d0f6524)
    #10 pc 0000000000066120 /system/lib64/platformsdk/libace_napi.z.so(panda::JSValueRef ArkNativeFunctionCallBack<true>(panda::JsiRuntimeCallInfo*)+288)(44ba8b408e7efd513bdc33ebeab8b671)
    #11 pc 0000000000e1537c /system/lib64/module/arkcompiler/stub.an(RTStub_PushCallArgsAndDispatchNative+40)
    #12 pc 0000000000466904 /system/lib64/module/arkcompiler/stub.an(BCStub_HandleCallthis0Imm8V8StwCopy+392)
    #13 at triggerBinderInterfaceBlockSync (entry|entry|1.0.0|src/main/ets/pages/page_second/page_third_appfreeze/appfreeze_threadblock.ts:216:43)

    The #13 stack frame of the stack indicates that the service function on the app side is triggerBinderInterfaceBlockSync, and the #09 stack frame indicates that the service calls the GetAudioSceneSync API provided by the system audio service. Because the system audio service does not respond in a timely manner, the main thread is blocked on the synchronous Binder communication. Because the peer process is the system audio service and does not belong to the service of the faulty app, you are advised to report the issue to the system side for optimization. If the peer process is the service of the faulty app, perform the following steps for further analysis.

  2. View the BinderCatcher information in the log and determine the process ID and IPC thread ID of the peer process based on to server_pid:server_tid.

    Evidence 2: BinderCatcher information

    17052:17052 to 772:0 code 1b wait:6.74904063 s frz_state:1,  ns:-1:-1 to -1:-1, debug:17052:17052 to 772:0, active_code:0, active_thread=0, pending_async_proc=0

    The BinderCatcher information indicates that the main thread has waited for the response from the peer process for more than 6s. The process ID of the peer process is 772, and the IPC thread ID is 0. This indicates that the Binder threads of the peer process are exhausted and no IPC thread can be allocated.

  3. Find the peer process information based on the process ID, and find the stacks of all IPC threads of the peer process. After the same stacks are aggregated, determine the root cause. Then, continue the analysis based on the analysis method for the corresponding root cause.

Prevention suggestions

  1. Move the synchronous Binder call to the child thread, or use an asynchronous API.
  2. If the peer process is the process of the faulty app, you are advised to increase the size of the Binder thread pool and optimize the response speed of the peer app.

[h2]Synchronous Time-Consuming I/O Operations

Description

The main thread of the app is blocked when performing a synchronous time-consuming I/O operation.

Problem analysis

  1. Find the app-side service that performs the I/O operation based on the stack.
  2. If there are system services between the app-side service stack frame and the top of the stack, you can further identify the specific I/O operation (such as database read/write).
  3. Analyze the HiLog log printing frequency. If more than 1000 lines are printed per second, you are advised to optimize the logs.

Keywords

Check whether the shared library and symbol at the top of the stack of the main thread match the following keywords:

Shared library:

ld-musl-aarch64.so.1

Symbol:

Case analysis

Case: The main thread is blocked due to time-consuming file read operations

Symptom

After a user triggers service code calling, the app does not respond. After a period of time, the app exits and an app freeze log file is generated.

Problem analysis

  1. Check the main thread stacks in THREAD_BLOCK_3S and THREAD_BLOCK_6S in the AppFreeze log. After confirming that they are consistent, analyze the stack content.

    Evidence: key stack frames of the main thread stack

    Tid:19502, Name:pfreezeanalysis
    state=R, utime=189, stime=635, priority=-14, nice=-10, clk=100
    #00 pc 00000000001ead94 /system/lib/ld-musl-aarch64.so.1(pread+76)(8c2ca4e308a1de9ed1e6b4ccc14c7443)
    #01 pc 00000000000121e0 /system/lib/libprocess.so(open+556)
    #02 pc 000000000001e108 /system/lib64/platformsdk/libuv.so(uv__fs_work.llvm.255697381303559595+204)(dd4ce8a088606e6444f0dceac8540868)
    #03 pc 0000000000020998 /system/lib64/platformsdk/libuv.so(uv_fs_open+176)(dd4ce8a088606e6444f0dceac8540868)
    #04 pc 0000000000109738 /system/lib64/module/file/libfs.z.so(OHOS::FileManagement::ModuleFileIO::OpenFileByPath(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>> const&, unsigned int)+148)(34d72fdf9d62dd4b9396c4c393f51d9f)
    #05 pc 0000000000107988 /system/lib64/module/file/libfs.z.so(OHOS::FileManagement::ModuleFileIO::Open::Sync(napi_env__*, napi_callback_info__*) (.cfi)+788)(34d72fdf9d62dd4b9396c4c393f51d9f)
    #06 pc 0000000000067c90 /system/lib64/platformsdk/libace_napi.z.so(panda::JSValueRef ArkNativeFunctionCallBack<true>(panda::JsiRuntimeCallInfo*)+288)(76bc1ed675edcc5f429a976d6c9b955d)
    #07 pc 0000000000e179f4 /system/lib64/module/arkcompiler/stub.an(RTStub_PushCallArgsAndDispatchNative+40)
    #08 pc 000000000046c00c /system/lib64/module/arkcompiler/stub.an(BCStub_HandleCallthis2Imm8V8V8V8StwCopy+432)
    #09 at write (entry|entry|1.0.0|src/main/ets/pages/page_second/page_third_appfreeze/appfreeze_threadblock_io.ts:182:31)
    #10 at anonymous (entry|entry|1.0.0|src/main/ets/pages/page_second/page_third_appfreeze/appfreeze_threadblock_io.ts:155:26)

    The stack top indicates that the main thread is blocked on the read operation. The #09 stack frame indicates that the I/O operation service is in the write function.

  2. Analyze the file operated in the write function and its size. Generally, the main thread is blocked during read and write operations because the file is too large.

Prevention suggestions

Move time-consuming I/O operations to the child thread to prevent the main thread from being blocked.

[h2]Long-Time GCs

Description

The main thread of the app triggers a long-time GC operation, causing a block.

Problem analysis

  1. Find the app-side service before GC based on the stack.
  2. Analyze the memory usage in the service based on the code.

Keywords

Check whether the shared library and symbol of the main thread stack match the following keywords:

Shared library:

libark_jsruntime.so

Symbol:

FullGC

Case analysis

Case: The main thread is blocked due to time-consuming Full GC

Symptom

After a user triggers service code calling, the app does not respond. After a period of time, the app exits and an app freeze log file is generated.

Problem analysis

  1. Check the main thread stacks in THREAD_BLOCK_3S and THREAD_BLOCK_6S in the AppFreeze log. After confirming that they are consistent, analyze the stack content.

    Evidence: key stack frames of the main thread stack

    Tid:22887, Name:pfreezeanalysis
    state=R, utime=628, stime=55, priority=-14, nice=-10, clk=100
    #00 pc 0000000000015890 /system/lib/libprocess.so(trap_handler+1232)
    #01 pc 00000000001edecc /system/lib/ld-musl-aarch64.so.1(signal_chain_handler+864)(8c2ca4e308a1de9ed1e6b4ccc14c7443)
    #02 pc 0000000000001d30 [shmm]
    #03 pc 0000000000001480 [shmm](__kernel_rt_sigreturn+0)
    #04 pc 000000000020e9a8 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::FullGCMarkRootVisitor::VisitRoot(panda::ecmascript::Root, panda::ecmascript::ObjectSlot)+548)(4025597d50430dc8ac5e5f0b44678df8)
    #05 pc 00000000008ec928 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::FullGCMarkRootVisitor::VisitRangeRoot(panda::ecmascript::Root, panda::ecmascript::ObjectSlot, panda::ecmascript::ObjectSlot)+64)(4025597d50430dc8ac5e5f0b44678df8)
    #06 pc 0000000000459134 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::EcmaVM::IterateSTWRoots(panda::ecmascript::RootVisitor&)+384)(4025597d50430dc8ac5e5f0b44678df8)
    #07 pc 0000000000458a6c /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::EcmaVM::Iterate(panda::ecmascript::RootVisitor&)+24)(4025597d50430dc8ac5e5f0b44678df8)
    #08 pc 00000000008ec000 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::FullGC::Mark()+248)(4025597d50430dc8ac5e5f0b44678df8)
    #09 pc 00000000008eb750 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::FullGC::RunPhases()+1660)(4025597d50430dc8ac5e5f0b44678df8)
    #10 pc 00000000001ef154 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::Heap::CollectGarbageImpl(panda::ecmascript::TriggerGCType, panda::ecmascript::GCReason)+1212)(4025597d50430dc8ac5e5f0b44678df8)
    #11 pc 00000000003854b0 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::Heap::CollectGarbage(panda::ecmascript::TriggerGCType, panda::ecmascript::GCReason)+136)(4025597d50430dc8ac5e5f0b44678df8)
    #12 pc 00000000003853b8 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::Heap::CheckAndTriggerOldGC(unsigned long)+312)(4025597d50430dc8ac5e5f0b44678df8)
    #13 pc 00000000001eae98 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::ObjectFactory::NewTaggedArray(unsigned int, panda::ecmascript::JSTaggedValue)+5012)(4025597d50430dc8ac5e5f0b44678df8)
    #14 pc 000000000026bc2c /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::JSObject::TryOptimizeAsFastElements(panda::ecmascript::JSThread const*, panda::ecmascript::JSHandle<panda::ecmascript::JSObject>)+168)(4025597d50430dc8ac5e5f0b44678df8)
    #15 pc 00000000004eeb9c /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::builtins::BuiltinsArray::Fill(panda::ecmascript::EcmaRuntimeCallInfo*)+1028)(4025597d50430dc8ac5e5f0b44678df8)
    #16 pc 0000000000e179f4 /system/lib64/module/arkcompiler/stub.an(RTStub_PushCallArgsAndDispatchNative+40)
    #17 pc 0000000000b25df8 /system/lib64/module/arkcompiler/stub.an(BuiltinStub_ArrayFillStwCopy+472)
    #18 pc 000000000046bce4 /system/lib64/module/arkcompiler/stub.an(BCStub_HandleCallthis1Imm8V8V8StwCopy+528)
    #19 at generateMemoryPressure (entry|entry|1.0.0|src/main/ets/pages/page_second/page_third_appfreeze/appfreeze_threadblock.ts:1865:56)

    The keyword FullGC indicates that the main thread is blocked on GC. The #19 stack frame indicates that the service before GC is generateMemoryPressure.

  2. Analyze the memory usage of the generateMemoryPressure service. Generally, large arrays or complex objects are frequently created, causing the memory pressure to increase. As a result, GC is triggered, leading to the blocking of the main thread.

Prevention suggestions

Reduce memory allocation and optimize the data structure to avoid frequent GC.

[h2]Long-Time Memory Snapshot Capturing

Description

The main thread OOM of the app triggers long-time memory snapshot capture, causing blocking.

Problem analysis

  1. Search for the OOM log corresponding to the app freeze issue.
  2. Analyze and solve the OOM issue.

Keywords

Check whether the shared library and symbol of the main thread stack match the following keywords:

Shared library:

libark_jsruntime.so

Symbol:

DumpHeapSnapshot

Case analysis

Case: The main thread is blocked due to long-time memory snapshot capture triggered by OOM

Symptom

After a user triggers service code calling, the app does not respond. After a period of time, the app exits and an app freeze log file is generated.

Problem analysis

  1. Check the main thread stacks in THREAD_BLOCK_3S and THREAD_BLOCK_6S in the AppFreeze log. After confirming that they are consistent, analyze the stack content.

    Evidence: key stack frames of the main thread stack

    Tid:12892, Name:pfreezeanalysis
    state=R, utime=648, stime=86, priority=-14, nice=-10, clk=100
    #00 pc 0000000000a45b30 /system/lib64/platformsdk/libark_jsruntime.so(4025597d50430dc8ac5e5f0b44678df8)
    #01 pc 0000000000a98880 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::EntryIdMap::FindId(unsigned long)+48)(4025597d50430dc8ac5e5f0b44678df8)
    #02 pc 0000000000aaa860 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::HeapSnapshot::GenerateNode(panda::ecmascript::JSTaggedValue, unsigned long, bool, bool, bool)+620)(4025597d50430dc8ac5e5f0b44678df8)
    #03 pc 0000000000aa2644 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::HeapSnapshot::FillEdges(bool)+852)(4025597d50430dc8ac5e5f0b44678df8)
    #04 pc 0000000000aa2138 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::HeapSnapshot::BuildUp(bool)+40)(4025597d50430dc8ac5e5f0b44678df8)
    #05 pc 0000000000a9aa30 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::HeapProfiler::MakeHeapSnapshot(panda::ecmascript::HeapProfiler::SampleType, panda::ecmascript::DumpSnapShotOption const&, bool)+360)(4025597d50430dc8ac5e5f0b44678df8)
    #06 pc 0000000000a9a1c8 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::HeapProfiler::DoDump(panda::ecmascript::Stream*, panda::ecmascript::Progress*, panda::ecmascript::DumpSnapShotOption const&)+100)(4025597d50430dc8ac5e5f0b44678df8)
    #07 pc 0000000000a9b630 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::HeapProfiler::DumpHeapSnapshot(panda::ecmascript::Stream*, panda::ecmascript::DumpSnapShotOption const&, panda::ecmascript::Progress*, std::__h::function<void (unsigned char)>)+972)(4025597d50430dc8ac5e5f0b44678df8)
    #08 pc 0000000000a88120 /system/lib64/platformsdk/libark_jsruntime.so(panda::DFXJSNApi::DumpHeapSnapshot(panda::ecmascript::EcmaVM const*, panda::ecmascript::Stream*, panda::ecmascript::DumpSnapShotOption const&, panda::ecmascript::Progress*, std::__h::function<void (unsigned char)>)+136)(4025597d50430dc8ac5e5f0b44678df8)
    #09 pc 0000000000a87fec /system/lib64/platformsdk/libark_jsruntime.so(panda::DFXJSNApi::DumpHeapSnapshot(panda::ecmascript::EcmaVM const*, std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>> const&, panda::ecmascript::DumpSnapShotOption const&, std::__h::function<void (unsigned char)> const&)+120)(4025597d50430dc8ac5e5f0b44678df8)
    #10 pc 000000000006d620 /system/lib64/platformsdk/libace_napi.z.so(ArkNativeEngine::DumpHeapSnapshot(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>> const&, bool, DumpFormat, bool, bool, bool, bool)+192)(76bc1ed675edcc5f429a976d6c9b955d)
    #11 pc 000000000000f4b4 /system/lib64/module/libhidebug.z.so(OHOS::HiviewDFX::DumpHeapSnapshot(std::__h::basic_string<char, std::__h::char_traits<char>, std::__h::allocator<char>> const&, napi_env__*, bool)+376)(91aaf2c7bd1d5fb2dfa77f48a3ffc31d)
    #12 pc 000000000000facc /system/lib64/module/libhidebug.z.so(OHOS::HiviewDFX::DumpJsHeapData(napi_env__*, napi_callback_info__*)+788)(91aaf2c7bd1d5fb2dfa77f48a3ffc31d)
    #13 pc 0000000000067c90 /system/lib64/platformsdk/libace_napi.z.so(panda::JSValueRef ArkNativeFunctionCallBack<true>(panda::JsiRuntimeCallInfo*)+288)(76bc1ed675edcc5f429a976d6c9b955d)
    #14 pc 0000000000e179f4 /system/lib64/module/arkcompiler/stub.an(RTStub_PushCallArgsAndDispatchNative+40)
    #15 pc 000000000046bc74 /system/lib64/module/arkcompiler/stub.an(BCStub_HandleCallthis1Imm8V8V8StwCopy+416)

    The DumpHeapSnapshot keyword in the stack indicates that the app is generating a heap snapshot. The snapshot generation takes a long time, causing the main thread to be blocked.

  2. This type of app freeze issue is caused by the time-consuming memory snapshot capture after an OOM issue occurs. You need to analyze the corresponding OOM issue.

Prevention suggestions

Optimize memory usage and management to prevent OOM caused by excessive memory usage.

[h2]Time-Consuming Operations

Description

The main thread of the app performs time-consuming operations, causing a block.

Problem analysis

  1. Find the time-consuming service on the app side based on the main thread stack.
  2. Analyze the cause of the time consumption in the service based on the code.

Keywords

Pay attention to the app-side service stack frame in the main thread stack.

Case analysis

Case: The main thread is blocked due to long-time service function execution

Symptom

After a user triggers service code calling, the app does not respond. After a period of time, the app exits and an app freeze log file is generated.

Problem analysis

  1. Check the main thread stacks in THREAD_BLOCK_3S and THREAD_BLOCK_6S in the AppFreeze log. After confirming that they are consistent, analyze the stack content.

    Evidence 1: key stack frames of the main thread stack

    Tid:23173, Name:pfreezeanalysis
    state=S, utime=61, stime=45, priority=-14, nice=-10, clk=100
    #00 pc 00000000001ebb00 /system/lib/ld-musl-aarch64.so.1(sleep+64)(8c2ca4e308a1de9ed1e6b4ccc14c7443)
    #01 pc 000000000007a638 /data/storage/el1/bundle/libs/arm64/libentry.so(TriggerLongTimeOp(napi_env__*, napi_callback_info__*)+44)(f9377ecb68078186ec50cb9740a6a4c259058746)
    #02 pc 0000000000067c90 /system/lib64/platformsdk/libace_napi.z.so(panda::JSValueRef ArkNativeFunctionCallBack<true>(panda::JsiRuntimeCallInfo*)+288)(76bc1ed675edcc5f429a976d6c9b955d)
    #03 pc 0000000000e179f4 /system/lib64/module/arkcompiler/stub.an(RTStub_PushCallArgsAndDispatchNative+40)
    #04 pc 000000000046b900 /system/lib64/module/arkcompiler/stub.an(BCStub_HandleCallthis0Imm8V8StwCopy+392)

    #01 stack frame indicates that the service party that performs time-consuming operations is libentry.so (TriggerLongTimeOp). Continue the analysis based on the code.

  2. In development and debugging scenarios, you can directly click the stack frame where libentry.so is located in DevEco Studio to go to the corresponding code line. In O&M scenarios, use llvm-addr2line to parse and find the corresponding code line.

    Evidence 2: code of the fault stack frame

    The while loop in the TriggerLongTimeOp method takes a long time, causing the main thread to be blocked.

Prevention suggestions

You can optimize the following aspects on the app side:

  1. Reduce the load of the main thread and move some services to asynchronous threads.
  2. Reduce the UI complexity, use lazy loading to load the UI, and release resources after the UI is invisible to users.

If the app is running in the background, the app freeze may be caused by insufficient system resources. In this case, you can ignore the app freeze fault.