The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | package com.android.internal.os; |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.os.IBinder; |
| 20 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.util.EventLog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
Hiroshi Yamauchi | dfefe2d | 2015-05-05 12:15:26 -0700 | [diff] [blame^] | 23 | import dalvik.system.VMRuntime; |
| 24 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import java.lang.ref.WeakReference; |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 26 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Private and debugging Binder APIs. |
| 30 | * |
| 31 | * @see IBinder |
| 32 | */ |
| 33 | public class BinderInternal { |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 34 | static WeakReference<GcWatcher> sGcWatcher |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | = new WeakReference<GcWatcher>(new GcWatcher()); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 36 | static ArrayList<Runnable> sGcWatchers = new ArrayList<>(); |
| 37 | static Runnable[] sTmpWatchers = new Runnable[1]; |
| 38 | static long sLastGcTime; |
| 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | static final class GcWatcher { |
| 41 | @Override |
| 42 | protected void finalize() throws Throwable { |
| 43 | handleGc(); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 44 | sLastGcTime = SystemClock.uptimeMillis(); |
| 45 | synchronized (sGcWatchers) { |
| 46 | sTmpWatchers = sGcWatchers.toArray(sTmpWatchers); |
| 47 | } |
| 48 | for (int i=0; i<sTmpWatchers.length; i++) { |
| 49 | if (sTmpWatchers[i] != null) { |
| 50 | sTmpWatchers[i].run(); |
| 51 | } |
| 52 | } |
| 53 | sGcWatcher = new WeakReference<GcWatcher>(new GcWatcher()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | } |
| 55 | } |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 56 | |
| 57 | public static void addGcWatcher(Runnable watcher) { |
| 58 | synchronized (sGcWatchers) { |
| 59 | sGcWatchers.add(watcher); |
| 60 | } |
| 61 | } |
| 62 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Add the calling thread to the IPC thread pool. This function does |
| 65 | * not return until the current process is exiting. |
| 66 | */ |
| 67 | public static final native void joinThreadPool(); |
| 68 | |
| 69 | /** |
| 70 | * Return the system time (as reported by {@link SystemClock#uptimeMillis |
| 71 | * SystemClock.uptimeMillis()}) that the last garbage collection occurred |
| 72 | * in this process. This is not for general application use, and the |
| 73 | * meaning of "when a garbage collection occurred" will change as the |
| 74 | * garbage collector evolves. |
| 75 | * |
| 76 | * @return Returns the time as per {@link SystemClock#uptimeMillis |
| 77 | * SystemClock.uptimeMillis()} of the last garbage collection. |
| 78 | */ |
| 79 | public static long getLastGcTime() { |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 80 | return sLastGcTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Return the global "context object" of the system. This is usually |
| 85 | * an implementation of IServiceManager, which you can use to find |
| 86 | * other services. |
| 87 | */ |
| 88 | public static final native IBinder getContextObject(); |
| 89 | |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Special for system process to not allow incoming calls to run at |
| 92 | * background scheduling priority. |
| 93 | * @hide |
| 94 | */ |
| 95 | public static final native void disableBackgroundScheduling(boolean disable); |
| 96 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | static native final void handleGc(); |
| 98 | |
| 99 | public static void forceGc(String reason) { |
| 100 | EventLog.writeEvent(2741, reason); |
Hiroshi Yamauchi | dfefe2d | 2015-05-05 12:15:26 -0700 | [diff] [blame^] | 101 | VMRuntime.getRuntime().requestConcurrentGC(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void forceBinderGc() { |
| 105 | forceGc("Binder"); |
| 106 | } |
| 107 | } |