Home | History | Annotate | Download | only in item
      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.item;
     17 
     18 import org.json.JSONArray;
     19 import org.json.JSONException;
     20 import org.json.JSONObject;
     21 
     22 import java.util.HashMap;
     23 import java.util.Map;
     24 import java.util.Set;
     25 
     26 
     27 /**
     28  * An {@link IItem} used to gfxinfo.
     29  */
     30 public class GfxInfoItem implements IItem {
     31     /** Constant for JSON output */
     32     public static final String PROCESSES_KEY = "processes";
     33     /** Constant for JSON output */
     34     public static final String PID_KEY = "pid";
     35     /** Constant for JSON output */
     36     public static final String NAME_KEY = "name";
     37     /** Constant for JSON output */
     38     public static final String TOTAL_FRAMES_KEY = "total_frames";
     39     /** Constant for JSON output */
     40     public static final String JANKY_FRAMES_KEY = "janky_frames";
     41     /** Constant for JSON output */
     42     public static final String PERCENTILE_90_KEY = "percentile_90";
     43     /** Constant for JSON output */
     44     public static final String PERCENTILE_95_KEY = "percentile_95";
     45     /** Constant for JSON output */
     46     public static final String PERCENTILE_99_KEY = "percentile_99";
     47 
     48     private Map<Integer, Row> mRows = new HashMap<Integer, Row>();
     49 
     50     private static class Row {
     51         public String name;
     52         public long totalFrames;
     53         public long jankyFrames;
     54         public int percentile90;
     55         public int percentile95;
     56         public int percentile99;
     57     }
     58 
     59     /**
     60      * {@inheritDoc}
     61      */
     62     @Override
     63     public IItem merge(IItem other) throws ConflictingItemException {
     64         throw new ConflictingItemException("GfxInfo items cannot be merged");
     65     }
     66 
     67     /**
     68      * {@inheritDoc}
     69      */
     70     @Override
     71     public boolean isConsistent(IItem other) {
     72         return false;
     73     }
     74 
     75     /**
     76      * {@inheritDoc}
     77      */
     78     @Override
     79     public JSONObject toJson() {
     80         JSONObject object = new JSONObject();
     81         JSONArray processes = new JSONArray();
     82         for (int pid : getPids()) {
     83             JSONObject proc = new JSONObject();
     84             try {
     85                 proc.put(PID_KEY, pid);
     86                 proc.put(NAME_KEY, getName(pid));
     87                 proc.put(TOTAL_FRAMES_KEY, getTotalFrames(pid));
     88                 proc.put(PERCENTILE_90_KEY, getPrecentile90(pid));
     89                 proc.put(PERCENTILE_95_KEY, getPrecentile95(pid));
     90                 proc.put(PERCENTILE_99_KEY, getPrecentile99(pid));
     91                 processes.put(proc);
     92             } catch (JSONException e) {
     93                 // ignore
     94             }
     95         }
     96 
     97         try {
     98             object.put(PROCESSES_KEY, processes);
     99         } catch (JSONException e) {
    100             // ignore
    101         }
    102         return object;
    103     }
    104 
    105     /**
    106      * Get a set of PIDs seen in the gfxinfo output.
    107      */
    108     public Set<Integer> getPids() {
    109         return mRows.keySet();
    110     }
    111 
    112     /**
    113      * Add a row from the gfxinfo output to the {@link GfxInfoItem}.
    114      *
    115      * @param pid The PID from the output
    116      * @param name The process name
    117      * @param totalFrames The number of total frames rendered by the process
    118      * @param jankyFrames The number of janky frames rendered by the process
    119      */
    120     public void addRow(
    121             int pid,
    122             String name,
    123             long totalFrames,
    124             long jankyFrames,
    125             int percentile90,
    126             int percentile95,
    127             int percentile99) {
    128         Row row = new Row();
    129         row.name = name;
    130         row.totalFrames = totalFrames;
    131         row.jankyFrames = jankyFrames;
    132         row.percentile90 = percentile90;
    133         row.percentile95 = percentile95;
    134         row.percentile99 = percentile99;
    135         mRows.put(pid, row);
    136     }
    137 
    138     /**
    139      * Get the process name for a given PID.
    140      */
    141     public String getName(int pid) {
    142         return mRows.get(pid).name;
    143     }
    144 
    145     /**
    146      * Get the number of total frames rendered by a given PID.
    147      */
    148     public long getTotalFrames(int pid) {
    149         return mRows.get(pid).totalFrames;
    150     }
    151 
    152     /**
    153      * Get the number of janky frames rendered by a given PID.
    154      */
    155     public long getJankyFrames(int pid) {
    156         return mRows.get(pid).jankyFrames;
    157     }
    158 
    159     /** Get the 90th percentile value of frame times (ms) */
    160     public int getPrecentile90(int pid) {
    161         return mRows.get(pid).percentile90;
    162     }
    163 
    164     /** Get the 95th percentile value of frame times (ms) */
    165     public int getPrecentile95(int pid) {
    166         return mRows.get(pid).percentile95;
    167     }
    168 
    169     /** Get the 99th percentile value of frame times (ms) */
    170     public int getPrecentile99(int pid) {
    171         return mRows.get(pid).percentile99;
    172     }
    173 }
    174