1 /* 2 * Copyright (C) 2006 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 package android.os; 18 19 import android.platform.test.annotations.IgnoreUnderRavenwood; 20 import android.platform.test.ravenwood.RavenwoodRule; 21 import android.util.Log; 22 23 import androidx.test.ext.junit.runners.AndroidJUnit4; 24 import androidx.test.filters.LargeTest; 25 import androidx.test.filters.SmallTest; 26 import androidx.test.filters.Suppress; 27 28 import org.junit.Rule; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 /** 33 * This class is used to test the native tracing support. Run this test 34 * while tracing on the emulator and then run traceview to view the trace. 35 */ 36 @RunWith(AndroidJUnit4.class) 37 public class TraceTest { 38 private static final String TAG = "TraceTest"; 39 40 @Rule 41 public final RavenwoodRule mRavenwood = new RavenwoodRule(); 42 43 private int eMethodCalls = 0; 44 private int fMethodCalls = 0; 45 private int gMethodCalls = 0; 46 47 @Test testEnableDisable()48 public void testEnableDisable() { 49 // Currently only verifying that we can invoke without crashing 50 Trace.setTracingEnabled(true, 0); 51 Trace.setTracingEnabled(false, 0); 52 53 Trace.setAppTracingAllowed(true); 54 Trace.setAppTracingAllowed(false); 55 } 56 57 @Test testBeginEnd()58 public void testBeginEnd() { 59 // Currently only verifying that we can invoke without crashing 60 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, TAG); 61 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); 62 63 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, TAG, 42); 64 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, TAG, 42); 65 66 Trace.asyncTraceForTrackBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, TAG, TAG, 42); 67 Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, TAG, 42); 68 69 Trace.beginSection(TAG); 70 Trace.endSection(); 71 72 Trace.beginAsyncSection(TAG, 42); 73 Trace.endAsyncSection(TAG, 42); 74 } 75 76 @Test testCounter()77 public void testCounter() { 78 // Currently only verifying that we can invoke without crashing 79 Trace.traceCounter(Trace.TRACE_TAG_ACTIVITY_MANAGER, TAG, 42); 80 Trace.setCounter(TAG, 42); 81 } 82 83 @Test testInstant()84 public void testInstant() { 85 // Currently only verifying that we can invoke without crashing 86 Trace.instant(Trace.TRACE_TAG_ACTIVITY_MANAGER, TAG); 87 Trace.instantForTrack(Trace.TRACE_TAG_ACTIVITY_MANAGER, TAG, TAG); 88 } 89 90 @Test testNullStrings()91 public void testNullStrings() { 92 // Currently only verifying that we can invoke without crashing 93 Trace.traceCounter(Trace.TRACE_TAG_ACTIVITY_MANAGER, null, 42); 94 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, null); 95 96 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, null, 42); 97 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, null, 42); 98 99 Trace.asyncTraceForTrackBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, null, null, 42); 100 Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, null, 42); 101 102 Trace.instant(Trace.TRACE_TAG_ACTIVITY_MANAGER, null); 103 Trace.instantForTrack(Trace.TRACE_TAG_ACTIVITY_MANAGER, null, null); 104 } 105 106 @Test 107 @SmallTest 108 @IgnoreUnderRavenwood(blockedBy = Debug.class) testNativeTracingFromJava()109 public void testNativeTracingFromJava() 110 { 111 long start = System.currentTimeMillis(); 112 Debug.startNativeTracing(); 113 //nativeMethod(); 114 int count = 0; 115 for (int ii = 0; ii < 20; ii++) { 116 count = eMethod(); 117 } 118 Debug.stopNativeTracing(); 119 long end = System.currentTimeMillis(); 120 long elapsed = end - start; 121 Log.i(TAG, "elapsed millis: " + elapsed); 122 Log.i(TAG, "eMethod calls: " + eMethodCalls 123 + " fMethod calls: " + fMethodCalls 124 + " gMethod calls: " + gMethodCalls); 125 } 126 127 // This should not run in the automated suite. 128 @Suppress 129 @IgnoreUnderRavenwood(blockedBy = Debug.class) disableTestNativeTracingFromC()130 public void disableTestNativeTracingFromC() 131 { 132 long start = System.currentTimeMillis(); 133 nativeMethodAndStartTracing(); 134 long end = System.currentTimeMillis(); 135 long elapsed = end - start; 136 Log.i(TAG, "elapsed millis: " + elapsed); 137 } 138 nativeMethod()139 native void nativeMethod(); nativeMethodAndStartTracing()140 native void nativeMethodAndStartTracing(); 141 142 @Test 143 @LargeTest 144 @Suppress // Failing. 145 @IgnoreUnderRavenwood(blockedBy = Debug.class) testMethodTracing()146 public void testMethodTracing() 147 { 148 long start = System.currentTimeMillis(); 149 Debug.startMethodTracing("traceTest"); 150 topMethod(); 151 Debug.stopMethodTracing(); 152 long end = System.currentTimeMillis(); 153 long elapsed = end - start; 154 Log.i(TAG, "elapsed millis: " + elapsed); 155 } 156 topMethod()157 private void topMethod() { 158 aMethod(); 159 bMethod(); 160 cMethod(); 161 dMethod(5); 162 163 Thread t1 = new aThread(); 164 t1.start(); 165 Thread t2 = new aThread(); 166 t2.start(); 167 Thread t3 = new aThread(); 168 t3.start(); 169 try { 170 t1.join(); 171 t2.join(); 172 t3.join(); 173 } catch (InterruptedException e) { 174 } 175 } 176 177 private class aThread extends Thread { 178 @Override run()179 public void run() { 180 aMethod(); 181 bMethod(); 182 cMethod(); 183 } 184 } 185 186 /** Calls other methods to make some interesting trace data. 187 * 188 * @return a meaningless value 189 */ aMethod()190 private int aMethod() { 191 int count = 0; 192 for (int ii = 0; ii < 6; ii++) { 193 count += bMethod(); 194 } 195 for (int ii = 0; ii < 5; ii++) { 196 count += cMethod(); 197 } 198 for (int ii = 0; ii < 4; ii++) { 199 count += dMethod(ii); 200 } 201 return count; 202 } 203 204 /** Calls another method to make some interesting trace data. 205 * 206 * @return a meaningless value 207 */ bMethod()208 private int bMethod() { 209 int count = 0; 210 for (int ii = 0; ii < 4; ii++) { 211 count += cMethod(); 212 } 213 return count; 214 } 215 216 /** Executes a simple loop to make some interesting trace data. 217 * 218 * @return a meaningless value 219 */ cMethod()220 private int cMethod() { 221 int count = 0; 222 for (int ii = 0; ii < 1000; ii++) { 223 count += ii; 224 } 225 return count; 226 } 227 228 /** Calls itself recursively to make some interesting trace data. 229 * 230 * @return a meaningless value 231 */ dMethod(int level)232 private int dMethod(int level) { 233 int count = 0; 234 if (level > 0) { 235 count = dMethod(level - 1); 236 } 237 for (int ii = 0; ii < 100; ii++) { 238 count += ii; 239 } 240 if (level == 0) { 241 return count; 242 } 243 return dMethod(level - 1); 244 } 245 eMethod()246 public int eMethod() { 247 eMethodCalls += 1; 248 int count = fMethod(); 249 count += gMethod(3); 250 return count; 251 } 252 fMethod()253 public int fMethod() { 254 fMethodCalls += 1; 255 int count = 0; 256 for (int ii = 0; ii < 10; ii++) { 257 count += ii; 258 } 259 return count; 260 } 261 gMethod(int level)262 public int gMethod(int level) { 263 gMethodCalls += 1; 264 int count = level; 265 if (level > 1) 266 count += gMethod(level - 1); 267 return count; 268 } 269 270 /* 271 * This causes the native shared library to be loaded when the 272 * class is first used. The library is only loaded once, even if 273 * multiple classes include this line. 274 * 275 * The library must be in java.library.path, which is derived from 276 * LD_LIBRARY_PATH. The actual library name searched for will be 277 * "libtrace_test.so" under Linux, but may be different on other 278 * platforms. 279 */ 280 static { Log.i(TAG, "Loading trace_test native library...")281 Log.i(TAG, "Loading trace_test native library..."); 282 try { 283 System.loadLibrary("trace_test"); Log.i(TAG, "Successfully loaded trace_test native library")284 Log.i(TAG, "Successfully loaded trace_test native library"); 285 } 286 catch (UnsatisfiedLinkError ule) { 287 Log.w(TAG, "Could not load trace_test native library"); 288 } 289 } 290 } 291