1 /* 2 * Copyright (C) 2016 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 17 package com.android.bugreport.cpuinfo; 18 19 import com.android.bugreport.util.Utils; 20 import com.android.bugreport.util.Line; 21 import com.android.bugreport.util.Lines; 22 23 import java.io.BufferedReader; 24 import java.io.IOException; 25 import java.util.ArrayList; 26 import java.util.regex.Pattern; 27 import java.util.regex.Matcher; 28 29 /** 30 * Incomplete - Reads a little bit of the cpu usage block printed in the monkey report. 31 */ 32 public class CpuUsageParser { 33 public static final Pattern CPU_USAGE_RE 34 = Pattern.compile("CPU usage from (-?\\d+)ms to (-?\\d+)ms ago \\((.*) to (.*)\\):"); 35 private static final Pattern TOTAL_RE 36 = Pattern.compile(".*TOTAL:.*"); 37 //= Pattern.compile("(-?\\d*(?:.\\d+)?)% TOTAL: (-?\\d*(?:.\\d+)?)% user \\+ (-?\\d*(?:.\\d+)?)% kernel \\+ (-?\\d*(?:.\\d+)?)% iowait \\+ (-?\\d*(?:.\\d+)?)% softirq"); 38 39 40 public CpuUsageParser() { 41 } 42 43 public CpuUsageSnapshot parse(Lines<? extends Line> lines) { 44 final CpuUsageSnapshot result = new CpuUsageSnapshot(); 45 46 final Matcher cpuUsageRe = CPU_USAGE_RE.matcher(""); 47 final Matcher totalRe = TOTAL_RE.matcher(""); 48 49 while (lines.hasNext()) { 50 final Line line = lines.next(); 51 final String text = line.text; 52 if (Utils.matches(cpuUsageRe, text)) { 53 // System.out.println("CpuUsageParser cpuUsageRe: " + text); 54 } else if (Utils.matches(totalRe, text)) { 55 /* 56 result.totalPercent = Float.parseFloat(totalRe.group(1)); 57 result.totalUser = Float.parseFloat(totalRe.group(2)); 58 result.totalKernel = Float.parseFloat(totalRe.group(3)); 59 result.totalIoWait = Float.parseFloat(totalRe.group(3)); 60 result.totalSoftIrq = Float.parseFloat(totalRe.group(3)); 61 */ 62 break; 63 } else { 64 if (false) { 65 System.out.println("CpuUsageParser Dropping: " + text); 66 } 67 } 68 } 69 70 if (false) { 71 System.out.println("totalPercent=" + result.totalPercent); 72 System.out.println("totalUser=" + result.totalUser); 73 System.out.println("totalKernel=" + result.totalKernel); 74 System.out.println("totalIoWait=" + result.totalIoWait); 75 System.out.println("totalSoftIrq=" + result.totalSoftIrq); 76 } 77 78 return result; 79 } 80 81 } 82 83