Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | import java.io.File; |
| 18 | import java.io.IOException; |
| 19 | import java.lang.reflect.Method; |
| 20 | import java.util.concurrent.ConcurrentSkipListMap; |
| 21 | import java.util.HashMap; |
| 22 | import java.util.HashSet; |
| 23 | import java.util.LinkedHashMap; |
| 24 | import java.util.LinkedHashSet; |
| 25 | import java.util.Map; |
| 26 | import java.util.Set; |
| 27 | import java.util.TreeMap; |
| 28 | import java.util.TreeSet; |
| 29 | |
| 30 | public class Main { |
| 31 | private static final String TEMP_FILE_NAME_PREFIX = "test"; |
| 32 | private static final String TEMP_FILE_NAME_SUFFIX = ".trace"; |
| 33 | private static File file; |
| 34 | |
| 35 | public static void main(String[] args) throws Exception { |
| 36 | String name = System.getProperty("java.vm.name"); |
| 37 | if (!"Dalvik".equals(name)) { |
| 38 | System.out.println("This test is not supported on " + name); |
| 39 | return; |
| 40 | } |
| 41 | file = createTempFile(); |
| 42 | try { |
| 43 | new Main().ensureCaller(true, 0); |
| 44 | new Main().ensureCaller(false, 0); |
| 45 | } finally { |
| 46 | if (file != null) { |
| 47 | file.delete(); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private static File createTempFile() throws Exception { |
| 53 | try { |
| 54 | return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); |
| 55 | } catch (IOException e) { |
| 56 | System.setProperty("java.io.tmpdir", "/data/local/tmp"); |
| 57 | try { |
| 58 | return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); |
| 59 | } catch (IOException e2) { |
| 60 | System.setProperty("java.io.tmpdir", "/sdcard"); |
| 61 | return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // We make sure 'doLoadsOfStuff' has a caller, because it is this caller that will be |
| 67 | // pushed in the side instrumentation frame. |
| 68 | public void ensureCaller(boolean warmup, int invocationCount) throws Exception { |
| 69 | doLoadsOfStuff(warmup, invocationCount); |
| 70 | } |
| 71 | |
| 72 | // The number of recursive calls we are going to do in 'doLoadsOfStuff' to ensure |
| 73 | // the JIT sees it hot. |
| 74 | static final int NUMBER_OF_INVOCATIONS = 5; |
| 75 | |
| 76 | public void doLoadsOfStuff(boolean warmup, int invocationCount) throws Exception { |
| 77 | // Warmup is to make sure the JIT gets a chance to compile 'doLoadsOfStuff'. |
| 78 | if (warmup) { |
| 79 | if (invocationCount < NUMBER_OF_INVOCATIONS) { |
| 80 | doLoadsOfStuff(warmup, ++invocationCount); |
| 81 | } else { |
| 82 | // Give the JIT a chance to compiler. |
| 83 | Thread.sleep(1000); |
| 84 | } |
| 85 | } else { |
| 86 | if (invocationCount == 0) { |
| 87 | VMDebug.startMethodTracing(file.getPath(), 0, 0, false, 0); |
| 88 | } |
| 89 | fillJit(); |
| 90 | if (invocationCount < NUMBER_OF_INVOCATIONS) { |
| 91 | doLoadsOfStuff(warmup, ++invocationCount); |
| 92 | } else { |
| 93 | VMDebug.stopMethodTracing(); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // This method creates enough profiling data to fill the code cache and trigger |
| 99 | // a collection in debug mode (at the time of the test 10KB of data space). We |
| 100 | // used to crash by not looking at the instrumentation stack and deleting JIT code |
| 101 | // that will be later restored by the instrumentation. |
| 102 | public static void fillJit() throws Exception { |
| 103 | Map map = new HashMap(); |
| 104 | map.put("foo", "bar"); |
| 105 | map.clear(); |
| 106 | map.containsKey("foo"); |
| 107 | map.containsValue("foo"); |
| 108 | map.entrySet(); |
| 109 | map.equals(map); |
| 110 | map.hashCode(); |
| 111 | map.isEmpty(); |
| 112 | map.keySet(); |
| 113 | map.putAll(map); |
| 114 | map.remove("foo"); |
| 115 | map.size(); |
| 116 | map.put("bar", "foo"); |
| 117 | map.values(); |
| 118 | |
| 119 | map = new LinkedHashMap(); |
| 120 | map.put("foo", "bar"); |
| 121 | map.clear(); |
| 122 | map.containsKey("foo"); |
| 123 | map.containsValue("foo"); |
| 124 | map.entrySet(); |
| 125 | map.equals(map); |
| 126 | map.hashCode(); |
| 127 | map.isEmpty(); |
| 128 | map.keySet(); |
| 129 | map.putAll(map); |
| 130 | map.remove("foo"); |
| 131 | map.size(); |
| 132 | map.put("bar", "foo"); |
| 133 | map.values(); |
| 134 | |
| 135 | map = new TreeMap(); |
| 136 | map.put("foo", "bar"); |
| 137 | map.clear(); |
| 138 | map.containsKey("foo"); |
| 139 | map.containsValue("foo"); |
| 140 | map.entrySet(); |
| 141 | map.equals(map); |
| 142 | map.hashCode(); |
| 143 | map.isEmpty(); |
| 144 | map.keySet(); |
| 145 | map.putAll(map); |
| 146 | map.remove("foo"); |
| 147 | map.size(); |
| 148 | map.put("bar", "foo"); |
| 149 | map.values(); |
| 150 | |
| 151 | map = new ConcurrentSkipListMap(); |
| 152 | map.put("foo", "bar"); |
| 153 | map.clear(); |
| 154 | map.containsKey("foo"); |
| 155 | map.containsValue("foo"); |
| 156 | map.entrySet(); |
| 157 | map.equals(map); |
| 158 | map.hashCode(); |
| 159 | map.isEmpty(); |
| 160 | map.keySet(); |
| 161 | map.putAll(map); |
| 162 | map.remove("foo"); |
| 163 | map.size(); |
| 164 | map.put("bar", "foo"); |
| 165 | map.values(); |
| 166 | |
| 167 | Set set = new HashSet(); |
| 168 | set.add("foo"); |
| 169 | set.addAll(set); |
| 170 | set.clear(); |
| 171 | set.contains("foo"); |
| 172 | set.containsAll(set); |
| 173 | set.equals(set); |
| 174 | set.hashCode(); |
| 175 | set.isEmpty(); |
| 176 | set.iterator(); |
| 177 | set.remove("foo"); |
| 178 | set.removeAll(set); |
| 179 | set.retainAll(set); |
| 180 | set.size(); |
| 181 | set.add("foo"); |
| 182 | set.toArray(); |
| 183 | |
| 184 | set = new LinkedHashSet(); |
| 185 | set.add("foo"); |
| 186 | set.addAll(set); |
| 187 | set.clear(); |
| 188 | set.contains("foo"); |
| 189 | set.containsAll(set); |
| 190 | set.equals(set); |
| 191 | set.hashCode(); |
| 192 | set.isEmpty(); |
| 193 | set.iterator(); |
| 194 | set.remove("foo"); |
| 195 | set.removeAll(set); |
| 196 | set.retainAll(set); |
| 197 | set.size(); |
| 198 | set.add("foo"); |
| 199 | set.toArray(); |
| 200 | |
| 201 | set = new TreeSet(); |
| 202 | set.add("foo"); |
| 203 | set.addAll(set); |
| 204 | set.clear(); |
| 205 | set.contains("foo"); |
| 206 | set.containsAll(set); |
| 207 | set.equals(set); |
| 208 | set.hashCode(); |
| 209 | set.isEmpty(); |
| 210 | set.iterator(); |
| 211 | set.remove("foo"); |
| 212 | set.removeAll(set); |
| 213 | set.retainAll(set); |
| 214 | set.size(); |
| 215 | set.add("foo"); |
| 216 | set.toArray(); |
| 217 | } |
| 218 | |
| 219 | private static class VMDebug { |
| 220 | private static final Method startMethodTracingMethod; |
| 221 | private static final Method stopMethodTracingMethod; |
| 222 | static { |
| 223 | try { |
| 224 | Class c = Class.forName("dalvik.system.VMDebug"); |
| 225 | startMethodTracingMethod = c.getDeclaredMethod("startMethodTracing", String.class, |
| 226 | Integer.TYPE, Integer.TYPE, Boolean.TYPE, Integer.TYPE); |
| 227 | stopMethodTracingMethod = c.getDeclaredMethod("stopMethodTracing"); |
| 228 | } catch (Exception e) { |
| 229 | throw new RuntimeException(e); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | public static void startMethodTracing(String filename, int bufferSize, int flags, |
| 234 | boolean samplingEnabled, int intervalUs) throws Exception { |
| 235 | startMethodTracingMethod.invoke(null, filename, bufferSize, flags, samplingEnabled, |
| 236 | intervalUs); |
| 237 | } |
| 238 | public static void stopMethodTracing() throws Exception { |
| 239 | stopMethodTracingMethod.invoke(null); |
| 240 | } |
| 241 | } |
| 242 | } |