成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

一種監(jiān)聽ANR方案

winterdawn / 1843人閱讀

摘要:監(jiān)聽上報(bào)應(yīng)用無響應(yīng)是數(shù)據(jù)采集系統(tǒng)功能之一,本文講述一種可行實(shí)現(xiàn)方案。是一個用于監(jiān)聽文件訪問創(chuàng)建修改刪除移動等操作的監(jiān)聽器。為此本文同時提供一種線程輪詢措施,用于輔助監(jiān)聽。

監(jiān)聽上報(bào)ANR(Application Not Responding,應(yīng)用無響應(yīng))是數(shù)據(jù)采集系統(tǒng)功能之一,本文講述一種可行實(shí)現(xiàn)方案。

方案概述

ANR一般有三種類型[1]

KeyDispatchTimeout(5 seconds) --主要類型按鍵或觸摸事件在特定時間內(nèi)無響應(yīng)

BroadcastTimeout(10 seconds) --BroadcastReceiver在特定時間內(nèi)無法處理完成

ServiceTimeout(20 seconds) --小概率類型 Service在特定的時間內(nèi)無法處理完成

當(dāng)應(yīng)用發(fā)生ANR,Android系統(tǒng)會將ANR Log輸出至/data/anr/traces.txt。traces.txt中l(wèi)og stack有固定格式(見以下示例代碼[2])。本文的方案就是監(jiān)聽traces.txt文件,對traces.txt內(nèi)容作解析,對解析結(jié)果進(jìn)行存儲和上報(bào);

----- pid 30307 at 2015-05-30 14:51:14 -----
Cmd line: com.example.androidyue.bitmapdemo

JNI: CheckJNI is off; workarounds are off; pins=0; globals=272

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

"main" prio=5 tid=1 TIMED_WAIT
  | group="main" sCount=1 dsCount=0 obj=0x416eaf18 self=0x416d8650
  | sysTid=30307 nice=0 sched=0/0 cgrp=apps handle=1074565528
  | state=S schedstat=( 0 0 0 ) utm=5 stm=4 core=3
  at java.lang.VMThread.sleep(Native Method)
  at java.lang.Thread.sleep(Thread.java:1044)
  at java.lang.Thread.sleep(Thread.java:1026)
  at com.example.androidyue.bitmapdemo.MainActivity$1.run(MainActivity.java:27)
  at android.app.Activity.runOnUiThread(Activity.java:4794)
  at com.example.androidyue.bitmapdemo.MainActivity.onResume(MainActivity.java:33)
  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1282)
  at android.app.Activity.performResume(Activity.java:5405)
方案實(shí)現(xiàn)

監(jiān)聽文件在Android中有android.os.FileObserver[3]實(shí)現(xiàn)方式。FileObserver是一個用于監(jiān)聽文件訪問、創(chuàng)建、修改、刪除、移動等操作的監(jiān)聽器。FileObserver是理想方案,但在測試過程中發(fā)現(xiàn)很多設(shè)備不支持FileObserver方式。為此本文同時提供一種線程輪詢措施,用于輔助ANR監(jiān)聽。以下列出幾個重要的實(shí)現(xiàn)類。

/**
 * A Observer to detect traces.txt CLOSE WRITE event
 *
 */
final class ANRFileObserver extends FileObserver {

    private IDetectCallback mIDetectCallback;


    public ANRFileObserver(String path, IDetectCallback callback) {
        super(path, FileObserver.CLOSE_WRITE);
        this.mIDetectCallback = callback;
    }

    @Override
    public void onEvent(int event, String path) {
        if (path == null) {
            return;
        }
        path = "/data/anr/" + path;
        if (path.contains("trace") && mIDetectCallback != null) {
            mIDetectCallback.onANR(path, false,System.currentTimeMillis());
        }
    }
}

/**
 *  loop traces.txt in 500MS
 *
 */
final class ANRLoopRunnable implements Runnable {
    
        private long lastModified;
    
        private IDetectCallback mIDetectCallback;
    
        private static final String ANR_PATH = "/data/anr/traces.txt";
    
        private int loop = 0;
    
        public ANRLoopRunnable(IDetectCallback callback) {
            mIDetectCallback = callback;
        }
    
        @Override
        public void run() {
    
            if (mIDetectCallback == null) {
                return;
            }
    
            if (!exists()) {
                mIDetectCallback.onFileNotExist();
                return;
            }
    
            if (!canRead()) {
                return;
            }
    
            lastModified = lastModified();
    
            while (!interrupted()) {
    
                long last = lastModified();
                if (last != lastModified) {
                    loop++;
                    trySleep(last);
                    continue;
                } else {
                    if (loop > 0) {
                        mIDetectCallback.onANR(ANR_PATH,true, System.currentTimeMillis());
                        break;
                    } else {
                        trySleep(last);
                        continue;
                    }
                }
            }
        }
    
        private void trySleep(long last) {
            lastModified = last;
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
        }
    
        /**
         * 獲取ANR文件最后一次更新時間
         *
         * @return
         */
        private long lastModified() {
            long time = 0L;
            File localFile = new File(ANR_PATH);
            if (localFile.exists()) {
                time = localFile.lastModified();
            }
            return time;
        }
    
        private boolean canRead() {
            boolean bool = false;
            File localFile = new File(ANR_PATH);
            if (localFile.exists()) {
                bool = localFile.canRead();
            }
            return bool;
        }
    
        private boolean exists() {
            File localFile = new File(ANR_PATH);
            return localFile.exists();
        }
    
    }
/**
 * An Item used to store traces info.`[4]` 
 */
public class TracesItem {
    
        /**
         * Constant for  output
         */
        public static final String PID = "PID";
        /**
         * Constant for  output
         */
        public static final String APP = "APP";
    
        /**
         * Constant for  output
         */
        public static final String STACK = "STACK";
    
        /**
         * Constant for  output
         */
        public static final String STATE = "STATE";
    
        private Map mAttributes = new HashMap();
    
        /**
         * Get the PID of the event.
         */
        public Integer getPid() {
            return (Integer) getAttribute(PID);
        }
    
        /**
         * Set the PID of the event.
         */
        public void setPid(Integer pid) {
            setAttribute(PID, pid);
        }
    
        /**
         * Get the reason of the event.
         */
        public String getState() {
            return (String) getAttribute(STATE);
        }
    
        /**
         * Set the reason of the event.
         */
        public void setState(String reason) {
            setAttribute(STATE, reason);
        }
    
        /**
         * Get the app or package name of the event.
         */
        public String getApp() {
            return (String) getAttribute(APP);
        }
    
        /**
         * Set the app or package name of the event.
         */
        public void setApp(String app) {
            setAttribute(APP, app);
        }
    
        /**
         * Get the stack for the crash.
         */
        public String getStack() {
            return (String) getAttribute(STACK);
        }
    
        /**
         * Set the stack for the crash.
         */
        public void setStack(String stack) {
            setAttribute(STACK, stack);
        }
    
        /**
         * Set an attribute to a value.
         *
         * @param attribute The name of the attribute.
         * @param value     The value.
         * @throws IllegalArgumentException If the attribute is not in allowedAttributes.
         */
        protected void setAttribute(String attribute, Object value) throws IllegalArgumentException {
            mAttributes.put(attribute, value);
        }
    
        /**
         * Get the value of an attribute.
         *
         * @param attribute The name of the attribute.
         * @return The value or null if the attribute has not been set.
         * @throws IllegalArgumentException If the attribute is not in allowedAttributes.
         */
        protected Object getAttribute(String attribute) throws IllegalArgumentException {
            return mAttributes.get(attribute);
        }
    }

/**
 * A  Parser to parse Android traces files. `[4]`
 */
public class TracesParser {
        
            /**
             * Matches: ----- pid PID at YYYY-MM-DD hh:mm:ss -----
             */
            private static final Pattern PID = Pattern.compile(
                    "^----- pid (d+) at d{4}-d{2}-d{2} d{2}:d{2}:d{2} -----$");
        
            /**
             * Matches: Cmd line: APP
             */
            private static final Pattern APP = Pattern.compile("^Cmd ?line: (S+).*$");
        
            /**
             * Matches: "main" prio=5 tid=1 STATE
             */
            private static final Pattern STACK = Pattern.compile("^"main" (prio=d+) (tid=d+) (.*)");
        
            /**
             * parse traces.txt
             *
             * @param path traces.txt
             * @return
             */
            public static TracesItem parse(String path) {
        
                BufferedReader br = null;
                FileReader fr = null;
                TracesItem traces = new TracesItem();
                StringBuffer stack = null;
                String state = null;
                try {
                    fr = new FileReader(path);
                    br = new BufferedReader(fr);
                    String sCurrentLine;
                    br = new BufferedReader(new FileReader(path));
        
                    while ((sCurrentLine = br.readLine()) != null) {
                        if (state == null) {
                            Matcher m = PID.matcher(sCurrentLine);
                            if (m.matches()) {
                                traces.setPid(Integer.parseInt(m.group(1)));
                            }
                            m = APP.matcher(sCurrentLine);
                            if (m.matches()) {
                                traces.setApp(m.group(1));
                            }
                            m = STACK.matcher(sCurrentLine);
                            if (m.matches()) {
                                state = m.group(3);
                                traces.setState(state);
                            }
                        } else if (!"".equals(sCurrentLine)) {
                            if (stack == null) {
                                stack = new StringBuffer();
                            }
                            stack.append(sCurrentLine);
                            stack.append("
");
                        } else {
                            traces.setStack(stack.toString().trim());
                            return traces;
                        }
                    }
        
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (br != null)
                            br.close();
                        if (fr != null)
                            fr.close();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
        
                if (stack == null) {
                    return null;
                }
                traces.setStack(stack.toString().trim());
                return traces;
            }
        
        }
        
    
引用:

[1] ANR簡介以及解決方案 : http://www.snowdream.tech/201...
[2] 說說Android中的ANR:http://droidyue.com/blog/2015...
[3] FileObserver類:https://developer.android.com...
[4] loganalysis:https://android.googlesource....

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/70236.html

相關(guān)文章

  • Android優(yōu)化總結(jié)

    摘要:錯誤使用單利在開發(fā)中單例經(jīng)常需要持有對象,如果持有的對象生命周期與單例生命周期更短時,或?qū)е聼o法被釋放回收,則有可能造成內(nèi)存泄漏。如果集合是類型的話,那內(nèi)存泄漏情況就會更為嚴(yán)重。 目錄介紹 1.OOM和崩潰優(yōu)化 1.1 OOM優(yōu)化 1.2 ANR優(yōu)化 1.3 Crash優(yōu)化 2.內(nèi)存泄漏優(yōu)化 2.0 動畫資源未釋放 2.1 錯誤使用單利 2.2 錯誤使用靜態(tài)變量 2.3 ...

    sunsmell 評論0 收藏0
  • ANR詳細(xì)介紹

    摘要:使用或者時,調(diào)用設(shè)置優(yōu)先級,否則仍然會降低程序響應(yīng),因?yàn)槟J(rèn)的優(yōu)先級和主線程相同。使用處理工作線程結(jié)果,而不是使用或者來阻塞主線程。目錄介紹 1.ANR簡單介紹 2.ANR發(fā)生場景 3.ANR發(fā)生的原理 4.ANR有哪些具體案例 5.ANR具體如何分析 6.解決方案 7.ANR問題解答 好消息 博客筆記大匯總【16年3月到至今】,包括Java基礎(chǔ)及深入知識點(diǎn),Android技術(shù)博客,P...

    番茄西紅柿 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<