blob: 7a05b1cb895d8c88e30b47808eaf07a77fa2d147 [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
17import dalvik.system.VMDebug;
18import java.io.IOException;
Richard Uhlerb3577302015-10-29 13:02:42 -070019import java.lang.ref.PhantomReference;
20import java.lang.ref.ReferenceQueue;
Richard Uhlerd640e292016-12-28 15:46:03 +000021import java.lang.ref.SoftReference;
Richard Uhlerb3577302015-10-29 13:02:42 -070022import java.lang.ref.WeakReference;
Richard Uhlercda4f2e2016-09-09 09:56:20 +010023import org.apache.harmony.dalvik.ddmc.DdmVmInternal;
Richard Uhler35244722015-09-10 16:45:54 -070024
25/**
26 * Program used to create a heap dump for test purposes.
27 */
28public class Main {
29 // Keep a reference to the DumpedStuff instance so that it is not garbage
30 // collected before we take the heap dump.
31 public static DumpedStuff stuff;
32
Richard Uhler69286492016-09-20 10:41:47 +010033 public static class ObjectTree {
34 public ObjectTree left;
35 public ObjectTree right;
36
37 public ObjectTree(ObjectTree left, ObjectTree right) {
38 this.left = left;
39 this.right = right;
40 }
41 }
42
Richard Uhlerf629cfd2016-12-12 13:11:26 +000043 public static class AddedObject {
44 }
45
46 public static class RemovedObject {
47 }
48
49 public static class UnchangedObject {
50 }
51
52 public static class ModifiedObject {
53 public int value;
54 public String modifiedRefField;
55 public String unmodifiedRefField;
56 }
57
58 public static class StackSmasher {
59 public StackSmasher child;
60 }
61
Richard Uhler35244722015-09-10 16:45:54 -070062 // We will take a heap dump that includes a single instance of this
63 // DumpedStuff class. Objects stored as fields in this class can be easily
64 // found in the hprof dump by searching for the instance of the DumpedStuff
65 // class and reading the desired field.
66 public static class DumpedStuff {
67 public String basicString = "hello, world";
Richard Uhlercda4f2e2016-09-09 09:56:20 +010068 public String nonAscii = "Sigma (Ʃ) is not ASCII";
Vladimir Marko595beb32017-02-06 14:11:54 +000069 public String embeddedZero = "embedded\0..."; // Non-ASCII for string compression purposes.
Richard Uhlerc7f77122016-02-11 08:48:24 -080070 public char[] charArray = "char thing".toCharArray();
Richard Uhler35244722015-09-10 16:45:54 -070071 public String nullString = null;
72 public Object anObject = new Object();
Richard Uhlerb3577302015-10-29 13:02:42 -070073 public ReferenceQueue<Object> referenceQueue = new ReferenceQueue<Object>();
74 public PhantomReference aPhantomReference = new PhantomReference(anObject, referenceQueue);
75 public WeakReference aWeakReference = new WeakReference(anObject, referenceQueue);
Richard Uhlercda4f2e2016-09-09 09:56:20 +010076 public WeakReference aNullReferentReference = new WeakReference(null, referenceQueue);
Richard Uhlerd640e292016-12-28 15:46:03 +000077 public SoftReference aSoftReference = new SoftReference(new Object());
Richard Uhler1af86f12015-10-29 14:55:00 -070078 public byte[] bigArray;
Richard Uhler69286492016-09-20 10:41:47 +010079 public ObjectTree[] gcPathArray = new ObjectTree[]{null, null,
80 new ObjectTree(
81 new ObjectTree(null, new ObjectTree(null, null)),
82 new ObjectTree(null, null)),
83 null};
Richard Uhlercda4f2e2016-09-09 09:56:20 +010084 public Object[] basicStringRef;
Richard Uhlerf629cfd2016-12-12 13:11:26 +000085 public AddedObject addedObject;
86 public UnchangedObject unchangedObject = new UnchangedObject();
87 public RemovedObject removedObject;
88 public ModifiedObject modifiedObject;
89 public StackSmasher stackSmasher;
90 public StackSmasher stackSmasherAdded;
91 public static String modifiedStaticField;
92 public int[] modifiedArray;
Richard Uhler1af86f12015-10-29 14:55:00 -070093
Richard Uhlerf629cfd2016-12-12 13:11:26 +000094 DumpedStuff(boolean baseline) {
95 int N = baseline ? 400000 : 1000000;
Richard Uhler1af86f12015-10-29 14:55:00 -070096 bigArray = new byte[N];
97 for (int i = 0; i < N; i++) {
98 bigArray[i] = (byte)((i*i) & 0xFF);
99 }
Richard Uhler1a5baaa2015-12-21 12:47:26 -0800100
Richard Uhlerf629cfd2016-12-12 13:11:26 +0000101 addedObject = baseline ? null : new AddedObject();
102 removedObject = baseline ? new RemovedObject() : null;
103 modifiedObject = new ModifiedObject();
104 modifiedObject.value = baseline ? 5 : 8;
105 modifiedObject.modifiedRefField = baseline ? "A1" : "A2";
106 modifiedObject.unmodifiedRefField = "B";
107 modifiedStaticField = baseline ? "C1" : "C2";
108 modifiedArray = baseline ? new int[]{0,1,2,3} : new int[]{3,1,2,0};
109
110 // Deep matching dominator trees shouldn't smash the stack when we try
111 // to diff them. Make some deep dominator trees to help test it.
112 for (int i = 0; i < 10000; i++) {
113 StackSmasher smasher = new StackSmasher();
114 smasher.child = stackSmasher;
115 stackSmasher = smasher;
116
117 if (!baseline) {
118 smasher = new StackSmasher();
119 smasher.child = stackSmasherAdded;
120 stackSmasherAdded = smasher;
121 }
122 }
Richard Uhler69286492016-09-20 10:41:47 +0100123
124 gcPathArray[2].right.left = gcPathArray[2].left.right;
Richard Uhler1af86f12015-10-29 14:55:00 -0700125 }
Richard Uhler35244722015-09-10 16:45:54 -0700126 }
127
128 public static void main(String[] args) throws IOException {
129 if (args.length < 1) {
130 System.err.println("no output file specified");
131 return;
132 }
133 String file = args[0];
134
Richard Uhlerf629cfd2016-12-12 13:11:26 +0000135 // If a --base argument is provided, it means we should generate a
136 // baseline hprof file suitable for using in testing diff.
137 boolean baseline = args.length > 1 && args[1].equals("--base");
138
Richard Uhlercda4f2e2016-09-09 09:56:20 +0100139 // Enable allocation tracking so we get stack traces in the heap dump.
140 DdmVmInternal.enableRecentAllocations(true);
141
Richard Uhler35244722015-09-10 16:45:54 -0700142 // Allocate the instance of DumpedStuff.
Richard Uhlerf629cfd2016-12-12 13:11:26 +0000143 stuff = new DumpedStuff(baseline);
Richard Uhler35244722015-09-10 16:45:54 -0700144
Richard Uhlercda4f2e2016-09-09 09:56:20 +0100145 // Create a bunch of unreachable objects pointing to basicString for the
146 // reverseReferencesAreNotUnreachable test
147 for (int i = 0; i < 100; i++) {
148 stuff.basicStringRef = new Object[]{stuff.basicString};
149 }
150
Richard Uhler35244722015-09-10 16:45:54 -0700151 // Take a heap dump that will include that instance of DumpedStuff.
152 System.err.println("Dumping hprof data to " + file);
153 VMDebug.dumpHprofData(file);
154 }
155}