blob: 765e34788ac8566f9a6fa64450af9b7f10311daa [file] [log] [blame]
Adrian Roos111aff92017-09-27 18:11:46 +02001/*
2 * Copyright (C) 2017 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.server.wm;
18
Vishnu Nairab250032017-11-21 07:32:43 -080019import static android.os.Build.IS_USER;
Nataniel Borges5a38bad2019-01-03 10:32:03 -080020
Yi Jin6c6e9ca2018-03-20 16:53:35 -070021import static com.android.server.wm.WindowManagerTraceFileProto.ENTRY;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070022import static com.android.server.wm.WindowManagerTraceProto.ELAPSED_REALTIME_NANOS;
23import static com.android.server.wm.WindowManagerTraceProto.WHERE;
24import static com.android.server.wm.WindowManagerTraceProto.WINDOW_MANAGER_SERVICE;
Adrian Roos111aff92017-09-27 18:11:46 +020025
Nataniel Borges5a38bad2019-01-03 10:32:03 -080026import android.annotation.Nullable;
Adrian Roos111aff92017-09-27 18:11:46 +020027import android.os.ShellCommand;
28import android.os.SystemClock;
29import android.os.Trace;
30import android.util.Log;
31import android.util.proto.ProtoOutputStream;
Nataniel Borges48d9aa92019-02-01 14:45:55 -080032import android.view.Choreographer;
33
Adrian Roos111aff92017-09-27 18:11:46 +020034import java.io.File;
Adrian Roos111aff92017-09-27 18:11:46 +020035import java.io.IOException;
Adrian Roos111aff92017-09-27 18:11:46 +020036import java.io.PrintWriter;
Adrian Roos111aff92017-09-27 18:11:46 +020037
38/**
39 * A class that allows window manager to dump its state continuously to a trace file, such that a
40 * time series of window manager state can be analyzed after the fact.
41 */
42class WindowTracing {
43
Nataniel Borges5a38bad2019-01-03 10:32:03 -080044 /**
45 * Maximum buffer size, currently defined as 512 KB
46 * Size was experimentally defined to fit between 100 to 150 elements.
47 */
Nataniel Borges7d37ce22019-02-05 10:07:02 -080048 private static final int BUFFER_CAPACITY_CRITICAL = 512 * 1024;
49 private static final int BUFFER_CAPACITY_TRIM = 2048 * 1024;
50 private static final int BUFFER_CAPACITY_ALL = 4096 * 1024;
51 private static final String TRACE_FILENAME = "/data/misc/wmtrace/wm_trace.pb";
Adrian Roos111aff92017-09-27 18:11:46 +020052 private static final String TAG = "WindowTracing";
Adrian Roos111aff92017-09-27 18:11:46 +020053
Nataniel Borges48d9aa92019-02-01 14:45:55 -080054 private final WindowManagerService mService;
55 private final Choreographer mChoreographer;
56 private final WindowManagerGlobalLock mGlobalLock;
57
Nataniel Borges7d37ce22019-02-05 10:07:02 -080058 private final Object mEnabledLock = new Object();
59 private final File mTraceFile;
60 private final WindowTraceBuffer mBuffer;
61 private final Choreographer.FrameCallback mFrameCallback = (frameTimeNanos) ->
62 log("onFrame" /* where */);
Nataniel Borges5a38bad2019-01-03 10:32:03 -080063
Nataniel Borges7d37ce22019-02-05 10:07:02 -080064 private @WindowTraceLogLevel int mLogLevel = WindowTraceLogLevel.TRIM;
65 private boolean mLogOnFrame = false;
Adrian Roos111aff92017-09-27 18:11:46 +020066 private boolean mEnabled;
67 private volatile boolean mEnabledLockFree;
Nataniel Borges48d9aa92019-02-01 14:45:55 -080068 private boolean mScheduled;
Adrian Roos111aff92017-09-27 18:11:46 +020069
Nataniel Borges7d37ce22019-02-05 10:07:02 -080070 static WindowTracing createDefaultAndStartLooper(WindowManagerService service,
71 Choreographer choreographer) {
72 File file = new File(TRACE_FILENAME);
73 return new WindowTracing(file, service, choreographer, BUFFER_CAPACITY_TRIM);
Nataniel Borges48d9aa92019-02-01 14:45:55 -080074 }
75
Nataniel Borges7d37ce22019-02-05 10:07:02 -080076 private WindowTracing(File file, WindowManagerService service, Choreographer choreographer,
77 int bufferCapacity) {
78 this(file, service, choreographer, service.mGlobalLock, bufferCapacity);
79 }
Nataniel Borges48d9aa92019-02-01 14:45:55 -080080
Nataniel Borges7d37ce22019-02-05 10:07:02 -080081 WindowTracing(File file, WindowManagerService service, Choreographer choreographer,
82 WindowManagerGlobalLock globalLock, int bufferCapacity) {
Nataniel Borges48d9aa92019-02-01 14:45:55 -080083 mChoreographer = choreographer;
84 mService = service;
85 mGlobalLock = globalLock;
Nataniel Borges7d37ce22019-02-05 10:07:02 -080086 mTraceFile = file;
87 mBuffer = new WindowTraceBuffer(bufferCapacity);
88 setLogLevel(WindowTraceLogLevel.TRIM, null /* pw */);
Adrian Roos111aff92017-09-27 18:11:46 +020089 }
90
Nataniel Borges7d37ce22019-02-05 10:07:02 -080091 void startTrace(@Nullable PrintWriter pw) {
Nataniel Borges5a38bad2019-01-03 10:32:03 -080092 if (IS_USER) {
Vishnu Nairab250032017-11-21 07:32:43 -080093 logAndPrintln(pw, "Error: Tracing is not supported on user builds.");
94 return;
95 }
Nataniel Borges7d37ce22019-02-05 10:07:02 -080096 synchronized (mEnabledLock) {
97 logAndPrintln(pw, "Start tracing to " + mTraceFile + ".");
98 mBuffer.resetBuffer();
Adrian Roos111aff92017-09-27 18:11:46 +020099 mEnabled = mEnabledLockFree = true;
100 }
Nataniel Borges8a5106c2019-03-06 11:31:34 -0800101 log("trace.enable");
Adrian Roos111aff92017-09-27 18:11:46 +0200102 }
103
Nataniel Borges9e260462019-02-27 17:02:20 -0800104 /**
105 * Stops the trace and write the current buffer to disk
106 * @param pw Print writer
107 */
Vishnu Nairc9615042018-01-09 07:23:31 -0800108 void stopTrace(@Nullable PrintWriter pw) {
Nataniel Borges9e260462019-02-27 17:02:20 -0800109 stopTrace(pw, true /* writeToFile */);
110 }
111
112 /**
113 * Stops the trace
114 * @param pw Print writer
115 * @param writeToFile If the current buffer should be written to disk or not
116 */
117 void stopTrace(@Nullable PrintWriter pw, boolean writeToFile) {
Nataniel Borges5a38bad2019-01-03 10:32:03 -0800118 if (IS_USER) {
Vishnu Nairab250032017-11-21 07:32:43 -0800119 logAndPrintln(pw, "Error: Tracing is not supported on user builds.");
120 return;
121 }
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800122 synchronized (mEnabledLock) {
123 logAndPrintln(pw, "Stop tracing to " + mTraceFile + ". Waiting for traces to flush.");
Adrian Roos111aff92017-09-27 18:11:46 +0200124 mEnabled = mEnabledLockFree = false;
Nataniel Borges5a38bad2019-01-03 10:32:03 -0800125
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800126 if (mEnabled) {
127 logAndPrintln(pw, "ERROR: tracing was re-enabled while waiting for flush.");
128 throw new IllegalStateException("tracing enabled while waiting for flush.");
Adrian Roos111aff92017-09-27 18:11:46 +0200129 }
Nataniel Borges9e260462019-02-27 17:02:20 -0800130 if (writeToFile) {
131 writeTraceToFileLocked();
132 logAndPrintln(pw, "Trace written to " + mTraceFile + ".");
133 }
Adrian Roos111aff92017-09-27 18:11:46 +0200134 }
135 }
136
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800137 private void setLogLevel(@WindowTraceLogLevel int logLevel, PrintWriter pw) {
138 logAndPrintln(pw, "Setting window tracing log level to " + logLevel);
139 mLogLevel = logLevel;
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800140
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800141 switch (logLevel) {
142 case WindowTraceLogLevel.ALL: {
143 setBufferCapacity(BUFFER_CAPACITY_ALL, pw);
144 break;
145 }
146 case WindowTraceLogLevel.TRIM: {
147 setBufferCapacity(BUFFER_CAPACITY_TRIM, pw);
148 break;
149 }
150 case WindowTraceLogLevel.CRITICAL: {
151 setBufferCapacity(BUFFER_CAPACITY_CRITICAL, pw);
152 break;
153 }
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800154 }
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800155 }
156
157 private void setLogFrequency(boolean onFrame, PrintWriter pw) {
158 logAndPrintln(pw, "Setting window tracing log frequency to "
159 + ((onFrame) ? "frame" : "transaction"));
160 mLogOnFrame = onFrame;
161 }
162
163 private void setBufferCapacity(int capacity, PrintWriter pw) {
164 logAndPrintln(pw, "Setting window tracing buffer capacity to " + capacity + "bytes");
165 mBuffer.setCapacity(capacity);
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800166 }
167
Adrian Roos111aff92017-09-27 18:11:46 +0200168 boolean isEnabled() {
169 return mEnabledLockFree;
170 }
171
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800172 int onShellCommand(ShellCommand shell) {
Adrian Roos111aff92017-09-27 18:11:46 +0200173 PrintWriter pw = shell.getOutPrintWriter();
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800174 String cmd = shell.getNextArgRequired();
175 switch (cmd) {
176 case "start":
177 startTrace(pw);
178 return 0;
179 case "stop":
180 stopTrace(pw);
181 return 0;
182 case "status":
183 logAndPrintln(pw, getStatus());
184 return 0;
185 case "frame":
186 setLogFrequency(true /* onFrame */, pw);
187 mBuffer.resetBuffer();
188 return 0;
189 case "transaction":
190 setLogFrequency(false /* onFrame */, pw);
191 mBuffer.resetBuffer();
192 return 0;
193 case "level":
194 String logLevelStr = shell.getNextArgRequired().toLowerCase();
195 switch (logLevelStr) {
196 case "all": {
197 setLogLevel(WindowTraceLogLevel.ALL, pw);
198 break;
199 }
200 case "trim": {
201 setLogLevel(WindowTraceLogLevel.TRIM, pw);
202 break;
203 }
204 case "critical": {
205 setLogLevel(WindowTraceLogLevel.CRITICAL, pw);
206 break;
207 }
208 default: {
209 setLogLevel(WindowTraceLogLevel.TRIM, pw);
210 break;
211 }
212 }
213 mBuffer.resetBuffer();
214 return 0;
215 case "size":
216 setBufferCapacity(Integer.parseInt(shell.getNextArgRequired()) * 1024, pw);
217 mBuffer.resetBuffer();
218 return 0;
219 default:
220 pw.println("Unknown command: " + cmd);
221 pw.println("Window manager trace options:");
222 pw.println(" start: Start logging");
223 pw.println(" stop: Stop logging");
224 pw.println(" frame: Log trace once per frame");
225 pw.println(" transaction: Log each transaction");
226 pw.println(" size: Set the maximum log size (in KB)");
Nataniel Borgesc0e18bf2019-02-25 10:45:27 -0800227 pw.println(" status: Print trace status");
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800228 pw.println(" level [lvl]: Set the log level between");
229 pw.println(" lvl may be one of:");
230 pw.println(" critical: Only visible windows with reduced information");
231 pw.println(" trim: All windows with reduced");
232 pw.println(" all: All window and information");
233 return -1;
Adrian Roos111aff92017-09-27 18:11:46 +0200234 }
235 }
236
Nataniel Borges24325ce2019-02-05 15:43:29 -0800237 String getStatus() {
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800238 return "Status: "
239 + ((isEnabled()) ? "Enabled" : "Disabled")
240 + "\n"
241 + "Log level: "
242 + mLogLevel
243 + "\n"
244 + mBuffer.getStatus();
245 }
246
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800247 /**
248 * If tracing is enabled, log the current state or schedule the next frame to be logged,
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800249 * according to {@link #mLogOnFrame}.
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800250 *
251 * @param where Logging point descriptor
252 */
253 void logState(String where) {
Adrian Roos111aff92017-09-27 18:11:46 +0200254 if (!isEnabled()) {
255 return;
256 }
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800257
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800258 if (mLogOnFrame) {
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800259 schedule();
260 } else {
261 log(where);
262 }
263 }
Nataniel Borges023ecb52019-01-16 14:15:43 -0800264
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800265 /**
266 * Schedule the log to trace the next frame
267 */
268 private void schedule() {
269 if (mScheduled) {
270 return;
271 }
272
273 mScheduled = true;
274 mChoreographer.postFrameCallback(mFrameCallback);
275 }
276
277 /**
278 * Write the current frame to the buffer
279 *
280 * @param where Logging point descriptor
281 */
282 private void log(String where) {
283 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "traceStateLocked");
284 try {
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800285 ProtoOutputStream os = new ProtoOutputStream();
286 long tokenOuter = os.start(ENTRY);
287 os.write(ELAPSED_REALTIME_NANOS, SystemClock.elapsedRealtimeNanos());
288 os.write(WHERE, where);
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800289
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800290 long tokenInner = os.start(WINDOW_MANAGER_SERVICE);
291 synchronized (mGlobalLock) {
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800292 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "writeToProtoLocked");
293 try {
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800294 mService.writeToProtoLocked(os, mLogLevel);
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800295 } finally {
296 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
297 }
Nataniel Borges023ecb52019-01-16 14:15:43 -0800298 }
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800299 os.end(tokenInner);
300 os.end(tokenOuter);
301 mBuffer.add(os);
302 mScheduled = false;
Nataniel Borges48d9aa92019-02-01 14:45:55 -0800303 } catch (Exception e) {
304 Log.wtf(TAG, "Exception while tracing state", e);
Adrian Roos111aff92017-09-27 18:11:46 +0200305 } finally {
306 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
307 }
Adrian Roos111aff92017-09-27 18:11:46 +0200308 }
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800309
310 /**
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800311 * Writes the trace buffer to new file for the bugreport.
312 *
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800313 * This method is synchronized with {@code #startTrace(PrintWriter)} and
314 * {@link #stopTrace(PrintWriter)}.
315 */
316 void writeTraceToFile() {
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800317 synchronized (mEnabledLock) {
Nataniel Borges98d92aa2019-01-03 14:22:44 -0800318 writeTraceToFileLocked();
319 }
320 }
Nataniel Borges7d37ce22019-02-05 10:07:02 -0800321
322 private void logAndPrintln(@Nullable PrintWriter pw, String msg) {
323 Log.i(TAG, msg);
324 if (pw != null) {
325 pw.println(msg);
326 pw.flush();
327 }
328 }
329
330 /**
331 * Writes the trace buffer to disk. This method has no internal synchronization and should be
332 * externally synchronized
333 */
334 private void writeTraceToFileLocked() {
335 try {
336 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "writeTraceToFileLocked");
337 mBuffer.writeTraceToFile(mTraceFile);
338 } catch (IOException e) {
339 Log.e(TAG, "Unable to write buffer to file", e);
340 } finally {
341 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
342 }
343 }
344}