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