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 <limits.h> 24 25 26 /* 27 * public native float getTargetHeapUtilization() 28 * 29 * Gets the current ideal heap utilization, represented as a number 30 * between zero and one. 31 */ 32 static void Dalvik_dalvik_system_VMRuntime_getTargetHeapUtilization( 33 const u4* args, JValue* pResult) 34 { 35 UNUSED_PARAMETER(args); 36 37 RETURN_FLOAT(dvmGetTargetHeapUtilization()); 38 } 39 40 /* 41 * native float nativeSetTargetHeapUtilization() 42 * 43 * Sets the current ideal heap utilization, represented as a number 44 * between zero and one. Returns the old utilization. 45 * 46 * Note that this is NOT static. 47 */ 48 static void Dalvik_dalvik_system_VMRuntime_nativeSetTargetHeapUtilization( 49 const u4* args, JValue* pResult) 50 { 51 dvmSetTargetHeapUtilization(dvmU4ToFloat(args[1])); 52 53 RETURN_VOID(); 54 } 55 56 /* 57 * native long nativeMinimumHeapSize(long size, boolean set) 58 * 59 * If set is true, sets the new minimum heap size to size; always 60 * returns the current (or previous) size. If size is negative or 61 * zero, removes the current minimum constraint (if present). 62 */ 63 static void Dalvik_dalvik_system_VMRuntime_nativeMinimumHeapSize( 64 const u4* args, JValue* pResult) 65 { 66 s8 longSize = GET_ARG_LONG(args, 1); 67 size_t size; 68 bool set = (args[3] != 0); 69 70 /* Fit in 32 bits. */ 71 if (longSize < 0) { 72 size = 0; 73 } else if (longSize > INT_MAX) { 74 size = INT_MAX; 75 } else { 76 size = (size_t)longSize; 77 } 78 79 size = dvmMinimumHeapSize(size, set); 80 81 RETURN_LONG(size); 82 } 83 84 /* 85 * public native void gcSoftReferences() 86 * 87 * Does a GC and forces collection of SoftReferences that are 88 * not strongly-reachable. 89 */ 90 static void Dalvik_dalvik_system_VMRuntime_gcSoftReferences(const u4* args, 91 JValue* pResult) 92 { 93 dvmCollectGarbage(true); 94 95 RETURN_VOID(); 96 } 97 98 /* 99 * public native void runFinalizationSync() 100 * 101 * Does not return until any pending finalizers have been called. 102 * This may or may not happen in the context of the calling thread. 103 * No exceptions will escape. 104 * 105 * Used by zygote, which doesn't have a HeapWorker thread. 106 */ 107 static void Dalvik_dalvik_system_VMRuntime_runFinalizationSync(const u4* args, 108 JValue* pResult) 109 { 110 dvmRunFinalizationSync(); 111 112 RETURN_VOID(); 113 } 114 115 /* 116 * public native boolean trackExternalAllocation(long size) 117 * 118 * Asks the VM if <size> bytes can be allocated in an external heap. 119 * This information may be used to limit the amount of memory available 120 * to Dalvik threads. Returns false if the VM would rather that the caller 121 * did not allocate that much memory. If the call returns false, the VM 122 * will not update its internal counts. 123 */ 124 static void Dalvik_dalvik_system_VMRuntime_trackExternalAllocation( 125 const u4* args, JValue* pResult) 126 { 127 s8 longSize = GET_ARG_LONG(args, 1); 128 129 /* Fit in 32 bits. */ 130 if (longSize < 0) { 131 dvmThrowException("Ljava/lang/IllegalArgumentException;", 132 "size must be positive"); 133 RETURN_VOID(); 134 } else if (longSize > INT_MAX) { 135 dvmThrowException("Ljava/lang/UnsupportedOperationException;", 136 "size must fit in 32 bits"); 137 RETURN_VOID(); 138 } 139 RETURN_BOOLEAN(dvmTrackExternalAllocation((size_t)longSize)); 140 } 141 142 /* 143 * public native void trackExternalFree(long size) 144 * 145 * Tells the VM that <size> bytes have been freed in an external 146 * heap. This information may be used to control the amount of memory 147 * available to Dalvik threads. 148 */ 149 static void Dalvik_dalvik_system_VMRuntime_trackExternalFree( 150 const u4* args, JValue* pResult) 151 { 152 s8 longSize = GET_ARG_LONG(args, 1); 153 154 /* Fit in 32 bits. */ 155 if (longSize < 0) { 156 dvmThrowException("Ljava/lang/IllegalArgumentException;", 157 "size must be positive"); 158 RETURN_VOID(); 159 } else if (longSize > INT_MAX) { 160 dvmThrowException("Ljava/lang/UnsupportedOperationException;", 161 "size must fit in 32 bits"); 162 RETURN_VOID(); 163 } 164 dvmTrackExternalFree((size_t)longSize); 165 166 RETURN_VOID(); 167 } 168 169 /* 170 * public native long getExternalBytesAllocated() 171 * 172 * Returns the number of externally-allocated bytes being tracked by 173 * trackExternalAllocation/Free(). 174 */ 175 static void Dalvik_dalvik_system_VMRuntime_getExternalBytesAllocated( 176 const u4* args, JValue* pResult) 177 { 178 RETURN_LONG((s8)dvmGetExternalBytesAllocated()); 179 } 180 181 /* 182 * public native void startJitCompilation() 183 * 184 * Callback function from the framework to indicate that an app has gone 185 * through the startup phase and it is time to enable the JIT compiler. 186 */ 187 static void Dalvik_dalvik_system_VMRuntime_startJitCompilation(const u4* args, 188 JValue* pResult) 189 { 190 #if defined(WITH_JIT) 191 if (gDvm.executionMode == kExecutionModeJit && 192 gDvmJit.disableJit == false) { 193 dvmLockMutex(&gDvmJit.compilerLock); 194 gDvmJit.alreadyEnabledViaFramework = true; 195 pthread_cond_signal(&gDvmJit.compilerQueueActivity); 196 dvmUnlockMutex(&gDvmJit.compilerLock); 197 } 198 #endif 199 RETURN_VOID(); 200 } 201 202 /* 203 * public native void disableJitCompilation() 204 * 205 * Callback function from the framework to indicate that a VM instance wants to 206 * permanently disable the JIT compiler. Currently only the system server uses 207 * this interface when it detects system-wide safe mode is enabled. 208 */ 209 static void Dalvik_dalvik_system_VMRuntime_disableJitCompilation(const u4* args, 210 JValue* pResult) 211 { 212 #if defined(WITH_JIT) 213 if (gDvm.executionMode == kExecutionModeJit) { 214 gDvmJit.disableJit = true; 215 } 216 #endif 217 RETURN_VOID(); 218 } 219 220 const DalvikNativeMethod dvm_dalvik_system_VMRuntime[] = { 221 { "getTargetHeapUtilization", "()F", 222 Dalvik_dalvik_system_VMRuntime_getTargetHeapUtilization }, 223 { "nativeSetTargetHeapUtilization", "(F)V", 224 Dalvik_dalvik_system_VMRuntime_nativeSetTargetHeapUtilization }, 225 { "nativeMinimumHeapSize", "(JZ)J", 226 Dalvik_dalvik_system_VMRuntime_nativeMinimumHeapSize }, 227 { "gcSoftReferences", "()V", 228 Dalvik_dalvik_system_VMRuntime_gcSoftReferences }, 229 { "runFinalizationSync", "()V", 230 Dalvik_dalvik_system_VMRuntime_runFinalizationSync }, 231 { "trackExternalAllocation", "(J)Z", 232 Dalvik_dalvik_system_VMRuntime_trackExternalAllocation }, 233 { "trackExternalFree", "(J)V", 234 Dalvik_dalvik_system_VMRuntime_trackExternalFree }, 235 { "getExternalBytesAllocated", "()J", 236 Dalvik_dalvik_system_VMRuntime_getExternalBytesAllocated }, 237 { "startJitCompilation", "()V", 238 Dalvik_dalvik_system_VMRuntime_startJitCompilation }, 239 { "disableJitCompilation", "()V", 240 Dalvik_dalvik_system_VMRuntime_disableJitCompilation }, 241 { NULL, NULL, NULL }, 242 }; 243