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