blob: b0f67c4c2ed947c89a4fbdae5ffee10767cb26cb [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
Elliott Hugheseac76672012-05-24 21:56:51 -070017#include <limits.h>
18
Elliott Hughes7ede61e2011-09-14 18:18:06 -070019#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080020#include "common_throws.h"
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070021#include "debugger.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "gc/allocator/dlmalloc.h"
Mathieu Chartier82353312013-07-18 10:47:51 -070024#include "gc/heap.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070025#include "gc/space/dlmalloc_space.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070026#include "jni_internal.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/object.h"
29#include "mirror/object-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080030#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031#include "scoped_thread_state_change.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070032#include "thread.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070033#include "thread_list.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070034#include "toStringArray.h"
35
Elliott Hughes7ede61e2011-09-14 18:18:06 -070036namespace art {
37
Elliott Hughes0512f022012-03-15 22:10:52 -070038static jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080039 return Runtime::Current()->GetHeap()->GetTargetHeapUtilization();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070040}
41
Elliott Hughes0512f022012-03-15 22:10:52 -070042static void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080043 Runtime::Current()->GetHeap()->SetTargetHeapUtilization(target);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070044}
45
Elliott Hughes0512f022012-03-15 22:10:52 -070046static void VMRuntime_startJitCompilation(JNIEnv*, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -070047}
48
Elliott Hughes0512f022012-03-15 22:10:52 -070049static void VMRuntime_disableJitCompilation(JNIEnv*, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -070050}
51
Elliott Hughes0512f022012-03-15 22:10:52 -070052static jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass, jint length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053 ScopedObjectAccess soa(env);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070054#ifdef MOVING_GARBAGE_COLLECTOR
55 // TODO: right now, we don't have a copying collector, so there's no need
56 // to do anything special here, but we ought to pass the non-movability
57 // through to the allocator.
58 UNIMPLEMENTED(FATAL);
59#endif
60
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 mirror::Class* element_class = soa.Decode<mirror::Class*>(javaElementClass);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070062 if (element_class == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -080063 ThrowNullPointerException(NULL, "element class == null");
Elliott Hughes7ede61e2011-09-14 18:18:06 -070064 return NULL;
65 }
66 if (length < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -080067 ThrowNegativeArraySizeException(length);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070068 return NULL;
69 }
70
71 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
72 std::string descriptor;
73 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080074 descriptor += ClassHelper(element_class).GetDescriptor();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075 mirror::Class* array_class = class_linker->FindClass(descriptor.c_str(), NULL);
76 mirror::Array* result = mirror::Array::Alloc(soa.Self(), array_class, length);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070077 if (result == NULL) {
78 return NULL;
79 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070080 return soa.AddLocalReference<jobject>(result);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070081}
82
Elliott Hughes0512f022012-03-15 22:10:52 -070083static jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) {
Ian Rogersa15e67d2012-02-28 13:51:55 -080084 if (javaArray == NULL) { // Most likely allocation failed
85 return 0;
86 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088 mirror::Array* array = soa.Decode<mirror::Array*>(javaArray);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070089 if (!array->IsArrayInstance()) {
Ian Rogers62d6c772013-02-27 08:32:07 -080090 ThrowIllegalArgumentException(NULL, "not an array");
Elliott Hughes7ede61e2011-09-14 18:18:06 -070091 return 0;
92 }
93 // TODO: we should also check that this is a non-movable array.
Ian Rogersa15e67d2012-02-28 13:51:55 -080094 return reinterpret_cast<uintptr_t>(array->GetRawData(array->GetClass()->GetComponentSize()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -070095}
96
Elliott Hughes0512f022012-03-15 22:10:52 -070097static void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080098 Runtime::Current()->GetHeap()->ClearGrowthLimit();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070099}
100
Elliott Hughes0512f022012-03-15 22:10:52 -0700101static jboolean VMRuntime_isDebuggerActive(JNIEnv*, jobject) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700102 return Dbg::IsDebuggerActive();
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700103}
104
Elliott Hughes0512f022012-03-15 22:10:52 -0700105static jobjectArray VMRuntime_properties(JNIEnv* env, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700106 return toStringArray(env, Runtime::Current()->GetProperties());
107}
108
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800109// This is for backward compatibility with dalvik which returned the
110// meaningless "." when no boot classpath or classpath was
111// specified. Unfortunately, some tests were using java.class.path to
112// lookup relative file locations, so they are counting on this to be
113// ".", presumably some applications or libraries could have as well.
Elliott Hughes0512f022012-03-15 22:10:52 -0700114static const char* DefaultToDot(const std::string& class_path) {
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800115 return class_path.empty() ? "." : class_path.c_str();
116}
117
Elliott Hughes0512f022012-03-15 22:10:52 -0700118static jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800119 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetBootClassPathString()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700120}
121
Elliott Hughes0512f022012-03-15 22:10:52 -0700122static jstring VMRuntime_classPath(JNIEnv* env, jobject) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800123 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPathString()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700124}
125
Elliott Hughes0512f022012-03-15 22:10:52 -0700126static jstring VMRuntime_vmVersion(JNIEnv* env, jobject) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700127 return env->NewStringUTF(Runtime::Current()->GetVersion());
128}
129
Brian Carlstromcef450c2013-06-28 14:38:26 -0700130static jstring VMRuntime_vmLibrary(JNIEnv* env, jobject) {
131 return env->NewStringUTF(kIsDebugBuild ? "libartd.so" : "libart.so");
132}
133
Ian Rogers50b35e22012-10-04 10:09:15 -0700134static void VMRuntime_setTargetSdkVersion(JNIEnv* env, jobject, jint targetSdkVersion) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700135 // This is the target SDK version of the app we're about to run.
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800136 // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000).
137 // Note that targetSdkVersion may be 0, meaning "current".
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700138 if (targetSdkVersion > 0 && targetSdkVersion <= 13 /* honeycomb-mr2 */) {
Elliott Hughes88c5c352012-03-15 18:49:48 -0700139 Runtime* runtime = Runtime::Current();
140 JavaVMExt* vm = runtime->GetJavaVM();
Elliott Hughes88c5c352012-03-15 18:49:48 -0700141 if (vm->check_jni) {
Ian Rogers1a4d6d82013-08-13 20:07:05 -0700142 LOG(INFO) << "CheckJNI enabled: not enabling JNI app bug workarounds.";
143 } else {
144 LOG(INFO) << "Turning on JNI app bug workarounds for target SDK version "
145 << targetSdkVersion << "...";
146
147 vm->work_around_app_jni_bugs = true;
Ian Rogers475a6442012-02-21 15:39:39 -0800148 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700149 }
150}
151
Mathieu Chartier987ccff2013-07-08 11:05:21 -0700152static void VMRuntime_registerNativeAllocation(JNIEnv* env, jobject, jint bytes) {
153 ScopedObjectAccess soa(env);
154 if (bytes < 0) {
155 ThrowRuntimeException("allocation size negative %d", bytes);
156 return;
157 }
158 Runtime::Current()->GetHeap()->RegisterNativeAllocation(bytes);
159}
160
161static void VMRuntime_registerNativeFree(JNIEnv* env, jobject, jint bytes) {
162 ScopedObjectAccess soa(env);
163 if (bytes < 0) {
164 ThrowRuntimeException("allocation size negative %d", bytes);
165 return;
166 }
167 Runtime::Current()->GetHeap()->RegisterNativeFree(bytes);
168}
169
Mathieu Chartier3056d0c2012-10-19 10:49:56 -0700170static void VMRuntime_trimHeap(JNIEnv*, jobject) {
Ian Rogers48931882013-01-22 14:35:16 -0800171 uint64_t start_ns = NanoTime();
172
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700173 // Trim the managed heap.
Ian Rogers1d54e732013-05-02 21:10:01 -0700174 gc::Heap* heap = Runtime::Current()->GetHeap();
175 gc::space::DlMallocSpace* alloc_space = heap->GetAllocSpace();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700176 size_t alloc_space_size = alloc_space->Size();
Ian Rogers48931882013-01-22 14:35:16 -0800177 float managed_utilization =
Ian Rogers1d54e732013-05-02 21:10:01 -0700178 static_cast<float>(alloc_space->GetBytesAllocated()) / alloc_space_size;
Ian Rogers48931882013-01-22 14:35:16 -0800179 size_t managed_reclaimed = heap->Trim();
180
181 uint64_t gc_heap_end_ns = NanoTime();
182
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700183 // Trim the native heap.
184 dlmalloc_trim(0);
Ian Rogers48931882013-01-22 14:35:16 -0800185 size_t native_reclaimed = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700186 dlmalloc_inspect_all(DlmallocMadviseCallback, &native_reclaimed);
Ian Rogers48931882013-01-22 14:35:16 -0800187
188 uint64_t end_ns = NanoTime();
189
190 LOG(INFO) << "Heap trim of managed (duration=" << PrettyDuration(gc_heap_end_ns - start_ns)
191 << ", advised=" << PrettySize(managed_reclaimed) << ") and native (duration="
192 << PrettyDuration(end_ns - gc_heap_end_ns) << ", advised=" << PrettySize(native_reclaimed)
193 << ") heaps. Managed heap utilization of " << static_cast<int>(100 * managed_utilization)
194 << "%.";
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800195}
196
Ian Rogers81d425b2012-09-27 16:03:43 -0700197static void VMRuntime_concurrentGC(JNIEnv* env, jobject) {
198 Thread* self = static_cast<JNIEnvExt*>(env)->self;
199 Runtime::Current()->GetHeap()->ConcurrentGC(self);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700200}
201
Elliott Hughes0512f022012-03-15 22:10:52 -0700202static JNINativeMethod gMethods[] = {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700203 NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
204 NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
205 NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
206 NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700207 NATIVE_METHOD(VMRuntime, concurrentGC, "()V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700208 NATIVE_METHOD(VMRuntime, disableJitCompilation, "()V"),
209 NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
210 NATIVE_METHOD(VMRuntime, isDebuggerActive, "()Z"),
211 NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
212 NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
213 NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
214 NATIVE_METHOD(VMRuntime, setTargetSdkVersion, "(I)V"),
Mathieu Chartier987ccff2013-07-08 11:05:21 -0700215 NATIVE_METHOD(VMRuntime, registerNativeAllocation, "(I)V"),
216 NATIVE_METHOD(VMRuntime, registerNativeFree, "(I)V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700217 NATIVE_METHOD(VMRuntime, startJitCompilation, "()V"),
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800218 NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700219 NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
Brian Carlstromcef450c2013-06-28 14:38:26 -0700220 NATIVE_METHOD(VMRuntime, vmLibrary, "()Ljava/lang/String;"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700221};
222
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700223void register_dalvik_system_VMRuntime(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700224 REGISTER_NATIVE_METHODS("dalvik/system/VMRuntime");
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700225}
226
227} // namespace art