1 /* 2 * Copyright (C) 2011 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.Map.Entry; 25 import java.util.Set; 26 27 28 /** 29 * An {@link IItem} used to procrank info. 30 */ 31 public class ProcrankItem implements IItem { 32 public static final String TYPE = "PROCRANK"; 33 34 /** Constant for JSON output */ 35 public static final String LINES = "LINES"; 36 /** Constant for JSON output */ 37 public static final String PID = "PID"; 38 /** Constant for JSON output */ 39 public static final String PROCESS_NAME = "PROCESS_NAME"; 40 /** Constant for JSON output */ 41 public static final String VSS = "VSS"; 42 /** Constant for JSON output */ 43 public static final String RSS = "RSS"; 44 /** Constant for JSON output */ 45 public static final String PSS = "PSS"; 46 /** Constant for JSON output */ 47 public static final String USS = "USS"; 48 /** Constant for JSON output */ 49 public static final String TEXT = "TEXT"; 50 51 private class ProcrankValue { 52 public String mProcessName; 53 public int mVss; 54 public int mRss; 55 public int mPss; 56 public int mUss; 57 ProcrankValue(String processName, int vss, int rss, int pss, int uss)58 public ProcrankValue(String processName, int vss, int rss, int pss, int uss) { 59 mProcessName = processName; 60 mVss = vss; 61 mRss = rss; 62 mPss = pss; 63 mUss = uss; 64 } 65 } 66 67 private String mText = null; 68 private Map<Integer, ProcrankValue> mProcrankLines = new HashMap<Integer, ProcrankValue>(); 69 70 /** 71 * Add a line from the procrank output to the {@link ProcrankItem}. 72 * 73 * @param pid The PID from the output 74 * @param processName The process name from the cmdline column 75 * @param vss The VSS in KB 76 * @param rss The RSS in KB 77 * @param pss The PSS in KB 78 * @param uss The USS in KB 79 */ addProcrankLine(int pid, String processName, int vss, int rss, int pss, int uss)80 public void addProcrankLine(int pid, String processName, int vss, int rss, int pss, int uss) { 81 mProcrankLines.put(pid, new ProcrankValue(processName, vss, rss, pss, uss)); 82 } 83 84 /** 85 * Get a set of PIDs seen in the procrank output. 86 */ getPids()87 public Set<Integer> getPids() { 88 return mProcrankLines.keySet(); 89 } 90 91 /** 92 * Get the process name for a given PID. 93 */ getProcessName(int pid)94 public String getProcessName(int pid) { 95 if (!mProcrankLines.containsKey(pid)) { 96 return null; 97 } 98 99 return mProcrankLines.get(pid).mProcessName; 100 } 101 102 /** 103 * Get the VSS for a given PID. 104 */ getVss(int pid)105 public Integer getVss(int pid) { 106 if (!mProcrankLines.containsKey(pid)) { 107 return null; 108 } 109 110 return mProcrankLines.get(pid).mVss; 111 } 112 113 /** 114 * Get the RSS for a given PID. 115 */ getRss(int pid)116 public Integer getRss(int pid) { 117 if (!mProcrankLines.containsKey(pid)) { 118 return null; 119 } 120 121 return mProcrankLines.get(pid).mRss; 122 } 123 124 /** 125 * Get the PSS for a given PID. 126 */ getPss(int pid)127 public Integer getPss(int pid) { 128 if (!mProcrankLines.containsKey(pid)) { 129 return null; 130 } 131 132 return mProcrankLines.get(pid).mPss; 133 } 134 135 /** 136 * Get the USS for a given PID. 137 */ getUss(int pid)138 public Integer getUss(int pid) { 139 if (!mProcrankLines.containsKey(pid)) { 140 return null; 141 } 142 143 return mProcrankLines.get(pid).mUss; 144 } 145 146 /** 147 * Get the raw text of the procrank command. 148 */ getText()149 public String getText() { 150 return mText; 151 } 152 153 /** 154 * Set the raw text of the procrank command. 155 */ setText(String text)156 public void setText(String text) { 157 mText = text; 158 } 159 160 /** 161 * {@inheritDoc} 162 */ 163 @Override merge(IItem other)164 public IItem merge(IItem other) throws ConflictingItemException { 165 throw new ConflictingItemException("Procrank items cannot be merged"); 166 } 167 168 /** 169 * {@inheritDoc} 170 */ 171 @Override isConsistent(IItem other)172 public boolean isConsistent(IItem other) { 173 return false; 174 } 175 176 /** 177 * {@inheritDoc} 178 */ 179 @Override toJson()180 public JSONObject toJson() { 181 JSONObject object = new JSONObject(); 182 JSONArray lines = new JSONArray(); 183 try { 184 for (Entry<Integer, ProcrankValue> entry : mProcrankLines.entrySet()) { 185 final ProcrankValue procrankValue = entry.getValue(); 186 JSONObject line = new JSONObject(); 187 line.put(PID, entry.getKey()); 188 line.put(PROCESS_NAME, procrankValue.mProcessName); 189 line.put(VSS, procrankValue.mVss); 190 line.put(RSS, procrankValue.mRss); 191 line.put(PSS, procrankValue.mPss); 192 line.put(USS, procrankValue.mUss); 193 lines.put(line); 194 } 195 object.put(LINES, lines); 196 object.put(TEXT, getText()); 197 } catch (JSONException e) { 198 // Ignore 199 } 200 return object; 201 } 202 } 203