• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.loganalysis.parser;
17 
18 import com.android.loganalysis.item.CompactMemInfoItem;
19 
20 import java.lang.NumberFormatException;
21 import java.util.List;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24 
25 /**
26  * Parser for the compact meminfo output, from the 'dumpsys meminfo -c' command.
27  * The output is a csv file which contains the information about how processes use memory.
28  * For now we are only interested in the pss of the processes. So we only parse the lines
29  * that start with proc and skip everything else.
30  *
31  * The format of the line is as follows:
32  * "proc,[type],[name],[pid],[pss],[activities].
33  *
34  * Type is the type of the process for example native, cached, foreground, etc.
35  * Name is the name of the process.
36  * Activities indicates if a process has any activities associated with it.
37  *
38  */
39 public class CompactMemInfoParser implements IParser {
40     private static final Pattern PROC_PATTERN =
41             Pattern.compile("proc,(\\w+),([a-zA-Z_0-9\\.]+),(\\d+),(\\d+),((\\S+),)?(.*)");
42     private static final Pattern LOST_RAM_PATTERN = Pattern.compile("lostram,(\\d+)");
43     private static final Pattern RAM_PATTERN = Pattern.compile("ram,(\\d+),(\\d+),(\\d+)");
44     private static final Pattern ZRAM_PATTERN = Pattern.compile("zram,(\\d+),(\\d+),(\\d+)");
45     private static final Pattern TUNING_PATTERN = Pattern.compile("tuning,(\\d+),(\\d+),(\\d+).*");
46 
47     /**
48      * Parse compact meminfo log. Output a CompactMemInfoItem which contains
49      * the list of processes, their pids and their pss.
50      */
51     @Override
parse(List<String> lines)52     public CompactMemInfoItem parse(List<String> lines) {
53         CompactMemInfoItem item = new CompactMemInfoItem();
54         for (String line : lines) {
55             Matcher m = PROC_PATTERN.matcher(line);
56             if (m.matches()) {
57                 String type = m.group(1);
58                 String name = m.group(2);
59                 try {
60                     int pid = Integer.parseInt(m.group(3));
61                     long pss = Long.parseLong(m.group(4));
62                     long swap = 0;
63                     if (m.group(6) != null && !"N/A".equals(m.group(6))) {
64                         swap = Long.parseLong(m.group(6));
65                     }
66                     boolean activities = "a".equals(m.group(7));
67                     item.addPid(pid, name, type, pss, swap, activities);
68                     continue;
69                 } catch (NumberFormatException nfe) {
70                     // ignore exception
71                 }
72             }
73 
74             m = LOST_RAM_PATTERN.matcher(line);
75             if (m.matches()) {
76                 try {
77                     long lostRam = Long.parseLong(m.group(1));
78                     item.setLostRam(lostRam);
79                     continue;
80                 } catch (NumberFormatException nfe) {
81                     // ignore exception
82                 }
83             }
84 
85             m = RAM_PATTERN.matcher(line);
86             if (m.matches()) {
87                 try {
88                     item.setFreeRam(Long.parseLong(m.group(2)));
89                     continue;
90                 } catch (NumberFormatException nfe) {
91                     // ignore exception
92                 }
93             }
94 
95             m = ZRAM_PATTERN.matcher(line);
96             if (m.matches()) {
97                 try {
98                     item.setTotalZram(Long.parseLong(m.group(1)));
99                     item.setFreeSwapZram(Long.parseLong(m.group(3)));
100                     continue;
101                 } catch (NumberFormatException nfe) {
102                     // ignore exception
103                 }
104             }
105 
106             m = TUNING_PATTERN.matcher(line);
107             if (m.matches()) {
108                 try {
109                     item.setTuningLevel(Long.parseLong(m.group(3)));
110                     continue;
111                 } catch (NumberFormatException nfe) {
112                     // ignore exception
113                 }
114             }
115         }
116         return item;
117     }
118 }
119