Keun young Park | 4403e50 | 2012-09-19 18:11:07 -0700 | [diff] [blame] | 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 | |
Keun young Park | 1665af1 | 2012-12-17 10:57:09 -0800 | [diff] [blame] | 22 | double currentTimeMillis() |
Keun young Park | 4403e50 | 2012-09-19 18:11:07 -0700 | [diff] [blame] | 23 | { |
| 24 | struct timeval tv; |
| 25 | gettimeofday(&tv, (struct timezone *) NULL); |
Keun young Park | 1665af1 | 2012-12-17 10:57:09 -0800 | [diff] [blame] | 26 | return tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0; |
Keun young Park | 4403e50 | 2012-09-19 18:11:07 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Stuart Scott | 5d50999 | 2015-09-15 10:56:10 -0700 | [diff] [blame] | 29 | extern "C" JNIEXPORT jdouble JNICALL Java_android_dram_cts_MemoryNative_runMemcpy(JNIEnv* env, |
Keun young Park | 697f564 | 2012-11-01 10:34:23 -0700 | [diff] [blame] | 30 | jclass clazz, jint bufferSize, jint repetition) |
Keun young Park | 4403e50 | 2012-09-19 18:11:07 -0700 | [diff] [blame] | 31 | { |
Keun young Park | 697f564 | 2012-11-01 10:34:23 -0700 | [diff] [blame] | 32 | char* src = new char[bufferSize]; |
| 33 | char* dst = new char[bufferSize]; |
Keun young Park | 4403e50 | 2012-09-19 18:11:07 -0700 | [diff] [blame] | 34 | if ((src == NULL) || (dst == NULL)) { |
Keun young Park | 697f564 | 2012-11-01 10:34:23 -0700 | [diff] [blame] | 35 | delete[] src; |
| 36 | delete[] dst; |
Keun young Park | 4403e50 | 2012-09-19 18:11:07 -0700 | [diff] [blame] | 37 | env->ThrowNew(env->FindClass("java/lang/OutOfMemoryError"), "No memory"); |
Keun young Park | 697f564 | 2012-11-01 10:34:23 -0700 | [diff] [blame] | 38 | return -1; |
Keun young Park | 4403e50 | 2012-09-19 18:11:07 -0700 | [diff] [blame] | 39 | } |
| 40 | memset(src, 0, bufferSize); |
| 41 | memset(dst, 0, bufferSize); |
Keun young Park | 1665af1 | 2012-12-17 10:57:09 -0800 | [diff] [blame] | 42 | double start = currentTimeMillis(); |
Keun young Park | 4403e50 | 2012-09-19 18:11:07 -0700 | [diff] [blame] | 43 | for (int i = 0; i < repetition; i++) { |
| 44 | memcpy(dst, src, bufferSize); |
| 45 | src[bufferSize - 1] = i & 0xff; |
| 46 | } |
Keun young Park | 1665af1 | 2012-12-17 10:57:09 -0800 | [diff] [blame] | 47 | double end = currentTimeMillis(); |
Keun young Park | 697f564 | 2012-11-01 10:34:23 -0700 | [diff] [blame] | 48 | delete[] src; |
| 49 | delete[] dst; |
Keun young Park | 4403e50 | 2012-09-19 18:11:07 -0700 | [diff] [blame] | 50 | return end - start; |
| 51 | } |
Keun young Park | 50418e9 | 2012-12-17 11:34:45 -0800 | [diff] [blame] | 52 | |
Stuart Scott | 5d50999 | 2015-09-15 10:56:10 -0700 | [diff] [blame] | 53 | extern "C" JNIEXPORT jdouble JNICALL Java_android_dram_cts_MemoryNative_runMemset(JNIEnv* env, |
Keun young Park | 50418e9 | 2012-12-17 11:34:45 -0800 | [diff] [blame] | 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 | |