dalvik_system_VMRuntime.cpp revision db0c9549818d9f6e508d26e45ff9d886802aa1da
1f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/*
2f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
3f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project *
4f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * you may not use this file except in compliance with the License.
6f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * You may obtain a copy of the License at
7f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project *
8f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project *
10f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * See the License for the specific language governing permissions and
14f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * limitations under the License.
15f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */
16f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project
17f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/*
18f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * dalvik.system.VMRuntime
19f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */
20f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "Dalvik.h"
21f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "native/InternalNativePriv.h"
22f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project
2349dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes#include <cutils/array.h>
24f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include <limits.h>
25f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project
26f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project
27f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/*
28f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * public native float getTargetHeapUtilization()
29f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project *
30f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Gets the current ideal heap utilization, represented as a number
31f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * between zero and one.
32f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */
33f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectstatic void Dalvik_dalvik_system_VMRuntime_getTargetHeapUtilization(
34f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project    const u4* args, JValue* pResult)
35f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{
36f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project    UNUSED_PARAMETER(args);
37f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project
38f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project    RETURN_FLOAT(dvmGetTargetHeapUtilization());
39f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project}
40f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project
41f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/*
42f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * native float nativeSetTargetHeapUtilization()
43f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project *
44f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Sets the current ideal heap utilization, represented as a number
45f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * between zero and one.  Returns the old utilization.
46f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project *
47f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Note that this is NOT static.
48f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */
49f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectstatic void Dalvik_dalvik_system_VMRuntime_nativeSetTargetHeapUtilization(
50f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project    const u4* args, JValue* pResult)
51f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{
52f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project    dvmSetTargetHeapUtilization(dvmU4ToFloat(args[1]));
53f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project
54f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project    RETURN_VOID();
55f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project}
56f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project
57f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/*
58a97ed1382c4e74704b44d366cf464bef5757c6b0Ben Cheng * public native void startJitCompilation()
596bad6a2d77bcfeebf1f5a24406f49bda88503353Ben Cheng *
606bad6a2d77bcfeebf1f5a24406f49bda88503353Ben Cheng * Callback function from the framework to indicate that an app has gone
616bad6a2d77bcfeebf1f5a24406f49bda88503353Ben Cheng * through the startup phase and it is time to enable the JIT compiler.
626bad6a2d77bcfeebf1f5a24406f49bda88503353Ben Cheng */
63a97ed1382c4e74704b44d366cf464bef5757c6b0Ben Chengstatic void Dalvik_dalvik_system_VMRuntime_startJitCompilation(const u4* args,
646bad6a2d77bcfeebf1f5a24406f49bda88503353Ben Cheng    JValue* pResult)
656bad6a2d77bcfeebf1f5a24406f49bda88503353Ben Cheng{
666bad6a2d77bcfeebf1f5a24406f49bda88503353Ben Cheng#if defined(WITH_JIT)
67f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng    if (gDvm.executionMode == kExecutionModeJit &&
68f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng        gDvmJit.disableJit == false) {
6996cfe6c39b91dabc78182e2f7676b27b4012886aBill Buzbee        dvmLockMutex(&gDvmJit.compilerLock);
70f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng        gDvmJit.alreadyEnabledViaFramework = true;
7196cfe6c39b91dabc78182e2f7676b27b4012886aBill Buzbee        pthread_cond_signal(&gDvmJit.compilerQueueActivity);
7296cfe6c39b91dabc78182e2f7676b27b4012886aBill Buzbee        dvmUnlockMutex(&gDvmJit.compilerLock);
7396cfe6c39b91dabc78182e2f7676b27b4012886aBill Buzbee    }
7494d89f8016b6c305ab0df491874dcedb252ecfccBill Buzbee#endif
756bad6a2d77bcfeebf1f5a24406f49bda88503353Ben Cheng    RETURN_VOID();
766bad6a2d77bcfeebf1f5a24406f49bda88503353Ben Cheng}
776bad6a2d77bcfeebf1f5a24406f49bda88503353Ben Cheng
78f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng/*
79f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng * public native void disableJitCompilation()
80f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng *
81f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng * Callback function from the framework to indicate that a VM instance wants to
82f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng * permanently disable the JIT compiler. Currently only the system server uses
83f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng * this interface when it detects system-wide safe mode is enabled.
84f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng */
85f30acbb249b137b049500f136d2bb273c0b6221aBen Chengstatic void Dalvik_dalvik_system_VMRuntime_disableJitCompilation(const u4* args,
86f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng    JValue* pResult)
87f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng{
88f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng#if defined(WITH_JIT)
89f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng    if (gDvm.executionMode == kExecutionModeJit) {
90f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng        gDvmJit.disableJit = true;
91f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng    }
92f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng#endif
93f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng    RETURN_VOID();
94f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng}
95f30acbb249b137b049500f136d2bb273c0b6221aBen Cheng
96f5949168d4142cde1582117173b03c614b632938Elliott Hughesstatic void Dalvik_dalvik_system_VMRuntime_newNonMovableArray(const u4* args,
97f5949168d4142cde1582117173b03c614b632938Elliott Hughes    JValue* pResult)
98f5949168d4142cde1582117173b03c614b632938Elliott Hughes{
99f5949168d4142cde1582117173b03c614b632938Elliott Hughes    ClassObject* elementClass = (ClassObject*) args[1];
100f5949168d4142cde1582117173b03c614b632938Elliott Hughes    int length = args[2];
101f5949168d4142cde1582117173b03c614b632938Elliott Hughes    ArrayObject* newArray;
102f5949168d4142cde1582117173b03c614b632938Elliott Hughes
103f5949168d4142cde1582117173b03c614b632938Elliott Hughes    if (elementClass == NULL) {
104d27f3cf3e7b373487f39e035fc4b55168d55c454Dan Bornstein        dvmThrowNullPointerException(NULL);
105f5949168d4142cde1582117173b03c614b632938Elliott Hughes        RETURN_VOID();
106f5949168d4142cde1582117173b03c614b632938Elliott Hughes    }
107f5949168d4142cde1582117173b03c614b632938Elliott Hughes    if (length < 0) {
1082c8e25b2010d1bba4dca0148691bc6e1a4ee7616Dan Bornstein        dvmThrowNegativeArraySizeException(length);
109f5949168d4142cde1582117173b03c614b632938Elliott Hughes        RETURN_VOID();
110f5949168d4142cde1582117173b03c614b632938Elliott Hughes    }
111f5949168d4142cde1582117173b03c614b632938Elliott Hughes
112f5949168d4142cde1582117173b03c614b632938Elliott Hughes    // TODO: right now, we don't have a copying collector, so there's no need
113f5949168d4142cde1582117173b03c614b632938Elliott Hughes    // to do anything special here, but we ought to pass the non-movability
114f5949168d4142cde1582117173b03c614b632938Elliott Hughes    // through to the allocator.
115f5949168d4142cde1582117173b03c614b632938Elliott Hughes    newArray = dvmAllocObjectArray(elementClass, length, ALLOC_DEFAULT);
116f5949168d4142cde1582117173b03c614b632938Elliott Hughes    if (newArray == NULL) {
117f5949168d4142cde1582117173b03c614b632938Elliott Hughes        assert(dvmCheckException(dvmThreadSelf()));
118f5949168d4142cde1582117173b03c614b632938Elliott Hughes        RETURN_VOID();
119f5949168d4142cde1582117173b03c614b632938Elliott Hughes    }
120f5949168d4142cde1582117173b03c614b632938Elliott Hughes    dvmReleaseTrackedAlloc((Object*) newArray, NULL);
121f5949168d4142cde1582117173b03c614b632938Elliott Hughes
122f5949168d4142cde1582117173b03c614b632938Elliott Hughes    RETURN_PTR(newArray);
123f5949168d4142cde1582117173b03c614b632938Elliott Hughes}
124f5949168d4142cde1582117173b03c614b632938Elliott Hughes
125f5949168d4142cde1582117173b03c614b632938Elliott Hughesstatic void Dalvik_dalvik_system_VMRuntime_addressOf(const u4* args,
126f5949168d4142cde1582117173b03c614b632938Elliott Hughes    JValue* pResult)
127f5949168d4142cde1582117173b03c614b632938Elliott Hughes{
128f5949168d4142cde1582117173b03c614b632938Elliott Hughes    ArrayObject* array = (ArrayObject*) args[1];
12919f4f02d096e549c1aaabdb5e8fa317588ab0bfdElliott Hughes    if (!dvmIsArray(array)) {
130d27f3cf3e7b373487f39e035fc4b55168d55c454Dan Bornstein        dvmThrowIllegalArgumentException(NULL);
13119f4f02d096e549c1aaabdb5e8fa317588ab0bfdElliott Hughes        RETURN_VOID();
13219f4f02d096e549c1aaabdb5e8fa317588ab0bfdElliott Hughes    }
13319f4f02d096e549c1aaabdb5e8fa317588ab0bfdElliott Hughes    // TODO: we should also check that this is a non-movable array.
134f5949168d4142cde1582117173b03c614b632938Elliott Hughes    s8 result = (uintptr_t) array->contents;
135f5949168d4142cde1582117173b03c614b632938Elliott Hughes    RETURN_LONG(result);
136f5949168d4142cde1582117173b03c614b632938Elliott Hughes}
137f5949168d4142cde1582117173b03c614b632938Elliott Hughes
138df9f08b877ecfd8ebadea822bb9e066ee7d30433Carl Shapirostatic void Dalvik_dalvik_system_VMRuntime_clearGrowthLimit(const u4* args,
139df9f08b877ecfd8ebadea822bb9e066ee7d30433Carl Shapiro    JValue* pResult)
140df9f08b877ecfd8ebadea822bb9e066ee7d30433Carl Shapiro{
141df9f08b877ecfd8ebadea822bb9e066ee7d30433Carl Shapiro    dvmClearGrowthLimit();
142df9f08b877ecfd8ebadea822bb9e066ee7d30433Carl Shapiro    RETURN_VOID();
143df9f08b877ecfd8ebadea822bb9e066ee7d30433Carl Shapiro}
144df9f08b877ecfd8ebadea822bb9e066ee7d30433Carl Shapiro
14549dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughesstatic void Dalvik_dalvik_system_VMRuntime_properties(const u4* args,
14649dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    JValue* pResult)
14749dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes{
148db0c9549818d9f6e508d26e45ff9d886802aa1daCarl Shapiro    const char** strings = (const char**) arrayUnwrap(gDvm.properties);
14949dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    int count = arraySize(gDvm.properties);
15049dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    ArrayObject* result = dvmCreateStringArray(strings, count);
15149dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    dvmReleaseTrackedAlloc((Object*) result, dvmThreadSelf());
15249dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    RETURN_PTR(result);
15349dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes}
15449dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes
15549dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughesstatic void returnCString(JValue* pResult, const char* s)
15649dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes{
15749dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    Object* result = (Object*) dvmCreateStringFromCstr(s);
15849dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    dvmReleaseTrackedAlloc(result, dvmThreadSelf());
15949dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    RETURN_PTR(result);
16049dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes}
16149dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes
16249dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughesstatic void Dalvik_dalvik_system_VMRuntime_bootClassPath(const u4* args,
16349dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    JValue* pResult)
16449dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes{
16549dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    returnCString(pResult, gDvm.bootClassPathStr);
16649dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes}
16749dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes
16849dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughesstatic void Dalvik_dalvik_system_VMRuntime_classPath(const u4* args,
16949dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    JValue* pResult)
17049dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes{
17149dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    returnCString(pResult, gDvm.classPathStr);
17249dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes}
17349dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes
17449dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughesstatic void Dalvik_dalvik_system_VMRuntime_vmVersion(const u4* args,
17549dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    JValue* pResult)
17649dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes{
17749dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    char buf[64];
17849dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    sprintf(buf, "%d.%d.%d",
17949dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes            DALVIK_MAJOR_VERSION, DALVIK_MINOR_VERSION, DALVIK_BUG_VERSION);
18049dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    returnCString(pResult, buf);
18149dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes}
18249dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes
183f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectconst DalvikNativeMethod dvm_dalvik_system_VMRuntime[] = {
18449dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    { "addressOf", "(Ljava/lang/Object;)J",
18549dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes        Dalvik_dalvik_system_VMRuntime_addressOf },
18649dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    { "bootClassPath", "()Ljava/lang/String;",
18749dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes        Dalvik_dalvik_system_VMRuntime_bootClassPath },
18849dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    { "classPath", "()Ljava/lang/String;",
18949dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes        Dalvik_dalvik_system_VMRuntime_classPath },
19049dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    { "clearGrowthLimit", "()V",
19149dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes        Dalvik_dalvik_system_VMRuntime_clearGrowthLimit },
19249dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    { "disableJitCompilation", "()V",
19349dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes        Dalvik_dalvik_system_VMRuntime_disableJitCompilation },
194f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project    { "getTargetHeapUtilization", "()F",
195f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project        Dalvik_dalvik_system_VMRuntime_getTargetHeapUtilization },
196f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project    { "nativeSetTargetHeapUtilization", "(F)V",
197f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project        Dalvik_dalvik_system_VMRuntime_nativeSetTargetHeapUtilization },
19849dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    { "newNonMovableArray", "(Ljava/lang/Class;I)Ljava/lang/Object;",
19949dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes        Dalvik_dalvik_system_VMRuntime_newNonMovableArray },
20049dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    { "properties", "()[Ljava/lang/String;",
20149dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes        Dalvik_dalvik_system_VMRuntime_properties },
202a97ed1382c4e74704b44d366cf464bef5757c6b0Ben Cheng    { "startJitCompilation", "()V",
203a97ed1382c4e74704b44d366cf464bef5757c6b0Ben Cheng        Dalvik_dalvik_system_VMRuntime_startJitCompilation },
20449dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes    { "vmVersion", "()Ljava/lang/String;",
20549dc060d5ae9bbcc78e570ec0dd244973e920fb6Elliott Hughes        Dalvik_dalvik_system_VMRuntime_vmVersion },
206f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project    { NULL, NULL, NULL },
207f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project};
208