diff options
author | Tetsui Ohkubo <tetsui@google.com> | 2015-10-14 23:56:33 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-10-14 23:56:33 +0000 |
commit | 3f279f649c38d932c7e559793eb97be2d08f3ca6 (patch) | |
tree | a50758b902cdcef8a0b32d60bde8ec57b97c13d1 | |
parent | ff248ffa4d6ee63ab8c0e1f71b86a3d8ea3f4b25 (diff) | |
parent | 8a64160b924919a2ff80afeff73fe645d2995156 (diff) | |
download | loganalysis-3f279f649c38d932c7e559793eb97be2d08f3ca6.tar.gz |
Merge "Add cpuinfo log collector to AUPT"
3 files changed, 235 insertions, 0 deletions
diff --git a/src/com/android/loganalysis/item/CpuInfoItem.java b/src/com/android/loganalysis/item/CpuInfoItem.java new file mode 100644 index 0000000..896f368 --- /dev/null +++ b/src/com/android/loganalysis/item/CpuInfoItem.java @@ -0,0 +1,123 @@ +/* + * 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.item; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * An {@link IItem} used to cpuinfo. + */ +public class CpuInfoItem implements IItem { + /** Constant for JSON output */ + public static final String PROCESSES_KEY = "processes"; + /** Constant for JSON output */ + public static final String PID_KEY = "pid"; + /** Constant for JSON output */ + public static final String PERCENT_KEY = "percent"; + /** Constant for JSON output */ + public static final String NAME_KEY = "name"; + + private Map<Integer, Row> mRows = new HashMap<Integer, Row>(); + + private static class Row { + public double percent; + public String name; + } + + /** + * {@inheritDoc} + */ + @Override + public IItem merge(IItem other) throws ConflictingItemException { + throw new ConflictingItemException("CpuInfo items cannot be merged"); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isConsistent(IItem other) { + return false; + } + + /** + * {@inheritDoc} + */ + @Override + public JSONObject toJson() { + JSONObject object = new JSONObject(); + JSONArray processes = new JSONArray(); + for (int pid : getPids()) { + JSONObject proc = new JSONObject(); + try { + proc.put(PID_KEY, pid); + proc.put(PERCENT_KEY, getPercent(pid)); + proc.put(NAME_KEY, getName(pid)); + processes.put(proc); + } catch (JSONException e) { + // ignore + } + } + + try { + object.put(PROCESSES_KEY, processes); + } catch (JSONException e) { + // ignore + } + return object; + } + + /** + * Get a set of PIDs seen in the cpuinfo output. + */ + public Set<Integer> getPids() { + return mRows.keySet(); + } + + /** + * Add a row from the cpuinfo output to the {@link CpuInfoItem}. + * + * @param pid The PID from the output + * @param percent The percentage of CPU usage by the process + * @param name The process name + */ + public void addRow(int pid, double percent, String name) { + Row row = new Row(); + row.percent = percent; + row.name = name; + mRows.put(pid, row); + } + + /** + * Get the percentage of CPU usage by a given PID. + */ + public double getPercent(int pid) { + return mRows.get(pid).percent; + } + + /** + * Get the process name for a given PID. + */ + public String getName(int pid) { + return mRows.get(pid).name; + } +} diff --git a/src/com/android/loganalysis/parser/CpuInfoParser.java b/src/com/android/loganalysis/parser/CpuInfoParser.java new file mode 100644 index 0000000..e6769c4 --- /dev/null +++ b/src/com/android/loganalysis/parser/CpuInfoParser.java @@ -0,0 +1,54 @@ +/* + * 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.CpuInfoItem; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * A {@link IParser} to handle the output of {@code cpuinfo}. + */ +public class CpuInfoParser implements IParser { + + // Example: + // 0.1% 170/surfaceflinger: 0% user + 0% kernel + private static final Pattern USAGE_PREFIX = Pattern.compile( + "^ *\\+?(\\d+\\.?\\d*)\\% (\\d+)/([^ ]+): "); + + /** + * {@inheritDoc} + */ + @Override + public CpuInfoItem parse(List<String> lines) { + CpuInfoItem item = new CpuInfoItem(); + for (String line : lines) { + Matcher m = USAGE_PREFIX.matcher(line); + if (!m.lookingAt()) continue; + + if (m.groupCount() != 3) continue; + + int pid = Integer.parseInt(m.group(2)); + double percent = Double.parseDouble(m.group(1)); + String name = m.group(3); + item.addRow(pid, percent, name); + } + + return item; + } +} diff --git a/tests/src/com/android/loganalysis/parser/CpuInfoParserTest.java b/tests/src/com/android/loganalysis/parser/CpuInfoParserTest.java new file mode 100644 index 0000000..b400db5 --- /dev/null +++ b/tests/src/com/android/loganalysis/parser/CpuInfoParserTest.java @@ -0,0 +1,58 @@ +/* + * 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.CpuInfoItem; + +import junit.framework.TestCase; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Arrays; +import java.util.List; + +public class CpuInfoParserTest extends TestCase { + + public void testSingleLine() { + List<String> input = Arrays.asList(" 0.1% 170/surfaceflinger: 0% user + 0% kernel"); + + CpuInfoItem item = new CpuInfoParser().parse(input); + + assertEquals(1, item.getPids().size()); + assertEquals("surfaceflinger", item.getName(170)); + assertEquals(0.1, item.getPercent(170), 0.0001); + } + + public void testMultipleLines() { + List<String> input = Arrays.asList( + "CPU usage from 35935ms to 26370ms ago:", + " 57% 489/system_server: 37% user + 20% kernel / faults: 39754 minor 57 major", + " 34% 853/com.google.android.leanbacklauncher: 30% user + 4.6% kernel / faults: 7838 minor 14 major", + " 15% 19463/com.google.android.videos: 11% user + 3.3% kernel / faults: 21603 minor 141 major", + " 8.2% 170/surfaceflinger: 3.4% user + 4.8% kernel / faults: 1 minor"); + CpuInfoItem item = new CpuInfoParser().parse(input); + + assertEquals(4, item.getPids().size()); + assertEquals("system_server", item.getName(489)); + assertEquals(57.0, item.getPercent(489), 0.0001); + assertEquals("surfaceflinger", item.getName(170)); + assertEquals(8.2, item.getPercent(170), 0.0001); + } + +} + |