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