blob: 544a3656b213fdc6ddff8f144773371efca33e8f [file] [log] [blame]
Andreas Gampee54d9922016-10-11 19:55:37 -07001/*
2 * Copyright (C) 2016 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 java.util.ArrayList;
18import java.util.Collections;
19
20public class Main {
21 public static void main(String[] args) throws Exception {
22 System.loadLibrary(args[1]);
23
24 doTest();
25 }
26
27 public static void doTest() throws Exception {
28 A a = new A();
29 B b = new B();
30 B b2 = new B();
31 C c = new C();
32 A[] aArray = new A[5];
33
34 setTag(a, 1);
35 setTag(b, 2);
36 setTag(b2, 3);
37 setTag(aArray, 4);
38 setTag(B.class, 100);
39
40 int all = iterateThroughHeapCount(0, null, Integer.MAX_VALUE);
41 int tagged = iterateThroughHeapCount(HEAP_FILTER_OUT_UNTAGGED, null, Integer.MAX_VALUE);
42 int untagged = iterateThroughHeapCount(HEAP_FILTER_OUT_TAGGED, null, Integer.MAX_VALUE);
43 int taggedClass = iterateThroughHeapCount(HEAP_FILTER_OUT_CLASS_UNTAGGED, null,
44 Integer.MAX_VALUE);
45 int untaggedClass = iterateThroughHeapCount(HEAP_FILTER_OUT_CLASS_TAGGED, null,
46 Integer.MAX_VALUE);
47
48 if (all != tagged + untagged) {
49 throw new IllegalStateException("Instances: " + all + " != " + tagged + " + " + untagged);
50 }
51 if (all != taggedClass + untaggedClass) {
52 throw new IllegalStateException("By class: " + all + " != " + taggedClass + " + " +
53 untaggedClass);
54 }
55 if (tagged != 5) {
56 throw new IllegalStateException(tagged + " tagged objects");
57 }
58 if (taggedClass != 2) {
59 throw new IllegalStateException(tagged + " objects with tagged class");
60 }
61 if (all == tagged) {
62 throw new IllegalStateException("All objects tagged");
63 }
64 if (all == taggedClass) {
65 throw new IllegalStateException("All objects have tagged class");
66 }
67
68 long classTags[] = new long[100];
69 long sizes[] = new long[100];
70 long tags[] = new long[100];
71 int lengths[] = new int[100];
72
73 int n = iterateThroughHeapData(HEAP_FILTER_OUT_UNTAGGED, null, classTags, sizes, tags, lengths);
74 System.out.println(sort(n, classTags, sizes, tags, lengths));
75
76 iterateThroughHeapAdd(HEAP_FILTER_OUT_UNTAGGED, null);
77 n = iterateThroughHeapData(HEAP_FILTER_OUT_UNTAGGED, null, classTags, sizes, tags, lengths);
78 System.out.println(sort(n, classTags, sizes, tags, lengths));
79 }
80
81 static class A {
82 }
83
84 static class B {
85 }
86
87 static class C {
88 }
89
90 static class HeapElem implements Comparable<HeapElem> {
91 long classTag;
92 long size;
93 long tag;
94 int length;
95
96 public int compareTo(HeapElem other) {
97 if (tag != other.tag) {
98 return Long.compare(tag, other.tag);
99 }
100 if (classTag != other.classTag) {
101 return Long.compare(classTag, other.classTag);
102 }
103 if (size != other.size) {
104 return Long.compare(size, other.size);
105 }
106 return Integer.compare(length, other.length);
107 }
108
109 public String toString() {
110 return "{tag=" + tag + ", class-tag=" + classTag + ", size=" +
111 (tag >= 100 ? "<class>" : size) // Class size is dependent on 32-bit vs 64-bit,
112 // so strip it.
113 + ", length=" + length + "}";
114 }
115 }
116
117 private static ArrayList<HeapElem> sort(int n, long classTags[], long sizes[], long tags[],
118 int lengths[]) {
119 ArrayList<HeapElem> ret = new ArrayList<HeapElem>(n);
120 for (int i = 0; i < n; i++) {
121 HeapElem elem = new HeapElem();
122 elem.classTag = classTags[i];
123 elem.size = sizes[i];
124 elem.tag = tags[i];
125 elem.length = lengths[i];
126 ret.add(elem);
127 }
128 Collections.sort(ret);
129 return ret;
130 }
131
132 private static native void setTag(Object o, long tag);
133 private static native long getTag(Object o);
134
135 private final static int HEAP_FILTER_OUT_TAGGED = 0x4;
136 private final static int HEAP_FILTER_OUT_UNTAGGED = 0x8;
137 private final static int HEAP_FILTER_OUT_CLASS_TAGGED = 0x10;
138 private final static int HEAP_FILTER_OUT_CLASS_UNTAGGED = 0x20;
139
140 private static native int iterateThroughHeapCount(int heapFilter,
141 Class<?> klassFilter, int stopAfter);
142 private static native int iterateThroughHeapData(int heapFilter,
143 Class<?> klassFilter, long classTags[], long sizes[], long tags[], int lengths[]);
144 private static native int iterateThroughHeapAdd(int heapFilter,
145 Class<?> klassFilter);
146}