summaryrefslogtreecommitdiff
path: root/src/com/android/loganalysis/parser/MemoryHealthParser.java
blob: fd44662c983857a4a3fb8b89705aa67391fb7203 (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
/*
 * Copyright (C) 2015 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.MemoryHealthItem;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Parses the memory health file generated by the tests.
 */
public class MemoryHealthParser implements IParser {
    private Map<String, Map<String, Long>> mForeground;
    private Map<String, Map<String, Long>> mBackground;
    private static final Map<String, String> SECTION_MAPPINGS;

    static {
        Map<String, String> mappings = new HashMap<String, String>();
        mappings.put("Average Dalvik Heap", MemoryHealthItem.DALVIK_AVG);
        mappings.put("Average Native Heap", MemoryHealthItem.NATIVE_AVG);
        mappings.put("Average PSS", MemoryHealthItem.PSS_AVG);
        mappings.put("Peak Dalvik Heap", MemoryHealthItem.DALVIK_PEAK);
        mappings.put("Peak Native Heap", MemoryHealthItem.NATIVE_PEAK);
        mappings.put("Peak PSS", MemoryHealthItem.PSS_PEAK);

        mappings.put("Average Summary Java Heap", MemoryHealthItem.SUMMARY_JAVA_HEAP_AVG);
        mappings.put("Average Summary Native Heap", MemoryHealthItem.SUMMARY_NATIVE_HEAP_AVG);
        mappings.put("Average Summary Code", MemoryHealthItem.SUMMARY_CODE_AVG);
        mappings.put("Average Summary Stack", MemoryHealthItem.SUMMARY_STACK_AVG);
        mappings.put("Average Summary Graphics", MemoryHealthItem.SUMMARY_GRAPHICS_AVG);
        mappings.put("Average Summary Other", MemoryHealthItem.SUMMARY_OTHER_AVG);
        mappings.put("Average Summary System", MemoryHealthItem.SUMMARY_SYSTEM_AVG);
        mappings.put("Average Summary Overall Pss", MemoryHealthItem.SUMMARY_OVERALL_PSS_AVG);
        SECTION_MAPPINGS = Collections.unmodifiableMap(mappings);
    }

    private static final Pattern COUNT_PATTERN = Pattern.compile("^Count (\\d+)$");
    private static final Pattern METRIC_PATTERN = Pattern.compile("^([^:]+): (\\d+)$");
    private static final Pattern PROCESS_PATTERN = Pattern.compile("^\\S+$");

    @Override
    /**
     * {@inheritDoc}
     */
    public MemoryHealthItem parse(List<String> lines) {
        Map<String, Map<String, Long>> currentSection = null;
        Map<String, Long> currentProcess = new HashMap<String, Long>();
        String processName = null;
        for (String line : lines) {
            if (line.contains("Foreground")) {  // switch to parsing foreground
                mForeground = new HashMap<String, Map<String, Long>>();
                currentSection = mForeground;
            } else if (line.contains("Background")) { //switch to parsing background
                mBackground= new HashMap<String, Map<String, Long>>();
                currentSection = mBackground;
            } else if (COUNT_PATTERN.matcher(line).matches()) {
                // commit current process once we get to count
                currentProcess.put("count", parseLong(line));
                currentSection.put(processName, currentProcess);
            } else if (METRIC_PATTERN.matcher(line).matches()) {
                Matcher m = METRIC_PATTERN.matcher(line);
                m.matches();
                Long value = parseLong(m.group(2));
                String key = SECTION_MAPPINGS.get(m.group(1));
                if (key == null) {
                    continue;
                }
                currentProcess.put(key, value);
            } else if (PROCESS_PATTERN.matcher(line).matches()) {
                processName = line;
                currentProcess = new HashMap<String, Long>();
            }
        }

        return new MemoryHealthItem(mForeground, mBackground);
    }

    private long parseLong(String str) {
        try {
            return Long.parseLong(str);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public MemoryHealthItem parse(BufferedReader reader) throws IOException {
        List<String> lines = new ArrayList<String>();
        String line = reader.readLine();
        while (line != null) {
            lines.add(line);
            line = reader.readLine();
        }
        return parse(lines);
    }
}