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 */
Dalvik_dalvik_system_VMRuntime_getTargetHeapUtilization(const u4 * args,JValue * pResult)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 */
Dalvik_dalvik_system_VMRuntime_nativeSetTargetHeapUtilization(const u4 * args,JValue * pResult)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 */
Dalvik_dalvik_system_VMRuntime_nativeMinimumHeapSize(const u4 * args,JValue * pResult)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 */
Dalvik_dalvik_system_VMRuntime_gcSoftReferences(const u4 * args,JValue * pResult)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 */
Dalvik_dalvik_system_VMRuntime_runFinalizationSync(const u4 * args,JValue * pResult)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 */
Dalvik_dalvik_system_VMRuntime_trackExternalAllocation(const u4 * args,JValue * pResult)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 */
Dalvik_dalvik_system_VMRuntime_trackExternalFree(const u4 * args,JValue * pResult)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 */
Dalvik_dalvik_system_VMRuntime_getExternalBytesAllocated(const u4 * args,JValue * pResult)175 static void Dalvik_dalvik_system_VMRuntime_getExternalBytesAllocated(
176 const u4* args, JValue* pResult)
177 {
178 RETURN_LONG((s8)dvmGetExternalBytesAllocated());
179 }
180
181 const DalvikNativeMethod dvm_dalvik_system_VMRuntime[] = {
182 { "getTargetHeapUtilization", "()F",
183 Dalvik_dalvik_system_VMRuntime_getTargetHeapUtilization },
184 { "nativeSetTargetHeapUtilization", "(F)V",
185 Dalvik_dalvik_system_VMRuntime_nativeSetTargetHeapUtilization },
186 { "nativeMinimumHeapSize", "(JZ)J",
187 Dalvik_dalvik_system_VMRuntime_nativeMinimumHeapSize },
188 { "gcSoftReferences", "()V",
189 Dalvik_dalvik_system_VMRuntime_gcSoftReferences },
190 { "runFinalizationSync", "()V",
191 Dalvik_dalvik_system_VMRuntime_runFinalizationSync },
192 { "trackExternalAllocation", "(J)Z",
193 Dalvik_dalvik_system_VMRuntime_trackExternalAllocation },
194 { "trackExternalFree", "(J)V",
195 Dalvik_dalvik_system_VMRuntime_trackExternalFree },
196 { "getExternalBytesAllocated", "()J",
197 Dalvik_dalvik_system_VMRuntime_getExternalBytesAllocated },
198 { NULL, NULL, NULL },
199 };
200
201