• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 /*
18  * java.lang.Runtime
19  */
20 #include "Dalvik.h"
21 #include "native/InternalNativePriv.h"
22 #include <unistd.h>
23 #include <limits.h>
24 
25 /*
26  * public void gc()
27  *
28  * Initiate a gc.
29  */
Dalvik_java_lang_Runtime_gc(const u4 * args,JValue * pResult)30 static void Dalvik_java_lang_Runtime_gc(const u4* args, JValue* pResult)
31 {
32     UNUSED_PARAMETER(args);
33 
34     dvmCollectGarbage(false);
35     RETURN_VOID();
36 }
37 
38 /*
39  * private static void nativeExit(int code, boolean isExit)
40  *
41  * Runtime.exit() calls this after doing shutdown processing.  Runtime.halt()
42  * uses this as well.
43  */
Dalvik_java_lang_Runtime_nativeExit(const u4 * args,JValue * pResult)44 static void Dalvik_java_lang_Runtime_nativeExit(const u4* args,
45     JValue* pResult)
46 {
47     int status = args[0];
48     bool isExit = (args[1] != 0);
49 
50     if (isExit && gDvm.exitHook != NULL) {
51         dvmChangeStatus(NULL, THREAD_NATIVE);
52         (*gDvm.exitHook)(status);     // not expected to return
53         dvmChangeStatus(NULL, THREAD_RUNNING);
54         LOGW("JNI exit hook returned\n");
55     }
56     LOGD("Calling exit(%d)\n", status);
57 #if defined(WITH_JIT) && defined(WITH_JIT_TUNING)
58     dvmCompilerDumpStats();
59 #endif
60     exit(status);
61 }
62 
63 /*
64  * static String nativeLoad(String filename, ClassLoader loader)
65  *
66  * Load the specified full path as a dynamic library filled with
67  * JNI-compatible methods. Returns null on success, or a failure
68  * message on failure.
69  */
Dalvik_java_lang_Runtime_nativeLoad(const u4 * args,JValue * pResult)70 static void Dalvik_java_lang_Runtime_nativeLoad(const u4* args,
71     JValue* pResult)
72 {
73     StringObject* fileNameObj = (StringObject*) args[0];
74     Object* classLoader = (Object*) args[1];
75     char* fileName = NULL;
76     StringObject* result = NULL;
77     char* reason = NULL;
78     bool success;
79 
80     assert(fileNameObj != NULL);
81     fileName = dvmCreateCstrFromString(fileNameObj);
82 
83     success = dvmLoadNativeCode(fileName, classLoader, &reason);
84     if (!success) {
85         const char* msg = (reason != NULL) ? reason : "unknown failure";
86         result = dvmCreateStringFromCstr(msg);
87         dvmReleaseTrackedAlloc((Object*) result, NULL);
88     }
89 
90     free(reason);
91     free(fileName);
92     RETURN_PTR(result);
93 }
94 
95 /*
96  * public void runFinalization(boolean forced)
97  *
98  * Requests that the VM runs finalizers for objects on the heap. If the
99  * parameter forced is true, then the VM needs to ensure finalization.
100  * Otherwise this only inspires the VM to make a best-effort attempt to
101  * run finalizers before returning, but it's not guaranteed to actually
102  * do anything.
103  */
Dalvik_java_lang_Runtime_runFinalization(const u4 * args,JValue * pResult)104 static void Dalvik_java_lang_Runtime_runFinalization(const u4* args,
105     JValue* pResult)
106 {
107     bool forced = (args[0] != 0);
108 
109     dvmWaitForHeapWorkerIdle();
110     if (forced) {
111         // TODO(Google) Need to explicitly implement this,
112         //              although dvmWaitForHeapWorkerIdle()
113         //              should usually provide the "forced"
114         //              behavior already.
115     }
116 
117     RETURN_VOID();
118 }
119 
120 /*
121  * public int availableProcessors()
122  *
123  * Returns the number of online processors, at least one.
124  *
125  */
Dalvik_java_lang_Runtime_availableProcessors(const u4 * args,JValue * pResult)126 static void Dalvik_java_lang_Runtime_availableProcessors(const u4* args,
127     JValue* pResult)
128 {
129     long result = 1;
130 #ifdef _SC_NPROCESSORS_ONLN
131     result = sysconf(_SC_NPROCESSORS_ONLN);
132     if (result > INT_MAX) {
133         result = INT_MAX;
134     } else if (result < 1 ) {
135         result = 1;
136     }
137 #endif
138     RETURN_INT((int)result);
139 }
140 /*
141  * public void maxMemory()
142  *
143  * Returns GC heap max memory in bytes.
144  */
Dalvik_java_lang_Runtime_maxMemory(const u4 * args,JValue * pResult)145 static void Dalvik_java_lang_Runtime_maxMemory(const u4* args, JValue* pResult)
146 {
147     unsigned int result = gDvm.heapSizeMax;
148     RETURN_LONG(result);
149 }
150 
151 /*
152  * public void totalMemory()
153  *
154  * Returns GC heap total memory in bytes.
155  */
Dalvik_java_lang_Runtime_totalMemory(const u4 * args,JValue * pResult)156 static void Dalvik_java_lang_Runtime_totalMemory(const u4* args,
157     JValue* pResult)
158 {
159     int result = dvmGetHeapDebugInfo(kVirtualHeapSize);
160     RETURN_LONG(result);
161 }
162 
163 /*
164  * public void freeMemory()
165  *
166  * Returns GC heap free memory in bytes.
167  */
Dalvik_java_lang_Runtime_freeMemory(const u4 * args,JValue * pResult)168 static void Dalvik_java_lang_Runtime_freeMemory(const u4* args,
169     JValue* pResult)
170 {
171     int result = dvmGetHeapDebugInfo(kVirtualHeapSize)
172                  - dvmGetHeapDebugInfo(kVirtualHeapAllocated);
173     if (result < 0) {
174         result = 0;
175     }
176     RETURN_LONG(result);
177 }
178 
179 const DalvikNativeMethod dvm_java_lang_Runtime[] = {
180     { "freeMemory",          "()J",
181         Dalvik_java_lang_Runtime_freeMemory },
182     { "gc",                 "()V",
183         Dalvik_java_lang_Runtime_gc },
184     { "availableProcessors", "()I",
185         Dalvik_java_lang_Runtime_availableProcessors },
186     { "maxMemory",          "()J",
187         Dalvik_java_lang_Runtime_maxMemory },
188     { "nativeExit",         "(IZ)V",
189         Dalvik_java_lang_Runtime_nativeExit },
190     { "nativeLoad",         "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/String;",
191         Dalvik_java_lang_Runtime_nativeLoad },
192     { "runFinalization",    "(Z)V",
193         Dalvik_java_lang_Runtime_runFinalization },
194     { "totalMemory",          "()J",
195         Dalvik_java_lang_Runtime_totalMemory },
196     { NULL, NULL, NULL },
197 };
198