1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.Serializable;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Memory usage information.
*/
class MemoryUsage implements Serializable {
private static final long serialVersionUID = 0;
static final MemoryUsage NOT_AVAILABLE = new MemoryUsage();
static int errorCount = 0;
// These values are in 1kB increments (not 4kB like you'd expect).
final int nativeSharedPages;
final int javaSharedPages;
final int otherSharedPages;
final int nativePrivatePages;
final int javaPrivatePages;
final int otherPrivatePages;
final int allocCount;
final int allocSize;
final int freedCount;
final int freedSize;
final long nativeHeapSize;
public MemoryUsage(String line) {
String[] parsed = line.split(",");
nativeSharedPages = Integer.parseInt(parsed[1]);
javaSharedPages = Integer.parseInt(parsed[2]);
otherSharedPages = Integer.parseInt(parsed[3]);
nativePrivatePages = Integer.parseInt(parsed[4]);
javaPrivatePages = Integer.parseInt(parsed[5]);
otherPrivatePages = Integer.parseInt(parsed[6]);
allocCount = Integer.parseInt(parsed[7]);
allocSize = Integer.parseInt(parsed[8]);
freedCount = Integer.parseInt(parsed[9]);
freedSize = Integer.parseInt(parsed[10]);
nativeHeapSize = Long.parseLong(parsed[11]);
}
MemoryUsage() {
nativeSharedPages = -1;
javaSharedPages = -1;
otherSharedPages = -1;
nativePrivatePages = -1;
javaPrivatePages = -1;
otherPrivatePages = -1;
allocCount = -1;
allocSize = -1;
freedCount = -1;
freedSize = -1;
nativeHeapSize = -1;
}
MemoryUsage(int nativeSharedPages,
int javaSharedPages,
int otherSharedPages,
int nativePrivatePages,
int javaPrivatePages,
int otherPrivatePages,
int allocCount,
int allocSize,
int freedCount,
int freedSize,
long nativeHeapSize) {
this.nativeSharedPages = nativeSharedPages;
this.javaSharedPages = javaSharedPages;
this.otherSharedPages = otherSharedPages;
this.nativePrivatePages = nativePrivatePages;
this.javaPrivatePages = javaPrivatePages;
this.otherPrivatePages = otherPrivatePages;
this.allocCount = allocCount;
this.allocSize = allocSize;
this.freedCount = freedCount;
this.freedSize = freedSize;
this.nativeHeapSize = nativeHeapSize;
}
MemoryUsage subtract(MemoryUsage baseline) {
return new MemoryUsage(
nativeSharedPages - baseline.nativeSharedPages,
javaSharedPages - baseline.javaSharedPages,
otherSharedPages - baseline.otherSharedPages,
nativePrivatePages - baseline.nativePrivatePages,
javaPrivatePages - baseline.javaPrivatePages,
otherPrivatePages - baseline.otherPrivatePages,
allocCount - baseline.allocCount,
allocSize - baseline.allocSize,
freedCount - baseline.freedCount,
freedSize - baseline.freedSize,
nativeHeapSize - baseline.nativeHeapSize);
}
int javaHeapSize() {
return allocSize - freedSize;
}
int totalHeap() {
return javaHeapSize() + (int) nativeHeapSize;
}
int javaPagesInK() {
return javaSharedPages + javaPrivatePages;
}
int nativePagesInK() {
return nativeSharedPages + nativePrivatePages;
}
int otherPagesInK() {
return otherSharedPages + otherPrivatePages;
}
int totalPages() {
return javaSharedPages + javaPrivatePages + nativeSharedPages +
nativePrivatePages + otherSharedPages + otherPrivatePages;
}
/**
* Was this information available?
*/
boolean isAvailable() {
return nativeSharedPages != -1;
}
/**
* Measures baseline memory usage.
*/
static MemoryUsage baseline() {
return forClass(null);
}
private static final String CLASS_PATH = "-Xbootclasspath"
+ ":/system/framework/core.jar"
+ ":/system/framework/ext.jar"
+ ":/system/framework/framework.jar"
+ ":/system/framework/framework-tests.jar"
+ ":/system/framework/services.jar"
+ ":/system/framework/loadclass.jar";
private static final String[] GET_DIRTY_PAGES = {
"adb", "shell", "dalvikvm", CLASS_PATH, "LoadClass" };
/**
* Measures memory usage for the given class.
*/
static MemoryUsage forClass(String className) {
MeasureWithTimeout measurer = new MeasureWithTimeout(className);
new Thread(measurer).start();
synchronized (measurer) {
if (measurer.memoryUsage == null) {
// Wait up to 10s.
try {
measurer.wait(30000);
} catch (InterruptedException e) {
System.err.println("Interrupted waiting for measurement.");
e.printStackTrace();
return NOT_AVAILABLE;
}
// If it's still null.
if (measurer.memoryUsage == null) {
System.err.println("Timed out while measuring "
+ className + ".");
return NOT_AVAILABLE;
}
}
System.err.println("Got memory usage for " + className + ".");
return measurer.memoryUsage;
}
}
static class MeasureWithTimeout implements Runnable {
final String className;
MemoryUsage memoryUsage = null;
MeasureWithTimeout(String className) {
this.className = className;
}
public void run() {
MemoryUsage measured = measure();
synchronized (this) {
memoryUsage = measured;
notifyAll();
}
}
private MemoryUsage measure() {
String[] commands = GET_DIRTY_PAGES;
if (className != null) {
List<String> commandList = new ArrayList<String>(
GET_DIRTY_PAGES.length + 1);
commandList.addAll(Arrays.asList(commands));
commandList.add(className);
commands = commandList.toArray(new String[commandList.size()]);
}
try {
final Process process = Runtime.getRuntime().exec(commands);
final InputStream err = process.getErrorStream();
// Send error output to stderr.
Thread errThread = new Thread() {
@Override
public void run() {
copy(err, System.err);
}
};
errThread.setDaemon(true);
errThread.start();
BufferedReader in = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = in.readLine();
if (line == null || !line.startsWith("DECAFBAD,")) {
System.err.println("Got bad response for " + className
+ ": " + line + "; command was " + Arrays.toString(commands));
errorCount += 1;
return NOT_AVAILABLE;
}
in.close();
err.close();
process.destroy();
return new MemoryUsage(line);
} catch (IOException e) {
System.err.println("Error getting stats for "
+ className + ".");
e.printStackTrace();
return NOT_AVAILABLE;
}
}
}
/**
* Copies from one stream to another.
*/
private static void copy(InputStream in, OutputStream out) {
byte[] buffer = new byte[1024];
int read;
try {
while ((read = in.read(buffer)) > -1) {
out.write(buffer, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/** Measures memory usage information and stores it in the model. */
public static void main(String[] args) throws IOException,
ClassNotFoundException {
Root root = Root.fromFile(args[0]);
root.baseline = baseline();
for (LoadedClass loadedClass : root.loadedClasses.values()) {
if (loadedClass.systemClass) {
loadedClass.measureMemoryUsage();
}
}
root.toFile(args[0]);
}
}
|