GC suspend abort

问题现象

  • 复现步骤
    • 手机(Android 7.0)
    • Monkey测试,Camera2 app偶现native crash

定位分析

  • 相关log
    • tombstone
      Revision: '0'
      ABI: 'arm' 
      pid: 20403, tid: 20414, name: HeapTaskDaemon  >>> com.android.camera2 <<<
      signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
      Abort message: 'art/runtime/thread_list.cc:621] Check failed: cur_val == 0 (cur_val=1, 0=0) '
      r0 00000000  r1 00004fbe  r2 00000006  r3 00000008
      r4 a0c7f978  r5 00000006  r6 a0c7f920  r7 0000010c
      r8 a7085900  r9 a6fae8c7  sl a0c7f2ec  fp 00000001
      ip 0000000b  sp a0c7f1f0  lr a7224597  pc a7226df4  cpsr 20010010
      d0  0000000000000000  d1  0000000000000000
      d2  0000000000000000  d3  0000000000000000
      d4  0000000000000000  d5  0000000000000000
      d6  0000000000000000  d7  0000000000000000
      d8  0000000000000000  d9  0000000000000000
      d10 0000000000000000  d11 0000000000000000
      d12 0000000000000000  d13 0000000000000000
      d14 0000000000000000  d15 0000000000000000
      d16 2e74736973726570  d17 2e6761742e676f6c
      d18 a72360a3a723609b  d19 a72360afa72360ab
      d20 656d72612c613776  d21 0065006d00720061
      d22 656d72612c613776  d23 0000000000000000
      d24 0000003f0000003f  d25 0000000000000000
      d26 0000000000000000  d27 0000000000000000
      d28 0000000000000000  d29 0000000000000000
      d30 0000000000000000  d31 00000000a70e7940
      scr 80000093
      
      backtrace:
      #00 pc 00049df4  /system/lib/libc.so (tgkill+12)
      #01 pc 00047593  /system/lib/libc.so (pthread_kill+34)
      #02 pc 0001d855  /system/lib/libc.so (raise+10)
      #03 pc 000193a1  /system/lib/libc.so (__libc_android_abort+34)
      #04 pc 00017014  /system/lib/libc.so (abort+4)
      #05 pc 003188f5  /system/lib/libart.so (_ZN3art7Runtime5AbortEv+252)
      #06 pc 000b4e79  /system/lib/libart.so (_ZN3art10LogMessageD2Ev+864)
      #07 pc 0033c0cd  /system/lib/libart.so (_ZN3art10ThreadList18SuspendAllInternalEPNS_6ThreadES2_S2_b+1400)
      #08 pc 0033c2cf  /system/lib/libart.so (_ZN3art10ThreadList10SuspendAllEPKcb+374)
      #09 pc 0016ebc5  /system/lib/libart.so (_ZN3art2gc9collector16GarbageCollector11ScopedPauseC2EPS2_+32)
      #10 pc 0017385d  /system/lib/libart.so (_ZN3art2gc9collector9MarkSweep9RunPhasesEv+160)
      #11 pc 0016e5d5  /system/lib/libart.so (_ZN3art2gc9collector16GarbageCollector3RunENS0_7GcCauseEb+248)
      #12 pc 00191aed  /system/lib/libart.so (_ZN3art2gc4Heap22CollectGarbageInternalENS0_9collector6GcTypeENS0_7GcCauseEb+2364)
      #13 pc 001973cd  /system/lib/libart.so (_ZN3art2gc4Heap12ConcurrentGCEPNS_6ThreadEb+68)
      #14 pc 0019bf03  /system/lib/libart.so (_ZN3art2gc4Heap16ConcurrentGCTask3RunEPNS_6ThreadE+18)
      #15 pc 001b42c3  /system/lib/libart.so (_ZN3art2gc13TaskProcessor11RunAllTasksEPNS_6ThreadE+30)
      #16 pc 7413f047  /data/dalvik-cache/arm/system@framework@boot.oat (offset 0x2845000)
      

初步分析

从堆栈来看,GC时去做suspend ,但suspend过程中检测到异常并主动abort.

01-02 00:04:16.124 20403 20414 F art     : art/runtime/thread_list.cc:621] Check failed: cur_val == 0 (cur_val=1, 0=0)


01-02 00:04:16.765 20403 20414 F art     : art/runtime/runtime.cc:404] Runtime aborting...
01-02 00:04:16.765 20403 20414 F art     : art/runtime/runtime.cc:404] Aborting thread:
01-02 00:04:16.765 20403 20414 F art     : art/runtime/runtime.cc:404] "HeapTaskDaemon" prio=5 tid=8 WaitingPerformingGc
01-02 00:04:16.765 20403 20414 F art     : art/runtime/runtime.cc:404]   | group="" sCount=0 dsCount=0 obj=0x12c3e4c0 self=0xa7085900
01-02 00:04:16.765 20403 20414 F art     : art/runtime/runtime.cc:404]   | sysTid=20414 nice=0 cgrp=default sched=0/0 handle=0xa0c7f920
01-02 00:04:16.766 20403 20414 F art     : art/runtime/runtime.cc:404]   | state=R schedstat=( 0 0 0 ) utm=16 stm=2 core=2 HZ=100
01-02 00:04:16.766 20403 20414 F art     : art/runtime/runtime.cc:404]   | stack=0xa0b7d000-0xa0b7f000 stackSize=1038KB
01-02 00:04:16.766 20403 20414 F art     : art/runtime/runtime.cc:404]   | held mutexes= "abort lock"
01-02 00:04:16.766 20403 20414 F art     : art/runtime/runtime.cc:404]   native: #00 pc 0034e109  /system/lib/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+128)
01-02 00:04:16.766 20403 20414 F art     : art/runtime/runtime.cc:404]   native: #01 pc 0032ea05  /system/lib/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMap+308)
01-02 00:04:16.766 20403 20414 F art     : art/runtime/runtime.cc:404]   native: #02 pc 00321ba5  /system/lib/libart.so (_ZNK3art10AbortState10DumpThreadERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEPNS_6ThreadE+24)
01-02 00:04:16.766 20403 20414 F art     : art/runtime/runtime.cc:404]   native: #03 pc 00321a2d  /system/lib/libart.so (_ZNK3art10AbortState4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+424)

深入分析

  • 关键code如下

    ... ...
    while (true) {
      int32_t cur_val = pending_threads.LoadRelaxed();
      if (LIKELY(cur_val > 0)) {
    #if ART_USE_FUTEXES
        if (futex(pending_threads.Address(), FUTEX_WAIT, cur_val, &wait_timeout, nullptr, 0) != 0) {
          // EAGAIN and EINTR both indicate a spurious failure, try again from the beginning.
          if ((errno != EAGAIN) && (errno != EINTR)) {
            if (errno == ETIMEDOUT) {
              LOG(kIsDebugBuild ? FATAL : ERROR) << "Unexpected time out during suspend all.";
            } else {
              PLOG(FATAL) << "futex wait failed for SuspendAllInternal()";
            }   
          }   
        } else {
          cur_val = pending_threads.LoadRelaxed();
          CHECK_EQ(cur_val, 0);              ==>check failed
          break;
        }   
    #else
        // Spin wait. This is likely to be slow, but on most architecture ART_USE_FUTEXES is set.
    #endif
      } else {
        CHECK_EQ(cur_val, 0); 
        break;
      }   
    }
    ... ...
    
    

    CHECK_EQ(cur_val, 0);失败的原因在于当前获取的cur_val为1,表示当前仍有一个线程在运行,并未挂起,但Google原生逻辑认为此时所有的线程已suspend.

  • 查看futex()用法如下

    int futex(int *uaddr, int op, int val, const struct timespec *timeout, int *uaddr2, int val3);
    
    FUTEX_WAIT
          This  operation  atomically  verifies that the futex address uaddr still contains the value val, and sleeps awaiting FUTEX_WAKE on this futex address.
          If the timeout argument is non-NULL, its contents specify the duration of the wait.  (This interval will be rounded up to the system  clock  granular‐
          ity,  and  kernel scheduling delays mean that the blocking interval may overrun by a small amount.) If timeout is NULL, the call blocks indefinitely.
          The arguments uaddr2 and val3 are ignored.
    

    可看到,futex()正常返回0的时候表示uaddr监测的内存内容发生变化,但变化并不等同于val变为一个特定的值(如0).

  • 另外,GC suspend慢的原因在于某些thread做了耗时操作,但目前log和debug 信息已无法确认哪些threads可能存在问题(线程均已suspend).

    Thread[tid = 20403, name = main                                    ]: flag = 1, state = 90
    Thread[tid = 20408, name = Jit thread pool worker thread 0         ]: flag = 1, state = 89
    Thread[tid = 20409, name = Signal Catcher                          ]: flag = 1, state = 81
    Thread[tid = 20410, name = JDWP                                    ]: flag = 1, state = 77
    Thread[tid = 20411, name = ReferenceQueueDaemon                    ]: flag = 1, state = 71
    Thread[tid = 20412, name = FinalizerDaemon                         ]: flag = 1, state = 71
    Thread[tid = 20413, name = FinalizerWatchdogDaemon                 ]: flag = 1, state = 69
    Thread[tid = 20414, name = HeapTaskDaemon                          ]: flag = 0, state = 74
    Thread[tid = 20415, name = Binder:20403_1                          ]: flag = 1, state = 89
    Thread[tid = 20416, name = Binder:20403_2                          ]: flag = 1, state = 89
    Thread[tid = 20417, name = Profile Saver                           ]: flag = 1, state = 89
    Thread[tid = 20425, name = pool-2-thread-1                         ]: flag = 1, state = 71
    Thread[tid = 20427, name = OnCreateAsync Task                      ]: flag = 1, state = 89
    Thread[tid = 20428, name = CameraRequest Task                      ]: flag = 1, state = 89
    Thread[tid = 20429, name = pool-2-thread-2                         ]: flag = 1, state = 71
    Thread[tid = 20430, name = pool-2-thread-3                         ]: flag = 1, state = 71
    Thread[tid = 20431, name = pool-2-thread-4                         ]: flag = 1, state = 71
    Thread[tid = 20432, name = pool-2-thread-5                         ]: flag = 1, state = 71
    Thread[tid = 20433, name = Camera Handler Thread                   ]: flag = 1, state = 89
    Thread[tid = 20435, name = Camera2 Handler Thread                  ]: flag = 1, state = 89
    Thread[tid = 20434, name = Camera Job Dispatch Thread              ]: flag = 1, state = 71
    Thread[tid = 20436, name = Camera Job Dispatch Thread              ]: flag = 1, state = 71
    Thread[tid = 20437, name = pool-5-thread-1                         ]: flag = 1, state = 71
    Thread[tid = 20438, name = AsyncTask #1                            ]: flag = 1, state = 68
    Thread[tid = 20442, name = dreamcamera                             ]: flag = 1, state = 89
    Thread[tid = 20491, name = RenderThread                            ]: flag = 1, state = 89
    Thread[tid = 20496, name = AsyncTask #2                            ]: flag = 1, state = 90
    Thread[tid = 20497, name = pool-3-thread-1                         ]: flag = 1, state = 89
    Thread[tid = 20499, name = <native thread without managed peer>    ]: flag = 1, state = 89
    

Root Cause

  • futex()系统调用正常返回后,当前监控的内存内容不一定为减少为0, 判断逻辑有误.移除后可再次循环判断是否所有线程已完成suspend.

解决方案

  • patch

    • Google已于去年AOSP提交关于此bug的修复patch
    • Android 7.x均未包含该patch,cherry pick过来即可
    • Android 8.0已包含此patch
  • 验证

    编译通过,手机能正常启动和运行.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,482评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,377评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,762评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,273评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,289评论 5 373
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,046评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,351评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,988评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,476评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,948评论 2 324
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,064评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,712评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,261评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,264评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,486评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,511评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,802评论 2 345

推荐阅读更多精彩内容