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.tradefed.profiler.recorder; 18 19 import java.util.HashMap; 20 import java.util.Map; 21 import java.util.regex.Matcher; 22 import java.util.regex.Pattern; 23 24 /** 25 * A parser for interpreting lines from /d/tracing/trace and encoding them as {@link TraceLine}s. 26 */ 27 public class TraceParser { 28 protected static final String REGEX_TASK_NAME = "^\\s*([a-zA-Z0-9_/\\-:<>]*)-\\d+"; 29 protected static final String REGEX_CPU_NUM = "\\s*\\[(\\d\\d\\d)\\]"; 30 protected static final String REGEX_FLAG_CLUSTER = "\\s*([d\\.])(.)(.)(\\d)"; 31 protected static final String REGEX_TIMESTAMP = "\\s*(\\d{1,5}\\.\\d{1,6}):"; 32 protected static final String REGEX_FUNCTION_NAME = "\\s*(\\w+):"; 33 protected static final String REGEX_FUNCTION_PARAMS = " (.*)$"; 34 35 private static final int GRP_TASK_NAME = 1; 36 private static final int GRP_CPU_NUM = 2; 37 private static final int GRP_IRQS_OFF = 3; 38 private static final int GRP_RESCHED = 4; 39 private static final int GRP_HARD_IRQ = 5; 40 private static final int GRP_PREEMPT = 6; 41 private static final int GRP_TIMESTAMP = 7; 42 private static final int GRP_FUNC_NAME = 8; 43 private static final int GRP_FUNC_PARAM = 9; 44 45 protected static final Pattern TRACE_LINE = 46 Pattern.compile( 47 REGEX_TASK_NAME 48 + REGEX_CPU_NUM 49 + REGEX_FLAG_CLUSTER 50 + REGEX_TIMESTAMP 51 + REGEX_FUNCTION_NAME 52 + REGEX_FUNCTION_PARAMS); 53 54 protected Map<String, Long> parseFunctionParams(String paramString) { 55 String[] pairs = paramString.split(","); 56 Map<String, Long> paramMap = new HashMap<>(); 57 for (String param : pairs) { 58 String[] splitParam = param.split("="); 59 String k = splitParam[0]; 60 String v = splitParam[1]; 61 if (v.length() >= 2 && v.charAt(1) == 'x') { 62 paramMap.put(k, Long.parseLong(v.substring(2), 16)); 63 } else { 64 paramMap.put(k, Long.parseLong(v)); 65 } 66 } 67 return paramMap; 68 } 69 70 public TraceLine parseTraceLine(String line) { 71 Matcher m = TRACE_LINE.matcher(line); 72 if (!m.find()) { 73 throw new IllegalArgumentException("Didn't match line: " + line); 74 } 75 TraceLine descriptor = new TraceLine(); 76 descriptor.setTaskName(m.group(GRP_TASK_NAME)); 77 descriptor.setCoreNum(Integer.parseInt(m.group(GRP_CPU_NUM))); 78 descriptor.setIrqsOff(!m.group(GRP_IRQS_OFF).equals(".")); 79 descriptor.setNeedsResched(!m.group(GRP_RESCHED).equals(".")); 80 descriptor.setHardIrq(m.group(GRP_HARD_IRQ).equals("H")); 81 descriptor.setPreemptDelay(Integer.parseInt(m.group(GRP_PREEMPT))); 82 descriptor.setTimestamp(Double.parseDouble(m.group(GRP_TIMESTAMP))); 83 descriptor.setFunctionName(m.group(GRP_FUNC_NAME)); 84 descriptor.setFunctionParams(parseFunctionParams(m.group(GRP_FUNC_PARAM))); 85 return descriptor; 86 } 87 } 88