Home | History | Annotate | Download | only in art
      1 /*
      2  * Copyright (C) 2017 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 art;
     18 
     19 import java.util.Arrays;
     20 import java.lang.reflect.Executable;
     21 import java.lang.reflect.Method;
     22 import java.util.concurrent.Semaphore;
     23 
     24 public class Test1941 {
     25   public static final boolean PRINT_CNT = false;
     26   public static long CNT = 0;
     27 
     28   // Method with multiple paths we can break on.
     29   public static long fib(long f) {
     30     if (f < 0) {
     31       throw new IllegalArgumentException("Bad argument f < 0: f = " + f);
     32     } else if (f == 0) {
     33       return 0;
     34     } else if (f == 1) {
     35       return 1;
     36     } else {
     37       return fib(f - 1) + fib(f - 2);
     38     }
     39   }
     40 
     41   public static void notifySingleStep(Thread thr, Executable e, long loc) {
     42     // Don't bother actually doing anything.
     43   }
     44 
     45   public static void LoopAllocFreeEnv(Semaphore sem) {
     46     sem.release();
     47     while (!Thread.interrupted()) {
     48       CNT++;
     49       long env = AllocEnv();
     50       FreeEnv(env);
     51     }
     52   }
     53 
     54   public static native long AllocEnv();
     55   public static native void FreeEnv(long env);
     56 
     57   public static native void setTracingOn(Thread thr, boolean enable);
     58 
     59   public static void run() throws Exception {
     60     final Semaphore sem = new Semaphore(0);
     61     Thread thr = new Thread(() -> { LoopAllocFreeEnv(sem); }, "LoopNative");
     62     thr.start();
     63     // Make sure the other thread is actually started.
     64     sem.acquire();
     65     Trace.enableSingleStepTracing(Test1941.class,
     66         Test1941.class.getDeclaredMethod(
     67             "notifySingleStep", Thread.class, Executable.class, Long.TYPE),
     68         thr);
     69     setTracingOn(Thread.currentThread(), true);
     70 
     71     System.out.println("fib(20) is " + fib(20));
     72 
     73     thr.interrupt();
     74     thr.join();
     75     setTracingOn(Thread.currentThread(), false);
     76     Trace.disableTracing(null);
     77     if (PRINT_CNT) {
     78       System.out.println("Number of envs created/destroyed: " + CNT);
     79     }
     80   }
     81 }
     82