Skip to content
Snippets Groups Projects
Commit 3f279f64 authored by Tetsui Ohkubo's avatar Tetsui Ohkubo Committed by Android (Google) Code Review
Browse files

Merge "Add cpuinfo log collector to AUPT"

parents ff248ffa 8a64160b
No related branches found
No related tags found
No related merge requests found
/*
* 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;
}
}
/*
* 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;
}
}
/*
* 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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment