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