blob: ebce61c2e81a36b33e6c57e86b0b8bb29772742a [file] [log] [blame]
Richard Uhler35244722015-09-10 16:45:54 -07001/*
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
17package com.android.ahat;
18
19import com.android.tools.perflib.heap.ClassObj;
20import com.android.tools.perflib.heap.Field;
21import com.android.tools.perflib.heap.Instance;
Richard Uhlerec78c782016-05-13 14:19:37 -070022import com.android.tools.perflib.heap.ProguardMap;
Richard Uhler35244722015-09-10 16:45:54 -070023import java.io.File;
24import java.io.IOException;
Richard Uhlerec78c782016-05-13 14:19:37 -070025import java.text.ParseException;
Richard Uhler35244722015-09-10 16:45:54 -070026import java.util.Map;
27
28/**
29 * The TestDump class is used to get an AhatSnapshot for the test-dump
30 * program.
31 */
32public class TestDump {
33 // It can take on the order of a second to parse and process the test-dump
34 // hprof. To avoid repeating this overhead for each test case, we cache the
35 // loaded instance of TestDump and reuse it when possible. In theory the
36 // test cases should not be able to modify the cached snapshot in a way that
37 // is visible to other test cases.
38 private static TestDump mCachedTestDump = null;
39
40 private AhatSnapshot mSnapshot = null;
41
42 /**
43 * Load the test-dump.hprof file.
44 * The location of the file is read from the system property
45 * "ahat.test.dump.hprof", which is expected to be set on the command line.
46 * For example:
47 * java -Dahat.test.dump.hprof=test-dump.hprof -jar ahat-tests.jar
48 *
Richard Uhlerec78c782016-05-13 14:19:37 -070049 * An IOException is thrown if there is a failure reading the hprof file or
50 * the proguard map.
Richard Uhler35244722015-09-10 16:45:54 -070051 */
52 private TestDump() throws IOException {
53 String hprof = System.getProperty("ahat.test.dump.hprof");
Richard Uhlerec78c782016-05-13 14:19:37 -070054
55 String mapfile = System.getProperty("ahat.test.dump.map");
56 ProguardMap map = new ProguardMap();
57 try {
58 map.readFromFile(new File(mapfile));
59 } catch (ParseException e) {
60 throw new IOException("Unable to load proguard map", e);
61 }
62
63 mSnapshot = AhatSnapshot.fromHprof(new File(hprof), map);
Richard Uhler35244722015-09-10 16:45:54 -070064 }
65
66 /**
67 * Get the AhatSnapshot for the test dump program.
68 */
69 public AhatSnapshot getAhatSnapshot() {
70 return mSnapshot;
71 }
72
73 /**
74 * Return the value of a field in the DumpedStuff instance in the
75 * snapshot for the test-dump program.
76 */
77 public Object getDumpedThing(String name) {
78 ClassObj main = mSnapshot.findClass("Main");
79 Instance stuff = null;
80 for (Map.Entry<Field, Object> fields : main.getStaticFieldValues().entrySet()) {
81 if ("stuff".equals(fields.getKey().getName())) {
82 stuff = (Instance) fields.getValue();
83 }
84 }
85 return InstanceUtils.getField(stuff, name);
86 }
87
88 /**
89 * Get the test dump.
90 * An IOException is thrown if there is an error reading the test dump hprof
91 * file.
92 * To improve performance, this returns a cached instance of the TestDump
93 * when possible.
94 */
95 public static synchronized TestDump getTestDump() throws IOException {
96 if (mCachedTestDump == null) {
97 mCachedTestDump = new TestDump();
98 }
99 return mCachedTestDump;
100 }
101}