blob: feb7749b1f96d03097c38bef46225abd21402a05 [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"
Elliott Hughes8cf5bc02012-02-02 16:32:16 -080024#include "thread_list.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070025
26#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
27#include "toStringArray.h"
28
29#include <limits.h>
30
31namespace art {
32
33namespace {
34
35jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) {
36 return Heap::GetTargetHeapUtilization();
37}
38
39void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) {
40 Heap::SetTargetHeapUtilization(target);
41}
42
43void VMRuntime_startJitCompilation(JNIEnv*, jobject) {
44}
45
46void VMRuntime_disableJitCompilation(JNIEnv*, jobject) {
47}
48
49jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass, jint length) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070050 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070051#ifdef MOVING_GARBAGE_COLLECTOR
52 // TODO: right now, we don't have a copying collector, so there's no need
53 // to do anything special here, but we ought to pass the non-movability
54 // through to the allocator.
55 UNIMPLEMENTED(FATAL);
56#endif
57
58 Class* element_class = Decode<Class*>(env, javaElementClass);
59 if (element_class == NULL) {
60 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "element class == null");
61 return NULL;
62 }
63 if (length < 0) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070064 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070065 return NULL;
66 }
67
68 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
69 std::string descriptor;
70 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080071 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughesc3b77c72011-12-15 20:56:48 -080072 Class* array_class = class_linker->FindClass(descriptor.c_str(), NULL);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070073 Array* result = Array::Alloc(array_class, length);
74 if (result == NULL) {
75 return NULL;
76 }
77 return AddLocalReference<jobject>(env, result);
78}
79
80jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070081 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070082 Array* array = Decode<Array*>(env, javaArray);
83 if (!array->IsArrayInstance()) {
84 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", "not an array");
85 return 0;
86 }
87 // TODO: we should also check that this is a non-movable array.
88 return reinterpret_cast<uintptr_t>(array->GetRawData());
89}
90
91void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) {
92 Heap::ClearGrowthLimit();
93}
94
95jboolean VMRuntime_isDebuggerActive(JNIEnv*, jobject) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070096 return Dbg::IsDebuggerConnected();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070097}
98
99jobjectArray VMRuntime_properties(JNIEnv* env, jobject) {
100 return toStringArray(env, Runtime::Current()->GetProperties());
101}
102
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800103// This is for backward compatibility with dalvik which returned the
104// meaningless "." when no boot classpath or classpath was
105// specified. Unfortunately, some tests were using java.class.path to
106// lookup relative file locations, so they are counting on this to be
107// ".", presumably some applications or libraries could have as well.
108const char* DefaultToDot(const std::string& class_path) {
109 return class_path.empty() ? "." : class_path.c_str();
110}
111
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700112jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) {
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800113 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetBootClassPath()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700114}
115
116jstring VMRuntime_classPath(JNIEnv* env, jobject) {
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800117 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPath()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700118}
119
120jstring VMRuntime_vmVersion(JNIEnv* env, jobject) {
121 return env->NewStringUTF(Runtime::Current()->GetVersion());
122}
123
124void VMRuntime_setTargetSdkVersion(JNIEnv* env, jobject, jint targetSdkVersion) {
125 // This is the target SDK version of the app we're about to run.
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800126 // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000).
127 // Note that targetSdkVersion may be 0, meaning "current".
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700128 if (targetSdkVersion > 0 && targetSdkVersion <= 13 /* honeycomb-mr2 */) {
129 // TODO: running with CheckJNI should override this and force you to obey the strictest rules.
130 LOG(INFO) << "Turning on JNI app bug workarounds for target SDK version " << targetSdkVersion << "...";
Brian Carlstrom49b4f072012-01-18 14:23:48 -0800131 // Runtime::Current()->GetJavaVM()->work_around_app_jni_bugs = true;
132 UNIMPLEMENTED(WARNING) << "Support work arounds for app JNI bugs";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700133 }
134}
135
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800136void VMRuntime_trimHeap(JNIEnv* env, jobject) {
137 ScopedThreadListLock thread_list_lock;
138 uint64_t start_ns = NanoTime();
139 Heap::GetAllocSpace()->Trim();
140 VLOG(gc) << "VMRuntime_trimHeap took " << PrettyDuration(NanoTime() - start_ns);
141}
142
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700143JNINativeMethod gMethods[] = {
144 NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
145 NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
146 NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
147 NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
148 NATIVE_METHOD(VMRuntime, disableJitCompilation, "()V"),
149 NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
150 NATIVE_METHOD(VMRuntime, isDebuggerActive, "()Z"),
151 NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
152 NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
153 NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
154 NATIVE_METHOD(VMRuntime, setTargetSdkVersion, "(I)V"),
155 NATIVE_METHOD(VMRuntime, startJitCompilation, "()V"),
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800156 NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700157 NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
158};
159
160} // namespace
161
162void register_dalvik_system_VMRuntime(JNIEnv* env) {
163 jniRegisterNativeMethods(env, "dalvik/system/VMRuntime", gMethods, NELEM(gMethods));
164}
165
166} // namespace art