Home | History | Annotate | Download | only in jni
      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 #include <stdlib.h>
     18 #include <stdio.h>
     19 //#include <fcntl.h>
     20 //#include <unistd.h>
     21 #include <math.h>
     22 #include <inttypes.h>
     23 #include <time.h>
     24 #include <android/log.h>
     25 
     26 #include "jni.h"
     27 #include "Bench.h"
     28 
     29 #define FUNC(name) Java_com_android_benchmark_synthetic_TestInterface_##name
     30 
     31 static uint64_t GetTime() {
     32     struct timespec t;
     33     clock_gettime(CLOCK_MONOTONIC, &t);
     34     return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
     35 }
     36 
     37 extern "C" {
     38 
     39 jlong Java_com_android_benchmark_synthetic_TestInterface_nInit(JNIEnv *_env, jobject _this, jlong options) {
     40     Bench *b = new Bench();
     41     bool ret = b->init();
     42 
     43     if (ret) {
     44         return (jlong)b;
     45     }
     46 
     47     delete b;
     48     return 0;
     49 }
     50 
     51 void Java_com_android_benchmark_synthetic_TestInterface_nDestroy(JNIEnv *_env, jobject _this, jlong _b) {
     52     Bench *b = (Bench *)_b;
     53 
     54     delete b;
     55 }
     56 
     57 jboolean Java_com_android_benchmark_synthetic_TestInterface_nRunPowerManagementTest(
     58         JNIEnv *_env, jobject _this, jlong _b, jlong options) {
     59     Bench *b = (Bench *)_b;
     60     return b->runPowerManagementTest(options);
     61 }
     62 
     63 jboolean Java_com_android_benchmark_synthetic_TestInterface_nRunCPUHeatSoakTest(
     64         JNIEnv *_env, jobject _this, jlong _b, jlong options) {
     65     Bench *b = (Bench *)_b;
     66     return b->runCPUHeatSoak(options);
     67 }
     68 
     69 float Java_com_android_benchmark_synthetic_TestInterface_nGetData(
     70         JNIEnv *_env, jobject _this, jlong _b, jfloatArray data) {
     71     Bench *b = (Bench *)_b;
     72 
     73     jsize len = _env->GetArrayLength(data);
     74     float * ptr = _env->GetFloatArrayElements(data, 0);
     75 
     76     b->getData(ptr, len);
     77 
     78     _env->ReleaseFloatArrayElements(data, (jfloat *)ptr, 0);
     79 
     80     return 0;
     81 }
     82 
     83 jboolean Java_com_android_benchmark_synthetic_TestInterface_nMemTestStart(
     84         JNIEnv *_env, jobject _this, jlong _b) {
     85     Bench *b = (Bench *)_b;
     86     return b->startMemTests();
     87 }
     88 
     89 float Java_com_android_benchmark_synthetic_TestInterface_nMemTestBandwidth(
     90         JNIEnv *_env, jobject _this, jlong _b, jlong opt) {
     91     Bench *b = (Bench *)_b;
     92     return b->runMemoryBandwidthTest(opt);
     93 }
     94 
     95 float Java_com_android_benchmark_synthetic_TestInterface_nGFlopsTest(
     96         JNIEnv *_env, jobject _this, jlong _b, jlong opt) {
     97     Bench *b = (Bench *)_b;
     98     return b->runGFlopsTest(opt);
     99 }
    100 
    101 float Java_com_android_benchmark_synthetic_TestInterface_nMemTestLatency(
    102         JNIEnv *_env, jobject _this, jlong _b, jlong opt) {
    103     Bench *b = (Bench *)_b;
    104     return b->runMemoryLatencyTest(opt);
    105 }
    106 
    107 void Java_com_android_benchmark_synthetic_TestInterface_nMemTestEnd(
    108         JNIEnv *_env, jobject _this, jlong _b) {
    109     Bench *b = (Bench *)_b;
    110     b->endMemTests();
    111 }
    112 
    113 float Java_com_android_benchmark_synthetic_TestInterface_nMemoryTest(
    114         JNIEnv *_env, jobject _this, jint subtest) {
    115 
    116     uint8_t * volatile m1 = (uint8_t *)malloc(1024*1024*64);
    117     uint8_t * m2 = (uint8_t *)malloc(1024*1024*64);
    118 
    119     memset(m1, 0, 1024*1024*16);
    120     memset(m2, 0, 1024*1024*16);
    121 
    122     //__android_log_print(ANDROID_LOG_INFO, "bench", "test %i  %p  %p", subtest, m1, m2);
    123 
    124 
    125     size_t loopCount = 0;
    126     uint64_t start = GetTime();
    127     while((GetTime() - start) < 1000000000) {
    128         memcpy(m1, m2, subtest);
    129         loopCount++;
    130     }
    131     if (loopCount == 0) {
    132         loopCount = 1;
    133     }
    134 
    135     size_t count = loopCount;
    136     uint64_t t1 = GetTime();
    137     while (loopCount > 0) {
    138         memcpy(m1, m2, subtest);
    139         loopCount--;
    140     }
    141     uint64_t t2 = GetTime();
    142 
    143     double dt = t2 - t1;
    144     dt /= 1000 * 1000 * 1000;
    145     double bw = ((double)subtest) * count / dt;
    146 
    147     bw /= 1024 * 1024 * 1024;
    148 
    149     __android_log_print(ANDROID_LOG_INFO, "bench", "size %i, bw %f", subtest, bw);
    150 
    151     free (m1);
    152     free (m2);
    153     return (float)bw;
    154 }
    155 
    156 jlong Java_com_android_benchmark_synthetic_MemoryAvailableLoad1_nMemTestMalloc(
    157         JNIEnv *_env, jobject _this, jint bytes) {
    158     uint8_t *p = (uint8_t *)malloc(bytes);
    159     memset(p, 0, bytes);
    160     return (jlong)p;
    161 }
    162 
    163 void Java_com_android_benchmark_synthetic_MemoryAvailableLoad1_nMemTestFree(
    164         JNIEnv *_env, jobject _this, jlong ptr) {
    165     free((void *)ptr);
    166 }
    167 
    168 jlong Java_com_android_benchmark_synthetic_MemoryAvailableLoad2_nMemTestMalloc(
    169         JNIEnv *_env, jobject _this, jint bytes) {
    170     return Java_com_android_benchmark_synthetic_MemoryAvailableLoad1_nMemTestMalloc(_env, _this, bytes);
    171 }
    172 
    173 void Java_com_android_benchmark_synthetic_MemoryAvailableLoad2_nMemTestFree(
    174         JNIEnv *_env, jobject _this, jlong ptr) {
    175     Java_com_android_benchmark_synthetic_MemoryAvailableLoad1_nMemTestFree(_env, _this, ptr);
    176 }
    177 
    178 }; // extern "C"
    179