OSDN Git Service

resolved conflicts for merge of 10185db0 to dalvik-dev
[android-x86/dalvik.git] / vm / native / dalvik_system_VMRuntime.c
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  * dalvik.system.VMRuntime
19  */
20 #include "Dalvik.h"
21 #include "native/InternalNativePriv.h"
22
23 #include <cutils/array.h>
24 #include <limits.h>
25
26
27 /*
28  * public native float getTargetHeapUtilization()
29  *
30  * Gets the current ideal heap utilization, represented as a number
31  * between zero and one.
32  */
33 static void Dalvik_dalvik_system_VMRuntime_getTargetHeapUtilization(
34     const u4* args, JValue* pResult)
35 {
36     UNUSED_PARAMETER(args);
37
38     RETURN_FLOAT(dvmGetTargetHeapUtilization());
39 }
40
41 /*
42  * native float nativeSetTargetHeapUtilization()
43  *
44  * Sets the current ideal heap utilization, represented as a number
45  * between zero and one.  Returns the old utilization.
46  *
47  * Note that this is NOT static.
48  */
49 static void Dalvik_dalvik_system_VMRuntime_nativeSetTargetHeapUtilization(
50     const u4* args, JValue* pResult)
51 {
52     dvmSetTargetHeapUtilization(dvmU4ToFloat(args[1]));
53
54     RETURN_VOID();
55 }
56
57 /*
58  * public native void runFinalizationSync()
59  *
60  * Does not return until any pending finalizers have been called.
61  * This may or may not happen in the context of the calling thread.
62  * No exceptions will escape.
63  *
64  * Used by zygote, which doesn't have a HeapWorker thread.
65  */
66 static void Dalvik_dalvik_system_VMRuntime_runFinalizationSync(const u4* args,
67     JValue* pResult)
68 {
69     dvmRunFinalizationSync();
70
71     RETURN_VOID();
72 }
73
74 /*
75  * public native void startJitCompilation()
76  *
77  * Callback function from the framework to indicate that an app has gone
78  * through the startup phase and it is time to enable the JIT compiler.
79  */
80 static void Dalvik_dalvik_system_VMRuntime_startJitCompilation(const u4* args,
81     JValue* pResult)
82 {
83 #if defined(WITH_JIT)
84     if (gDvm.executionMode == kExecutionModeJit &&
85         gDvmJit.disableJit == false) {
86         dvmLockMutex(&gDvmJit.compilerLock);
87         gDvmJit.alreadyEnabledViaFramework = true;
88         pthread_cond_signal(&gDvmJit.compilerQueueActivity);
89         dvmUnlockMutex(&gDvmJit.compilerLock);
90     }
91 #endif
92     RETURN_VOID();
93 }
94
95 /*
96  * public native void disableJitCompilation()
97  *
98  * Callback function from the framework to indicate that a VM instance wants to
99  * permanently disable the JIT compiler. Currently only the system server uses
100  * this interface when it detects system-wide safe mode is enabled.
101  */
102 static void Dalvik_dalvik_system_VMRuntime_disableJitCompilation(const u4* args,
103     JValue* pResult)
104 {
105 #if defined(WITH_JIT)
106     if (gDvm.executionMode == kExecutionModeJit) {
107         gDvmJit.disableJit = true;
108     }
109 #endif
110     RETURN_VOID();
111 }
112
113 static void Dalvik_dalvik_system_VMRuntime_newNonMovableArray(const u4* args,
114     JValue* pResult)
115 {
116     ClassObject* elementClass = (ClassObject*) args[1];
117     int length = args[2];
118     ArrayObject* newArray;
119
120     if (elementClass == NULL) {
121         dvmThrowNullPointerException(NULL);
122         RETURN_VOID();
123     }
124     if (length < 0) {
125         dvmThrowNegativeArraySizeException(length);
126         RETURN_VOID();
127     }
128
129     // TODO: right now, we don't have a copying collector, so there's no need
130     // to do anything special here, but we ought to pass the non-movability
131     // through to the allocator.
132     newArray = dvmAllocObjectArray(elementClass, length, ALLOC_DEFAULT);
133     if (newArray == NULL) {
134         assert(dvmCheckException(dvmThreadSelf()));
135         RETURN_VOID();
136     }
137     dvmReleaseTrackedAlloc((Object*) newArray, NULL);
138
139     RETURN_PTR(newArray);
140 }
141
142 static void Dalvik_dalvik_system_VMRuntime_addressOf(const u4* args,
143     JValue* pResult)
144 {
145     ArrayObject* array = (ArrayObject*) args[1];
146     if (!dvmIsArray(array)) {
147         dvmThrowIllegalArgumentException(NULL);
148         RETURN_VOID();
149     }
150     // TODO: we should also check that this is a non-movable array.
151     s8 result = (uintptr_t) array->contents;
152     RETURN_LONG(result);
153 }
154
155 static void Dalvik_dalvik_system_VMRuntime_clearGrowthLimit(const u4* args,
156     JValue* pResult)
157 {
158     dvmClearGrowthLimit();
159     RETURN_VOID();
160 }
161
162 static void Dalvik_dalvik_system_VMRuntime_properties(const u4* args,
163     JValue* pResult)
164 {
165     char** strings = (char**) arrayUnwrap(gDvm.properties);
166     int count = arraySize(gDvm.properties);
167     ArrayObject* result = dvmCreateStringArray(strings, count);
168     dvmReleaseTrackedAlloc((Object*) result, dvmThreadSelf());
169     RETURN_PTR(result);
170 }
171
172 static void returnCString(JValue* pResult, const char* s)
173 {
174     Object* result = (Object*) dvmCreateStringFromCstr(s);
175     dvmReleaseTrackedAlloc(result, dvmThreadSelf());
176     RETURN_PTR(result);
177 }
178
179 static void Dalvik_dalvik_system_VMRuntime_bootClassPath(const u4* args,
180     JValue* pResult)
181 {
182     returnCString(pResult, gDvm.bootClassPathStr);
183 }
184
185 static void Dalvik_dalvik_system_VMRuntime_classPath(const u4* args,
186     JValue* pResult)
187 {
188     returnCString(pResult, gDvm.classPathStr);
189 }
190
191 static void Dalvik_dalvik_system_VMRuntime_vmVersion(const u4* args,
192     JValue* pResult)
193 {
194     char buf[64];
195     sprintf(buf, "%d.%d.%d",
196             DALVIK_MAJOR_VERSION, DALVIK_MINOR_VERSION, DALVIK_BUG_VERSION);
197     returnCString(pResult, buf);
198 }
199
200 const DalvikNativeMethod dvm_dalvik_system_VMRuntime[] = {
201     { "addressOf", "(Ljava/lang/Object;)J",
202         Dalvik_dalvik_system_VMRuntime_addressOf },
203     { "bootClassPath", "()Ljava/lang/String;",
204         Dalvik_dalvik_system_VMRuntime_bootClassPath },
205     { "classPath", "()Ljava/lang/String;",
206         Dalvik_dalvik_system_VMRuntime_classPath },
207     { "clearGrowthLimit", "()V",
208         Dalvik_dalvik_system_VMRuntime_clearGrowthLimit },
209     { "disableJitCompilation", "()V",
210         Dalvik_dalvik_system_VMRuntime_disableJitCompilation },
211     { "getTargetHeapUtilization", "()F",
212         Dalvik_dalvik_system_VMRuntime_getTargetHeapUtilization },
213     { "nativeSetTargetHeapUtilization", "(F)V",
214         Dalvik_dalvik_system_VMRuntime_nativeSetTargetHeapUtilization },
215     { "newNonMovableArray", "(Ljava/lang/Class;I)Ljava/lang/Object;",
216         Dalvik_dalvik_system_VMRuntime_newNonMovableArray },
217     { "properties", "()[Ljava/lang/String;",
218         Dalvik_dalvik_system_VMRuntime_properties },
219     { "runFinalizationSync", "()V",
220         Dalvik_dalvik_system_VMRuntime_runFinalizationSync },
221     { "startJitCompilation", "()V",
222         Dalvik_dalvik_system_VMRuntime_startJitCompilation },
223     { "vmVersion", "()Ljava/lang/String;",
224         Dalvik_dalvik_system_VMRuntime_vmVersion },
225     { NULL, NULL, NULL },
226 };