• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * An {@link IItem} used to cpuinfo.
28  */
29 public class CpuInfoItem implements IItem {
30     /** Constant for JSON output */
31     public static final String PROCESSES_KEY = "processes";
32     /** Constant for JSON output */
33     public static final String PID_KEY = "pid";
34     /** Constant for JSON output */
35     public static final String PERCENT_KEY = "percent";
36     /** Constant for JSON output */
37     public static final String NAME_KEY = "name";
38 
39     private Map<Integer, Row> mRows = new HashMap<Integer, Row>();
40 
41     private static class Row {
42         public double percent;
43         public String name;
44     }
45 
46     /**
47      * {@inheritDoc}
48      */
49     @Override
merge(IItem other)50     public IItem merge(IItem other) throws ConflictingItemException {
51         throw new ConflictingItemException("CpuInfo items cannot be merged");
52     }
53 
54     /**
55      * {@inheritDoc}
56      */
57     @Override
isConsistent(IItem other)58     public boolean isConsistent(IItem other) {
59         return false;
60     }
61 
62     /**
63      * {@inheritDoc}
64      */
65     @Override
toJson()66     public JSONObject toJson() {
67         JSONObject object = new JSONObject();
68         JSONArray processes = new JSONArray();
69         for (int pid : getPids()) {
70             JSONObject proc = new JSONObject();
71             try {
72                 proc.put(PID_KEY, pid);
73                 proc.put(PERCENT_KEY, getPercent(pid));
74                 proc.put(NAME_KEY, getName(pid));
75                 processes.put(proc);
76             } catch (JSONException e) {
77                 // ignore
78             }
79         }
80 
81         try {
82             object.put(PROCESSES_KEY, processes);
83         } catch (JSONException e) {
84             // ignore
85         }
86         return object;
87     }
88 
89     /**
90      * Get a set of PIDs seen in the cpuinfo output.
91      */
getPids()92     public Set<Integer> getPids() {
93         return mRows.keySet();
94     }
95 
96     /**
97      * Add a row from the cpuinfo output to the {@link CpuInfoItem}.
98      *
99      * @param pid The PID from the output
100      * @param percent The percentage of CPU usage by the process
101      * @param name The process name
102      */
addRow(int pid, double percent, String name)103     public void addRow(int pid, double percent, String name) {
104         Row row = new Row();
105         row.percent = percent;
106         row.name = name;
107         mRows.put(pid, row);
108     }
109 
110     /**
111      * Get the percentage of CPU usage by a given PID.
112      */
getPercent(int pid)113     public double getPercent(int pid) {
114         return mRows.get(pid).percent;
115     }
116 
117     /**
118      * Get the process name for a given PID.
119      */
getName(int pid)120     public String getName(int pid) {
121         return mRows.get(pid).name;
122     }
123 }
124