1e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom/*
2e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * Copyright (C) 2007 The Android Open Source Project
3e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom *
4e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License");
5e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * you may not use this file except in compliance with the License.
6e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * You may obtain a copy of the License at
7e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom *
8e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom *      http://www.apache.org/licenses/LICENSE-2.0
9e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom *
10e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * Unless required by applicable law or agreed to in writing, software
11e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS,
12e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * See the License for the specific language governing permissions and
14e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * limitations under the License.
15e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom */
16e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
17e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrompackage dalvik.system;
18e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
1951855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartierimport java.lang.ref.FinalizerReference;
20a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamathimport java.util.HashMap;
21a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamathimport java.util.Map;
22a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath
23e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom/**
24e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * Provides an interface to VM-global, Dalvik-specific features.
25e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * An application cannot create its own Runtime instance, and must obtain
26e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * one from the getRuntime method.
27e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom *
28e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * @hide
29e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom */
30e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrompublic final class VMRuntime {
31e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
32e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
33e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Holds the VMRuntime singleton.
34e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
35e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    private static final VMRuntime THE_ONE = new VMRuntime();
36e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
37371d2b005a4c1769e10179ccaaedd67e6a4946daCalin Juravle    // Note: Instruction set names are used to construct the names of some
38371d2b005a4c1769e10179ccaaedd67e6a4946daCalin Juravle    // system properties. To be sure that the properties stay valid the
39371d2b005a4c1769e10179ccaaedd67e6a4946daCalin Juravle    // instruction set name should not exceed 7 characters. See installd
40371d2b005a4c1769e10179ccaaedd67e6a4946daCalin Juravle    // and the package manager for the actual propeties.
41a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath    private static final Map<String, String> ABI_TO_INSTRUCTION_SET_MAP
425ea9303bf3e5ceba5adbe106d3ebf2e0c39d749eNarayan Kamath            = new HashMap<String, String>(16);
43a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath    static {
44a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath        ABI_TO_INSTRUCTION_SET_MAP.put("armeabi", "arm");
45a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath        ABI_TO_INSTRUCTION_SET_MAP.put("armeabi-v7a", "arm");
46a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath        ABI_TO_INSTRUCTION_SET_MAP.put("mips", "mips");
4751b69d37a34a44e045a87d0950a0602be415c126Narayan Kamath        ABI_TO_INSTRUCTION_SET_MAP.put("mips64", "mips64");
48a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath        ABI_TO_INSTRUCTION_SET_MAP.put("x86", "x86");
49a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath        ABI_TO_INSTRUCTION_SET_MAP.put("x86_64", "x86_64");
50a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath        ABI_TO_INSTRUCTION_SET_MAP.put("arm64-v8a", "arm64");
51a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath    }
52a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath
531ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin    private int targetSdkVersion;
541ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin
55e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
56e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Prevents this class from being instantiated.
57e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
58e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    private VMRuntime() {
59e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
60e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
61e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
62e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Returns the object that represents the VM instance's Dalvik-specific
63e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * runtime environment.
64e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     *
65e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * @return the runtime object
66e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
67e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public static VMRuntime getRuntime() {
68e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        return THE_ONE;
69e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
70e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
71e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
72e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Returns a copy of the VM's command-line property settings.
73e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * These are in the form "name=value" rather than "-Dname=value".
74e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
75e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public native String[] properties();
76e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
77e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
78e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Returns the VM's boot class path.
79e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
80e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public native String bootClassPath();
81e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
82e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
83e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Returns the VM's class path.
84e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
85e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public native String classPath();
86e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
87e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
88e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Returns the VM's version.
89e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
90e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public native String vmVersion();
91e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
92e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
9326376765d7333ddf04c021365a1eadd9d47feb7eBrian Carlstrom     * Returns the name of the shared library providing the VM implementation.
9426376765d7333ddf04c021365a1eadd9d47feb7eBrian Carlstrom     */
9526376765d7333ddf04c021365a1eadd9d47feb7eBrian Carlstrom    public native String vmLibrary();
9626376765d7333ddf04c021365a1eadd9d47feb7eBrian Carlstrom
9726376765d7333ddf04c021365a1eadd9d47feb7eBrian Carlstrom    /**
98451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz     * Returns the VM's instruction set.
99451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz     */
100451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz    public native String vmInstructionSet();
101451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz
102451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz    /**
103451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz     * Returns whether the VM is running in 64-bit mode.
104451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz     */
105451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz    public native boolean is64Bit();
106451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz
107451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz    /**
108451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz     * Returns whether the VM is running with JNI checking enabled.
109451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz     */
110451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz    public native boolean isCheckJniEnabled();
111451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz
112451d99525dc8fef247e6fe6f4c714cbdf7f8b322Sebastien Hertz    /**
113e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Gets the current ideal heap utilization, represented as a number
114e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * between zero and one.  After a GC happens, the Dalvik heap may
115e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * be resized so that (size of live objects) / (size of heap) is
116e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * equal to this number.
117e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     *
118e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * @return the current ideal heap utilization
119e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
120e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public native float getTargetHeapUtilization();
121e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
122e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
123e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Sets the current ideal heap utilization, represented as a number
124e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * between zero and one.  After a GC happens, the Dalvik heap may
125e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * be resized so that (size of live objects) / (size of heap) is
126e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * equal to this number.
127e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     *
128e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * <p>This is only a hint to the garbage collector and may be ignored.
129e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     *
130e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * @param newTarget the new suggested ideal heap utilization.
131e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     *                  This value may be adjusted internally.
132e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * @return the previous ideal heap utilization
133e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * @throws IllegalArgumentException if newTarget is &lt;= 0.0 or &gt;= 1.0
134e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
135e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public float setTargetHeapUtilization(float newTarget) {
136e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        if (newTarget <= 0.0f || newTarget >= 1.0f) {
137e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom            throw new IllegalArgumentException(newTarget +
138e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom                    " out of range (0,1)");
139e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        }
140e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        /* Synchronize to make sure that only one thread gets
141e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom         * a given "old" value if both update at the same time.
142e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom         * Allows for reliable save-and-restore semantics.
143e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom         */
144e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        synchronized (this) {
145e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom            float oldTarget = getTargetHeapUtilization();
146e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom            nativeSetTargetHeapUtilization(newTarget);
147e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom            return oldTarget;
148e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        }
149e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
150e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
151e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
152e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Sets the target SDK version. Should only be called before the
153e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * app starts to run, because it may change the VM's behavior in
154e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * dangerous ways. Use 0 to mean "current" (since callers won't
155e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * necessarily know the actual current SDK version, and the
1561ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin     * allocated version numbers start at 1), and 10000 to mean
1571ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin     * CUR_DEVELOPMENT.
158e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
1591ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin    public synchronized void setTargetSdkVersion(int targetSdkVersion) {
1601ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin        this.targetSdkVersion = targetSdkVersion;
1611ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin        setTargetSdkVersionNative(this.targetSdkVersion);
1621ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin    }
1631ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin
1641ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin    /**
1651ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin     * Gets the target SDK version. See {@link #setTargetSdkVersion} for
1661ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin     * special values.
1671ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin     */
1681ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin    public synchronized int getTargetSdkVersion() {
1691ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin        return targetSdkVersion;
1701ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin    }
1711ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin
1721ec5a34b4367fd7c44266c5bdd363ea5cfac7a67Alex Klyubin    private native void setTargetSdkVersionNative(int targetSdkVersion);
173e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
174e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
175e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * This method exists for binary compatibility.  It was part of a
176413d4592ee114eac81014af4b6347e73873ce8ceElliott Hughes     * heap sizing API which was removed in Android 3.0 (Honeycomb).
177e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
178e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    @Deprecated
179e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public long getMinimumHeapSize() {
180e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        return 0;
181e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
182e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
183e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
184e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * This method exists for binary compatibility.  It was part of a
185413d4592ee114eac81014af4b6347e73873ce8ceElliott Hughes     * heap sizing API which was removed in Android 3.0 (Honeycomb).
186e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
187e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    @Deprecated
188e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public long setMinimumHeapSize(long size) {
189e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        return 0;
190e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
191e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
192e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
193e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * This method exists for binary compatibility.  It used to
194e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * perform a garbage collection that cleared SoftReferences.
195e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
196e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    @Deprecated
197e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public void gcSoftReferences() {}
198e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
199e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
200e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * This method exists for binary compatibility.  It is equivalent
201e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * to {@link System#runFinalization}.
202e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
203e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    @Deprecated
204e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public void runFinalizationSync() {
205e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        System.runFinalization();
206e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
207e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
208e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
209e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Implements setTargetHeapUtilization().
210e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     *
211e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * @param newTarget the new suggested ideal heap utilization.
212e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     *                  This value may be adjusted internally.
213e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
214e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    private native void nativeSetTargetHeapUtilization(float newTarget);
215e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
216e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
217e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * This method exists for binary compatibility.  It was part of
218413d4592ee114eac81014af4b6347e73873ce8ceElliott Hughes     * the external allocation API which was removed in Android 3.0 (Honeycomb).
219e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
220e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    @Deprecated
221e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public boolean trackExternalAllocation(long size) {
222e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        return true;
223e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
224e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
225e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
226e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * This method exists for binary compatibility.  It was part of
227413d4592ee114eac81014af4b6347e73873ce8ceElliott Hughes     * the external allocation API which was removed in Android 3.0 (Honeycomb).
228e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
229e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    @Deprecated
230e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public void trackExternalFree(long size) {}
231e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
232e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
233e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * This method exists for binary compatibility.  It was part of
234413d4592ee114eac81014af4b6347e73873ce8ceElliott Hughes     * the external allocation API which was removed in Android 3.0 (Honeycomb).
235e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
236e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    @Deprecated
237e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public long getExternalBytesAllocated() {
238e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        return 0;
239e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
240e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
241e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
242e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Tells the VM to enable the JIT compiler. If the VM does not have a JIT
243e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * implementation, calling this method should have no effect.
244e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
245e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public native void startJitCompilation();
246e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
247e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
248e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Tells the VM to disable the JIT compiler. If the VM does not have a JIT
249e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * implementation, calling this method should have no effect.
250e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
251e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public native void disableJitCompilation();
252e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
253e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
254e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Returns an array allocated in an area of the Java heap where it will never be moved.
255e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * This is used to implement native allocations on the Java heap, such as DirectByteBuffers
256e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * and Bitmaps.
257e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
258e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public native Object newNonMovableArray(Class<?> componentType, int length);
259e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
260e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
26110af76d5af1d87686ff9ab8d00f3dadc15a84fb7Ian Rogers     * Returns an array of at least minLength, but potentially larger. The increased size comes from
26210af76d5af1d87686ff9ab8d00f3dadc15a84fb7Ian Rogers     * avoiding any padding after the array. The amount of padding varies depending on the
26310af76d5af1d87686ff9ab8d00f3dadc15a84fb7Ian Rogers     * componentType and the memory allocator implementation.
26410af76d5af1d87686ff9ab8d00f3dadc15a84fb7Ian Rogers     */
26510af76d5af1d87686ff9ab8d00f3dadc15a84fb7Ian Rogers    public native Object newUnpaddedArray(Class<?> componentType, int minLength);
26610af76d5af1d87686ff9ab8d00f3dadc15a84fb7Ian Rogers
26710af76d5af1d87686ff9ab8d00f3dadc15a84fb7Ian Rogers    /**
268e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Returns the address of array[0]. This differs from using JNI in that JNI might lie and
269e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * give you the address of a copy of the array when in forcecopy mode.
270e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
271e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public native long addressOf(Object array);
272e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
273e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
274e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Removes any growth limits, allowing the application to allocate
275e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * up to the maximum heap size.
276e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
277e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public native void clearGrowthLimit();
278e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
279e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    /**
28016936e0db519ae4a0e2e5904e70a5ed6d2f26152Mathieu Chartier     * Make the current growth limit the new non growth limit capacity by releasing pages which
28116936e0db519ae4a0e2e5904e70a5ed6d2f26152Mathieu Chartier     * are after the growth limit but before the non growth limit capacity.
28216936e0db519ae4a0e2e5904e70a5ed6d2f26152Mathieu Chartier     */
28316936e0db519ae4a0e2e5904e70a5ed6d2f26152Mathieu Chartier    public native void clampGrowthLimit();
28416936e0db519ae4a0e2e5904e70a5ed6d2f26152Mathieu Chartier
28516936e0db519ae4a0e2e5904e70a5ed6d2f26152Mathieu Chartier    /**
286e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     * Returns true if either a Java debugger or native debugger is active.
287e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom     */
288e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public native boolean isDebuggerActive();
289e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
290ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier    /**
2910e28d3557851a7a17ca826501942bd26852c8287Oleksiy Vyalov     * Returns true if native debugging is on.
2920e28d3557851a7a17ca826501942bd26852c8287Oleksiy Vyalov     */
2930e28d3557851a7a17ca826501942bd26852c8287Oleksiy Vyalov    public native boolean isNativeDebuggable();
2940e28d3557851a7a17ca826501942bd26852c8287Oleksiy Vyalov
2950e28d3557851a7a17ca826501942bd26852c8287Oleksiy Vyalov    /**
296ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier     * Registers a native allocation so that the heap knows about it and performs GC as required.
297ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier     * If the number of native allocated bytes exceeds the native allocation watermark, the
298ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier     * function requests a concurrent GC. If the native bytes allocated exceeds a second higher
299ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier     * watermark, it is determined that the application is registering native allocations at an
300ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier     * unusually high rate and a GC is performed inside of the function to prevent memory usage
301ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier     * from excessively increasing.
302ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier     */
303ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier    public native void registerNativeAllocation(int bytes);
304ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier
305ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier    /**
306ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier     * Registers a native free by reducing the number of native bytes accounted for.
307ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier     */
308ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier    public native void registerNativeFree(int bytes);
309ddd074801339ef32237f38fb99b43a8844201d89Mathieu Chartier
31051855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier    /**
31151855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier     * Wait for objects to be finalized.
31251855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier     *
31351855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier     * If finalization takes longer than timeout, then the function returns before all objects are
31451855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier     * finalized.
31551855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier     *
31651855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier     * @param timeout
31751855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier     *            timeout in nanoseconds of the maximum time to wait until all pending finalizers
31851855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier     *            are run. If timeout is 0, then there is no timeout. Note that the timeout does
31951855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier     *            not stop the finalization process, it merely stops the wait.
32051855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier     *
32151855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier     * @see #Runtime.runFinalization()
32251855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier     * @see #wait(long,int)
32351855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier     */
32451855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier    public static void runFinalization(long timeout) {
32551855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier        try {
32651855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier            FinalizerReference.finalizeAllEnqueued(timeout);
32751855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier        } catch (InterruptedException e) {
32851855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier            // Interrupt the current thread without actually throwing the InterruptionException
32951855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier            // for the caller.
33051855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier            Thread.currentThread().interrupt();
33151855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier        }
33251855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier    }
33351855ccb72d01a28babedbaed70974d4f3697ab0Mathieu Chartier
334a57efa2083afd417b0850a0e2a904780c90e1e1dMathieu Chartier    public native void requestConcurrentGC();
335152be540e335376f66ed662d8e63e601a09cb4b3Mathieu Chartier    public native void concurrentGC();
336152be540e335376f66ed662d8e63e601a09cb4b3Mathieu Chartier    public native void requestHeapTrim();
337152be540e335376f66ed662d8e63e601a09cb4b3Mathieu Chartier    public native void trimHeap();
338152be540e335376f66ed662d8e63e601a09cb4b3Mathieu Chartier    public native void startHeapTaskProcessor();
339152be540e335376f66ed662d8e63e601a09cb4b3Mathieu Chartier    public native void stopHeapTaskProcessor();
340152be540e335376f66ed662d8e63e601a09cb4b3Mathieu Chartier    public native void runHeapTasks();
341c8cfc667ddb82b08b889a24831a3d00cc6490bd9Brian Carlstrom
34265e6acab4f777245dc73f40ecc3b42b065d21ed2Brian Carlstrom    /**
3430c85c33c5b400c0d32cf8c77d3bca704b7cca1e7Mathieu Chartier     * Let the heap know of the new process state. This can change allocation and garbage collection
3440c85c33c5b400c0d32cf8c77d3bca704b7cca1e7Mathieu Chartier     * behavior regarding trimming and compaction.
3450c85c33c5b400c0d32cf8c77d3bca704b7cca1e7Mathieu Chartier     */
3460c85c33c5b400c0d32cf8c77d3bca704b7cca1e7Mathieu Chartier    public native void updateProcessState(int state);
3470c85c33c5b400c0d32cf8c77d3bca704b7cca1e7Mathieu Chartier
3480c85c33c5b400c0d32cf8c77d3bca704b7cca1e7Mathieu Chartier    /**
34965e6acab4f777245dc73f40ecc3b42b065d21ed2Brian Carlstrom     * Fill in dex caches with classes, fields, and methods that are
35065e6acab4f777245dc73f40ecc3b42b065d21ed2Brian Carlstrom     * already loaded. Typically used after Zygote preloading.
35165e6acab4f777245dc73f40ecc3b42b065d21ed2Brian Carlstrom     */
35265e6acab4f777245dc73f40ecc3b42b065d21ed2Brian Carlstrom    public native void preloadDexCaches();
353eee45fe9d744ed1d9a505f020129430f4b733473Dave Allison
354eee45fe9d744ed1d9a505f020129430f4b733473Dave Allison    /**
355eee45fe9d744ed1d9a505f020129430f4b733473Dave Allison     * Register application info
356eee45fe9d744ed1d9a505f020129430f4b733473Dave Allison     */
357d7743c7b88afe3f41fc2f2db4051cb0071492150Calin Juravle    public static native void registerAppInfo(String packageName, String appDir,
358b4ffda4b3d8cc818dbc6f8eba3d974a4acf4da66Calin Juravle             String[] codePaths, String foreignDexProfileDir);
359a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath
360a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath    /**
361a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath     * Returns the runtime instruction set corresponding to a given ABI. Multiple
362a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath     * compatible ABIs might map to the same instruction set. For example
363a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath     * {@code armeabi-v7a} and {@code armeabi} might map to the instruction set {@code arm}.
364a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath     *
365a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath     * This influences the compilation of the applications classes.
366a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath     */
367a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath    public static String getInstructionSet(String abi) {
368a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath        final String instructionSet = ABI_TO_INSTRUCTION_SET_MAP.get(abi);
369a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath        if (instructionSet == null) {
370a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath            throw new IllegalArgumentException("Unsupported ABI: " + abi);
371a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath        }
372a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath
373a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath        return instructionSet;
374a607cc9c4cd4e21df39e549c14dac81c5cd18922Narayan Kamath    }
37551b69d37a34a44e045a87d0950a0602be415c126Narayan Kamath
37651b69d37a34a44e045a87d0950a0602be415c126Narayan Kamath    public static boolean is64BitInstructionSet(String instructionSet) {
37751b69d37a34a44e045a87d0950a0602be415c126Narayan Kamath        return "arm64".equals(instructionSet) ||
37851b69d37a34a44e045a87d0950a0602be415c126Narayan Kamath                "x86_64".equals(instructionSet) ||
37951b69d37a34a44e045a87d0950a0602be415c126Narayan Kamath                "mips64".equals(instructionSet);
38051b69d37a34a44e045a87d0950a0602be415c126Narayan Kamath    }
38151b69d37a34a44e045a87d0950a0602be415c126Narayan Kamath
38251b69d37a34a44e045a87d0950a0602be415c126Narayan Kamath    public static boolean is64BitAbi(String abi) {
38351b69d37a34a44e045a87d0950a0602be415c126Narayan Kamath        return is64BitInstructionSet(getInstructionSet(abi));
38451b69d37a34a44e045a87d0950a0602be415c126Narayan Kamath    }
385852d9339528720340e14451fbd4ad1795fb9e7e4Brian Carlstrom
386852d9339528720340e14451fbd4ad1795fb9e7e4Brian Carlstrom    /**
387852d9339528720340e14451fbd4ad1795fb9e7e4Brian Carlstrom     * Return false if the boot class path for the given instruction
388852d9339528720340e14451fbd4ad1795fb9e7e4Brian Carlstrom     * set mapped from disk storage, versus being interpretted from
389852d9339528720340e14451fbd4ad1795fb9e7e4Brian Carlstrom     * dirty pages in memory.
390852d9339528720340e14451fbd4ad1795fb9e7e4Brian Carlstrom     */
391852d9339528720340e14451fbd4ad1795fb9e7e4Brian Carlstrom    public static native boolean isBootClassPathOnDisk(String instructionSet);
392852d9339528720340e14451fbd4ad1795fb9e7e4Brian Carlstrom
393852d9339528720340e14451fbd4ad1795fb9e7e4Brian Carlstrom    /**
394852d9339528720340e14451fbd4ad1795fb9e7e4Brian Carlstrom     * Returns the instruction set of the current runtime.
395852d9339528720340e14451fbd4ad1795fb9e7e4Brian Carlstrom     */
396852d9339528720340e14451fbd4ad1795fb9e7e4Brian Carlstrom    public static native String getCurrentInstructionSet();
397852d9339528720340e14451fbd4ad1795fb9e7e4Brian Carlstrom
3980eedcb21289c1916e876810982c2c8d473edf24fAndreas Gampe    /**
3990eedcb21289c1916e876810982c2c8d473edf24fAndreas Gampe     * Return true if the dalvik cache was pruned when booting. This may have happened for
4000eedcb21289c1916e876810982c2c8d473edf24fAndreas Gampe     * various reasons, e.g., after an OTA. The return value is for the current instruction
4010eedcb21289c1916e876810982c2c8d473edf24fAndreas Gampe     * set.
4020eedcb21289c1916e876810982c2c8d473edf24fAndreas Gampe     */
4030eedcb21289c1916e876810982c2c8d473edf24fAndreas Gampe    public static native boolean didPruneDalvikCache();
4044fa4dcc6493bf71a775f3ec097251c8704b5e63dCalin Juravle
4054fa4dcc6493bf71a775f3ec097251c8704b5e63dCalin Juravle    /**
4064fa4dcc6493bf71a775f3ec097251c8704b5e63dCalin Juravle     * Register the current execution thread to the runtime as sensitive thread.
4074fa4dcc6493bf71a775f3ec097251c8704b5e63dCalin Juravle     * Should be called just once. Subsequent calls are ignored.
4084fa4dcc6493bf71a775f3ec097251c8704b5e63dCalin Juravle     */
4094fa4dcc6493bf71a775f3ec097251c8704b5e63dCalin Juravle    public static native void registerSensitiveThread();
410e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom}
411