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 dalvik.system.VMRuntime;
24 
25 import java.lang.ref.WeakReference;
26 import java.util.ArrayList;
27 
28 /**
29  * Private and debugging Binder APIs.
30  *
31  * @see IBinder
32  */
33 public class BinderInternal {
34     static WeakReference<GcWatcher> sGcWatcher
35             = new WeakReference<GcWatcher>(new GcWatcher());
36     static ArrayList<Runnable> sGcWatchers = new ArrayList<>();
37     static Runnable[] sTmpWatchers = new Runnable[1];
38     static long sLastGcTime;
39 
40     static final class GcWatcher {
41         @Override
finalize()42         protected void finalize() throws Throwable {
43             handleGc();
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());
54         }
55     }
56 
addGcWatcher(Runnable watcher)57     public static void addGcWatcher(Runnable watcher) {
58         synchronized (sGcWatchers) {
59             sGcWatchers.add(watcher);
60         }
61     }
62 
63     /**
64      * Add the calling thread to the IPC thread pool.  This function does
65      * not return until the current process is exiting.
66      */
joinThreadPool()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      */
getLastGcTime()79     public static long getLastGcTime() {
80         return sLastGcTime;
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      */
getContextObject()88     public static final native IBinder getContextObject();
89 
90     /**
91      * Special for system process to not allow incoming calls to run at
92      * background scheduling priority.
93      * @hide
94      */
disableBackgroundScheduling(boolean disable)95     public static final native void disableBackgroundScheduling(boolean disable);
96 
setMaxThreads(int numThreads)97     public static final native void setMaxThreads(int numThreads);
98 
handleGc()99     static native final void handleGc();
100 
forceGc(String reason)101     public static void forceGc(String reason) {
102         EventLog.writeEvent(2741, reason);
103         VMRuntime.getRuntime().requestConcurrentGC();
104     }
105 
forceBinderGc()106     static void forceBinderGc() {
107         forceGc("Binder");
108     }
109 }
110