blob: ea4575aba9c0bbf5a20f12d7ad3995b4e68373b8 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.os.IBinder;
20import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.util.EventLog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
Hiroshi Yamauchidfefe2d2015-05-05 12:15:26 -070023import dalvik.system.VMRuntime;
24
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import java.lang.ref.WeakReference;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070026import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
28/**
29 * Private and debugging Binder APIs.
30 *
31 * @see IBinder
32 */
33public class BinderInternal {
Dianne Hackborn89ad4562014-08-24 16:45:38 -070034 static WeakReference<GcWatcher> sGcWatcher
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 = new WeakReference<GcWatcher>(new GcWatcher());
Dianne Hackborn89ad4562014-08-24 16:45:38 -070036 static ArrayList<Runnable> sGcWatchers = new ArrayList<>();
37 static Runnable[] sTmpWatchers = new Runnable[1];
38 static long sLastGcTime;
39
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 static final class GcWatcher {
41 @Override
42 protected void finalize() throws Throwable {
43 handleGc();
Dianne Hackborn89ad4562014-08-24 16:45:38 -070044 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());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 }
55 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -070056
57 public static void addGcWatcher(Runnable watcher) {
58 synchronized (sGcWatchers) {
59 sGcWatchers.add(watcher);
60 }
61 }
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 /**
64 * Add the calling thread to the IPC thread pool. This function does
65 * not return until the current process is exiting.
66 */
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 */
79 public static long getLastGcTime() {
Dianne Hackborn89ad4562014-08-24 16:45:38 -070080 return sLastGcTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 }
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 */
88 public static final native IBinder getContextObject();
89
Dianne Hackborn887f3552009-12-07 17:59:37 -080090 /**
91 * Special for system process to not allow incoming calls to run at
92 * background scheduling priority.
93 * @hide
94 */
95 public static final native void disableBackgroundScheduling(boolean disable);
Tim Murrayeef4a3d2016-04-19 14:14:20 -070096
97 public static final native void setMaxThreads(int numThreads);
Dianne Hackborn887f3552009-12-07 17:59:37 -080098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 static native final void handleGc();
100
101 public static void forceGc(String reason) {
102 EventLog.writeEvent(2741, reason);
Hiroshi Yamauchidfefe2d2015-05-05 12:15:26 -0700103 VMRuntime.getRuntime().requestConcurrentGC();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 }
105
106 static void forceBinderGc() {
107 forceGc("Binder");
108 }
109}