Home | History | Annotate | Download | only in parser
      1 /*
      2  * Copyright (C) 2015 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.CpuInfoItem;
     19 
     20 import java.util.List;
     21 import java.util.regex.Matcher;
     22 import java.util.regex.Pattern;
     23 
     24 /**
     25  * A {@link IParser} to handle the output of {@code cpuinfo}.
     26  */
     27 public class CpuInfoParser implements IParser {
     28 
     29     // Example:
     30     //  0.1% 170/surfaceflinger: 0% user + 0% kernel
     31     private static final Pattern USAGE_PREFIX = Pattern.compile(
     32             "^ *\\+?(\\d+\\.?\\d*)\\% (\\d+)/([^ ]+): ");
     33 
     34     /**
     35      * {@inheritDoc}
     36      */
     37     @Override
     38     public CpuInfoItem parse(List<String> lines) {
     39         CpuInfoItem item = new CpuInfoItem();
     40         for (String line : lines) {
     41             Matcher m = USAGE_PREFIX.matcher(line);
     42             if (!m.lookingAt()) continue;
     43 
     44             if (m.groupCount() != 3) continue;
     45 
     46             int pid = Integer.parseInt(m.group(2));
     47             double percent = Double.parseDouble(m.group(1));
     48             String name = m.group(3);
     49             item.addRow(pid, percent, name);
     50         }
     51 
     52         return item;
     53     }
     54 }
     55