1 /*
2  * Copyright (C) 2012 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 <jni.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/time.h>
21 
currentTimeMillis()22 double currentTimeMillis()
23 {
24     struct timeval tv;
25     gettimeofday(&tv, (struct timezone *) NULL);
26     return tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
27 }
28 
Java_android_dram_cts_MemoryNative_runMemcpy(JNIEnv * env,jclass clazz,jint bufferSize,jint repetition)29 extern "C" JNIEXPORT jdouble JNICALL Java_android_dram_cts_MemoryNative_runMemcpy(JNIEnv* env,
30         jclass clazz, jint bufferSize, jint repetition)
31 {
32     char* src = new char[bufferSize];
33     char* dst = new char[bufferSize];
34     if ((src == NULL) || (dst == NULL)) {
35         delete[] src;
36         delete[] dst;
37         env->ThrowNew(env->FindClass("java/lang/OutOfMemoryError"), "No memory");
38         return -1;
39     }
40     memset(src, 0, bufferSize);
41     memset(dst, 0, bufferSize);
42     double start = currentTimeMillis();
43     for (int i = 0; i < repetition; i++) {
44         memcpy(dst, src, bufferSize);
45         src[bufferSize - 1] = i & 0xff;
46     }
47     double end = currentTimeMillis();
48     delete[] src;
49     delete[] dst;
50     return end - start;
51 }
52 
Java_android_dram_cts_MemoryNative_runMemset(JNIEnv * env,jclass clazz,jint bufferSize,jint repetition,jint c)53 extern "C" JNIEXPORT jdouble JNICALL Java_android_dram_cts_MemoryNative_runMemset(JNIEnv* env,
54         jclass clazz, jint bufferSize, jint repetition, jint c)
55 {
56     char* dst = new char[bufferSize];
57     if (dst == NULL) {
58         delete[] dst;
59         env->ThrowNew(env->FindClass("java/lang/OutOfMemoryError"), "No memory");
60         return -1;
61     }
62     memset(dst, 0, bufferSize);
63     double start = currentTimeMillis();
64     for (int i = 0; i < repetition; i++) {
65         memset(dst, (c + i) & 0xff, bufferSize);
66     }
67     double end = currentTimeMillis();
68     delete[] dst;
69     return end - start;
70 }
71 
72