blob: c8a3a4c9aaf009dcbd925b18a788fe29b73576df [file] [log] [blame]
Elliott Hughes7ede61e2011-09-14 18:18:06 -07001/*
2 * Copyright (C) 2008 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 "class_linker.h"
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070018#include "debugger.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070019#include "jni_internal.h"
20#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "object_utils.h"
Elliott Hughes8cf5bc02012-02-02 16:32:16 -080022#include "space.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070023#include "thread.h"
24
25#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
26#include "toStringArray.h"
27
28#include <limits.h>
29
30namespace art {
31
32namespace {
33
34jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) {
35 return Heap::GetTargetHeapUtilization();
36}
37
38void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) {
39 Heap::SetTargetHeapUtilization(target);
40}
41
42void VMRuntime_startJitCompilation(JNIEnv*, jobject) {
43}
44
45void VMRuntime_disableJitCompilation(JNIEnv*, jobject) {
46}
47
48jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass, jint length) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070049 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070050#ifdef MOVING_GARBAGE_COLLECTOR
51 // TODO: right now, we don't have a copying collector, so there's no need
52 // to do anything special here, but we ought to pass the non-movability
53 // through to the allocator.
54 UNIMPLEMENTED(FATAL);
55#endif
56
57 Class* element_class = Decode<Class*>(env, javaElementClass);
58 if (element_class == NULL) {
59 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "element class == null");
60 return NULL;
61 }
62 if (length < 0) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070063 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070064 return NULL;
65 }
66
67 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
68 std::string descriptor;
69 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080070 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughesc3b77c72011-12-15 20:56:48 -080071 Class* array_class = class_linker->FindClass(descriptor.c_str(), NULL);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070072 Array* result = Array::Alloc(array_class, length);
73 if (result == NULL) {
74 return NULL;
75 }
76 return AddLocalReference<jobject>(env, result);
77}
78
79jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) {
Ian Rogersa15e67d2012-02-28 13:51:55 -080080 if (javaArray == NULL) { // Most likely allocation failed
81 return 0;
82 }
Brian Carlstromb82b6872011-10-26 17:18:07 -070083 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070084 Array* array = Decode<Array*>(env, javaArray);
85 if (!array->IsArrayInstance()) {
86 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", "not an array");
87 return 0;
88 }
89 // TODO: we should also check that this is a non-movable array.
Ian Rogersa15e67d2012-02-28 13:51:55 -080090 return reinterpret_cast<uintptr_t>(array->GetRawData(array->GetClass()->GetComponentSize()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -070091}
92
93void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) {
94 Heap::ClearGrowthLimit();
95}
96
97jboolean VMRuntime_isDebuggerActive(JNIEnv*, jobject) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070098 return Dbg::IsDebuggerConnected();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070099}
100
101jobjectArray VMRuntime_properties(JNIEnv* env, jobject) {
102 return toStringArray(env, Runtime::Current()->GetProperties());
103}
104
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800105// This is for backward compatibility with dalvik which returned the
106// meaningless "." when no boot classpath or classpath was
107// specified. Unfortunately, some tests were using java.class.path to
108// lookup relative file locations, so they are counting on this to be
109// ".", presumably some applications or libraries could have as well.
110const char* DefaultToDot(const std::string& class_path) {
111 return class_path.empty() ? "." : class_path.c_str();
112}
113
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700114jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800115 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetBootClassPathString()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700116}
117
118jstring VMRuntime_classPath(JNIEnv* env, jobject) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800119 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPathString()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700120}
121
122jstring VMRuntime_vmVersion(JNIEnv* env, jobject) {
123 return env->NewStringUTF(Runtime::Current()->GetVersion());
124}
125
126void VMRuntime_setTargetSdkVersion(JNIEnv* env, jobject, jint targetSdkVersion) {
127 // This is the target SDK version of the app we're about to run.
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800128 // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000).
129 // Note that targetSdkVersion may be 0, meaning "current".
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700130 if (targetSdkVersion > 0 && targetSdkVersion <= 13 /* honeycomb-mr2 */) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800131 JNIEnvExt* env_ext = reinterpret_cast<JNIEnvExt*>(env);
Ian Rogers475a6442012-02-21 15:39:39 -0800132 // running with CheckJNI forces you to obey the strictest rules.
133 if (!env_ext->check_jni) {
134 LOG(INFO) << "Turning on JNI app bug workarounds for target SDK version "
135 << targetSdkVersion << "...";
136 env_ext->vm->work_around_app_jni_bugs = true;
137 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700138 }
139}
140
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800141void VMRuntime_trimHeap(JNIEnv* env, jobject) {
Elliott Hughes37be8fb2012-02-03 09:34:29 -0800142 ScopedHeapLock heap_lock;
Elliott Hughes0d0ba692012-02-03 17:28:52 -0800143 size_t alloc_space_size = Heap::GetAllocSpace()->Size();
144 float utilization = static_cast<float>(Heap::GetBytesAllocated()) / alloc_space_size;
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800145 uint64_t start_ns = NanoTime();
146 Heap::GetAllocSpace()->Trim();
Elliott Hughes0d0ba692012-02-03 17:28:52 -0800147 LOG(INFO) << "Parallel heap trimming took " << PrettyDuration(NanoTime() - start_ns)
148 << " on a " << PrettySize(alloc_space_size)
149 << " heap with " << static_cast<int>(100 * utilization) << "% utilization";
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800150}
151
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700152JNINativeMethod gMethods[] = {
153 NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
154 NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
155 NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
156 NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
157 NATIVE_METHOD(VMRuntime, disableJitCompilation, "()V"),
158 NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
159 NATIVE_METHOD(VMRuntime, isDebuggerActive, "()Z"),
160 NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
161 NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
162 NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
163 NATIVE_METHOD(VMRuntime, setTargetSdkVersion, "(I)V"),
164 NATIVE_METHOD(VMRuntime, startJitCompilation, "()V"),
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800165 NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700166 NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
167};
168
169} // namespace
170
171void register_dalvik_system_VMRuntime(JNIEnv* env) {
172 jniRegisterNativeMethods(env, "dalvik/system/VMRuntime", gMethods, NELEM(gMethods));
173}
174
175} // namespace art