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
|
/*
* Copyright (C) 2016 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.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) throws Exception {
doTest();
}
public static void doTest() throws Exception {
A a = new A();
B b = new B();
B b2 = new B();
C c = new C();
A[] aArray = new A[5];
String s = "Hello World";
setTag(a, 1);
setTag(b, 2);
setTag(b2, 3);
setTag(aArray, 4);
setTag(s, 5);
setTag(B.class, 100);
int all = iterateThroughHeapCount(0, null, Integer.MAX_VALUE);
int tagged = iterateThroughHeapCount(HEAP_FILTER_OUT_UNTAGGED, null, Integer.MAX_VALUE);
int untagged = iterateThroughHeapCount(HEAP_FILTER_OUT_TAGGED, null, Integer.MAX_VALUE);
int taggedClass = iterateThroughHeapCount(HEAP_FILTER_OUT_CLASS_UNTAGGED, null,
Integer.MAX_VALUE);
int untaggedClass = iterateThroughHeapCount(HEAP_FILTER_OUT_CLASS_TAGGED, null,
Integer.MAX_VALUE);
if (all != tagged + untagged) {
throw new IllegalStateException("Instances: " + all + " != " + tagged + " + " + untagged);
}
if (all != taggedClass + untaggedClass) {
throw new IllegalStateException("By class: " + all + " != " + taggedClass + " + " +
untaggedClass);
}
if (tagged != 6) {
throw new IllegalStateException(tagged + " tagged objects");
}
if (taggedClass != 2) {
throw new IllegalStateException(tagged + " objects with tagged class");
}
if (all == tagged) {
throw new IllegalStateException("All objects tagged");
}
if (all == taggedClass) {
throw new IllegalStateException("All objects have tagged class");
}
long classTags[] = new long[100];
long sizes[] = new long[100];
long tags[] = new long[100];
int lengths[] = new int[100];
int n = iterateThroughHeapData(HEAP_FILTER_OUT_UNTAGGED, null, classTags, sizes, tags, lengths);
System.out.println(sort(n, classTags, sizes, tags, lengths));
iterateThroughHeapAdd(HEAP_FILTER_OUT_UNTAGGED, null);
n = iterateThroughHeapData(HEAP_FILTER_OUT_UNTAGGED, null, classTags, sizes, tags, lengths);
System.out.println(sort(n, classTags, sizes, tags, lengths));
System.out.println(iterateThroughHeapString(getTag(s)));
System.out.println(getTag(s));
boolean[] zArray = new boolean[] { false, true };
setTag(zArray, 1);
System.out.println(iterateThroughHeapPrimitiveArray(getTag(zArray)));
System.out.println(getTag(zArray));
byte[] bArray = new byte[] { 1, 2, 3 };
setTag(bArray, 1);
System.out.println(iterateThroughHeapPrimitiveArray(getTag(bArray)));
System.out.println(getTag(bArray));
char[] cArray = new char[] { 'A', 'Z' };
setTag(cArray, 1);
System.out.println(iterateThroughHeapPrimitiveArray(getTag(cArray)));
System.out.println(getTag(cArray));
short[] sArray = new short[] { 1, 2, 3 };
setTag(sArray, 1);
System.out.println(iterateThroughHeapPrimitiveArray(getTag(sArray)));
System.out.println(getTag(sArray));
int[] iArray = new int[] { 1, 2, 3 };
setTag(iArray, 1);
System.out.println(iterateThroughHeapPrimitiveArray(getTag(iArray)));
System.out.println(getTag(iArray));
float[] fArray = new float[] { 0.0f, 1.0f };
setTag(fArray, 1);
System.out.println(iterateThroughHeapPrimitiveArray(getTag(fArray)));
System.out.println(getTag(fArray));
long[] lArray = new long[] { 1, 2, 3 };
setTag(lArray, 1);
System.out.println(iterateThroughHeapPrimitiveArray(getTag(lArray)));
System.out.println(getTag(lArray));
double[] dArray = new double[] { 0.0, 1.0 };
setTag(dArray, 1);
System.out.println(iterateThroughHeapPrimitiveArray(getTag(dArray)));
System.out.println(getTag(dArray));
}
static class A {
}
static class B {
}
static class C {
}
static class HeapElem implements Comparable<HeapElem> {
long classTag;
long size;
long tag;
int length;
public int compareTo(HeapElem other) {
if (tag != other.tag) {
return Long.compare(tag, other.tag);
}
if (classTag != other.classTag) {
return Long.compare(classTag, other.classTag);
}
if (size != other.size) {
return Long.compare(size, other.size);
}
return Integer.compare(length, other.length);
}
public String toString() {
return "{tag=" + tag + ", class-tag=" + classTag + ", size=" +
(tag >= 100 ? "<class>" : size) // Class size is dependent on 32-bit vs 64-bit,
// so strip it.
+ ", length=" + length + "}";
}
}
private static ArrayList<HeapElem> sort(int n, long classTags[], long sizes[], long tags[],
int lengths[]) {
ArrayList<HeapElem> ret = new ArrayList<HeapElem>(n);
for (int i = 0; i < n; i++) {
HeapElem elem = new HeapElem();
elem.classTag = classTags[i];
elem.size = sizes[i];
elem.tag = tags[i];
elem.length = lengths[i];
ret.add(elem);
}
Collections.sort(ret);
return ret;
}
private static native void setTag(Object o, long tag);
private static native long getTag(Object o);
private final static int HEAP_FILTER_OUT_TAGGED = 0x4;
private final static int HEAP_FILTER_OUT_UNTAGGED = 0x8;
private final static int HEAP_FILTER_OUT_CLASS_TAGGED = 0x10;
private final static int HEAP_FILTER_OUT_CLASS_UNTAGGED = 0x20;
private static native int iterateThroughHeapCount(int heapFilter,
Class<?> klassFilter, int stopAfter);
private static native int iterateThroughHeapData(int heapFilter,
Class<?> klassFilter, long classTags[], long sizes[], long tags[], int lengths[]);
private static native int iterateThroughHeapAdd(int heapFilter,
Class<?> klassFilter);
private static native String iterateThroughHeapString(long tag);
private static native String iterateThroughHeapPrimitiveArray(long tag);
}
|