Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 19 | import android.content.Context; |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 20 | import android.database.ContentObserver; |
| 21 | import android.net.Uri; |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 22 | import android.os.Binder; |
| 23 | import android.os.Looper; |
| 24 | import android.os.ResultReceiver; |
| 25 | import android.os.ShellCallback; |
| 26 | import android.os.ShellCommand; |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 27 | import android.os.SystemProperties; |
| 28 | import android.os.UserHandle; |
| 29 | import android.provider.Settings; |
Marcin Oczeretko | d464bcf | 2018-10-19 10:46:21 +0100 | [diff] [blame] | 30 | import android.text.format.DateFormat; |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 31 | import android.util.KeyValueListParser; |
| 32 | import android.util.Slog; |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 33 | |
Marcin Oczeretko | c4c45a8 | 2018-12-06 15:09:49 +0000 | [diff] [blame] | 34 | import com.android.internal.os.AppIdToPackageMap; |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 35 | import com.android.internal.os.BackgroundThread; |
Marcin Oczeretko | c80c81a | 2018-08-30 20:15:52 +0100 | [diff] [blame] | 36 | import com.android.internal.os.CachedDeviceState; |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 37 | import com.android.internal.os.LooperStats; |
| 38 | import com.android.internal.util.DumpUtils; |
| 39 | |
| 40 | import java.io.FileDescriptor; |
| 41 | import java.io.PrintWriter; |
| 42 | import java.util.Arrays; |
| 43 | import java.util.Comparator; |
| 44 | import java.util.List; |
| 45 | |
| 46 | /** |
| 47 | * @hide Only for use within the system server. |
| 48 | */ |
| 49 | public class LooperStatsService extends Binder { |
| 50 | private static final String TAG = "LooperStatsService"; |
| 51 | private static final String LOOPER_STATS_SERVICE_NAME = "looper_stats"; |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 52 | private static final String SETTINGS_ENABLED_KEY = "enabled"; |
| 53 | private static final String SETTINGS_SAMPLING_INTERVAL_KEY = "sampling_interval"; |
Olivier Gaillard | 36b80ca | 2019-02-11 11:41:39 +0000 | [diff] [blame] | 54 | private static final String SETTINGS_TRACK_SCREEN_INTERACTIVE_KEY = "track_screen_state"; |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 55 | private static final String DEBUG_SYS_LOOPER_STATS_ENABLED = |
| 56 | "debug.sys.looper_stats_enabled"; |
Olivier Gaillard | 51f669e | 2019-02-21 16:05:27 +0000 | [diff] [blame] | 57 | private static final int DEFAULT_SAMPLING_INTERVAL = 1000; |
Olivier Gaillard | 45b6865 | 2019-03-01 13:37:47 +0000 | [diff] [blame] | 58 | private static final int DEFAULT_ENTRIES_SIZE_CAP = 1500; |
Olivier Gaillard | 2d45606 | 2019-01-23 17:56:58 +0000 | [diff] [blame] | 59 | private static final boolean DEFAULT_ENABLED = true; |
Olivier Gaillard | 36b80ca | 2019-02-11 11:41:39 +0000 | [diff] [blame] | 60 | private static final boolean DEFAULT_TRACK_SCREEN_INTERACTIVE = false; |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 61 | |
| 62 | private final Context mContext; |
| 63 | private final LooperStats mStats; |
Olivier Gaillard | 4158938 | 2019-02-04 10:42:34 +0000 | [diff] [blame] | 64 | // Default should be false so that the first call to #setEnabled installed the looper observer. |
| 65 | private boolean mEnabled = false; |
Olivier Gaillard | 36b80ca | 2019-02-11 11:41:39 +0000 | [diff] [blame] | 66 | private boolean mTrackScreenInteractive = false; |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 67 | |
| 68 | private LooperStatsService(Context context, LooperStats stats) { |
| 69 | this.mContext = context; |
| 70 | this.mStats = stats; |
| 71 | } |
| 72 | |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 73 | private void initFromSettings() { |
| 74 | final KeyValueListParser parser = new KeyValueListParser(','); |
| 75 | |
| 76 | try { |
| 77 | parser.setString(Settings.Global.getString(mContext.getContentResolver(), |
| 78 | Settings.Global.LOOPER_STATS)); |
| 79 | } catch (IllegalArgumentException e) { |
| 80 | Slog.e(TAG, "Bad looper_stats settings", e); |
| 81 | } |
| 82 | |
| 83 | setSamplingInterval( |
| 84 | parser.getInt(SETTINGS_SAMPLING_INTERVAL_KEY, DEFAULT_SAMPLING_INTERVAL)); |
Olivier Gaillard | 36b80ca | 2019-02-11 11:41:39 +0000 | [diff] [blame] | 85 | setTrackScreenInteractive( |
| 86 | parser.getBoolean(SETTINGS_TRACK_SCREEN_INTERACTIVE_KEY, |
| 87 | DEFAULT_TRACK_SCREEN_INTERACTIVE)); |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 88 | // Manually specified value takes precedence over Settings. |
| 89 | setEnabled(SystemProperties.getBoolean( |
| 90 | DEBUG_SYS_LOOPER_STATS_ENABLED, |
| 91 | parser.getBoolean(SETTINGS_ENABLED_KEY, DEFAULT_ENABLED))); |
| 92 | } |
| 93 | |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 94 | @Override |
| 95 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 96 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
| 97 | (new LooperShellCommand()).exec(this, in, out, err, args, callback, resultReceiver); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 102 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Marcin Oczeretko | c4c45a8 | 2018-12-06 15:09:49 +0000 | [diff] [blame] | 103 | AppIdToPackageMap packageMap = AppIdToPackageMap.getSnapshot(); |
Marcin Oczeretko | d464bcf | 2018-10-19 10:46:21 +0100 | [diff] [blame] | 104 | pw.print("Start time: "); |
| 105 | pw.println(DateFormat.format("yyyy-MM-dd HH:mm:ss", mStats.getStartTimeMillis())); |
Marcin Oczeretko | 6a2e524 | 2018-11-28 11:08:50 +0000 | [diff] [blame] | 106 | pw.print("On battery time (ms): "); |
| 107 | pw.println(mStats.getBatteryTimeMillis()); |
Marcin Oczeretko | d464bcf | 2018-10-19 10:46:21 +0100 | [diff] [blame] | 108 | final List<LooperStats.ExportedEntry> entries = mStats.getEntries(); |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 109 | entries.sort(Comparator |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 110 | .comparing((LooperStats.ExportedEntry entry) -> entry.workSourceUid) |
| 111 | .thenComparing(entry -> entry.threadName) |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 112 | .thenComparing(entry -> entry.handlerClassName) |
| 113 | .thenComparing(entry -> entry.messageName)); |
| 114 | String header = String.join(",", Arrays.asList( |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 115 | "work_source_uid", |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 116 | "thread_name", |
| 117 | "handler_class", |
| 118 | "message_name", |
Marcin Oczeretko | c80c81a | 2018-08-30 20:15:52 +0100 | [diff] [blame] | 119 | "is_interactive", |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 120 | "message_count", |
| 121 | "recorded_message_count", |
| 122 | "total_latency_micros", |
| 123 | "max_latency_micros", |
| 124 | "total_cpu_micros", |
| 125 | "max_cpu_micros", |
Marcin Oczeretko | 4427272 | 2018-09-19 11:01:32 +0100 | [diff] [blame] | 126 | "recorded_delay_message_count", |
| 127 | "total_delay_millis", |
| 128 | "max_delay_millis", |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 129 | "exception_count")); |
| 130 | pw.println(header); |
| 131 | for (LooperStats.ExportedEntry entry : entries) { |
Marcin Oczeretko | 9450171 | 2018-12-17 18:03:45 +0000 | [diff] [blame] | 132 | if (entry.messageName.startsWith(LooperStats.DEBUG_ENTRY_PREFIX)) { |
| 133 | // Do not dump debug entries. |
| 134 | continue; |
| 135 | } |
Marcin Oczeretko | 4427272 | 2018-09-19 11:01:32 +0100 | [diff] [blame] | 136 | pw.printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", |
Marcin Oczeretko | c4c45a8 | 2018-12-06 15:09:49 +0000 | [diff] [blame] | 137 | packageMap.mapUid(entry.workSourceUid), |
Marcin Oczeretko | 4427272 | 2018-09-19 11:01:32 +0100 | [diff] [blame] | 138 | entry.threadName, |
| 139 | entry.handlerClassName, |
| 140 | entry.messageName, |
| 141 | entry.isInteractive, |
| 142 | entry.messageCount, |
| 143 | entry.recordedMessageCount, |
| 144 | entry.totalLatencyMicros, |
| 145 | entry.maxLatencyMicros, |
| 146 | entry.cpuUsageMicros, |
| 147 | entry.maxCpuUsageMicros, |
| 148 | entry.recordedDelayMessageCount, |
| 149 | entry.delayMillis, |
| 150 | entry.maxDelayMillis, |
| 151 | entry.exceptionCount); |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | private void setEnabled(boolean enabled) { |
| 156 | if (mEnabled != enabled) { |
| 157 | mEnabled = enabled; |
| 158 | mStats.reset(); |
Marcin Oczeretko | d32a5135 | 2018-11-20 18:11:37 +0000 | [diff] [blame] | 159 | mStats.setAddDebugEntries(enabled); |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 160 | Looper.setObserver(enabled ? mStats : null); |
| 161 | } |
| 162 | } |
| 163 | |
Olivier Gaillard | 36b80ca | 2019-02-11 11:41:39 +0000 | [diff] [blame] | 164 | private void setTrackScreenInteractive(boolean enabled) { |
| 165 | if (mTrackScreenInteractive != enabled) { |
| 166 | mTrackScreenInteractive = enabled; |
| 167 | mStats.reset(); |
| 168 | } |
| 169 | } |
| 170 | |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 171 | private void setSamplingInterval(int samplingInterval) { |
Marcin Oczeretko | 37d4109 | 2018-10-02 15:35:44 +0100 | [diff] [blame] | 172 | if (samplingInterval > 0) { |
| 173 | mStats.setSamplingInterval(samplingInterval); |
| 174 | } else { |
| 175 | Slog.w(TAG, "Ignored invalid sampling interval (value must be positive): " |
| 176 | + samplingInterval); |
| 177 | } |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 180 | /** |
| 181 | * Manages the lifecycle of LooperStatsService within System Server. |
| 182 | */ |
| 183 | public static class Lifecycle extends SystemService { |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 184 | private final SettingsObserver mSettingsObserver; |
| 185 | private final LooperStatsService mService; |
| 186 | private final LooperStats mStats; |
| 187 | |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 188 | public Lifecycle(Context context) { |
| 189 | super(context); |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 190 | mStats = new LooperStats(DEFAULT_SAMPLING_INTERVAL, DEFAULT_ENTRIES_SIZE_CAP); |
| 191 | mService = new LooperStatsService(getContext(), mStats); |
| 192 | mSettingsObserver = new SettingsObserver(mService); |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void onStart() { |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 197 | publishLocalService(LooperStats.class, mStats); |
Marcin Oczeretko | 1d5d0ec | 2018-08-30 13:11:27 +0100 | [diff] [blame] | 198 | publishBinderService(LOOPER_STATS_SERVICE_NAME, mService); |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 199 | } |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 200 | |
| 201 | @Override |
| 202 | public void onBootPhase(int phase) { |
| 203 | if (SystemService.PHASE_SYSTEM_SERVICES_READY == phase) { |
| 204 | mService.initFromSettings(); |
| 205 | Uri settingsUri = Settings.Global.getUriFor(Settings.Global.LOOPER_STATS); |
| 206 | getContext().getContentResolver().registerContentObserver( |
| 207 | settingsUri, false, mSettingsObserver, UserHandle.USER_SYSTEM); |
Marcin Oczeretko | c80c81a | 2018-08-30 20:15:52 +0100 | [diff] [blame] | 208 | mStats.setDeviceState(getLocalService(CachedDeviceState.Readonly.class)); |
Marcin Oczeretko | 3680ae6 | 2018-08-23 16:33:34 +0100 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | private static class SettingsObserver extends ContentObserver { |
| 214 | private final LooperStatsService mService; |
| 215 | |
| 216 | SettingsObserver(LooperStatsService service) { |
| 217 | super(BackgroundThread.getHandler()); |
| 218 | mService = service; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void onChange(boolean selfChange, Uri uri, int userId) { |
| 223 | mService.initFromSettings(); |
| 224 | } |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | private class LooperShellCommand extends ShellCommand { |
| 228 | @Override |
| 229 | public int onCommand(String cmd) { |
| 230 | if ("enable".equals(cmd)) { |
| 231 | setEnabled(true); |
| 232 | return 0; |
| 233 | } else if ("disable".equals(cmd)) { |
| 234 | setEnabled(false); |
| 235 | return 0; |
| 236 | } else if ("reset".equals(cmd)) { |
| 237 | mStats.reset(); |
| 238 | return 0; |
Marcin Oczeretko | 86d07aa | 2018-09-14 11:51:08 +0100 | [diff] [blame] | 239 | } else if ("sampling_interval".equals(cmd)) { |
| 240 | int sampling = Integer.parseUnsignedInt(getNextArgRequired()); |
| 241 | setSamplingInterval(sampling); |
| 242 | return 0; |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 243 | } else { |
| 244 | return handleDefaultCommands(cmd); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void onHelp() { |
| 250 | final PrintWriter pw = getOutPrintWriter(); |
| 251 | pw.println(LOOPER_STATS_SERVICE_NAME + " commands:"); |
Marcin Oczeretko | 86d07aa | 2018-09-14 11:51:08 +0100 | [diff] [blame] | 252 | pw.println(" enable: Enable collecting stats."); |
| 253 | pw.println(" disable: Disable collecting stats."); |
| 254 | pw.println(" sampling_interval: Change the sampling interval."); |
| 255 | pw.println(" reset: Reset stats."); |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |