summaryrefslogtreecommitdiff
path: root/src/com/android/loganalysis/parser/AnrParser.java
blob: f2317289b4495194f0b045e40dfef797a55ecad2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.loganalysis.parser;

import com.android.loganalysis.item.AnrItem;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * An {@link IParser} to handle ANRs.
 */
public class AnrParser implements IParser {
    /**
     * Matches: ANR (application not responding) in process: app
     * Matches: ANR in app
     * Matches: ANR in app (class/package)
     */
    public static final Pattern START = Pattern.compile(
            "^ANR (?:\\(application not responding\\) )?in (?:process: )?(\\S+).*$");
    /**
     * Matches: PID: 1234
     */
    private static final Pattern PID = Pattern.compile("^PID: (\\d+)$");
    /**
     * Matches: Reason: reason
     */
    private static final Pattern REASON = Pattern.compile("^Reason: (.*)$");
    /**
     * Matches: Load: 0.71 / 0.83 / 0.51
     */
    private static final Pattern LOAD = Pattern.compile(
            "^Load: (\\d+\\.\\d+) / (\\d+\\.\\d+) / (\\d+\\.\\d+)$");

    /**
     * Matches: 33% TOTAL: 21% user + 11% kernel + 0.3% iowait
     */
    private static final Pattern TOTAL = Pattern.compile("^(\\d+(\\.\\d+)?)% TOTAL: .*$");
    private static final Pattern USER = Pattern.compile("^.* (\\d+(\\.\\d+)?)% user.*$");
    private static final Pattern KERNEL = Pattern.compile("^.* (\\d+(\\.\\d+)?)% kernel.*$");
    private static final Pattern IOWAIT = Pattern.compile("^.* (\\d+(\\.\\d+)?)% iowait.*$");

    /**
     * {@inheritDoc}
     *
     * @return The {@link AnrItem}.
     */
    @Override
    public AnrItem parse(List<String> lines) {
        AnrItem anr = null;
        StringBuilder stack = new StringBuilder();
        boolean matchedTotal = false;

        for (String line : lines) {
            Matcher m = START.matcher(line);
            // Ignore all input until the start pattern is matched.
            if (m.matches()) {
                anr = new AnrItem();
                anr.setApp(m.group(1));
            }

            if (anr != null) {
                m = PID.matcher(line);
                if (m.matches()) {
                    anr.setPid(Integer.valueOf(m.group(1)));
                }
                m = REASON.matcher(line);
                if (m.matches()) {
                    anr.setReason(m.group(1));
                }

                m = LOAD.matcher(line);
                if (m.matches()) {
                    anr.setLoad(AnrItem.LoadCategory.LOAD_1, Double.parseDouble(m.group(1)));
                    anr.setLoad(AnrItem.LoadCategory.LOAD_5, Double.parseDouble(m.group(2)));
                    anr.setLoad(AnrItem.LoadCategory.LOAD_15, Double.parseDouble(m.group(3)));
                }

                m = TOTAL.matcher(line);
                if (!matchedTotal && m.matches()) {
                    matchedTotal = true;
                    anr.setCpuUsage(AnrItem.CpuUsageCategory.TOTAL, Double.parseDouble(m.group(1)));

                    m = USER.matcher(line);
                    Double usage = m.matches() ? Double.parseDouble(m.group(1)) : 0.0;
                    anr.setCpuUsage(AnrItem.CpuUsageCategory.USER, usage);

                    m = KERNEL.matcher(line);
                    usage = m.matches() ? Double.parseDouble(m.group(1)) : 0.0;
                    anr.setCpuUsage(AnrItem.CpuUsageCategory.KERNEL, usage);

                    m = IOWAIT.matcher(line);
                    usage = m.matches() ? Double.parseDouble(m.group(1)) : 0.0;
                    anr.setCpuUsage(AnrItem.CpuUsageCategory.IOWAIT, usage);
                }

                stack.append(line);
                stack.append("\n");
            }
        }

        if (anr != null) {
            anr.setStack(stack.toString().trim());
        }
        return anr;
    }
}