blob: e969fcf503e4bf44d2d6a5247f1cafb0adf05edb [file] [log] [blame]
Elliott Hughesbf86d042011-08-31 17:53:14 -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 Hughesc8fece32013-01-02 11:27:23 -080017#include <dlfcn.h>
Elliott Hughesbf86d042011-08-31 17:53:14 -070018#include <limits.h>
Elliott Hugheseac76672012-05-24 21:56:51 -070019#include <unistd.h>
Elliott Hughesbf86d042011-08-31 17:53:14 -070020
Ian Rogers1d54e732013-05-02 21:10:01 -070021#include "gc/heap.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070022#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/class_loader.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070024#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070026#include "ScopedUtfChars.h"
27
28namespace art {
29
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030static void Runtime_gc(JNIEnv*, jclass) {
Anwar Ghuloum87183592013-08-14 12:12:19 -070031 if (Runtime::Current()->IsExplicitGcDisabled()) {
32 LOG(INFO) << "Explicit GC skipped.";
33 return;
34 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080035 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughesbf86d042011-08-31 17:53:14 -070036}
37
Elliott Hughes28c7bfd2012-06-01 12:45:40 -070038static void Runtime_nativeExit(JNIEnv*, jclass, jint status) {
39 Runtime::Current()->CallExitHook(status);
Elliott Hughesbf86d042011-08-31 17:53:14 -070040 exit(status);
41}
42
Elliott Hughesc8fece32013-01-02 11:27:23 -080043static jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader, jstring javaLdLibraryPath) {
Elliott Hughesbf86d042011-08-31 17:53:14 -070044 ScopedUtfChars filename(env, javaFilename);
45 if (filename.c_str() == NULL) {
46 return NULL;
47 }
48
Elliott Hughesc8fece32013-01-02 11:27:23 -080049 if (javaLdLibraryPath != NULL) {
50 ScopedUtfChars ldLibraryPath(env, javaLdLibraryPath);
51 if (ldLibraryPath.c_str() == NULL) {
52 return NULL;
53 }
54 void* sym = dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH");
55 if (sym != NULL) {
56 typedef void (*Fn)(const char*);
57 Fn android_update_LD_LIBRARY_PATH = reinterpret_cast<Fn>(sym);
58 (*android_update_LD_LIBRARY_PATH)(ldLibraryPath.c_str());
59 } else {
60 LOG(ERROR) << "android_update_LD_LIBRARY_PATH not found; .so dependencies will not work!";
61 }
62 }
63
Elliott Hughesbf86d042011-08-31 17:53:14 -070064 std::string detail;
Ian Rogers1eb512d2013-10-18 15:42:20 -070065 {
66 ScopedObjectAccess soa(env);
67 mirror::ClassLoader* classLoader = soa.Decode<mirror::ClassLoader*>(javaLoader);
68 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
69 bool success = vm->LoadNativeLibrary(filename.c_str(), classLoader, &detail);
70 if (success) {
71 return nullptr;
72 }
Elliott Hughesbf86d042011-08-31 17:53:14 -070073 }
74
Brian Carlstrom75fe90c2013-06-26 22:26:16 -070075 // Don't let a pending exception from JNI_OnLoad cause a CheckJNI issue with NewStringUTF.
76 env->ExceptionClear();
Elliott Hughesbf86d042011-08-31 17:53:14 -070077 return env->NewStringUTF(detail.c_str());
78}
79
Elliott Hughes1bac54f2012-03-16 12:48:31 -070080static jlong Runtime_maxMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080081 return Runtime::Current()->GetHeap()->GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070082}
83
Elliott Hughes1bac54f2012-03-16 12:48:31 -070084static jlong Runtime_totalMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080085 return Runtime::Current()->GetHeap()->GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070086}
87
Elliott Hughes1bac54f2012-03-16 12:48:31 -070088static jlong Runtime_freeMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080089 return Runtime::Current()->GetHeap()->GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070090}
91
92static JNINativeMethod gMethods[] = {
Elliott Hugheseac76672012-05-24 21:56:51 -070093 NATIVE_METHOD(Runtime, freeMemory, "()J"),
94 NATIVE_METHOD(Runtime, gc, "()V"),
95 NATIVE_METHOD(Runtime, maxMemory, "()J"),
Elliott Hughes28c7bfd2012-06-01 12:45:40 -070096 NATIVE_METHOD(Runtime, nativeExit, "(I)V"),
Elliott Hughesc8fece32013-01-02 11:27:23 -080097 NATIVE_METHOD(Runtime, nativeLoad, "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/String;"),
Elliott Hugheseac76672012-05-24 21:56:51 -070098 NATIVE_METHOD(Runtime, totalMemory, "()J"),
Elliott Hughesbf86d042011-08-31 17:53:14 -070099};
100
Elliott Hughesbf86d042011-08-31 17:53:14 -0700101void register_java_lang_Runtime(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700102 REGISTER_NATIVE_METHODS("java/lang/Runtime");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700103}
104
105} // namespace art