blob: 5bddd2f9898319a89b6ddca1529e8d3210f4dde7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.internal.os;
18
Michael Wachenschwanz55182462017-08-14 23:10:13 -070019import android.annotation.NonNull;
20import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.os.IBinder;
22import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.util.EventLog;
Michael Wachenschwanz55182462017-08-14 23:10:13 -070024import android.util.Log;
25import android.util.SparseIntArray;
26
27import com.android.internal.util.Preconditions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Hiroshi Yamauchidfefe2d2015-05-05 12:15:26 -070029import dalvik.system.VMRuntime;
30
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import java.lang.ref.WeakReference;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070032import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34/**
35 * Private and debugging Binder APIs.
36 *
37 * @see IBinder
38 */
39public class BinderInternal {
Michael Wachenschwanz55182462017-08-14 23:10:13 -070040 private static final String TAG = "BinderInternal";
Dianne Hackborn89ad4562014-08-24 16:45:38 -070041 static WeakReference<GcWatcher> sGcWatcher
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 = new WeakReference<GcWatcher>(new GcWatcher());
Dianne Hackborn89ad4562014-08-24 16:45:38 -070043 static ArrayList<Runnable> sGcWatchers = new ArrayList<>();
44 static Runnable[] sTmpWatchers = new Runnable[1];
45 static long sLastGcTime;
Michael Wachenschwanz55182462017-08-14 23:10:13 -070046 static final BinderProxyLimitListenerDelegate sBinderProxyLimitListenerDelegate =
47 new BinderProxyLimitListenerDelegate();
Dianne Hackborn89ad4562014-08-24 16:45:38 -070048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 static final class GcWatcher {
50 @Override
51 protected void finalize() throws Throwable {
52 handleGc();
Dianne Hackborn89ad4562014-08-24 16:45:38 -070053 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 Project9066cfe2009-03-03 19:31:44 -080063 }
64 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -070065
66 public static void addGcWatcher(Runnable watcher) {
67 synchronized (sGcWatchers) {
68 sGcWatchers.add(watcher);
69 }
70 }
71
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 /**
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 Hackborn89ad4562014-08-24 16:45:38 -070089 return sLastGcTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 }
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 Hackborn887f3552009-12-07 17:59:37 -080099 /**
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 Murrayeef4a3d2016-04-19 14:14:20 -0700105
106 public static final native void setMaxThreads(int numThreads);
Dianne Hackborn887f3552009-12-07 17:59:37 -0800107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 static native final void handleGc();
109
110 public static void forceGc(String reason) {
111 EventLog.writeEvent(2741, reason);
Hiroshi Yamauchidfefe2d2015-05-05 12:15:26 -0700112 VMRuntime.getRuntime().requestConcurrentGC();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 }
114
115 static void forceBinderGc() {
116 forceGc("Binder");
117 }
Michael Wachenschwanz55182462017-08-14 23:10:13 -0700118
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 Project9066cfe2009-03-03 19:31:44 -0800210}