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 | |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame^] | 19 | import android.annotation.NonNull; |
| 20 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.os.IBinder; |
| 22 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.util.EventLog; |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame^] | 24 | import android.util.Log; |
| 25 | import android.util.SparseIntArray; |
| 26 | |
| 27 | import com.android.internal.util.Preconditions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
Hiroshi Yamauchi | dfefe2d | 2015-05-05 12:15:26 -0700 | [diff] [blame] | 29 | import dalvik.system.VMRuntime; |
| 30 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import java.lang.ref.WeakReference; |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 32 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Private and debugging Binder APIs. |
| 36 | * |
| 37 | * @see IBinder |
| 38 | */ |
| 39 | public class BinderInternal { |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame^] | 40 | private static final String TAG = "BinderInternal"; |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 41 | static WeakReference<GcWatcher> sGcWatcher |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | = new WeakReference<GcWatcher>(new GcWatcher()); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 43 | static ArrayList<Runnable> sGcWatchers = new ArrayList<>(); |
| 44 | static Runnable[] sTmpWatchers = new Runnable[1]; |
| 45 | static long sLastGcTime; |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame^] | 46 | static final BinderProxyLimitListenerDelegate sBinderProxyLimitListenerDelegate = |
| 47 | new BinderProxyLimitListenerDelegate(); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 48 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | static final class GcWatcher { |
| 50 | @Override |
| 51 | protected void finalize() throws Throwable { |
| 52 | handleGc(); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 53 | sLastGcTime = SystemClock.uptimeMillis(); |
| 54 | synchronized (sGcWatchers) { |
| 55 | sTmpWatchers = sGcWatchers.toArray(sTmpWatchers); |
| 56 | } |
| 57 | for (int i=0; i<sTmpWatchers.length; i++) { |
| 58 | if (sTmpWatchers[i] != null) { |
| 59 | sTmpWatchers[i].run(); |
| 60 | } |
| 61 | } |
| 62 | sGcWatcher = new WeakReference<GcWatcher>(new GcWatcher()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 65 | |
| 66 | public static void addGcWatcher(Runnable watcher) { |
| 67 | synchronized (sGcWatchers) { |
| 68 | sGcWatchers.add(watcher); |
| 69 | } |
| 70 | } |
| 71 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | /** |
| 73 | * Add the calling thread to the IPC thread pool. This function does |
| 74 | * not return until the current process is exiting. |
| 75 | */ |
| 76 | public static final native void joinThreadPool(); |
| 77 | |
| 78 | /** |
| 79 | * Return the system time (as reported by {@link SystemClock#uptimeMillis |
| 80 | * SystemClock.uptimeMillis()}) that the last garbage collection occurred |
| 81 | * in this process. This is not for general application use, and the |
| 82 | * meaning of "when a garbage collection occurred" will change as the |
| 83 | * garbage collector evolves. |
| 84 | * |
| 85 | * @return Returns the time as per {@link SystemClock#uptimeMillis |
| 86 | * SystemClock.uptimeMillis()} of the last garbage collection. |
| 87 | */ |
| 88 | public static long getLastGcTime() { |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 89 | return sLastGcTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Return the global "context object" of the system. This is usually |
| 94 | * an implementation of IServiceManager, which you can use to find |
| 95 | * other services. |
| 96 | */ |
| 97 | public static final native IBinder getContextObject(); |
| 98 | |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Special for system process to not allow incoming calls to run at |
| 101 | * background scheduling priority. |
| 102 | * @hide |
| 103 | */ |
| 104 | public static final native void disableBackgroundScheduling(boolean disable); |
Tim Murray | eef4a3d | 2016-04-19 14:14:20 -0700 | [diff] [blame] | 105 | |
| 106 | public static final native void setMaxThreads(int numThreads); |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 107 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | static native final void handleGc(); |
| 109 | |
| 110 | public static void forceGc(String reason) { |
| 111 | EventLog.writeEvent(2741, reason); |
Hiroshi Yamauchi | dfefe2d | 2015-05-05 12:15:26 -0700 | [diff] [blame] | 112 | VMRuntime.getRuntime().requestConcurrentGC(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static void forceBinderGc() { |
| 116 | forceGc("Binder"); |
| 117 | } |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame^] | 118 | |
| 119 | /** |
| 120 | * Enable/disable Binder Proxy Instance Counting by Uid. While enabled, the set callback will |
| 121 | * be called if this process holds too many Binder Proxies on behalf of a Uid. |
| 122 | * @param enabled true to enable counting, false to disable |
| 123 | */ |
| 124 | public static final native void nSetBinderProxyCountEnabled(boolean enabled); |
| 125 | |
| 126 | /** |
| 127 | * Get the current number of Binder Proxies held for each uid. |
| 128 | * @return SparseIntArray mapping uids to the number of Binder Proxies currently held |
| 129 | */ |
| 130 | public static final native SparseIntArray nGetBinderProxyPerUidCounts(); |
| 131 | |
| 132 | /** |
| 133 | * Get the current number of Binder Proxies held for an individual uid. |
| 134 | * @param uid Requested uid for Binder Proxy count |
| 135 | * @return int with the number of Binder proxies held for a uid |
| 136 | */ |
| 137 | public static final native int nGetBinderProxyCount(int uid); |
| 138 | |
| 139 | /** |
| 140 | * Set the Binder Proxy watermarks. Default high watermark = 2500. Default low watermark = 2000 |
| 141 | * @param high The limit at which the BinderProxyListener callback will be called. |
| 142 | * @param low The threshold a binder count must drop below before the callback |
| 143 | * can be called again. (This is to avoid many repeated calls to the |
| 144 | * callback in a brief period of time) |
| 145 | */ |
| 146 | public static final native void nSetBinderProxyCountWatermarks(int high, int low); |
| 147 | |
| 148 | /** |
| 149 | * Interface for callback invocation when the Binder Proxy limit is reached. onLimitReached will |
| 150 | * be called with the uid of the app causing too many Binder Proxies |
| 151 | */ |
| 152 | public interface BinderProxyLimitListener { |
| 153 | public void onLimitReached(int uid); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Callback used by native code to trigger a callback in java code. The callback will be |
| 158 | * triggered when too many binder proxies from a uid hits the allowed limit. |
| 159 | * @param uid The uid of the bad behaving app sending too many binders |
| 160 | */ |
| 161 | public static void binderProxyLimitCallbackFromNative(int uid) { |
| 162 | sBinderProxyLimitListenerDelegate.notifyClient(uid); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Set a callback to be triggered when a uid's Binder Proxy limit is reached for this process. |
| 167 | * @param listener OnLimitReached of listener will be called in the thread provided by handler |
| 168 | * @param handler must not be null, callback will be posted through the handler; |
| 169 | * |
| 170 | */ |
| 171 | public static void setBinderProxyCountCallback(BinderProxyLimitListener listener, |
| 172 | @NonNull Handler handler) { |
| 173 | Preconditions.checkNotNull(handler, |
| 174 | "Must provide NonNull Handler to setBinderProxyCountCallback when setting " |
| 175 | + "BinderProxyLimitListener"); |
| 176 | sBinderProxyLimitListenerDelegate.setListener(listener, handler); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Clear the Binder Proxy callback |
| 181 | */ |
| 182 | public static void clearBinderProxyCountCallback() { |
| 183 | sBinderProxyLimitListenerDelegate.setListener(null, null); |
| 184 | } |
| 185 | |
| 186 | static private class BinderProxyLimitListenerDelegate { |
| 187 | private BinderProxyLimitListener mBinderProxyLimitListener; |
| 188 | private Handler mHandler; |
| 189 | |
| 190 | void setListener(BinderProxyLimitListener listener, Handler handler) { |
| 191 | synchronized (this) { |
| 192 | mBinderProxyLimitListener = listener; |
| 193 | mHandler = handler; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void notifyClient(final int uid) { |
| 198 | synchronized (this) { |
| 199 | if (mBinderProxyLimitListener != null) { |
| 200 | mHandler.post(new Runnable() { |
| 201 | @Override |
| 202 | public void run() { |
| 203 | mBinderProxyLimitListener.onLimitReached(uid); |
| 204 | } |
| 205 | }); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | } |