某些设备上的ANR内部功能

时间:2014-04-22 17:08:21

标签: android surfaceview

我有一个使用surfaceview来显示UI的应用。该应用程序对大约18,000个用户运行稳定,但有3个设备在返回到surfaceview活动时获得ANR(sv活动 - >常规活动 - >返回到sv活动)。

这3个设备是:

  • 昂达平板电脑(Allwinner A31芯片组)
  • Sero 8(Rockchip芯片组)
  • Acer 10"使用Intel Atom的平板电脑

我试图重新创建ANR但失败了。根据我的用户,除了上面列出的设备外,该应用程序运行良好数小时,没有任何问题。

来自Android 4.2的ANR Stacktrace:

DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0)

"main" prio=5 tid=1 WAIT
| group="main" sCount=1 dsCount=0 obj=0x41c899a0 self=0x41a6c010
| sysTid=5497 nice=0 sched=0/0 cgrp=apps handle=1074877404
| state=S schedstat=( 0 0 0 ) utm=541 stm=129 core=2
at java.lang.Object.wait(Native Method)
- waiting on <0x41c89da0> (a java.lang.VMThread) held by tid=1 (main)
at java.lang.Thread.parkFor(Thread.java:1231)
at sun.misc.Unsafe.park(Unsafe.java:323)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:159)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:810)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:843)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1173)
at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:183)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:259)
at android.view.SurfaceView.updateWindow(SurfaceView.java:597)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:329)
at android.view.View.dispatchWindowVisibilityChanged(View.java:7544)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1224)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1002)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4400)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)

"SurfaceDraw" prio=5 tid=15 SUSPENDED
| group="main" sCount=1 dsCount=0 obj=0x42867d30 self=0x69a65b38
| sysTid=12101 nice=0 sched=0/0 cgrp=apps handle=1753253360
| state=S schedstat=( 0 0 0 ) utm=7914 stm=23 core=0
at android.graphics.Canvas.native_drawARGB(Native Method)
at android.graphics.Canvas.drawARGB(Canvas.java:801)
at com.davidgiga1993.mixingstationlibrary.surface.BaseSurface.b(BaseSurface.java:167)
at com.davidgiga1993.mixingstationlibrary.surface.k.run(DrawThread.java:27)

"AsyncTask #3" prio=5 tid=14 WAIT
| group="main" sCount=1 dsCount=0 obj=0x421734d0 self=0x690d59d8
| sysTid=5820 nice=0 sched=0/0 cgrp=apps handle=1762495280
| state=S schedstat=( 0 0 0 ) utm=0 stm=0 core=0
at java.lang.Object.wait(Native Method)
- waiting on <0x421735f0> (a java.lang.VMThread) held by tid=14 (AsyncTask #3)
at java.lang.Thread.parkFor(Thread.java:1231)
at sun.misc.Unsafe.park(Unsafe.java:323)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:159)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2019)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:413)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1013)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1073)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)

"Binder_3" prio=5 tid=13 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x4215a380 self=0x684f9540
| sysTid=5691 nice=0 sched=0/0 cgrp=apps handle=1693280728
| state=S schedstat=( 0 0 0 ) utm=3 stm=0 core=3
#00 pc 00016fe4 /system/lib/libc.so (__ioctl+8)
#01 pc 0002a75d /system/lib/libc.so (ioctl+16)
#02 pc 00016ba1 /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+132)
#03 pc 00017363 /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+154)
#04 pc 0001b15d /system/lib/libbinder.so
#05 pc 00011267 /system/lib/libutils.so (android::Thread::_threadLoop(void*)+114)
#06 pc 00046887 /system/lib/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell(void*)+66)
#07 pc 00010dcd /system/lib/libutils.so
#08 pc 0000e3d8 /system/lib/libc.so (__thread_entry+72)
#09 pc 0000dac4 /system/lib/libc.so (pthread_create+160)
at dalvik.system.NativeStart.run(Native Method)

"AsyncTask #2" prio=5 tid=12 WAIT
| group="main" sCount=1 dsCount=0 obj=0x42145450 self=0x64f1aac8
| sysTid=5525 nice=0 sched=0/0 cgrp=apps handle=1693560600
| state=S schedstat=( 0 0 0 ) utm=0 stm=0 core=3
at java.lang.Object.wait(Native Method)
- waiting on <0x421455c8> (a java.lang.VMThread) held by tid=12 (AsyncTask #2)
at java.lang.Thread.parkFor(Thread.java:1231)
at sun.misc.Unsafe.park(Unsafe.java:323)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:159)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2019)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:413)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1013)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1073)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)

"AsyncTask #1" prio=5 tid=11 WAIT
| group="main" sCount=1 dsCount=0 obj=0x42141d40 self=0x64ef35e8
| sysTid=5524 nice=0 sched=0/0 cgrp=apps handle=1693143224
| state=S schedstat=( 0 0 0 ) utm=0 stm=0 core=2
at java.lang.Object.wait(Native Method)
- waiting on <0x42141ed8> (a java.lang.VMThread) held by tid=11 (AsyncTask #1)
at java.lang.Thread.parkFor(Thread.java:1231)
at sun.misc.Unsafe.park(Unsafe.java:323)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:159)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2019)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:413)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1013)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1073)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)

根据我的解释,ANR在调用surfaceCreated之前发生。

这是surfaceview和绘图线程的源代码:

public class BaseSurface extends SurfaceView implements SurfaceHolder.Callback
{
    protected SurfaceHolder holder;
    private DrawThread drawThread;

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    {
        Log.d("Surface", "Changed");
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder)
    {
        synchronized (holder)
        {
            this.holder = holder;

            if (drawThread != null)
            {
                drawThread.Active = false;
                try
                {
                    drawThread.join();
                }
                catch (InterruptedException e)
                {
                }
            }

            drawThread = new DrawThread(this);
            drawThread.Active = true;
            drawThread.start();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        synchronized (holder)
        {

            drawThread.Active = false;
            boolean retry = true;
            while (retry)
            {
                try
                {
                    drawThread.join();
                    retry = false;
                }
                catch (InterruptedException e)
                {
                }
            }

            drawThread = null;
            this.holder = null;
        }
    }

    public void Update()
    {
        if (holder == null)
            return;

        Canvas canvas = holder.lockCanvas();
        if (canvas != null)
        {
            synchronized (holder)
            {
                //drawing the ui...
                holder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

public class DrawThread extends Thread
{
    public boolean Active = false;
    private BaseSurface surface;

    private long frameStartTime;
    public float FPS = 38f;// = 26fps; 27f = 37fps
    private int sleepTime;

    public DrawThread(BaseSurface surface)
    {
        super("SurfaceDraw");
        this.surface = surface;
    }

    @Override
    public void run()
    {
        while (Active)
        {
            frameStartTime = SystemClock.uptimeMillis();
            surface.Update();
            try
            {
                sleepTime = (int) (FPS - (SystemClock.uptimeMillis() - frameStartTime));
                if (sleepTime > 0 && sleepTime < 1000)
                {
                    Thread.sleep(sleepTime);
                }
            }
            catch (InterruptedException ex)
            {
                Log.d("DrawThread", "Interrupred");
            }
        }
        Log.d("DrawThread", "Finished");
    }
}

我在最后几天已经搜索了很多但是没有找到任何线索,为什么会发生这种情况。 我发现的唯一类似问题是:https://groups.google.com/forum/#!msg/android-developers/0VuqnrYe7b0/Yw1mHodmrwoJ但他没有发布任何解决方案,他的问题与特定设备无关。

在使用某些特定设备之前是否有其他人遇到过这些问题,并且知道这个问题的解决方案?

修改

我找到了一种重现ANR(随机)的方法。 当表面活动关闭时,导致ANR发生的真正问题。 这是一个&#34; good&#34;的堆栈跟踪。关闭和'#34;坏&#34;接近:

04-24 14:54:10.798: D/DrawThread(1526): Finished
04-24 14:54:10.798: D/Surface(1526): surfaceDestroyed

04-24 14:54:16.851: D/DrawThread(1526): Finished
04-24 14:54:16.851: D/Surface(1526): surfaceDestroyed
04-24 14:54:16.860: E/SurfaceHolder(1526): Exception locking surface
04-24 14:54:16.860: E/SurfaceHolder(1526): java.lang.IllegalArgumentException
04-24 14:54:16.860: E/SurfaceHolder(1526):  at android.view.Surface.lockCanvasNative(Native Method)
04-24 14:54:16.860: E/SurfaceHolder(1526):  at android.view.Surface.lockCanvas(Surface.java:76)
04-24 14:54:16.860: E/SurfaceHolder(1526):  at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:744)
04-24 14:54:16.860: E/SurfaceHolder(1526):  at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:720)
04-24 14:54:16.860: E/SurfaceHolder(1526):  at com.davidgiga1993.mixingstationlibrary.surface.BaseSurface.Update(BaseSurface.java:169)
04-24 14:54:16.860: E/SurfaceHolder(1526):  at com.davidgiga1993.mixingstationlibrary.surface.DrawThread.run(DrawThread.java:27)
04-24 14:54:16.860: D/Surface(1526): surfaceCreated

为什么要调用异常?在表面被破坏之前停止绘制线程,并且不再有任何东西接触到surfaceview。另外,为什么在该异常之后调用表面的surfaceCreated? 此时活动甚至不再可见。

我也尝试删除所有已同步的块,但它们没有改变行为。

1 个答案:

答案 0 :(得分:1)

查看android-developers链接中的ANR跟踪,他们正在运行Android 4.2,当SurfaceView尝试锁定其Surface时,他们的主线程停滞不前。我认为问题在于渲染线程调用了lockCanvas(),它锁定了Surface(使用ReentrantLock),然后发生了导致SurfaceView需要更新的事情(例如它的大小或位置发生了变化)。您可以在跟踪中看到(可能)称为lockCanvas()的线程正在运行(&#34;线程-3899&#34;在NATIVE中带有state=R)复杂的Skia代码。因此,要么Skia代码永远循环,要么花费很长时间才能完成。

在您的情况下,渲染线程(SurfaceDraw)被暂停,可能是因为它完成了它正在做的事情并且从本机代码返回到VM。你的这是一个简单的drawARGB()电话,所以我不确定为什么这么长时间。它可能会让其它东西停滞不前,而这恰好是ANR快照机制最终陷入困境的时候。

在调用lockCanvas()之前抓住SurfaceHolder 上的锁定可能是明智之举,以确保您不会在持有Canvas锁的情况下阻止等待它。

(FWIW,在SurfaceHolder实例上同步让我有点紧张,因为你不知道SurfaceView中的某些东西是否会为了它自己的恶意目的而锁定它。不要认为&#39;这是问题所在。)