Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.server; |
| 18 | |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 19 | import android.annotation.NonNull; |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 20 | import android.os.Build; |
| 21 | import android.os.Process; |
| 22 | import android.util.Slog; |
| 23 | |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 24 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 25 | import com.android.internal.util.ConcurrentUtils; |
| 26 | import com.android.internal.util.Preconditions; |
Hui Yu | a4d089f | 2019-04-02 16:19:12 -0700 | [diff] [blame] | 27 | import com.android.server.am.ActivityManagerService; |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 28 | |
Hui Yu | a4d089f | 2019-04-02 16:19:12 -0700 | [diff] [blame] | 29 | import java.util.ArrayList; |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 30 | import java.util.List; |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 31 | import java.util.concurrent.ExecutorService; |
| 32 | import java.util.concurrent.Future; |
| 33 | import java.util.concurrent.TimeUnit; |
| 34 | |
| 35 | /** |
| 36 | * Thread pool used during initialization of system server. |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 37 | * |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 38 | * <p>System services can {@link #submit(Runnable)} tasks for execution during boot. |
| 39 | * The pool will be shut down after {@link SystemService#PHASE_BOOT_COMPLETED}. |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 40 | * |
| 41 | * <p>New tasks <em>should not</em> be submitted afterwards. |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 42 | * |
| 43 | * @hide |
| 44 | */ |
| 45 | public class SystemServerInitThreadPool { |
| 46 | private static final String TAG = SystemServerInitThreadPool.class.getSimpleName(); |
| 47 | private static final int SHUTDOWN_TIMEOUT_MILLIS = 20000; |
| 48 | private static final boolean IS_DEBUGGABLE = Build.IS_DEBUGGABLE; |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 49 | private static final Object LOCK = new Object(); |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 50 | |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 51 | @GuardedBy("LOCK") |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 52 | private static SystemServerInitThreadPool sInstance; |
| 53 | |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 54 | private final ExecutorService mService; |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 55 | |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 56 | @GuardedBy("mPendingTasks") |
| 57 | private final List<String> mPendingTasks = new ArrayList<>(); |
Hui Yu | a4d089f | 2019-04-02 16:19:12 -0700 | [diff] [blame] | 58 | |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 59 | @GuardedBy("mPendingTasks") |
| 60 | private boolean mShutDown; |
| 61 | |
| 62 | private SystemServerInitThreadPool() { |
| 63 | final int size = Runtime.getRuntime().availableProcessors(); |
| 64 | Slog.i(TAG, "Creating instance with " + size + " threads"); |
| 65 | mService = ConcurrentUtils.newFixedThreadPool(size, |
| 66 | "system-server-init-thread", Process.THREAD_PRIORITY_FOREGROUND); |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 69 | /** |
| 70 | * Gets the singleton. |
| 71 | * |
| 72 | * @throws IllegalStateException if it hasn't been started or has been shut down already. |
| 73 | */ |
| 74 | public static SystemServerInitThreadPool get() { |
| 75 | synchronized (LOCK) { |
| 76 | Preconditions.checkState(sInstance != null, "Cannot get " + TAG |
| 77 | + " - it has been shut down"); |
| 78 | return sInstance; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Submits a task for execution. |
| 84 | */ |
| 85 | public @NonNull Future<?> submit(@NonNull Runnable runnable, @NonNull String description) { |
| 86 | Preconditions.checkNotNull(description, "description cannot be null"); |
Hui Yu | a4d089f | 2019-04-02 16:19:12 -0700 | [diff] [blame] | 87 | synchronized (mPendingTasks) { |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 88 | Preconditions.checkState(!mShutDown, TAG + " already shut down"); |
Hui Yu | a4d089f | 2019-04-02 16:19:12 -0700 | [diff] [blame] | 89 | mPendingTasks.add(description); |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 90 | } |
Hui Yu | a4d089f | 2019-04-02 16:19:12 -0700 | [diff] [blame] | 91 | return mService.submit(() -> { |
| 92 | if (IS_DEBUGGABLE) { |
| 93 | Slog.d(TAG, "Started executing " + description); |
| 94 | } |
| 95 | try { |
| 96 | runnable.run(); |
| 97 | } catch (RuntimeException e) { |
| 98 | Slog.e(TAG, "Failure in " + description + ": " + e, e); |
| 99 | throw e; |
| 100 | } |
| 101 | synchronized (mPendingTasks) { |
| 102 | mPendingTasks.remove(description); |
| 103 | } |
| 104 | if (IS_DEBUGGABLE) { |
| 105 | Slog.d(TAG, "Finished executing " + description); |
| 106 | } |
| 107 | }); |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 110 | /** |
| 111 | * Starts it. |
| 112 | * |
| 113 | * <p>Note:</p> should only be called by {@link SystemServer}. |
| 114 | * |
| 115 | * @throws IllegalStateException if it has been started already without being shut down yet. |
| 116 | */ |
| 117 | static void start() { |
| 118 | synchronized (LOCK) { |
| 119 | Preconditions.checkState(sInstance == null, TAG + " already started"); |
| 120 | sInstance = new SystemServerInitThreadPool(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Shuts it down. |
| 126 | * |
| 127 | * <p>Note:</p> should only be called by {@link SystemServer}. |
| 128 | */ |
| 129 | static void shutdown() { |
| 130 | synchronized (LOCK) { |
| 131 | if (sInstance == null) { |
| 132 | Slog.wtf(TAG, "Already shutdown", new Exception()); |
| 133 | return; |
| 134 | } |
| 135 | synchronized (sInstance.mPendingTasks) { |
| 136 | sInstance.mShutDown = true; |
| 137 | } |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 138 | sInstance.mService.shutdown(); |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 139 | final boolean terminated; |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 140 | try { |
| 141 | terminated = sInstance.mService.awaitTermination(SHUTDOWN_TIMEOUT_MILLIS, |
| 142 | TimeUnit.MILLISECONDS); |
| 143 | } catch (InterruptedException e) { |
| 144 | Thread.currentThread().interrupt(); |
Hui Yu | a4d089f | 2019-04-02 16:19:12 -0700 | [diff] [blame] | 145 | dumpStackTraces(); |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 146 | throw new IllegalStateException(TAG + " init interrupted"); |
| 147 | } |
Hui Yu | a4d089f | 2019-04-02 16:19:12 -0700 | [diff] [blame] | 148 | if (!terminated) { |
| 149 | // dump stack must be called before shutdownNow() to collect stacktrace of threads |
| 150 | // in the thread pool. |
| 151 | dumpStackTraces(); |
| 152 | } |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 153 | final List<Runnable> unstartedRunnables = sInstance.mService.shutdownNow(); |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 154 | if (!terminated) { |
Hui Yu | a4d089f | 2019-04-02 16:19:12 -0700 | [diff] [blame] | 155 | final List<String> copy = new ArrayList<>(); |
| 156 | synchronized (sInstance.mPendingTasks) { |
| 157 | copy.addAll(sInstance.mPendingTasks); |
| 158 | } |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 159 | throw new IllegalStateException("Cannot shutdown. Unstarted tasks " |
Hui Yu | a4d089f | 2019-04-02 16:19:12 -0700 | [diff] [blame] | 160 | + unstartedRunnables + " Unfinished tasks " + copy); |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 161 | } |
Felipe Leme | 29f1ae9 | 2019-10-03 15:38:43 -0700 | [diff] [blame^] | 162 | sInstance = null; // Make eligible for GC |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 163 | Slog.d(TAG, "Shutdown successful"); |
| 164 | } |
| 165 | } |
| 166 | |
Hui Yu | a4d089f | 2019-04-02 16:19:12 -0700 | [diff] [blame] | 167 | /** |
| 168 | * A helper function to call ActivityManagerService.dumpStackTraces(). |
| 169 | */ |
| 170 | private static void dumpStackTraces() { |
| 171 | final ArrayList<Integer> pids = new ArrayList<>(); |
| 172 | pids.add(Process.myPid()); |
Hui Yu | 7e65c3f | 2019-04-16 17:03:16 -0700 | [diff] [blame] | 173 | ActivityManagerService.dumpStackTraces(pids, null, null, |
| 174 | Watchdog.getInterestingNativePids()); |
Hui Yu | a4d089f | 2019-04-02 16:19:12 -0700 | [diff] [blame] | 175 | } |
Fyodor Kupolov | e29a5a1 | 2016-12-16 16:14:17 -0800 | [diff] [blame] | 176 | } |