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 19 import android.annotation.NonNull; 20 import android.os.Handler; 21 import android.os.IBinder; 22 import android.os.SystemClock; 23 import android.util.EventLog; 24 import android.util.Log; 25 import android.util.SparseIntArray; 26 27 import com.android.internal.util.Preconditions; 28 29 import dalvik.system.VMRuntime; 30 31 import java.lang.ref.WeakReference; 32 import java.util.ArrayList; 33 34 /** 35 * Private and debugging Binder APIs. 36 * 37 * @see IBinder 38 */ 39 public class BinderInternal { 40 private static final String TAG = "BinderInternal"; 41 static WeakReference<GcWatcher> sGcWatcher 42 = new WeakReference<GcWatcher>(new GcWatcher()); 43 static ArrayList<Runnable> sGcWatchers = new ArrayList<>(); 44 static Runnable[] sTmpWatchers = new Runnable[1]; 45 static long sLastGcTime; 46 static final BinderProxyLimitListenerDelegate sBinderProxyLimitListenerDelegate = 47 new BinderProxyLimitListenerDelegate(); 48 49 static final class GcWatcher { 50 @Override finalize()51 protected void finalize() throws Throwable { 52 handleGc(); 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()); 63 } 64 } 65 addGcWatcher(Runnable watcher)66 public static void addGcWatcher(Runnable watcher) { 67 synchronized (sGcWatchers) { 68 sGcWatchers.add(watcher); 69 } 70 } 71 72 /** 73 * Add the calling thread to the IPC thread pool. This function does 74 * not return until the current process is exiting. 75 */ joinThreadPool()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 */ getLastGcTime()88 public static long getLastGcTime() { 89 return sLastGcTime; 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 */ getContextObject()97 public static final native IBinder getContextObject(); 98 99 /** 100 * Special for system process to not allow incoming calls to run at 101 * background scheduling priority. 102 * @hide 103 */ disableBackgroundScheduling(boolean disable)104 public static final native void disableBackgroundScheduling(boolean disable); 105 setMaxThreads(int numThreads)106 public static final native void setMaxThreads(int numThreads); 107 handleGc()108 static native final void handleGc(); 109 forceGc(String reason)110 public static void forceGc(String reason) { 111 EventLog.writeEvent(2741, reason); 112 VMRuntime.getRuntime().requestConcurrentGC(); 113 } 114 forceBinderGc()115 static void forceBinderGc() { 116 forceGc("Binder"); 117 } 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 */ nSetBinderProxyCountEnabled(boolean enabled)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 */ nGetBinderProxyPerUidCounts()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 */ nGetBinderProxyCount(int uid)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 */ nSetBinderProxyCountWatermarks(int high, int low)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 { onLimitReached(int uid)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 */ binderProxyLimitCallbackFromNative(int uid)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 */ setBinderProxyCountCallback(BinderProxyLimitListener listener, @NonNull Handler handler)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 */ clearBinderProxyCountCallback()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 setListener(BinderProxyLimitListener listener, Handler handler)190 void setListener(BinderProxyLimitListener listener, Handler handler) { 191 synchronized (this) { 192 mBinderProxyLimitListener = listener; 193 mHandler = handler; 194 } 195 } 196 notifyClient(final int uid)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 } 210 } 211