Debian Codegraph @

core/java/com/android/internal/inputmethod/ImeTracingServerImpl.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 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.internal.inputmethod;
18
19 import static android.os.Build.IS_USER;
20
21 import android.annotation.Nullable;
22 import android.os.RemoteException;
23 import android.os.ServiceManager.ServiceNotFoundException;
24 import android.util.Log;
25 import android.util.proto.ProtoOutputStream;
26 import android.view.inputmethod.InputMethodEditorTraceProto.InputMethodClientsTraceFileProto;
27 import android.view.inputmethod.InputMethodEditorTraceProto.InputMethodManagerServiceTraceFileProto;
28 import android.view.inputmethod.InputMethodEditorTraceProto.InputMethodServiceTraceFileProto;
29 import android.view.inputmethod.InputMethodManager;
30
31 import com.android.internal.annotations.GuardedBy;
32 import com.android.internal.util.TraceBuffer;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.io.PrintWriter;
37
38 /**
39 * An implementation of {@link ImeTracing} for the system_server process.
40 */
41 class ImeTracingServerImpl extends ImeTracing {
42 private static final String TRACE_DIRNAME = "/data/misc/wmtrace/";
43 private static final String TRACE_FILENAME_CLIENTS = "ime_trace_clients.winscope";
44 private static final String TRACE_FILENAME_IMS = "ime_trace_service.winscope";
45 private static final String TRACE_FILENAME_IMMS = "ime_trace_managerservice.winscope";
46 private static final int BUFFER_CAPACITY = 4096 * 1024;
47
48 // Needed for winscope to auto-detect the dump type. Explained further in
49 // core.proto.android.view.inputmethod.inputmethodeditortrace.proto.
50 // This magic number corresponds to InputMethodClientsTraceFileProto.
51 private static final long MAGIC_NUMBER_CLIENTS_VALUE =
52 ((long) InputMethodClientsTraceFileProto.MAGIC_NUMBER_H << 32)
53 | InputMethodClientsTraceFileProto.MAGIC_NUMBER_L;
54 // This magic number corresponds to InputMethodServiceTraceFileProto.
55 private static final long MAGIC_NUMBER_IMS_VALUE =
56 ((long) InputMethodServiceTraceFileProto.MAGIC_NUMBER_H << 32)
57 | InputMethodServiceTraceFileProto.MAGIC_NUMBER_L;
58 // This magic number corresponds to InputMethodManagerServiceTraceFileProto.
59 private static final long MAGIC_NUMBER_IMMS_VALUE =
60 ((long) InputMethodManagerServiceTraceFileProto.MAGIC_NUMBER_H << 32)
61 | InputMethodManagerServiceTraceFileProto.MAGIC_NUMBER_L;
62
63 private final TraceBuffer mBufferClients;
64 private final File mTraceFileClients;
65 private final TraceBuffer mBufferIms;
66 private final File mTraceFileIms;
67 private final TraceBuffer mBufferImms;
68 private final File mTraceFileImms;
69
70 private final Object mEnabledLock = new Object();
71
72 ImeTracingServerImpl() throws ServiceNotFoundException {
73 mBufferClients = new TraceBuffer<>(BUFFER_CAPACITY);
74 mTraceFileClients = new File(TRACE_DIRNAME + TRACE_FILENAME_CLIENTS);
75 mBufferIms = new TraceBuffer<>(BUFFER_CAPACITY);
76 mTraceFileIms = new File(TRACE_DIRNAME + TRACE_FILENAME_IMS);
77 mBufferImms = new TraceBuffer<>(BUFFER_CAPACITY);
78 mTraceFileImms = new File(TRACE_DIRNAME + TRACE_FILENAME_IMMS);
79 }
80
81 /**
82 * The provided dump is added to the corresponding dump buffer:
83 * {@link ImeTracingServerImpl#mBufferClients} or {@link ImeTracingServerImpl#mBufferIms}.
84 *
85 * @param proto dump to be added to the buffer
86 */
87 @Override
88 public void addToBuffer(ProtoOutputStream proto, int source) {
89 if (isAvailable() && isEnabled()) {
90 switch (source) {
91 case IME_TRACING_FROM_CLIENT:
92 mBufferClients.add(proto);
93 return;
94 case IME_TRACING_FROM_IMS:
95 mBufferIms.add(proto);
96 return;
97 case IME_TRACING_FROM_IMMS:
98 mBufferImms.add(proto);
99 return;
100 default:
101 // Source not recognised.
102 Log.w(TAG, "Request to add to buffer, but source not recognised.");
103 }
104 }
105 }
106
107 @Override
108 public void triggerClientDump(String where, InputMethodManager immInstance,
109 @Nullable byte[] icProto) {
110 // Intentionally left empty, this is implemented in ImeTracingClientImpl
111 }
112
113 @Override
114 public void triggerServiceDump(String where, ServiceDumper dumper, @Nullable byte[] icProto) {
115 // Intentionally left empty, this is implemented in ImeTracingClientImpl
116 }
117
118 @Override
119 public void triggerManagerServiceDump(String where) {
120 if (!isEnabled() || !isAvailable()) {
121 return;
122 }
123
124 synchronized (mDumpInProgressLock) {
125 if (mDumpInProgress) {
126 return;
127 }
128 mDumpInProgress = true;
129 }
130
131 try {
132 sendToService(null, IME_TRACING_FROM_IMMS, where);
133 } catch (RemoteException e) {
134 Log.e(TAG, "Exception while sending ime-related manager service dump to server", e);
135 } finally {
136 mDumpInProgress = false;
137 }
138 }
139
140 private void writeTracesToFilesLocked() {
141 try {
142 ProtoOutputStream clientsProto = new ProtoOutputStream();
143 clientsProto.write(InputMethodClientsTraceFileProto.MAGIC_NUMBER,
144 MAGIC_NUMBER_CLIENTS_VALUE);
145 mBufferClients.writeTraceToFile(mTraceFileClients, clientsProto);
146
147 ProtoOutputStream imsProto = new ProtoOutputStream();
148 imsProto.write(InputMethodServiceTraceFileProto.MAGIC_NUMBER, MAGIC_NUMBER_IMS_VALUE);
149 mBufferIms.writeTraceToFile(mTraceFileIms, imsProto);
150
151 ProtoOutputStream immsProto = new ProtoOutputStream();
152 immsProto.write(InputMethodManagerServiceTraceFileProto.MAGIC_NUMBER,
153 MAGIC_NUMBER_IMMS_VALUE);
154 mBufferImms.writeTraceToFile(mTraceFileImms, immsProto);
155
156 resetBuffers();
157 } catch (IOException e) {
158 Log.e(TAG, "Unable to write buffer to file", e);
159 }
160 }
161
162 @GuardedBy("mEnabledLock")
163 @Override
164 public void startTrace(@Nullable PrintWriter pw) {
165 if (IS_USER) {
166 Log.w(TAG, "Warn: Tracing is not supported on user builds.");
167 return;
168 }
169
170 synchronized (mEnabledLock) {
171 if (isAvailable() && isEnabled()) {
172 Log.w(TAG, "Warn: Tracing is already started.");
173 return;
174 }
175
176 logAndPrintln(pw, "Starting tracing in " + TRACE_DIRNAME + ": " + TRACE_FILENAME_CLIENTS
177 + ", " + TRACE_FILENAME_IMS + ", " + TRACE_FILENAME_IMMS);
178 sEnabled = true;
179 resetBuffers();
180 }
181 }
182
183 @Override
184 public void stopTrace(@Nullable PrintWriter pw) {
185 if (IS_USER) {
186 Log.w(TAG, "Warn: Tracing is not supported on user builds.");
187 return;
188 }
189
190 synchronized (mEnabledLock) {
191 if (!isAvailable() || !isEnabled()) {
192 Log.w(TAG, "Warn: Tracing is not available or not started.");
193 return;
194 }
195
196 logAndPrintln(pw, "Stopping tracing and writing traces in " + TRACE_DIRNAME + ": "
197 + TRACE_FILENAME_CLIENTS + ", " + TRACE_FILENAME_IMS + ", "
198 + TRACE_FILENAME_IMMS);
199 sEnabled = false;
200 writeTracesToFilesLocked();
201 }
202 }
203
204 /**
205 * {@inheritDoc}
206 */
207 @Override
208 public void saveForBugreport(@Nullable PrintWriter pw) {
209 if (IS_USER) {
210 return;
211 }
212 synchronized (mEnabledLock) {
213 if (!isAvailable() || !isEnabled()) {
214 return;
215 }
216 // Temporarily stop accepting logs from trace event providers. There is a small chance
217 // that we may drop some trace events while writing the file, but we currently need to
218 // live with that. Note that addToBuffer() also has a bug that it doesn't do
219 // read-acquire so flipping sEnabled here doesn't even guarantee that addToBuffer() will
220 // temporarily stop accepting incoming events...
221 // TODO(b/175761228): Implement atomic snapshot to avoid downtime.
222 // TODO(b/175761228): Fix synchronization around sEnabled.
223 sEnabled = false;
224 logAndPrintln(pw, "Writing traces in " + TRACE_DIRNAME + ": "
225 + TRACE_FILENAME_CLIENTS + ", " + TRACE_FILENAME_IMS + ", "
226 + TRACE_FILENAME_IMMS);
227 writeTracesToFilesLocked();
228 sEnabled = true;
229 }
230 }
231
232 private void resetBuffers() {
233 mBufferClients.resetBuffer();
234 mBufferIms.resetBuffer();
235 mBufferImms.resetBuffer();
236 }
237 }