135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe/*
235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * Copyright (C) 2017 The Android Open Source Project
335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe *
435bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * Licensed under the Apache License, Version 2.0 (the "License");
535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * you may not use this file except in compliance with the License.
635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * You may obtain a copy of the License at
735bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe *
835bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe *      http://www.apache.org/licenses/LICENSE-2.0
935bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe *
1035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * Unless required by applicable law or agreed to in writing, software
1135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * distributed under the License is distributed on an "AS IS" BASIS,
1235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * See the License for the specific language governing permissions and
1435bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe * limitations under the License.
1535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe */
1635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
174665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampepackage art;
184665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampe
1935bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampeimport java.util.Arrays;
2035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
214665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampepublic class Test927 {
224665167ddc34008dfa78a2873685fe7a98772eabAndreas Gampe  public static void run() throws Exception {
2335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    doTest();
2435bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  }
2535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
2635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  private static void doTest() {
2735bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    int all1 = Runtime.getRuntime().availableProcessors();
2835bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    int all2 = getAvailableProcessors();
2935bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    if (all1 != all2) {
3035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe      throw new RuntimeException("Available processors doesn't match: " + all1 + " vs " + all2);
3135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    }
3235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    System.out.println("availableProcessors OK");
3335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
3435bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    Object info[] = getTimerInfo();
3535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    System.out.println(Arrays.toString(info));
3635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
3735bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    // getTime checks.
3835bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    // Note: there isn't really much to check independent from the implementation. So we check
3935bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    //       a few details of the ART implementation. This may fail on other runtimes.
4035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    long time1 = getTime();
4135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    long time2 = getTime();
4235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
4335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    // Under normal circumstances, time1 <= time2.
4435bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    if (time2 < time1) {
4535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe      throw new RuntimeException("Time unexpectedly decreased: " + time1 + " vs " + time2);
4635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    }
4735bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
4835bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    long time3 = System.nanoTime();
4935bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    long time4 = getTime();
5035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
5135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    final long MINUTE = 60l * 1000 * 1000 * 1000;
5235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    if (time4 < time3 || (time4 - time3 > MINUTE)) {
5335bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe      throw new RuntimeException("Time unexpectedly divergent: " + time3 + " vs " + time4);
5435bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    }
5535bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
5635bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe    System.out.println("Time OK");
5735bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  }
5835bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe
5935bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  private static native int getAvailableProcessors();
6035bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  private static native Object[] getTimerInfo();
6135bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe  private static native long getTime();
6235bcf817cb2dd9b641080e23b0fbb08870a45cbfAndreas Gampe}
63